Vector3.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 "Vector2.h"
  28. #include "MathTypes.h"
  29. namespace crown
  30. {
  31. /// Negates @a a and returns the result.
  32. Vector3 operator-(const Vector3& a);
  33. /// Adds the vector @a a to @a b and returns the result.
  34. Vector3 operator+(Vector3 a, const Vector3& b);
  35. /// Subtracts the vector @a b from @a a and returns the result.
  36. Vector3 operator-(Vector3 a, const Vector3& b);
  37. /// Multiplies the vector @a a by the scalar @a k and returns the result.
  38. Vector3 operator*(Vector3 a, float k);
  39. /// @copydoc operator*(Vector3, float)
  40. Vector3 operator*(float k, Vector3 a);
  41. /// Divides the vector @a a by the scalar @a k and returns the result.
  42. Vector3 operator/(Vector3 a, float k);
  43. /// Returns true whether the vectors @a a and @a b are equal.
  44. bool operator==(const Vector3& a, const Vector3& b);
  45. /// Functions to manipulate Vector3.
  46. ///
  47. /// @ingroup Math
  48. namespace vector3
  49. {
  50. const Vector3 ZERO = Vector3(0, 0, 0);
  51. const Vector3 XAXIS = Vector3(1, 0, 0);
  52. const Vector3 YAXIS = Vector3(0, 1, 0);
  53. const Vector3 ZAXIS = Vector3(0, 0, 1);
  54. /// Returns the dot product between the vectors @a a and @a b.
  55. float dot(const Vector3& a, const Vector3& b);
  56. /// Returns the cross product between the vectors @a a and @a b.
  57. Vector3 cross(const Vector3& a, const Vector3& b);
  58. /// Returns the lenght of @a a.
  59. float length(const Vector3& a);
  60. /// Returns the squared length of @a a.
  61. float squared_length(const Vector3& a);
  62. /// Sets the lenght of @a a to @a len.
  63. void set_length(Vector3& a, float len);
  64. /// Normalizes @a a and returns the result.
  65. Vector3 normalize(Vector3& a);
  66. /// Returns the distance between the points @a a and @a b.
  67. float distance(const Vector3& a, const Vector3& b);
  68. /// Returns the angle between the vectors @a a and @a b.
  69. float angle(const Vector3& a, const Vector3& b);
  70. /// Returns the pointer to the data of @a a.
  71. float* to_float_ptr(Vector3& a);
  72. /// @copydoc to_float_ptr(Vector3&)
  73. const float* to_float_ptr(const Vector3& a);
  74. /// Returns the Vector2 portion of @a a. (i.e. truncates z)
  75. Vector2 to_vector2(const Vector3& a);
  76. } // namespace vector3
  77. inline Vector3 operator-(const Vector3& a)
  78. {
  79. return Vector3(-a.x, -a.y, -a.z);
  80. }
  81. inline Vector3 operator+(Vector3 a, const Vector3& b)
  82. {
  83. a += b;
  84. return a;
  85. }
  86. inline Vector3 operator-(Vector3 a, const Vector3& b)
  87. {
  88. a -= b;
  89. return a;
  90. }
  91. inline Vector3 operator*(Vector3 a, float k)
  92. {
  93. a *= k;
  94. return a;
  95. }
  96. inline Vector3 operator*(float k, Vector3 a)
  97. {
  98. a *= k;
  99. return a;
  100. }
  101. inline Vector3 operator/(Vector3 a, float k)
  102. {
  103. a /= k;
  104. return a;
  105. }
  106. inline bool operator==(const Vector3& a, const Vector3& b)
  107. {
  108. return math::equals(a.x, b.x) && math::equals(a.y, b.y) && math::equals(a.z, b.z);
  109. }
  110. namespace vector3
  111. {
  112. //-----------------------------------------------------------------------------
  113. inline float dot(const Vector3& a, const Vector3& b)
  114. {
  115. return a.x * b.x + a.y * b.y + a.z * b.z;
  116. }
  117. //-----------------------------------------------------------------------------
  118. inline Vector3 cross(const Vector3& a, const Vector3& b)
  119. {
  120. return Vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
  121. }
  122. //-----------------------------------------------------------------------------
  123. inline float length(const Vector3& a)
  124. {
  125. return math::sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
  126. }
  127. //-----------------------------------------------------------------------------
  128. inline float squared_length(const Vector3& a)
  129. {
  130. return a.x * a.x + a.y * a.y + a.z * a.z;
  131. }
  132. //-----------------------------------------------------------------------------
  133. inline void set_length(Vector3& a, float len)
  134. {
  135. normalize(a);
  136. a.x *= len;
  137. a.y *= len;
  138. a.z *= len;
  139. }
  140. //-----------------------------------------------------------------------------
  141. inline Vector3 normalize(Vector3& a)
  142. {
  143. float inv_len = 1.0 / length(a);
  144. a.x *= inv_len;
  145. a.y *= inv_len;
  146. a.z *= inv_len;
  147. return a;
  148. }
  149. //-----------------------------------------------------------------------------
  150. inline float distance(const Vector3& a, const Vector3& b)
  151. {
  152. return length(b - a);
  153. }
  154. //-----------------------------------------------------------------------------
  155. inline float angle(const Vector3& a, const Vector3& b)
  156. {
  157. return math::acos(dot(a, b) / (length(a) * length(b)));
  158. }
  159. //-----------------------------------------------------------------------------
  160. inline float* to_float_ptr(Vector3& a)
  161. {
  162. return &a.x;
  163. }
  164. //-----------------------------------------------------------------------------
  165. inline const float* to_float_ptr(const Vector3& a)
  166. {
  167. return &a.x;
  168. }
  169. //-----------------------------------------------------------------------------
  170. inline Vector2 to_vector2(const Vector3& a)
  171. {
  172. return Vector2(a.x, a.y);
  173. }
  174. } // namespace vector3
  175. //-----------------------------------------------------------------------------
  176. inline Vector3::Vector3()
  177. {
  178. // Do not initialize
  179. }
  180. //-----------------------------------------------------------------------------
  181. inline Vector3::Vector3(float val) : x(val), y(val), z(val)
  182. {
  183. }
  184. //-----------------------------------------------------------------------------
  185. inline Vector3::Vector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz)
  186. {
  187. }
  188. //-----------------------------------------------------------------------------
  189. inline Vector3::Vector3(const float v[3]) : x(v[0]), y(v[1]), z(v[2])
  190. {
  191. }
  192. //-----------------------------------------------------------------------------
  193. inline Vector3::Vector3(const Vector3& a) : x(a.x), y(a.y), z(a.z)
  194. {
  195. }
  196. //-----------------------------------------------------------------------------
  197. inline float Vector3::operator[](uint32_t i) const
  198. {
  199. CE_ASSERT(i < 3, "Index must be < 3");
  200. return (&x)[i];
  201. }
  202. //-----------------------------------------------------------------------------
  203. inline float& Vector3::operator[](uint32_t i)
  204. {
  205. CE_ASSERT(i < 3, "Index must be < 3");
  206. return (&x)[i];
  207. }
  208. //-----------------------------------------------------------------------------
  209. inline Vector3& Vector3::operator+=(const Vector3& a)
  210. {
  211. x += a.x;
  212. y += a.y;
  213. z += a.z;
  214. return *this;
  215. }
  216. //-----------------------------------------------------------------------------
  217. inline Vector3& Vector3::operator-=(const Vector3& a)
  218. {
  219. x -= a.x;
  220. y -= a.y;
  221. z -= a.z;
  222. return *this;
  223. }
  224. //-----------------------------------------------------------------------------
  225. inline Vector3& Vector3::operator*=(float k)
  226. {
  227. x *= k;
  228. y *= k;
  229. z *= k;
  230. return *this;
  231. }
  232. //-----------------------------------------------------------------------------
  233. inline Vector3& Vector3::operator/=(float k)
  234. {
  235. CE_ASSERT(k != (float)0.0, "Division by zero");
  236. float inv = (float)(1.0 / k);
  237. x *= inv;
  238. y *= inv;
  239. z *= inv;
  240. return *this;
  241. }
  242. } // namespace crown