Vector.cpp 2.4 KB

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