vector3.h 5.0 KB

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