Vector.h 2.9 KB

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