Vector.h 4.3 KB

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