Vector3.pkg 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. $#include "Vector3.h"
  2. /// Three-dimensional vector.
  3. class Vector3
  4. {
  5. public:
  6. /// Construct undefined.
  7. Vector3()
  8. {
  9. }
  10. /// Copy-construct from another vector.
  11. Vector3(const Vector3& vector) :
  12. x_(vector.x_),
  13. y_(vector.y_),
  14. z_(vector.z_)
  15. {
  16. }
  17. /// Construct from a two-dimensional vector and the Z coordinate.
  18. Vector3(const Vector2& vector, float z) :
  19. x_(vector.x_),
  20. y_(vector.y_),
  21. z_(z)
  22. {
  23. }
  24. /// Construct from coordinates.
  25. Vector3(float x, float y, float z) :
  26. x_(x),
  27. y_(y),
  28. z_(z)
  29. {
  30. }
  31. /// Test for equality with another vector without epsilon.
  32. bool operator == (const Vector3& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_; }
  33. /// Add a vector.
  34. Vector3 operator + (const Vector3& rhs) const { return Vector3(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_); }
  35. /// Return negation.
  36. Vector3 operator - () const { return Vector3(-x_, -y_, -z_); }
  37. /// Subtract a vector.
  38. Vector3 operator - (const Vector3& rhs) const { return Vector3(x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_); }
  39. /// Multiply with a scalar.
  40. Vector3 operator * (float rhs) const { return Vector3(x_ * rhs, y_ * rhs, z_ * rhs); }
  41. /// Multiply with a vector.
  42. Vector3 operator * (const Vector3& rhs) const { return Vector3(x_ * rhs.x_, y_ * rhs.y_, z_ * rhs.z_); }
  43. /// Divide by a scalar.
  44. Vector3 operator / (float rhs) const { return Vector3(x_ / rhs, y_ / rhs, z_ / rhs); }
  45. /// Divide by a vector.
  46. Vector3 operator / (const Vector3& rhs) const { return Vector3(x_ / rhs.x_, y_ / rhs.y_, z_ / rhs.z_); }
  47. Vector3 operator / (const Vector3& rhs) const;
  48. /// Normalize to unit length and return the previous length.
  49. float Normalize()
  50. {
  51. float len = Length();
  52. if (len >= M_EPSILON)
  53. {
  54. float invLen = 1.0f / len;
  55. x_ *= invLen;
  56. y_ *= invLen;
  57. z_ *= invLen;
  58. }
  59. return len;
  60. }
  61. /// Return length.
  62. float Length() const { return sqrtf(x_ * x_ + y_ * y_ + z_ * z_); }
  63. /// Return squared length.
  64. float LengthSquared() const { return x_ * x_ + y_ * y_ + z_ * z_; }
  65. /// Calculate dot product.
  66. float DotProduct(const Vector3& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
  67. /// Calculate absolute dot product.
  68. float AbsDotProduct(const Vector3& rhs) const { return Urho3D::Abs(x_ * rhs.x_) + Urho3D::Abs(y_ * rhs.y_) + Urho3D::Abs(z_ * rhs.z_); }
  69. /// Calculate cross product.
  70. Vector3 CrossProduct(const Vector3& rhs) const
  71. {
  72. return Vector3(
  73. y_ * rhs.z_ - z_ * rhs.y_,
  74. z_ * rhs.x_ - x_ * rhs.z_,
  75. x_ * rhs.y_ - y_ * rhs.x_
  76. );
  77. }
  78. /// Return absolute vector.
  79. Vector3 Abs() const { return Vector3(Urho3D::Abs(x_), Urho3D::Abs(y_), Urho3D::Abs(z_)); }
  80. /// Linear interpolation with another vector.
  81. Vector3 Lerp(const Vector3& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  82. /// Test for equality with another vector with epsilon.
  83. bool Equals(const Vector3& rhs) const { return Urho3D::Equals(x_, rhs.x_) && Urho3D::Equals(y_, rhs.y_) && Urho3D::Equals(z_, rhs.z_); }
  84. /// Return normalized to unit length.
  85. Vector3 Normalized() const
  86. {
  87. float len = Length();
  88. if (len >= M_EPSILON)
  89. return *this * (1.0f / len);
  90. else
  91. return *this;
  92. }
  93. /// Return as string.
  94. String ToString() const;
  95. /// X coordinate.
  96. float x_ @ x;
  97. /// Y coordinate.
  98. float y_ @ y;
  99. /// Z coordinate.
  100. float z_ @ z;
  101. /// Zero vector.
  102. static const Vector3 ZERO;
  103. /// (-1,0,0) vector.
  104. static const Vector3 LEFT;
  105. /// (1,0,0) vector.
  106. static const Vector3 RIGHT;
  107. /// (0,1,0) vector.
  108. static const Vector3 UP;
  109. /// (0,-1,0) vector.
  110. static const Vector3 DOWN;
  111. /// (0,0,1) vector.
  112. static const Vector3 FORWARD;
  113. /// (0,0,-1) vector.
  114. static const Vector3 BACK;
  115. /// (1,1,1) vector.
  116. static const Vector3 ONE;
  117. };