vec3.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #pragma once
  15. #include "math-defs.h"
  16. #include "vec4.h"
  17. #include <xmmintrin.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. struct plane;
  22. struct matrix3;
  23. struct matrix4;
  24. struct quat;
  25. struct vec3 {
  26. union {
  27. struct {
  28. float x, y, z, w;
  29. };
  30. float ptr[4];
  31. __m128 m;
  32. };
  33. };
  34. static inline void vec3_zero(struct vec3 *v)
  35. {
  36. v->m = _mm_setzero_ps();
  37. }
  38. static inline void vec3_set(struct vec3 *dst, float x, float y, float z)
  39. {
  40. dst->m = _mm_set_ps(0.0f, z, y, x);
  41. }
  42. static inline void vec3_copy(struct vec3 *dst, const struct vec3 *v)
  43. {
  44. dst->m = v->m;
  45. }
  46. EXPORT void vec3_from_vec4(struct vec3 *dst, const struct vec4 *v);
  47. static inline void vec3_add(struct vec3 *dst, const struct vec3 *v1,
  48. const struct vec3 *v2)
  49. {
  50. dst->m = _mm_add_ps(v1->m, v2->m);
  51. dst->w = 0.0f;
  52. }
  53. static inline void vec3_sub(struct vec3 *dst, const struct vec3 *v1,
  54. const struct vec3 *v2)
  55. {
  56. dst->m = _mm_sub_ps(v1->m, v2->m);
  57. dst->w = 0.0f;
  58. }
  59. static inline void vec3_mul(struct vec3 *dst, const struct vec3 *v1,
  60. const struct vec3 *v2)
  61. {
  62. dst->m = _mm_mul_ps(v1->m, v2->m);
  63. }
  64. static inline void vec3_div(struct vec3 *dst, const struct vec3 *v1,
  65. const struct vec3 *v2)
  66. {
  67. dst->m = _mm_div_ps(v1->m, v2->m);
  68. dst->w = 0.0f;
  69. }
  70. static inline void vec3_addf(struct vec3 *dst, const struct vec3 *v,
  71. float f)
  72. {
  73. dst->m = _mm_add_ps(v->m, _mm_set1_ps(f));
  74. dst->w = 0.0f;
  75. }
  76. static inline void vec3_subf(struct vec3 *dst, const struct vec3 *v,
  77. float f)
  78. {
  79. dst->m = _mm_sub_ps(v->m, _mm_set1_ps(f));
  80. dst->w = 0.0f;
  81. }
  82. static inline void vec3_mulf(struct vec3 *dst, const struct vec3 *v,
  83. float f)
  84. {
  85. dst->m = _mm_mul_ps(v->m, _mm_set1_ps(f));
  86. }
  87. static inline void vec3_divf(struct vec3 *dst, const struct vec3 *v,
  88. float f)
  89. {
  90. dst->m = _mm_div_ps(v->m, _mm_set1_ps(f));
  91. dst->w = 0.0f;
  92. }
  93. static inline float vec3_dot(const struct vec3 *v1, const struct vec3 *v2)
  94. {
  95. struct vec3 add;
  96. __m128 mul = _mm_mul_ps(v1->m, v2->m);
  97. add.m = _mm_add_ps(_mm_movehl_ps(mul, mul), mul);
  98. add.m = _mm_add_ps(_mm_shuffle_ps(add.m, add.m, 0x55), add.m);
  99. return add.x;
  100. }
  101. static inline void vec3_cross(struct vec3 *dst, const struct vec3 *v1,
  102. const struct vec3 *v2)
  103. {
  104. __m128 s1v1 = _mm_shuffle_ps(v1->m, v1->m, _MM_SHUFFLE(3, 0, 2, 1));
  105. __m128 s1v2 = _mm_shuffle_ps(v2->m, v2->m, _MM_SHUFFLE(3, 1, 0, 2));
  106. __m128 s2v1 = _mm_shuffle_ps(v1->m, v1->m, _MM_SHUFFLE(3, 1, 0, 2));
  107. __m128 s2v2 = _mm_shuffle_ps(v2->m, v2->m, _MM_SHUFFLE(3, 0, 2, 1));
  108. dst->m = _mm_sub_ps(_mm_mul_ps(s1v1, s1v2), _mm_mul_ps(s2v1, s2v2));
  109. }
  110. static inline void vec3_neg(struct vec3 *dst, const struct vec3 *v)
  111. {
  112. dst->x = -v->x;
  113. dst->y = -v->y;
  114. dst->z = -v->z;
  115. dst->w = 0.0f;
  116. }
  117. static inline float vec3_len(const struct vec3 *v)
  118. {
  119. float dot_val = vec3_dot(v, v);
  120. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  121. }
  122. static inline float vec3_dist(const struct vec3 *v1, const struct vec3 *v2)
  123. {
  124. struct vec3 temp;
  125. float dot_val;
  126. vec3_sub(&temp, v1, v2);
  127. dot_val = vec3_dot(&temp, &temp);
  128. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  129. }
  130. static inline void vec3_norm(struct vec3 *dst, const struct vec3 *v)
  131. {
  132. float dot_val = vec3_dot(v, v);
  133. dst->m = (dot_val > 0.0f) ?
  134. _mm_mul_ps(v->m, _mm_set1_ps(1.0f/sqrtf(dot_val))) :
  135. _mm_setzero_ps();
  136. }
  137. static inline bool vec3_close(const struct vec3 *v1, const struct vec3 *v2,
  138. float epsilon)
  139. {
  140. struct vec3 test;
  141. vec3_sub(&test, v1, v2);
  142. return test.x < epsilon && test.y < epsilon && test.z < epsilon;
  143. }
  144. static inline void vec3_min(struct vec3 *dst, const struct vec3 *v1,
  145. const struct vec3 *v2)
  146. {
  147. dst->m = _mm_min_ps(v1->m, v2->m);
  148. dst->w = 0.0f;
  149. }
  150. static inline void vec3_minf(struct vec3 *dst, const struct vec3 *v,
  151. float f)
  152. {
  153. dst->m = _mm_min_ps(v->m, _mm_set1_ps(f));
  154. dst->w = 0.0f;
  155. }
  156. static inline void vec3_max(struct vec3 *dst, const struct vec3 *v1,
  157. const struct vec3 *v2)
  158. {
  159. dst->m = _mm_max_ps(v1->m, v2->m);
  160. dst->w = 0.0f;
  161. }
  162. static inline void vec3_maxf(struct vec3 *dst, const struct vec3 *v,
  163. float f)
  164. {
  165. dst->m = _mm_max_ps(v->m, _mm_set1_ps(f));
  166. dst->w = 0.0f;
  167. }
  168. static inline void vec3_abs(struct vec3 *dst, const struct vec3 *v)
  169. {
  170. dst->x = fabsf(v->x);
  171. dst->y = fabsf(v->y);
  172. dst->z = fabsf(v->z);
  173. dst->w = 0.0f;
  174. }
  175. static inline void vec3_floor(struct vec3 *dst, const struct vec3 *v)
  176. {
  177. dst->x = floorf(v->x);
  178. dst->y = floorf(v->y);
  179. dst->z = floorf(v->z);
  180. dst->w = 0.0f;
  181. }
  182. static inline void vec3_ceil(struct vec3 *dst, const struct vec3 *v)
  183. {
  184. dst->x = ceilf(v->x);
  185. dst->y = ceilf(v->y);
  186. dst->z = ceilf(v->z);
  187. dst->w = 0.0f;
  188. }
  189. EXPORT float vec3_plane_dist(const struct vec3 *v, const struct plane *p);
  190. EXPORT void vec3_transform(struct vec3 *dst, const struct vec3 *v,
  191. const struct matrix4 *m);
  192. EXPORT void vec3_rotate(struct vec3 *dst, const struct vec3 *v,
  193. const struct matrix3 *m);
  194. EXPORT void vec3_transform3x4(struct vec3 *dst, const struct vec3 *v,
  195. const struct matrix3 *m);
  196. EXPORT void vec3_mirror(struct vec3 *dst, const struct vec3 *v,
  197. const struct plane *p);
  198. EXPORT void vec3_mirrorv(struct vec3 *dst, const struct vec3 *v,
  199. const struct vec3 *vec);
  200. EXPORT void vec3_rand(struct vec3 *dst, int positive_only);
  201. #ifdef __cplusplus
  202. }
  203. #endif