Vector.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/(T factor) const {
  89. Vector v = *this;
  90. v /= factor;
  91. return v;
  92. }
  93. T dot(const Vector& v) const {
  94. T length = 0.0f;
  95. for(int i = 0; i < N; i++) {
  96. length += values[i] * v.values[i];
  97. }
  98. return length;
  99. }
  100. T squareLength() const {
  101. return dot(*this);
  102. }
  103. float length() const {
  104. return sqrtf(squareLength());
  105. }
  106. Vector& normalize() {
  107. *this *= 1.0f / length();
  108. return *this;
  109. }
  110. T& operator[](int index) {
  111. return values[index];
  112. }
  113. const T& operator[](int index) const {
  114. return values[index];
  115. }
  116. Vector<N, int> toInt() const {
  117. Vector<N, int> cast;
  118. for(int i = 0; i < N; i++) {
  119. cast[i] = values[i];
  120. }
  121. return cast;
  122. }
  123. Vector<N, float> toFloat() const {
  124. Vector<N, float> cast;
  125. for(int i = 0; i < N; i++) {
  126. cast[i] = values[i];
  127. }
  128. return cast;
  129. }
  130. template<int L>
  131. void toString(StringBuffer<L>& s) const {
  132. s.append("[");
  133. for(int i = 0; i < N - 1; i++) {
  134. s.append(values[i]);
  135. s.append(", ");
  136. }
  137. if(N > 0) {
  138. s.append(values[N - 1]);
  139. }
  140. s.append("]");
  141. }
  142. };
  143. template<int N, typename T>
  144. Vector<N, T> operator*(T factor, const Vector<N, T>& v) {
  145. return v * factor;
  146. }
  147. typedef Vector<4, float> Vector4;
  148. typedef Vector<3, float> Vector3;
  149. typedef Vector<2, float> Vector2;
  150. typedef Vector<4, int> IntVector4;
  151. typedef Vector<3, int> IntVector3;
  152. typedef Vector<2, int> IntVector2;
  153. template<>
  154. Vector3& Vector3::setAngles(float lengthAngle, float widthAngle);
  155. template<>
  156. Vector3 Vector3::cross(const Vector3& other) const;
  157. #endif