🛠️🐜 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.

103 lines
2.9 KiB

  1. #ifndef COMMON_VECMAT_H
  2. #define COMMON_VECMAT_H
  3. #include <cmath>
  4. #include <array>
  5. #include <limits>
  6. #include <algorithm>
  7. #include "math_defs.h"
  8. namespace alu {
  9. class Vector {
  10. alignas(16) std::array<float,4> mVals{};
  11. public:
  12. constexpr Vector() noexcept = default;
  13. constexpr Vector(float a, float b, float c, float d) noexcept
  14. : mVals{{a, b, c, d}}
  15. { }
  16. Vector(const Vector &rhs) noexcept
  17. { std::copy(rhs.mVals.begin(), rhs.mVals.end(), mVals.begin()); }
  18. Vector& operator=(const Vector &rhs) noexcept
  19. {
  20. std::copy(rhs.mVals.begin(), rhs.mVals.end(), mVals.begin());
  21. return *this;
  22. }
  23. float& operator[](size_t idx) noexcept { return mVals[idx]; }
  24. constexpr const float& operator[](size_t idx) const noexcept { return mVals[idx]; }
  25. Vector& operator+=(const Vector &rhs) noexcept
  26. {
  27. mVals[0] += rhs.mVals[0];
  28. mVals[1] += rhs.mVals[1];
  29. mVals[2] += rhs.mVals[2];
  30. mVals[3] += rhs.mVals[3];
  31. return *this;
  32. }
  33. float normalize()
  34. {
  35. const float length{std::sqrt(mVals[0]*mVals[0] + mVals[1]*mVals[1] + mVals[2]*mVals[2])};
  36. if(length > std::numeric_limits<float>::epsilon())
  37. {
  38. float inv_length = 1.0f/length;
  39. mVals[0] *= inv_length;
  40. mVals[1] *= inv_length;
  41. mVals[2] *= inv_length;
  42. return length;
  43. }
  44. mVals[0] = mVals[1] = mVals[2] = 0.0f;
  45. return 0.0f;
  46. }
  47. };
  48. class Matrix {
  49. alignas(16) std::array<std::array<float,4>,4> mVals{};
  50. public:
  51. constexpr Matrix() noexcept = default;
  52. constexpr Matrix(float aa, float ab, float ac, float ad,
  53. float ba, float bb, float bc, float bd,
  54. float ca, float cb, float cc, float cd,
  55. float da, float db, float dc, float dd) noexcept
  56. : mVals{{{{aa, ab, ac, ad}}, {{ba, bb, bc, bd}}, {{ca, cb, cc, cd}}, {{da, db, dc, dd}}}}
  57. { }
  58. Matrix(const Matrix &rhs) noexcept
  59. { std::copy(rhs.mVals.begin(), rhs.mVals.end(), mVals.begin()); }
  60. Matrix& operator=(const Matrix &rhs) noexcept
  61. {
  62. std::copy(rhs.mVals.begin(), rhs.mVals.end(), mVals.begin());
  63. return *this;
  64. }
  65. std::array<float,4>& operator[](size_t idx) noexcept { return mVals[idx]; }
  66. constexpr const std::array<float,4>& operator[](size_t idx) const noexcept { return mVals[idx]; }
  67. void setRow(size_t idx, float a, float b, float c, float d) noexcept
  68. {
  69. mVals[idx][0] = a;
  70. mVals[idx][1] = b;
  71. mVals[idx][2] = c;
  72. mVals[idx][3] = d;
  73. }
  74. static const Matrix &Identity() noexcept
  75. {
  76. static constexpr Matrix identity{
  77. 1.0f, 0.0f, 0.0f, 0.0f,
  78. 0.0f, 1.0f, 0.0f, 0.0f,
  79. 0.0f, 0.0f, 1.0f, 0.0f,
  80. 0.0f, 0.0f, 0.0f, 1.0f
  81. };
  82. return identity;
  83. }
  84. };
  85. } // namespace alu
  86. #endif /* COMMON_VECMAT_H */