Vector.hpp 5.9 KB

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