BsVector3.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 "BsRadian.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. inline float length() const;
  191. /** Returns the square of the length(magnitude) of the vector. */
  192. float squaredLength() const
  193. {
  194. return x * x + y * y + z * z;
  195. }
  196. /** Returns the distance to another vector. */
  197. float distance(const Vector3& rhs) const
  198. {
  199. return (*this - rhs).length();
  200. }
  201. /** Returns the square of the distance to another vector. */
  202. float squaredDistance(const Vector3& rhs) const
  203. {
  204. return (*this - rhs).squaredLength();
  205. }
  206. /** Calculates the dot (scalar) product of this vector with another. */
  207. float dot(const Vector3& vec) const
  208. {
  209. return x * vec.x + y * vec.y + z * vec.z;
  210. }
  211. /** Normalizes the vector. */
  212. inline float normalize();
  213. /** Calculates the cross-product of 2 vectors, that is, the vector that lies perpendicular to them both. */
  214. Vector3 cross(const Vector3& other) const
  215. {
  216. return Vector3(
  217. y * other.z - z * other.y,
  218. z * other.x - x * other.z,
  219. x * other.y - y * other.x);
  220. }
  221. /** Sets this vector's components to the minimum of its own and the ones of the passed in vector. */
  222. void floor(const Vector3& cmp)
  223. {
  224. if(cmp.x < x) x = cmp.x;
  225. if(cmp.y < y) y = cmp.y;
  226. if(cmp.z < z) z = cmp.z;
  227. }
  228. /** Sets this vector's components to the maximum of its own and the ones of the passed in vector. */
  229. void ceil(const Vector3& cmp)
  230. {
  231. if(cmp.x > x) x = cmp.x;
  232. if(cmp.y > y) y = cmp.y;
  233. if(cmp.z > z) z = cmp.z;
  234. }
  235. /** Generates a vector perpendicular to this vector. */
  236. Vector3 perpendicular() const
  237. {
  238. static const float squareZero = (float)(1e-06 * 1e-06);
  239. Vector3 perp = this->cross(Vector3::UNIT_X);
  240. if(perp.squaredLength() < squareZero)
  241. perp = this->cross(Vector3::UNIT_Y);
  242. perp.normalize();
  243. return perp;
  244. }
  245. /** Gets the angle between 2 vectors. */
  246. inline Radian angleBetween(const Vector3& dest) const;
  247. /** Returns true if this vector is zero length. */
  248. bool isZeroLength() const
  249. {
  250. float sqlen = (x * x) + (y * y) + (z * z);
  251. return (sqlen < (1e-06 * 1e-06));
  252. }
  253. /** Calculates a reflection vector to the plane with the given normal. */
  254. Vector3 reflect(const Vector3& normal) const
  255. {
  256. return Vector3(*this - (2 * this->dot(normal) * normal));
  257. }
  258. /** Calculates two vectors orthonormal to the current vector, and normalizes the current vector if not already. */
  259. void orthogonalComplement(Vector3& a, Vector3& b)
  260. {
  261. if (fabs(x) > fabs(y))
  262. a = Vector3(-z, 0, x);
  263. else
  264. a = Vector3(0, z, -y);
  265. b = cross(a);
  266. orthonormalize(*this, a, b);
  267. }
  268. /** Performs Gram-Schmidt orthonormalization. */
  269. static void orthonormalize(Vector3& vec0, Vector3& vec1, Vector3& vec2)
  270. {
  271. vec0.normalize();
  272. float dot0 = vec0.dot(vec1);
  273. vec1 -= dot0*vec0;
  274. vec1.normalize();
  275. float dot1 = vec1.dot(vec2);
  276. dot0 = vec0.dot(vec2);
  277. vec2 -= dot0*vec0 + dot1*vec1;
  278. vec2.normalize();
  279. }
  280. /** Calculates the dot (scalar) product of two vectors. */
  281. static float dot(const Vector3& a, const Vector3& b)
  282. {
  283. return a.x * b.x + a.y * b.y + a.z * b.z;
  284. }
  285. /** Normalizes the provided vector and returns a new normalized instance. */
  286. static Vector3 normalize(const Vector3& val);
  287. /** Calculates the cross-product of 2 vectors, that is, the vector that lies perpendicular to them both. */
  288. static Vector3 cross(const Vector3& a, const Vector3& b)
  289. {
  290. return Vector3(
  291. a.y * b.z - a.z * b.y,
  292. a.z * b.x - a.x * b.z,
  293. a.x * b.y - a.y * b.x);
  294. }
  295. /** Checks are any of the vector components not a number. */
  296. inline bool isNaN() const;
  297. /** Returns the minimum of all the vector components as a new vector. */
  298. static Vector3 min(const Vector3& a, const Vector3& b)
  299. {
  300. return Vector3(std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z));
  301. }
  302. /** Returns the maximum of all the vector components as a new vector. */
  303. static Vector3 max(const Vector3& a, const Vector3& b)
  304. {
  305. return Vector3(std::max(a.x, b.x), std::max(a.y, b.y), std::max(a.z, b.z));
  306. }
  307. static const Vector3 ZERO;
  308. static const Vector3 ONE;
  309. static const Vector3 INF;
  310. static const Vector3 UNIT_X;
  311. static const Vector3 UNIT_Y;
  312. static const Vector3 UNIT_Z;
  313. };
  314. /** @} */
  315. /** @cond SPECIALIZATIONS */
  316. BS_ALLOW_MEMCPY_SERIALIZATION(Vector3);
  317. /** @endcond */
  318. }