aboutsummaryrefslogtreecommitdiffstats
path: root/common/vecmat.h
blob: 1ecc4b7cb107d393d022aec911e35c77c7f2db44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef COMMON_VECMAT_H
#define COMMON_VECMAT_H

#include <cmath>
#include <array>
#include <algorithm>

#include "math_defs.h"

namespace alu {

class Vector {
    alignas(16) std::array<float,4> mVals{};

public:
    constexpr Vector() noexcept = default;
    constexpr Vector(float a, float b, float c, float d) noexcept
      : mVals{{a, b, c, d}}
    { }
    Vector(const Vector &rhs) noexcept
    { std::copy(rhs.mVals.begin(), rhs.mVals.end(), mVals.begin()); }

    Vector& operator=(const Vector &rhs) noexcept
    {
        std::copy(rhs.mVals.begin(), rhs.mVals.end(), mVals.begin());
        return *this;
    }

    float& operator[](size_t idx) noexcept { return mVals[idx]; }
    constexpr const float& operator[](size_t idx) const noexcept { return mVals[idx]; }

    Vector& operator+=(const Vector &rhs) noexcept
    {
        mVals[0] += rhs.mVals[0];
        mVals[1] += rhs.mVals[1];
        mVals[2] += rhs.mVals[2];
        mVals[3] += rhs.mVals[3];
        return *this;
    }

    float normalize()
    {
        const float length{std::sqrt(mVals[0]*mVals[0] + mVals[1]*mVals[1] + mVals[2]*mVals[2])};
        if(length > FLT_EPSILON)
        {
            float inv_length = 1.0f/length;
            mVals[0] *= inv_length;
            mVals[1] *= inv_length;
            mVals[2] *= inv_length;
            return length;
        }
        mVals[0] = mVals[1] = mVals[2] = 0.0f;
        return 0.0f;
    }
};

class Matrix {
    alignas(16) std::array<std::array<float,4>,4> mVals{};

public:
    constexpr Matrix() noexcept = default;
    constexpr Matrix(float aa, float ab, float ac, float ad,
                     float ba, float bb, float bc, float bd,
                     float ca, float cb, float cc, float cd,
                     float da, float db, float dc, float dd) noexcept
      : mVals{{{{aa, ab, ac, ad}}, {{ba, bb, bc, bd}}, {{ca, cb, cc, cd}}, {{da, db, dc, dd}}}}
    { }
    Matrix(const Matrix &rhs) noexcept
    { std::copy(rhs.mVals.begin(), rhs.mVals.end(), mVals.begin()); }

    Matrix& operator=(const Matrix &rhs) noexcept
    {
        std::copy(rhs.mVals.begin(), rhs.mVals.end(), mVals.begin());
        return *this;
    }

    std::array<float,4>& operator[](size_t idx) noexcept { return mVals[idx]; }
    constexpr const std::array<float,4>& operator[](size_t idx) const noexcept { return mVals[idx]; }

    void setRow(size_t idx, float a, float b, float c, float d) noexcept
    {
        mVals[idx][0] = a;
        mVals[idx][1] = b;
        mVals[idx][2] = c;
        mVals[idx][3] = d;
    }

    static const Matrix &Identity() noexcept
    {
        static constexpr Matrix identity{
            1.0f, 0.0f, 0.0f, 0.0f,
            0.0f, 1.0f, 0.0f, 0.0f,
            0.0f, 0.0f, 1.0f, 0.0f,
            0.0f, 0.0f, 0.0f, 1.0f
        };
        return identity;
    }
};

} // namespace alu

#endif /* COMMON_VECMAT_H */