vector4.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "math_types.h"
  7. #include "math_utils.h"
  8. namespace crown
  9. {
  10. /// @addtogroup Math
  11. /// @{
  12. inline Vector4 vector4(float x, float y, float z, float w)
  13. {
  14. Vector4 v;
  15. v.x = x;
  16. v.y = y;
  17. v.z = z;
  18. v.w = w;
  19. return v;
  20. }
  21. inline Vector4 vector4(const Vector3& a, float w)
  22. {
  23. Vector4 v;
  24. v.x = a.x;
  25. v.y = a.y;
  26. v.z = a.z;
  27. v.w = w;
  28. return v;
  29. }
  30. inline Vector4& operator+=(Vector4& a, const Vector4& b)
  31. {
  32. a.x += b.x;
  33. a.y += b.y;
  34. a.z += b.z;
  35. a.w += b.w;
  36. return a;
  37. }
  38. inline Vector4& operator-=(Vector4& a, const Vector4& b)
  39. {
  40. a.x -= b.x;
  41. a.y -= b.y;
  42. a.z -= b.z;
  43. a.w -= b.w;
  44. return a;
  45. }
  46. inline Vector4& operator*=(Vector4& a, float k)
  47. {
  48. a.x *= k;
  49. a.y *= k;
  50. a.z *= k;
  51. a.w *= k;
  52. return a;
  53. }
  54. /// Negates @a a and returns the result.
  55. inline Vector4 operator-(const Vector4& a)
  56. {
  57. Vector4 res;
  58. res.x = -a.x;
  59. res.y = -a.y;
  60. res.z = -a.z;
  61. res.w = -a.w;
  62. return res;
  63. }
  64. /// Adds the vector @a a to @a b and returns the result.
  65. inline Vector4 operator+(Vector4 a, const Vector4& b)
  66. {
  67. a += b;
  68. return a;
  69. }
  70. /// Subtracts the vector @a b from @a a and returns the result.
  71. inline Vector4 operator-(Vector4 a, const Vector4& b)
  72. {
  73. a -= b;
  74. return a;
  75. }
  76. /// Multiplies the vector @a a by the scalar @a k and returns the result.
  77. inline Vector4 operator*(Vector4 a, float k)
  78. {
  79. a *= k;
  80. return a;
  81. }
  82. /// Multiplies the vector @a a by the scalar @a k and returns the result.
  83. inline Vector4 operator*(float k, Vector4 a)
  84. {
  85. a *= k;
  86. return a;
  87. }
  88. /// Returns true whether the vectors @a a and @a b are equal.
  89. inline bool operator==(const Vector4& a, const Vector4& b)
  90. {
  91. return fequal(a.x, b.x)
  92. && fequal(a.y, b.y)
  93. && fequal(a.z, b.z)
  94. && fequal(a.w, b.w);
  95. }
  96. /// Returns the dot product between the vectors @a a and @a b.
  97. inline float dot(const Vector4& a, const Vector4& b)
  98. {
  99. return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
  100. }
  101. /// Returns the squared length of @a a.
  102. inline float length_squared(const Vector4& a)
  103. {
  104. return dot(a, a);
  105. }
  106. /// Returns the length of @a a.
  107. inline float length(const Vector4& a)
  108. {
  109. return sqrtf(length_squared(a));
  110. }
  111. /// Normalizes @a a and returns the result.
  112. inline Vector4 normalize(Vector4& a)
  113. {
  114. const float len = length(a);
  115. const float inv_len = 1.0f / len;
  116. a.x *= inv_len;
  117. a.y *= inv_len;
  118. a.z *= inv_len;
  119. a.w *= inv_len;
  120. return a;
  121. }
  122. /// Sets the length of @a a to @a len.
  123. inline void set_length(Vector4& a, float len)
  124. {
  125. normalize(a);
  126. a.x *= len;
  127. a.y *= len;
  128. a.z *= len;
  129. a.w *= len;
  130. }
  131. /// Returns the squared distance between the points @a a and @a b.
  132. inline float distance_squared(const Vector4& a, const Vector4& b)
  133. {
  134. return length_squared(b - a);
  135. }
  136. /// Returns the distance between the points @a a and @a b.
  137. inline float distance(const Vector4& a, const Vector4& b)
  138. {
  139. return length(b - a);
  140. }
  141. /// Returns the angle between the vectors @a a and @a b.
  142. inline float angle(const Vector4& a, const Vector4& b)
  143. {
  144. return acos(dot(a, b) / (length(a) * length(b)));
  145. }
  146. /// Returns a vector that contains the largest value for each component from @a a and @a b.
  147. inline Vector4 max(const Vector4& a, const Vector4& b)
  148. {
  149. Vector4 v;
  150. v.x = fmax(a.x, b.x);
  151. v.y = fmax(a.y, b.y);
  152. v.z = fmax(a.z, b.z);
  153. v.w = fmax(a.w, b.w);
  154. return v;
  155. }
  156. /// Returns a vector that contains the smallest value for each component from @a a and @a b.
  157. inline Vector4 min(const Vector4& a, const Vector4& b)
  158. {
  159. Vector4 v;
  160. v.x = fmin(a.x, b.x);
  161. v.y = fmin(a.y, b.y);
  162. v.z = fmin(a.z, b.z);
  163. v.w = fmin(a.w, b.w);
  164. return v;
  165. }
  166. /// Returns the linearly interpolated vector between @a and @b at time @a t in [0, 1].
  167. inline Vector4 lerp(const Vector4& a, const Vector4& b, float t)
  168. {
  169. Vector4 v;
  170. v.x = lerp(a.x, b.x, t);
  171. v.y = lerp(a.y, b.y, t);
  172. v.z = lerp(a.z, b.z, t);
  173. v.w = lerp(a.w, b.w, t);
  174. return v;
  175. }
  176. /// Returns the pointer to the data of @a a.
  177. inline float* to_float_ptr(Vector4& a)
  178. {
  179. return &a.x;
  180. }
  181. /// Returns the pointer to the data of @a a.
  182. inline const float* to_float_ptr(const Vector4& a)
  183. {
  184. return &a.x;
  185. }
  186. /// Returns the Vector3 portion of @a a. (i.e. truncates w)
  187. inline Vector3 to_vector3(const Vector4& a)
  188. {
  189. Vector3 v;
  190. v.x = a.x;
  191. v.y = a.y;
  192. v.z = a.z;
  193. return v;
  194. }
  195. /// @}
  196. } // namespace crown