BsVector3.h 11 KB

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