Vector.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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*(float factor) const {
  62. Vector v = *this;
  63. v *= factor;
  64. return v;
  65. }
  66. Vector& operator/=(float factor) {
  67. for(int i = 0; i < N; i++) {
  68. values[i] /= factor;
  69. }
  70. return *this;
  71. }
  72. Vector operator/(float factor) const {
  73. Vector v = *this;
  74. v /= factor;
  75. return v;
  76. }
  77. float dot(const Vector& v) const {
  78. float length = 0.0f;
  79. for(int i = 0; i < N; i++) {
  80. length += values[i] * v.values[i];
  81. }
  82. return length;
  83. }
  84. float squareLength() const {
  85. return dot(*this);
  86. }
  87. float length() const {
  88. return sqrtf(squareLength());
  89. }
  90. Vector& normalize() {
  91. *this *= 1.0f / length();
  92. return *this;
  93. }
  94. float& operator[](int index) {
  95. return values[index];
  96. }
  97. const float& operator[](int index) const {
  98. return values[index];
  99. }
  100. template<int L>
  101. void toString(StringBuffer<L>& s) const {
  102. s.append("[");
  103. for(int i = 0; i < N - 1; i++) {
  104. s.append(values[i]);
  105. s.append(", ");
  106. }
  107. if(N > 0) {
  108. s.append(values[N - 1]);
  109. }
  110. s.append("]");
  111. }
  112. };
  113. template<int N>
  114. Vector<N> operator*(float factor, const Vector<N>& v) {
  115. return v * factor;
  116. }
  117. template<>
  118. Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle);
  119. template<>
  120. Vector<3> Vector<3>::cross(const Vector<3>& other) const;
  121. typedef Vector<4> Vector4;
  122. typedef Vector<3> Vector3;
  123. typedef Vector<2> Vector2;
  124. #endif