Vector3.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // Copyright (c) 2008-2015 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 "../Math/Vector2.h"
  24. namespace Atomic
  25. {
  26. /// Three-dimensional vector.
  27. class ATOMIC_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 (!Atomic::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
  174. {
  175. return Atomic::Abs(x_ * rhs.x_) + Atomic::Abs(y_ * rhs.y_) + Atomic::Abs(z_ * rhs.z_);
  176. }
  177. /// Calculate cross product.
  178. Vector3 CrossProduct(const Vector3& rhs) const
  179. {
  180. return Vector3(
  181. y_ * rhs.z_ - z_ * rhs.y_,
  182. z_ * rhs.x_ - x_ * rhs.z_,
  183. x_ * rhs.y_ - y_ * rhs.x_
  184. );
  185. }
  186. /// Return absolute vector.
  187. Vector3 Abs() const { return Vector3(Atomic::Abs(x_), Atomic::Abs(y_), Atomic::Abs(z_)); }
  188. /// Linear interpolation with another vector.
  189. Vector3 Lerp(const Vector3& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  190. /// Test for equality with another vector with epsilon.
  191. bool Equals(const Vector3& rhs) const
  192. {
  193. return Atomic::Equals(x_, rhs.x_) && Atomic::Equals(y_, rhs.y_) && Atomic::Equals(z_, rhs.z_);
  194. }
  195. /// Returns the angle between this vector and another vector in degrees.
  196. float Angle(const Vector3& rhs) const { return Atomic::Acos(DotProduct(rhs) / (Length() * rhs.Length())); }
  197. /// Return whether is NaN.
  198. bool IsNaN() const { return Atomic::IsNaN(x_) || Atomic::IsNaN(y_) || Atomic::IsNaN(z_); }
  199. /// Return normalized to unit length.
  200. Vector3 Normalized() const
  201. {
  202. float lenSquared = LengthSquared();
  203. if (!Atomic::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  204. {
  205. float invLen = 1.0f / sqrtf(lenSquared);
  206. return *this * invLen;
  207. }
  208. else
  209. return *this;
  210. }
  211. /// Return float data.
  212. const float* Data() const { return &x_; }
  213. /// Return as string.
  214. String ToString() const;
  215. /// X coordinate.
  216. float x_;
  217. /// Y coordinate.
  218. float y_;
  219. /// Z coordinate.
  220. float z_;
  221. /// Zero vector.
  222. static const Vector3 ZERO;
  223. /// (-1,0,0) vector.
  224. static const Vector3 LEFT;
  225. /// (1,0,0) vector.
  226. static const Vector3 RIGHT;
  227. /// (0,1,0) vector.
  228. static const Vector3 UP;
  229. /// (0,-1,0) vector.
  230. static const Vector3 DOWN;
  231. /// (0,0,1) vector.
  232. static const Vector3 FORWARD;
  233. /// (0,0,-1) vector.
  234. static const Vector3 BACK;
  235. /// (1,1,1) vector.
  236. static const Vector3 ONE;
  237. };
  238. /// Multiply Vector3 with a scalar.
  239. inline Vector3 operator *(float lhs, const Vector3& rhs) { return rhs * lhs; }
  240. }