1 #ifndef COMMON_VECMAT_H
2 #define COMMON_VECMAT_H
17 static_assert(std::is_floating_point
<T
>::value
, "Must use floating-point types");
18 alignas(16) std::array
<T
,4> mVals
;
21 constexpr VectorR() noexcept
= default;
22 constexpr VectorR(const VectorR
&) noexcept
= default;
23 constexpr explicit VectorR(T a
, T b
, T c
, T d
) noexcept
: mVals
{a
, b
, c
, d
} { }
25 constexpr VectorR
& operator=(const VectorR
&) noexcept
= default;
27 constexpr T
& operator[](size_t idx
) noexcept
{ return mVals
[idx
]; }
28 constexpr const T
& operator[](size_t idx
) const noexcept
{ return mVals
[idx
]; }
30 constexpr VectorR
& operator+=(const VectorR
&rhs
) noexcept
32 mVals
[0] += rhs
.mVals
[0];
33 mVals
[1] += rhs
.mVals
[1];
34 mVals
[2] += rhs
.mVals
[2];
35 mVals
[3] += rhs
.mVals
[3];
39 constexpr VectorR
operator-(const VectorR
&rhs
) const noexcept
41 return VectorR
{mVals
[0] - rhs
.mVals
[0], mVals
[1] - rhs
.mVals
[1],
42 mVals
[2] - rhs
.mVals
[2], mVals
[3] - rhs
.mVals
[3]};
45 constexpr T
normalize(T limit
= std::numeric_limits
<T
>::epsilon())
47 limit
= std::max(limit
, std::numeric_limits
<T
>::epsilon());
48 const T length_sqr
{mVals
[0]*mVals
[0] + mVals
[1]*mVals
[1] + mVals
[2]*mVals
[2]};
49 if(length_sqr
> limit
*limit
)
51 const T length
{std::sqrt(length_sqr
)};
52 T inv_length
{T
{1}/length
};
53 mVals
[0] *= inv_length
;
54 mVals
[1] *= inv_length
;
55 mVals
[2] *= inv_length
;
58 mVals
[0] = mVals
[1] = mVals
[2] = T
{0};
62 [[nodiscard
]] constexpr auto cross_product(const alu::VectorR
<T
> &rhs
) const noexcept
-> VectorR
65 mVals
[1]*rhs
.mVals
[2] - mVals
[2]*rhs
.mVals
[1],
66 mVals
[2]*rhs
.mVals
[0] - mVals
[0]*rhs
.mVals
[2],
67 mVals
[0]*rhs
.mVals
[1] - mVals
[1]*rhs
.mVals
[0],
71 [[nodiscard
]] constexpr auto dot_product(const alu::VectorR
<T
> &rhs
) const noexcept
-> T
72 { return mVals
[0]*rhs
.mVals
[0] + mVals
[1]*rhs
.mVals
[1] + mVals
[2]*rhs
.mVals
[2]; }
74 using Vector
= VectorR
<float>;
78 static_assert(std::is_floating_point
<T
>::value
, "Must use floating-point types");
79 alignas(16) std::array
<T
,16> mVals
;
82 constexpr MatrixR() noexcept
= default;
83 constexpr MatrixR(const MatrixR
&) noexcept
= default;
84 constexpr explicit MatrixR(
85 T aa
, T ab
, T ac
, T ad
,
86 T ba
, T bb
, T bc
, T bd
,
87 T ca
, T cb
, T cc
, T cd
,
88 T da
, T db
, T dc
, T dd
) noexcept
89 : mVals
{aa
,ab
,ac
,ad
, ba
,bb
,bc
,bd
, ca
,cb
,cc
,cd
, da
,db
,dc
,dd
}
92 constexpr MatrixR
& operator=(const MatrixR
&) noexcept
= default;
94 constexpr auto operator[](size_t idx
) noexcept
{ return al::span
<T
,4>{&mVals
[idx
*4], 4}; }
95 constexpr auto operator[](size_t idx
) const noexcept
96 { return al::span
<const T
,4>{&mVals
[idx
*4], 4}; }
98 static constexpr MatrixR
Identity() noexcept
101 T
{1}, T
{0}, T
{0}, T
{0},
102 T
{0}, T
{1}, T
{0}, T
{0},
103 T
{0}, T
{0}, T
{1}, T
{0},
104 T
{0}, T
{0}, T
{0}, T
{1}};
107 using Matrix
= MatrixR
<float>;
110 constexpr VectorR
<T
> operator*(const MatrixR
<T
> &mtx
, const VectorR
<T
> &vec
) noexcept
113 vec
[0]*mtx
[0][0] + vec
[1]*mtx
[1][0] + vec
[2]*mtx
[2][0] + vec
[3]*mtx
[3][0],
114 vec
[0]*mtx
[0][1] + vec
[1]*mtx
[1][1] + vec
[2]*mtx
[2][1] + vec
[3]*mtx
[3][1],
115 vec
[0]*mtx
[0][2] + vec
[1]*mtx
[1][2] + vec
[2]*mtx
[2][2] + vec
[3]*mtx
[3][2],
116 vec
[0]*mtx
[0][3] + vec
[1]*mtx
[1][3] + vec
[2]*mtx
[2][3] + vec
[3]*mtx
[3][3]};
121 #endif /* COMMON_VECMAT_H */