CmVector3.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. #include "CmMath.h"
  4. namespace CamelotFramework
  5. {
  6. /**
  7. * @brief A three dimensional vector.
  8. */
  9. class CM_UTILITY_EXPORT Vector3
  10. {
  11. public:
  12. float x, y, z;
  13. public:
  14. Vector3()
  15. { }
  16. Vector3(float x, float y, float z)
  17. : x(x), y(y), z(z)
  18. { }
  19. /**
  20. * @brief Exchange the contents of this vector with another.
  21. */
  22. void swap(Vector3& other)
  23. {
  24. std::swap(x, other.x);
  25. std::swap(y, other.y);
  26. std::swap(z, other.z);
  27. }
  28. float operator[] (UINT32 i) const
  29. {
  30. assert(i < 3);
  31. return *(&x+i);
  32. }
  33. float& operator[] (UINT32 i)
  34. {
  35. assert(i < 3);
  36. return *(&x+i);
  37. }
  38. /**
  39. * @brief Pointer accessor for direct copying.
  40. */
  41. float* ptr()
  42. {
  43. return &x;
  44. }
  45. /**
  46. * @brief Pointer accessor for direct copying.
  47. */
  48. const float* ptr() const
  49. {
  50. return &x;
  51. }
  52. Vector3& operator= (const Vector3& rhs)
  53. {
  54. x = rhs.x;
  55. y = rhs.y;
  56. z = rhs.z;
  57. return *this;
  58. }
  59. Vector3& operator= (float rhs)
  60. {
  61. x = rhs;
  62. y = rhs;
  63. z = rhs;
  64. return *this;
  65. }
  66. bool operator== (const Vector3& rhs) const
  67. {
  68. return (x == rhs.x && y == rhs.y && z == rhs.z);
  69. }
  70. bool operator!= (const Vector3& rhs) const
  71. {
  72. return (x != rhs.x || y != rhs.y || z != rhs.z);
  73. }
  74. Vector3 operator+ (const Vector3& rhs) const
  75. {
  76. return Vector3(x + rhs.x, y + rhs.y, z + rhs.z);
  77. }
  78. Vector3 operator- (const Vector3& rhs) const
  79. {
  80. return Vector3(x - rhs.x, y - rhs.y, z - rhs.z);
  81. }
  82. Vector3 operator* (float rhs) const
  83. {
  84. return Vector3(x * rhs, y * rhs, z * rhs);
  85. }
  86. Vector3 operator* (const Vector3& rhs) const
  87. {
  88. return Vector3(x * rhs.x, y * rhs.y, z * rhs.z);
  89. }
  90. Vector3 operator/ (float val) const
  91. {
  92. assert(val != 0.0);
  93. float fInv = 1.0f / val;
  94. return Vector3(x * fInv, y * fInv, z * fInv);
  95. }
  96. Vector3 operator/ (const Vector3& rhs) const
  97. {
  98. return Vector3(x / rhs.x, y / rhs.y, z / rhs.z);
  99. }
  100. const Vector3& operator+ () const
  101. {
  102. return *this;
  103. }
  104. Vector3 operator- () const
  105. {
  106. return Vector3(-x, -y, -z);
  107. }
  108. friend Vector3 operator* (float lhs, const Vector3& rhs)
  109. {
  110. return Vector3(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
  111. }
  112. friend Vector3 operator/ (float lhs, const Vector3& rhs)
  113. {
  114. return Vector3(lhs / rhs.x, lhs / rhs.y, lhs / rhs.z);
  115. }
  116. friend Vector3 operator+ (const Vector3& lhs, float rhs)
  117. {
  118. return Vector3(lhs.x + rhs, lhs.y + rhs, lhs.z + rhs);
  119. }
  120. friend Vector3 operator+ (float lhs, const Vector3& rhs)
  121. {
  122. return Vector3(lhs + rhs.x, lhs + rhs.y, lhs + rhs.z);
  123. }
  124. friend Vector3 operator- (const Vector3& lhs, float rhs)
  125. {
  126. return Vector3(lhs.x - rhs, lhs.y - rhs, lhs.z - rhs);
  127. }
  128. friend Vector3 operator- (float lhs, const Vector3& rhs)
  129. {
  130. return Vector3(lhs - rhs.x, lhs - rhs.y, lhs - rhs.z);
  131. }
  132. Vector3& operator+= (const Vector3& rhs)
  133. {
  134. x += rhs.x;
  135. y += rhs.y;
  136. z += rhs.z;
  137. return *this;
  138. }
  139. Vector3& operator+= (float rhs)
  140. {
  141. x += rhs;
  142. y += rhs;
  143. z += rhs;
  144. return *this;
  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*= (float rhs)
  161. {
  162. x *= rhs;
  163. y *= rhs;
  164. z *= rhs;
  165. return *this;
  166. }
  167. Vector3& operator*= (const Vector3& rhs)
  168. {
  169. x *= rhs.x;
  170. y *= rhs.y;
  171. z *= rhs.z;
  172. return *this;
  173. }
  174. Vector3& operator/= (float rhs)
  175. {
  176. assert(rhs != 0.0f);
  177. float inv = 1.0f / rhs;
  178. x *= inv;
  179. y *= inv;
  180. z *= inv;
  181. return *this;
  182. }
  183. Vector3& operator/= (const Vector3& rhs)
  184. {
  185. x /= rhs.x;
  186. y /= rhs.y;
  187. z /= rhs.z;
  188. return *this;
  189. }
  190. /**
  191. * @brief Returns the length (magnitude) of the vector.
  192. */
  193. float length() const
  194. {
  195. return Math::sqrt(x * x + y * y + z * z);
  196. }
  197. /**
  198. * @brief Returns the square of the length(magnitude) of the vector.
  199. */
  200. float squaredLength() const
  201. {
  202. return x * x + y * y + z * z;
  203. }
  204. /**
  205. * @brief Returns the distance to another vector.
  206. */
  207. float distance(const Vector3& rhs) const
  208. {
  209. return (*this - rhs).length();
  210. }
  211. /**
  212. * @brief Returns the square of the distance to another vector.
  213. */
  214. float squaredDistance(const Vector3& rhs) const
  215. {
  216. return (*this - rhs).squaredLength();
  217. }
  218. /**
  219. * @brief Calculates the dot (scalar) product of this vector with another
  220. */
  221. float dot(const Vector3& vec) const
  222. {
  223. return x * vec.x + y * vec.y + z * vec.z;
  224. }
  225. /**
  226. * @brief Normalizes the vector.
  227. */
  228. float normalize()
  229. {
  230. float len = Math::sqrt(x * x + y * y + z * z);
  231. // Will also work for zero-sized vectors, but will change nothing
  232. if (len > 1e-08)
  233. {
  234. float invLen = 1.0f / len;
  235. x *= invLen;
  236. y *= invLen;
  237. z *= invLen;
  238. }
  239. return len;
  240. }
  241. /**
  242. * @brief Calculates the cross-product of 2 vectors, i.e. the vector that
  243. * lies perpendicular to them both.
  244. */
  245. Vector3 cross(const Vector3& other) const
  246. {
  247. return Vector3(
  248. y * other.z - z * other.y,
  249. z * other.x - x * other.z,
  250. x * other.y - y * other.x);
  251. }
  252. /**
  253. * @brief Sets this vector's components to the minimum of its own and the
  254. * ones of the passed in vector.
  255. */
  256. void floor(const Vector3& cmp)
  257. {
  258. if(cmp.x < x) x = cmp.x;
  259. if(cmp.y < y) y = cmp.y;
  260. if(cmp.z < z) z = cmp.z;
  261. }
  262. /**
  263. * @brief Sets this vector's components to the maximum of its own and the
  264. * ones of the passed in vector.
  265. */
  266. void ceil(const Vector3& cmp)
  267. {
  268. if(cmp.x > x) x = cmp.x;
  269. if(cmp.y > y) y = cmp.y;
  270. if(cmp.z > z) z = cmp.z;
  271. }
  272. /**
  273. * @brief Generates a vector perpendicular to this vector.
  274. */
  275. Vector3 perpendicular() const
  276. {
  277. static const float squareZero = (float)(1e-06 * 1e-06);
  278. Vector3 perp = this->cross(Vector3::UNIT_X);
  279. if(perp.squaredLength() < squareZero)
  280. perp = this->cross(Vector3::UNIT_Y);
  281. perp.normalize();
  282. return perp;
  283. }
  284. /**
  285. * @brief Gets the angle between 2 vectors.
  286. */
  287. Radian angleBetween(const Vector3& dest)
  288. {
  289. float lenProduct = length() * dest.length();
  290. // Divide by zero check
  291. if(lenProduct < 1e-6f)
  292. lenProduct = 1e-6f;
  293. float f = dot(dest) / lenProduct;
  294. f = Math::clamp(f, -1.0f, 1.0f);
  295. return Math::acos(f);
  296. }
  297. /**
  298. * @brief Returns true if this vector is zero length.
  299. */
  300. bool isZeroLength() const
  301. {
  302. float sqlen = (x * x) + (y * y) + (z * z);
  303. return (sqlen < (1e-06 * 1e-06));
  304. }
  305. /**
  306. * @brief Calculates a reflection vector to the plane with the given normal.
  307. */
  308. Vector3 reflect(const Vector3& normal) const
  309. {
  310. return Vector3(*this - (2 * this->dot(normal) * normal));
  311. }
  312. /**
  313. * @brief Performs Gram-Schmidt orthonormalization
  314. */
  315. static void orthonormalize(Vector3& vec0, Vector3& vec1, Vector3& vec2)
  316. {
  317. vec0.normalize();
  318. float dot0 = vec0.dot(vec1);
  319. vec1 -= dot0*vec0;
  320. vec1.normalize();
  321. float dot1 = vec1.dot(vec2);
  322. dot0 = vec0.dot(vec2);
  323. vec2 -= dot0*vec0 + dot1*vec1;
  324. vec2.normalize();
  325. }
  326. /**
  327. * @brief Normalizes the provided vector and returns a new normalized instance.
  328. */
  329. static Vector3 normalize(const Vector3& val)
  330. {
  331. float len = Math::sqrt(val.x * val.x + val.y * val.y + val.z * val.z);
  332. // Will also work for zero-sized vectors, but will change nothing
  333. if (len > 1e-08)
  334. {
  335. float invLen = 1.0f / len;
  336. Vector3 normalizedVec;
  337. normalizedVec.x = val.x * invLen;
  338. normalizedVec.y = val.y * invLen;
  339. normalizedVec.z = val.z * invLen;
  340. return normalizedVec;
  341. }
  342. else
  343. return val;
  344. }
  345. /**
  346. * @brief Checks are any of the vector components NaN.
  347. */
  348. bool isNaN() const
  349. {
  350. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z);
  351. }
  352. /**
  353. * @brief Returns the minimum of all the vector components as a
  354. * new vector.
  355. */
  356. static Vector3 min(const Vector3& a, const Vector3& b)
  357. {
  358. return Vector3(std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z));
  359. }
  360. /**
  361. * @brief Returns the maximum of all the vector components as a
  362. * new vector.
  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. }