Vector.cpp 2.4 KB

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