Vector.h 2.8 KB

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