BsVector3.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include <cmath>
  5. #include "Prerequisites/BsPrerequisitesUtil.h"
  6. #include "Math/BsRadian.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Math
  10. * @{
  11. */
  12. /** A three dimensional vector. */
  13. class BS_UTILITY_EXPORT Vector3
  14. {
  15. public:
  16. float x, y, z;
  17. public:
  18. Vector3()
  19. { }
  20. Vector3(BS_ZERO zero)
  21. :x(0.0f), y(0.0f), z(0.0f)
  22. { }
  23. Vector3(float x, float y, float z)
  24. :x(x), y(y), z(z)
  25. { }
  26. explicit Vector3(const Vector4& vec);
  27. /** Exchange the contents of this vector with another. */
  28. void swap(Vector3& other)
  29. {
  30. std::swap(x, other.x);
  31. std::swap(y, other.y);
  32. std::swap(z, other.z);
  33. }
  34. float operator[] (UINT32 i) const
  35. {
  36. assert(i < 3);
  37. return *(&x + i);
  38. }
  39. float& operator[] (UINT32 i)
  40. {
  41. assert(i < 3);
  42. return *(&x + i);
  43. }
  44. /** Pointer accessor for direct copying. */
  45. float* ptr()
  46. {
  47. return &x;
  48. }
  49. /** Pointer accessor for direct copying. */
  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. /** Returns the length (magnitude) of the vector. */
  193. float length() const
  194. {
  195. return std::sqrt(x * x + y * y + z * z);
  196. }
  197. /** Returns the square of the length(magnitude) of the vector. */
  198. float squaredLength() const
  199. {
  200. return x * x + y * y + z * z;
  201. }
  202. /** Returns the distance to another vector. */
  203. float distance(const Vector3& rhs) const
  204. {
  205. return (*this - rhs).length();
  206. }
  207. /** Returns the square of the distance to another vector. */
  208. float squaredDistance(const Vector3& rhs) const
  209. {
  210. return (*this - rhs).squaredLength();
  211. }
  212. /** Calculates the dot (scalar) product of this vector with another. */
  213. float dot(const Vector3& vec) const
  214. {
  215. return x * vec.x + y * vec.y + z * vec.z;
  216. }
  217. /** Normalizes the vector. */
  218. float normalize()
  219. {
  220. float len = length();
  221. // Will also work for zero-sized vectors, but will change nothing
  222. if (len > 1e-08)
  223. {
  224. float invLen = 1.0f / len;
  225. x *= invLen;
  226. y *= invLen;
  227. z *= invLen;
  228. }
  229. return len;
  230. }
  231. /** Calculates the cross-product of 2 vectors, that is, the vector that lies perpendicular to them both. */
  232. Vector3 cross(const Vector3& other) const
  233. {
  234. return Vector3(
  235. y * other.z - z * other.y,
  236. z * other.x - x * other.z,
  237. x * other.y - y * other.x);
  238. }
  239. /** Sets this vector's components to the minimum of its own and the ones of the passed in vector. */
  240. void floor(const Vector3& cmp)
  241. {
  242. if (cmp.x < x) x = cmp.x;
  243. if (cmp.y < y) y = cmp.y;
  244. if (cmp.z < z) z = cmp.z;
  245. }
  246. /** Sets this vector's components to the maximum of its own and the ones of the passed in vector. */
  247. void ceil(const Vector3& cmp)
  248. {
  249. if (cmp.x > x) x = cmp.x;
  250. if (cmp.y > y) y = cmp.y;
  251. if (cmp.z > z) z = cmp.z;
  252. }
  253. /** Generates a vector perpendicular to this vector. */
  254. Vector3 perpendicular() const
  255. {
  256. static const float squareZero = (float)(1e-06 * 1e-06);
  257. Vector3 perp = this->cross(Vector3::UNIT_X);
  258. if (perp.squaredLength() < squareZero)
  259. perp = this->cross(Vector3::UNIT_Y);
  260. perp.normalize();
  261. return perp;
  262. }
  263. /** Gets the angle between 2 vectors. */
  264. inline Radian angleBetween(const Vector3& dest) const;
  265. /** Returns true if this vector is zero length. */
  266. bool isZeroLength() const
  267. {
  268. float sqlen = (x * x) + (y * y) + (z * z);
  269. return (sqlen < (1e-06 * 1e-06));
  270. }
  271. /** Calculates a reflection vector to the plane with the given normal. */
  272. Vector3 reflect(const Vector3& normal) const
  273. {
  274. return Vector3(*this - (2 * this->dot(normal) * normal));
  275. }
  276. /** Calculates two vectors orthonormal to the current vector, and normalizes the current vector if not already. */
  277. void orthogonalComplement(Vector3& a, Vector3& b)
  278. {
  279. if (fabs(x) > fabs(y))
  280. a = Vector3(-z, 0, x);
  281. else
  282. a = Vector3(0, z, -y);
  283. b = cross(a);
  284. orthonormalize(*this, a, b);
  285. }
  286. /** Performs Gram-Schmidt orthonormalization. */
  287. static void orthonormalize(Vector3& vec0, Vector3& vec1, Vector3& vec2)
  288. {
  289. vec0.normalize();
  290. float dot0 = vec0.dot(vec1);
  291. vec1 -= dot0*vec0;
  292. vec1.normalize();
  293. float dot1 = vec1.dot(vec2);
  294. dot0 = vec0.dot(vec2);
  295. vec2 -= dot0*vec0 + dot1*vec1;
  296. vec2.normalize();
  297. }
  298. /** Calculates the dot (scalar) product of two vectors. */
  299. static float dot(const Vector3& a, const Vector3& b)
  300. {
  301. return a.x * b.x + a.y * b.y + a.z * b.z;
  302. }
  303. /** Normalizes the provided vector and returns a new normalized instance. */
  304. static Vector3 normalize(const Vector3& val);
  305. /** Calculates the cross-product of 2 vectors, that is, the vector that lies perpendicular to them both. */
  306. static Vector3 cross(const Vector3& a, const Vector3& b)
  307. {
  308. return Vector3(
  309. a.y * b.z - a.z * b.y,
  310. a.z * b.x - a.x * b.z,
  311. a.x * b.y - a.y * b.x);
  312. }
  313. /**
  314. * Linearly interpolates between the two vectors using @p t. t should be in [0, 1] range, where t = 0 corresponds
  315. * to the left vector, while t = 1 corresponds to the right vector.
  316. */
  317. static Vector3 lerp(float t, const Vector3& a, const Vector3& b)
  318. {
  319. return (1.0f - t) * a + t * b;
  320. }
  321. /** Checks are any of the vector components not a number. */
  322. inline bool isNaN() const;
  323. /** Returns the minimum of all the vector components as a new vector. */
  324. static Vector3 min(const Vector3& a, const Vector3& b)
  325. {
  326. return Vector3(std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z));
  327. }
  328. /** Returns the maximum of all the vector components as a new vector. */
  329. static Vector3 max(const Vector3& a, const Vector3& b)
  330. {
  331. return Vector3(std::max(a.x, b.x), std::max(a.y, b.y), std::max(a.z, b.z));
  332. }
  333. static const Vector3 ZERO;
  334. static const Vector3 ONE;
  335. static const Vector3 INF;
  336. static const Vector3 UNIT_X;
  337. static const Vector3 UNIT_Y;
  338. static const Vector3 UNIT_Z;
  339. };
  340. /** @} */
  341. /** @cond SPECIALIZATIONS */
  342. BS_ALLOW_MEMCPY_SERIALIZATION(Vector3);
  343. /** @endcond */
  344. }