c3dlas.c 73 KB

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