Vector3.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 undefined.
  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. float invRhs = 1.0f / rhs;
  123. x_ *= invRhs;
  124. y_ *= invRhs;
  125. z_ *= invRhs;
  126. return *this;
  127. }
  128. /// Divide-assign a vector.
  129. Vector3& operator /= (const Vector3& rhs)
  130. {
  131. x_ /= rhs.x_;
  132. y_ /= rhs.y_;
  133. z_ /= rhs.z_;
  134. return *this;
  135. }
  136. /// Normalize to unit length and return the previous length.
  137. float Normalize()
  138. {
  139. float len = Length();
  140. if (len >= M_EPSILON)
  141. {
  142. float invLen = 1.0f / len;
  143. x_ *= invLen;
  144. y_ *= invLen;
  145. z_ *= invLen;
  146. }
  147. return len;
  148. }
  149. /// Normalize to unit length using fast inverse square root.
  150. void NormalizeFast()
  151. {
  152. float invLen = FastInvSqrt(x_ * x_ + y_ * y_ + z_ * z_);
  153. x_ *= invLen;
  154. y_ *= invLen;
  155. z_ *= invLen;
  156. }
  157. /// Return length.
  158. float Length() const { return sqrtf(x_ * x_ + y_ * y_ + z_ * z_); }
  159. /// Return length using fast square root.
  160. float LengthFast() const { return FastSqrt(x_ * x_ + y_ * y_ + z_ * z_); }
  161. /// Return squared length.
  162. float LengthSquared() const { return x_ * x_ + y_ * y_ + z_ * z_; }
  163. /// Calculate dot product.
  164. float DotProduct(const Vector3& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
  165. /// Calculate absolute dot product.
  166. float AbsDotProduct(const Vector3& rhs) const { return fabsf(x_ * rhs.x_) + fabsf(y_ * rhs.y_) + fabsf(z_ * rhs.z_); }
  167. /// Calculate cross product.
  168. Vector3 CrossProduct(const Vector3& rhs) const
  169. {
  170. return Vector3(
  171. y_ * rhs.z_ - z_ * rhs.y_,
  172. z_ * rhs.x_ - x_ * rhs.z_,
  173. x_ * rhs.y_ - y_ * rhs.x_
  174. );
  175. }
  176. /// Return absolute vector.
  177. Vector3 Abs() const { return Vector3(fabsf(x_), fabsf(y_), fabsf(z_)); }
  178. /// Linear interpolation with another vector.
  179. Vector3 Lerp(const Vector3& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  180. /// Return normalized to unit length.
  181. Vector3 Normalized() const
  182. {
  183. float len = Length();
  184. if (len >= M_EPSILON)
  185. return *this * (1.0f / len);
  186. else
  187. return *this;
  188. }
  189. /// Return normalized to unit length using fast inverse square root.
  190. Vector3 NormalizedFast() const { return *this * FastInvSqrt(x_ * x_ + y_ * y_ + z_ * z_); }
  191. /// Return float data.
  192. const float* GetData() 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; }