CmVector3.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  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. */
  24. #pragma once
  25. #include "CmPrerequisitesUtil.h"
  26. #include "CmMath.h"
  27. namespace CamelotFramework
  28. {
  29. class CM_UTILITY_EXPORT Vector3
  30. {
  31. public:
  32. float x, y, z;
  33. public:
  34. Vector3()
  35. { }
  36. Vector3(float x, float y, float z)
  37. : x(x), y(y), z(z)
  38. { }
  39. /**
  40. * @brief Exchange the contents of this vector with another.
  41. */
  42. void swap(Vector3& other)
  43. {
  44. std::swap(x, other.x);
  45. std::swap(y, other.y);
  46. std::swap(z, other.z);
  47. }
  48. float operator[] (size_t i) const
  49. {
  50. assert(i < 3);
  51. return *(&x+i);
  52. }
  53. float& operator[] (size_t i)
  54. {
  55. assert(i < 3);
  56. return *(&x+i);
  57. }
  58. float* ptr()
  59. {
  60. return &x;
  61. }
  62. const float* ptr() const
  63. {
  64. return &x;
  65. }
  66. Vector3& operator= (const Vector3& rhs)
  67. {
  68. x = rhs.x;
  69. y = rhs.y;
  70. z = rhs.z;
  71. return *this;
  72. }
  73. Vector3& operator= (float rhs)
  74. {
  75. x = rhs;
  76. y = rhs;
  77. z = rhs;
  78. return *this;
  79. }
  80. bool operator== (const Vector3& rhs) const
  81. {
  82. return (x == rhs.x && y == rhs.y && z == rhs.z);
  83. }
  84. bool operator!= (const Vector3& rhs) const
  85. {
  86. return (x != rhs.x || y != rhs.y || z != rhs.z);
  87. }
  88. Vector3 operator+ (const Vector3& rhs) const
  89. {
  90. return Vector3(x + rhs.x, y + rhs.y, z + rhs.z);
  91. }
  92. Vector3 operator- (const Vector3& rhs) const
  93. {
  94. return Vector3(x - rhs.x, y - rhs.y, z - rhs.z);
  95. }
  96. Vector3 operator* (float rhs) const
  97. {
  98. return Vector3(x * rhs, y * rhs, z * rhs);
  99. }
  100. Vector3 operator* (const Vector3& rhs) const
  101. {
  102. return Vector3(x * rhs.x, y * rhs.y, z * rhs.z);
  103. }
  104. Vector3 operator/ (float val) const
  105. {
  106. assert(val != 0.0);
  107. float fInv = 1.0f / val;
  108. return Vector3(x * fInv, y * fInv, z * fInv);
  109. }
  110. Vector3 operator/ (const Vector3& rhs) const
  111. {
  112. return Vector3(x / rhs.x, y / rhs.y, z / rhs.z);
  113. }
  114. const Vector3& operator+ () const
  115. {
  116. return *this;
  117. }
  118. Vector3 operator- () const
  119. {
  120. return Vector3(-x, -y, -z);
  121. }
  122. friend Vector3 operator* (float lhs, const Vector3& rhs)
  123. {
  124. return Vector3(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
  125. }
  126. friend Vector3 operator/ (float lhs, const Vector3& rhs)
  127. {
  128. return Vector3(lhs / rhs.x, lhs / rhs.y, lhs / rhs.z);
  129. }
  130. friend Vector3 operator+ (const Vector3& lhs, float rhs)
  131. {
  132. return Vector3(lhs.x + rhs, lhs.y + rhs, lhs.z + rhs);
  133. }
  134. friend Vector3 operator+ (float lhs, const Vector3& rhs)
  135. {
  136. return Vector3(lhs + rhs.x, lhs + rhs.y, lhs + rhs.z);
  137. }
  138. friend Vector3 operator- (const Vector3& lhs, float rhs)
  139. {
  140. return Vector3(lhs.x - rhs, lhs.y - rhs, lhs.z - rhs);
  141. }
  142. friend Vector3 operator- (float lhs, const Vector3& rhs)
  143. {
  144. return Vector3(lhs - rhs.x, lhs - rhs.y, lhs - rhs.z);
  145. }
  146. Vector3& operator+= (const Vector3& rhs)
  147. {
  148. x += rhs.x;
  149. y += rhs.y;
  150. z += rhs.z;
  151. return *this;
  152. }
  153. Vector3& operator+= (float rhs)
  154. {
  155. x += rhs;
  156. y += rhs;
  157. z += rhs;
  158. return *this;
  159. }
  160. Vector3& operator-= (const Vector3& rhs)
  161. {
  162. x -= rhs.x;
  163. y -= rhs.y;
  164. z -= rhs.z;
  165. return *this;
  166. }
  167. Vector3& operator-= (float rhs)
  168. {
  169. x -= rhs;
  170. y -= rhs;
  171. z -= rhs;
  172. return *this;
  173. }
  174. Vector3& operator*= (float rhs)
  175. {
  176. x *= rhs;
  177. y *= rhs;
  178. z *= rhs;
  179. return *this;
  180. }
  181. Vector3& operator*= (const Vector3& rhs)
  182. {
  183. x *= rhs.x;
  184. y *= rhs.y;
  185. z *= rhs.z;
  186. return *this;
  187. }
  188. Vector3& operator/= (float rhs)
  189. {
  190. assert(rhs != 0.0f);
  191. float inv = 1.0f / rhs;
  192. x *= inv;
  193. y *= inv;
  194. z *= inv;
  195. return *this;
  196. }
  197. Vector3& operator/= (const Vector3& rhs)
  198. {
  199. x /= rhs.x;
  200. y /= rhs.y;
  201. z /= rhs.z;
  202. return *this;
  203. }
  204. /**
  205. * @brief Returns the length (magnitude) of the vector.
  206. */
  207. float length() const
  208. {
  209. return Math::sqrt(x * x + y * y + z * z);
  210. }
  211. /**
  212. * @brief Returns the square of the length(magnitude) of the vector.
  213. */
  214. float squaredLength() const
  215. {
  216. return x * x + y * y + z * z;
  217. }
  218. /**
  219. * @brief Returns the distance to another vector.
  220. */
  221. float distance(const Vector3& rhs) const
  222. {
  223. return (*this - rhs).length();
  224. }
  225. /**
  226. * @brief Returns the square of the distance to another vector.
  227. */
  228. float squaredDistance(const Vector3& rhs) const
  229. {
  230. return (*this - rhs).squaredLength();
  231. }
  232. /**
  233. * @brief Calculates the dot (scalar) product of this vector with another
  234. */
  235. float dot(const Vector3& vec) const
  236. {
  237. return x * vec.x + y * vec.y + z * vec.z;
  238. }
  239. /**
  240. * @brief Normalizes the vector.
  241. */
  242. float normalize()
  243. {
  244. float len = Math::sqrt(x * x + y * y + z * z);
  245. // Will also work for zero-sized vectors, but will change nothing
  246. if (len > 1e-08)
  247. {
  248. float invLen = 1.0f / len;
  249. x *= invLen;
  250. y *= invLen;
  251. z *= invLen;
  252. }
  253. return len;
  254. }
  255. /**
  256. * @brief Calculates the cross-product of 2 vectors, i.e. the vector that
  257. * lies perpendicular to them both.
  258. */
  259. Vector3 cross(const Vector3& other) const
  260. {
  261. return Vector3(
  262. y * other.z - z * other.y,
  263. z * other.x - x * other.z,
  264. x * other.y - y * other.x);
  265. }
  266. /**
  267. * @brief Sets this vector's components to the minimum of its own and the
  268. * ones of the passed in vector.
  269. */
  270. void floor(const Vector3& cmp)
  271. {
  272. if(cmp.x < x) x = cmp.x;
  273. if(cmp.y < y) y = cmp.y;
  274. if(cmp.z < z) z = cmp.z;
  275. }
  276. /**
  277. * @brief Sets this vector's components to the maximum of its own and the
  278. * ones of the passed in vector.
  279. */
  280. void ceil(const Vector3& cmp)
  281. {
  282. if(cmp.x > x) x = cmp.x;
  283. if(cmp.y > y) y = cmp.y;
  284. if(cmp.z > z) z = cmp.z;
  285. }
  286. /**
  287. * @brief Generates a vector perpendicular to this vector.
  288. */
  289. Vector3 perpendicular() const
  290. {
  291. static const float squareZero = (float)(1e-06 * 1e-06);
  292. Vector3 perp = this->cross(Vector3::UNIT_X);
  293. if(perp.squaredLength() < squareZero)
  294. perp = this->cross(Vector3::UNIT_Y);
  295. perp.normalize();
  296. return perp;
  297. }
  298. /**
  299. * @brief Gets the angle between 2 vectors.
  300. */
  301. Radian angleBetween(const Vector3& dest)
  302. {
  303. float lenProduct = length() * dest.length();
  304. // Divide by zero check
  305. if(lenProduct < 1e-6f)
  306. lenProduct = 1e-6f;
  307. float f = dot(dest) / lenProduct;
  308. f = Math::clamp(f, -1.0f, 1.0f);
  309. return Math::acos(f);
  310. }
  311. /**
  312. * @brief Returns true if this vector is zero length.
  313. */
  314. bool isZeroLength() const
  315. {
  316. float sqlen = (x * x) + (y * y) + (z * z);
  317. return (sqlen < (1e-06 * 1e-06));
  318. }
  319. /**
  320. * @brief Calculates a reflection vector to the plane with the given normal.
  321. */
  322. Vector3 reflect(const Vector3& normal) const
  323. {
  324. return Vector3(*this - (2 * this->dot(normal) * normal));
  325. }
  326. /**
  327. * @brief Performs Gram-Schmidt orthonormalization
  328. */
  329. static void orthonormalize(Vector3& vec0, Vector3& vec1, Vector3& vec2)
  330. {
  331. vec0.normalize();
  332. float dot0 = vec0.dot(vec1);
  333. vec1 -= dot0*vec0;
  334. vec1.normalize();
  335. float dot1 = vec1.dot(vec2);
  336. dot0 = vec0.dot(vec2);
  337. vec2 -= dot0*vec0 + dot1*vec1;
  338. vec2.normalize();
  339. }
  340. static Vector3 normalize(const Vector3& val)
  341. {
  342. float len = Math::sqrt(val.x * val.x + val.y * val.y + val.z * val.z);
  343. // Will also work for zero-sized vectors, but will change nothing
  344. if (len > 1e-08)
  345. {
  346. float invLen = 1.0f / len;
  347. Vector3 normalizedVec;
  348. normalizedVec.x = val.x * invLen;
  349. normalizedVec.y = val.y * invLen;
  350. normalizedVec.z = val.z * invLen;
  351. return normalizedVec;
  352. }
  353. else
  354. return val;
  355. }
  356. bool isNaN() const
  357. {
  358. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z);
  359. }
  360. static Vector3 min(const Vector3& a, const Vector3& b)
  361. {
  362. return Vector3(std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z));
  363. }
  364. static Vector3 max(const Vector3& a, const Vector3& b)
  365. {
  366. return Vector3(std::max(a.x, b.x), std::max(a.y, b.y), std::max(a.z, b.z));
  367. }
  368. static const Vector3 ZERO;
  369. static const Vector3 ONE;
  370. static const Vector3 UNIT_X;
  371. static const Vector3 UNIT_Y;
  372. static const Vector3 UNIT_Z;
  373. };
  374. CM_ALLOW_MEMCPY_SERIALIZATION(Vector3);
  375. }