Vec4.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef ANKI_MATH_VEC4_H
  2. #define ANKI_MATH_VEC4_H
  3. #include "anki/math/CommonIncludes.h"
  4. namespace anki {
  5. /// @addtogroup Math
  6. /// @{
  7. /// 4D vector. SIMD optimized
  8. class Vec4
  9. {
  10. public:
  11. /// @name Constructors
  12. /// @{
  13. explicit Vec4();
  14. explicit Vec4(const F32 x, const F32 y, const F32 z,
  15. const F32 w);
  16. explicit Vec4(const F32 f);
  17. explicit Vec4(const F32 arr[]);
  18. explicit Vec4(const Vec2& v2, const F32 z, const F32 w);
  19. explicit Vec4(const Vec2& av2, const Vec2& bv2);
  20. explicit Vec4(const Vec3& v3, const F32 w);
  21. Vec4(const Vec4& b);
  22. explicit Vec4(const Quat& q);
  23. #if ANKI_MATH_SIMD == ANKI_MATH_SIMD_SSE
  24. explicit Vec4(const __m128& mm);
  25. #endif
  26. /// @}
  27. /// @name Accessors
  28. /// @{
  29. F32& x();
  30. F32 x() const;
  31. F32& y();
  32. F32 y() const;
  33. F32& z();
  34. F32 z() const;
  35. F32& w();
  36. F32 w() const;
  37. F32& operator[](const U i);
  38. F32 operator[](const U i) const;
  39. #if ANKI_MATH_SIMD == ANKI_MATH_SIMD_SSE
  40. __m128& getMm();
  41. const __m128& getMm() const;
  42. #endif
  43. Vec2 xy() const;
  44. Vec3 xyz() const;
  45. /// @}
  46. /// @name Operators with same type
  47. /// @{
  48. Vec4& operator=(const Vec4& b);
  49. Vec4 operator+(const Vec4& b) const;
  50. Vec4& operator+=(const Vec4& b);
  51. Vec4 operator-(const Vec4& b) const;
  52. Vec4& operator-=(const Vec4& b);
  53. Vec4 operator*(const Vec4& b) const;
  54. Vec4& operator*=(const Vec4& b);
  55. Vec4 operator/(const Vec4& b) const;
  56. Vec4& operator/=(const Vec4& b);
  57. Vec4 operator-() const;
  58. Bool operator==(const Vec4& b) const;
  59. Bool operator!=(const Vec4& b) const;
  60. Bool operator<(const Vec4& b) const;
  61. Bool operator<=(const Vec4& b) const;
  62. Bool operator>(const Vec4& b) const;
  63. Bool operator>=(const Vec4& b) const;
  64. /// @}
  65. /// @name Operators with F32
  66. /// @{
  67. Vec4 operator+(const F32 f) const;
  68. Vec4& operator+=(const F32 f);
  69. Vec4 operator-(const F32 f) const;
  70. Vec4& operator-=(const F32 f);
  71. Vec4 operator*(const F32 f) const;
  72. Vec4& operator*=(const F32 f);
  73. Vec4 operator/(const F32 f) const;
  74. Vec4& operator/=(const F32 f);
  75. /// @}
  76. /// @name Operators with other
  77. /// @{
  78. Vec4 operator*(const Mat4& m4) const;
  79. /// @}
  80. /// @name Other
  81. /// @{
  82. F32 getLength() const;
  83. Vec4 getNormalized() const;
  84. void normalize();
  85. F32 dot(const Vec4& b) const;
  86. /// @}
  87. /// @name Friends
  88. /// @{
  89. friend Vec4 operator+(const F32 f, const Vec4& v4);
  90. friend Vec4 operator-(const F32 f, const Vec4& v4);
  91. friend Vec4 operator*(const F32 f, const Vec4& v4);
  92. friend Vec4 operator/(const F32 f, const Vec4& v4);
  93. friend std::ostream& operator<<(std::ostream& s, const Vec4& v);
  94. /// @}
  95. private:
  96. /// @name Data
  97. /// @{
  98. union
  99. {
  100. struct
  101. {
  102. F32 x, y, z, w;
  103. } vec;
  104. Array<F32, 4> arr;
  105. #if ANKI_MATH_SIMD == ANKI_MATH_SIMD_SSE
  106. __m128 mm;
  107. #endif
  108. };
  109. /// @}
  110. };
  111. /// @}
  112. static_assert(sizeof(Vec4) == sizeof(F32) * 4, "Incorrect size");
  113. } // end namespace
  114. #include "anki/math/Vec4.inl.h"
  115. #endif