Vector.h 2.8 KB

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