Vector.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef VECTOR_H
  2. #define VECTOR_H
  3. #include <cmath>
  4. #include "common/utils/String.h"
  5. #include "client/math/Matrix.h"
  6. template<uint N>
  7. struct Vector final {
  8. Vector() {
  9. for(uint 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& set(float, float, float) = delete;
  16. Vector& setAngles(float, float) = delete;
  17. Vector cross(const Vector&) const = delete;
  18. Vector& operator+=(const Vector& other) {
  19. for(uint i = 0; i < N; i++) {
  20. values[i] += other.values[i];
  21. }
  22. return *this;
  23. }
  24. Vector operator+(const Vector& other) const {
  25. Vector v = *this;
  26. v += other;
  27. return v;
  28. }
  29. Vector& operator-=(const Vector& other) {
  30. for(uint i = 0; i < N; i++) {
  31. values[i] -= other.values[i];
  32. }
  33. return *this;
  34. }
  35. Vector operator-() const {
  36. Vector v = *this;
  37. for(uint i = 0; i < N; i++) {
  38. v.values[i] = -v.values[i];
  39. }
  40. return v;
  41. }
  42. Vector operator-(const Vector& other) const {
  43. Vector v = *this;
  44. v -= other;
  45. return v;
  46. }
  47. Vector& operator*=(float factor) {
  48. for(uint i = 0; i < N; i++) {
  49. values[i] *= factor;
  50. }
  51. return *this;
  52. }
  53. Vector operator*(float factor) const {
  54. Vector v = *this;
  55. v *= factor;
  56. return v;
  57. }
  58. float dot(const Vector& v) const {
  59. float length = 0.0f;
  60. for(uint i = 0; i < N; i++) {
  61. length += values[i] * v.values[i];
  62. }
  63. return length;
  64. }
  65. float squareLength() const {
  66. return dot(*this);
  67. }
  68. float length() const {
  69. return sqrtf(squareLength());
  70. }
  71. Vector& normalize() {
  72. *this *= 1.0f / length();
  73. return *this;
  74. }
  75. const float& operator[](uint index) const {
  76. return values[index];
  77. }
  78. private:
  79. float values[N];
  80. };
  81. template<uint N>
  82. Vector<N> operator*(float factor, const Vector<N>& v) {
  83. return v * factor;
  84. }
  85. template<> Vector<3>::Vector(float x, float y, float z);
  86. template<> Vector<2>::Vector(float x, float y);
  87. template<> Vector<3>& Vector<3>::setAngles(float lengthAngle, float widthAngle);
  88. template<> Vector<3>::Vector(float lengthAngle, float widthAngle);
  89. template<> Vector<3>& Vector<3>::set(float x, float y, float z);
  90. template<> Vector<3> Vector<3>::cross(const Vector<3>& other) const;
  91. Vector<3> operator*(const Matrix& m, const Vector<3>& v);
  92. template<uint N>
  93. String& operator+=(String& buffer, const Vector<N>& v) {
  94. buffer.append('(');
  95. if(N > 0) {
  96. buffer.append(v[0]);
  97. }
  98. for(uint i = 1; i < N; i++) {
  99. buffer.append(", ").append(v[i]);
  100. }
  101. buffer.append(')');
  102. return buffer;
  103. }
  104. typedef Vector<3> Vector3;
  105. typedef Vector<2> Vector2;
  106. #endif