Vector3.h 7.5 KB

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