Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Vector3d.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*!
  2. * \file src/Vector3d.cpp
  3. * \brief 3D Math vector
  4. *
  5. * \author Mongoose
  6. */
  7. #include <math.h>
  8. #include "Vector3d.h"
  9. Vector3d::Vector3d() {
  10. mVec[0] = mVec[1] = mVec[2] = 0.0f;
  11. }
  12. Vector3d::Vector3d(vec3_t v) {
  13. mVec[0] = v[0];
  14. mVec[1] = v[1];
  15. mVec[2] = v[2];
  16. }
  17. Vector3d::Vector3d(vec_t x, vec_t y, vec_t z) {
  18. mVec[0] = x;
  19. mVec[1] = y;
  20. mVec[2] = z;
  21. }
  22. Vector3d::~Vector3d() {
  23. }
  24. vec_t Vector3d::dot(const Vector3d &u, const Vector3d &v) {
  25. return (u.mVec[0]*v.mVec[0] + u.mVec[1]*v.mVec[1] + u.mVec[2]*v.mVec[2]);
  26. }
  27. Vector3d Vector3d::cross(const Vector3d &u, const Vector3d &v) {
  28. return Vector3d(u.mVec[1] * v.mVec[2] - u.mVec[2] * v.mVec[1],
  29. u.mVec[2] * v.mVec[0] - u.mVec[0] * v.mVec[2],
  30. u.mVec[0] * v.mVec[1] - u.mVec[1] * v.mVec[0]);
  31. }
  32. vec_t Vector3d::magnitude() {
  33. return sqrtf(mVec[0]*mVec[0] + mVec[1]*mVec[1] + mVec[2]*mVec[2]);
  34. }
  35. Vector3d Vector3d::unit() {
  36. vec_t norm = magnitude();
  37. return Vector3d(mVec[0] / norm,
  38. mVec[1] / norm,
  39. mVec[2] / norm);
  40. }
  41. Vector3d Vector3d::zeroVector() {
  42. return Vector3d(0, 0, 0);
  43. }
  44. Vector3d Vector3d::operator +(const Vector3d &v) {
  45. return Vector3d(mVec[0] + v.mVec[0],
  46. mVec[1] + v.mVec[1],
  47. mVec[2] + v.mVec[2]);
  48. }
  49. Vector3d Vector3d::operator -(const Vector3d &v) {
  50. return Vector3d(mVec[0] - v.mVec[0],
  51. mVec[1] - v.mVec[1],
  52. mVec[2] - v.mVec[2]);
  53. }
  54. Vector3d Vector3d::operator -() {
  55. return Vector3d(-mVec[0],
  56. -mVec[1],
  57. -mVec[2]);
  58. }
  59. Vector3d Vector3d::operator *(vec_t s) {
  60. return Vector3d(s * mVec[0],
  61. s * mVec[1],
  62. s * mVec[2]);
  63. }
  64. Vector3d Vector3d::operator /(vec_t s) {
  65. return Vector3d(mVec[0] / s,
  66. mVec[1] / s,
  67. mVec[2] / s);
  68. }
  69. vec_t Vector3d::operator *(const Vector3d &v) {
  70. return dot(*this, v);
  71. }
  72. void Vector3d::normalize() {
  73. vec_t norm = magnitude();
  74. mVec[0] /= norm;
  75. mVec[1] /= norm;
  76. mVec[2] /= norm;
  77. }
  78. void Vector3d::zero() {
  79. mVec[0] = 0;
  80. mVec[1] = 0;
  81. mVec[2] = 0;
  82. }
  83. Vector3d &Vector3d::operator =(const Vector3d &v) {
  84. mVec[0] = v.mVec[0];
  85. mVec[1] = v.mVec[1];
  86. mVec[2] = v.mVec[2];
  87. return *this;
  88. }
  89. Vector3d &Vector3d::operator +=(const Vector3d &v) {
  90. mVec[0] += v.mVec[0];
  91. mVec[1] += v.mVec[1];
  92. mVec[2] += v.mVec[2];
  93. return *this;
  94. }
  95. Vector3d &Vector3d::operator -=(const Vector3d &v) {
  96. mVec[0] -= v.mVec[0];
  97. mVec[1] -= v.mVec[1];
  98. mVec[2] -= v.mVec[2];
  99. return *this;
  100. }
  101. Vector3d &Vector3d::operator *=(vec_t s) {
  102. mVec[0] *= s;
  103. mVec[1] *= s;
  104. mVec[2] *= s;
  105. return *this;
  106. }