Vector3.h 7.1 KB

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