Vector.h 3.1 KB

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