c3dlas.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <float.h>
  4. #include <math.h>
  5. #include <limits.h>
  6. #include <float.h>
  7. #include <x86intrin.h>
  8. #include "c3dlas.h"
  9. #ifdef C3DLAS_USE_BUILTINS
  10. #define abs_double __builtin_fabs
  11. #define abs_float __builtin_fabsf
  12. #else
  13. #define abs_double fabs
  14. #define abs_float fabsf
  15. #endif
  16. #ifdef C3DLAS_NO_TGMATH
  17. // requires GCC probably
  18. /*
  19. #define FD_CHOOSE_1(a, b, fn_f, fn_d)\
  20. __builtin_choose_expr( \
  21. __builtin_types_compatible_p(__typeof__(a), double), \
  22. fn_d(a), \
  23. fn_f(a))
  24. #define FD_CHOOSE_2(a, b, fn_f, fn_d)\
  25. __builtin_choose_expr( \
  26. __builtin_types_compatible_p(__typeof__(a), double) || __builtin_types_compatible_p(__typeof__(b), double), \
  27. fn_d(a, b), \
  28. fn_f(a, b))
  29. #define fmax(a,b) FD_CHOOSE_2(a, b, fmaxf, fmax)
  30. #define fmin(a,b) FD_CHOOSE_2(a, b, fminf, fmin)
  31. #define fabs(a) FD_CHOOSE_1(a, fabsf, fabs)
  32. #define sqrt(a) FD_CHOOSE_1(a, sqrtf, sqrt)
  33. */
  34. #else
  35. #include <tgmath.h>
  36. #endif
  37. #ifndef _GNU_SOURCE
  38. static inline void sincosf(float x, float* s, float* c) {
  39. *s = sinf(x);
  40. *c = cosf(x);
  41. }
  42. #endif
  43. // utilities
  44. // reverses the argument
  45. uint32_t bitReverse32(uint32_t x) {
  46. x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
  47. x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
  48. x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
  49. x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
  50. return ((x >> 16) | (x << 16));
  51. }
  52. // reverses the least significant (len) bits, zeroing the top
  53. uint32_t reverseBits(uint32_t n, int len) {
  54. uint32_t rn = bitReverse32(n);
  55. return rn >> (32 - len);
  56. }
  57. // random numbers
  58. // returns a random number in (-1, 1) uninclusive
  59. // Thanks to Kaslai (https://github.com/Aslai) for fixing a nasty bug in the previous version
  60. float pcg_f(uint64_t* state, uint64_t stream) {
  61. union {
  62. uint32_t fu;
  63. float ff;
  64. } u;
  65. uint64_t last = *state;
  66. *state = (last * 6364136223846793005ULL) + (stream | 1);
  67. uint32_t xs = ((last >> 18) ^ last) >> 27;
  68. uint32_t rot = last >> 59;
  69. uint32_t fin = (xs >> rot) | (xs << ((-rot) & 31));
  70. uint32_t exp = (fin & 0x3F800000);
  71. exp = (0x7F + 33 - __builtin_clzl(exp)) << 23;
  72. u.fu = ((fin) & 0x807fffff) | exp;
  73. return u.ff;
  74. }
  75. // returns a random number in [0, UINT32_MAX] inclusive
  76. uint32_t pcg_u32(uint64_t* state, uint64_t stream) {
  77. uint64_t last = *state;
  78. *state = (last * 6364136223846793005ULL) + (stream | 1);
  79. uint32_t xs = ((last >> 18) ^ last) >> 27;
  80. uint32_t rot = last >> 59;
  81. uint32_t fin = (xs >> rot) | (xs << ((-rot) & 31));
  82. return fin;
  83. }
  84. // BUG: totally untested
  85. // SIMD and C versions do not return the same values.
  86. void pcg_f8(uint64_t* state, uint64_t stream, float* out) {
  87. #if defined(C3DLAS_USE_SIMD)
  88. __m256i s1, s2, xs1, xs2, xs, r, nra, q, f;
  89. s1 = _mm256_add_epi64(_mm256_set1_epi64x(*state), _mm256_set_epi64x(1,2,3,4));
  90. s2 = _mm256_add_epi64(_mm256_set1_epi64x(*state), _mm256_set_epi64x(5,6,7,8));
  91. // cycle the state
  92. *state = (*state * 6364136223846793005ULL) + (stream | 1);
  93. xs1 = _mm256_srli_epi64(_mm256_xor_si256(_mm256_srli_epi64(s1, 18), s1), 27);
  94. xs2 = _mm256_srli_epi64(_mm256_xor_si256(_mm256_srli_epi64(s2, 18), s2), 27);
  95. xs = _mm256_unpacklo_epi32(xs1, xs2);
  96. r = _mm256_srai_epi32(xs, 59);
  97. nra = _mm256_and_si256(_mm256_sign_epi32(r, _mm256_set1_epi32(-1)), _mm256_set1_epi32(31));
  98. q = _mm256_or_si256(_mm256_srav_epi32(xs, r), _mm256_sllv_epi32(xs, nra));
  99. // q is full of random 32bit integers now
  100. // convert to (-1, 1) floats by jamming in some exponent info
  101. f = _mm256_or_si256(_mm256_and_si256(q, _mm256_set1_epi32(0x807fffff)), _mm256_set1_epi32(0x3f000000));
  102. _mm256_storeu_si256((__m256i*)out, f);
  103. #else
  104. out[0] = pcg_f(state, stream);
  105. out[1] = pcg_f(state, stream);
  106. out[2] = pcg_f(state, stream);
  107. out[3] = pcg_f(state, stream);
  108. out[4] = pcg_f(state, stream);
  109. out[5] = pcg_f(state, stream);
  110. out[6] = pcg_f(state, stream);
  111. out[7] = pcg_f(state, stream);
  112. #endif
  113. }
  114. float frandPCG(float low, float high, PCG* pcg) {
  115. return low + ((high - low) * (pcg_f(&pcg->state, pcg->stream) * 0.5 + 0.5));
  116. }
  117. uint32_t urandPCG(uint32_t low, uint32_t high, PCG* pcg) {
  118. return pcg_u32(&pcg->state, pcg->stream) % (high - low) + low;
  119. }
  120. #define FN(sz, suf, ty, ft, sufft, pref, ...) \
  121. \
  122. int vEqExact##suf(const Vector##suf a, const Vector##suf b) { \
  123. return vEqExact##suf##p(&a, &b); \
  124. } \
  125. int vEqExact##suf##p(const Vector##suf const * a, const Vector##suf const * b) { \
  126. int tmp = 0; \
  127. for(int i = 0; i < sz; i++) \
  128. tmp += ((ty*)a)[i] == ((ty*)b)[i]; \
  129. return tmp == sz; \
  130. } \
  131. \
  132. int vEq##suf(const Vector##suf a, const Vector##suf b) { \
  133. return vEqEp##suf(a, b, pref##_CMP_EPSILON); \
  134. } \
  135. int vEq##suf##p(const Vector##suf* a, const Vector##suf* b) { \
  136. return vEqEp##suf(*a, *b, pref##_CMP_EPSILON); \
  137. } \
  138. \
  139. int vEqEp##suf(const Vector##suf a, const Vector##suf b, ft epsilon) { \
  140. return vEqEp##suf##p(&a, &b, epsilon); \
  141. } \
  142. int vEqEp##suf##p(const Vector##suf* a, const Vector##suf* b, ft epsilon) { \
  143. return vDistSq##suf(*a, *b) <= epsilon * epsilon; \
  144. } \
  145. \
  146. ft vDistSq##suf(const Vector##suf a, const Vector##suf b) { \
  147. return vDistSq##suf##p(&a, &b); \
  148. } \
  149. ft vDistSq##suf##p(const Vector##suf* a, const Vector##suf* b) { \
  150. ft tmp = 0; \
  151. for(int i = 0; i < sz; i++) { \
  152. ft q = ((ty*)a)[i] - ((ty*)b)[i]; \
  153. tmp += q * q; \
  154. } \
  155. return tmp;\
  156. } \
  157. ft vDist##suf(const Vector##suf a, const Vector##suf b) { \
  158. return sqrt(vDistSq##suf##p(&a, &b)); \
  159. } \
  160. ft vDist##suf##p(const Vector##suf* a, const Vector##suf* b) { \
  161. return sqrt(vDistSq##suf##p(a, b)); \
  162. } \
  163. \
  164. Vector##suf vAdd##suf(const Vector##suf a, const Vector##suf b) { \
  165. Vector##suf out; \
  166. vAdd##suf##p(&a, &b, &out); \
  167. return out; \
  168. } \
  169. void vAdd##suf##p(const Vector##suf* a, const Vector##suf* b, Vector##suf* out) { \
  170. for(int i = 0; i < sz; i++) { \
  171. ((ty*)out)[i] = ((ty*)a)[i] + ((ty*)b)[i]; \
  172. } \
  173. } \
  174. \
  175. Vector##suf vSub##suf(const Vector##suf a, const Vector##suf b) { \
  176. Vector##suf out; \
  177. vSub##suf##p(&a, &b, &out); \
  178. return out; \
  179. } \
  180. void vSub##suf##p(const Vector##suf const * a, const Vector##suf const * b, Vector##suf* out) { \
  181. for(int i = 0; i < sz; i++) { \
  182. ((ty*)out)[i] = ((ty*)a)[i] - ((ty*)b)[i]; \
  183. } \
  184. } \
  185. \
  186. Vector##suf vMul##suf(const Vector##suf a, const Vector##suf b) { \
  187. Vector##suf out; \
  188. vMul##suf##p(&a, &b, &out); \
  189. return out; \
  190. } \
  191. void vMul##suf##p(const Vector##suf const * a, const Vector##suf const * b, Vector##suf* out) { \
  192. for(int i = 0; i < sz; i++) { \
  193. ((ty*)out)[i] = ((ty*)a)[i] * ((ty*)b)[i]; \
  194. } \
  195. } \
  196. \
  197. Vector##suf vDiv##suf(const Vector##suf top, const Vector##suf bot) { \
  198. Vector##suf out; \
  199. vDiv##suf##p(&top, &bot, &out); \
  200. return out; \
  201. } \
  202. void vDiv##suf##p(const Vector##suf const * top, const Vector##suf const * bot, Vector##suf* out) { \
  203. for(int i = 0; i < sz; i++) { \
  204. ((ty*)out)[i] = ((ty*)top)[i] / ((ty*)bot)[i]; \
  205. } \
  206. } \
  207. \
  208. ft vDot##suf(const Vector##suf a, const Vector##suf b) { \
  209. return vDot##suf##p(&a, &b); \
  210. } \
  211. ft vDot##suf##p(const Vector##suf* a, const Vector##suf* b) { \
  212. ft tmp = 0; \
  213. for(int i = 0; i < sz; i++) { \
  214. tmp += ((ty*)a)[i] * ((ty*)b)[i]; \
  215. } \
  216. return tmp;\
  217. } \
  218. \
  219. Vector##sufft vScale##suf(const Vector##suf v, ft scalar) { \
  220. Vector##sufft out; \
  221. vScale##suf##p(&v, scalar, &out); \
  222. return out; \
  223. } \
  224. void vScale##suf##p(const Vector##suf* v, ft scalar, Vector##sufft* out) { \
  225. for(int i = 0; i < sz; i++) \
  226. ((ft*)out)[i] = (ft)((ty*)v)[i] * scalar; \
  227. } \
  228. \
  229. \
  230. Vector##sufft vAvg##suf(const Vector##suf a, const Vector##suf b) { \
  231. Vector##sufft out; \
  232. vAvg##suf##p(&a, &b, &out); \
  233. return out; \
  234. } \
  235. void vAvg##suf##p(const Vector##suf* a, const Vector##suf* b, Vector##sufft* out) { \
  236. for(int i = 0; i < sz; i++) { \
  237. ((ty*)out)[i] = (((ty*)a)[i] + ((ty*)b)[i]) / (ft)2.0; \
  238. } \
  239. } \
  240. \
  241. Vector##suf vNeg##suf(const Vector##suf v) { \
  242. Vector##suf out; \
  243. vNeg##suf##p(&v, &out); \
  244. return out; \
  245. } \
  246. void vNeg##suf##p(const Vector##suf* v, Vector##suf* out) { \
  247. for(int i = 0; i < sz; i++) \
  248. ((ty*)out)[i] = -((ty*)v)[i]; \
  249. } \
  250. \
  251. Vector##sufft vLerp##suf(const Vector##suf a, const Vector##suf b, ft t) { \
  252. Vector##sufft out; \
  253. vLerp##suf##p(&a, &b, t, &out); \
  254. return out; \
  255. } \
  256. void vLerp##suf##p(const Vector##suf* a, const Vector##suf* b, ft t, Vector##sufft* out) { \
  257. for(int i = 0; i < sz; i++) \
  258. ((ft*)out)[i] = (ft)((ty*)a)[i] + ((ft)(((ty*)b)[i] - ((ty*)a)[i]) * t) ; \
  259. } \
  260. \
  261. Vector##sufft vInv##suf(const Vector##suf v) { \
  262. Vector##sufft out; \
  263. vInv##suf##p(&v, &out); \
  264. return out; \
  265. } \
  266. void vInv##suf##p(const Vector##suf* v, Vector##sufft* out) { \
  267. for(int i = 0; i < sz; i++) \
  268. ((ft*)out)[i] = (((ty*)v)[i] == 0) ? pref##_MAX : ((ft)1.0 / (ft)((ty*)v)[i]); \
  269. } \
  270. \
  271. ft vLen##suf(const Vector##suf v) { \
  272. return vLen##suf##p(&v); \
  273. } \
  274. ft vLen##suf##p(const Vector##suf* v) { \
  275. ft tmp = 0.0; \
  276. for(int i = 0; i < sz; i++) \
  277. tmp += (ft)((ty*)v)[i] * (ft)((ty*)v)[i]; \
  278. return sqrt(tmp); \
  279. } \
  280. \
  281. ft vLenSq##suf(const Vector##suf v) { \
  282. return vLenSq##suf##p(&v); \
  283. } \
  284. ft vLenSq##suf##p(const Vector##suf* v) { \
  285. return vDot##suf##p(v, v); \
  286. } \
  287. \
  288. ft vMag##suf(const Vector##suf v) { \
  289. return vLen##suf##p(&v); \
  290. } \
  291. ft vMag##suf##p(const Vector##suf* v) { \
  292. return vLen##suf##p(v); \
  293. } \
  294. \
  295. ft vInvLen##suf(const Vector##suf v) { \
  296. ft tmp = vLen##suf(v); \
  297. return tmp == 0 ? pref##_MAX : (ft)1.0 / tmp; \
  298. } \
  299. ft vInvLen##suf##p(const Vector##suf* v) { \
  300. return vInvLen##suf(*v); \
  301. } \
  302. \
  303. Vector##sufft vNorm##suf(const Vector##suf v) { \
  304. Vector##sufft out; \
  305. vNorm##suf##p(&v, &out); \
  306. return out; \
  307. } \
  308. void vNorm##suf##p(const Vector##suf* v, Vector##sufft* out) { \
  309. ft n = vLenSq##suf(*v); \
  310. \
  311. if(n >= (ft)1.0f - pref##_CMP_EPSILON && n <= (ft)1.0 + pref##_CMP_EPSILON) { \
  312. for(int i = 0; i < sz; i++) \
  313. ((ft*)out)[i] = (ft)((ty*)v)[i]; \
  314. return; \
  315. } \
  316. else if(n == 0.0) { \
  317. for(int i = 0; i < sz; i++) \
  318. ((ft*)out)[i] = 0; \
  319. return; \
  320. } \
  321. \
  322. n = (ft)1.0 / sqrt(n); \
  323. for(int i = 0; i < sz; i++) \
  324. ((ft*)out)[i] = (ft)((ty*)v)[i] * n; \
  325. } \
  326. \
  327. Vector##sufft vUnit##suf(const Vector##suf v) { \
  328. return vNorm##suf(v); \
  329. } \
  330. void vUnit##suf##p(const Vector##suf* v, Vector##sufft* out) { \
  331. return vNorm##suf##p(v, out); \
  332. } \
  333. C3DLAS_VECTOR_LIST(FN)
  334. #undef FN
  335. // swap two vectors
  336. void vSwap2ip(Vector2i* a, Vector2i* b) {
  337. Vector2i t;
  338. t = *b;
  339. *b = *a;
  340. *a = t;
  341. }
  342. void vSwap2p(Vector2* a, Vector2* b) {
  343. Vector2 t;
  344. t = *b;
  345. *b = *a;
  346. *a = t;
  347. }
  348. void vSwap3p(Vector3* a, Vector3* b) {
  349. Vector3 t;
  350. t = *b;
  351. *b = *a;
  352. *a = t;
  353. }
  354. void vSwap4p(Vector4* a, Vector4* b) {
  355. Vector4 t;
  356. t = *b;
  357. *b = *a;
  358. *a = t;
  359. }
  360. // scalar muliplication
  361. // Dot product (inner product)
  362. // Cross product: out = a x b
  363. // Cross products only exist in 3 and 7 dimensions
  364. Vector3 vCross3(Vector3 a, Vector3 b) {
  365. Vector3 out;
  366. vCross3p(&a, &b, &out);
  367. return out;
  368. }
  369. void vCross3p(Vector3* a, Vector3* b, Vector3* out) {
  370. out->x = (a->y * b->z) - (a->z * b->y);
  371. out->y = (a->z * b->x) - (a->x * b->z);
  372. out->z = (a->x * b->y) - (a->y * b->x);
  373. }
  374. // Scalar triple product: a . (b x c)
  375. float vScalarTriple3(Vector3 a, Vector3 b, Vector3 c) {
  376. return vScalarTriple3p(&a, &b, &c);
  377. }
  378. float vScalarTriple3p(Vector3* a, Vector3* b, Vector3* c) {
  379. return (float)((a->x * b->y * c->z) - (a->x * b->z * c->y) - (a->y * b->x * c->z)
  380. + (a->z * b->x * c->y) + (a->y * b->z * c->x) - (a->z * b->y * c->x));
  381. }
  382. // Vector Inverse. Returns FLT_MAX on div/0
  383. // Vector magnitude (length)
  384. // Squared distance from one point to another
  385. // Distance from one point to another
  386. // Vector normalize (scale to unit length)
  387. // vMin(a, b) Returns the minimum values of each component
  388. // vMin(a, b) Returns the maximum values of each component
  389. #define FN(sz, suf, t, maxval) \
  390. void vMin##sz##suf##p(const Vector##sz##suf* a, const Vector##sz##suf* b, Vector##sz##suf* out) { \
  391. for(int i = 0; i < sz; i++) \
  392. ((t*)out)[i] = fmin(((t*)a)[i], ((t*)b)[i]); \
  393. } \
  394. void vMax##sz##suf##p(const Vector##sz##suf* a, const Vector##sz##suf* b, Vector##sz##suf* out) { \
  395. for(int i = 0; i < sz; i++) \
  396. ((t*)out)[i] = fmax(((t*)a)[i], ((t*)b)[i]); \
  397. } \
  398. Vector##sz##suf vMin##sz##suf(Vector##sz##suf a, Vector##sz##suf b) { \
  399. Vector##sz##suf out; \
  400. vMin##sz##suf##p(&a, &b, &out); \
  401. return out; \
  402. } \
  403. Vector##sz##suf vMax##sz##suf(Vector##sz##suf a, Vector##sz##suf b) { \
  404. Vector##sz##suf out; \
  405. vMax##sz##suf##p(&a, &b, &out); \
  406. return out; \
  407. } \
  408. \
  409. int vMinComp##sz##suf##p(const Vector##sz##suf* a) { \
  410. t best = ((t*)a)[0]; \
  411. int best_ind = 0; \
  412. for(int i = 1; i < sz; i++) { \
  413. if(((t*)a)[i] < best) { \
  414. best = ((t*)a)[i]; \
  415. best_ind = i; \
  416. } \
  417. } \
  418. return best_ind; \
  419. } \
  420. \
  421. int vMaxComp##sz##suf##p(const Vector##sz##suf* a) { \
  422. t best = ((t*)a)[0]; \
  423. int best_ind = 0; \
  424. for(int i = 1; i < sz; i++) { \
  425. if(((t*)a)[i] > best) { \
  426. best = ((t*)a)[i]; \
  427. best_ind = i; \
  428. } \
  429. } \
  430. return best_ind; \
  431. } \
  432. \
  433. int vMinComp##sz##suf(const Vector##sz##suf a) { \
  434. return vMinComp##sz##suf##p(&a); \
  435. } \
  436. \
  437. int vMaxComp##sz##suf(const Vector##sz##suf a) { \
  438. return vMaxComp##sz##suf##p(&a); \
  439. } \
  440. \
  441. Vector##sz##suf vClamp##sz##suf(Vector##sz##suf in, Vector##sz##suf min, Vector##sz##suf max) { \
  442. Vector##sz##suf out; \
  443. for(int i = 0; i < sz; i++) \
  444. ((t*)&out)[i] = fmax(((t*)&min)[i], fmin(((t*)&in)[i], ((t*)&max)[i])); \
  445. return out; \
  446. } \
  447. Vector##sz##suf vAbs##sz##suf(const Vector##sz##suf v) { \
  448. Vector##sz##suf out; \
  449. vAbs##sz##suf##p(&v, &out); \
  450. return out; \
  451. } \
  452. void vAbs##sz##suf##p(const Vector##sz##suf* v, Vector##sz##suf* out) { \
  453. for(int i = 0; i < sz; i++) \
  454. ((t*)out)[i] = abs_##t( ((t*)v)[i] ); \
  455. } \
  456. Vector##sz##suf vSign##sz##suf(const Vector##sz##suf v) { \
  457. Vector##sz##suf out; \
  458. vSign##sz##suf##p(&v, &out); \
  459. return out; \
  460. } \
  461. void vSign##sz##suf##p(const Vector##sz##suf* v, Vector##sz##suf* out) { \
  462. for(int i = 0; i < sz; i++) \
  463. ((t*)out)[i] = copysign((t)1.0, ((t*)v)[i] ); \
  464. } \
  465. Vector##sz##suf vStep##sz##suf(const Vector##sz##suf edge, const Vector##sz##suf v) { \
  466. Vector##sz##suf out; \
  467. vStep##sz##suf##p(&edge, &v, &out); \
  468. return out; \
  469. } \
  470. void vStep##sz##suf##p(const Vector##sz##suf* edge, const Vector##sz##suf* v, Vector##sz##suf* out) { \
  471. for(int i = 0; i < sz; i++) \
  472. ((t*)out)[i] = ((t*)v)[i] < ((t*)edge)[i] ? 0.0 : 1.0; \
  473. } \
  474. FN(2, , float, FLT_MAX)
  475. FN(3, , float, FLT_MAX)
  476. FN(4, , float, FLT_MAX)
  477. FN(2, d, double, DBL_MAX)
  478. FN(3, d, double, DBL_MAX)
  479. FN(4, d, double, DBL_MAX)
  480. #undef FN
  481. #define FN(sz, suf, t, maxval) \
  482. void vMin##sz##suf##p(const Vector##sz##suf* a, const Vector##sz##suf* b, Vector##sz##suf* out) { \
  483. for(int i = 0; i < sz; i++) \
  484. ((t*)out)[i] = MIN(((t*)a)[i], ((t*)b)[i]); \
  485. } \
  486. void vMax##sz##suf##p(const Vector##sz##suf* a, const Vector##sz##suf* b, Vector##sz##suf* out) { \
  487. for(int i = 0; i < sz; i++) \
  488. ((t*)out)[i] = MAX(((t*)a)[i], ((t*)b)[i]); \
  489. } \
  490. Vector##sz##suf vMin##sz##suf(Vector##sz##suf a, Vector##sz##suf b) { \
  491. Vector##sz##suf out; \
  492. vMin##sz##suf##p(&a, &b, &out); \
  493. return out; \
  494. } \
  495. Vector##sz##suf vMax##sz##suf(Vector##sz##suf a, Vector##sz##suf b) { \
  496. Vector##sz##suf out; \
  497. vMax##sz##suf##p(&a, &b, &out); \
  498. return out; \
  499. } \
  500. Vector##sz##suf vClamp##sz##suf(Vector##sz##suf in, Vector##sz##suf min, Vector##sz##suf max) { \
  501. Vector##sz##suf out; \
  502. for(int i = 0; i < sz; i++) \
  503. ((t*)&out)[i] = MAX(((t*)&min)[i], MIN(((t*)&in)[i], ((t*)&max)[i])); \
  504. return out; \
  505. } \
  506. Vector##sz##suf vAbs##sz##suf(const Vector##sz##suf v) { \
  507. Vector##sz##suf out; \
  508. vAbs##sz##suf##p(&v, &out); \
  509. return out; \
  510. } \
  511. void vAbs##sz##suf##p(const Vector##sz##suf* v, Vector##sz##suf* out) { \
  512. for(int i = 0; i < sz; i++) \
  513. ((t*)out)[i] = labs( ((t*)v)[i] ); \
  514. } \
  515. Vector##sz##suf vSign##sz##suf(const Vector##sz##suf v) { \
  516. Vector##sz##suf out; \
  517. vSign##sz##suf##p(&v, &out); \
  518. return out; \
  519. } \
  520. void vSign##sz##suf##p(const Vector##sz##suf* v, Vector##sz##suf* out) { \
  521. for(int i = 0; i < sz; i++) \
  522. ((t*)out)[i] = ((t*)v)[i] < 0 ? -1 : 1; \
  523. } \
  524. Vector##sz##suf vStep##sz##suf(const Vector##sz##suf edge, const Vector##sz##suf v) { \
  525. Vector##sz##suf out; \
  526. vStep##sz##suf##p(&edge, &v, &out); \
  527. return out; \
  528. } \
  529. void vStep##sz##suf##p(const Vector##sz##suf* edge, const Vector##sz##suf* v, Vector##sz##suf* out) { \
  530. for(int i = 0; i < sz; i++) \
  531. ((t*)out)[i] = ((t*)v)[i] < ((t*)edge)[i] ? 0.0 : 1.0; \
  532. } \
  533. FN(2, i, int, DBL_MAX)
  534. FN(3, i, int, DBL_MAX)
  535. FN(4, i, int, DBL_MAX)
  536. FN(2, l, long, DBL_MAX)
  537. FN(3, l, long, DBL_MAX)
  538. FN(4, l, long, DBL_MAX)
  539. #undef FN
  540. // Returns an arbitrary unit vector perpendicular to the input
  541. // The input vector does not need to be normalized
  542. void vPerp3p(Vector3* n, Vector3* out) {
  543. *out = vPerp3(*n);
  544. }
  545. Vector3 vPerp3(Vector3 n) {
  546. float f, d;
  547. float absx = fabs(n.x);
  548. float absy = fabs(n.y);
  549. if(absx < absy) {
  550. if(n.x < n.z) goto MIN_X;
  551. goto MIN_Z;
  552. }
  553. if(absy < fabs(n.z)) goto MIN_Y;
  554. goto MIN_Z;
  555. MIN_X:
  556. d = 1.0f / sqrtf(n.z * n.z + n.y * n.y);
  557. f = n.z;
  558. n.z = n.y * d;
  559. n.y = -f * d;
  560. n.x = 0;
  561. return n;
  562. MIN_Y:
  563. d = 1.0f / sqrtf(n.z * n.z + n.x * n.x);
  564. f = n.x;
  565. n.x = n.z * d;
  566. n.z = -f * d;
  567. n.y = 0;
  568. return n;
  569. MIN_Z:
  570. d = 1.0f / sqrtf(n.x * n.x + n.y * n.y);
  571. f = n.y;
  572. n.y = n.x * d;
  573. n.x = -f * d;
  574. n.z = 0;
  575. return n;
  576. }
  577. // Returns an arbitrary unit vector perpendicular to the input
  578. // The input vector does not need to be normalized
  579. void vPerp2p(Vector2* n, Vector2* out) {
  580. *out = vPerp2(*n);
  581. }
  582. Vector2 vPerp2(Vector2 n) {
  583. return vNorm2((Vector2){.x = -n.y, .y = n.x});
  584. }
  585. // Coordinate system conversions
  586. // Does not check for degenerate vectors
  587. // Cartesian to Spherical
  588. Vector3 vC2S3(Vector3 cart) {
  589. Vector3 sp;
  590. sp.rho = vMag3(cart);
  591. sp.theta = atan2f(cart.x, cart.y);
  592. sp.phi = acosf(cart.z / sp.rho);
  593. return sp;
  594. }
  595. // Spherical to Cartesian
  596. Vector3 vS2C3(Vector3 s) {
  597. float st, ct, sp, cp;
  598. // as of July 2022, gcc trunk is smart enough to automatically optimize to sincos, but clang isn't.
  599. sincosf(s.phi, &sp, &cp);
  600. sincosf(s.theta, &st, &ct);
  601. return (Vector3){
  602. .x = s.rho * sp * ct,
  603. .y = s.rho * sp * st,
  604. .z = s.rho * cp
  605. };
  606. }
  607. Vector3 closestPointToRay3(Vector3 p, Ray3 r) {
  608. Vector3 po = vSub3(p, r.o); // vector from the starting point to p
  609. float t = vDot3(po, r.d); // project the pa onto the ray direction
  610. fclamp(t, 0.0, 1.0); // clamp t to between the endpoints of the line segment
  611. return vSub3(po, vScale3(r.d, t));
  612. }
  613. // completely untested.
  614. // can probably be optimized
  615. // This function is poorly named. It is designed to check if a bounding sphere intersects a cone surrounding a viewing frustum.
  616. int distanceSphereToCone(Vector3 spc, float spr, Vector3 c1, Vector3 c2, float cr1, float cr2) {
  617. Vector3 cnorm = vNorm(vSub(c2, c1)); // normal pointing down the center of the cone
  618. Vector3 sp_c1 = vSub(spc, c1); // vector pointing from c1 to the sphere center
  619. Vector3 up = vCross3(spc, cnorm); // vector perpendicular to the plane containing the cone's centerline and the sphere center.
  620. Vector3 perp_toward_sp = vNorm(vCross3(cnorm, up)); // vector perpendicular to the cone's centerline within the plane, towards the sphere
  621. Vector3 outer_c1 = vAdd(c1, vScale(perp_toward_sp, cr1)); // point in the plane on the outer edge of the cone
  622. Vector3 outer_c2 = vAdd(c2, vScale(perp_toward_sp, cr2)); // point in the plane on the outer edge of the cone
  623. Vector3 closest = closestPointToRay3(spc, (Ray3){.o = outer_c1, .d = vNorm(vSub(outer_c2, outer_c1))}); // point on the cone closest to the sphere
  624. // this part is probably wrong
  625. if(vDot(perp_toward_sp, vSub(spc, closest)) < 0) return 1; // is the sphere center inside the cone?
  626. return (vDist(closest, spc) - spr) <= 0;
  627. }
  628. // Muchas gracias, Inigo.
  629. // https://iquilezles.org/articles/distfunctions2d/
  630. float vDistPointLine2(Vector2 p, Line2 ls) {
  631. Vector2 pa = vSub2(p, ls.start); // vector from the starting point to p
  632. Vector2 ba = vSub2(ls.end, ls.start); // vector from the starting point to the ending point
  633. float t = vDot2(pa, ba) / vDot2(ba, ba); // project the pa onto ba, then divide that distance by the length of ba to normalize it
  634. fclamp(t, 0.0, 1.0); // clamp t to between the endpoints of the line segment
  635. // Consider the starting point to be at the origin, for ease of visualization.
  636. // ba is the vector from the origin to the endpoint og the line that now passes through the origin.
  637. // Scaling ba by t gives the intercept point of the line through p that is perpendicular to the test line segment.
  638. // pa is p if a was the origin. Therefore, pi is the vector from p to the intercept point on the test line segment.
  639. Vector2 pi = vSub2(pa, vScale2(ba, t));
  640. return vMag2(pi); // the answer is the length of pi
  641. }
  642. float vDistPointLine3(Vector3 p, Line3 ls) {
  643. Vector3 pa = vSub3(p, ls.start);
  644. Vector3 ba = vSub3(ls.end, ls.start);
  645. float t = fclamp(vDot3(pa, ba) / vDot3(ba, ba), 0.0, 1.0);
  646. return vMag3(vSub3(pa, vScale3(ba, t)));
  647. }
  648. // This version also returns the normalized distance along the line of the closest point
  649. float vDistTPointLine2(Vector2 p, Line2 ls, float* T) {
  650. Vector2 pa = vSub2(p, ls.start);
  651. Vector2 ba = vSub2(ls.end, ls.start);
  652. float t = fclamp(vDot2(pa, ba) / vDot2(ba, ba), 0.0, 1.0);
  653. if(T) *T = t;
  654. return vMag2(vSub2(pa, vScale2(ba, t)));
  655. }
  656. float vDistTPointLine3(Vector3 p, Line3 ls, float* T) {
  657. Vector3 pa = vSub3(p, ls.start);
  658. Vector3 ba = vSub3(ls.end, ls.start);
  659. float t = fclamp(vDot3(pa, ba) / vDot3(ba, ba), 0.0, 1.0);
  660. if(T) *T = t;
  661. return vMag3(vSub3(pa, vScale3(ba, t)));
  662. }
  663. // ----
  664. float projPointLine2(Vector2 p, Line2 ls) {
  665. Vector2 pa = vSub2(p, ls.start);
  666. Vector2 ba = vSub2(ls.end, ls.start);
  667. return vDot2(pa, ba) / vDot2(ba, ba);
  668. }
  669. float distTPointRay3(Vector3 p, Ray3 r, float* T) {
  670. Vector3 pa = vSub3(p, r.o);
  671. Vector3 ba = vNeg3(r.d);// vSub3(ls.end, ls.start);
  672. float t = vDot3(pa, ba) / vDot3(ba, ba);
  673. if(T) *T = t;
  674. return vLen3(vSub3(pa, vScale3(ba, t)));
  675. }
  676. float dist2TPointRay3(Vector3 p, Ray3 r, float* T) {
  677. Vector3 pa = vSub3(p, r.o);
  678. Vector3 ba = vNeg3(r.d);// vSub3(ls.end, ls.start);
  679. float t = vDot3(pa, ba) / vDot3(ba, ba);
  680. if(T) *T = t;
  681. return vLenSq3(vSub3(pa, vScale3(ba, t)));
  682. }
  683. int vInsidePolygon(Vector2 p, Polygon* poly) {
  684. int inside = 0;
  685. int cnt = poly->pointCount;
  686. if(poly->maxRadiusSq < vDot2(poly->centroid, p)) return 0;
  687. for(int i = 0; i < cnt; i++) {
  688. Vector2 a = poly->points[i];
  689. Vector2 b = poly->points[(i + 1) % cnt];
  690. if(a.y == b.y) continue; // horizontal edges are ignored
  691. // we're testing a ray going to the right
  692. if(a.x < p.x && b.x < p.x) continue; // segment is entirely left of the point
  693. if(a.y >= p.y && b.y >= p.y) continue; // segment entirely above the point
  694. if(a.y < p.y && b.y < p.y) continue; // segment entirely below the point
  695. // segment is in the same vertical band as the point
  696. float sx = a.x + (b.x - a.x) * ((p.y - a.y) / (b.y - a.y));
  697. if(p.x > sx) continue;
  698. inside = !inside;
  699. }
  700. return inside;
  701. }
  702. // Muchas gracias, Inigo.
  703. // https://iquilezles.org/articles/distfunctions2d/
  704. float vDistPolygon(Vector2 p, Polygon* poly) {
  705. float d = vDot2(vSub2(p, poly->points[0]), vSub2(p, poly->points[0]));
  706. float s = 1.0;
  707. for(int i = 0, j = poly->pointCount - 1; i < poly->pointCount; j = i, i++) {
  708. Vector2 A = poly->points[i];
  709. Vector2 B = poly->points[j];
  710. Vector2 e = vSub2(B, A);
  711. Vector2 w = vSub2(p, A);
  712. Vector2 b = vSub2(w, vScale2(e, fclamp(vDot2(w, e) / vDot2(e, e), 0.0, 1.0)));
  713. d = fminf(d, vDot2(b, b));
  714. int c1 = p.y >= A.y;
  715. int c2 = p.y < B.y;
  716. int c3 = e.x * w.y > e.y * w.x;
  717. if((c1 && c2 && c3) || (!c1 && !c2 && !c3)) s *= -1.0;
  718. }
  719. return s * sqrtf(d);
  720. }
  721. // ----
  722. void polyCalcCentroid(Polygon* poly) {
  723. int cnt = poly->pointCount;
  724. Vector2 centroid = {0,0};
  725. for(int i = 0; i < cnt; i++) {
  726. Vector2 a = poly->points[i];
  727. centroid = vAdd2(centroid, a);
  728. }
  729. poly->centroid = vScale2(centroid, 1.0 / poly->pointCount);
  730. }
  731. void polyCalcRadiusSq(Polygon* poly) {
  732. int cnt = poly->pointCount;
  733. float d = 0;
  734. for(int i = 0; i < cnt; i++) {
  735. Vector2 a = poly->points[i];
  736. d = fmaxf(d, vDot2(poly->centroid, a));
  737. }
  738. poly->maxRadiusSq = d;
  739. }
  740. // feeding a zero vector into this will cause div/0 and you will deserve it
  741. void vProject3p(Vector3* what, Vector3* onto, Vector3* out) { // slower; onto may not be normalized
  742. float wdo = vDot3p(what, onto);
  743. float odo = vDot3p(onto, onto);
  744. vScale3p(onto, wdo / odo, out);
  745. }
  746. void vProjectNorm3p(Vector3* what, Vector3* onto, Vector3* out) { // faster; onto must be normalized
  747. float wdo = vDot3p(what, onto);
  748. vScale3p(onto, wdo, out);
  749. }
  750. void vRandomPCG2p(Vector2* end1, Vector2* end2, PCG* pcg, Vector2* out) {
  751. out->x = frandPCG(fminf(end1->x, end2->x), fmaxf(end1->x, end2->x), pcg);
  752. out->y = frandPCG(fminf(end1->y, end2->y), fmaxf(end1->y, end2->y), pcg);
  753. }
  754. Vector2 vRandomPCG2(Vector2 end1, Vector2 end2, PCG* pcg) {
  755. Vector2 o;
  756. vRandomPCG2p(&end1, &end2, pcg, &o);
  757. return o;
  758. }
  759. void vRandomNormPCG2p(PCG* pcg, Vector2* out) {
  760. float th = frandPCG(0, 2.0 * F_PI, pcg);
  761. float sth, cth;
  762. sincosf(th, &sth, &cth);
  763. out->x = cth;
  764. out->y = sth;
  765. }
  766. Vector2 vRandomNormPCG2(PCG* pcg) {
  767. Vector2 o;
  768. vRandomNormPCG2p(pcg, &o);
  769. return o;
  770. }
  771. void vRandomPCG3p(Vector3* end1, Vector3* end2, PCG* pcg, Vector3* out) {
  772. out->x = frandPCG(fminf(end1->x, end2->x), fmaxf(end1->x, end2->x), pcg);
  773. out->y = frandPCG(fminf(end1->y, end2->y), fmaxf(end1->y, end2->y), pcg);
  774. out->z = frandPCG(fminf(end1->z, end2->z), fmaxf(end1->z, end2->z), pcg);
  775. }
  776. Vector3 vRandomPCG3(Vector3 end1, Vector3 end2, PCG* pcg) {
  777. Vector3 o;
  778. vRandomPCG3p(&end1, &end2, pcg, &o);
  779. return o;
  780. }
  781. // This algorithm is uniformly distributed over the surface of a sphere. There is no clustering at the poles.
  782. void vRandomNormPCG3p(PCG* pcg, Vector3* out) {
  783. float u = frandPCG(-1.f, 1.f, pcg);
  784. float th = frandPCG(0.f, 2.f * F_PI, pcg);
  785. float q = sqrtf(1.f - u * u);
  786. float sth, cth;
  787. sincosf(th, &sth, &cth);
  788. out->x = u * cth;
  789. out->y = u * sth;
  790. out->z = u;
  791. }
  792. Vector3 vRandomNormPCG3(PCG* pcg) {
  793. Vector3 o;
  794. vRandomNormPCG3p(pcg, &o);
  795. return o;
  796. }
  797. void vRandom3p(Vector3* end1, Vector3* end2, Vector3* out) {
  798. out->x = frand(fminf(end1->x, end2->x), fmaxf(end1->x, end2->x));
  799. out->y = frand(fminf(end1->y, end2->y), fmaxf(end1->y, end2->y));
  800. out->z = frand(fminf(end1->z, end2->z), fmaxf(end1->z, end2->z));
  801. }
  802. Vector3 vRandom3(Vector3 end1, Vector3 end2) {
  803. return (Vector3){
  804. .x = frand(fminf(end1.x, end2.x), fmaxf(end1.x, end2.x)),
  805. .y = frand(fminf(end1.y, end2.y), fmaxf(end1.y, end2.y)),
  806. .z = frand(fminf(end1.z, end2.z), fmaxf(end1.z, end2.z))
  807. };
  808. }
  809. // Uniformly distributed around the unit sphere; ie, no clustering at the poles.
  810. Vector3 vRandomNorm3() {
  811. Vector3 out;
  812. vRandomNorm3p(&out);
  813. return out;
  814. }
  815. void vRandomNorm3p(Vector3* out) {
  816. float u = frand(-1.0, 1.0);
  817. float th = frand(0, 2.0 * F_PI);
  818. float q = sqrtf(1.0 - u * u);
  819. float sth, cth;
  820. sincosf(th, &sth, &cth);
  821. out->x = u * cth;
  822. out->y = u * sth;
  823. out->z = u;
  824. }
  825. Vector4i vFloor4(const Vector4 v) {
  826. return (Vector4i){.x = floorf(v.x), .y = floorf(v.y), .z = floorf(v.z), .w = floorf(v.w)};
  827. }
  828. Vector4i vCeil4(const Vector4 v) {
  829. return (Vector4i){.x = ceilf(v.x), .y = ceilf(v.y), .z = ceilf(v.z), .w = ceilf(v.w)};
  830. }
  831. Vector3i vFloor3(const Vector3 v) {
  832. return (Vector3i){.x = floorf(v.x), .y = floorf(v.y), .z = floorf(v.z)};
  833. }
  834. Vector3i vCeil3(const Vector3 v) {
  835. return (Vector3i){.x = ceilf(v.x), .y = ceilf(v.y), .z = ceilf(v.z)};
  836. }
  837. Vector2i vFloor2(const Vector2 v) {
  838. return (Vector2i){.x = floorf(v.x), .y = floorf(v.y)};
  839. }
  840. Vector2i vCeil2(const Vector2 v) {
  841. return (Vector2i){.x = ceilf(v.x), .y = ceilf(v.y)};
  842. }
  843. Vector4l vFloor4d(const Vector4d v) {
  844. return (Vector4l){.x = floor(v.x), .y = floor(v.y), .z = floor(v.z), .w = floor(v.w)};
  845. }
  846. Vector4l vCeil4d(const Vector4d v) {
  847. return (Vector4l){.x = ceil(v.x), .y = ceil(v.y), .z = ceil(v.z), .w = ceil(v.w)};
  848. }
  849. Vector3l vFloor3d(const Vector3d v) {
  850. return (Vector3l){.x = floor(v.x), .y = floor(v.y), .z = floor(v.z)};
  851. }
  852. Vector3l vCeil3d(const Vector3d v) {
  853. return (Vector3l){.x = ceil(v.x), .y = ceil(v.y), .z = ceil(v.z)};
  854. }
  855. Vector2l vFloor2d(const Vector2d v) {
  856. return (Vector2l){.x = floor(v.x), .y = floor(v.y)};
  857. }
  858. Vector2l vCeil2d(const Vector2d v) {
  859. return (Vector2l){.x = ceil(v.x), .y = ceil(v.y)};
  860. }
  861. Vector2 vModPositive2(Vector2 v, Vector2 m) {
  862. return (Vector2){
  863. .x = fmodf(fmodf(v.x, m.x) + m.x, m.x),
  864. .y = fmodf(fmodf(v.y, m.y) + m.y, m.y),
  865. };
  866. }
  867. Vector3 vModPositive3(Vector3 v, Vector3 m) {
  868. return (Vector3){
  869. .x = fmodf(fmodf(v.x, m.x) + m.x, m.x),
  870. .y = fmodf(fmodf(v.y, m.y) + m.y, m.y),
  871. .z = fmodf(fmodf(v.z, m.z) + m.z, m.z),
  872. };
  873. }
  874. Vector4 vModPositive4(Vector4 v, Vector4 m) {
  875. return (Vector4){
  876. .x = fmodf(fmodf(v.x, m.x) + m.x, m.x),
  877. .y = fmodf(fmodf(v.y, m.y) + m.y, m.y),
  878. .z = fmodf(fmodf(v.z, m.z) + m.z, m.z),
  879. .w = fmodf(fmodf(v.w, m.w) + m.w, m.w),
  880. };
  881. }
  882. // reflects the distance from v to pivot across pivot.
  883. // out, pivot, and v will all be in the same plane, with pivot half way between v and out
  884. void vReflectAcross3p(Vector3* v, Vector3* pivot, Vector3* out) {
  885. Vector3 v2 = vScale3(*v, -1);
  886. float d = vDot3(v2, *pivot) * 2.0;
  887. *out = vSub3(v2, vScale3(*pivot, d));
  888. }
  889. Vector3 vReflectAcross3(Vector3 v, Vector3 pivot) {
  890. Vector3 o;
  891. vReflectAcross3p(&v, &pivot, &o);
  892. return o;
  893. }
  894. // calculate a unit vector normal to a triangle's face.
  895. void vTriFaceNormal3p(Vector3* a, Vector3* b, Vector3* c, Vector3* out) {
  896. Vector3 b_a, c_a;
  897. vSub3p(b, a, &b_a);
  898. vSub3p(c, a, &c_a);
  899. vCross3p(&b_a, &c_a, out);
  900. vNorm3p(out, out);
  901. }
  902. // calculate a unit vector normal to a triangle's face.
  903. Vector3 vTriFaceNormal3(Vector3 a, Vector3 b, Vector3 c) {
  904. Vector3 b_a, c_a, out;
  905. b_a = vSub3(b, a);
  906. c_a = vSub3(c, a);
  907. return vNorm3(vCross3(b_a, c_a));
  908. }
  909. // calculate a unit vector normal to a triangle's face.
  910. Vector3 vTriFaceNormalArea3(Vector3 a, Vector3 b, Vector3 c, float* area) {
  911. Vector3 b_a, c_a, out;
  912. b_a = vSub3(b, a);
  913. c_a = vSub3(c, a);
  914. Vector3 n = vCross3(b_a, c_a);
  915. if(area) *area = vLen(n) * .5f;
  916. return vNorm3(n);
  917. }
  918. // calculate a unit vector normal to a triangle's face.
  919. void vpTriFaceNormal3p(Vector3* tri, Vector3* out) {
  920. vTriFaceNormal3p(tri+0, tri+1, tri+2, out);
  921. }
  922. void vProjectOntoPlane3p(Vector3* v, Plane* p, Vector3* out) {
  923. Vector3 v_ortho;
  924. // get the component of v that's perpendicular to the plane
  925. vProjectNorm3p(v, &p->n, &v_ortho);
  926. // subtract it from v
  927. vSub3p(v, &v_ortho, out);
  928. }
  929. void vProjectOntoPlaneNormalized3p(Vector3* v, Plane* p, Vector3* out) {
  930. vProjectOntoPlane3p(v, p, out);
  931. vNorm3p(out, out);
  932. }
  933. void planeFromPointNormal(Vector3* p, Vector3* norm, Plane* out) {
  934. out->n = *norm;
  935. out->d = vDot3p(norm, p);
  936. }
  937. // calculates a plane from a triangle
  938. void planeFromTriangle3p(Vector3* v1, Vector3* v2, Vector3* v3, Plane* out) {
  939. vTriFaceNormal3p(v1, v2, v3, &out->n);
  940. out->d = vDot3p(&out->n, v1);
  941. }
  942. // copy a plane
  943. void planeCopy3p(Plane* in, Plane* out) {
  944. out->n = in->n;
  945. out->d = in->d;
  946. }
  947. // reverses the plane's direction
  948. void planeInverse3p(Plane* in, Plane* out) {
  949. vInv3p(&in->n, &out->n);
  950. out->d = -in->d;
  951. }
  952. // classifies a point by which side of the plane it's on, default espilon
  953. int planeClassifyPoint3p(Plane* p, Vector3* pt) {
  954. return planeClassifyPointEps3p(p, pt, FLT_CMP_EPSILON);
  955. }
  956. // classifies a point by which side of the plane it's on, custom espilon
  957. int planeClassifyPointEps3p(Plane* p, Vector3* pt, float epsilon) {
  958. float dist = vDot3p(&p->n, pt);
  959. if(fabs(dist - p->d) < epsilon)
  960. return C3DLAS_COPLANAR;
  961. else if (dist < p->d)
  962. return C3DLAS_BACK;
  963. else
  964. return C3DLAS_FRONT;
  965. }
  966. // https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
  967. // returns _INTERSECT or _DISJOINT
  968. int rayTriangleIntersect(
  969. Vector3* a, Vector3* b, Vector3* c, // triangle
  970. Vector3* ray_origin, Vector3* ray_dir, // ray
  971. float* u, float* v, float* t // barycentric out coords, t of intersection point along ray
  972. ) {
  973. Vector3 ab = vSub3(*b, *a);
  974. Vector3 ac = vSub3(*c, *a);
  975. Vector3 n = vCross3(ab, ac);
  976. float det = -vDot3(*ray_dir, n);
  977. if(fabsf(det) <= FLT_CMP_EPSILON) {
  978. return C3DLAS_DISJOINT; // the ray is parallel to the triangle
  979. }
  980. float idet = 1.0f / det;
  981. Vector3 ao = vSub3(*ray_origin, *a);
  982. Vector3 dao = vCross3(ao, *ray_dir);
  983. *u = vDot3(ac, dao) * idet;
  984. if(*u < 0.f) return C3DLAS_DISJOINT; // barycentric coord is outside the triangle
  985. *v = -vDot3(ab, dao) * idet;
  986. if(*v < 0.f || *u + *v > 1.f) return C3DLAS_DISJOINT; // barycentric coord is outside the triangle
  987. *t = vDot3(ao, n) * idet;
  988. // if(*t < 0.0f) return C3DLAS_DISJOINT; // the ray intersects the triangle behind the origin
  989. return C3DLAS_INTERSECT;
  990. }
  991. // returns _INTERSECT or _DISJOINT
  992. Vector3 triangleClosestPoint_Reference(
  993. Vector3* a, Vector3* b, Vector3* c, // triangle
  994. Vector3* p, // test point
  995. float* out_u, float* out_v // barycentric out coords of closest point
  996. ) {
  997. Vector3 ab = vSub3(*b, *a);
  998. Vector3 ac = vSub3(*c, *a);
  999. Vector3 n = vCross3(ab, ac);
  1000. Vector3 ray_dir = vNeg3(vNorm(n));
  1001. float idet = 1.0f / -vDot3(ray_dir, n);
  1002. Vector3 ao = vSub3(*p, *a);
  1003. Vector3 dao = vCross3(ao, ray_dir);
  1004. // printf("idet = %f, n = %f,%f,%f\n", idet, n.x, n.y, n.z);
  1005. float u = vDot3(ac, dao) * idet;
  1006. float v = -vDot3(ab, dao) * idet;
  1007. // printf("u,v = %f, %f\n", u, v);
  1008. if(u >= 0 && v >= 0.f && u + v <= 1.f) {
  1009. float nt = vDot3(ao, n);
  1010. Vector3 planep = vAdd3(*p, vScale3(vNeg3(n), nt));
  1011. return planep; // the ray intersects the triangle
  1012. }
  1013. float t_ab, t_bc, t_ca;
  1014. // collect all the possible locations
  1015. float dist[6];
  1016. dist[0] = vDistTPointLine3(*p, (Line3){*a, *b}, &t_ab);
  1017. dist[1] = vDistTPointLine3(*p, (Line3){*b, *c}, &t_bc);
  1018. dist[2] = vDistTPointLine3(*p, (Line3){*c, *a}, &t_ca);
  1019. dist[3] = vDist(*a, *p);
  1020. dist[4] = vDist(*b, *p);
  1021. dist[5] = vDist(*c, *p);
  1022. // find the smallest distance
  1023. float min = dist[0];
  1024. int mini = 0;
  1025. for(int i = 1; i < 6; i++) {
  1026. if(dist[i] < min) {
  1027. min = dist[i];
  1028. mini = i;
  1029. }
  1030. }
  1031. switch(mini) {
  1032. case 0: return vLerp(*a, *b, t_ab);
  1033. case 1: return vLerp(*b, *c, t_bc);
  1034. case 2: return vLerp(*c, *a, t_ca);
  1035. case 3: return *a;
  1036. case 4: return *b;
  1037. case 5: return *c;
  1038. }
  1039. return (Vector3){0,0,0}; // HACK just return something
  1040. }
  1041. /*
  1042. // https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
  1043. // returns _INTERSECT or _DISJOINT
  1044. vec3 triangleClosestPoint(
  1045. Vector3* a, Vector3* b, Vector3* c, // triangle
  1046. Vector3* p, // test point
  1047. float* out_u, float* out_v // barycentric out coords of closest point
  1048. ) {
  1049. Vector3 ab = vSub3(*b, *a);
  1050. Vector3 ac = vSub3(*c, *a);
  1051. Vector3 n = vCross3(ab, ac); // triangle plane normal
  1052. Vector3 ap = vSub3(*p, *a);
  1053. Vector3 dap = vCross3(ap, vNeg(n)); // p projected onto the triangle's plane, relative to a
  1054. // p = w*a + u*b + v*c;
  1055. float u = vDot3(ac, dap); // inversely proportional to distance from _b_, aka "beta"
  1056. // u < 0 means outside the triangle past the a/c edge
  1057. // u > 1 means outside the triangle past b
  1058. // u == 0 means somewhere on the a/c edge
  1059. // if(*u < 0.f) return C3DLAS_DISJOINT; // barycentric coord is outside the triangle
  1060. float v = -vDot3(ab, dap); // inversely proportional to distance from _c_, aka "gamma"
  1061. // v < 0 means outside the triangle past the a/b edge
  1062. // v > 1 means outside the triangle past c
  1063. // v == 0 means somewhere on the a/b edge
  1064. // if(*u < 0.f || *u + *v < 1.0f) return C3DLAS_DISJOINT; // barycentric coord is outside the triangle
  1065. float w = 1.0f - u - v; // inversely proportional to distance from _a_, aka "alpha"
  1066. // w < 0 means outside the triangle past the b/c edge
  1067. // w > 1 means outside the triangle past a
  1068. // w == 0 means somewhere on the b/c edge
  1069. if(u > 0 && v > 0 && w > 0) // point inside triangle
  1070. float t = vDot3(ap, n);
  1071. vec3 closest = vAdd3(*p, vScale3(vNeg(n), t));
  1072. return closest;
  1073. }
  1074. float new_u = 0, new_v = 0, new_w = 0;
  1075. if(w < 0) {
  1076. float t = fclamp(0f, 1f, vDot3() / vDot3());
  1077. new_v = 1.f - t;
  1078. new_w = t;
  1079. }
  1080. else if(v < 0) {
  1081. float t = fclamp(0f, 1f, vDot3() / vDot3());
  1082. new_u = t;
  1083. new_w = 1.f - t;
  1084. }
  1085. else if(u < 0) {
  1086. float t = fclamp(0f, 1f, vDot3() / vDot3());
  1087. new_u = 1.f - t;
  1088. new_v = t;
  1089. }
  1090. // if(*t < 0.0f) return C3DLAS_DISJOINT; // the ray intersects the triangle behind the origin
  1091. return C3DLAS_INTERSECT;
  1092. }
  1093. */
  1094. float distLineLine3(Line3* a, Line3* b) {
  1095. Vector3 ea = vSub3(a->end, a->start);
  1096. Vector3 eb = vSub3(b->end, b->start);
  1097. Vector3 q = vSub(b->start, a->start);
  1098. float vaa = vLenSq3(ea);
  1099. float vbb = vLenSq3(eb);
  1100. float vba = vDot3(ea, eb);
  1101. float vba_a = vDot3(q, ea);
  1102. float den = vba * vba - vbb * vaa;
  1103. float ta, tb;
  1104. if(fabs(den) < 1e-6) {
  1105. ta = 0;
  1106. tb = -vba_a / vba; // vba can never be zero here
  1107. }
  1108. else {
  1109. float vba_b = vDot3(q, eb);
  1110. ta = (vba_b * vba - vbb * vba_a) / den;
  1111. tb = (-vba_a * vba + vaa * vba_b) / den;
  1112. }
  1113. ta = fclamp(0, 1, ta);
  1114. tb = fclamp(0, 1, tb);
  1115. Vector3 pa = vAdd(a->start, vScale(ea, ta));
  1116. Vector3 pb = vAdd(b->start, vScale(eb, tb));
  1117. return vDist(pa, pb);
  1118. }
  1119. /*
  1120. float distLineLine3Slow(Line3* a, Line3* b) {
  1121. Vector3 ea = vSub3(a->end, a->start);
  1122. Vector3 eb = vSub3(b->end, b->start);
  1123. Vector3 n = vCross3(ea, eb);
  1124. float nsq = vLenSq3(n);
  1125. // TODO: handle parallel lines
  1126. vec3 b1ma1 = vSub(b->start, a->start);
  1127. float ta = vDot3(vCross3(eb, n), b1ma1) / nsq;
  1128. float tb = vDot3(vCross3(ea, n), b1ma1) / nsq;
  1129. ta = fclamp(0, 1, ta);
  1130. tb = fclamp(0, 1, tb);
  1131. vec3 pa = vAdd(a->start, vScale(ea, ta));
  1132. vec3 pb = vAdd(b->start, vScale(eb, tb));
  1133. return vDist3(pa, pb);
  1134. }
  1135. */
  1136. // C3DLAS_COPLANAR, _PARALLEL, _INTERSECT, or _DISJOINT
  1137. // aboveCnt and belowCnt are always set.
  1138. int linePlaneClip3p(
  1139. Vector3* la,
  1140. Vector3* lb,
  1141. Plane* pl,
  1142. Vector3* aboveOut,
  1143. Vector3* belowOut,
  1144. int* aboveCnt,
  1145. int* belowCnt
  1146. ) {
  1147. Vector3 ldir, c;
  1148. float da, db;
  1149. vSub3p(lb, la, &ldir);
  1150. da = vDot3p(la, &pl->n) - pl->d;
  1151. // bail if the line and plane are parallel
  1152. if(fabs(vDot3p(&pl->n, &ldir)) < FLT_CMP_EPSILON) {
  1153. *aboveCnt = 0;
  1154. *belowCnt = 0;
  1155. // check coplanarity
  1156. if(fabs(da) < FLT_CMP_EPSILON) {
  1157. return C3DLAS_COPLANAR; // the end is on the plane, so the other is too
  1158. }
  1159. return C3DLAS_PARALLEL;
  1160. }
  1161. db = vDot3p(lb, &pl->n) - pl->d;
  1162. // check if one of the points is on the plane
  1163. if(fabs(da) < FLT_CMP_EPSILON) {
  1164. if(db > 0) {
  1165. aboveOut[0] = *la; // correct ordering
  1166. aboveOut[1] = *lb;
  1167. *aboveCnt = 1;
  1168. *belowCnt = 0;
  1169. }
  1170. else {
  1171. belowOut[0] = *la; // correct ordering
  1172. belowOut[1] = *lb;
  1173. *aboveCnt = 0;
  1174. *belowCnt = 1;
  1175. }
  1176. return C3DLAS_INTERSECT;
  1177. }
  1178. if(fabs(db) < FLT_CMP_EPSILON) {
  1179. if(da > 0) {
  1180. aboveOut[0] = *la; // correct ordering
  1181. aboveOut[1] = *lb;
  1182. *aboveCnt = 1;
  1183. *belowCnt = 0;
  1184. }
  1185. else {
  1186. belowOut[0] = *la; // correct ordering
  1187. belowOut[1] = *lb;
  1188. *aboveCnt = 0;
  1189. *belowCnt = 1;
  1190. }
  1191. return C3DLAS_INTERSECT;
  1192. }
  1193. // calculate itnersection point, c
  1194. Vector3 p0, g, j;
  1195. vScale3p(&pl->n, pl->d, &p0);
  1196. vSub3p(&p0, la, &g);
  1197. float h = vDot3p(&g, &pl->n);
  1198. float i = vDot3p(&ldir, &pl->n);
  1199. float d = i != 0 ? h / i : 0;
  1200. // check if the plane intersects outside the two points
  1201. if(d < 0 || d > vDist3p(la, lb)) {
  1202. if(da > 0) {
  1203. aboveOut[0] = *la; // correct ordering
  1204. aboveOut[1] = *lb;
  1205. *aboveCnt = 1;
  1206. *belowCnt = 0;
  1207. }
  1208. else {
  1209. belowOut[0] = *la; // correct ordering
  1210. belowOut[1] = *lb;
  1211. *aboveCnt = 0;
  1212. *belowCnt = 1;
  1213. }
  1214. return C3DLAS_DISJOINT;
  1215. }
  1216. vScale3p(&ldir, d, &j);
  1217. vAdd3p(la, &j, &c);
  1218. if(da > 0) {
  1219. aboveOut[0] = *la; // correct ordering
  1220. aboveOut[1] = c;
  1221. belowOut[0] = c;
  1222. belowOut[1] = *lb;
  1223. }
  1224. else {
  1225. belowOut[0] = *la; // correct ordering
  1226. belowOut[1] = c;
  1227. aboveOut[0] = c;
  1228. aboveOut[1] = *lb;
  1229. }
  1230. *aboveCnt = 1;
  1231. *belowCnt = 1;
  1232. return C3DLAS_INTERSECT;
  1233. }
  1234. // C3DLAS_COPLANAR, _INTERSECT, or _DISJOINT
  1235. int triPlaneTestIntersect3p(Vector3* pTri, Plane* pl) {
  1236. Vector3 a, b, c;
  1237. float da, db, dc;
  1238. // get distance of each vertex from the plane
  1239. // bail early if any of them are coplanar
  1240. a = pTri[0];
  1241. da = vDot3p(&a, &pl->n) - pl->d;
  1242. if(fabs(da) < FLT_CMP_EPSILON) {
  1243. return C3DLAS_COPLANAR;
  1244. }
  1245. b = pTri[1];
  1246. db = vDot3p(&b, &pl->n) - pl->d;
  1247. if(fabs(db) < FLT_CMP_EPSILON) {
  1248. return C3DLAS_COPLANAR;
  1249. }
  1250. c = pTri[2];
  1251. dc = vDot3p(&c, &pl->n) - pl->d;
  1252. if(fabs(dc) < FLT_CMP_EPSILON) {
  1253. return C3DLAS_COPLANAR;
  1254. }
  1255. // the triangle intersects if the sign of all the distances does not match,
  1256. // ie, on vertex is on the opposite side of the plane from the others
  1257. return (signbit(da) == signbit(db) && signbit(db) == signbit(dc)) ? C3DLAS_DISJOINT : C3DLAS_INTERSECT;
  1258. }
  1259. // C3DLAS_COPLANAR, _INTERSECT, or _DISJOINT
  1260. int triPlaneClip3p(
  1261. Vector3* pTri,
  1262. Plane* pl,
  1263. Vector3* aboveOut,
  1264. Vector3* belowOut,
  1265. int* aboveCnt,
  1266. int* belowCnt
  1267. ) {
  1268. Vector3 v0, v1, v2;
  1269. float vp_d0, vp_d1, vp_d2;
  1270. v0 = pTri[0];
  1271. v1 = pTri[1];
  1272. v2 = pTri[2];
  1273. // get distance of each vertex from the plane
  1274. vp_d0 = vDot3p(&v0, &pl->n) - pl->d;
  1275. vp_d1 = vDot3p(&v1, &pl->n) - pl->d;
  1276. vp_d2 = vDot3p(&v2, &pl->n) - pl->d;
  1277. // bail early if just one is coplanar
  1278. // split in half with single-edge intersections
  1279. if(fabs(vp_d0) < FLT_CMP_EPSILON) {
  1280. if( // single edge intersection
  1281. signbit(vp_d1) != signbit(vp_d2) &&
  1282. fabs(vp_d1) > FLT_CMP_EPSILON &&
  1283. fabs(vp_d2) > FLT_CMP_EPSILON
  1284. ) {
  1285. // get intersection point
  1286. Vector3 c;
  1287. planeLineFindIntersectFast3p(pl, &v1, &v2, &c);
  1288. if(vp_d1 > 0) { // v1 is above the plane
  1289. aboveOut[0] = c; // correct winding
  1290. aboveOut[1] = v0;
  1291. aboveOut[2] = v1;
  1292. belowOut[0] = c;
  1293. belowOut[1] = v2;
  1294. belowOut[2] = v0;
  1295. }
  1296. else {
  1297. belowOut[0] = c; // correct winding
  1298. belowOut[1] = v0;
  1299. belowOut[2] = v1;
  1300. aboveOut[0] = c;
  1301. aboveOut[1] = v2;
  1302. aboveOut[2] = v0;
  1303. }
  1304. *aboveCnt = 1;
  1305. *belowCnt = 1;
  1306. return C3DLAS_INTERSECT;
  1307. }
  1308. return C3DLAS_COPLANAR; // one vertex is on the plane, the others all above or below
  1309. }
  1310. if(fabs(vp_d1) < FLT_CMP_EPSILON) {
  1311. if( // single edge intersection
  1312. signbit(vp_d0) != signbit(vp_d2) &&
  1313. fabs(vp_d0) > FLT_CMP_EPSILON &&
  1314. fabs(vp_d2) > FLT_CMP_EPSILON
  1315. ) {
  1316. // get intersection point
  1317. Vector3 c;
  1318. planeLineFindIntersectFast3p(pl, &v0, &v2, &c);
  1319. if(vp_d0 > 0) { // v0 is above the plane
  1320. aboveOut[0] = c; // correct winding
  1321. aboveOut[1] = v0;
  1322. aboveOut[2] = v1;
  1323. belowOut[0] = c;
  1324. belowOut[1] = v1;
  1325. belowOut[2] = v2;
  1326. }
  1327. else {
  1328. belowOut[0] = c; // correct winding
  1329. belowOut[1] = v0;
  1330. belowOut[2] = v1;
  1331. aboveOut[0] = c;
  1332. aboveOut[1] = v1;
  1333. aboveOut[2] = v2;
  1334. }
  1335. *aboveCnt = 1;
  1336. *belowCnt = 1;
  1337. return C3DLAS_INTERSECT;
  1338. }
  1339. return C3DLAS_COPLANAR; // one vertex is on the plane, the others all above or below
  1340. }
  1341. if(fabs(vp_d2) < FLT_CMP_EPSILON) {
  1342. if( // single edge intersection
  1343. signbit(vp_d0) != signbit(vp_d1) &&
  1344. fabs(vp_d0) > FLT_CMP_EPSILON &&
  1345. fabs(vp_d1) > FLT_CMP_EPSILON
  1346. ) {
  1347. // get intersection point
  1348. Vector3 c;
  1349. planeLineFindIntersectFast3p(pl, &v0, &v1, &c);
  1350. if(vp_d0 > 0) { // v0 is above the plane
  1351. aboveOut[0] = c; // correct winding
  1352. aboveOut[1] = v2;
  1353. aboveOut[2] = v0;
  1354. belowOut[0] = c;
  1355. belowOut[1] = v1;
  1356. belowOut[2] = v2;
  1357. }
  1358. else {
  1359. belowOut[0] = c; // correct winding
  1360. belowOut[1] = v2;
  1361. belowOut[2] = v0;
  1362. aboveOut[0] = c;
  1363. aboveOut[1] = v1;
  1364. aboveOut[2] = v2;
  1365. }
  1366. *aboveCnt = 1;
  1367. *belowCnt = 1;
  1368. return C3DLAS_INTERSECT;
  1369. }
  1370. return C3DLAS_COPLANAR; // one vertex is on the plane, the others all above or below
  1371. }
  1372. // the triangle intersects if the sign of all the distances does not match,
  1373. // ie, on vertex is on the opposite side of the plane from the others
  1374. // bail if disjoint
  1375. if(signbit(vp_d0) == signbit(vp_d1) && signbit(vp_d1) == signbit(vp_d2)) {
  1376. return C3DLAS_DISJOINT;
  1377. }
  1378. // split on which edges intersect the plane
  1379. if(signbit(vp_d0) == signbit(vp_d1)) {
  1380. // vertex 2 is isolated; edges 0,2 and 1,2 intersect
  1381. Vector3 c0, c1;
  1382. planeLineFindIntersectFast3p(pl, &v0, &v2, &c0);
  1383. planeLineFindIntersectFast3p(pl, &v1, &v2, &c1);
  1384. if(vp_d2 > 0) { // v2 is above the plane
  1385. aboveOut[0] = v2; // correct winding
  1386. aboveOut[1] = c0;
  1387. aboveOut[2] = c1;
  1388. belowOut[0] = c1;
  1389. belowOut[1] = v0;
  1390. belowOut[2] = v1;
  1391. belowOut[3] = c1;
  1392. belowOut[4] = c0;
  1393. belowOut[5] = v1;
  1394. *aboveCnt = 1;
  1395. *belowCnt = 2;
  1396. }
  1397. else {
  1398. belowOut[0] = v2; // correct winding
  1399. belowOut[1] = c0;
  1400. belowOut[2] = c1;
  1401. aboveOut[0] = c1;
  1402. aboveOut[1] = v0;
  1403. aboveOut[2] = v1;
  1404. aboveOut[3] = c1;
  1405. aboveOut[4] = c0;
  1406. aboveOut[5] = v1;
  1407. *aboveCnt = 2;
  1408. *belowCnt = 1;
  1409. }
  1410. }
  1411. else if(signbit(vp_d1) == signbit(vp_d2)) {
  1412. // vertex 0 is isolated; edges 1,0 and 2,0 intersect
  1413. Vector3 c0, c1;
  1414. planeLineFindIntersectFast3p(pl, &v1, &v0, &c0);
  1415. planeLineFindIntersectFast3p(pl, &v2, &v0, &c1);
  1416. if(vp_d0 > 0) { // v0 is above the plane
  1417. aboveOut[0] = v0; // correct winding
  1418. aboveOut[1] = c0;
  1419. aboveOut[2] = c1;
  1420. belowOut[0] = c1;
  1421. belowOut[1] = v1;
  1422. belowOut[2] = v2;
  1423. belowOut[3] = c1;
  1424. belowOut[4] = c0;
  1425. belowOut[5] = v1;
  1426. *aboveCnt = 1;
  1427. *belowCnt = 2;
  1428. }
  1429. else {
  1430. belowOut[0] = v0; // correct winding
  1431. belowOut[1] = c0;
  1432. belowOut[2] = c1;
  1433. aboveOut[0] = c1;
  1434. aboveOut[1] = v1;
  1435. aboveOut[2] = v2;
  1436. aboveOut[3] = c1;
  1437. aboveOut[4] = c0;
  1438. aboveOut[5] = v1;
  1439. *aboveCnt = 2;
  1440. *belowCnt = 1;
  1441. }
  1442. }
  1443. else {
  1444. // vertex 1 is isolated; edges 0,1 and 2,1 intersect
  1445. Vector3 c0, c1;
  1446. planeLineFindIntersectFast3p(pl, &v0, &v1, &c0);
  1447. planeLineFindIntersectFast3p(pl, &v2, &v1, &c1);
  1448. if(vp_d1 > 0) { // v1 is above the plane
  1449. aboveOut[0] = v1; // correct winding
  1450. aboveOut[1] = c1;
  1451. aboveOut[2] = c0;
  1452. belowOut[0] = c1;
  1453. belowOut[1] = v2;
  1454. belowOut[2] = v0;
  1455. belowOut[3] = c0;
  1456. belowOut[4] = c1;
  1457. belowOut[5] = v0;
  1458. *aboveCnt = 1;
  1459. *belowCnt = 2;
  1460. }
  1461. else {
  1462. belowOut[0] = v1; // correct winding
  1463. belowOut[1] = c1;
  1464. belowOut[2] = c0;
  1465. aboveOut[0] = c1;
  1466. aboveOut[1] = v2;
  1467. aboveOut[2] = v0;
  1468. aboveOut[3] = c0;
  1469. aboveOut[4] = c1;
  1470. aboveOut[5] = v0;
  1471. *aboveCnt = 2;
  1472. *belowCnt = 1;
  1473. }
  1474. }
  1475. return C3DLAS_INTERSECT;
  1476. }
  1477. // http://geomalgorithms.com/a07-_distance.html
  1478. // _PARALLEL with no output on parallel lines
  1479. // _INTERSECT with one point of output on intersection
  1480. // _DISJOINT with two outputs otherwise
  1481. int shortestLineFromRayToRay3p(Ray3* r1, Ray3* r2, Vector3* pOut) {
  1482. Vector3 u, v, w, ps, pt;
  1483. float a, b, c, d, e, s, t;
  1484. u = r1->d;
  1485. v = r2->d;
  1486. vSub3p(&r1->o, &r2->o, &w);
  1487. a = vDot3p(&u, &u);
  1488. b = vDot3p(&u, &v);
  1489. c = vDot3p(&v, &v);
  1490. d = vDot3p(&u, &w);
  1491. e = vDot3p(&v, &w);
  1492. float ac_bb = a * c - b * b;
  1493. if(fabs(ac_bb) < FLT_CMP_EPSILON) {
  1494. return C3DLAS_PARALLEL;
  1495. }
  1496. s = (b * e - c * d) / ac_bb;
  1497. t = (a * e - b * d) / ac_bb;
  1498. vScale3p(&u, s, &ps);
  1499. vScale3p(&v, t, &pt);
  1500. vAdd3p(&r1->o, &ps, &ps);
  1501. vAdd3p(&r2->o, &pt, &pt);
  1502. pOut[0] = ps;
  1503. pOut[1] = pt;
  1504. if(vDistSq3p(&ps, &pt) < FLT_CMP_EPSILON_SQ) {
  1505. return C3DLAS_INTERSECT;
  1506. }
  1507. return C3DLAS_DISJOINT;
  1508. }
  1509. //
  1510. Line3 shortestLineFromLineToLine(Line3* a, Line3* b) {
  1511. Vector3 ea = vSub3(a->end, a->start);
  1512. Vector3 eb = vSub3(b->end, b->start);
  1513. Vector3 q = vSub(b->start, a->start);
  1514. float vaa = vLenSq3(ea);
  1515. float vbb = vLenSq3(eb);
  1516. float vba = vDot3(ea, eb);
  1517. float vba_a = vDot3(q, ea);
  1518. float den = vba * vba - vbb * vaa;
  1519. float ta, tb;
  1520. if(fabs(den) < 1e-6) {
  1521. ta = 0;
  1522. tb = -vba_a / vba; // vba can never be zero here
  1523. }
  1524. else {
  1525. float vba_b = vDot3(q, eb);
  1526. ta = (vba_b * vba - vbb * vba_a) / den;
  1527. tb = (-vba_a * vba + vaa * vba_b) / den;
  1528. }
  1529. ta = fclamp(ta, 0, 1);
  1530. tb = fclamp(tb, 0, 1);
  1531. Vector3 pa = vAdd(a->start, vScale(ea, ta));
  1532. Vector3 pb = vAdd(b->start, vScale(eb, tb));
  1533. return (Line3){pa, pb};
  1534. }
  1535. void frustumFromMatrix(Matrix* m, Frustum* out) {
  1536. Matrix inv;
  1537. mInverse(m, &inv);
  1538. // first the points
  1539. // these MUST be in this order
  1540. // near
  1541. vMatrixMulf3p(-1,-1,-1, &inv, &out->points[0]);
  1542. vMatrixMulf3p(-1, 1,-1, &inv, &out->points[1]);
  1543. vMatrixMulf3p( 1,-1,-1, &inv, &out->points[2]);
  1544. vMatrixMulf3p( 1, 1,-1, &inv, &out->points[3]);
  1545. // far
  1546. vMatrixMulf3p(-1,-1, 1, &inv, &out->points[4]);
  1547. vMatrixMulf3p(-1, 1, 1, &inv, &out->points[5]);
  1548. vMatrixMulf3p( 1,-1, 1, &inv, &out->points[6]);
  1549. vMatrixMulf3p( 1, 1, 1, &inv, &out->points[7]);
  1550. // now the planes
  1551. // near and far
  1552. planeFromTriangle3p(&out->points[0], &out->points[1], &out->points[2], &out->planes[0]);
  1553. planeFromTriangle3p(&out->points[4], &out->points[5], &out->points[6], &out->planes[1]);
  1554. // sides
  1555. planeFromTriangle3p(&out->points[0], &out->points[4], &out->points[1], &out->planes[2]);
  1556. planeFromTriangle3p(&out->points[0], &out->points[4], &out->points[2], &out->planes[3]);
  1557. planeFromTriangle3p(&out->points[3], &out->points[7], &out->points[1], &out->planes[4]);
  1558. planeFromTriangle3p(&out->points[3], &out->points[7], &out->points[2], &out->planes[5]);
  1559. }
  1560. void frustumFromMatrixVK(Matrix* m, Frustum* out) {
  1561. Matrix inv;
  1562. mInverse(m, &inv);
  1563. // first the points
  1564. // these MUST be in this order
  1565. // near
  1566. vMatrixMulf3p(-1,-1, 0, &inv, &out->points[0]);
  1567. vMatrixMulf3p(-1, 1, 0, &inv, &out->points[1]);
  1568. vMatrixMulf3p( 1,-1, 0, &inv, &out->points[2]);
  1569. vMatrixMulf3p( 1, 1, 0, &inv, &out->points[3]);
  1570. // far
  1571. vMatrixMulf3p(-1,-1, 1, &inv, &out->points[4]);
  1572. vMatrixMulf3p(-1, 1, 1, &inv, &out->points[5]);
  1573. vMatrixMulf3p( 1,-1, 1, &inv, &out->points[6]);
  1574. vMatrixMulf3p( 1, 1, 1, &inv, &out->points[7]);
  1575. // now the planes
  1576. // near and far
  1577. planeFromTriangle3p(&out->points[0], &out->points[1], &out->points[2], &out->planes[0]);
  1578. planeFromTriangle3p(&out->points[4], &out->points[5], &out->points[6], &out->planes[1]);
  1579. // sides
  1580. planeFromTriangle3p(&out->points[0], &out->points[4], &out->points[1], &out->planes[2]);
  1581. planeFromTriangle3p(&out->points[0], &out->points[4], &out->points[2], &out->planes[3]);
  1582. planeFromTriangle3p(&out->points[3], &out->points[7], &out->points[1], &out->planes[4]);
  1583. planeFromTriangle3p(&out->points[3], &out->points[7], &out->points[2], &out->planes[5]);
  1584. }
  1585. void frustumFromMatrixVK_ZUP(Matrix* m, Frustum* out) {
  1586. Matrix inv;
  1587. mInverse(m, &inv);
  1588. *out = (Frustum){0};
  1589. // first the points
  1590. // these MUST be in this order
  1591. // near
  1592. vMatrixMulf3p(-1,-1, 0, &inv, &out->points[0]); // BUG this order is likely wrong for the planes but results in a sane wireframe.
  1593. vMatrixMulf3p( 1,-1, 0, &inv, &out->points[1]);
  1594. vMatrixMulf3p(-1, 1, 0, &inv, &out->points[2]);
  1595. vMatrixMulf3p( 1, 1, 0, &inv, &out->points[3]);
  1596. // far
  1597. vMatrixMulf3p(-1,-1, 1, &inv, &out->points[4]);
  1598. vMatrixMulf3p(1, -1, 1, &inv, &out->points[5]);
  1599. vMatrixMulf3p( -1,1, 1, &inv, &out->points[6]);
  1600. vMatrixMulf3p( 1, 1, 1, &inv, &out->points[7]);
  1601. // now the planes
  1602. // near and far
  1603. planeFromTriangle3p(&out->points[0], &out->points[1], &out->points[2], &out->planes[0]);
  1604. planeFromTriangle3p(&out->points[4], &out->points[5], &out->points[6], &out->planes[1]);
  1605. // sides
  1606. planeFromTriangle3p(&out->points[0], &out->points[4], &out->points[1], &out->planes[2]);
  1607. planeFromTriangle3p(&out->points[0], &out->points[4], &out->points[2], &out->planes[3]);
  1608. planeFromTriangle3p(&out->points[3], &out->points[7], &out->points[1], &out->planes[4]);
  1609. planeFromTriangle3p(&out->points[3], &out->points[7], &out->points[2], &out->planes[5]);
  1610. }
  1611. void frustumFromMatrixVK_RDepth(Matrix* m, Frustum* out) {
  1612. Matrix inv;
  1613. mInverse(m, &inv);
  1614. // first the points
  1615. // these MUST be in this order
  1616. // near
  1617. vMatrixMulf3p(-1,-1, 1, &inv, &out->points[0]);
  1618. vMatrixMulf3p(-1, 1, 1, &inv, &out->points[1]);
  1619. vMatrixMulf3p( 1,-1, 1, &inv, &out->points[2]);
  1620. vMatrixMulf3p( 1, 1, 1, &inv, &out->points[3]);
  1621. // far
  1622. vMatrixMulf3p(-1,-1, 0, &inv, &out->points[4]);
  1623. vMatrixMulf3p(-1, 1, 0, &inv, &out->points[5]);
  1624. vMatrixMulf3p( 1,-1, 0, &inv, &out->points[6]);
  1625. vMatrixMulf3p( 1, 1, 0, &inv, &out->points[7]);
  1626. // now the planes
  1627. // near and far
  1628. planeFromTriangle3p(&out->points[0], &out->points[1], &out->points[2], &out->planes[0]);
  1629. planeFromTriangle3p(&out->points[4], &out->points[5], &out->points[6], &out->planes[1]);
  1630. // sides
  1631. planeFromTriangle3p(&out->points[0], &out->points[4], &out->points[1], &out->planes[2]);
  1632. planeFromTriangle3p(&out->points[0], &out->points[4], &out->points[2], &out->planes[3]);
  1633. planeFromTriangle3p(&out->points[3], &out->points[7], &out->points[1], &out->planes[4]);
  1634. planeFromTriangle3p(&out->points[3], &out->points[7], &out->points[2], &out->planes[5]);
  1635. }
  1636. void frustumFromMatrixVK_ZUP_RDepth(Matrix* m, Frustum* out) {
  1637. Matrix inv;
  1638. mInverse(m, &inv);
  1639. *out = (Frustum){0};
  1640. // first the points
  1641. // these MUST be in this order
  1642. // near
  1643. vMatrixMulf3p(-1,-1, 1, &inv, &out->points[0]); // BUG this order is likely wrong for the planes but results in a sane wireframe.
  1644. vMatrixMulf3p( 1,-1, 1, &inv, &out->points[1]);
  1645. vMatrixMulf3p(-1, 1, 1, &inv, &out->points[2]);
  1646. vMatrixMulf3p( 1, 1, 1, &inv, &out->points[3]);
  1647. // far
  1648. vMatrixMulf3p(-1,-1, 0, &inv, &out->points[4]);
  1649. vMatrixMulf3p(1, -1, 0, &inv, &out->points[5]);
  1650. vMatrixMulf3p( -1,1, 0, &inv, &out->points[6]);
  1651. vMatrixMulf3p( 1, 1, 0, &inv, &out->points[7]);
  1652. // now the planes
  1653. // near and far
  1654. planeFromTriangle3p(&out->points[0], &out->points[1], &out->points[2], &out->planes[0]);
  1655. planeFromTriangle3p(&out->points[4], &out->points[5], &out->points[6], &out->planes[1]);
  1656. // sides
  1657. planeFromTriangle3p(&out->points[0], &out->points[4], &out->points[1], &out->planes[2]);
  1658. planeFromTriangle3p(&out->points[0], &out->points[4], &out->points[2], &out->planes[3]);
  1659. planeFromTriangle3p(&out->points[3], &out->points[7], &out->points[1], &out->planes[4]);
  1660. planeFromTriangle3p(&out->points[3], &out->points[7], &out->points[2], &out->planes[5]);
  1661. }
  1662. void frustumCenter(Frustum* f, Vector3* out) {
  1663. Vector3 sum = {0.0f,0.0f,0.0f};
  1664. for(int i = 0; i < 8; i++) vAdd3p(&f->points[i], &sum, &sum);
  1665. vScale3p(&sum, 1.0f/8.0f, out);
  1666. }
  1667. // General idea of the algorithm:
  1668. // https://lxjk.github.io/2017/04/15/Calculate-Minimal-Bounding-Sphere-of-Frustum.html
  1669. // http://archive.is/YACj2
  1670. void frustumBoundingSphere(Frustum* f, Sphere* out) {
  1671. Vector3 f0, n0;
  1672. vPointAvg3p(&f->points[0], &f->points[3], &n0);
  1673. vPointAvg3p(&f->points[4], &f->points[7], &f0);
  1674. float Dn2 = vDistSq3p(&n0, &f->points[0]);
  1675. float Df2 = vDistSq3p(&f0, &f->points[4]);
  1676. // check for ortho
  1677. if(Dn2 - Df2 < 0.00001) {
  1678. frustumCenter(f, &out->center);
  1679. out->r = vDist3p(&out->center, &f->points[0]);
  1680. return;
  1681. }
  1682. float Dnf = vDist3p(&f0, &n0);
  1683. float Dnc = (Dn2 - Df2 - Df2) / (2 * Dnf);
  1684. // printf("\n f: %f,%f,%f\n", f->points[4].x,f->points[4].y,f->points[4].z);
  1685. // printf(" n: %f,%f,%f\n", f->points[0].x,f->points[0].y,f->points[0].z);
  1686. // printf(" f0: %f,%f,%f\n", f0.x,f0.y,f0.z);
  1687. // printf(" n0: %f,%f,%f\n", n0.x,n0.y,n0.z);
  1688. // printf(" dn2, df2, dnf, dnc: %f,%f,%f,%f\n", Dn2, Df2, Dnf, Dnc);
  1689. if(Dnc > 0 && Dnc < Dnf) {
  1690. vLerp3p(&f0, &n0, Dnc / Dnf, &out->center);
  1691. out->r = sqrt(Dnc * Dnc + Dn2);
  1692. }
  1693. else {
  1694. out->center = f0;
  1695. out->r = sqrt(Df2);
  1696. }
  1697. }
  1698. void frustumInscribeSphere(Frustum* f, Sphere* out) {
  1699. Vector3 fx, nx;
  1700. vPointAvg3p(&f->points[0], &f->points[3], &nx);
  1701. vPointAvg3p(&f->points[4], &f->points[7], &fx);
  1702. /*
  1703. float Dn2 = vDistSq3p(&n0, &f->points[0]);
  1704. float Df2 = vDistSq3p(&f0, &f->points[4]);
  1705. float Dnf = vDist3p(&f0, n0);
  1706. float Dnc = (Dn2 - Df2 - Df2) / (2 * Dnf);*/
  1707. }
  1708. void quadCenterp3p(Vector3* a, Vector3* b, Vector3* c, Vector3* d, Vector3* out) {
  1709. Vector3 sum;
  1710. vAdd3p(a, b, &sum);
  1711. vAdd3p(&sum, c, &sum);
  1712. vAdd3p(&sum, d, &sum);
  1713. vScale3p(&sum, 0.25f, out);
  1714. }
  1715. void vPointAvg3p(Vector3* a, Vector3* b, Vector3* out) {
  1716. Vector3 sum;
  1717. vAdd3p(a, b, &sum);
  1718. vScale3p(&sum, 0.5f, out);
  1719. }
  1720. // reflects the distance from v to pivot across pivot.
  1721. // out, pivot, and v will form a straight line with pivot exactly in the middle.
  1722. void vReflectAcross2p(Vector2* v, Vector2* pivot, Vector2* out) {
  1723. Vector2 diff;
  1724. vSub2p(pivot, v, &diff);
  1725. vAdd2p(pivot, &diff, out);
  1726. }
  1727. // degenerate cases may not give desired results. GIGO.
  1728. void vRoundAway2p(const Vector2* in, const Vector2* center, Vector2i* out) {
  1729. if(in->x > center->x) out->x = ceilf(in->x);
  1730. else out->x = floorf(in->x);
  1731. if(in->y > center->y) out->y = ceilf(in->y);
  1732. else out->y = floorf(in->y);
  1733. }
  1734. // degenerate cases may not give desired results. GIGO.
  1735. void vRoundToward2p(const Vector2* in, const Vector2* center, Vector2i* out) {
  1736. if(in->x > center->x) out->x = floorf(in->x);
  1737. else out->x = ceilf(in->x);
  1738. if(in->y > center->y) out->y = floorf(in->y);
  1739. else out->y = ceilf(in->y);
  1740. }
  1741. // returns the *signed* area of a triangle. useful for determining winding
  1742. // positive values mean a clockwise triangle
  1743. float triArea2p(Vector2* a, Vector2* b, Vector2* c) {
  1744. return 0.5 * (
  1745. ((b->x - a->x) * (b->y + a->y)) +
  1746. ((c->x - b->x) * (c->y + b->y)) +
  1747. ((a->x - c->x) * (a->y + c->y)));
  1748. }
  1749. // determines if a point is inside a triangle
  1750. int triPointInside2p(Vector2* p, Vector2* a, Vector2* b, Vector2* c) {
  1751. int d = signbit((p->x - b->x) * (a->y - b->y) - (a->x - b->x) * (p->y - b->y));
  1752. int e = signbit((p->x - c->x) * (b->y - c->y) - (b->x - c->x) * (p->y - c->y));
  1753. if(d != e) return 0;
  1754. int f = signbit((p->x - a->x) * (c->y - a->y) - (c->x - a->x) * (p->y - a->y));
  1755. return e == f;
  1756. }
  1757. // plane-vector operations
  1758. // distance from point to plane
  1759. float pvDist3p(Plane* p, Vector3* v) {
  1760. return vDot3p(v, &p->n) + p->d;
  1761. }
  1762. // matrix-vector operations
  1763. Vector3 vMatrixMulProjectedMagic3(Vector3 in, Matrix* m) {
  1764. Vector4 v;
  1765. v.x = in.x * m->m[0+0] + in.y * m->m[4+0] + in.z * m->m[8+0] + 1.0 * m->m[12+0];
  1766. v.y = in.x * m->m[0+1] + in.y * m->m[4+1] + in.z * m->m[8+1] + 1.0 * m->m[12+1];
  1767. v.z = in.x * m->m[0+2] + in.y * m->m[4+2] + in.z * m->m[8+2] + 1.0 * m->m[12+2];
  1768. v.w = in.x * m->m[0+3] + in.y * m->m[4+3] + in.z * m->m[8+3] + 1.0 * m->m[12+3];
  1769. if(v.w == 0) return (Vector3){0,0,0};
  1770. if(v.w < 0) v.w = -v.w;
  1771. return (Vector3){.x = v.x / v.w, .y = v.y / v.w, .z = v.z / v.w};
  1772. }
  1773. Vector3 vMatrixMul3(Vector3 in, Matrix* m) {
  1774. Vector4 v;
  1775. v.x = in.x * m->m[0+0] + in.y * m->m[4+0] + in.z * m->m[8+0] + 1.0 * m->m[12+0];
  1776. v.y = in.x * m->m[0+1] + in.y * m->m[4+1] + in.z * m->m[8+1] + 1.0 * m->m[12+1];
  1777. v.z = in.x * m->m[0+2] + in.y * m->m[4+2] + in.z * m->m[8+2] + 1.0 * m->m[12+2];
  1778. v.w = in.x * m->m[0+3] + in.y * m->m[4+3] + in.z * m->m[8+3] + 1.0 * m->m[12+3];
  1779. if(v.w == 0) return (Vector3){0,0,0};
  1780. return (Vector3){.x = v.x / v.w, .y = v.y / v.w, .z = v.z / v.w};
  1781. }
  1782. Vector4 vMatrixMul4(Vector4 in, Matrix* m) {
  1783. Vector4 v;
  1784. v.x = in.x * m->m[0+0] + in.y * m->m[4+0] + in.z * m->m[8+0] + in.w * m->m[12+0];
  1785. v.y = in.x * m->m[0+1] + in.y * m->m[4+1] + in.z * m->m[8+1] + in.w * m->m[12+1];
  1786. v.z = in.x * m->m[0+2] + in.y * m->m[4+2] + in.z * m->m[8+2] + in.w * m->m[12+2];
  1787. v.w = in.x * m->m[0+3] + in.y * m->m[4+3] + in.z * m->m[8+3] + in.w * m->m[12+3];
  1788. return v;
  1789. }
  1790. // multiply a vector by a matrix
  1791. void vMatrixMul3p(Vector3* restrict in, Matrix* restrict m, Vector3* restrict out) {
  1792. vMatrixMulf3p(in->x, in->y, in->z, m, out);
  1793. }
  1794. void vMatrixMulf3p(float x, float y, float z, Matrix* restrict m, Vector3* restrict out) {
  1795. Vector4 v;
  1796. v.x = x * m->m[0+0] + y * m->m[4+0] + z * m->m[8+0] + 1 * m->m[12+0];
  1797. v.y = x * m->m[0+1] + y * m->m[4+1] + z * m->m[8+1] + 1 * m->m[12+1];
  1798. v.z = x * m->m[0+2] + y * m->m[4+2] + z * m->m[8+2] + 1 * m->m[12+2];
  1799. v.w = x * m->m[0+3] + y * m->m[4+3] + z * m->m[8+3] + 1 * m->m[12+3];
  1800. out->x = v.x / v.w;
  1801. out->y = v.y / v.w;
  1802. out->z = v.z / v.w;
  1803. }
  1804. void evalBezier3p(Vector3* e1, Vector3* e2, Vector3* c1, Vector3* c2, float t, Vector3* out) {
  1805. out->x = evalBezier1D(e1->x, e2->x, c1->x, c2->x, t);
  1806. out->y = evalBezier1D(e1->y, e2->y, c1->y, c2->y, t);
  1807. out->z = evalBezier1D(e1->z, e2->z, c1->z, c2->z, t);
  1808. }
  1809. void evalBezier2D(Vector2* e1, Vector2* e2, Vector2* c1, Vector2* c2, float t, Vector2* out) {
  1810. out->x = evalBezier1D(e1->x, e2->x, c1->x, c2->x, t);
  1811. out->y = evalBezier1D(e1->y, e2->y, c1->y, c2->y, t);
  1812. }
  1813. float evalBezier1D(float e1, float e2, float c1, float c2, float t) {
  1814. float mt, mt2, t2;
  1815. mt = 1 - t;
  1816. mt2 = mt * mt;
  1817. t2 = t * t;
  1818. return (mt2 * mt * e1) + (3 * mt2 * t * c1) + (3 * t2 * mt * c2) + (t2 * t * e2);
  1819. }
  1820. void evalBezierTangent3p(Vector3* e1, Vector3* e2, Vector3* c1, Vector3* c2, float t, Vector3* out) {
  1821. out->x = evalBezier1D_dt(e1->x, e2->x, c1->x, c2->x, t);
  1822. out->y = evalBezier1D_dt(e1->y, e2->y, c1->y, c2->y, t);
  1823. out->z = evalBezier1D_dt(e1->z, e2->z, c1->z, c2->z, t);
  1824. }
  1825. float evalBezier1D_dt(float e1, float e2, float c1, float c2, float t) {
  1826. float mt, mt2, t2;
  1827. mt = 1 - t;
  1828. mt2 = mt * mt;
  1829. t2 = t * t;
  1830. return (3 * mt2 * (c1 - e1)) + (6 * mt * t * (c2 - c1)) + (3 * t2 * (e2 - c2));
  1831. }
  1832. float evalBezier1D_ddt(float e1, float e2, float c1, float c2, float t) {
  1833. return (6 * (1 - t) * (c2 - c1 - c1 + e1)) + (6 * t * (e2 - c2 - c2 - c1));
  1834. }
  1835. void evalBezierNorm3p(Vector3* e1, Vector3* e2, Vector3* c1, Vector3* c2, float t, Vector3* out) {
  1836. out->x = evalBezier1D_ddt(e1->x, e2->x, c1->x, c2->x, t);
  1837. out->y = evalBezier1D_ddt(e1->y, e2->y, c1->y, c2->y, t);
  1838. out->z = evalBezier1D_ddt(e1->z, e2->z, c1->z, c2->z, t);
  1839. }
  1840. // Quadratic bezier functions
  1841. float evalQBezier1D(float e1, float e2, float c1, float t) {
  1842. float mt, mt2;
  1843. mt = 1 - t;
  1844. mt2 = mt * mt;
  1845. return (mt2 * e1) + (2 * mt * t * c1) + (t * t * e2);
  1846. }
  1847. void evalQBezier2D3p(Vector2* e1, Vector2* e2, Vector2* c1, float t, Vector2* out) {
  1848. out->x = evalQBezier1D(e1->x, e2->x, c1->x, t);
  1849. out->y = evalQBezier1D(e1->y, e2->y, c1->y, t);
  1850. }
  1851. void evalQBezier3p(Vector3* e1, Vector3* e2, Vector3* c1, float t, Vector3* out) {
  1852. out->x = evalQBezier1D(e1->x, e2->x, c1->x, t);
  1853. out->y = evalQBezier1D(e1->y, e2->y, c1->y, t);
  1854. out->z = evalQBezier1D(e1->z, e2->z, c1->z, t);
  1855. }
  1856. ///// bounding box functions
  1857. // 3D versions
  1858. int boxDisjoint3p(const AABB3* a, const AABB3* b) {
  1859. return a->max.x < b->min.x || b->max.x < a->min.x
  1860. || a->max.y < b->min.y || b->max.y < a->min.y
  1861. || a->max.z < b->min.z || b->max.z < a->min.z;
  1862. }
  1863. int boxOverlaps3p(const AABB3* a, const AABB3* b) {
  1864. return !boxDisjoint3p(a, b);
  1865. }
  1866. int boxContainsPoint3p(const AABB3* b, const Vector3* p) {
  1867. return b->min.x <= p->x && b->max.x >= p->x
  1868. && b->min.y <= p->y && b->max.y >= p->y
  1869. && b->min.z <= p->z && b->max.z >= p->z;
  1870. }
  1871. void boxCenter3p(const AABB3* b, Vector3* out) {
  1872. out->x = (b->max.x + b->min.x) * .5f;
  1873. out->y = (b->max.y + b->min.y) * .5f;
  1874. out->z = (b->max.z + b->min.z) * .5f;
  1875. }
  1876. Vector3 boxCenter3(const AABB3 b) {
  1877. return (Vector3) {
  1878. (b.max.x + b.min.x) * .5f,
  1879. (b.max.y + b.min.y) * .5f,
  1880. (b.max.z + b.min.z) * .5f
  1881. };
  1882. }
  1883. void boxSize3p(const AABB3* b, Vector3* out) {
  1884. out->x = b->max.x - b->min.x;
  1885. out->y = b->max.y - b->min.y;
  1886. out->z = b->max.z - b->min.z;
  1887. }
  1888. Vector3 boxSize3(const AABB3 b) {
  1889. return (Vector3){
  1890. b.max.x - b.min.x,
  1891. b.max.y - b.min.y,
  1892. b.max.z - b.min.z
  1893. };
  1894. }
  1895. void boxExpandTo3p(AABB3* b, Vector3* p) {
  1896. b->min.x = fminf(b->min.x, p->x);
  1897. b->min.y = fminf(b->min.y, p->y);
  1898. b->min.z = fminf(b->min.z, p->z);
  1899. b->max.x = fmaxf(b->max.x, p->x);
  1900. b->max.y = fmaxf(b->max.y, p->y);
  1901. b->max.z = fmaxf(b->max.z, p->z);
  1902. }
  1903. void boxExpandTo3(AABB3* b, Vector3 p) {
  1904. boxExpandTo3p(b, &p);
  1905. }
  1906. // 2D versions
  1907. int boxDisjoint2p(const AABB2* a, const AABB2* b) {
  1908. return a->max.x < b->min.x || b->max.x < a->min.x
  1909. || a->max.y < b->min.y || b->max.y < a->min.y;
  1910. }
  1911. int boxOverlaps2p(const AABB2* a, const AABB2* b) {
  1912. return !boxDisjoint2p(a, b);
  1913. }
  1914. int boxContainsPoint2p(const AABB2* b, const Vector2* p) {
  1915. return b->min.x <= p->x && b->max.x >= p->x
  1916. && b->min.y <= p->y && b->max.y >= p->y;
  1917. }
  1918. void boxCenter2p(const AABB2* b, Vector2* out) {
  1919. out->x = (b->max.x + b->min.x) / 2.0;
  1920. out->y = (b->max.y + b->min.y) / 2.0;
  1921. }
  1922. Vector2 boxSize2(const AABB2 b) {
  1923. return (Vector2){
  1924. b.max.x - b.min.x,
  1925. b.max.y - b.min.y
  1926. };
  1927. }
  1928. void boxSize2p(const AABB2* b, Vector2* out) {
  1929. out->x = b->max.x - b->min.x;
  1930. out->y = b->max.y - b->min.y;
  1931. }
  1932. void boxQuadrant2p(const AABB2* in, char ix, char iy, AABB2* out) {
  1933. Vector2 sz, c;
  1934. boxCenter2p(in, &c);
  1935. boxSize2p(in, &sz);
  1936. sz.x *= .5;
  1937. sz.y *= .5;
  1938. out->min.x = c.x - (ix ? 0.0f : sz.x);
  1939. out->min.y = c.y - (iy ? 0.0f : sz.y);
  1940. out->max.x = c.x + (ix ? sz.x : 0.0f);
  1941. out->max.y = c.y + (iy ? sz.y : 0.0f);
  1942. }
  1943. // 2D integer versions
  1944. int boxDisjoint2ip(const AABB2i* a, const AABB2i* b) {
  1945. return a->max.x < b->min.x || b->max.x < a->min.x
  1946. || a->max.y < b->min.y || b->max.y < a->min.y;
  1947. }
  1948. int boxOverlaps2ip(const AABB2i* a, const AABB2i* b) {
  1949. return !boxDisjoint2ip(a, b);
  1950. }
  1951. int boxContainsPoint2ip(const AABB2i* b, const Vector2i* p) {
  1952. return b->min.x <= p->x && b->max.x >= p->x
  1953. && b->min.y <= p->y && b->max.y >= p->y;
  1954. }
  1955. void boxCenter2ip(const AABB2i* b, Vector2* out) {
  1956. out->x = (b->max.x + b->min.x) / 2.0f;
  1957. out->y = (b->max.y + b->min.y) / 2.0f;
  1958. }
  1959. void boxSize2ip(const AABB2i* b, Vector2* out) {
  1960. out->x = b->max.x - b->min.x;
  1961. out->y = b->max.y - b->min.y;
  1962. }
  1963. // BUG: needs some fancy math work to keep everything tight. integers don't split nicely
  1964. void boxQuadrant2ip(const AABB2i* in, char ix, char iy, AABB2i* out) {
  1965. Vector2 sz, c;
  1966. printf("fix me: %s:%d", __FILE__, __LINE__);
  1967. exit(666);
  1968. boxCenter2ip(in, &c);
  1969. boxSize2ip(in, &sz);
  1970. sz.x *= .5;
  1971. sz.y *= .5;
  1972. out->min.x = c.x - (ix ? 0.0f : sz.x);
  1973. out->min.y = c.y - (iy ? 0.0f : sz.y);
  1974. out->max.x = c.x + (ix ? sz.x : 0.0f);
  1975. out->max.y = c.y + (iy ? sz.y : 0.0f);
  1976. }
  1977. // find the center of a quad
  1978. void quadCenter2p(const Quad2* q, Vector2* out) {
  1979. Vector2 c = {0};
  1980. int i;
  1981. for(i = 0; i < 4; i++) {
  1982. c.x += q->v[i].x;
  1983. c.y += q->v[i].y;
  1984. }
  1985. out->x = c.x / 4;
  1986. out->y = c.y / 4;
  1987. }
  1988. void quadRoundOutward2p(const Quad2* in, Quad2i* out) {
  1989. Vector2 c;
  1990. int i;
  1991. quadCenter2p(in, &c);
  1992. for(i = 0; i < 4; i++)
  1993. vRoundAway2p(&in->v[i], &c, &out->v[i]);
  1994. }
  1995. void quadRoundInward2p(const Quad2* in, Quad2i* out) {
  1996. Vector2 c;
  1997. int i;
  1998. quadCenter2p(in, &c);
  1999. for(i = 0; i < 4; i++)
  2000. vRoundToward2p(&in->v[i], &c, &out->v[i]);
  2001. }
  2002. int quadIsPoint2i(const Quad2i* q) {
  2003. return (
  2004. (q->v[0].x == q->v[1].x) && (q->v[1].x == q->v[2].x) && (q->v[2].x == q->v[3].x) &&
  2005. (q->v[0].y == q->v[1].y) && (q->v[1].y == q->v[2].y) && (q->v[2].y == q->v[3].y)
  2006. );
  2007. }
  2008. int quadIsAARect2i(const Quad2i* q) {
  2009. return (
  2010. q->v[0].x == q->v[3].x && q->v[1].x == q->v[2].x &&
  2011. q->v[0].y == q->v[1].y && q->v[2].y == q->v[3].y);
  2012. }
  2013. // ray stuff
  2014. void makeRay3p(Vector3* origin, Vector3* direction, Ray3* out) {
  2015. out->o.x = origin->x;
  2016. out->o.y = origin->y;
  2017. out->o.z = origin->z;
  2018. vNorm3p(direction, &out->d);
  2019. }
  2020. // ray stuff
  2021. void makeRay2(Vector2* origin, Vector2* direction, Ray2* out) {
  2022. out->o.x = origin->x;
  2023. out->o.y = origin->y;
  2024. vNorm2p(direction, &out->d);
  2025. }
  2026. // returns a local t value for the segment the normalized value falls into
  2027. static float bsNormalToLocalT2(BezierSpline2* bs, float normalT, int* segNum) {
  2028. float segT = 1.0 / (bs->length - (!bs->isLoop));
  2029. if(segNum) *segNum = (int)(normalT / segT);
  2030. return fmod(normalT, segT) / segT;
  2031. }
  2032. // returns which segment a normalized t falls in
  2033. static int bsSegNum2(BezierSpline2* bs, float normalT) {
  2034. float segT = 1.0 / (bs->length - (!bs->isLoop));
  2035. return (int)(normalT / segT);
  2036. }
  2037. // returns a full set of control points for the segment t falls into
  2038. // out's order is e1, c1, c2, e2
  2039. static void bsSegmentForT2(BezierSpline2* bs, float normalT, Vector2* out) {
  2040. BezierSplineSegment2* p, *n;
  2041. int segN, i;
  2042. segN = i = bsSegNum2(bs, normalT);
  2043. p = bs->segments;
  2044. while(i--) p = p->next;
  2045. // a loop wraps around at the end
  2046. n = (bs->isLoop && (segN == (bs->length - 1))) ? bs->segments : p->next;
  2047. // end 1
  2048. out[0].x = p->e.x;
  2049. out[0].y = p->e.y;
  2050. // control 1
  2051. out[1].x = p->c.x;
  2052. out[1].y = p->c.y;
  2053. // control 2 - this one is reflected across e2
  2054. vReflectAcross2p(&n->c, &n->e, &out[2]);
  2055. // end 2
  2056. out[3].x = n->e.x;
  2057. out[3].y = n->e.y;
  2058. }
  2059. // BUG: this function is probably horribly broken
  2060. // this function evaluates a spline with a [0,1] normalized value of t across the whole spline
  2061. void bsEvalPoint2(BezierSpline2* bs, float normalT, Vector2* out) {
  2062. int segN;
  2063. float localT;
  2064. // find which spline segment the t is in
  2065. localT = bsNormalToLocalT2(bs, normalT, &segN);
  2066. // find the local value of t
  2067. Vector2 cp[4];
  2068. bsSegmentForT2(bs, normalT, cp);
  2069. evalBezier2D(&cp[0], &cp[3], &cp[1], &cp[2], localT, out);
  2070. }
  2071. // subdivide a spline into a set of line segments. linePoints must be allocated externally.
  2072. // this function is faster than more accurate ones.
  2073. void bsEvenLines(BezierSpline2* bs, int lineCount, Vector2* linePoints) {
  2074. /*
  2075. float tIncrement;
  2076. tIncrement = (float)(bs->length - (!bs->isLoop)) / (float)lineCount;
  2077. */
  2078. }
  2079. // Catmull-Rom Spline
  2080. float evalCatmullRom1D(float t, float a, float b, float c, float d) {
  2081. float t2 = t * t;
  2082. float t3 = t2 * t;
  2083. return 0.5f * (
  2084. (2.f * b) +
  2085. (-a + c) * t +
  2086. (2.f * a - 5.f * b + 4.f * c - d) * t2 +
  2087. (-a + 3.f * b - 3.f * c + d) * t3
  2088. );
  2089. }
  2090. Vector2 evalCatmullRom2D(float t, Vector2 a, Vector2 b, Vector2 c, Vector2 d) {
  2091. float t2 = t * t;
  2092. float t3 = t2 * t;
  2093. float q0 = -t3 + 2.f * t2 - t;
  2094. float q1 = 3.f * t3 - 5.f * t2 + 2.f;
  2095. float q2 = -3.f * t3 + 4.f * t2 + t;
  2096. float q3 = t3 - t2;
  2097. return (Vector2){
  2098. .x = 0.5f * (a.x * q0 + b.x * q1 + c.x * q2 + d.x * q3),
  2099. .y = 0.5f * (a.y * q0 + b.y * q1 + c.y * q2 + d.y * q3)
  2100. };
  2101. }
  2102. Vector3 evalCatmullRom3D(float t, Vector3 a, Vector3 b, Vector3 c, Vector3 d) {
  2103. float t2 = t * t;
  2104. float t3 = t2 * t;
  2105. float q0 = -t3 + 2.f * t2 - t;
  2106. float q1 = 3.f * t3 - 5.f * t2 + 2.f;
  2107. float q2 = -3.f * t3 + 4.f * t2 + t;
  2108. float q3 = t3 - t2;
  2109. return (Vector3){
  2110. .x = 0.5f * (a.x * q0 + b.x * q1 + c.x * q2 + d.x * q3),
  2111. .y = 0.5f * (a.y * q0 + b.y * q1 + c.y * q2 + d.y * q3),
  2112. .z = 0.5f * (a.z * q0 + b.z * q1 + c.z * q2 + d.z * q3)
  2113. };
  2114. }
  2115. float evalCatmullRom1D_dt(float t, float a, float b, float c, float d) {
  2116. float t2 = t * t;
  2117. float q0 = -3.f * t2 + 4.f * t - 1.f;
  2118. float q1 = 9.f * t2 - 10.f * t;
  2119. float q2 = -9.f * t2 + 8.f * t + 1.f;
  2120. float q3 = 3.f * t2 - 2.f * t;
  2121. return 0.5f * (a * q0 + b * q1 + c * q2 + d * q3);
  2122. }
  2123. Vector2 evalCatmullRom2D_dt(float t, Vector2 a, Vector2 b, Vector2 c, Vector2 d) {
  2124. float t2 = t * t;
  2125. float q0 = -3.f * t2 + 4.f * t - 1.f;
  2126. float q1 = 9.f * t2 - 10.f * t;
  2127. float q2 = -9.f * t2 + 8.f * t + 1.f;
  2128. float q3 = 3.f * t2 - 2.f * t;
  2129. return (Vector2){
  2130. .x = 0.5f * (a.x * q0 + b.x * q1 + c.x * q2 + d.x * q3),
  2131. .y = 0.5f * (a.y * q0 + b.y * q1 + c.y * q2 + d.y * q3)
  2132. };
  2133. }
  2134. Vector3 evalCatmullRom3D_dt(float t, Vector3 a, Vector3 b, Vector3 c, Vector3 d) {
  2135. float t2 = t * t;
  2136. float q0 = -3.f * t2 + 4.f * t - 1.f;
  2137. float q1 = 9.f * t2 - 10.f * t;
  2138. float q2 = -9.f * t2 + 8.f * t + 1.f;
  2139. float q3 = 3.f * t2 - 2.f * t;
  2140. return (Vector3){
  2141. .x = 0.5f * (a.x * q0 + b.x * q1 + c.x * q2 + d.x * q3),
  2142. .y = 0.5f * (a.y * q0 + b.y * q1 + c.y * q2 + d.y * q3),
  2143. .z = 0.5f * (a.z * q0 + b.z * q1 + c.z * q2 + d.z * q3)
  2144. };
  2145. }
  2146. float evalCatmullRom1D_both(float t, float a, float b, float c, float d, float* dt) {
  2147. float t2 = t * t;
  2148. float t3 = t2 * t;
  2149. float q0 = -t3 + 2.f * t2 - t;
  2150. float q1 = 3.f * t3 - 5.f * t2 + 2.f;
  2151. float q2 = -3.f * t3 + 4.f * t2 + t;
  2152. float q3 = t3 - t2;
  2153. float dq0 = -3.f * t2 + 4.f * t - 1.f;
  2154. float dq1 = 9.f * t2 - 10.f * t;
  2155. float dq2 = -9.f * t2 + 8.f * t + 1.f;
  2156. float dq3 = 3.f * t2 - 2.f * t;
  2157. *dt = 0.5f * (a * dq0 + b * dq1 + c * dq2 + d * dq3);
  2158. return 0.5f * (a * q0 + b * q1 + c * q2 + d * q3);
  2159. }
  2160. Vector2 evalCatmullRom2D_both(float t, Vector2 a, Vector2 b, Vector2 c, Vector2 d, Vector2* dt) {
  2161. float t2 = t * t;
  2162. float t3 = t2 * t;
  2163. float q0 = -t3 + 2.f * t2 - t;
  2164. float q1 = 3.f * t3 - 5.f * t2 + 2.f;
  2165. float q2 = -3.f * t3 + 4.f * t2 + t;
  2166. float q3 = t3 - t2;
  2167. float dq0 = -3.f * t2 + 4.f * t - 1.f;
  2168. float dq1 = 9.f * t2 - 10.f * t;
  2169. float dq2 = -9.f * t2 + 8.f * t + 1.f;
  2170. float dq3 = 3.f * t2 - 2.f * t;
  2171. *dt = (Vector2){
  2172. .x = 0.5f * (a.x * dq0 + b.x * dq1 + c.x * dq2 + d.x * dq3),
  2173. .y = 0.5f * (a.y * dq0 + b.y * dq1 + c.y * dq2 + d.y * dq3)
  2174. };
  2175. return (Vector2){
  2176. .x = 0.5f * (a.x * q0 + b.x * q1 + c.x * q2 + d.x * q3),
  2177. .y = 0.5f * (a.y * q0 + b.y * q1 + c.y * q2 + d.y * q3)
  2178. };
  2179. }
  2180. Vector3 evalCatmullRom3D_both(float t, Vector3 a, Vector3 b, Vector3 c, Vector3 d, Vector3* dt) {
  2181. float t2 = t * t;
  2182. float t3 = t2 * t;
  2183. float q0 = -t3 + 2.f * t2 - t;
  2184. float q1 = 3.f * t3 - 5.f * t2 + 2.f;
  2185. float q2 = -3.f * t3 + 4.f * t2 + t;
  2186. float q3 = t3 - t2;
  2187. float dq0 = -3.f * t2 + 4.f * t - 1.f;
  2188. float dq1 = 9.f * t2 - 10.f * t;
  2189. float dq2 = -9.f * t2 + 8.f * t + 1.f;
  2190. float dq3 = 3.f * t2 - 2.f * t;
  2191. *dt = (Vector3){
  2192. .x = 0.5f * (a.x * dq0 + b.x * dq1 + c.x * dq2 + d.x * dq3),
  2193. .y = 0.5f * (a.y * dq0 + b.y * dq1 + c.y * dq2 + d.y * dq3),
  2194. .z = 0.5f * (a.z * dq0 + b.z * dq1 + c.z * dq2 + d.z * dq3)
  2195. };
  2196. return (Vector3){
  2197. .x = 0.5f * (a.x * q0 + b.x * q1 + c.x * q2 + d.x * q3),
  2198. .y = 0.5f * (a.y * q0 + b.y * q1 + c.y * q2 + d.y * q3),
  2199. .z = 0.5f * (a.z * q0 + b.z * q1 + c.z * q2 + d.z * q3)
  2200. };
  2201. }
  2202. // Cubic Hermite Splines
  2203. float evalCubicHermite1D(float t, float p0, float p1, float m0, float m1) {
  2204. const float t2 = t * t;
  2205. const float t3 = t2 * t;
  2206. return (1 + t3 + t3 - t2 - t2 - t2) * p0 +
  2207. (t3 - t2 - t2 + t) * m0 +
  2208. (t2 + t2 + t2 - t3 - t3) * p1 +
  2209. (t3 - t2) * m1;
  2210. }
  2211. Vector2 evalCubicHermite2D(float t, Vector2 p0, Vector2 p1, Vector2 m0, Vector2 m1) {
  2212. return (Vector2){
  2213. .x = evalCubicHermite1D(t, p0.x, p1.x, m0.x, m1.x),
  2214. .y = evalCubicHermite1D(t, p0.y, p1.y, m0.y, m1.y)
  2215. };
  2216. }
  2217. Vector3 evalCubicHermite3D(float t, Vector3 p0, Vector3 p1, Vector3 m0, Vector3 m1) {
  2218. #ifdef C3DLAS_USE_SIMD
  2219. __m128 p0_ = _mm_loadu_ps((float*)&p0);
  2220. __m128 p1_ = _mm_loadu_ps((float*)&p1);
  2221. __m128 m0_ = _mm_loadu_ps((float*)&m0);
  2222. __m128 m1_ = _mm_loadu_ps((float*)&m1);
  2223. // __m128 t_ = _mm_load1_ps(&t);
  2224. // __m128 one = _mm_set_ps1(1.0f);
  2225. float t2 = t * t;
  2226. float t3 = t2 * t;
  2227. float t3_2 = t3 + t3;
  2228. float t2_2 = t2 + t2;
  2229. float t2_3 = t2_2 + t2;
  2230. // __m128 t3_2 = _mm_add_ps(t3, t3);
  2231. // __m128 t2_2 = _mm_add_ps(t2, t2);
  2232. // __m128 t2_3 = _mm_add_ps(t2_2, t2);
  2233. __m128 a = _mm_set_ps1(1.0f + t3_2 - t2_3);
  2234. __m128 o1 = _mm_mul_ps(a, p0_);
  2235. __m128 d = _mm_set_ps1(t3 + t - t2_2);
  2236. __m128 o2 = _mm_mul_ps(d, m0_);
  2237. __m128 e = _mm_set_ps1(t2_3 - t3_2);
  2238. __m128 o3 = _mm_mul_ps(e, p1_);
  2239. __m128 f = _mm_set_ps1(t3 + t2);
  2240. __m128 o4 = _mm_mul_ps(f, m1_);
  2241. __m128 o = _mm_add_ps(_mm_add_ps(o1, o2), _mm_add_ps(o3, o4));
  2242. union {
  2243. Vector4 v4;
  2244. Vector3 v3;
  2245. } u;
  2246. _mm_storeu_ps(&u.v4, o);
  2247. return u.v3;
  2248. #else
  2249. return (Vector3){
  2250. .x = evalCubicHermite1D(t, p0.x, p1.x, m0.x, m1.x),
  2251. .y = evalCubicHermite1D(t, p0.y, p1.y, m0.y, m1.y),
  2252. .z = evalCubicHermite1D(t, p0.z, p1.z, m0.z, m1.z)
  2253. };
  2254. #endif
  2255. }
  2256. #include "matrix3.c"
  2257. #include "matrix4.c"
  2258. #include "quaternion.c"
  2259. #include "intersect/plane.c"
  2260. #include "intersect/box.c"