44#include < types.h>
55#include < intrin.h>
66
7- #include " iMath.h" // icos and isin
7+ #include " iMath.h"
8+ #include " xMath.h" // icos and isin
89#include " xClimate.h" // xMat3x3Identity
910#include " xMathInlines.h" // xasin, xatan2
1011// #include "xVec3Inlines.h" // xVec3Init, imported, realized xClimate has a declaration as well though.
1112
12- xVec3 g_O3 = { 0 , 0 , 0 };
13+ const xVec3 g_O3 = { 0 , 0 , 0 };
14+ const xQuat g_IQ = { 0 .0f , 0 .0f , 0 .0f , 1 .0f };
1315
1416S32 xPointInBox (const xBox* b, const xVec3* p)
1517{
@@ -248,32 +250,33 @@ void xMat4x3MoveLocalUp(xMat4x3* m, F32 mag)
248250/* xMat3x3GetEuler (xMat3x3 const *, xVec3 *) */
249251void xMat3x3GetEuler (const xMat3x3* m, xVec3* a)
250252{
251- F32 temp_f31;
252- F32 var_f1;
253- F32 var_f30;
253+ F32 yaw = -xasin (m->at .y );
254+
255+ F32 roll;
256+ F32 pitch;
254257
255- temp_f31 = -xasin (m->at .y );
256- if (temp_f31 < 1 .5707964f )
258+ if (yaw < (PI / 2 ))
257259 {
258- if (temp_f31 > -1 . 5707964f )
260+ if (yaw > -(PI / 2 ) )
259261 {
260- var_f30 = xatan2 (m->at .x , m->at .z );
261- var_f1 = xatan2 (m->right .y , m->up .y );
262+ pitch = xatan2 (m->at .x , m->at .z );
263+ roll = xatan2 (m->right .y , m->up .y );
262264 }
263265 else
264266 {
265- var_f30 = -xatan2 (-m->up .x , m->up .x );
266- var_f1 = 0 .0f ;
267+ pitch = -xatan2 (-m->up .x , m->right .x );
268+ roll = 0 .0f ;
267269 }
268270 }
269271 else
270272 {
271- var_f1 = 0 . 0f ;
272- var_f30 = xatan2 (-m-> up . x , m-> right . x ) ;
273+ pitch = xatan2 (-m-> up . x , m-> right . x ) ;
274+ roll = 0 . 0f ;
273275 }
274- a->x = var_f30;
275- a->y = temp_f31;
276- a->z = var_f1;
276+
277+ a->x = pitch;
278+ a->y = yaw;
279+ a->z = roll;
277280}
278281
279282/* xMat3x3Euler (xMat3x3 *, xVec3 const *) */
@@ -285,48 +288,31 @@ void xMat3x3Euler(xMat3x3* m, const xVec3* ypr)
285288/* xQuatToMat (xQuat const *, xMat3x3 *) */
286289void xQuatToMat (const xQuat* q, xMat3x3* m)
287290{
288- F32 temp_f10;
289- F32 temp_f11;
290- F32 temp_f12;
291- F32 temp_f13;
292- F32 temp_f1;
293- F32 temp_f2;
294- F32 temp_f3;
295- F32 temp_f3_2;
296- F32 temp_f4;
297- F32 temp_f4_2;
298- F32 temp_f5;
299- F32 temp_f5_2;
300- F32 temp_f6;
301- F32 temp_f7;
302- F32 temp_f8;
303- F32 temp_f9;
291+ F32 tx = (2 .0f * q->v .x );
292+ F32 ty = (2 .0f * q->v .y );
293+ F32 tz = (2 .0f * q->v .z );
294+ F32 tsx = tx * q->s ;
295+ F32 tsy = ty * q->s ;
296+ F32 tsz = tz * q->s ;
297+ F32 txx = tx * q->v .x ;
298+ F32 txy = ty * q->v .x ;
299+ F32 txz = tz * q->v .x ;
300+ F32 tyy = ty * q->v .y ;
301+ F32 tyz = tz * q->v .y ;
302+ F32 tzz = tz * q->v .z ;
303+
304+ m->right .x = (1 .0f - tyy) - tzz;
305+ m->right .y = txy - tsz;
306+ m->right .z = txz + tsy;
307+
308+ m->up .x = txy + tsz;
309+ m->up .y = (1 .0f - tzz) - txx;
310+ m->up .z = tyz - tsx;
311+
312+ m->at .x = txz - tsy;
313+ m->at .y = tyz + tsx;
314+ m->at .z = (1 .0f - txx) - tyy;
304315
305- temp_f5 = q->v .y ;
306- temp_f1 = q->v .z ;
307- temp_f2 = 2 .0f * temp_f5;
308- temp_f4 = q->v .x ;
309- temp_f7 = 2 .0f * temp_f1;
310- temp_f3 = q->s ;
311- temp_f6 = 2 .0f * temp_f4;
312- temp_f12 = temp_f2 * temp_f5;
313- temp_f13 = temp_f7 * temp_f1;
314- temp_f9 = temp_f7 * temp_f3;
315- temp_f10 = temp_f2 * temp_f4;
316- temp_f8 = temp_f2 * temp_f3;
317- temp_f11 = temp_f7 * temp_f4;
318- m->right .x = (1 .0f - temp_f12) - temp_f13;
319- temp_f4_2 = temp_f6 * temp_f4;
320- m->right .y = temp_f10 - temp_f9;
321- temp_f3_2 = temp_f6 * temp_f3;
322- temp_f5_2 = temp_f7 * temp_f5;
323- m->right .z = temp_f11 + temp_f8;
324- m->up .x = temp_f10 + temp_f9;
325- m->up .y = (1 .0f - temp_f13) - temp_f4_2;
326- m->up .z = temp_f5_2 - temp_f3_2;
327- m->at .x = temp_f11 - temp_f8;
328- m->at .y = temp_f5_2 + temp_f3_2;
329- m->at .z = (1 .0f - temp_f4_2) - temp_f12;
330316 m->flags = 0 ;
331317}
332318
@@ -404,6 +390,28 @@ void xQuatFromMat(xQuat* q, const xMat3x3* m)
404390 // }
405391}
406392
393+ void xQuatFromAxisAngle (xQuat* q, const xVec3* a, F32 t)
394+ {
395+ F32 t_2;
396+
397+ if (t == 0 .0f )
398+ {
399+ xQuatCopy (q, &g_IQ);
400+ }
401+ else
402+ {
403+ t_2 = isin (t * 0 .5f );
404+ q->s = icos ((t * 0 .5f ));
405+ xVec3SMul (&q->v , a, t_2);
406+ }
407+ }
408+
409+ void xQuatToAxisAngle (const xQuat* q, xVec3* a, F32* t)
410+ {
411+ *t = 2 .0f * xacos (q->s );
412+ xVec3Normalize (a, &q->v );
413+ }
414+
407415/* xQuatSlerp (xQuat *, xQuat const *, xQuat const *, float) */
408416void xQuatSlerp (xQuat* q, const xQuat* a, const xQuat* b, F32 t)
409417{
@@ -589,6 +597,74 @@ void xMat3x3ScaleC(xMat3x3* m, F32 x, F32 y, F32 z)
589597
590598void xMat3x3RMulRotY (xMat3x3* o, const xMat3x3* m, F32 t)
591599{
600+ F32 cos = icos (t);
601+ F32 sin = isin (t);
602+ if (o == m)
603+ {
604+ F32 temp = o->right .z ;
605+ o->right .z = ((cos * o->right .z ) - (sin * o->right .x ));
606+ o->right .x = ((cos * o->right .x ) + (sin * temp));
607+
608+ temp = o->up .z ;
609+ o->up .z = ((cos * temp) - (sin * o->up .x ));
610+ o->up .x = ((cos * o->up .x ) + (sin * temp));
611+
612+ temp = o->at .z ;
613+ o->at .z = ((cos * temp) - (sin * o->at .x ));
614+ o->at .x = ((cos * o->at .x ) + (sin * temp));
615+ }
616+ else
617+ {
618+ o->right .x = (cos * m->right .x + (sin * m->right .z ));
619+ o->right .y = m->right .y ;
620+ o->right .z = (cos * m->right .z - (sin * m->right .x ));
621+
622+ o->up .x = (cos * m->up .x + (sin * m->up .z ));
623+ o->up .y = m->up .y ;
624+ o->up .z = (cos * m->up .z - (sin * m->up .x ));
625+
626+ o->at .x = (cos * m->at .x + (sin * m->at .z ));
627+ o->at .y = m->at .y ;
628+ o->at .z = (cos * m->at .z - (sin * m->at .x ));
629+
630+ o->flags = 0 ;
631+ }
632+ }
633+
634+ void xMat3x3Transpose (xMat3x3* o, const xMat3x3* m)
635+ {
636+ F32 temp;
637+
638+ if (o == m)
639+ {
640+ temp = o->right .y ;
641+ o->right .y = o->up .x ;
642+ o->up .x = temp;
643+
644+ temp = o->right .z ;
645+ o->right .z = o->at .x ;
646+ o->at .x = temp;
647+
648+ temp = o->up .z ;
649+ o->up .z = o->at .y ;
650+ o->at .y = temp;
651+
652+ return ;
653+ }
654+
655+ o->right .x = m->right .x ;
656+ o->right .y = m->up .x ;
657+ o->right .z = m->at .x ;
658+
659+ o->up .x = m->right .y ;
660+ o->up .y = m->up .y ;
661+ o->up .z = m->at .y ;
662+
663+ o->at .x = m->right .z ;
664+ o->at .y = m->up .z ;
665+ o->at .z = m->at .z ;
666+
667+ o->flags = 0 ;
592668}
593669
594670/* xMat3x3Mul (xMat3x3 *, xMat3x3 const *, xMat3x3 const *) */
@@ -674,6 +750,21 @@ void xBoxFromRay(xBox& box, const xRay3& ray)
674750{
675751}
676752
753+ void xBoxUnion (xBox& a, const xBox& b, const xBox& c)
754+ {
755+ a.upper .x = MAX (b.upper .x , c.upper .x );
756+ a.upper .y = MAX (b.upper .y , c.upper .y );
757+ a.upper .z = MAX (b.upper .z , c.upper .z );
758+
759+ a.lower .x = MIN (b.lower .x , c.lower .x );
760+ a.lower .y = MIN (b.lower .y , c.lower .y );
761+ a.lower .z = MIN (b.lower .z , c.lower .z );
762+ }
763+
764+ // Equivalent: regalloc(?)
677765void xMat3x3LMulVec (xVec3* o, const xMat3x3* m, const xVec3* v)
678766{
767+ o->x = (v->x * m->right .x ) + (v->y * m->right .y ) + (v->z * m->right .z );
768+ o->y = (m->up .x * v->x ) + (m->up .y * v->y ) + (m->up .z * v->z );
769+ o->z = (m->at .x * v->x ) + (m->at .y * v->y ) + (m->at .z * v->z );
679770}
0 commit comments