Vector.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. Vector::Vector(float lengthAngle, float widthAngle) {
  8. setAngles(lengthAngle, widthAngle);
  9. }
  10. float Vector::getX() const {
  11. return x;
  12. }
  13. float Vector::getY() const {
  14. return y;
  15. }
  16. float Vector::getZ() const {
  17. return z;
  18. }
  19. Vector& Vector::setX(float ix) {
  20. x = ix;
  21. return *this;
  22. }
  23. Vector& Vector::setY(float iy) {
  24. y = iy;
  25. return *this;
  26. }
  27. Vector& Vector::setZ(float iz) {
  28. z = iz;
  29. return *this;
  30. }
  31. Vector& Vector::set(const Vector& v) {
  32. return set(v.x, v.y, v.z);
  33. }
  34. Vector& Vector::set(float ix, float iy, float iz) {
  35. x = ix;
  36. y = iy;
  37. z = iz;
  38. return *this;
  39. }
  40. Vector& Vector::setInverse(const Vector& v) {
  41. x = -v.x;
  42. y = -v.y;
  43. z = -v.z;
  44. return *this;
  45. }
  46. Vector& Vector::setMul(const Vector& v, float f) {
  47. x = v.x * f;
  48. y = v.y * f;
  49. z = v.z * f;
  50. return *this;
  51. }
  52. Vector& Vector::setAngles(float lengthAngle, float widthAngle) {
  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. return *this;
  59. }
  60. Vector& Vector::add(const Vector& v) {
  61. x += v.x;
  62. y += v.y;
  63. z += v.z;
  64. return *this;
  65. }
  66. Vector& Vector::sub(const Vector& v) {
  67. x -= v.x;
  68. y -= v.y;
  69. z -= v.z;
  70. return *this;
  71. }
  72. Vector& Vector::mul(float f) {
  73. x *= f;
  74. y *= f;
  75. z *= f;
  76. return *this;
  77. }
  78. Vector& Vector::mul(const Matrix& m) {
  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. return *this;
  87. }
  88. Vector& Vector::addMul(const Vector& v, float f) {
  89. x += v.x * f;
  90. y += v.y * f;
  91. z += v.z * f;
  92. return *this;
  93. }
  94. Vector& Vector::cross(float ix, float iy, float iz) {
  95. return set(y * iz - z * iy, z * ix - x * iz, x * iy - y * ix);
  96. }
  97. Vector& Vector::cross(const Vector& v) {
  98. return set(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
  99. }
  100. Vector& Vector::normalize() {
  101. float f = 1.0f / length();
  102. x *= f;
  103. y *= f;
  104. z *= f;
  105. return *this;
  106. }
  107. float Vector::squareLength() const {
  108. return x * x + y * y + z * z;
  109. }
  110. float Vector::length() const {
  111. return sqrtf(squareLength());
  112. }
  113. float Vector::dot(const Vector& v) const {
  114. return x * v.x + y * v.y + z * v.z;
  115. }
  116. float Vector::dotInverse(const Vector& v) const {
  117. return x * (-v.x) + y * (-v.y) + z * (-v.z);
  118. }
  119. void Vector::toString(String& s) const {
  120. s.append("(x = ").append(x).append(", y = ").append(y).append(", z = ").append(z).append(")");
  121. }