Vector3.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 a two-dimensional vector (for Urho2D).
  52. Vector3(const Vector2& vector) :
  53. x_(vector.x_),
  54. y_(vector.y_),
  55. z_(0.0f)
  56. {
  57. }
  58. /// Construct from coordinates.
  59. Vector3(float x, float y, float z) :
  60. x_(x),
  61. y_(y),
  62. z_(z)
  63. {
  64. }
  65. /// Construct from two-dimensional coordinates (for Urho2D).
  66. Vector3(float x, float y) :
  67. x_(x),
  68. y_(y),
  69. z_(0.0f)
  70. {
  71. }
  72. /// Construct from a float array.
  73. Vector3(const float* data) :
  74. x_(data[0]),
  75. y_(data[1]),
  76. z_(data[2])
  77. {
  78. }
  79. /// Assign from another vector.
  80. Vector3& operator = (const Vector3& rhs)
  81. {
  82. x_ = rhs.x_;
  83. y_ = rhs.y_;
  84. z_ = rhs.z_;
  85. return *this;
  86. }
  87. /// Test for equality with another vector without epsilon.
  88. bool operator == (const Vector3& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_; }
  89. /// Test for inequality with another vector without epsilon.
  90. bool operator != (const Vector3& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_; }
  91. /// Add a vector.
  92. Vector3 operator + (const Vector3& rhs) const { return Vector3(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_); }
  93. /// Return negation.
  94. Vector3 operator - () const { return Vector3(-x_, -y_, -z_); }
  95. /// Subtract a vector.
  96. Vector3 operator - (const Vector3& rhs) const { return Vector3(x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_); }
  97. /// Multiply with a scalar.
  98. Vector3 operator * (float rhs) const { return Vector3(x_ * rhs, y_ * rhs, z_ * rhs); }
  99. /// Multiply with a vector.
  100. Vector3 operator * (const Vector3& rhs) const { return Vector3(x_ * rhs.x_, y_ * rhs.y_, z_ * rhs.z_); }
  101. /// Divide by a scalar.
  102. Vector3 operator / (float rhs) const { return Vector3(x_ / rhs, y_ / rhs, z_ / rhs); }
  103. /// Divide by a vector.
  104. Vector3 operator / (const Vector3& rhs) const { return Vector3(x_ / rhs.x_, y_ / rhs.y_, z_ / rhs.z_); }
  105. /// Add-assign a vector.
  106. Vector3& operator += (const Vector3& rhs)
  107. {
  108. x_ += rhs.x_;
  109. y_ += rhs.y_;
  110. z_ += rhs.z_;
  111. return *this;
  112. }
  113. /// Subtract-assign a vector.
  114. Vector3& operator -= (const Vector3& rhs)
  115. {
  116. x_ -= rhs.x_;
  117. y_ -= rhs.y_;
  118. z_ -= rhs.z_;
  119. return *this;
  120. }
  121. /// Multiply-assign a scalar.
  122. Vector3& operator *= (float rhs)
  123. {
  124. x_ *= rhs;
  125. y_ *= rhs;
  126. z_ *= rhs;
  127. return *this;
  128. }
  129. /// Multiply-assign a vector.
  130. Vector3& operator *= (const Vector3& rhs)
  131. {
  132. x_ *= rhs.x_;
  133. y_ *= rhs.y_;
  134. z_ *= rhs.z_;
  135. return *this;
  136. }
  137. /// Divide-assign a scalar.
  138. Vector3& operator /= (float rhs)
  139. {
  140. float invRhs = 1.0f / rhs;
  141. x_ *= invRhs;
  142. y_ *= invRhs;
  143. z_ *= invRhs;
  144. return *this;
  145. }
  146. /// Divide-assign a vector.
  147. Vector3& operator /= (const Vector3& rhs)
  148. {
  149. x_ /= rhs.x_;
  150. y_ /= rhs.y_;
  151. z_ /= rhs.z_;
  152. return *this;
  153. }
  154. /// Normalize to unit length.
  155. void Normalize()
  156. {
  157. float lenSquared = LengthSquared();
  158. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  159. {
  160. float invLen = 1.0f / sqrtf(lenSquared);
  161. x_ *= invLen;
  162. y_ *= invLen;
  163. z_ *= invLen;
  164. }
  165. }
  166. /// Return length.
  167. float Length() const { return sqrtf(x_ * x_ + y_ * y_ + z_ * z_); }
  168. /// Return squared length.
  169. float LengthSquared() const { return x_ * x_ + y_ * y_ + z_ * z_; }
  170. /// Calculate dot product.
  171. float DotProduct(const Vector3& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
  172. /// Calculate absolute dot product.
  173. float AbsDotProduct(const Vector3& rhs) const { return Urho3D::Abs(x_ * rhs.x_) + Urho3D::Abs(y_ * rhs.y_) + Urho3D::Abs(z_ * rhs.z_); }
  174. /// Calculate cross product.
  175. Vector3 CrossProduct(const Vector3& rhs) const
  176. {
  177. return Vector3(
  178. y_ * rhs.z_ - z_ * rhs.y_,
  179. z_ * rhs.x_ - x_ * rhs.z_,
  180. x_ * rhs.y_ - y_ * rhs.x_
  181. );
  182. }
  183. /// Return absolute vector.
  184. Vector3 Abs() const { return Vector3(Urho3D::Abs(x_), Urho3D::Abs(y_), Urho3D::Abs(z_)); }
  185. /// Linear interpolation with another vector.
  186. Vector3 Lerp(const Vector3& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  187. /// Test for equality with another vector with epsilon.
  188. bool Equals(const Vector3& rhs) const { return Urho3D::Equals(x_, rhs.x_) && Urho3D::Equals(y_, rhs.y_) && Urho3D::Equals(z_, rhs.z_); }
  189. /// Returns the angle between this vector and another vector in degrees.
  190. float Angle(const Vector3& rhs) const { return Urho3D::Acos(DotProduct(rhs) / (Length() * rhs.Length() ) ); }
  191. /// Return whether is NaN.
  192. bool IsNaN() const { return Urho3D::IsNaN(x_) || Urho3D::IsNaN(y_) || Urho3D::IsNaN(z_); }
  193. /// Return normalized to unit length.
  194. Vector3 Normalized() const
  195. {
  196. float lenSquared = LengthSquared();
  197. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  198. {
  199. float invLen = 1.0f / sqrtf(lenSquared);
  200. return *this * invLen;
  201. }
  202. else
  203. return *this;
  204. }
  205. /// Return float data.
  206. const float* Data() const { return &x_; }
  207. /// Return as string.
  208. String ToString() const;
  209. /// X coordinate.
  210. float x_;
  211. /// Y coordinate.
  212. float y_;
  213. /// Z coordinate.
  214. float z_;
  215. /// Zero vector.
  216. static const Vector3 ZERO;
  217. /// (-1,0,0) vector.
  218. static const Vector3 LEFT;
  219. /// (1,0,0) vector.
  220. static const Vector3 RIGHT;
  221. /// (0,1,0) vector.
  222. static const Vector3 UP;
  223. /// (0,-1,0) vector.
  224. static const Vector3 DOWN;
  225. /// (0,0,1) vector.
  226. static const Vector3 FORWARD;
  227. /// (0,0,-1) vector.
  228. static const Vector3 BACK;
  229. /// (1,1,1) vector.
  230. static const Vector3 ONE;
  231. };
  232. /// Multiply Vector3 with a scalar.
  233. inline Vector3 operator * (float lhs, const Vector3& rhs) { return rhs * lhs; }
  234. }