vector3.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #include "error.h"
  7. #include "types.h"
  8. #include "math_utils.h"
  9. #include "vector2.h"
  10. #include "math_types.h"
  11. namespace crown
  12. {
  13. /// Negates @a a and returns the result.
  14. Vector3 operator-(const Vector3& a);
  15. /// Adds the vector @a a to @a b and returns the result.
  16. Vector3 operator+(Vector3 a, const Vector3& b);
  17. /// Subtracts the vector @a b from @a a and returns the result.
  18. Vector3 operator-(Vector3 a, const Vector3& b);
  19. /// Multiplies the vector @a a by the scalar @a k and returns the result.
  20. Vector3 operator*(Vector3 a, float k);
  21. /// @copydoc operator*(Vector3, float)
  22. Vector3 operator*(float k, Vector3 a);
  23. /// Divides the vector @a a by the scalar @a k and returns the result.
  24. Vector3 operator/(Vector3 a, float k);
  25. /// Returns true whether the vectors @a a and @a b are equal.
  26. bool operator==(const Vector3& a, const Vector3& b);
  27. /// Functions to manipulate Vector3.
  28. ///
  29. /// @ingroup Math
  30. namespace vector3
  31. {
  32. const Vector3 ZERO = Vector3(0, 0, 0);
  33. const Vector3 XAXIS = Vector3(1, 0, 0);
  34. const Vector3 YAXIS = Vector3(0, 1, 0);
  35. const Vector3 ZAXIS = Vector3(0, 0, 1);
  36. const Vector3 FORWARD = Vector3(0, 0, 1);
  37. const Vector3 BACKWARD = Vector3(0, 0, -1);
  38. const Vector3 LEFT = Vector3(-1, 0, 0);
  39. const Vector3 RIGHT = Vector3(1, 0, 0);
  40. const Vector3 UP = Vector3(0, 1, 0);
  41. const Vector3 DOWN = Vector3(0, -1, 0);
  42. /// Returns the dot product between the vectors @a a and @a b.
  43. float dot(const Vector3& a, const Vector3& b);
  44. /// Returns the cross product between the vectors @a a and @a b.
  45. Vector3 cross(const Vector3& a, const Vector3& b);
  46. /// Returns the lenght of @a a.
  47. float length(const Vector3& a);
  48. /// Returns the squared length of @a a.
  49. float squared_length(const Vector3& a);
  50. /// Sets the lenght of @a a to @a len.
  51. void set_length(Vector3& a, float len);
  52. /// Normalizes @a a and returns the result.
  53. Vector3 normalize(Vector3& a);
  54. /// Returns the distance between the points @a a and @a b.
  55. float distance(const Vector3& a, const Vector3& b);
  56. /// Returns the angle between the vectors @a a and @a b.
  57. float angle(const Vector3& a, const Vector3& b);
  58. /// Returns the pointer to the data of @a a.
  59. float* to_float_ptr(Vector3& a);
  60. /// @copydoc to_float_ptr(Vector3&)
  61. const float* to_float_ptr(const Vector3& a);
  62. /// Returns the Vector2 portion of @a a. (i.e. truncates z)
  63. Vector2 to_vector2(const Vector3& a);
  64. } // namespace vector3
  65. inline Vector3 operator-(const Vector3& a)
  66. {
  67. return Vector3(-a.x, -a.y, -a.z);
  68. }
  69. inline Vector3 operator+(Vector3 a, const Vector3& b)
  70. {
  71. a += b;
  72. return a;
  73. }
  74. inline Vector3 operator-(Vector3 a, const Vector3& b)
  75. {
  76. a -= b;
  77. return a;
  78. }
  79. inline Vector3 operator*(Vector3 a, float k)
  80. {
  81. a *= k;
  82. return a;
  83. }
  84. inline Vector3 operator*(float k, Vector3 a)
  85. {
  86. a *= k;
  87. return a;
  88. }
  89. inline Vector3 operator/(Vector3 a, float k)
  90. {
  91. a /= k;
  92. return a;
  93. }
  94. inline bool operator==(const Vector3& a, const Vector3& b)
  95. {
  96. return equals(a.x, b.x) && equals(a.y, b.y) && equals(a.z, b.z);
  97. }
  98. namespace vector3
  99. {
  100. inline float dot(const Vector3& a, const Vector3& b)
  101. {
  102. return a.x * b.x + a.y * b.y + a.z * b.z;
  103. }
  104. inline Vector3 cross(const Vector3& a, const Vector3& b)
  105. {
  106. 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);
  107. }
  108. inline float length(const Vector3& a)
  109. {
  110. return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
  111. }
  112. inline float squared_length(const Vector3& a)
  113. {
  114. return a.x * a.x + a.y * a.y + a.z * a.z;
  115. }
  116. inline void set_length(Vector3& a, float len)
  117. {
  118. normalize(a);
  119. a.x *= len;
  120. a.y *= len;
  121. a.z *= len;
  122. }
  123. inline Vector3 normalize(Vector3& a)
  124. {
  125. float inv_len = 1.0f / length(a);
  126. a.x *= inv_len;
  127. a.y *= inv_len;
  128. a.z *= inv_len;
  129. return a;
  130. }
  131. inline float distance(const Vector3& a, const Vector3& b)
  132. {
  133. return length(b - a);
  134. }
  135. inline float angle(const Vector3& a, const Vector3& b)
  136. {
  137. return acos(dot(a, b) / (length(a) * length(b)));
  138. }
  139. inline float* to_float_ptr(Vector3& a)
  140. {
  141. return &a.x;
  142. }
  143. inline const float* to_float_ptr(const Vector3& a)
  144. {
  145. return &a.x;
  146. }
  147. inline Vector2 to_vector2(const Vector3& a)
  148. {
  149. return Vector2(a.x, a.y);
  150. }
  151. } // namespace vector3
  152. inline Vector3::Vector3()
  153. {
  154. // Do not initialize
  155. }
  156. inline Vector3::Vector3(float val) : x(val), y(val), z(val)
  157. {
  158. }
  159. inline Vector3::Vector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz)
  160. {
  161. }
  162. inline Vector3::Vector3(const float v[3]) : x(v[0]), y(v[1]), z(v[2])
  163. {
  164. }
  165. inline const float& Vector3::operator[](uint32_t i) const
  166. {
  167. CE_ASSERT(i < 3, "Index out of bounds");
  168. return (&x)[i];
  169. }
  170. inline float& Vector3::operator[](uint32_t i)
  171. {
  172. CE_ASSERT(i < 3, "Index out of bounds");
  173. return (&x)[i];
  174. }
  175. inline Vector3& Vector3::operator+=(const Vector3& a)
  176. {
  177. x += a.x;
  178. y += a.y;
  179. z += a.z;
  180. return *this;
  181. }
  182. inline Vector3& Vector3::operator-=(const Vector3& a)
  183. {
  184. x -= a.x;
  185. y -= a.y;
  186. z -= a.z;
  187. return *this;
  188. }
  189. inline Vector3& Vector3::operator*=(float k)
  190. {
  191. x *= k;
  192. y *= k;
  193. z *= k;
  194. return *this;
  195. }
  196. inline Vector3& Vector3::operator/=(float k)
  197. {
  198. CE_ASSERT(k != (float)0.0, "Division by zero");
  199. float inv = (float)(1.0 / k);
  200. x *= inv;
  201. y *= inv;
  202. z *= inv;
  203. return *this;
  204. }
  205. } // namespace crown