BsVector3.h 11 KB

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