Vector.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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, "vector size and amount of float arguments do not match");
  19. for(int i = 0; i < N; i++) {
  20. values[i] = init[i];
  21. }
  22. }
  23. Vector& setAngles(float, float) = delete;
  24. Vector cross(const Vector&) const = delete;
  25. Vector& operator+=(const Vector& other) {
  26. for(int i = 0; i < N; i++) {
  27. values[i] += other.values[i];
  28. }
  29. return *this;
  30. }
  31. Vector operator+(const Vector& other) const {
  32. Vector v = *this;
  33. v += other;
  34. return v;
  35. }
  36. Vector& operator-=(const Vector& other) {
  37. for(int i = 0; i < N; i++) {
  38. values[i] -= other.values[i];
  39. }
  40. return *this;
  41. }
  42. Vector operator-() const {
  43. Vector v = *this;
  44. for(int i = 0; i < N; i++) {
  45. v.values[i] = -v.values[i];
  46. }
  47. return v;
  48. }
  49. Vector operator-(const Vector& other) const {
  50. Vector v = *this;
  51. v -= other;
  52. return v;
  53. }
  54. Vector& operator*=(float factor) {
  55. for(int i = 0; i < N; i++) {
  56. values[i] *= factor;
  57. }
  58. return *this;
  59. }
  60. Vector operator*(float factor) const {
  61. Vector v = *this;
  62. v *= factor;
  63. return v;
  64. }
  65. float dot(const Vector& v) const {
  66. float length = 0.0f;
  67. for(int i = 0; i < N; i++) {
  68. length += values[i] * v.values[i];
  69. }
  70. return length;
  71. }
  72. float squareLength() const {
  73. return dot(*this);
  74. }
  75. float length() const {
  76. return sqrtf(squareLength());
  77. }
  78. Vector& normalize() {
  79. *this *= 1.0f / length();
  80. return *this;
  81. }
  82. float& operator[](int index) {
  83. return values[index];
  84. }
  85. const float& operator[](int index) const {
  86. return values[index];
  87. }
  88. template<int L>
  89. void toString(StringBuffer<L>& s) const {
  90. s.append("[");
  91. for(int i = 0; i < N - 1; i++) {
  92. s.append(values[i]);
  93. s.append(", ");
  94. }
  95. if(N > 0) {
  96. s.append(values[N - 1]);
  97. }
  98. s.append("]");
  99. }
  100. };
  101. template<int N>
  102. Vector<N> operator*(float factor, const Vector<N>& v) {
  103. return v * factor;
  104. }
  105. template<> Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle);
  106. template<> Vector<3> Vector<3>::cross(const Vector<3>& other) const;
  107. typedef Vector<4> Vector4;
  108. typedef Vector<3> Vector3;
  109. typedef Vector<2> Vector2;
  110. #endif