17 #ifndef _VECTORMATH_MAT_AOS_CPP_H
18 #define _VECTORMATH_MAT_AOS_CPP_H
20 namespace Vectormath {
26 #define _VECTORMATH_PI_OVER_2 1.570796327f
47 float qx, qy, qz, qw, qx2, qy2, qz2, qxqx2, qyqy2, qzqz2, qxqy2, qyqz2, qzqw2, qxqz2, qyqw2, qxqw2;
64 mCol0 =
Vector3( ( ( 1.0f - qyqy2 ) - qzqz2 ), ( qxqy2 + qzqw2 ), ( qxqz2 - qyqw2 ) );
65 mCol1 =
Vector3( ( qxqy2 - qzqw2 ), ( ( 1.0f - qxqx2 ) - qzqz2 ), ( qyqz2 + qxqw2 ) );
66 mCol2 =
Vector3( ( qxqz2 + qyqw2 ), ( qyqz2 - qxqw2 ), ( ( 1.0f - qxqx2 ) - qyqy2 ) );
96 *(&
mCol0 + col) = vec;
111 tmpV3_0 = this->
getCol( col );
113 this->
setCol( col, tmpV3_0 );
139 return *(&
mCol0 + col);
149 return *(&
mCol0 + col);
154 return *(&
mCol0 + col);
181 detinv = ( 1.0f /
dot( mat.
getCol2(), tmp2 ) );
183 Vector3( ( tmp0.
getX() * detinv ), ( tmp1.getX() * detinv ), ( tmp2.getX() * detinv ) ),
184 Vector3( ( tmp0.
getY() * detinv ), ( tmp1.getY() * detinv ), ( tmp2.getY() * detinv ) ),
185 Vector3( ( tmp0.
getZ() * detinv ), ( tmp1.getZ() * detinv ), ( tmp2.getZ() * detinv ) )
253 *
this = *
this * scalar;
274 ( *
this * mat.
mCol0 ),
275 ( *
this * mat.
mCol1 ),
276 ( *
this * mat.
mCol2 )
342 float sX, cX, sY, cY, sZ, cZ, tmp0, tmp1;
343 sX = sinf( radiansXYZ.
getX() );
344 cX = cosf( radiansXYZ.
getX() );
345 sY = sinf( radiansXYZ.
getY() );
346 cY = cosf( radiansXYZ.
getY() );
347 sZ = sinf( radiansXYZ.
getZ() );
348 cZ = cosf( radiansXYZ.
getZ() );
352 Vector3( ( cZ * cY ), ( sZ * cY ), -sY ),
353 Vector3( ( ( tmp0 * sX ) - ( sZ * cX ) ), ( ( tmp1 * sX ) + ( cZ * cX ) ), ( cY * sX ) ),
354 Vector3( ( ( tmp0 * cX ) + ( sZ * sX ) ), ( ( tmp1 * cX ) - ( cZ * sX ) ), ( cY * cX ) )
360 float x, y, z, s, c, oneMinusC, xy, yz, zx;
369 oneMinusC = ( 1.0f - c );
371 Vector3( ( ( ( x * x ) * oneMinusC ) + c ), ( ( xy * oneMinusC ) + ( z * s ) ), ( ( zx * oneMinusC ) - ( y * s ) ) ),
372 Vector3( ( ( xy * oneMinusC ) - ( z * s ) ), ( ( ( y * y ) * oneMinusC ) + c ), ( ( yz * oneMinusC ) + ( x * s ) ) ),
373 Vector3( ( ( zx * oneMinusC ) + ( y * s ) ), ( ( yz * oneMinusC ) - ( x * s ) ), ( ( ( z * z ) * oneMinusC ) + c ) )
418 #ifdef _VECTORMATH_DEBUG
420 inline void print(
const Matrix3 & mat )
422 print( mat.getRow( 0 ) );
423 print( mat.getRow( 1 ) );
424 print( mat.getRow( 2 ) );
427 inline void print(
const Matrix3 & mat,
const char * name )
429 printf(
"%s:\n", name);
511 *(&
mCol0 + col) = vec;
527 tmpV3_0 = this->
getCol( col );
529 this->
setCol( col, tmpV3_0 );
560 return *(&
mCol0 + col);
570 return *(&
mCol0 + col);
575 return *(&
mCol0 + col);
599 Vector4 res0, res1, res2, res3;
600 float mA, mB, mC, mD, mE, mF, mG, mH, mI, mJ, mK, mL, mM, mN, mO, mP, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, detInv;
617 tmp0 = ( ( mK * mD ) - ( mC * mL ) );
618 tmp1 = ( ( mO * mH ) - ( mG * mP ) );
619 tmp2 = ( ( mB * mK ) - ( mJ * mC ) );
620 tmp3 = ( ( mF * mO ) - ( mN * mG ) );
621 tmp4 = ( ( mJ * mD ) - ( mB * mL ) );
622 tmp5 = ( ( mN * mH ) - ( mF * mP ) );
623 res0.
setX( ( ( ( mJ * tmp1 ) - ( mL * tmp3 ) ) - ( mK * tmp5 ) ) );
624 res0.
setY( ( ( ( mN * tmp0 ) - ( mP * tmp2 ) ) - ( mO * tmp4 ) ) );
625 res0.
setZ( ( ( ( mD * tmp3 ) + ( mC * tmp5 ) ) - ( mB * tmp1 ) ) );
626 res0.
setW( ( ( ( mH * tmp2 ) + ( mG * tmp4 ) ) - ( mF * tmp0 ) ) );
627 detInv = ( 1.0f / ( ( ( ( mA * res0.
getX() ) + ( mE * res0.
getY() ) ) + ( mI * res0.
getZ() ) ) + ( mM * res0.
getW() ) ) );
628 res1.
setX( ( mI * tmp1 ) );
629 res1.
setY( ( mM * tmp0 ) );
630 res1.
setZ( ( mA * tmp1 ) );
631 res1.
setW( ( mE * tmp0 ) );
632 res3.
setX( ( mI * tmp3 ) );
633 res3.
setY( ( mM * tmp2 ) );
634 res3.
setZ( ( mA * tmp3 ) );
635 res3.
setW( ( mE * tmp2 ) );
636 res2.
setX( ( mI * tmp5 ) );
637 res2.
setY( ( mM * tmp4 ) );
638 res2.
setZ( ( mA * tmp5 ) );
639 res2.
setW( ( mE * tmp4 ) );
640 tmp0 = ( ( mI * mB ) - ( mA * mJ ) );
641 tmp1 = ( ( mM * mF ) - ( mE * mN ) );
642 tmp2 = ( ( mI * mD ) - ( mA * mL ) );
643 tmp3 = ( ( mM * mH ) - ( mE * mP ) );
644 tmp4 = ( ( mI * mC ) - ( mA * mK ) );
645 tmp5 = ( ( mM * mG ) - ( mE * mO ) );
646 res2.
setX( ( ( ( mL * tmp1 ) - ( mJ * tmp3 ) ) + res2.
getX() ) );
647 res2.
setY( ( ( ( mP * tmp0 ) - ( mN * tmp2 ) ) + res2.
getY() ) );
648 res2.
setZ( ( ( ( mB * tmp3 ) - ( mD * tmp1 ) ) - res2.
getZ() ) );
649 res2.
setW( ( ( ( mF * tmp2 ) - ( mH * tmp0 ) ) - res2.
getW() ) );
650 res3.
setX( ( ( ( mJ * tmp5 ) - ( mK * tmp1 ) ) + res3.
getX() ) );
651 res3.
setY( ( ( ( mN * tmp4 ) - ( mO * tmp0 ) ) + res3.
getY() ) );
652 res3.
setZ( ( ( ( mC * tmp1 ) - ( mB * tmp5 ) ) - res3.
getZ() ) );
653 res3.
setW( ( ( ( mG * tmp0 ) - ( mF * tmp4 ) ) - res3.
getW() ) );
654 res1.
setX( ( ( ( mK * tmp3 ) - ( mL * tmp5 ) ) - res1.
getX() ) );
655 res1.
setY( ( ( ( mO * tmp2 ) - ( mP * tmp4 ) ) - res1.
getY() ) );
656 res1.
setZ( ( ( ( mD * tmp5 ) - ( mC * tmp3 ) ) + res1.
getZ() ) );
657 res1.
setW( ( ( ( mH * tmp4 ) - ( mG * tmp2 ) ) + res1.
getW() ) );
688 float dx, dy, dz, dw, mA, mB, mC, mD, mE, mF, mG, mH, mI, mJ, mK, mL, mM, mN, mO, mP, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5;
705 tmp0 = ( ( mK * mD ) - ( mC * mL ) );
706 tmp1 = ( ( mO * mH ) - ( mG * mP ) );
707 tmp2 = ( ( mB * mK ) - ( mJ * mC ) );
708 tmp3 = ( ( mF * mO ) - ( mN * mG ) );
709 tmp4 = ( ( mJ * mD ) - ( mB * mL ) );
710 tmp5 = ( ( mN * mH ) - ( mF * mP ) );
711 dx = ( ( ( mJ * tmp1 ) - ( mL * tmp3 ) ) - ( mK * tmp5 ) );
712 dy = ( ( ( mN * tmp0 ) - ( mP * tmp2 ) ) - ( mO * tmp4 ) );
713 dz = ( ( ( mD * tmp3 ) + ( mC * tmp5 ) ) - ( mB * tmp1 ) );
714 dw = ( ( ( mH * tmp2 ) + ( mG * tmp4 ) ) - ( mF * tmp0 ) );
715 return ( ( ( ( mA * dx ) + ( mE * dy ) ) + ( mI * dz ) ) + ( mM * dw ) );
782 *
this = *
this * scalar;
824 ( *
this * mat.
mCol0 ),
825 ( *
this * mat.
mCol1 ),
826 ( *
this * mat.
mCol2 ),
827 ( *
this * mat.
mCol3 )
849 *
this = *
this * tfrm;
942 float sX, cX, sY, cY, sZ, cZ, tmp0, tmp1;
943 sX = sinf( radiansXYZ.
getX() );
944 cX = cosf( radiansXYZ.
getX() );
945 sY = sinf( radiansXYZ.
getY() );
946 cY = cosf( radiansXYZ.
getY() );
947 sZ = sinf( radiansXYZ.
getZ() );
948 cZ = cosf( radiansXYZ.
getZ() );
952 Vector4( ( cZ * cY ), ( sZ * cY ), -sY, 0.0f ),
953 Vector4( ( ( tmp0 * sX ) - ( sZ * cX ) ), ( ( tmp1 * sX ) + ( cZ * cX ) ), ( cY * sX ), 0.0f ),
954 Vector4( ( ( tmp0 * cX ) + ( sZ * sX ) ), ( ( tmp1 * cX ) - ( cZ * sX ) ), ( cY * cX ), 0.0f ),
961 float x, y, z, s, c, oneMinusC, xy, yz, zx;
970 oneMinusC = ( 1.0f - c );
972 Vector4( ( ( ( x * x ) * oneMinusC ) + c ), ( ( xy * oneMinusC ) + ( z * s ) ), ( ( zx * oneMinusC ) - ( y * s ) ), 0.0f ),
973 Vector4( ( ( xy * oneMinusC ) - ( z * s ) ), ( ( ( y * y ) * oneMinusC ) + c ), ( ( yz * oneMinusC ) + ( x * s ) ), 0.0f ),
974 Vector4( ( ( zx * oneMinusC ) + ( y * s ) ), ( ( yz * oneMinusC ) - ( x * s ) ), ( ( ( z * z ) * oneMinusC ) + c ), 0.0f ),
1007 scale4 =
Vector4( scaleVec, 1.0f );
1031 v3Z =
normalize( ( eyePos - lookAtPos ) );
1033 v3Y =
cross( v3Z, v3X );
1042 rangeInv = ( 1.0f / ( zNear - zFar ) );
1044 Vector4( ( f / aspect ), 0.0f, 0.0f, 0.0f ),
1045 Vector4( 0.0f, f, 0.0f, 0.0f ),
1046 Vector4( 0.0f, 0.0f, ( ( zNear + zFar ) * rangeInv ), -1.0f ),
1047 Vector4( 0.0f, 0.0f, ( ( ( zNear * zFar ) * rangeInv ) * 2.0f ), 0.0f )
1053 float sum_rl, sum_tb, sum_nf, inv_rl, inv_tb, inv_nf, n2;
1054 sum_rl = ( right + left );
1055 sum_tb = ( top + bottom );
1056 sum_nf = ( zNear + zFar );
1057 inv_rl = ( 1.0f / ( right - left ) );
1058 inv_tb = ( 1.0f / ( top - bottom ) );
1059 inv_nf = ( 1.0f / ( zNear - zFar ) );
1060 n2 = ( zNear + zNear );
1062 Vector4( ( n2 * inv_rl ), 0.0f, 0.0f, 0.0f ),
1063 Vector4( 0.0f, ( n2 * inv_tb ), 0.0f, 0.0f ),
1064 Vector4( ( sum_rl * inv_rl ), ( sum_tb * inv_tb ), ( sum_nf * inv_nf ), -1.0f ),
1065 Vector4( 0.0f, 0.0f, ( ( n2 * inv_nf ) * zFar ), 0.0f )
1071 float sum_rl, sum_tb, sum_nf, inv_rl, inv_tb, inv_nf;
1072 sum_rl = ( right + left );
1073 sum_tb = ( top + bottom );
1074 sum_nf = ( zNear + zFar );
1075 inv_rl = ( 1.0f / ( right - left ) );
1076 inv_tb = ( 1.0f / ( top - bottom ) );
1077 inv_nf = ( 1.0f / ( zNear - zFar ) );
1079 Vector4( ( inv_rl + inv_rl ), 0.0f, 0.0f, 0.0f ),
1080 Vector4( 0.0f, ( inv_tb + inv_tb ), 0.0f, 0.0f ),
1081 Vector4( 0.0f, 0.0f, ( inv_nf + inv_nf ), 0.0f ),
1082 Vector4( ( -sum_rl * inv_rl ), ( -sum_tb * inv_tb ), ( sum_nf * inv_nf ), 1.0f )
1096 #ifdef _VECTORMATH_DEBUG
1098 inline void print(
const Matrix4 & mat )
1100 print( mat.getRow( 0 ) );
1101 print( mat.getRow( 1 ) );
1102 print( mat.getRow( 2 ) );
1103 print( mat.getRow( 3 ) );
1106 inline void print(
const Matrix4 & mat,
const char * name )
1108 printf(
"%s:\n", name);
1176 *(&
mCol0 + col) = vec;
1192 tmpV3_0 = this->
getCol( col );
1194 this->
setCol( col, tmpV3_0 );
1225 return *(&
mCol0 + col);
1235 return *(&
mCol0 + col);
1240 return *(&
mCol0 + col);
1254 Vector3 tmp0, tmp1, tmp2, inv0, inv1, inv2;
1259 detinv = ( 1.0f /
dot( tfrm.
getCol2(), tmp2 ) );
1260 inv0 =
Vector3( ( tmp0.
getX() * detinv ), ( tmp1.getX() * detinv ), ( tmp2.getX() * detinv ) );
1261 inv1 =
Vector3( ( tmp0.
getY() * detinv ), ( tmp1.getY() * detinv ), ( tmp2.getY() * detinv ) );
1262 inv2 =
Vector3( ( tmp0.
getZ() * detinv ), ( tmp1.getZ() * detinv ), ( tmp2.getZ() * detinv ) );
1316 ( *
this * tfrm.
mCol0 ),
1317 ( *
this * tfrm.
mCol1 ),
1318 ( *
this * tfrm.
mCol2 ),
1325 *
this = *
this * tfrm;
1364 mCol3 = translateVec;
1376 s = sinf( radians );
1377 c = cosf( radians );
1389 s = sinf( radians );
1390 c = cosf( radians );
1402 s = sinf( radians );
1403 c = cosf( radians );
1414 float sX, cX, sY, cY, sZ, cZ, tmp0, tmp1;
1415 sX = sinf( radiansXYZ.
getX() );
1416 cX = cosf( radiansXYZ.
getX() );
1417 sY = sinf( radiansXYZ.
getY() );
1418 cY = cosf( radiansXYZ.
getY() );
1419 sZ = sinf( radiansXYZ.
getZ() );
1420 cZ = cosf( radiansXYZ.
getZ() );
1424 Vector3( ( cZ * cY ), ( sZ * cY ), -sY ),
1425 Vector3( ( ( tmp0 * sX ) - ( sZ * cX ) ), ( ( tmp1 * sX ) + ( cZ * cX ) ), ( cY * sX ) ),
1426 Vector3( ( ( tmp0 * cX ) + ( sZ * sX ) ), ( ( tmp1 * cX ) - ( cZ * sX ) ), ( cY * cX ) ),
1491 #ifdef _VECTORMATH_DEBUG
1493 inline void print(
const Transform3 & tfrm )
1495 print( tfrm.getRow( 0 ) );
1496 print( tfrm.getRow( 1 ) );
1497 print( tfrm.getRow( 2 ) );
1500 inline void print(
const Transform3 & tfrm,
const char * name )
1502 printf(
"%s:\n", name);
1510 float trace, radicand,
scale, xx, yx, zx, xy, yy, zy, xz, yz, zz, tmpx, tmpy, tmpz, tmpw, qx, qy, qz, qw;
1511 int negTrace, ZgtX, ZgtY, YgtX;
1512 int largestXorY, largestYorZ, largestZorX;
1524 trace = ( ( xx + yy ) + zz );
1526 negTrace = ( trace < 0.0f );
1530 largestXorY = ( !ZgtX || !ZgtY ) && negTrace;
1531 largestYorZ = ( YgtX || ZgtX ) && negTrace;
1532 largestZorX = ( ZgtY || !YgtX ) && negTrace;
1550 radicand = ( ( ( xx + yy ) + zz ) + 1.0f );
1551 scale = ( 0.5f * ( 1.0f / sqrtf( radicand ) ) );
1553 tmpx = ( ( zy - yz ) * scale );
1554 tmpy = ( ( xz - zx ) * scale );
1555 tmpz = ( ( yx - xy ) * scale );
1556 tmpw = ( radicand *
scale );
1588 ( tfrm0 * tfrm1.
getX( ) ),
1589 ( tfrm0 * tfrm1.
getY( ) ),
1590 ( tfrm0 * tfrm1.
getZ( ) )
1597 ( tfrm0 * tfrm1.
getX( ) ),
1598 ( tfrm0 * tfrm1.
getY( ) ),
1599 ( tfrm0 * tfrm1.
getZ( ) ),
1600 ( tfrm0 * tfrm1.
getW( ) )
const Quat normalize(const Quat &quat)
Matrix4 & setCol1(const Vector4 &col1)
float determinant(const Matrix3 &mat)
const Vector3 rowMul(const Vector3 &vec, const Matrix3 &mat)
static const Matrix4 scale(const Vector3 &scaleVec)
const Vector3 getXYZ() const
const Matrix3 crossMatrixMul(const Vector3 &vec, const Matrix3 &mat)
static const Vector4 yAxis()
Vector4 & setXYZ(const Vector3 &vec)
static const Matrix4 rotationX(float radians)
static const Matrix4 translation(const Vector3 &translateVec)
static const Vector3 xAxis()
static const Vector3 zAxis()
const Matrix4 operator+(const Matrix4 &mat) const
const Matrix3 operator*(float scalar) const
Matrix3 & setElem(int col, int row, float val)
static const Vector4 xAxis()
const Matrix3 appendScale(const Matrix3 &mat, const Vector3 &scaleVec)
Matrix3 & setCol0(const Vector3 &col0)
Matrix3 & setCol2(const Vector3 &col2)
Matrix3 & operator-=(const Matrix3 &mat)
const Matrix3 inverse(const Matrix3 &mat)
static const Matrix4 rotation(float radians, const Vector3 &unitVec)
static const Matrix4 rotationY(float radians)
const Matrix3 crossMatrix(const Vector3 &vec)
float getElem(int col, int row) const
Matrix4 & operator-=(const Matrix4 &mat)
static const Matrix4 orthographic(float left, float right, float bottom, float top, float zNear, float zFar)
static const Matrix3 rotationZ(float radians)
static const Vector3 yAxis()
static const Matrix3 rotationZYX(const Vector3 &radiansXYZ)
const Matrix4 operator*(float scalar) const
const Vector4 getCol3() const
static const Matrix4 identity()
static const Vector4 wAxis()
static const Matrix3 identity()
const Vector4 getCol1() const
Matrix4 & setTranslation(const Vector3 &translateVec)
const Vector4 getCol0() const
const Matrix4 affineInverse(const Matrix4 &mat)
static const Matrix3 scale(const Vector3 &scaleVec)
const Point3 scale(const Point3 &pnt, float scaleVal)
#define _VECTORMATH_PI_OVER_2
Vector4 & operator[](int col)
const Vector3 getCol0() const
const Matrix3 getUpper3x3() const
Matrix4 & setElem(int col, int row, float val)
const Matrix3 outer(const Vector3 &tfrm0, const Vector3 &tfrm1)
const Matrix4 operator-() const
Matrix4 & operator*=(float scalar)
static const Matrix4 frustum(float left, float right, float bottom, float top, float zNear, float zFar)
Vector3 & operator[](int col)
Vector3 & setElem(int idx, float value)
const Vector3 getTranslation() const
const Matrix3 prependScale(const Vector3 &scaleVec, const Matrix3 &mat)
const Vector4 getCol2() const
const Matrix3 operator+(const Matrix3 &mat) const
Matrix3 & operator+=(const Matrix3 &mat)
const Matrix3 operator-() const
Matrix3 & setCol1(const Vector3 &col1)
static const Matrix3 rotationX(float radians)
const Matrix4 orthoInverse(const Matrix4 &mat)
float getElem(int idx) const
Matrix4 & setCol3(const Vector4 &col3)
static const Matrix3 rotation(float radians, const Vector3 &unitVec)
const Vector4 getCol(int col) const
Matrix4 & setCol(int col, const Vector4 &vec)
Matrix4 & setUpper3x3(const Matrix3 &mat3)
Matrix4 & setCol2(const Vector4 &col2)
static const Matrix4 lookAt(const Point3 &eyePos, const Point3 &lookAtPos, const Vector3 &upVec)
const Matrix3 select(const Matrix3 &mat0, const Matrix3 &mat1, bool select1)
Matrix4 & operator+=(const Matrix4 &mat)
static const Matrix4 perspective(float fovyRadians, float aspect, float zNear, float zFar)
float getElem(int idx) const
static const Matrix3 rotationY(float radians)
const Matrix3 transpose(const Matrix3 &mat)
Matrix3 & operator*=(float scalar)
const Vector3 getRow(int row) const
Matrix4 & setCol0(const Vector4 &col0)
Matrix4 & setRow(int row, const Vector4 &vec)
Matrix3 & setCol(int col, const Vector3 &vec)
float getElem(int col, int row) const
const Vector3 getCol(int col) const
static const Matrix4 rotationZYX(const Vector3 &radiansXYZ)
static const Vector4 zAxis()
float dot(const Quat &quat0, const Quat &quat1)
const Vector3 cross(const Vector3 &vec0, const Vector3 &vec1)
const Matrix3 mulPerElem(const Matrix3 &mat0, const Matrix3 &mat1)
static const Matrix4 rotationZ(float radians)
Matrix3 & operator=(const Matrix3 &mat)
const Vector4 getRow(int row) const
const Vector3 getCol2() const
const Matrix3 absPerElem(const Matrix3 &mat)
Vector4 & setElem(int idx, float value)
const Matrix3 operator*(float scalar, const Matrix3 &mat)
Matrix3 & setRow(int row, const Vector3 &vec)
Matrix4 & operator=(const Matrix4 &mat)
const Vector3 getCol1() const