Vector.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #ifndef CORE_VECTOR_HPP
  2. #define CORE_VECTOR_HPP
  3. #include "core/math/Math.hpp"
  4. #include "core/utils/ArrayString.hpp"
  5. namespace Core {
  6. template<int N, typename T>
  7. class alignas(sizeof(T) * (Math::isPowerOf2(N) ? N : 1)) Vector final {
  8. static_assert(N > 0, "Vector size must be positive");
  9. T values[static_cast<unsigned int>(N)];
  10. public:
  11. Vector() {
  12. for(int i = 0; i < N; i++) {
  13. values[i] = static_cast<T>(0);
  14. }
  15. }
  16. template<typename OT, typename... Args>
  17. Vector(OT a, Args&&... args)
  18. : values(static_cast<T>(a), static_cast<T>(args)...) {
  19. static_assert(sizeof...(args) + 1 == N,
  20. "vector parameters do not match its size");
  21. }
  22. public:
  23. Vector& setAngles(float, float) = delete;
  24. Vector cross(const Vector&) const = delete;
  25. Vector& operator+=(const Vector& other) {
  26. for(int i = 0; i < N; i++) {
  27. values[i] += other.values[i];
  28. }
  29. return *this;
  30. }
  31. Vector operator+(const Vector& other) const {
  32. Vector v = *this;
  33. v += other;
  34. return v;
  35. }
  36. Vector& operator-=(const Vector& other) {
  37. for(int i = 0; i < N; i++) {
  38. values[i] -= other.values[i];
  39. }
  40. return *this;
  41. }
  42. Vector operator-() const {
  43. Vector v = *this;
  44. for(int i = 0; i < N; i++) {
  45. v.values[i] = -v.values[i];
  46. }
  47. return v;
  48. }
  49. Vector operator-(const Vector& other) const {
  50. Vector v = *this;
  51. v -= other;
  52. return v;
  53. }
  54. Vector& operator*=(T factor) {
  55. for(int i = 0; i < N; i++) {
  56. values[i] *= factor;
  57. }
  58. return *this;
  59. }
  60. Vector& operator*=(const Vector& other) {
  61. for(int i = 0; i < N; i++) {
  62. values[i] *= other.values[i];
  63. }
  64. return *this;
  65. }
  66. Vector operator*(T factor) const {
  67. Vector v = *this;
  68. v *= factor;
  69. return v;
  70. }
  71. Vector operator*(const Vector& other) const {
  72. Vector v = *this;
  73. v *= other;
  74. return v;
  75. }
  76. Vector& operator/=(T factor) {
  77. for(int i = 0; i < N; i++) {
  78. values[i] /= factor;
  79. }
  80. return *this;
  81. }
  82. Vector& operator/=(const Vector& other) {
  83. for(int i = 0; i < N; i++) {
  84. values[i] /= other.values[i];
  85. }
  86. return *this;
  87. }
  88. Vector operator/(T factor) const {
  89. Vector v = *this;
  90. v /= factor;
  91. return v;
  92. }
  93. Vector operator/(const Vector& other) const {
  94. Vector v = *this;
  95. v /= other;
  96. return v;
  97. }
  98. T dot(const Vector& v) const {
  99. T length = 0.0f;
  100. for(int i = 0; i < N; i++) {
  101. length += values[i] * v.values[i];
  102. }
  103. return length;
  104. }
  105. T squareLength() const {
  106. return dot(*this);
  107. }
  108. float length() const {
  109. return Math::squareRoot(static_cast<float>(squareLength()));
  110. }
  111. Vector& normalize() {
  112. if constexpr(Core::IsSame<float, T> || Core::IsSame<double, T>) {
  113. *this *= 1.0f / length();
  114. } else {
  115. *this /= static_cast<T>(length());
  116. }
  117. return *this;
  118. }
  119. T& operator[](int index) {
  120. return values[index];
  121. }
  122. const T& operator[](int index) const {
  123. return values[index];
  124. }
  125. Vector<N, int> toInt() const {
  126. Vector<N, int> cast;
  127. for(int i = 0; i < N; i++) {
  128. cast[i] = static_cast<int>(values[i]);
  129. }
  130. return cast;
  131. }
  132. Vector<N, float> toFloat() const {
  133. Vector<N, float> cast;
  134. for(int i = 0; i < N; i++) {
  135. cast[i] = static_cast<float>(values[i]);
  136. }
  137. return cast;
  138. }
  139. template<typename String>
  140. check_return Error toString(String& s) const {
  141. CORE_RETURN_ERROR(s.append("["));
  142. for(int i = 0; i < N - 1; i++) {
  143. CORE_RETURN_ERROR(s.append(values[i]));
  144. CORE_RETURN_ERROR(s.append(", "));
  145. }
  146. if(N > 0) {
  147. CORE_RETURN_ERROR(s.append(values[N - 1]));
  148. }
  149. return s.append("]");
  150. }
  151. };
  152. using Vector4 = Vector<4, float>;
  153. using Vector3 = Vector<3, float>;
  154. using Vector2 = Vector<2, float>;
  155. static_assert(alignof(Vector4) == sizeof(float) * 4,
  156. "invalid Vector4 alignment");
  157. static_assert(alignof(Vector3) == sizeof(float),
  158. "invalid Vector3 alignment");
  159. static_assert(alignof(Vector2) == sizeof(float) * 2,
  160. "invalid Vector2 alignment");
  161. using IntVector4 = Vector<4, int>;
  162. using IntVector3 = Vector<3, int>;
  163. using IntVector2 = Vector<2, int>;
  164. static_assert(alignof(IntVector4) == sizeof(int) * 4,
  165. "invalid IntVector4 alignment");
  166. static_assert(alignof(IntVector3) == sizeof(int),
  167. "invalid IntVector3 alignment");
  168. static_assert(alignof(IntVector2) == sizeof(int) * 2,
  169. "invalid IntVector2 alignment");
  170. template<>
  171. Vector3& Vector3::setAngles(float lengthAngle, float widthAngle);
  172. template<>
  173. Vector3 Vector3::cross(const Vector3& other) const;
  174. }
  175. template<int N, typename T>
  176. Core::Vector<N, T> operator*(T factor, const Core::Vector<N, T>& v) {
  177. return v * factor;
  178. }
  179. #endif