🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.9 KiB

  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. VectorR operator-(const VectorR &rhs) const noexcept
  29. {
  30. const VectorR ret{mVals[0] - rhs.mVals[0], mVals[1] - rhs.mVals[1],
  31. mVals[2] - rhs.mVals[2], mVals[3] - rhs.mVals[3]};
  32. return ret;
  33. }
  34. T normalize(T limit = std::numeric_limits<T>::epsilon())
  35. {
  36. limit = std::max(limit, std::numeric_limits<T>::epsilon());
  37. const T length_sqr{mVals[0]*mVals[0] + mVals[1]*mVals[1] + mVals[2]*mVals[2]};
  38. if(length_sqr > limit*limit)
  39. {
  40. const T length{std::sqrt(length_sqr)};
  41. T inv_length{T{1}/length};
  42. mVals[0] *= inv_length;
  43. mVals[1] *= inv_length;
  44. mVals[2] *= inv_length;
  45. return length;
  46. }
  47. mVals[0] = mVals[1] = mVals[2] = T{0};
  48. return T{0};
  49. }
  50. constexpr VectorR cross_product(const alu::VectorR<T> &rhs) const
  51. {
  52. return VectorR{
  53. (*this)[1]*rhs[2] - (*this)[2]*rhs[1],
  54. (*this)[2]*rhs[0] - (*this)[0]*rhs[2],
  55. (*this)[0]*rhs[1] - (*this)[1]*rhs[0],
  56. T{0}};
  57. }
  58. constexpr T dot_product(const alu::VectorR<T> &rhs) const
  59. { return (*this)[0]*rhs[0] + (*this)[1]*rhs[1] + (*this)[2]*rhs[2]; }
  60. };
  61. using Vector = VectorR<float>;
  62. template<typename T>
  63. class MatrixR {
  64. static_assert(std::is_floating_point<T>::value, "Must use floating-point types");
  65. alignas(16) std::array<T,16> mVals;
  66. public:
  67. constexpr MatrixR() noexcept = default;
  68. constexpr MatrixR(const MatrixR&) noexcept = default;
  69. constexpr MatrixR(T aa, T ab, T ac, T ad,
  70. T ba, T bb, T bc, T bd,
  71. T ca, T cb, T cc, T cd,
  72. T da, T db, T dc, T dd) noexcept
  73. : mVals{{aa,ab,ac,ad, ba,bb,bc,bd, ca,cb,cc,cd, da,db,dc,dd}}
  74. { }
  75. constexpr MatrixR& operator=(const MatrixR&) noexcept = default;
  76. auto operator[](size_t idx) noexcept { return al::span<T,4>{&mVals[idx*4], 4}; }
  77. constexpr auto operator[](size_t idx) const noexcept
  78. { return al::span<const T,4>{&mVals[idx*4], 4}; }
  79. static constexpr MatrixR Identity() noexcept
  80. {
  81. return MatrixR{
  82. T{1}, T{0}, T{0}, T{0},
  83. T{0}, T{1}, T{0}, T{0},
  84. T{0}, T{0}, T{1}, T{0},
  85. T{0}, T{0}, T{0}, T{1}};
  86. }
  87. };
  88. using Matrix = MatrixR<float>;
  89. template<typename T>
  90. inline VectorR<T> operator*(const MatrixR<T> &mtx, const VectorR<T> &vec) noexcept
  91. {
  92. return VectorR<T>{
  93. vec[0]*mtx[0][0] + vec[1]*mtx[1][0] + vec[2]*mtx[2][0] + vec[3]*mtx[3][0],
  94. vec[0]*mtx[0][1] + vec[1]*mtx[1][1] + vec[2]*mtx[2][1] + vec[3]*mtx[3][1],
  95. vec[0]*mtx[0][2] + vec[1]*mtx[1][2] + vec[2]*mtx[2][2] + vec[3]*mtx[3][2],
  96. vec[0]*mtx[0][3] + vec[1]*mtx[1][3] + vec[2]*mtx[2][3] + vec[3]*mtx[3][3]};
  97. }
  98. template<typename U, typename T>
  99. inline VectorR<U> cast_to(const VectorR<T> &vec) noexcept
  100. {
  101. return VectorR<U>{static_cast<U>(vec[0]), static_cast<U>(vec[1]),
  102. static_cast<U>(vec[2]), static_cast<U>(vec[3])};
  103. }
  104. } // namespace alu
  105. #endif /* COMMON_VECMAT_H */