vec3.h 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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. Macros:
  9. GLMS_VEC3_ONE_INIT
  10. GLMS_VEC3_ZERO_INIT
  11. GLMS_VEC3_ONE
  12. GLMS_VEC3_ZERO
  13. GLMS_YUP
  14. GLMS_ZUP
  15. GLMS_XUP
  16. Functions:
  17. CGLM_INLINE vec3s glms_vec3(vec4s v4);
  18. CGLM_INLINE void glms_vec3_pack(vec3s dst[], vec3 src[], size_t len);
  19. CGLM_INLINE void glms_vec3_unpack(vec3 dst[], vec3s src[], size_t len);
  20. CGLM_INLINE vec3s glms_vec3_zero(void);
  21. CGLM_INLINE vec3s glms_vec3_one(void);
  22. CGLM_INLINE float glms_vec3_dot(vec3s a, vec3s b);
  23. CGLM_INLINE float glms_vec3_norm2(vec3s v);
  24. CGLM_INLINE float glms_vec3_norm(vec3s v);
  25. CGLM_INLINE float glms_vec3_norm_one(vec3s v);
  26. CGLM_INLINE float glms_vec3_norm_inf(vec3s v);
  27. CGLM_INLINE vec3s glms_vec3_add(vec3s a, vec3s b);
  28. CGLM_INLINE vec3s glms_vec3_adds(vec3s a, float s);
  29. CGLM_INLINE vec3s glms_vec3_sub(vec3s a, vec3s b);
  30. CGLM_INLINE vec3s glms_vec3_subs(vec3s a, float s);
  31. CGLM_INLINE vec3s glms_vec3_mul(vec3s a, vec3s b);
  32. CGLM_INLINE vec3s glms_vec3_scale(vec3s v, float s);
  33. CGLM_INLINE vec3s glms_vec3_scale_as(vec3s v, float s);
  34. CGLM_INLINE vec3s glms_vec3_div(vec3s a, vec3s b);
  35. CGLM_INLINE vec3s glms_vec3_divs(vec3s a, float s);
  36. CGLM_INLINE vec3s glms_vec3_addadd(vec3s a, vec3s b, vec3s dest);
  37. CGLM_INLINE vec3s glms_vec3_subadd(vec3s a, vec3s b, vec3s dest);
  38. CGLM_INLINE vec3s glms_vec3_muladd(vec3s a, vec3s b, vec3s dest);
  39. CGLM_INLINE vec3s glms_vec3_muladds(vec3s a, float s, vec3s dest);
  40. CGLM_INLINE vec3s glms_vec3_maxadd(vec3s a, vec3s b, vec3s dest);
  41. CGLM_INLINE vec3s glms_vec3_minadd(vec3s a, vec3s b, vec3s dest);
  42. CGLM_INLINE vec3s glms_vec3_subsub(vec3s a, vec3s b, vec3s dest);
  43. CGLM_INLINE vec3s glms_vec3_addsub(vec3s a, vec3s b, vec3s dest);
  44. CGLM_INLINE vec3s glms_vec3_mulsub(vec3s a, vec3s b, vec3s dest);
  45. CGLM_INLINE vec3s glms_vec3_mulsubs(vec3s a, float s, vec3s dest);
  46. CGLM_INLINE vec3s glms_vec3_maxsub(vec3s a, vec3s b, vec3s dest);
  47. CGLM_INLINE vec3s glms_vec3_minsub(vec3s a, vec3s b, vec3s dest);
  48. CGLM_INLINE vec3s glms_vec3_flipsign(vec3s v);
  49. CGLM_INLINE vec3s glms_vec3_negate(vec3s v);
  50. CGLM_INLINE vec3s glms_vec3_normalize(vec3s v);
  51. CGLM_INLINE vec3s glms_vec3_cross(vec3s a, vec3s b);
  52. CGLM_INLINE vec3s glms_vec3_crossn(vec3s a, vec3s b);
  53. CGLM_INLINE float glms_vec3_angle(vec3s a, vec3s b);
  54. CGLM_INLINE vec3s glms_vec3_rotate(vec3s v, float angle, vec3s axis);
  55. CGLM_INLINE vec3s glms_vec3_rotate_m4(mat4s m, vec3s v);
  56. CGLM_INLINE vec3s glms_vec3_rotate_m3(mat3s m, vec3s v);
  57. CGLM_INLINE vec3s glms_vec3_proj(vec3s a, vec3s b);
  58. CGLM_INLINE vec3s glms_vec3_center(vec3s a, vec3s b);
  59. CGLM_INLINE float glms_vec3_distance(vec3s a, vec3s b);
  60. CGLM_INLINE float glms_vec3_distance2(vec3s a, vec3s b);
  61. CGLM_INLINE vec3s glms_vec3_maxv(vec3s a, vec3s b);
  62. CGLM_INLINE vec3s glms_vec3_minv(vec3s a, vec3s b);
  63. CGLM_INLINE vec3s glms_vec3_ortho(vec3s v);
  64. CGLM_INLINE vec3s glms_vec3_clamp(vec3s v, float minVal, float maxVal);
  65. CGLM_INLINE vec3s glms_vec3_lerp(vec3s from, vec3s to, float t);
  66. CGLM_INLINE vec3s glms_vec3_lerpc(vec3s from, vec3s to, float t);
  67. CGLM_INLINE vec3s glms_vec3_mix(vec3s from, vec3s to, float t);
  68. CGLM_INLINE vec3s glms_vec3_mixc(vec3s from, vec3s to, float t);
  69. CGLM_INLINE vec3s glms_vec3_step(vec3s edge, vec3s x);
  70. CGLM_INLINE vec3s glms_vec3_smoothstep_uni(float edge0, float edge1, vec3s x);
  71. CGLM_INLINE vec3s glms_vec3_smoothstep(vec3s edge0, vec3s edge1, vec3s x);
  72. CGLM_INLINE vec3s glms_vec3_smoothinterp(vec3s from, vec3s to, float t);
  73. CGLM_INLINE vec3s glms_vec3_smoothinterpc(vec3s from, vec3s to, float t);
  74. CGLM_INLINE vec3s glms_vec3_swizzle(vec3s v, int mask);
  75. CGLM_INLINE vec3s glms_vec3_make(float * restrict src);
  76. CGLM_INLINE vec3s glms_vec3_faceforward(vec3s n, vec3s v, vec3s nref);
  77. CGLM_INLINE vec3s glms_vec3_reflect(vec3s v, vec3s n);
  78. CGLM_INLINE bool glms_vec3_refract(vec3s v, vec3s n, float eta, vec3s *dest)
  79. Convenient:
  80. CGLM_INLINE vec3s glms_cross(vec3s a, vec3s b);
  81. CGLM_INLINE float glms_dot(vec3s a, vec3s b);
  82. CGLM_INLINE vec3s glms_normalize(vec3s v);
  83. Deprecated:
  84. glms_vec3_step_uni --> use glms_vec3_steps
  85. */
  86. #ifndef cglms_vec3s_h
  87. #define cglms_vec3s_h
  88. #include "../common.h"
  89. #include "../types-struct.h"
  90. #include "../util.h"
  91. #include "../vec3.h"
  92. #include "vec3-ext.h"
  93. /* DEPRECATED! */
  94. #define glms_vec3_step_uni(edge, x) glms_vec3_steps(edge, x)
  95. #define GLMS_VEC3_ONE_INIT {GLM_VEC3_ONE_INIT}
  96. #define GLMS_VEC3_ZERO_INIT {GLM_VEC3_ZERO_INIT}
  97. #define GLMS_VEC3_ONE ((vec3s)GLMS_VEC3_ONE_INIT)
  98. #define GLMS_VEC3_ZERO ((vec3s)GLMS_VEC3_ZERO_INIT)
  99. #define GLMS_YUP ((vec3s){{0.0f, 1.0f, 0.0f}})
  100. #define GLMS_ZUP ((vec3s){{0.0f, 0.0f, 1.0f}})
  101. #define GLMS_XUP ((vec3s){{1.0f, 0.0f, 0.0f}})
  102. /*!
  103. * @brief init vec3 using vec4
  104. *
  105. * @param[in] v4 vector4
  106. * @returns destination
  107. */
  108. CGLM_INLINE
  109. vec3s
  110. glms_vec3(vec4s v4) {
  111. vec3s r;
  112. glm_vec3(v4.raw, r.raw);
  113. return r;
  114. }
  115. /*!
  116. * @brief pack an array of vec3 into an array of vec3s
  117. *
  118. * @param[out] dst array of vec3
  119. * @param[in] src array of vec3s
  120. * @param[in] len number of elements
  121. */
  122. CGLM_INLINE
  123. void
  124. glms_vec3_(pack)(vec3s dst[], vec3 src[], size_t len) {
  125. size_t i;
  126. for (i = 0; i < len; i++) {
  127. glm_vec3_copy(src[i], dst[i].raw);
  128. }
  129. }
  130. /*!
  131. * @brief unpack an array of vec3s into an array of vec3
  132. *
  133. * @param[out] dst array of vec3s
  134. * @param[in] src array of vec3
  135. * @param[in] len number of elements
  136. */
  137. CGLM_INLINE
  138. void
  139. glms_vec3_(unpack)(vec3 dst[], vec3s src[], size_t len) {
  140. size_t i;
  141. for (i = 0; i < len; i++) {
  142. glm_vec3_copy(src[i].raw, dst[i]);
  143. }
  144. }
  145. /*!
  146. * @brief make vector zero
  147. *
  148. * @returns zero vector
  149. */
  150. CGLM_INLINE
  151. vec3s
  152. glms_vec3_(zero)(void) {
  153. vec3s r;
  154. glm_vec3_zero(r.raw);
  155. return r;
  156. }
  157. /*!
  158. * @brief make vector one
  159. *
  160. * @returns one vector
  161. */
  162. CGLM_INLINE
  163. vec3s
  164. glms_vec3_(one)(void) {
  165. vec3s r;
  166. glm_vec3_one(r.raw);
  167. return r;
  168. }
  169. /*!
  170. * @brief vec3 dot product
  171. *
  172. * @param[in] a vector1
  173. * @param[in] b vector2
  174. *
  175. * @return dot product
  176. */
  177. CGLM_INLINE
  178. float
  179. glms_vec3_(dot)(vec3s a, vec3s b) {
  180. return glm_vec3_dot(a.raw, b.raw);
  181. }
  182. /*!
  183. * @brief norm * norm (magnitude) of vec
  184. *
  185. * we can use this func instead of calling norm * norm, because it would call
  186. * sqrtf function twice but with this func we can avoid func call, maybe this is
  187. * not good name for this func
  188. *
  189. * @param[in] v vector
  190. *
  191. * @return norm * norm
  192. */
  193. CGLM_INLINE
  194. float
  195. glms_vec3_(norm2)(vec3s v) {
  196. return glm_vec3_norm2(v.raw);
  197. }
  198. /*!
  199. * @brief norm (magnitude) of vec3
  200. *
  201. * @param[in] v vector
  202. *
  203. * @return norm
  204. */
  205. CGLM_INLINE
  206. float
  207. glms_vec3_(norm)(vec3s v) {
  208. return glm_vec3_norm(v.raw);
  209. }
  210. /*!
  211. * @brief L1 norm of vec3
  212. * Also known as Manhattan Distance or Taxicab norm.
  213. * L1 Norm is the sum of the magnitudes of the vectors in a space.
  214. * It is calculated as the sum of the absolute values of the vector components.
  215. * In this norm, all the components of the vector are weighted equally.
  216. *
  217. * This computes:
  218. * R = |v[0]| + |v[1]| + |v[2]|
  219. *
  220. * @param[in] v vector
  221. *
  222. * @return L1 norm
  223. */
  224. CGLM_INLINE
  225. float
  226. glms_vec3_(norm_one)(vec3s v) {
  227. return glm_vec3_norm_one(v.raw);
  228. }
  229. /*!
  230. * @brief Infinity norm of vec3
  231. * Also known as Maximum norm.
  232. * Infinity Norm is the largest magnitude among each element of a vector.
  233. * It is calculated as the maximum of the absolute values of the vector components.
  234. *
  235. * This computes:
  236. * inf norm = max(|v[0]|, |v[1]|, |v[2]|)
  237. *
  238. * @param[in] v vector
  239. *
  240. * @return Infinity norm
  241. */
  242. CGLM_INLINE
  243. float
  244. glms_vec3_(norm_inf)(vec3s v) {
  245. return glm_vec3_norm_inf(v.raw);
  246. }
  247. /*!
  248. * @brief add a vector to b vector store result in dest
  249. *
  250. * @param[in] a vector1
  251. * @param[in] b vector2
  252. * @returns destination vector
  253. */
  254. CGLM_INLINE
  255. vec3s
  256. glms_vec3_(add)(vec3s a, vec3s b) {
  257. vec3s r;
  258. glm_vec3_add(a.raw, b.raw, r.raw);
  259. return r;
  260. }
  261. /*!
  262. * @brief add scalar to v vector store result in dest (d = v + s)
  263. *
  264. * @param[in] a vector
  265. * @param[in] s scalar
  266. * @returns destination vector
  267. */
  268. CGLM_INLINE
  269. vec3s
  270. glms_vec3_(adds)(vec3s a, float s) {
  271. vec3s r;
  272. glm_vec3_adds(a.raw, s, r.raw);
  273. return r;
  274. }
  275. /*!
  276. * @brief subtract b vector from a vector store result in dest
  277. *
  278. * @param[in] a vector1
  279. * @param[in] b vector2
  280. * @returns destination vector
  281. */
  282. CGLM_INLINE
  283. vec3s
  284. glms_vec3_(sub)(vec3s a, vec3s b) {
  285. vec3s r;
  286. glm_vec3_sub(a.raw, b.raw, r.raw);
  287. return r;
  288. }
  289. /*!
  290. * @brief subtract scalar from v vector store result in dest (d = v - s)
  291. *
  292. * @param[in] a vector
  293. * @param[in] s scalar
  294. * @returns destination vector
  295. */
  296. CGLM_INLINE
  297. vec3s
  298. glms_vec3_(subs)(vec3s a, float s) {
  299. vec3s r;
  300. glm_vec3_subs(a.raw, s, r.raw);
  301. return r;
  302. }
  303. /*!
  304. * @brief multiply two vectors (component-wise multiplication)
  305. *
  306. * @param a vector1
  307. * @param b vector2
  308. * @returns v3 = (a[0] * b[0], a[1] * b[1], a[2] * b[2])
  309. */
  310. CGLM_INLINE
  311. vec3s
  312. glms_vec3_(mul)(vec3s a, vec3s b) {
  313. vec3s r;
  314. glm_vec3_mul(a.raw, b.raw, r.raw);
  315. return r;
  316. }
  317. /*!
  318. * @brief multiply/scale vec3 vector with scalar: result = v * s
  319. *
  320. * @param[in] v vector
  321. * @param[in] s scalar
  322. * @returns destination vector
  323. */
  324. CGLM_INLINE
  325. vec3s
  326. glms_vec3_(scale)(vec3s v, float s) {
  327. vec3s r;
  328. glm_vec3_scale(v.raw, s, r.raw);
  329. return r;
  330. }
  331. /*!
  332. * @brief make vec3 vector scale as specified: result = unit(v) * s
  333. *
  334. * @param[in] v vector
  335. * @param[in] s scalar
  336. * @returns destination vector
  337. */
  338. CGLM_INLINE
  339. vec3s
  340. glms_vec3_(scale_as)(vec3s v, float s) {
  341. vec3s r;
  342. glm_vec3_scale_as(v.raw, s, r.raw);
  343. return r;
  344. }
  345. /*!
  346. * @brief div vector with another component-wise division: d = a / b
  347. *
  348. * @param[in] a vector 1
  349. * @param[in] b vector 2
  350. * @returns result = (a[0]/b[0], a[1]/b[1], a[2]/b[2])
  351. */
  352. CGLM_INLINE
  353. vec3s
  354. glms_vec3_(div)(vec3s a, vec3s b) {
  355. vec3s r;
  356. glm_vec3_div(a.raw, b.raw, r.raw);
  357. return r;
  358. }
  359. /*!
  360. * @brief div vector with scalar: d = v / s
  361. *
  362. * @param[in] a vector
  363. * @param[in] s scalar
  364. * @returns result = (a[0]/s, a[1]/s, a[2]/s)
  365. */
  366. CGLM_INLINE
  367. vec3s
  368. glms_vec3_(divs)(vec3s a, float s) {
  369. vec3s r;
  370. glm_vec3_divs(a.raw, s, r.raw);
  371. return r;
  372. }
  373. /*!
  374. * @brief add two vectors and add result to sum
  375. *
  376. * it applies += operator so dest must be initialized
  377. *
  378. * @param[in] a vector 1
  379. * @param[in] b vector 2
  380. * @returns dest += (a + b)
  381. */
  382. CGLM_INLINE
  383. vec3s
  384. glms_vec3_(addadd)(vec3s a, vec3s b, vec3s dest) {
  385. glm_vec3_addadd(a.raw, b.raw, dest.raw);
  386. return dest;
  387. }
  388. /*!
  389. * @brief sub two vectors and add result to dest
  390. *
  391. * it applies += operator so dest must be initialized
  392. *
  393. * @param[in] a vector 1
  394. * @param[in] b vector 2
  395. * @returns dest += (a + b)
  396. */
  397. CGLM_INLINE
  398. vec3s
  399. glms_vec3_(subadd)(vec3s a, vec3s b, vec3s dest) {
  400. glm_vec3_subadd(a.raw, b.raw, dest.raw);
  401. return dest;
  402. }
  403. /*!
  404. * @brief mul two vectors and add result to dest
  405. *
  406. * it applies += operator so dest must be initialized
  407. *
  408. * @param[in] a vector 1
  409. * @param[in] b vector 2
  410. * @returns dest += (a * b)
  411. */
  412. CGLM_INLINE
  413. vec3s
  414. glms_vec3_(muladd)(vec3s a, vec3s b, vec3s dest) {
  415. glm_vec3_muladd(a.raw, b.raw, dest.raw);
  416. return dest;
  417. }
  418. /*!
  419. * @brief mul vector with scalar and add result to sum
  420. *
  421. * it applies += operator so dest must be initialized
  422. *
  423. * @param[in] a vector
  424. * @param[in] s scalar
  425. * @returns dest += (a * b)
  426. */
  427. CGLM_INLINE
  428. vec3s
  429. glms_vec3_(muladds)(vec3s a, float s, vec3s dest) {
  430. glm_vec3_muladds(a.raw, s, dest.raw);
  431. return dest;
  432. }
  433. /*!
  434. * @brief add max of two vectors to result/dest
  435. *
  436. * it applies += operator so dest must be initialized
  437. *
  438. * @param[in] a vector 1
  439. * @param[in] b vector 2
  440. * @returns dest += max(a, b)
  441. */
  442. CGLM_INLINE
  443. vec3s
  444. glms_vec3_(maxadd)(vec3s a, vec3s b, vec3s dest) {
  445. glm_vec3_maxadd(a.raw, b.raw, dest.raw);
  446. return dest;
  447. }
  448. /*!
  449. * @brief add min of two vectors to result/dest
  450. *
  451. * it applies += operator so dest must be initialized
  452. *
  453. * @param[in] a vector 1
  454. * @param[in] b vector 2
  455. * @returns dest += min(a, b)
  456. */
  457. CGLM_INLINE
  458. vec3s
  459. glms_vec3_(minadd)(vec3s a, vec3s b, vec3s dest) {
  460. glm_vec3_minadd(a.raw, b.raw, dest.raw);
  461. return dest;
  462. }
  463. /*!
  464. * @brief sub two vectors and sub result to dest
  465. *
  466. * it applies -= operator so dest must be initialized
  467. *
  468. * @param[in] a vector 1
  469. * @param[in] b vector 2
  470. * @returns dest -= (a - b)
  471. */
  472. CGLM_INLINE
  473. vec3s
  474. glms_vec3_(subsub)(vec3s a, vec3s b, vec3s dest) {
  475. glm_vec3_subsub(a.raw, b.raw, dest.raw);
  476. return dest;
  477. }
  478. /*!
  479. * @brief add two vectors and sub result to dest
  480. *
  481. * it applies -= operator so dest must be initialized
  482. *
  483. * @param[in] a vector 1
  484. * @param[in] b vector 2
  485. * @returns dest -= (a + b)
  486. */
  487. CGLM_INLINE
  488. vec3s
  489. glms_vec3_(addsub)(vec3s a, vec3s b, vec3s dest) {
  490. glm_vec3_addsub(a.raw, b.raw, dest.raw);
  491. return dest;
  492. }
  493. /*!
  494. * @brief mul two vectors and sub result to dest
  495. *
  496. * it applies -= operator so dest must be initialized
  497. *
  498. * @param[in] a vector 1
  499. * @param[in] b vector 2
  500. * @returns dest -= (a * b)
  501. */
  502. CGLM_INLINE
  503. vec3s
  504. glms_vec3_(mulsub)(vec3s a, vec3s b, vec3s dest) {
  505. glm_vec3_mulsub(a.raw, b.raw, dest.raw);
  506. return dest;
  507. }
  508. /*!
  509. * @brief mul vector with scalar and sub result to dest
  510. *
  511. * it applies -= operator so dest must be initialized
  512. *
  513. * @param[in] a vector
  514. * @param[in] s scalar
  515. * @returns dest -= (a * b)
  516. */
  517. CGLM_INLINE
  518. vec3s
  519. glms_vec3_(mulsubs)(vec3s a, float s, vec3s dest) {
  520. glm_vec3_mulsubs(a.raw, s, dest.raw);
  521. return dest;
  522. }
  523. /*!
  524. * @brief sub max of two vectors to dest
  525. *
  526. * it applies -= operator so dest must be initialized
  527. *
  528. * @param[in] a vector 1
  529. * @param[in] b vector 2
  530. * @returns dest -= max(a, b)
  531. */
  532. CGLM_INLINE
  533. vec3s
  534. glms_vec3_(maxsub)(vec3s a, vec3s b, vec3s dest) {
  535. glm_vec3_maxsub(a.raw, b.raw, dest.raw);
  536. return dest;
  537. }
  538. /*!
  539. * @brief sub min of two vectors to dest
  540. *
  541. * it applies -= operator so dest must be initialized
  542. *
  543. * @param[in] a vector 1
  544. * @param[in] b vector 2
  545. * @returns dest -= min(a, b)
  546. */
  547. CGLM_INLINE
  548. vec3s
  549. glms_vec3_(minsub)(vec3s a, vec3s b, vec3s dest) {
  550. glm_vec3_minsub(a.raw, b.raw, dest.raw);
  551. return dest;
  552. }
  553. /*!
  554. * @brief negate vector components and store result in dest
  555. *
  556. * @param[in] v vector
  557. * @returns result vector
  558. */
  559. CGLM_INLINE
  560. vec3s
  561. glms_vec3_(flipsign)(vec3s v) {
  562. glm_vec3_flipsign(v.raw);
  563. return v;
  564. }
  565. /*!
  566. * @brief negate vector components
  567. *
  568. * @param[in] v vector
  569. * @returns negated vector
  570. */
  571. CGLM_INLINE
  572. vec3s
  573. glms_vec3_(negate)(vec3s v) {
  574. glm_vec3_negate(v.raw);
  575. return v;
  576. }
  577. /*!
  578. * @brief normalize vec3 and store result in same vec
  579. *
  580. * @param[in] v vector
  581. * @returns normalized vector
  582. */
  583. CGLM_INLINE
  584. vec3s
  585. glms_vec3_(normalize)(vec3s v) {
  586. glm_vec3_normalize(v.raw);
  587. return v;
  588. }
  589. /*!
  590. * @brief cross product of two vector (RH)
  591. *
  592. * @param[in] a vector 1
  593. * @param[in] b vector 2
  594. * @returns destination
  595. */
  596. CGLM_INLINE
  597. vec3s
  598. glms_vec3_(cross)(vec3s a, vec3s b) {
  599. vec3s r;
  600. glm_vec3_cross(a.raw, b.raw, r.raw);
  601. return r;
  602. }
  603. /*!
  604. * @brief cross product of two vector (RH) and normalize the result
  605. *
  606. * @param[in] a vector 1
  607. * @param[in] b vector 2
  608. * @returns destination
  609. */
  610. CGLM_INLINE
  611. vec3s
  612. glms_vec3_(crossn)(vec3s a, vec3s b) {
  613. vec3s r;
  614. glm_vec3_crossn(a.raw, b.raw, r.raw);
  615. return r;
  616. }
  617. /*!
  618. * @brief angle between two vector
  619. *
  620. * @param[in] a vector1
  621. * @param[in] b vector2
  622. *
  623. * @return angle as radians
  624. */
  625. CGLM_INLINE
  626. float
  627. glms_vec3_(angle)(vec3s a, vec3s b) {
  628. return glm_vec3_angle(a.raw, b.raw);
  629. }
  630. /*!
  631. * @brief rotate vec3 around axis by angle using Rodrigues' rotation formula
  632. *
  633. * @param[in] v vector
  634. * @param[in] axis axis vector (must be unit vector)
  635. * @param[in] angle angle by radians
  636. * @returns rotated vector
  637. */
  638. CGLM_INLINE
  639. vec3s
  640. glms_vec3_(rotate)(vec3s v, float angle, vec3s axis) {
  641. glm_vec3_rotate(v.raw, angle, axis.raw);
  642. return v;
  643. }
  644. /*!
  645. * @brief apply rotation matrix to vector
  646. *
  647. * matrix format should be (no perspective):
  648. * a b c x
  649. * e f g y
  650. * i j k z
  651. * 0 0 0 w
  652. *
  653. * @param[in] m affine matrix or rot matrix
  654. * @param[in] v vector
  655. * @returns rotated vector
  656. */
  657. CGLM_INLINE
  658. vec3s
  659. glms_vec3_(rotate_m4)(mat4s m, vec3s v) {
  660. vec3s r;
  661. glm_vec3_rotate_m4(m.raw, v.raw, r.raw);
  662. return r;
  663. }
  664. /*!
  665. * @brief apply rotation matrix to vector
  666. *
  667. * @param[in] m affine matrix or rot matrix
  668. * @param[in] v vector
  669. * @returns rotated vector
  670. */
  671. CGLM_INLINE
  672. vec3s
  673. glms_vec3_(rotate_m3)(mat3s m, vec3s v) {
  674. vec3s r;
  675. glm_vec3_rotate_m3(m.raw, v.raw, r.raw);
  676. return r;
  677. }
  678. /*!
  679. * @brief project a vector onto b vector
  680. *
  681. * @param[in] a vector1
  682. * @param[in] b vector2
  683. * @returns projected vector
  684. */
  685. CGLM_INLINE
  686. vec3s
  687. glms_vec3_(proj)(vec3s a, vec3s b) {
  688. vec3s r;
  689. glm_vec3_proj(a.raw, b.raw, r.raw);
  690. return r;
  691. }
  692. /**
  693. * @brief find center point of two vector
  694. *
  695. * @param[in] a vector1
  696. * @param[in] b vector2
  697. * @returns center point
  698. */
  699. CGLM_INLINE
  700. vec3s
  701. glms_vec3_(center)(vec3s a, vec3s b) {
  702. vec3s r;
  703. glm_vec3_center(a.raw, b.raw, r.raw);
  704. return r;
  705. }
  706. /**
  707. * @brief distance between two vectors
  708. *
  709. * @param[in] a vector1
  710. * @param[in] b vector2
  711. * @return distance
  712. */
  713. CGLM_INLINE
  714. float
  715. glms_vec3_(distance)(vec3s a, vec3s b) {
  716. return glm_vec3_distance(a.raw, b.raw);
  717. }
  718. /**
  719. * @brief squared distance between two vectors
  720. *
  721. * @param[in] a vector1
  722. * @param[in] b vector2
  723. * @return squared distance (distance * distance)
  724. */
  725. CGLM_INLINE
  726. float
  727. glms_vec3_(distance2)(vec3s a, vec3s b) {
  728. return glm_vec3_distance2(a.raw, b.raw);
  729. }
  730. /*!
  731. * @brief max values of vectors
  732. *
  733. * @param[in] a vector1
  734. * @param[in] b vector2
  735. * @returns destination
  736. */
  737. CGLM_INLINE
  738. vec3s
  739. glms_vec3_(maxv)(vec3s a, vec3s b) {
  740. vec3s r;
  741. glm_vec3_maxv(a.raw, b.raw, r.raw);
  742. return r;
  743. }
  744. /*!
  745. * @brief min values of vectors
  746. *
  747. * @param[in] a vector1
  748. * @param[in] b vector2
  749. * @returns destination
  750. */
  751. CGLM_INLINE
  752. vec3s
  753. glms_vec3_(minv)(vec3s a, vec3s b) {
  754. vec3s r;
  755. glm_vec3_minv(a.raw, b.raw, r.raw);
  756. return r;
  757. }
  758. /*!
  759. * @brief possible orthogonal/perpendicular vector
  760. *
  761. * @param[in] v vector
  762. * @returns orthogonal/perpendicular vector
  763. */
  764. CGLM_INLINE
  765. vec3s
  766. glms_vec3_(ortho)(vec3s v) {
  767. vec3s r;
  768. glm_vec3_ortho(v.raw, r.raw);
  769. return r;
  770. }
  771. /*!
  772. * @brief clamp vector's individual members between min and max values
  773. *
  774. * @param[in] v vector
  775. * @param[in] minVal minimum value
  776. * @param[in] maxVal maximum value
  777. * @returns clamped vector
  778. */
  779. CGLM_INLINE
  780. vec3s
  781. glms_vec3_(clamp)(vec3s v, float minVal, float maxVal) {
  782. glm_vec3_clamp(v.raw, minVal, maxVal);
  783. return v;
  784. }
  785. /*!
  786. * @brief linear interpolation between two vectors
  787. *
  788. * formula: from + s * (to - from)
  789. *
  790. * @param[in] from from value
  791. * @param[in] to to value
  792. * @param[in] t interpolant (amount)
  793. * @returns destination
  794. */
  795. CGLM_INLINE
  796. vec3s
  797. glms_vec3_(lerp)(vec3s from, vec3s to, float t) {
  798. vec3s r;
  799. glm_vec3_lerp(from.raw, to.raw, t, r.raw);
  800. return r;
  801. }
  802. /*!
  803. * @brief linear interpolation between two vectors (clamped)
  804. *
  805. * formula: from + s * (to - from)
  806. *
  807. * @param[in] from from value
  808. * @param[in] to to value
  809. * @param[in] t interpolant (amount) clamped between 0 and 1
  810. * @returns destination
  811. */
  812. CGLM_INLINE
  813. vec3s
  814. glms_vec3_(lerpc)(vec3s from, vec3s to, float t) {
  815. vec3s r;
  816. glm_vec3_lerpc(from.raw, to.raw, t, r.raw);
  817. return r;
  818. }
  819. /*!
  820. * @brief linear interpolation between two vectors
  821. *
  822. * formula: from + s * (to - from)
  823. *
  824. * @param[in] from from value
  825. * @param[in] to to value
  826. * @param[in] t interpolant (amount)
  827. * @returns destination
  828. */
  829. CGLM_INLINE
  830. vec3s
  831. glms_vec3_(mix)(vec3s from, vec3s to, float t) {
  832. vec3s r;
  833. glm_vec3_mix(from.raw, to.raw, t, r.raw);
  834. return r;
  835. }
  836. /*!
  837. * @brief linear interpolation between two vectors (clamped)
  838. *
  839. * formula: from + s * (to - from)
  840. *
  841. * @param[in] from from value
  842. * @param[in] to to value
  843. * @param[in] t interpolant (amount) clamped between 0 and 1
  844. * @returns destination
  845. */
  846. CGLM_INLINE
  847. vec3s
  848. glms_vec3_(mixc)(vec3s from, vec3s to, float t) {
  849. vec3s r;
  850. glm_vec3_mixc(from.raw, to.raw, t, r.raw);
  851. return r;
  852. }
  853. /*!
  854. * @brief threshold function
  855. *
  856. * @param[in] edge threshold
  857. * @param[in] x value to test against threshold
  858. * @returns 0.0 if x < edge, else 1.0
  859. */
  860. CGLM_INLINE
  861. vec3s
  862. glms_vec3_(step)(vec3s edge, vec3s x) {
  863. vec3s r;
  864. glm_vec3_step(edge.raw, x.raw, r.raw);
  865. return r;
  866. }
  867. /*!
  868. * @brief threshold function with a smooth transition (unidimensional)
  869. *
  870. * @param[in] edge0 low threshold
  871. * @param[in] edge1 high threshold
  872. * @param[in] x value to test against threshold
  873. * @returns destination
  874. */
  875. CGLM_INLINE
  876. vec3s
  877. glms_vec3_(smoothstep_uni)(float edge0, float edge1, vec3s x) {
  878. vec3s r;
  879. glm_vec3_smoothstep_uni(edge0, edge1, x.raw, r.raw);
  880. return r;
  881. }
  882. /*!
  883. * @brief threshold function with a smooth transition
  884. *
  885. * @param[in] edge0 low threshold
  886. * @param[in] edge1 high threshold
  887. * @param[in] x value to test against threshold
  888. * @returns destination
  889. */
  890. CGLM_INLINE
  891. vec3s
  892. glms_vec3_(smoothstep)(vec3s edge0, vec3s edge1, vec3s x) {
  893. vec3s r;
  894. glm_vec3_smoothstep(edge0.raw, edge1.raw, x.raw, r.raw);
  895. return r;
  896. }
  897. /*!
  898. * @brief smooth Hermite interpolation between two vectors
  899. *
  900. * formula: from + s * (to - from)
  901. *
  902. * @param[in] from from value
  903. * @param[in] to to value
  904. * @param[in] t interpolant (amount)
  905. * @returns destination
  906. */
  907. CGLM_INLINE
  908. vec3s
  909. glms_vec3_(smoothinterp)(vec3s from, vec3s to, float t) {
  910. vec3s r;
  911. glm_vec3_smoothinterp(from.raw, to.raw, t, r.raw);
  912. return r;
  913. }
  914. /*!
  915. * @brief smooth Hermite interpolation between two vectors (clamped)
  916. *
  917. * formula: from + s * (to - from)
  918. *
  919. * @param[in] from from value
  920. * @param[in] to to value
  921. * @param[in] t interpolant (amount) clamped between 0 and 1
  922. * @returns destination
  923. */
  924. CGLM_INLINE
  925. vec3s
  926. glms_vec3_(smoothinterpc)(vec3s from, vec3s to, float t) {
  927. vec3s r;
  928. glm_vec3_smoothinterpc(from.raw, to.raw, t, r.raw);
  929. return r;
  930. }
  931. /*!
  932. * @brief vec3 cross product
  933. *
  934. * this is just convenient wrapper
  935. *
  936. * @param[in] a source 1
  937. * @param[in] b source 2
  938. * @returns destination
  939. */
  940. CGLM_INLINE
  941. vec3s
  942. glms_cross(vec3s a, vec3s b) {
  943. vec3s r;
  944. glm_cross(a.raw, b.raw, r.raw);
  945. return r;
  946. }
  947. /*!
  948. * @brief vec3 dot product
  949. *
  950. * this is just convenient wrapper
  951. *
  952. * @param[in] a vector1
  953. * @param[in] b vector2
  954. * @return dot product
  955. */
  956. CGLM_INLINE
  957. float
  958. glms_dot(vec3s a, vec3s b) {
  959. return glm_dot(a.raw, b.raw);
  960. }
  961. /*!
  962. * @brief normalize vec3 and store result in same vec
  963. *
  964. * this is just convenient wrapper
  965. *
  966. * @param[in] v vector
  967. * @returns normalized vector
  968. */
  969. CGLM_INLINE
  970. vec3s
  971. glms_normalize(vec3s v) {
  972. glm_normalize(v.raw);
  973. return v;
  974. }
  975. /*!
  976. * @brief swizzle vector components
  977. *
  978. * you can use existing masks e.g. GLM_XXX, GLM_ZYX
  979. *
  980. * @param[in] v source
  981. * @param[in] mask mask
  982. * @returns swizzled vector
  983. */
  984. CGLM_INLINE
  985. vec3s
  986. glms_vec3_(swizzle)(vec3s v, int mask) {
  987. vec3s dest;
  988. glm_vec3_swizzle(v.raw, mask, dest.raw);
  989. return dest;
  990. }
  991. /*!
  992. * @brief Create three dimensional vector from pointer
  993. *
  994. * @param[in] src pointer to an array of floats
  995. * @returns constructed 3D vector from raw pointer
  996. */
  997. CGLM_INLINE
  998. vec3s
  999. glms_vec3_(make)(const float * __restrict src) {
  1000. vec3s dest;
  1001. glm_vec3_make(src, dest.raw);
  1002. return dest;
  1003. }
  1004. /*!
  1005. * @brief a vector pointing in the same direction as another
  1006. *
  1007. * orients a vector to point away from a surface as defined by its normal
  1008. *
  1009. * @param[in] n vector to orient.
  1010. * @param[in] v incident vector
  1011. * @param[in] nref reference vector
  1012. * @returns oriented vector, pointing away from the surface.
  1013. */
  1014. CGLM_INLINE
  1015. vec3s
  1016. glms_vec3_(faceforward)(vec3s n, vec3s v, vec3s nref) {
  1017. vec3s dest;
  1018. glm_vec3_faceforward(n.raw, v.raw, nref.raw, dest.raw);
  1019. return dest;
  1020. }
  1021. /*!
  1022. * @brief reflection vector using an incident ray and a surface normal
  1023. *
  1024. * @param[in] I incident vector
  1025. * @param[in] N normalized normal vector
  1026. * @returns reflection result
  1027. */
  1028. CGLM_INLINE
  1029. vec3s
  1030. glms_vec3_(reflect)(vec3s v, vec3s n) {
  1031. vec3s dest;
  1032. glm_vec3_reflect(v.raw, n.raw, dest.raw);
  1033. return dest;
  1034. }
  1035. /*!
  1036. * @brief computes refraction vector for an incident vector and a surface normal.
  1037. *
  1038. * calculates the refraction vector based on Snell's law. If total internal reflection
  1039. * occurs (angle too great given eta), dest is set to zero and returns false.
  1040. * Otherwise, computes refraction vector, stores it in dest, and returns true.
  1041. *
  1042. * @param[in] v normalized incident vector
  1043. * @param[in] n normalized normal vector
  1044. * @param[in] eta ratio of indices of refraction (incident/transmitted)
  1045. * @param[out] dest refraction vector if refraction occurs; zero vector otherwise
  1046. *
  1047. * @returns true if refraction occurs; false if total internal reflection occurs.
  1048. */
  1049. CGLM_INLINE
  1050. bool
  1051. glms_vec3_(refract)(vec3s v, vec3s n, float eta, vec3s * __restrict dest) {
  1052. return glm_vec3_refract(v.raw, n.raw, eta, dest->raw);
  1053. }
  1054. #endif /* cglms_vec3s_h */