aboutsummaryrefslogtreecommitdiffstats
path: root/common/vecmat.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/vecmat.h')
-rw-r--r--common/vecmat.h51
1 files changed, 22 insertions, 29 deletions
diff --git a/common/vecmat.h b/common/vecmat.h
index 78fd806e..a45f262f 100644
--- a/common/vecmat.h
+++ b/common/vecmat.h
@@ -14,19 +14,19 @@ namespace alu {
template<typename T>
class VectorR {
static_assert(std::is_floating_point<T>::value, "Must use floating-point types");
- alignas(16) std::array<T,4> mVals;
+ alignas(16) T mVals[4];
public:
constexpr VectorR() noexcept = default;
constexpr VectorR(const VectorR&) noexcept = default;
- constexpr VectorR(T a, T b, T c, T d) noexcept : mVals{{a, b, c, d}} { }
+ constexpr explicit VectorR(T a, T b, T c, T d) noexcept : mVals{a, b, c, d} { }
constexpr VectorR& operator=(const VectorR&) noexcept = default;
- T& operator[](size_t idx) noexcept { return mVals[idx]; }
+ constexpr T& operator[](size_t idx) noexcept { return mVals[idx]; }
constexpr const T& operator[](size_t idx) const noexcept { return mVals[idx]; }
- VectorR& operator+=(const VectorR &rhs) noexcept
+ constexpr VectorR& operator+=(const VectorR &rhs) noexcept
{
mVals[0] += rhs.mVals[0];
mVals[1] += rhs.mVals[1];
@@ -35,14 +35,13 @@ public:
return *this;
}
- VectorR operator-(const VectorR &rhs) const noexcept
+ constexpr VectorR operator-(const VectorR &rhs) const noexcept
{
- const VectorR ret{mVals[0] - rhs.mVals[0], mVals[1] - rhs.mVals[1],
+ return VectorR{mVals[0] - rhs.mVals[0], mVals[1] - rhs.mVals[1],
mVals[2] - rhs.mVals[2], mVals[3] - rhs.mVals[3]};
- return ret;
}
- T normalize(T limit = std::numeric_limits<T>::epsilon())
+ constexpr T normalize(T limit = std::numeric_limits<T>::epsilon())
{
limit = std::max(limit, std::numeric_limits<T>::epsilon());
const T length_sqr{mVals[0]*mVals[0] + mVals[1]*mVals[1] + mVals[2]*mVals[2]};
@@ -59,38 +58,39 @@ public:
return T{0};
}
- constexpr VectorR cross_product(const alu::VectorR<T> &rhs) const
+ constexpr VectorR cross_product(const alu::VectorR<T> &rhs) const noexcept
{
return VectorR{
- (*this)[1]*rhs[2] - (*this)[2]*rhs[1],
- (*this)[2]*rhs[0] - (*this)[0]*rhs[2],
- (*this)[0]*rhs[1] - (*this)[1]*rhs[0],
+ mVals[1]*rhs.mVals[2] - mVals[2]*rhs.mVals[1],
+ mVals[2]*rhs.mVals[0] - mVals[0]*rhs.mVals[2],
+ mVals[0]*rhs.mVals[1] - mVals[1]*rhs.mVals[0],
T{0}};
}
- constexpr T dot_product(const alu::VectorR<T> &rhs) const
- { return (*this)[0]*rhs[0] + (*this)[1]*rhs[1] + (*this)[2]*rhs[2]; }
+ constexpr T dot_product(const alu::VectorR<T> &rhs) const noexcept
+ { return mVals[0]*rhs.mVals[0] + mVals[1]*rhs.mVals[1] + mVals[2]*rhs.mVals[2]; }
};
using Vector = VectorR<float>;
template<typename T>
class MatrixR {
static_assert(std::is_floating_point<T>::value, "Must use floating-point types");
- alignas(16) std::array<T,16> mVals;
+ alignas(16) T mVals[16];
public:
constexpr MatrixR() noexcept = default;
constexpr MatrixR(const MatrixR&) noexcept = default;
- constexpr MatrixR(T aa, T ab, T ac, T ad,
- T ba, T bb, T bc, T bd,
- T ca, T cb, T cc, T cd,
- T da, T db, T dc, T dd) noexcept
- : mVals{{aa,ab,ac,ad, ba,bb,bc,bd, ca,cb,cc,cd, da,db,dc,dd}}
+ constexpr explicit MatrixR(
+ T aa, T ab, T ac, T ad,
+ T ba, T bb, T bc, T bd,
+ T ca, T cb, T cc, T cd,
+ T da, T db, T dc, T dd) noexcept
+ : mVals{aa,ab,ac,ad, ba,bb,bc,bd, ca,cb,cc,cd, da,db,dc,dd}
{ }
constexpr MatrixR& operator=(const MatrixR&) noexcept = default;
- auto operator[](size_t idx) noexcept { return al::span<T,4>{&mVals[idx*4], 4}; }
+ constexpr auto operator[](size_t idx) noexcept { return al::span<T,4>{&mVals[idx*4], 4}; }
constexpr auto operator[](size_t idx) const noexcept
{ return al::span<const T,4>{&mVals[idx*4], 4}; }
@@ -106,7 +106,7 @@ public:
using Matrix = MatrixR<float>;
template<typename T>
-inline VectorR<T> operator*(const MatrixR<T> &mtx, const VectorR<T> &vec) noexcept
+constexpr VectorR<T> operator*(const MatrixR<T> &mtx, const VectorR<T> &vec) noexcept
{
return VectorR<T>{
vec[0]*mtx[0][0] + vec[1]*mtx[1][0] + vec[2]*mtx[2][0] + vec[3]*mtx[3][0],
@@ -115,13 +115,6 @@ inline VectorR<T> operator*(const MatrixR<T> &mtx, const VectorR<T> &vec) noexce
vec[0]*mtx[0][3] + vec[1]*mtx[1][3] + vec[2]*mtx[2][3] + vec[3]*mtx[3][3]};
}
-template<typename U, typename T>
-inline VectorR<U> cast_to(const VectorR<T> &vec) noexcept
-{
- return VectorR<U>{static_cast<U>(vec[0]), static_cast<U>(vec[1]),
- static_cast<U>(vec[2]), static_cast<U>(vec[3])};
-}
-
} // namespace alu
#endif /* COMMON_VECMAT_H */