Vector.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #ifndef CORE_VECTOR_HPP
  2. #define CORE_VECTOR_HPP
  3. #include "math/Math.hpp"
  4. #include "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(squareLength());
  118. }
  119. Vector& normalize() {
  120. *this *= 1.0f / length();
  121. return *this;
  122. }
  123. T& operator[](int index) {
  124. return values[index];
  125. }
  126. const T& operator[](int index) const {
  127. return values[index];
  128. }
  129. Vector<N, int> toInt() const {
  130. Vector<N, int> cast;
  131. for(int i = 0; i < N; i++) {
  132. cast[i] = static_cast<int>(values[i]);
  133. }
  134. return cast;
  135. }
  136. Vector<N, float> toFloat() const {
  137. Vector<N, float> cast;
  138. for(int i = 0; i < N; i++) {
  139. cast[i] = static_cast<float>(values[i]);
  140. }
  141. return cast;
  142. }
  143. template<typename String>
  144. check_return Error toString(String& s) const {
  145. CORE_RETURN_ERROR(s.append("["));
  146. for(int i = 0; i < N - 1; i++) {
  147. CORE_RETURN_ERROR(s.append(values[i]));
  148. CORE_RETURN_ERROR(s.append(", "));
  149. }
  150. if(N > 0) {
  151. CORE_RETURN_ERROR(s.append(values[N - 1]));
  152. }
  153. return s.append("]");
  154. }
  155. };
  156. using Vector4 = Vector<4, float>;
  157. using Vector3 = Vector<3, float>;
  158. using Vector2 = Vector<2, float>;
  159. static_assert(alignof(Vector4) == sizeof(float) * 4,
  160. "invalid Vector4 alignment");
  161. static_assert(alignof(Vector3) == sizeof(float),
  162. "invalid Vector3 alignment");
  163. static_assert(alignof(Vector2) == sizeof(float) * 2,
  164. "invalid Vector2 alignment");
  165. using IntVector4 = Vector<4, int>;
  166. using IntVector3 = Vector<3, int>;
  167. using IntVector2 = Vector<2, int>;
  168. static_assert(alignof(IntVector4) == sizeof(int) * 4,
  169. "invalid IntVector4 alignment");
  170. static_assert(alignof(IntVector3) == sizeof(int),
  171. "invalid IntVector3 alignment");
  172. static_assert(alignof(IntVector2) == sizeof(int) * 2,
  173. "invalid IntVector2 alignment");
  174. template<>
  175. Vector3& Vector3::setAngles(float lengthAngle, float widthAngle);
  176. template<>
  177. Vector3 Vector3::cross(const Vector3& other) const;
  178. }
  179. template<int N, typename T>
  180. Core::Vector<N, T> operator*(T factor, const Core::Vector<N, T>& v) {
  181. return v * factor;
  182. }
  183. #endif