Release 1.20.1
[openal-soft.git] / common / vecmat.h
blob7186ec6c01936c29627ae9722fe2aaf429cd87fa
1 #ifndef COMMON_VECMAT_H
2 #define COMMON_VECMAT_H
4 #include <array>
5 #include <cmath>
6 #include <cstddef>
7 #include <limits>
10 namespace alu {
12 class Vector {
13 alignas(16) std::array<float,4> mVals;
15 public:
16 Vector() noexcept = default;
17 constexpr Vector(float a, float b, float c, float d) noexcept
18 : mVals{{a, b, c, d}}
19 { }
21 float& operator[](size_t idx) noexcept { return mVals[idx]; }
22 constexpr const float& operator[](size_t idx) const noexcept { return mVals[idx]; }
24 Vector& operator+=(const Vector &rhs) noexcept
26 mVals[0] += rhs.mVals[0];
27 mVals[1] += rhs.mVals[1];
28 mVals[2] += rhs.mVals[2];
29 mVals[3] += rhs.mVals[3];
30 return *this;
33 float normalize()
35 const float length{std::sqrt(mVals[0]*mVals[0] + mVals[1]*mVals[1] + mVals[2]*mVals[2])};
36 if(length > std::numeric_limits<float>::epsilon())
38 float inv_length = 1.0f/length;
39 mVals[0] *= inv_length;
40 mVals[1] *= inv_length;
41 mVals[2] *= inv_length;
42 return length;
44 mVals[0] = mVals[1] = mVals[2] = 0.0f;
45 return 0.0f;
49 class Matrix {
50 alignas(16) std::array<std::array<float,4>,4> mVals;
52 public:
53 Matrix() noexcept = default;
54 constexpr Matrix(float aa, float ab, float ac, float ad,
55 float ba, float bb, float bc, float bd,
56 float ca, float cb, float cc, float cd,
57 float da, float db, float dc, float dd) noexcept
58 : mVals{{{{aa, ab, ac, ad}}, {{ba, bb, bc, bd}}, {{ca, cb, cc, cd}}, {{da, db, dc, dd}}}}
59 { }
61 std::array<float,4>& operator[](size_t idx) noexcept { return mVals[idx]; }
62 constexpr const std::array<float,4>& operator[](size_t idx) const noexcept { return mVals[idx]; }
64 void setRow(size_t idx, float a, float b, float c, float d) noexcept
66 mVals[idx][0] = a;
67 mVals[idx][1] = b;
68 mVals[idx][2] = c;
69 mVals[idx][3] = d;
72 static const Matrix &Identity() noexcept
74 static constexpr Matrix identity{
75 1.0f, 0.0f, 0.0f, 0.0f,
76 0.0f, 1.0f, 0.0f, 0.0f,
77 0.0f, 0.0f, 1.0f, 0.0f,
78 0.0f, 0.0f, 0.0f, 1.0f
80 return identity;
84 } // namespace alu
86 #endif /* COMMON_VECMAT_H */