Vector.hpp 5.7 KB

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