1#ifndef NW4R_MATH_TYPES_H
2#define NW4R_MATH_TYPES_H
3#include <nw4r/types_nw4r.h>
5#include <nw4r/math/math_arithmetic.h>
6#include <nw4r/math/math_triangular.h>
8#include <revolution/MTX.h>
26 VEC2(f32 fx, f32 fy) {
32 return reinterpret_cast<f32*
>(
this);
34 operator const f32*()
const {
35 return reinterpret_cast<const f32*
>(
this);
38 VEC2 operator+(
const VEC2& rRhs)
const {
39 return VEC2(x + rRhs.x, y + rRhs.y);
41 VEC2 operator-(
const VEC2& rRhs)
const {
42 return VEC2(x - rRhs.x, y - rRhs.y);
45 VEC2& operator+=(
const VEC2& rRhs) {
50 VEC2& operator-=(
const VEC2& rRhs) {
56 bool operator==(
const VEC2& rRhs)
const {
57 return x == rRhs.x && y == rRhs.y;
59 bool operator!=(
const VEC2& rRhs)
const {
60 return x != rRhs.x || y != rRhs.y;
83 VEC3(f32 fx, f32 fy, f32 fz) {
88 VEC3(
const _VEC3& rVec) {
93 VEC3(
const Vec& rVec) {
98 VEC3(
const f32* pData) {
105 return reinterpret_cast<Vec*
>(
this);
107 operator const Vec*()
const {
108 return reinterpret_cast<const Vec*
>(
this);
112 return x * x + y * y + z * z;
115 VEC3 operator-()
const {
116 return VEC3(-x, -y, -z);
119 VEC3 operator+(
const VEC3& rRhs)
const {
121 VEC3Add(&out,
this, &rRhs);
124 VEC3 operator-(
const VEC3& rRhs)
const {
126 VEC3Sub(&out,
this, &rRhs);
129 VEC3 operator*(f32 x)
const {
131 VEC3Scale(&out,
this, x);
134 VEC3 operator/(f32 x)
const {
139 VEC3& operator+=(
const VEC3& rRhs) {
140 VEC3Add(
this,
this, &rRhs);
143 VEC3& operator-=(
const VEC3& rRhs) {
144 VEC3Sub(
this,
this, &rRhs);
147 VEC3& operator*=(f32 x) {
148 VEC3Scale(
this,
this, x);
151 VEC3& operator/=(f32 x) {
152 return *
this *= (1 / x);
155 bool operator==(
const VEC3& rRhs)
const {
156 return x == rRhs.x && y == rRhs.y && z == rRhs.z;
158 bool operator!=(
const VEC3& rRhs)
const {
159 return x != rRhs.x || y != rRhs.y || z != rRhs.z;
197 f32 _00, _01, _02, _03;
198 f32 _10, _11, _12, _13;
199 f32 _20, _21, _22, _23;
209 typedef f32 (*MtxRef)[4];
210 typedef const f32 (*MtxRefConst)[4];
215 MTX34(f32 f00, f32 f01, f32 f02, f32 f03,
216 f32 f10, f32 f11, f32 f12, f32 f13,
217 f32 f20, f32 f21, f32 f22, f32 f23) {
218 _00 = f00; _01 = f01; _02 = f02; _03 = f03;
219 _10 = f10; _11 = f11; _12 = f12; _13 = f13;
220 _20 = f20; _21 = f21; _22 = f22; _23 = f23;
227 operator MtxRefConst()
const {
242 f32 _00, _01, _02, _03;
243 f32 _10, _11, _12, _13;
244 f32 _20, _21, _22, _23;
245 f32 _30, _31, _32, _33;
255 typedef f32 (*Mtx44Ref)[4];
256 typedef const f32 (*Mtx44RefConst)[4];
260 operator Mtx44Ref() {
263 operator Mtx44RefConst()
const {
281 QUAT(f32 fx, f32 fy, f32 fz, f32 fw) {
293 return reinterpret_cast<const Quaternion*
>(
this);
302inline f32 VEC2Len(
const VEC2* pVec) {
303 return FSqrt(pVec->x * pVec->x + pVec->y * pVec->y);
311VEC3* VEC3Maximize(VEC3* pOut,
const VEC3* pA,
const VEC3* pB);
312VEC3* VEC3Minimize(VEC3* pOut,
const VEC3* pA,
const VEC3* pB);
313VEC3* VEC3TransformNormal(VEC3* pOut,
const MTX34* pMtx,
const VEC3* pVec);
315inline VEC3* VEC3Add(
register VEC3* pOut,
register const VEC3* pA,
316 register const VEC3* pB) {
317 register f32 work0, work1, work2;
322 psq_l work0, VEC3.x(pA), 0, 0
323 psq_l work1, VEC3.x(pB), 0, 0
324 ps_add work2, work0, work1
325 psq_st work2, VEC3.x(pOut), 0, 0
328 psq_l work0, VEC3.z(pA), 1, 0
329 psq_l work1, VEC3.z(pB), 1, 0
330 ps_add work2, work0, work1
331 psq_st work2, VEC3.z(pOut), 1, 0
338inline f32 VEC3Dot(
register const VEC3* pA,
register const VEC3* pB) {
340 register f32 work0, work1, work2, work3;
345 psq_l work0,
VEC3.y(pA), 0, 0
346 psq_l work1,
VEC3.y(pB), 0, 0
347 ps_mul work0, work0, work1
350 psq_l work3,
VEC3.x(pA), 1, 0
351 psq_l work2,
VEC3.x(pB), 1, 0
352 ps_madd work1, work3, work2, work0
355 ps_sum0 dot, work1, work0, work0
362inline f32 VEC3LenSq(
register const VEC3* pVec) {
363 register f32 work0, work1, work2;
368 psq_l work0,
VEC3.x(pVec), 0, 0
369 ps_mul work0, work0, work0
372 lfs work1,
VEC3.z(pVec)
373 ps_madd work2, work1, work1, work0
376 ps_sum0 work2, work2, work0, work0
383inline VEC3* VEC3Lerp(
register VEC3* pOut,
register const VEC3* pVec1,
384 register const VEC3* pVec2,
register f32 t) {
385 register f32 work0, work1, work2;
390 psq_l work0,
VEC3.x(pVec1), 0, 0
391 psq_l work1,
VEC3.x(pVec2), 0, 0
392 ps_sub work2, work1, work0
394 ps_madds0 work2, work2, t, work0
395 psq_st work2,
VEC3.x(pOut), 0, 0
398 psq_l work0,
VEC3.z(pVec1), 1, 0
399 psq_l work1,
VEC3.z(pVec2), 1, 0
400 ps_sub work2, work1, work0
402 ps_madds0 work2, work2, t, work0
403 psq_st work2,
VEC3.z(pOut), 1, 0
410inline VEC3* VEC3Scale(
register VEC3* pOut,
register const VEC3* pIn,
411 register f32 scale) {
412 register f32 work0, work1;
417 psq_l work0,
VEC3.x(pIn), 0, 0
418 ps_muls0 work1, work0, scale
419 psq_st work1,
VEC3.x(pOut), 0, 0
422 psq_l work0,
VEC3.z(pIn), 1, 0
423 ps_muls0 work1, work0, scale
424 psq_st work1,
VEC3.z(pOut), 1, 0
431inline VEC3* VEC3Sub(
register VEC3* pOut,
register const VEC3* pA,
432 register const VEC3* pB) {
433 register f32 work0, work1, work2;
438 psq_l work0,
VEC3.x(pA), 0, 0
439 psq_l work1,
VEC3.x(pB), 0, 0
440 ps_sub work2, work0, work1
441 psq_st work2,
VEC3.x(pOut), 0, 0
444 psq_l work0,
VEC3.z(pA), 1, 0
445 psq_l work1,
VEC3.z(pB), 1, 0
446 ps_sub work2, work0, work1
447 psq_st work2,
VEC3.z(pOut), 1, 0
455 PSVECCrossProduct(*pA, *pB, *pOut);
459inline f32 VEC3DistSq(
const VEC3* pA,
const VEC3* pB) {
460 return PSVECSquareDistance(*pA, *pB);
463inline f32 VEC3Len(
const VEC3* pVec) {
464 return PSVECMag(*pVec);
467inline VEC3* VEC3Normalize(
VEC3* pOut,
const VEC3* pIn) {
468 PSVECNormalize(*pIn, *pOut);
473 PSMTXMultVec(*pMtx, *pVec, *pOut);
477inline VEC3* VEC3TransformCoord(
VEC3* pOut,
const MTX34* pMtx,
479 PSMTXMultVec(*pMtx, *pVec, *pOut);
496ulong MTX34InvTranspose(
MTX33* pOut,
const MTX34* pIn);
501MTX34* MTX34RotXYZFIdx(
MTX34* pMtx, f32 fx, f32 fy, f32 fz);
504 PSMTXCopy(*pIn, *pOut);
509 PSMTXIdentity(*pMtx);
513inline ulong MTX34Inv(
MTX34* pOut,
const MTX34* pIn) {
514 return PSMTXInverse(*pIn, *pOut);
517inline ulong MTX34InvTranspose(
MTX34* pOut,
const MTX34* pIn) {
518 return PSMTXInvXpose(*pIn, *pOut);
522 const VEC3* pTarget) {
523 C_MTXLookAt(*pMtx, *pPos, *pUp, *pTarget);
528 PSMTXConcat(*pA, *pB, *pOut);
534 PSMTXConcatArray(*p1, *pSrc, *pOut, len);
538inline MTX34* MTX34RotAxisRad(
MTX34* pOut,
const VEC3* pAxis, f32 frad) {
539 return MTX34RotAxisFIdx(pOut, pAxis, NW4R_MATH_RAD_TO_FIDX(frad));
542inline MTX34* MTX34RotXYZDeg(
MTX34* pMtx, f32 dx, f32 dy, f32 dz) {
543 return MTX34RotXYZFIdx(pMtx, NW4R_MATH_DEG_TO_FIDX(dx),
544 NW4R_MATH_DEG_TO_FIDX(dy),
545 NW4R_MATH_DEG_TO_FIDX(dz));
548inline MTX34* MTX34RotXYZRad(
MTX34* pMtx, f32 rx, f32 ry, f32 rz) {
549 return MTX34RotXYZFIdx(pMtx, NW4R_MATH_RAD_TO_FIDX(rx),
550 NW4R_MATH_RAD_TO_FIDX(ry),
551 NW4R_MATH_RAD_TO_FIDX(rz));
555 PSMTXScaleApply(*pIn, *pOut, pScale->x, pScale->y, pScale->z);
560 C_QUATMtx(*pQuat, *pMtx);
565 PSMTXTransApply(*pIn, *pOut, pTrans->x, pTrans->y, pTrans->z);
583 PSMTXQuat(*pMtx, *pQuat);
588inline QUAT* C_QUATSlerp(
QUAT* pOut,
const QUAT* p1,
const QUAT* p2, f32 t) {
589 ::C_QUATSlerp(*p1, *p2, *pOut, t);