Vector.h 4.1 KB

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