vector4.h 5.0 KB

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