Vec3.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Assert.h"
  25. #include "Types.h"
  26. #include "MathUtils.h"
  27. #include "Vec2.h"
  28. namespace crown
  29. {
  30. /// 3D column vector.
  31. class Vec3
  32. {
  33. public:
  34. float x, y, z;
  35. /// Does nothing for efficiency.
  36. Vec3();
  37. /// Initializes all the components to val
  38. Vec3(float val);
  39. /// Constructs from three components
  40. Vec3(float nx, float ny, float nz);
  41. /// Constructs from array
  42. Vec3(const float v[3]);
  43. Vec3(const Vec3& a);
  44. /// Random access by index
  45. float operator[](uint32_t i) const;
  46. /// Random access by index
  47. float& operator[](uint32_t i);
  48. Vec3 operator+(const Vec3& a) const;
  49. Vec3& operator+=(const Vec3& a);
  50. Vec3 operator-(const Vec3& a) const;
  51. Vec3& operator-=(const Vec3& a);
  52. Vec3 operator*(float k) const;
  53. Vec3& operator*=(float k);
  54. Vec3 operator/(float k) const;
  55. Vec3& operator/=(float k);
  56. /// Dot product
  57. float dot(const Vec3& a) const;
  58. /// Cross product
  59. Vec3 cross(const Vec3& a) const;
  60. /// For simmetry
  61. friend Vec3 operator*(float k, const Vec3& a);
  62. bool operator==(const Vec3& other) const;
  63. bool operator!=(const Vec3& other) const;
  64. /// Returns whether all the components of this vector are smaller than all of the "other" vector
  65. bool operator<(const Vec3& other) const;
  66. /// Returns whether all the components of this vector are greater than all of the "other" vector
  67. bool operator>(const Vec3& other) const;
  68. /// Returns the vector's length
  69. float length() const;
  70. /// Returns the vector's squared length
  71. float squared_length() const;
  72. /// Sets the vector's length
  73. void set_length(float len);
  74. /// Normalizes the vector
  75. Vec3& normalize();
  76. /// Returns the normalized vector
  77. Vec3 get_normalized() const;
  78. /// Negates the vector (i.e. builds the inverse)
  79. Vec3& negate();
  80. /// Negates the vector (i.e. builds the inverse)
  81. Vec3 operator-() const;
  82. /// Returns the distance
  83. float get_distance_to(const Vec3& a) const;
  84. /// Returns the angle in radians
  85. float get_angle_between(const Vec3& a) const;
  86. /// Sets all components to zero
  87. void zero();
  88. /// Returns the pointer to the vector's data
  89. float* to_float_ptr();
  90. /// Returns the pointer to the vector's data
  91. const float* to_float_ptr() const;
  92. /// Returns a Vec2 with only x and y coordinates
  93. Vec2 to_vec2() const;
  94. static const Vec3 ZERO;
  95. static const Vec3 ONE;
  96. static const Vec3 XAXIS;
  97. static const Vec3 YAXIS;
  98. static const Vec3 ZAXIS;
  99. };
  100. //-----------------------------------------------------------------------------
  101. inline Vec3::Vec3()
  102. {
  103. }
  104. //-----------------------------------------------------------------------------
  105. inline Vec3::Vec3(float val) : x(val), y(val), z(val)
  106. {
  107. }
  108. //-----------------------------------------------------------------------------
  109. inline Vec3::Vec3(float nx, float ny, float nz) : x(nx), y(ny), z(nz)
  110. {
  111. }
  112. //-----------------------------------------------------------------------------
  113. inline Vec3::Vec3(const float v[3]) : x(v[0]), y(v[1]), z(v[2])
  114. {
  115. }
  116. //-----------------------------------------------------------------------------
  117. inline Vec3::Vec3(const Vec3& a) : x(a.x), y(a.y), z(a.z)
  118. {
  119. }
  120. //-----------------------------------------------------------------------------
  121. inline float Vec3::operator[](uint32_t i) const
  122. {
  123. CE_ASSERT(i < 3, "Index must be < 3");
  124. return (&x)[i];
  125. }
  126. //-----------------------------------------------------------------------------
  127. inline float& Vec3::operator[](uint32_t i)
  128. {
  129. CE_ASSERT(i < 3, "Index must be < 3");
  130. return (&x)[i];
  131. }
  132. //-----------------------------------------------------------------------------
  133. inline Vec3 Vec3::operator+(const Vec3& a) const
  134. {
  135. return Vec3(x + a.x, y + a.y, z + a.z);
  136. }
  137. //-----------------------------------------------------------------------------
  138. inline Vec3& Vec3::operator+=(const Vec3& a)
  139. {
  140. x += a.x;
  141. y += a.y;
  142. z += a.z;
  143. return *this;
  144. }
  145. //-----------------------------------------------------------------------------
  146. inline Vec3 Vec3::operator-(const Vec3& a) const
  147. {
  148. return Vec3(x - a.x, y - a.y, z - a.z);
  149. }
  150. //-----------------------------------------------------------------------------
  151. inline Vec3& Vec3::operator-=(const Vec3& a)
  152. {
  153. x -= a.x;
  154. y -= a.y;
  155. z -= a.z;
  156. return *this;
  157. }
  158. //-----------------------------------------------------------------------------
  159. inline Vec3 Vec3::operator*(float k) const
  160. {
  161. return Vec3(x * k, y * k, z * k);
  162. }
  163. //-----------------------------------------------------------------------------
  164. inline Vec3& Vec3::operator*=(float k)
  165. {
  166. x *= k;
  167. y *= k;
  168. z *= k;
  169. return *this;
  170. }
  171. //-----------------------------------------------------------------------------
  172. inline Vec3 Vec3::operator/(float k) const
  173. {
  174. CE_ASSERT(k != (float)0.0, "Division by zero");
  175. float inv = (float)(1.0 / k);
  176. return Vec3(x * inv, y * inv, z * inv);
  177. }
  178. //-----------------------------------------------------------------------------
  179. inline Vec3& Vec3::operator/=(float k)
  180. {
  181. CE_ASSERT(k != (float)0.0, "Division by zero");
  182. float inv = (float)(1.0 / k);
  183. x *= inv;
  184. y *= inv;
  185. z *= inv;
  186. return *this;
  187. }
  188. //-----------------------------------------------------------------------------
  189. inline float Vec3::dot(const Vec3& a) const
  190. {
  191. return x * a.x + y * a.y + z * a.z;
  192. }
  193. //-----------------------------------------------------------------------------
  194. inline Vec3 Vec3::cross(const Vec3& a) const
  195. {
  196. return Vec3(y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x);
  197. }
  198. //-----------------------------------------------------------------------------
  199. inline Vec3 operator*(float k, const Vec3& a)
  200. {
  201. return a * k;
  202. }
  203. //-----------------------------------------------------------------------------
  204. inline bool Vec3::operator==(const Vec3& other) const
  205. {
  206. return math::equals(x, other.x) && math::equals(y, other.y) && math::equals(z, other.z);
  207. }
  208. //-----------------------------------------------------------------------------
  209. inline bool Vec3::operator!=(const Vec3& other) const
  210. {
  211. return !math::equals(x, other.x) || !math::equals(y, other.y) || !math::equals(z, other.z);
  212. }
  213. //-----------------------------------------------------------------------------
  214. inline bool Vec3::operator<(const Vec3& other) const
  215. {
  216. return ((x < other.x) && (y < other.y) && (z < other.z));
  217. }
  218. //-----------------------------------------------------------------------------
  219. inline bool Vec3::operator>(const Vec3& other) const
  220. {
  221. return ((x > other.x) && (y > other.y) && (z > other.z));
  222. }
  223. //-----------------------------------------------------------------------------
  224. inline float Vec3::length() const
  225. {
  226. return math::sqrt(x * x + y * y + z * z);
  227. }
  228. //-----------------------------------------------------------------------------
  229. inline float Vec3::squared_length() const
  230. {
  231. return x * x + y * y + z * z;
  232. }
  233. //-----------------------------------------------------------------------------
  234. inline void Vec3::set_length(float len)
  235. {
  236. normalize();
  237. x *= len;
  238. y *= len;
  239. z *= len;
  240. }
  241. //-----------------------------------------------------------------------------
  242. inline Vec3& Vec3::normalize()
  243. {
  244. float len = length();
  245. if (math::equals(len, (float)0.0))
  246. {
  247. return *this;
  248. }
  249. len = (float)(1.0 / len);
  250. x *= len;
  251. y *= len;
  252. z *= len;
  253. return *this;
  254. }
  255. //-----------------------------------------------------------------------------
  256. inline Vec3 Vec3::get_normalized() const
  257. {
  258. Vec3 tmp(x, y, z);
  259. return tmp.normalize();
  260. }
  261. //-----------------------------------------------------------------------------
  262. inline Vec3& Vec3::negate()
  263. {
  264. x = -x;
  265. y = -y;
  266. z = -z;
  267. return *this;
  268. }
  269. //-----------------------------------------------------------------------------
  270. inline Vec3 Vec3::operator-() const
  271. {
  272. return Vec3(-x, -y, -z);
  273. }
  274. //-----------------------------------------------------------------------------
  275. inline float Vec3::get_distance_to(const Vec3& a) const
  276. {
  277. return (*this - a).length();
  278. }
  279. //-----------------------------------------------------------------------------
  280. inline float Vec3::get_angle_between(const Vec3& a) const
  281. {
  282. return math::acos(this->dot(a) / (this->length() * a.length()));
  283. }
  284. //-----------------------------------------------------------------------------
  285. inline void Vec3::zero()
  286. {
  287. x = 0.0;
  288. y = 0.0;
  289. z = 0.0;
  290. }
  291. //-----------------------------------------------------------------------------
  292. inline float* Vec3::to_float_ptr()
  293. {
  294. return &x;
  295. }
  296. //-----------------------------------------------------------------------------
  297. inline const float* Vec3::to_float_ptr() const
  298. {
  299. return &x;
  300. }
  301. //-----------------------------------------------------------------------------
  302. inline Vec2 Vec3::to_vec2() const
  303. {
  304. return Vec2(x, y);
  305. }
  306. //-----------------------------------------------------------------------------
  307. /// Returns the parallel portion of "v" projected onto "n"
  308. inline Vec3 get_projected_parallel(const Vec3& v, const Vec3& n)
  309. {
  310. float n_len_q;
  311. n_len_q = n.length();
  312. n_len_q = n_len_q * n_len_q;
  313. return n * (v.dot(n) / n_len_q);
  314. }
  315. //-----------------------------------------------------------------------------
  316. /// Returns the perpendicular portion of "v" projected onto "n"
  317. inline Vec3 get_projected_perpendicular(const Vec3& v, const Vec3& n)
  318. {
  319. return v - get_projected_parallel(v, n);
  320. }
  321. } // namespace crown