Vector.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef VECTOR_H
  2. #define VECTOR_H
  3. #include <cmath>
  4. #include "utils/StringBuffer.h"
  5. template<int N>
  6. class Vector final {
  7. float values[N];
  8. public:
  9. Vector() {
  10. for(int i = 0; i < N; i++) {
  11. values[i] = 0.0f;
  12. }
  13. }
  14. template<typename... Args>
  15. Vector(float a, Args&&... args) {
  16. const int size = sizeof...(args) + 1;
  17. float init[size] = {a, args...};
  18. static_assert(N == size,
  19. "vector size and amount of float arguments do not match");
  20. for(int i = 0; i < N; i++) {
  21. values[i] = init[i];
  22. }
  23. }
  24. Vector& setAngles(float, float) = delete;
  25. Vector cross(const Vector&) const = delete;
  26. Vector& operator+=(const Vector& other) {
  27. for(int i = 0; i < N; i++) {
  28. values[i] += other.values[i];
  29. }
  30. return *this;
  31. }
  32. Vector operator+(const Vector& other) const {
  33. Vector v = *this;
  34. v += other;
  35. return v;
  36. }
  37. Vector& operator-=(const Vector& other) {
  38. for(int i = 0; i < N; i++) {
  39. values[i] -= other.values[i];
  40. }
  41. return *this;
  42. }
  43. Vector operator-() const {
  44. Vector v = *this;
  45. for(int i = 0; i < N; i++) {
  46. v.values[i] = -v.values[i];
  47. }
  48. return v;
  49. }
  50. Vector operator-(const Vector& other) const {
  51. Vector v = *this;
  52. v -= other;
  53. return v;
  54. }
  55. Vector& operator*=(float factor) {
  56. for(int i = 0; i < N; i++) {
  57. values[i] *= factor;
  58. }
  59. return *this;
  60. }
  61. Vector& operator*=(const Vector<N>& other) {
  62. for(int i = 0; i < N; i++) {
  63. values[i] *= other.values[i];
  64. }
  65. return *this;
  66. }
  67. Vector operator*(float factor) const {
  68. Vector v = *this;
  69. v *= factor;
  70. return v;
  71. }
  72. Vector operator*(const Vector<N>& other) const {
  73. Vector v = *this;
  74. v *= other;
  75. return v;
  76. }
  77. Vector& operator/=(float factor) {
  78. for(int i = 0; i < N; i++) {
  79. values[i] /= factor;
  80. }
  81. return *this;
  82. }
  83. Vector operator/(float factor) const {
  84. Vector v = *this;
  85. v /= factor;
  86. return v;
  87. }
  88. float dot(const Vector& v) const {
  89. float length = 0.0f;
  90. for(int i = 0; i < N; i++) {
  91. length += values[i] * v.values[i];
  92. }
  93. return length;
  94. }
  95. float squareLength() const {
  96. return dot(*this);
  97. }
  98. float length() const {
  99. return sqrtf(squareLength());
  100. }
  101. Vector& normalize() {
  102. *this *= 1.0f / length();
  103. return *this;
  104. }
  105. float& operator[](int index) {
  106. return values[index];
  107. }
  108. const float& operator[](int index) const {
  109. return values[index];
  110. }
  111. template<int L>
  112. void toString(StringBuffer<L>& s) const {
  113. s.append("[");
  114. for(int i = 0; i < N - 1; i++) {
  115. s.append(values[i]);
  116. s.append(", ");
  117. }
  118. if(N > 0) {
  119. s.append(values[N - 1]);
  120. }
  121. s.append("]");
  122. }
  123. };
  124. template<int N>
  125. Vector<N> operator*(float factor, const Vector<N>& v) {
  126. return v * factor;
  127. }
  128. template<>
  129. Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle);
  130. template<>
  131. Vector<3> Vector<3>::cross(const Vector<3>& other) const;
  132. typedef Vector<4> Vector4;
  133. typedef Vector<3> Vector3;
  134. typedef Vector<2> Vector2;
  135. #endif