vecmat.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef COMMON_VECMAT_H
  2. #define COMMON_VECMAT_H
  3. #include <array>
  4. #include <cmath>
  5. #include <cstddef>
  6. #include <limits>
  7. #include "alspan.h"
  8. namespace alu {
  9. template<typename T>
  10. class VectorR {
  11. static_assert(std::is_floating_point<T>::value, "Must use floating-point types");
  12. alignas(16) std::array<T,4> mVals;
  13. public:
  14. constexpr VectorR() noexcept = default;
  15. constexpr VectorR(const VectorR&) noexcept = default;
  16. constexpr VectorR(T a, T b, T c, T d) noexcept : mVals{{a, b, c, d}} { }
  17. constexpr VectorR& operator=(const VectorR&) noexcept = default;
  18. T& operator[](size_t idx) noexcept { return mVals[idx]; }
  19. constexpr const T& operator[](size_t idx) const noexcept { return mVals[idx]; }
  20. VectorR& operator+=(const VectorR &rhs) noexcept
  21. {
  22. mVals[0] += rhs.mVals[0];
  23. mVals[1] += rhs.mVals[1];
  24. mVals[2] += rhs.mVals[2];
  25. mVals[3] += rhs.mVals[3];
  26. return *this;
  27. }
  28. T normalize()
  29. {
  30. const T length{std::sqrt(mVals[0]*mVals[0] + mVals[1]*mVals[1] + mVals[2]*mVals[2])};
  31. if(length > std::numeric_limits<T>::epsilon())
  32. {
  33. T inv_length{T{1}/length};
  34. mVals[0] *= inv_length;
  35. mVals[1] *= inv_length;
  36. mVals[2] *= inv_length;
  37. return length;
  38. }
  39. mVals[0] = mVals[1] = mVals[2] = T{0};
  40. return T{0};
  41. }
  42. constexpr VectorR cross_product(const alu::VectorR<T> &rhs) const
  43. {
  44. return VectorR{
  45. (*this)[1]*rhs[2] - (*this)[2]*rhs[1],
  46. (*this)[2]*rhs[0] - (*this)[0]*rhs[2],
  47. (*this)[0]*rhs[1] - (*this)[1]*rhs[0],
  48. T{0}};
  49. }
  50. constexpr T dot_product(const alu::VectorR<T> &rhs) const
  51. { return (*this)[0]*rhs[0] + (*this)[1]*rhs[1] + (*this)[2]*rhs[2]; }
  52. };
  53. using Vector = VectorR<float>;
  54. template<typename T>
  55. class MatrixR {
  56. static_assert(std::is_floating_point<T>::value, "Must use floating-point types");
  57. alignas(16) std::array<T,16> mVals;
  58. public:
  59. constexpr MatrixR() noexcept = default;
  60. constexpr MatrixR(const MatrixR&) noexcept = default;
  61. constexpr MatrixR(T aa, T ab, T ac, T ad,
  62. T ba, T bb, T bc, T bd,
  63. T ca, T cb, T cc, T cd,
  64. T da, T db, T dc, T dd) noexcept
  65. : mVals{{aa,ab,ac,ad, ba,bb,bc,bd, ca,cb,cc,cd, da,db,dc,dd}}
  66. { }
  67. constexpr MatrixR& operator=(const MatrixR&) noexcept = default;
  68. auto operator[](size_t idx) noexcept { return al::span<T,4>{&mVals[idx*4], 4}; }
  69. constexpr auto operator[](size_t idx) const noexcept
  70. { return al::span<const T,4>{&mVals[idx*4], 4}; }
  71. static constexpr MatrixR Identity() noexcept
  72. {
  73. return MatrixR{
  74. T{1}, T{0}, T{0}, T{0},
  75. T{0}, T{1}, T{0}, T{0},
  76. T{0}, T{0}, T{1}, T{0},
  77. T{0}, T{0}, T{0}, T{1}};
  78. }
  79. };
  80. using Matrix = MatrixR<float>;
  81. template<typename T>
  82. inline VectorR<T> operator*(const MatrixR<T> &mtx, const VectorR<T> &vec) noexcept
  83. {
  84. return VectorR<T>{
  85. vec[0]*mtx[0][0] + vec[1]*mtx[1][0] + vec[2]*mtx[2][0] + vec[3]*mtx[3][0],
  86. vec[0]*mtx[0][1] + vec[1]*mtx[1][1] + vec[2]*mtx[2][1] + vec[3]*mtx[3][1],
  87. vec[0]*mtx[0][2] + vec[1]*mtx[1][2] + vec[2]*mtx[2][2] + vec[3]*mtx[3][2],
  88. vec[0]*mtx[0][3] + vec[1]*mtx[1][3] + vec[2]*mtx[2][3] + vec[3]*mtx[3][3]};
  89. }
  90. template<typename U, typename T>
  91. inline VectorR<U> cast_to(const VectorR<T> &vec) noexcept
  92. {
  93. return VectorR<U>{static_cast<U>(vec[0]), static_cast<U>(vec[1]),
  94. static_cast<U>(vec[2]), static_cast<U>(vec[3])};
  95. }
  96. } // namespace alu
  97. #endif /* COMMON_VECMAT_H */