Vector3.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Vector2.h"
  24. namespace Urho3D
  25. {
  26. /// Three-dimensional vector.
  27. class URHO3D_API Vector3
  28. {
  29. public:
  30. /// Construct a zero vector.
  31. Vector3() :
  32. x_(0.0f),
  33. y_(0.0f),
  34. z_(0.0f)
  35. {
  36. }
  37. /// Copy-construct from another vector.
  38. Vector3(const Vector3& vector) :
  39. x_(vector.x_),
  40. y_(vector.y_),
  41. z_(vector.z_)
  42. {
  43. }
  44. /// Construct from a two-dimensional vector and the Z coordinate.
  45. Vector3(const Vector2& vector, float z) :
  46. x_(vector.x_),
  47. y_(vector.y_),
  48. z_(z)
  49. {
  50. }
  51. /// Construct from coordinates.
  52. Vector3(float x, float y, float z) :
  53. x_(x),
  54. y_(y),
  55. z_(z)
  56. {
  57. }
  58. /// Construct from a float array.
  59. Vector3(const float* data) :
  60. x_(data[0]),
  61. y_(data[1]),
  62. z_(data[2])
  63. {
  64. }
  65. /// Assign from another vector.
  66. Vector3& operator = (const Vector3& rhs)
  67. {
  68. x_ = rhs.x_;
  69. y_ = rhs.y_;
  70. z_ = rhs.z_;
  71. return *this;
  72. }
  73. /// Test for equality with another vector without epsilon.
  74. bool operator == (const Vector3& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_; }
  75. /// Test for inequality with another vector without epsilon.
  76. bool operator != (const Vector3& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_; }
  77. /// Add a vector.
  78. Vector3 operator + (const Vector3& rhs) const { return Vector3(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_); }
  79. /// Return negation.
  80. Vector3 operator - () const { return Vector3(-x_, -y_, -z_); }
  81. /// Subtract a vector.
  82. Vector3 operator - (const Vector3& rhs) const { return Vector3(x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_); }
  83. /// Multiply with a scalar.
  84. Vector3 operator * (float rhs) const { return Vector3(x_ * rhs, y_ * rhs, z_ * rhs); }
  85. /// Multiply with a vector.
  86. Vector3 operator * (const Vector3& rhs) const { return Vector3(x_ * rhs.x_, y_ * rhs.y_, z_ * rhs.z_); }
  87. /// Divide by a scalar.
  88. Vector3 operator / (float rhs) const { return Vector3(x_ / rhs, y_ / rhs, z_ / rhs); }
  89. /// Divide by a vector.
  90. Vector3 operator / (const Vector3& rhs) const { return Vector3(x_ / rhs.x_, y_ / rhs.y_, z_ / rhs.z_); }
  91. /// Add-assign a vector.
  92. Vector3& operator += (const Vector3& rhs)
  93. {
  94. x_ += rhs.x_;
  95. y_ += rhs.y_;
  96. z_ += rhs.z_;
  97. return *this;
  98. }
  99. /// Subtract-assign a vector.
  100. Vector3& operator -= (const Vector3& rhs)
  101. {
  102. x_ -= rhs.x_;
  103. y_ -= rhs.y_;
  104. z_ -= rhs.z_;
  105. return *this;
  106. }
  107. /// Multiply-assign a scalar.
  108. Vector3& operator *= (float rhs)
  109. {
  110. x_ *= rhs;
  111. y_ *= rhs;
  112. z_ *= rhs;
  113. return *this;
  114. }
  115. /// Multiply-assign a vector.
  116. Vector3& operator *= (const Vector3& rhs)
  117. {
  118. x_ *= rhs.x_;
  119. y_ *= rhs.y_;
  120. z_ *= rhs.z_;
  121. return *this;
  122. }
  123. /// Divide-assign a scalar.
  124. Vector3& operator /= (float rhs)
  125. {
  126. float invRhs = 1.0f / rhs;
  127. x_ *= invRhs;
  128. y_ *= invRhs;
  129. z_ *= invRhs;
  130. return *this;
  131. }
  132. /// Divide-assign a vector.
  133. Vector3& operator /= (const Vector3& rhs)
  134. {
  135. x_ /= rhs.x_;
  136. y_ /= rhs.y_;
  137. z_ /= rhs.z_;
  138. return *this;
  139. }
  140. /// Normalize to unit length.
  141. void Normalize()
  142. {
  143. float lenSquared = LengthSquared();
  144. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  145. {
  146. float invLen = 1.0f / sqrtf(lenSquared);
  147. x_ *= invLen;
  148. y_ *= invLen;
  149. z_ *= invLen;
  150. }
  151. }
  152. /// Return length.
  153. float Length() const { return sqrtf(x_ * x_ + y_ * y_ + z_ * z_); }
  154. /// Return squared length.
  155. float LengthSquared() const { return x_ * x_ + y_ * y_ + z_ * z_; }
  156. /// Calculate dot product.
  157. float DotProduct(const Vector3& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
  158. /// Calculate absolute dot product.
  159. float AbsDotProduct(const Vector3& rhs) const { return Urho3D::Abs(x_ * rhs.x_) + Urho3D::Abs(y_ * rhs.y_) + Urho3D::Abs(z_ * rhs.z_); }
  160. /// Calculate cross product.
  161. Vector3 CrossProduct(const Vector3& rhs) const
  162. {
  163. return Vector3(
  164. y_ * rhs.z_ - z_ * rhs.y_,
  165. z_ * rhs.x_ - x_ * rhs.z_,
  166. x_ * rhs.y_ - y_ * rhs.x_
  167. );
  168. }
  169. /// Return absolute vector.
  170. Vector3 Abs() const { return Vector3(Urho3D::Abs(x_), Urho3D::Abs(y_), Urho3D::Abs(z_)); }
  171. /// Linear interpolation with another vector.
  172. Vector3 Lerp(const Vector3& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  173. /// Test for equality with another vector with epsilon.
  174. bool Equals(const Vector3& rhs) const { return Urho3D::Equals(x_, rhs.x_) && Urho3D::Equals(y_, rhs.y_) && Urho3D::Equals(z_, rhs.z_); }
  175. /// Returns the angle between this vector and another vector in degrees.
  176. float Angle(const Vector3& rhs) const { return Urho3D::Acos(DotProduct(rhs) / (Length() * rhs.Length() ) ); }
  177. /// Return whether is NaN.
  178. bool IsNaN() const { return Urho3D::IsNaN(x_) || Urho3D::IsNaN(y_) || Urho3D::IsNaN(z_); }
  179. /// Return normalized to unit length.
  180. Vector3 Normalized() const
  181. {
  182. float lenSquared = LengthSquared();
  183. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  184. {
  185. float invLen = 1.0f / sqrtf(lenSquared);
  186. return *this * invLen;
  187. }
  188. else
  189. return *this;
  190. }
  191. /// Return float data.
  192. const float* Data() const { return &x_; }
  193. /// Return as string.
  194. String ToString() const;
  195. /// X coordinate.
  196. float x_;
  197. /// Y coordinate.
  198. float y_;
  199. /// Z coordinate.
  200. float z_;
  201. /// Zero vector.
  202. static const Vector3 ZERO;
  203. /// (-1,0,0) vector.
  204. static const Vector3 LEFT;
  205. /// (1,0,0) vector.
  206. static const Vector3 RIGHT;
  207. /// (0,1,0) vector.
  208. static const Vector3 UP;
  209. /// (0,-1,0) vector.
  210. static const Vector3 DOWN;
  211. /// (0,0,1) vector.
  212. static const Vector3 FORWARD;
  213. /// (0,0,-1) vector.
  214. static const Vector3 BACK;
  215. /// (1,1,1) vector.
  216. static const Vector3 ONE;
  217. };
  218. /// Multiply Vector3 with a scalar.
  219. inline Vector3 operator * (float lhs, const Vector3& rhs) { return rhs * lhs; }
  220. }