BsVector3.h 11 KB

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