Vector3.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // Copyright (c) 2008-2013 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 undefined.
  31. Vector3()
  32. {
  33. }
  34. /// Copy-construct from another vector.
  35. Vector3(const Vector3& vector) :
  36. x_(vector.x_),
  37. y_(vector.y_),
  38. z_(vector.z_)
  39. {
  40. }
  41. /// Construct from a two-dimensional vector and the Z coordinate.
  42. Vector3(const Vector2& vector, float z) :
  43. x_(vector.x_),
  44. y_(vector.y_),
  45. z_(z)
  46. {
  47. }
  48. /// Construct from coordinates.
  49. Vector3(float x, float y, float z) :
  50. x_(x),
  51. y_(y),
  52. z_(z)
  53. {
  54. }
  55. /// Construct from a float array.
  56. Vector3(const float* data) :
  57. x_(data[0]),
  58. y_(data[1]),
  59. z_(data[2])
  60. {
  61. }
  62. /// Assign from another vector.
  63. Vector3& operator = (const Vector3& rhs)
  64. {
  65. x_ = rhs.x_;
  66. y_ = rhs.y_;
  67. z_ = rhs.z_;
  68. return *this;
  69. }
  70. /// Test for equality with another vector without epsilon.
  71. bool operator == (const Vector3& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_; }
  72. /// Test for inequality with another vector without epsilon.
  73. bool operator != (const Vector3& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_; }
  74. /// Add a vector.
  75. Vector3 operator + (const Vector3& rhs) const { return Vector3(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_); }
  76. /// Return negation.
  77. Vector3 operator - () const { return Vector3(-x_, -y_, -z_); }
  78. /// Subtract a vector.
  79. Vector3 operator - (const Vector3& rhs) const { return Vector3(x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_); }
  80. /// Multiply with a scalar.
  81. Vector3 operator * (float rhs) const { return Vector3(x_ * rhs, y_ * rhs, z_ * rhs); }
  82. /// Multiply with a vector.
  83. Vector3 operator * (const Vector3& rhs) const { return Vector3(x_ * rhs.x_, y_ * rhs.y_, z_ * rhs.z_); }
  84. /// Divide by a scalar.
  85. Vector3 operator / (float rhs) const { return Vector3(x_ / rhs, y_ / rhs, z_ / rhs); }
  86. /// Divide by a vector.
  87. Vector3 operator / (const Vector3& rhs) const { return Vector3(x_ / rhs.x_, y_ / rhs.y_, z_ / rhs.z_); }
  88. /// Add-assign a vector.
  89. Vector3& operator += (const Vector3& rhs)
  90. {
  91. x_ += rhs.x_;
  92. y_ += rhs.y_;
  93. z_ += rhs.z_;
  94. return *this;
  95. }
  96. /// Subtract-assign a vector.
  97. Vector3& operator -= (const Vector3& rhs)
  98. {
  99. x_ -= rhs.x_;
  100. y_ -= rhs.y_;
  101. z_ -= rhs.z_;
  102. return *this;
  103. }
  104. /// Multiply-assign a scalar.
  105. Vector3& operator *= (float rhs)
  106. {
  107. x_ *= rhs;
  108. y_ *= rhs;
  109. z_ *= rhs;
  110. return *this;
  111. }
  112. /// Multiply-assign a vector.
  113. Vector3& operator *= (const Vector3& rhs)
  114. {
  115. x_ *= rhs.x_;
  116. y_ *= rhs.y_;
  117. z_ *= rhs.z_;
  118. return *this;
  119. }
  120. /// Divide-assign a scalar.
  121. Vector3& operator /= (float rhs)
  122. {
  123. float invRhs = 1.0f / rhs;
  124. x_ *= invRhs;
  125. y_ *= invRhs;
  126. z_ *= invRhs;
  127. return *this;
  128. }
  129. /// Divide-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. /// Normalize to unit length.
  138. void Normalize()
  139. {
  140. float lenSquared = LengthSquared();
  141. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  142. {
  143. float invLen = 1.0f / sqrtf(lenSquared);
  144. x_ *= invLen;
  145. y_ *= invLen;
  146. z_ *= invLen;
  147. }
  148. }
  149. /// Return length.
  150. float Length() const { return sqrtf(x_ * x_ + y_ * y_ + z_ * z_); }
  151. /// Return squared length.
  152. float LengthSquared() const { return x_ * x_ + y_ * y_ + z_ * z_; }
  153. /// Calculate dot product.
  154. float DotProduct(const Vector3& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_ + z_ * rhs.z_; }
  155. /// Calculate absolute dot product.
  156. float AbsDotProduct(const Vector3& rhs) const { return Urho3D::Abs(x_ * rhs.x_) + Urho3D::Abs(y_ * rhs.y_) + Urho3D::Abs(z_ * rhs.z_); }
  157. /// Calculate cross product.
  158. Vector3 CrossProduct(const Vector3& rhs) const
  159. {
  160. return Vector3(
  161. y_ * rhs.z_ - z_ * rhs.y_,
  162. z_ * rhs.x_ - x_ * rhs.z_,
  163. x_ * rhs.y_ - y_ * rhs.x_
  164. );
  165. }
  166. /// Return absolute vector.
  167. Vector3 Abs() const { return Vector3(Urho3D::Abs(x_), Urho3D::Abs(y_), Urho3D::Abs(z_)); }
  168. /// Linear interpolation with another vector.
  169. Vector3 Lerp(const Vector3& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  170. /// Test for equality with another vector with epsilon.
  171. bool Equals(const Vector3& rhs) const { return Urho3D::Equals(x_, rhs.x_) && Urho3D::Equals(y_, rhs.y_) && Urho3D::Equals(z_, rhs.z_); }
  172. /// Return normalized to unit length.
  173. Vector3 Normalized() const
  174. {
  175. float lenSquared = LengthSquared();
  176. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  177. {
  178. float invLen = 1.0f / sqrtf(lenSquared);
  179. return *this * invLen;
  180. }
  181. else
  182. return *this;
  183. }
  184. /// Return float data.
  185. const float* Data() const { return &x_; }
  186. /// Return as string.
  187. String ToString() const;
  188. /// X coordinate.
  189. float x_;
  190. /// Y coordinate.
  191. float y_;
  192. /// Z coordinate.
  193. float z_;
  194. /// Zero vector.
  195. static const Vector3 ZERO;
  196. /// (-1,0,0) vector.
  197. static const Vector3 LEFT;
  198. /// (1,0,0) vector.
  199. static const Vector3 RIGHT;
  200. /// (0,1,0) vector.
  201. static const Vector3 UP;
  202. /// (0,-1,0) vector.
  203. static const Vector3 DOWN;
  204. /// (0,0,1) vector.
  205. static const Vector3 FORWARD;
  206. /// (0,0,-1) vector.
  207. static const Vector3 BACK;
  208. /// (1,1,1) vector.
  209. static const Vector3 ONE;
  210. };
  211. /// Multiply Vector3 with a scalar.
  212. inline Vector3 operator * (float lhs, const Vector3& rhs) { return rhs * lhs; }
  213. }