Vector3.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #ifndef MATH_VECTOR3_H
  24. #define MATH_VECTOR3_H
  25. #include "Vector2.h"
  26. //! A three-dimensional vector
  27. class Vector3
  28. {
  29. public:
  30. //! Construct an undefined vector
  31. Vector3()
  32. {
  33. }
  34. //! Copy-construct from another vector
  35. Vector3(const Vector3& vector) :
  36. mX(vector.mX),
  37. mY(vector.mY),
  38. mZ(vector.mZ)
  39. {
  40. }
  41. //! Construct from a two-dimensional vector and the Z coordinate
  42. Vector3(const Vector2& vector, float z) :
  43. mX(vector.mX),
  44. mY(vector.mY),
  45. mZ(z)
  46. {
  47. }
  48. //! Construct from coordinates
  49. Vector3(float x, float y, float z) :
  50. mX(x),
  51. mY(y),
  52. mZ(z)
  53. {
  54. }
  55. //! Construct from a float array
  56. Vector3(const float* data) :
  57. mX(data[0]),
  58. mY(data[1]),
  59. mZ(data[2])
  60. {
  61. }
  62. //! Assign from another vector
  63. Vector3& operator = (const Vector3& rhs)
  64. {
  65. mX = rhs.mX;
  66. mY = rhs.mY;
  67. mZ = rhs.mZ;
  68. return *this;
  69. }
  70. //! Test for equality with another vector
  71. bool operator == (const Vector3& rhs) const
  72. {
  73. return (mX == rhs.mX) && (mY == rhs.mY) && (mZ == rhs.mZ);
  74. }
  75. //! Test for inequality with another vector
  76. bool operator != (const Vector3& rhs) const
  77. {
  78. return (mX != rhs.mX) || (mY != rhs.mY) || (mZ != rhs.mZ);
  79. }
  80. //! Add a vector
  81. Vector3 operator + (const Vector3& rhs) const
  82. {
  83. return Vector3(mX + rhs.mX, mY + rhs.mY, mZ + rhs.mZ);
  84. }
  85. //! Return negation
  86. Vector3 operator - () const
  87. {
  88. return Vector3(-mX, -mY, -mZ);
  89. }
  90. //! Subtract a vector
  91. Vector3 operator - (const Vector3& rhs) const
  92. {
  93. return Vector3(mX - rhs.mX, mY - rhs.mY, mZ - rhs.mZ);
  94. }
  95. //! Multiply with a scalar
  96. Vector3 operator * (float rhs) const
  97. {
  98. return Vector3(mX * rhs, mY * rhs, mZ * rhs);
  99. }
  100. //! Multiply with a vector
  101. Vector3 operator * (const Vector3& rhs) const
  102. {
  103. return Vector3(mX * rhs.mX, mY * rhs.mY, mZ * rhs.mZ);
  104. }
  105. //! Divide by a scalar
  106. Vector3 operator / (float rhs) const
  107. {
  108. return Vector3(mX / rhs, mY / rhs, mZ / rhs);
  109. }
  110. //! Divide by a vector
  111. Vector3 operator / (const Vector3& rhs) const
  112. {
  113. return Vector3(mX / rhs.mX, mY / rhs.mY, mZ / rhs.mZ);
  114. }
  115. //! Add-assign a vector
  116. Vector3& operator += (const Vector3& rhs)
  117. {
  118. mX += rhs.mX;
  119. mY += rhs.mY;
  120. mZ += rhs.mZ;
  121. return *this;
  122. }
  123. //! Subtract-assign a vector
  124. Vector3& operator -= (const Vector3& rhs)
  125. {
  126. mX -= rhs.mX;
  127. mY -= rhs.mY;
  128. mZ -= rhs.mZ;
  129. return *this;
  130. }
  131. //! Multiply-assign a scalar
  132. Vector3& operator *= (float rhs)
  133. {
  134. mX *= rhs;
  135. mY *= rhs;
  136. mZ *= rhs;
  137. return *this;
  138. }
  139. //! Multiply-assign a vector
  140. Vector3& operator *= (const Vector3& rhs)
  141. {
  142. mX *= rhs.mX;
  143. mY *= rhs.mY;
  144. mZ *= rhs.mZ;
  145. return *this;
  146. }
  147. //! Divide-assign a scalar
  148. Vector3& operator /= (float rhs)
  149. {
  150. mX /= rhs;
  151. mY /= rhs;
  152. mZ /= rhs;
  153. return *this;
  154. }
  155. //! Divide-assign a vector
  156. Vector3& operator /= (const Vector3& rhs)
  157. {
  158. mX /= rhs.mX;
  159. mY /= rhs.mY;
  160. mZ /= rhs.mZ;
  161. return *this;
  162. }
  163. //! Normalize to unit length and return the previous length
  164. float normalize()
  165. {
  166. float len = getLength();
  167. if (len < M_EPSILON)
  168. return len;
  169. float invLen = 1.0f / len;
  170. mX *= invLen;
  171. mY *= invLen;
  172. mZ *= invLen;
  173. return len;
  174. }
  175. //! Normalize to unit length using fast inverse square root
  176. void normalizeFast()
  177. {
  178. float invLen = fastInvSqrt(mX * mX + mY * mY + mZ * mZ);
  179. mX *= invLen;
  180. mY *= invLen;
  181. mZ *= invLen;
  182. }
  183. //! Return length
  184. float getLength() const
  185. {
  186. return sqrtf(mX * mX + mY * mY + mZ * mZ);
  187. }
  188. //! Return length using fast square root
  189. float getLengthFast() const
  190. {
  191. return fastSqrt(mX * mX + mY * mY + mZ * mZ);
  192. }
  193. //! Return squared length
  194. float getLengthSquared() const
  195. {
  196. return mX * mX + mY * mY + mZ * mZ;
  197. }
  198. //! Calculate dot product
  199. float dotProduct(const Vector3& rhs) const
  200. {
  201. return mX * rhs.mX + mY * rhs.mY + mZ * rhs.mZ;
  202. }
  203. //! Calculate absolute dot product
  204. float absDotProduct(const Vector3& rhs) const
  205. {
  206. return fabsf(mX * rhs.mX) + fabsf(mY * rhs.mY) + fabsf(mZ * rhs.mZ);
  207. }
  208. //! Calculate cross product
  209. Vector3 crossProduct(const Vector3& rhs) const
  210. {
  211. return Vector3(
  212. mY * rhs.mZ - mZ * rhs.mY,
  213. mZ * rhs.mX - mX * rhs.mZ,
  214. mX * rhs.mY - mY * rhs.mX
  215. );
  216. }
  217. //! Linear interpolation with another vector
  218. Vector3 lerp(const Vector3& rhs, float t) const
  219. {
  220. return *this * (1.0f - t) + rhs * t;
  221. }
  222. //! Return normalized to unit length
  223. Vector3 getNormalized() const
  224. {
  225. float len = getLength();
  226. if (len < M_EPSILON)
  227. return *this;
  228. float invLen = 1.0f / len;
  229. return *this * invLen;
  230. }
  231. //! Return normalized to unit length using fast inverse square root
  232. Vector3 getNormalizedFast() const
  233. {
  234. float invLen = fastInvSqrt(mX * mX + mY * mY + mZ * mZ);
  235. return *this * invLen;
  236. }
  237. //! Return float data
  238. const float* getData() const { return &mX; }
  239. //! X coordinate
  240. float mX;
  241. //! Y coordinate
  242. float mY;
  243. //! Z coordinate
  244. float mZ;
  245. //! Zero vector
  246. static const Vector3 sZero;
  247. //! (-1,0,0) vector
  248. static const Vector3 sLeft;
  249. //! (1,0,0) vector
  250. static const Vector3 sRight;
  251. //! (0,1,0) vector
  252. static const Vector3 sUp;
  253. //! (0,-1,0) vector
  254. static const Vector3 sDown;
  255. //! (0,0,1) vector
  256. static const Vector3 sForward;
  257. //! (0,0,-1) vector
  258. static const Vector3 sBack;
  259. //! (1,1,1) vector
  260. static const Vector3 sUnity;
  261. };
  262. //! Multiply Vector3 with a scalar
  263. inline Vector3 operator * (float lhs, const Vector3& rhs) { return rhs * lhs; }
  264. #endif // MATH_VECTOR3_H