1
0

vec2-ext.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. /*
  8. Functions:
  9. CGLM_INLINE void glm_vec2_fill(vec2 v, float val)
  10. CGLM_INLINE bool glm_vec2_eq(vec2 v, float val);
  11. CGLM_INLINE bool glm_vec2_eq_eps(vec2 v, float val);
  12. CGLM_INLINE bool glm_vec2_eq_all(vec2 v);
  13. CGLM_INLINE bool glm_vec2_eqv(vec2 a, vec2 b);
  14. CGLM_INLINE bool glm_vec2_eqv_eps(vec2 a, vec2 b);
  15. CGLM_INLINE float glm_vec2_max(vec2 v);
  16. CGLM_INLINE float glm_vec2_min(vec2 v);
  17. CGLM_INLINE bool glm_vec2_isnan(vec2 v);
  18. CGLM_INLINE bool glm_vec2_isinf(vec2 v);
  19. CGLM_INLINE bool glm_vec2_isvalid(vec2 v);
  20. CGLM_INLINE void glm_vec2_sign(vec2 v, vec2 dest);
  21. CGLM_INLINE void glm_vec2_abs(vec2 v, vec2 dest);
  22. CGLM_INLINE void glm_vec2_fract(vec2 v, vec2 dest);
  23. CGLM_INLINE void glm_vec2_floor(vec2 v, vec2 dest);
  24. CGLM_INLINE float glm_vec2_mods(vec2 v, float s, vec2 dest);
  25. CGLM_INLINE float glm_vec2_steps(float edge, vec2 v, vec2 dest);
  26. CGLM_INLINE void glm_vec2_stepr(vec2 edge, float v, vec2 dest);
  27. CGLM_INLINE void glm_vec2_sqrt(vec2 v, vec2 dest);
  28. CGLM_INLINE void glm_vec2_complex_mul(vec2 a, vec2 b, vec2 dest)
  29. CGLM_INLINE void glm_vec2_complex_div(vec2 a, vec2 b, vec2 dest)
  30. CGLM_INLINE void glm_vec2_complex_conjugate(vec2 a, vec2 dest)
  31. */
  32. #ifndef cglm_vec2_ext_h
  33. #define cglm_vec2_ext_h
  34. #include "common.h"
  35. #include "util.h"
  36. /*!
  37. * @brief fill a vector with specified value
  38. *
  39. * @param[out] v dest
  40. * @param[in] val value
  41. */
  42. CGLM_INLINE
  43. void
  44. glm_vec2_fill(vec2 v, float val) {
  45. v[0] = v[1] = val;
  46. }
  47. /*!
  48. * @brief check if vector is equal to value (without epsilon)
  49. *
  50. * @param[in] v vector
  51. * @param[in] val value
  52. */
  53. CGLM_INLINE
  54. bool
  55. glm_vec2_eq(vec2 v, float val) {
  56. return v[0] == val && v[0] == v[1];
  57. }
  58. /*!
  59. * @brief check if vector is equal to value (with epsilon)
  60. *
  61. * @param[in] v vector
  62. * @param[in] val value
  63. */
  64. CGLM_INLINE
  65. bool
  66. glm_vec2_eq_eps(vec2 v, float val) {
  67. return fabsf(v[0] - val) <= GLM_FLT_EPSILON
  68. && fabsf(v[1] - val) <= GLM_FLT_EPSILON;
  69. }
  70. /*!
  71. * @brief check if vector members are equal (without epsilon)
  72. *
  73. * @param[in] v vector
  74. */
  75. CGLM_INLINE
  76. bool
  77. glm_vec2_eq_all(vec2 v) {
  78. return glm_vec2_eq_eps(v, v[0]);
  79. }
  80. /*!
  81. * @brief check if vector is equal to another (without epsilon)
  82. *
  83. * @param[in] a vector
  84. * @param[in] b vector
  85. */
  86. CGLM_INLINE
  87. bool
  88. glm_vec2_eqv(vec2 a, vec2 b) {
  89. return a[0] == b[0] && a[1] == b[1];
  90. }
  91. /*!
  92. * @brief check if vector is equal to another (with epsilon)
  93. *
  94. * @param[in] a vector
  95. * @param[in] b vector
  96. */
  97. CGLM_INLINE
  98. bool
  99. glm_vec2_eqv_eps(vec2 a, vec2 b) {
  100. return fabsf(a[0] - b[0]) <= GLM_FLT_EPSILON
  101. && fabsf(a[1] - b[1]) <= GLM_FLT_EPSILON;
  102. }
  103. /*!
  104. * @brief max value of vector
  105. *
  106. * @param[in] v vector
  107. */
  108. CGLM_INLINE
  109. float
  110. glm_vec2_max(vec2 v) {
  111. return glm_max(v[0], v[1]);
  112. }
  113. /*!
  114. * @brief min value of vector
  115. *
  116. * @param[in] v vector
  117. */
  118. CGLM_INLINE
  119. float
  120. glm_vec2_min(vec2 v) {
  121. return glm_min(v[0], v[1]);
  122. }
  123. /*!
  124. * @brief check if one of items is NaN (not a number)
  125. * you should only use this in DEBUG mode or very critical asserts
  126. *
  127. * @param[in] v vector
  128. */
  129. CGLM_INLINE
  130. bool
  131. glm_vec2_isnan(vec2 v) {
  132. #ifndef CGLM_FAST_MATH
  133. return isnan(v[0]) || isnan(v[1]);
  134. #else
  135. return false;
  136. #endif
  137. }
  138. /*!
  139. * @brief check if one of items is INFINITY
  140. * you should only use this in DEBUG mode or very critical asserts
  141. *
  142. * @param[in] v vector
  143. */
  144. CGLM_INLINE
  145. bool
  146. glm_vec2_isinf(vec2 v) {
  147. #ifndef CGLM_FAST_MATH
  148. return isinf(v[0]) || isinf(v[1]);
  149. #else
  150. return false;
  151. #endif
  152. }
  153. /*!
  154. * @brief check if all items are valid number
  155. * you should only use this in DEBUG mode or very critical asserts
  156. *
  157. * @param[in] v vector
  158. */
  159. CGLM_INLINE
  160. bool
  161. glm_vec2_isvalid(vec2 v) {
  162. return !glm_vec2_isnan(v) && !glm_vec2_isinf(v);
  163. }
  164. /*!
  165. * @brief get sign of 32 bit float as +1, -1, 0
  166. *
  167. * Important: It returns 0 for zero/NaN input
  168. *
  169. * @param v vector
  170. */
  171. CGLM_INLINE
  172. void
  173. glm_vec2_sign(vec2 v, vec2 dest) {
  174. dest[0] = glm_signf(v[0]);
  175. dest[1] = glm_signf(v[1]);
  176. }
  177. /*!
  178. * @brief absolute value of v
  179. *
  180. * @param[in] v vector
  181. * @param[out] dest destination
  182. */
  183. CGLM_INLINE
  184. void
  185. glm_vec2_abs(vec2 v, vec2 dest) {
  186. dest[0] = fabsf(v[0]);
  187. dest[1] = fabsf(v[1]);
  188. }
  189. /*!
  190. * @brief fractional part of each vector item
  191. *
  192. * @param[in] v vector
  193. * @param[out] dest destination vector
  194. */
  195. CGLM_INLINE
  196. void
  197. glm_vec2_fract(vec2 v, vec2 dest) {
  198. dest[0] = fminf(v[0] - floorf(v[0]), 0.999999940395355224609375f);
  199. dest[1] = fminf(v[1] - floorf(v[1]), 0.999999940395355224609375f);
  200. }
  201. /*!
  202. * @brief floor of each vector item
  203. *
  204. * @param[in] v vector
  205. * @param[out] dest destination vector
  206. */
  207. CGLM_INLINE
  208. void
  209. glm_vec2_floor(vec2 v, vec2 dest) {
  210. dest[0] = floorf(v[0]);
  211. dest[1] = floorf(v[1]);
  212. }
  213. /*!
  214. * @brief mod of each vector item, result is written to dest (dest = v % s)
  215. *
  216. * @param[in] v vector
  217. * @param[in] s scalar
  218. * @param[out] dest destination vector
  219. */
  220. CGLM_INLINE
  221. void
  222. glm_vec2_mods(vec2 v, float s, vec2 dest) {
  223. dest[0] = fmodf(v[0], s);
  224. dest[1] = fmodf(v[1], s);
  225. }
  226. /*!
  227. * @brief square root of each vector item
  228. *
  229. * @param[in] v vector
  230. * @param[out] dest destination vector
  231. */
  232. CGLM_INLINE
  233. void
  234. glm_vec2_sqrt(vec2 v, vec2 dest) {
  235. dest[0] = sqrtf(v[0]);
  236. dest[1] = sqrtf(v[1]);
  237. }
  238. /*!
  239. * @brief treat vectors as complex numbers and multiply them as such.
  240. *
  241. * @param[in] a left number
  242. * @param[in] b right number
  243. * @param[out] dest destination number
  244. */
  245. CGLM_INLINE
  246. void
  247. glm_vec2_complex_mul(vec2 a, vec2 b, vec2 dest) {
  248. float tr, ti;
  249. tr = a[0] * b[0] - a[1] * b[1];
  250. ti = a[0] * b[1] + a[1] * b[0];
  251. dest[0] = tr;
  252. dest[1] = ti;
  253. }
  254. /*!
  255. * @brief threshold each vector item with scalar
  256. * condition is: (x[i] < edge) ? 0.0 : 1.0
  257. *
  258. * @param[in] edge threshold
  259. * @param[in] x vector to test against threshold
  260. * @param[out] dest destination
  261. */
  262. CGLM_INLINE
  263. void
  264. glm_vec2_steps(float edge, vec2 x, vec2 dest) {
  265. dest[0] = glm_step(edge, x[0]);
  266. dest[1] = glm_step(edge, x[1]);
  267. }
  268. /*!
  269. * @brief threshold a value with *vector* as the threshold
  270. * condition is: (x < edge[i]) ? 0.0 : 1.0
  271. *
  272. * @param[in] edge threshold vector
  273. * @param[in] x value to test against threshold
  274. * @param[out] dest destination
  275. */
  276. CGLM_INLINE
  277. void
  278. glm_vec2_stepr(vec2 edge, float x, vec2 dest) {
  279. dest[0] = glm_step(edge[0], x);
  280. dest[1] = glm_step(edge[1], x);
  281. }
  282. /*!
  283. * @brief treat vectors as complex numbers and divide them as such.
  284. *
  285. * @param[in] a left number (numerator)
  286. * @param[in] b right number (denominator)
  287. * @param[out] dest destination number
  288. */
  289. CGLM_INLINE
  290. void
  291. glm_vec2_complex_div(vec2 a, vec2 b, vec2 dest) {
  292. float tr, ti;
  293. float const ibnorm2 = 1.0f / (b[0] * b[0] + b[1] * b[1]);
  294. tr = ibnorm2 * (a[0] * b[0] + a[1] * b[1]);
  295. ti = ibnorm2 * (a[1] * b[0] - a[0] * b[1]);
  296. dest[0] = tr;
  297. dest[1] = ti;
  298. }
  299. /*!
  300. * @brief treat the vector as a complex number and conjugate it as such.
  301. *
  302. * @param[in] a the number
  303. * @param[out] dest destination number
  304. */
  305. CGLM_INLINE
  306. void
  307. glm_vec2_complex_conjugate(vec2 a, vec2 dest) {
  308. dest[0] = a[0];
  309. dest[1] = -a[1];
  310. }
  311. #endif /* cglm_vec2_ext_h */