Vector.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <cmath>
  2. #include "client/math/Vector.h"
  3. Vector::Vector() : x(0), y(0), z(0) {
  4. }
  5. Vector::Vector(float ix, float iy, float iz) : x(ix), y(iy), z(iz) {
  6. }
  7. float Vector::getX() const {
  8. return x;
  9. }
  10. float Vector::getY() const {
  11. return y;
  12. }
  13. float Vector::getZ() const {
  14. return z;
  15. }
  16. Vector& Vector::setX(float ix) {
  17. x = ix;
  18. return *this;
  19. }
  20. Vector& Vector::setY(float iy) {
  21. y = iy;
  22. return *this;
  23. }
  24. Vector& Vector::setZ(float iz) {
  25. z = iz;
  26. return *this;
  27. }
  28. Vector& Vector::set(const Vector& v) {
  29. return set(v.x, v.y, v.z);
  30. }
  31. Vector& Vector::set(float ix, float iy, float iz) {
  32. x = ix;
  33. y = iy;
  34. z = iz;
  35. return *this;
  36. }
  37. Vector& Vector::setInverse(const Vector& v) {
  38. x = -v.x;
  39. y = -v.y;
  40. z = -v.z;
  41. return *this;
  42. }
  43. Vector& Vector::setMul(const Vector& v, float f) {
  44. x = v.x * f;
  45. y = v.y * f;
  46. z = v.z * f;
  47. return *this;
  48. }
  49. Vector& Vector::setAngles(float lengthAngle, float widthAngle) {
  50. lengthAngle = lengthAngle * M_PI / 180.0f;
  51. widthAngle = widthAngle * M_PI / 180.0f;
  52. x = cosf(widthAngle) * sinf(lengthAngle);
  53. y = sinf(widthAngle);
  54. z = cosf(widthAngle) * cosf(lengthAngle);
  55. return *this;
  56. }
  57. Vector& Vector::add(const Vector& v) {
  58. x += v.x;
  59. y += v.y;
  60. z += v.z;
  61. return *this;
  62. }
  63. Vector& Vector::sub(const Vector& v) {
  64. x -= v.x;
  65. y -= v.y;
  66. z -= v.z;
  67. return *this;
  68. }
  69. Vector& Vector::mul(float f) {
  70. x *= f;
  71. y *= f;
  72. z *= f;
  73. return *this;
  74. }
  75. Vector& Vector::mul(const Matrix& m) {
  76. const float* d = m.getValues();
  77. const float w = 1.0f;
  78. float nx = x * d[0] + y * d[4] + z * d[8] + w * d[12];
  79. float ny = x * d[1] + y * d[5] + z * d[9] + w * d[13];
  80. float nz = x * d[2] + y * d[6] + z * d[10] + w * d[14];
  81. float nw = x * d[3] + y * d[7] + z * d[11] + w * d[15];
  82. set(nx / nw, ny / nw, nz / nw);
  83. return *this;
  84. }
  85. Vector& Vector::addMul(const Vector& v, float f) {
  86. x += v.x * f;
  87. y += v.y * f;
  88. z += v.z * f;
  89. return *this;
  90. }
  91. Vector& Vector::cross(float ix, float iy, float iz) {
  92. return set(y * iz - z * iy, z * ix - x * iz, x * iy - y * ix);
  93. }
  94. Vector& Vector::cross(const Vector& v) {
  95. return set(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
  96. }
  97. Vector& Vector::normalize() {
  98. float f = 1.0f / sqrtf(squareLength());
  99. x *= f;
  100. y *= f;
  101. z *= f;
  102. return *this;
  103. }
  104. float Vector::squareLength() const {
  105. return x * x + y * y + z * z;
  106. }
  107. float Vector::dot(const Vector& v) const {
  108. return x * v.x + y * v.y + z * v.z;
  109. }
  110. float Vector::dotInverse(const Vector& v) const {
  111. return x * (-v.x) + y * (-v.y) + z * (-v.z);
  112. }
  113. std::ostream& operator<<(std::ostream& os, const Vector& v) {
  114. return os << "Vector(x = " << v.getX() << ", y = " << v.getY() << ", z = " << v.getZ() << ")";
  115. }