BsVector3.h 11 KB

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