1 #ifndef COMMON_VECMAT_H
2 #define COMMON_VECMAT_H
16 alignas(16) std::array
<float,4> mVals
{};
19 constexpr Vector() noexcept
= default;
20 constexpr Vector(const Vector
&) noexcept
= default;
21 constexpr Vector(Vector
&&) noexcept
= default;
22 constexpr explicit Vector(float a
, float b
, float c
, float d
) noexcept
: mVals
{{a
,b
,c
,d
}} { }
24 constexpr auto operator=(const Vector
&) noexcept
-> Vector
& = default;
25 constexpr auto operator=(Vector
&&) noexcept
-> Vector
& = default;
27 [[nodiscard
]] constexpr
28 auto operator[](std::size_t idx
) noexcept
-> float& { return mVals
[idx
]; }
29 [[nodiscard
]] constexpr
30 auto operator[](std::size_t idx
) const noexcept
-> const float& { return mVals
[idx
]; }
32 constexpr auto operator+=(const Vector
&rhs
) noexcept
-> Vector
&
34 mVals
[0] += rhs
.mVals
[0];
35 mVals
[1] += rhs
.mVals
[1];
36 mVals
[2] += rhs
.mVals
[2];
37 mVals
[3] += rhs
.mVals
[3];
41 [[nodiscard
]] constexpr
42 auto operator-(const Vector
&rhs
) const noexcept
-> Vector
44 return Vector
{mVals
[0] - rhs
.mVals
[0], mVals
[1] - rhs
.mVals
[1],
45 mVals
[2] - rhs
.mVals
[2], mVals
[3] - rhs
.mVals
[3]};
48 constexpr auto normalize(float limit
= std::numeric_limits
<float>::epsilon()) -> float
50 limit
= std::max(limit
, std::numeric_limits
<float>::epsilon());
51 const auto length_sqr
= float{mVals
[0]*mVals
[0] + mVals
[1]*mVals
[1] + mVals
[2]*mVals
[2]};
52 if(length_sqr
> limit
*limit
)
54 const auto length
= float{std::sqrt(length_sqr
)};
55 auto inv_length
= float{1.0f
/ length
};
56 mVals
[0] *= inv_length
;
57 mVals
[1] *= inv_length
;
58 mVals
[2] *= inv_length
;
61 mVals
[0] = mVals
[1] = mVals
[2] = 0.0f
;
65 [[nodiscard
]] constexpr auto cross_product(const Vector
&rhs
) const noexcept
-> Vector
68 mVals
[1]*rhs
.mVals
[2] - mVals
[2]*rhs
.mVals
[1],
69 mVals
[2]*rhs
.mVals
[0] - mVals
[0]*rhs
.mVals
[2],
70 mVals
[0]*rhs
.mVals
[1] - mVals
[1]*rhs
.mVals
[0],
74 [[nodiscard
]] constexpr auto dot_product(const Vector
&rhs
) const noexcept
-> float
75 { return mVals
[0]*rhs
.mVals
[0] + mVals
[1]*rhs
.mVals
[1] + mVals
[2]*rhs
.mVals
[2]; }
79 alignas(16) std::array
<float,16> mVals
{};
82 constexpr Matrix() noexcept
= default;
83 constexpr Matrix(const Matrix
&) noexcept
= default;
84 constexpr Matrix(Matrix
&&) noexcept
= default;
85 constexpr explicit Matrix(
86 float aa
, float ab
, float ac
, float ad
,
87 float ba
, float bb
, float bc
, float bd
,
88 float ca
, float cb
, float cc
, float cd
,
89 float da
, float db
, float dc
, float dd
) noexcept
90 : mVals
{{aa
,ab
,ac
,ad
, ba
,bb
,bc
,bd
, ca
,cb
,cc
,cd
, da
,db
,dc
,dd
}}
93 constexpr auto operator=(const Matrix
&) noexcept
-> Matrix
& = default;
94 constexpr auto operator=(Matrix
&&) noexcept
-> Matrix
& = default;
96 [[nodiscard
]] constexpr auto operator[](std::size_t idx
) noexcept
97 { return al::span
<float,4>{&mVals
[idx
*4], 4}; }
98 [[nodiscard
]] constexpr auto operator[](std::size_t idx
) const noexcept
99 { return al::span
<const float,4>{&mVals
[idx
*4], 4}; }
101 [[nodiscard
]] static constexpr auto Identity() noexcept
-> Matrix
104 1.0f
, 0.0f
, 0.0f
, 0.0f
,
105 0.0f
, 1.0f
, 0.0f
, 0.0f
,
106 0.0f
, 0.0f
, 1.0f
, 0.0f
,
107 0.0f
, 0.0f
, 0.0f
, 1.0f
};
110 [[nodiscard
]] friend constexpr
111 auto operator*(const Matrix
&mtx
, const Vector
&vec
) noexcept
-> Vector
114 vec
[0]*mtx
[0][0] + vec
[1]*mtx
[1][0] + vec
[2]*mtx
[2][0] + vec
[3]*mtx
[3][0],
115 vec
[0]*mtx
[0][1] + vec
[1]*mtx
[1][1] + vec
[2]*mtx
[2][1] + vec
[3]*mtx
[3][1],
116 vec
[0]*mtx
[0][2] + vec
[1]*mtx
[1][2] + vec
[2]*mtx
[2][2] + vec
[3]*mtx
[3][2],
117 vec
[0]*mtx
[0][3] + vec
[1]*mtx
[1][3] + vec
[2]*mtx
[2][3] + vec
[3]*mtx
[3][3]};
123 #endif /* COMMON_VECMAT_H */