2 Copyright (C) 2006-2010 Sony Computer Entertainment Inc.
\r
5 Redistribution and use in source and binary forms,
\r
6 with or without modification, are permitted provided that the
\r
7 following conditions are met:
\r
8 * Redistributions of source code must retain the above copyright
\r
9 notice, this list of conditions and the following disclaimer.
\r
10 * Redistributions in binary form must reproduce the above copyright
\r
11 notice, this list of conditions and the following disclaimer in the
\r
12 documentation and/or other materials provided with the distribution.
\r
13 * Neither the name of the Sony Computer Entertainment Inc nor the names
\r
14 of its contributors may be used to endorse or promote products derived
\r
15 from this software without specific prior written permission.
\r
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
\r
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
\r
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
\r
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
\r
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
\r
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
\r
27 POSSIBILITY OF SUCH DAMAGE.
\r
30 #ifndef _VECTORMATH_MAT_AOS_CPP__SCALAR_H
\r
31 #define _VECTORMATH_MAT_AOS_CPP__SCALAR_H
\r
33 namespace Vectormath {
\r
36 //-----------------------------------------------------------------------------
\r
39 #define _VECTORMATH_PI_OVER_2 1.570796327f
\r
41 //-----------------------------------------------------------------------------
\r
44 inline Matrix3::Matrix3( const Matrix3 & mat )
\r
51 inline Matrix3::Matrix3( float scalar )
\r
53 mCol0 = Vector3( scalar );
\r
54 mCol1 = Vector3( scalar );
\r
55 mCol2 = Vector3( scalar );
\r
58 inline Matrix3::Matrix3( const Quat & unitQuat )
\r
60 float qx, qy, qz, qw, qx2, qy2, qz2, qxqx2, qyqy2, qzqz2, qxqy2, qyqz2, qzqw2, qxqz2, qyqw2, qxqw2;
\r
61 qx = unitQuat.getX();
\r
62 qy = unitQuat.getY();
\r
63 qz = unitQuat.getZ();
\r
64 qw = unitQuat.getW();
\r
68 qxqx2 = ( qx * qx2 );
\r
69 qxqy2 = ( qx * qy2 );
\r
70 qxqz2 = ( qx * qz2 );
\r
71 qxqw2 = ( qw * qx2 );
\r
72 qyqy2 = ( qy * qy2 );
\r
73 qyqz2 = ( qy * qz2 );
\r
74 qyqw2 = ( qw * qy2 );
\r
75 qzqz2 = ( qz * qz2 );
\r
76 qzqw2 = ( qw * qz2 );
\r
77 mCol0 = Vector3( ( ( 1.0f - qyqy2 ) - qzqz2 ), ( qxqy2 + qzqw2 ), ( qxqz2 - qyqw2 ) );
\r
78 mCol1 = Vector3( ( qxqy2 - qzqw2 ), ( ( 1.0f - qxqx2 ) - qzqz2 ), ( qyqz2 + qxqw2 ) );
\r
79 mCol2 = Vector3( ( qxqz2 + qyqw2 ), ( qyqz2 - qxqw2 ), ( ( 1.0f - qxqx2 ) - qyqy2 ) );
\r
82 inline Matrix3::Matrix3( const Vector3 & _col0, const Vector3 & _col1, const Vector3 & _col2 )
\r
89 inline Matrix3 & Matrix3::setCol0( const Vector3 & _col0 )
\r
95 inline Matrix3 & Matrix3::setCol1( const Vector3 & _col1 )
\r
101 inline Matrix3 & Matrix3::setCol2( const Vector3 & _col2 )
\r
107 inline Matrix3 & Matrix3::setCol( int col, const Vector3 & vec )
\r
109 *(&mCol0 + col) = vec;
\r
113 inline Matrix3 & Matrix3::setRow( int row, const Vector3 & vec )
\r
115 mCol0.setElem( row, vec.getElem( 0 ) );
\r
116 mCol1.setElem( row, vec.getElem( 1 ) );
\r
117 mCol2.setElem( row, vec.getElem( 2 ) );
\r
121 inline Matrix3 & Matrix3::setElem( int col, int row, float val )
\r
124 tmpV3_0 = this->getCol( col );
\r
125 tmpV3_0.setElem( row, val );
\r
126 this->setCol( col, tmpV3_0 );
\r
130 inline float Matrix3::getElem( int col, int row ) const
\r
132 return this->getCol( col ).getElem( row );
\r
135 inline const Vector3 Matrix3::getCol0( ) const
\r
140 inline const Vector3 Matrix3::getCol1( ) const
\r
145 inline const Vector3 Matrix3::getCol2( ) const
\r
150 inline const Vector3 Matrix3::getCol( int col ) const
\r
152 return *(&mCol0 + col);
\r
155 inline const Vector3 Matrix3::getRow( int row ) const
\r
157 return Vector3( mCol0.getElem( row ), mCol1.getElem( row ), mCol2.getElem( row ) );
\r
160 inline Vector3 & Matrix3::operator []( int col )
\r
162 return *(&mCol0 + col);
\r
165 inline const Vector3 Matrix3::operator []( int col ) const
\r
167 return *(&mCol0 + col);
\r
170 inline Matrix3 & Matrix3::operator =( const Matrix3 & mat )
\r
178 inline const Matrix3 transpose( const Matrix3 & mat )
\r
181 Vector3( mat.getCol0().getX(), mat.getCol1().getX(), mat.getCol2().getX() ),
\r
182 Vector3( mat.getCol0().getY(), mat.getCol1().getY(), mat.getCol2().getY() ),
\r
183 Vector3( mat.getCol0().getZ(), mat.getCol1().getZ(), mat.getCol2().getZ() )
\r
187 inline const Matrix3 inverse( const Matrix3 & mat )
\r
189 Vector3 tmp0, tmp1, tmp2;
\r
191 tmp0 = cross( mat.getCol1(), mat.getCol2() );
\r
192 tmp1 = cross( mat.getCol2(), mat.getCol0() );
\r
193 tmp2 = cross( mat.getCol0(), mat.getCol1() );
\r
194 detinv = ( 1.0f / dot( mat.getCol2(), tmp2 ) );
\r
196 Vector3( ( tmp0.getX() * detinv ), ( tmp1.getX() * detinv ), ( tmp2.getX() * detinv ) ),
\r
197 Vector3( ( tmp0.getY() * detinv ), ( tmp1.getY() * detinv ), ( tmp2.getY() * detinv ) ),
\r
198 Vector3( ( tmp0.getZ() * detinv ), ( tmp1.getZ() * detinv ), ( tmp2.getZ() * detinv ) )
\r
202 inline float determinant( const Matrix3 & mat )
\r
204 return dot( mat.getCol2(), cross( mat.getCol0(), mat.getCol1() ) );
\r
207 inline const Matrix3 Matrix3::operator +( const Matrix3 & mat ) const
\r
210 ( mCol0 + mat.mCol0 ),
\r
211 ( mCol1 + mat.mCol1 ),
\r
212 ( mCol2 + mat.mCol2 )
\r
216 inline const Matrix3 Matrix3::operator -( const Matrix3 & mat ) const
\r
219 ( mCol0 - mat.mCol0 ),
\r
220 ( mCol1 - mat.mCol1 ),
\r
221 ( mCol2 - mat.mCol2 )
\r
225 inline Matrix3 & Matrix3::operator +=( const Matrix3 & mat )
\r
227 *this = *this + mat;
\r
231 inline Matrix3 & Matrix3::operator -=( const Matrix3 & mat )
\r
233 *this = *this - mat;
\r
237 inline const Matrix3 Matrix3::operator -( ) const
\r
246 inline const Matrix3 absPerElem( const Matrix3 & mat )
\r
249 absPerElem( mat.getCol0() ),
\r
250 absPerElem( mat.getCol1() ),
\r
251 absPerElem( mat.getCol2() )
\r
255 inline const Matrix3 Matrix3::operator *( float scalar ) const
\r
258 ( mCol0 * scalar ),
\r
259 ( mCol1 * scalar ),
\r
264 inline Matrix3 & Matrix3::operator *=( float scalar )
\r
266 *this = *this * scalar;
\r
270 inline const Matrix3 operator *( float scalar, const Matrix3 & mat )
\r
272 return mat * scalar;
\r
275 inline const Vector3 Matrix3::operator *( const Vector3 & vec ) const
\r
278 ( ( ( mCol0.getX() * vec.getX() ) + ( mCol1.getX() * vec.getY() ) ) + ( mCol2.getX() * vec.getZ() ) ),
\r
279 ( ( ( mCol0.getY() * vec.getX() ) + ( mCol1.getY() * vec.getY() ) ) + ( mCol2.getY() * vec.getZ() ) ),
\r
280 ( ( ( mCol0.getZ() * vec.getX() ) + ( mCol1.getZ() * vec.getY() ) ) + ( mCol2.getZ() * vec.getZ() ) )
\r
284 inline const Matrix3 Matrix3::operator *( const Matrix3 & mat ) const
\r
287 ( *this * mat.mCol0 ),
\r
288 ( *this * mat.mCol1 ),
\r
289 ( *this * mat.mCol2 )
\r
293 inline Matrix3 & Matrix3::operator *=( const Matrix3 & mat )
\r
295 *this = *this * mat;
\r
299 inline const Matrix3 mulPerElem( const Matrix3 & mat0, const Matrix3 & mat1 )
\r
302 mulPerElem( mat0.getCol0(), mat1.getCol0() ),
\r
303 mulPerElem( mat0.getCol1(), mat1.getCol1() ),
\r
304 mulPerElem( mat0.getCol2(), mat1.getCol2() )
\r
308 inline const Matrix3 Matrix3::identity( )
\r
317 inline const Matrix3 Matrix3::rotationX( float radians )
\r
320 s = sinf( radians );
\r
321 c = cosf( radians );
\r
324 Vector3( 0.0f, c, s ),
\r
325 Vector3( 0.0f, -s, c )
\r
329 inline const Matrix3 Matrix3::rotationY( float radians )
\r
332 s = sinf( radians );
\r
333 c = cosf( radians );
\r
335 Vector3( c, 0.0f, -s ),
\r
337 Vector3( s, 0.0f, c )
\r
341 inline const Matrix3 Matrix3::rotationZ( float radians )
\r
344 s = sinf( radians );
\r
345 c = cosf( radians );
\r
347 Vector3( c, s, 0.0f ),
\r
348 Vector3( -s, c, 0.0f ),
\r
353 inline const Matrix3 Matrix3::rotationZYX( const Vector3 & radiansXYZ )
\r
355 float sX, cX, sY, cY, sZ, cZ, tmp0, tmp1;
\r
356 sX = sinf( radiansXYZ.getX() );
\r
357 cX = cosf( radiansXYZ.getX() );
\r
358 sY = sinf( radiansXYZ.getY() );
\r
359 cY = cosf( radiansXYZ.getY() );
\r
360 sZ = sinf( radiansXYZ.getZ() );
\r
361 cZ = cosf( radiansXYZ.getZ() );
\r
362 tmp0 = ( cZ * sY );
\r
363 tmp1 = ( sZ * sY );
\r
365 Vector3( ( cZ * cY ), ( sZ * cY ), -sY ),
\r
366 Vector3( ( ( tmp0 * sX ) - ( sZ * cX ) ), ( ( tmp1 * sX ) + ( cZ * cX ) ), ( cY * sX ) ),
\r
367 Vector3( ( ( tmp0 * cX ) + ( sZ * sX ) ), ( ( tmp1 * cX ) - ( cZ * sX ) ), ( cY * cX ) )
\r
371 inline const Matrix3 Matrix3::rotation( float radians, const Vector3 & unitVec )
\r
373 float x, y, z, s, c, oneMinusC, xy, yz, zx;
\r
374 s = sinf( radians );
\r
375 c = cosf( radians );
\r
376 x = unitVec.getX();
\r
377 y = unitVec.getY();
\r
378 z = unitVec.getZ();
\r
382 oneMinusC = ( 1.0f - c );
\r
384 Vector3( ( ( ( x * x ) * oneMinusC ) + c ), ( ( xy * oneMinusC ) + ( z * s ) ), ( ( zx * oneMinusC ) - ( y * s ) ) ),
\r
385 Vector3( ( ( xy * oneMinusC ) - ( z * s ) ), ( ( ( y * y ) * oneMinusC ) + c ), ( ( yz * oneMinusC ) + ( x * s ) ) ),
\r
386 Vector3( ( ( zx * oneMinusC ) + ( y * s ) ), ( ( yz * oneMinusC ) - ( x * s ) ), ( ( ( z * z ) * oneMinusC ) + c ) )
\r
390 inline const Matrix3 Matrix3::rotation( const Quat & unitQuat )
\r
392 return Matrix3( unitQuat );
\r
395 inline const Matrix3 Matrix3::scale( const Vector3 & scaleVec )
\r
398 Vector3( scaleVec.getX(), 0.0f, 0.0f ),
\r
399 Vector3( 0.0f, scaleVec.getY(), 0.0f ),
\r
400 Vector3( 0.0f, 0.0f, scaleVec.getZ() )
\r
404 inline const Matrix3 appendScale( const Matrix3 & mat, const Vector3 & scaleVec )
\r
407 ( mat.getCol0() * scaleVec.getX( ) ),
\r
408 ( mat.getCol1() * scaleVec.getY( ) ),
\r
409 ( mat.getCol2() * scaleVec.getZ( ) )
\r
413 inline const Matrix3 prependScale( const Vector3 & scaleVec, const Matrix3 & mat )
\r
416 mulPerElem( mat.getCol0(), scaleVec ),
\r
417 mulPerElem( mat.getCol1(), scaleVec ),
\r
418 mulPerElem( mat.getCol2(), scaleVec )
\r
422 inline const Matrix3 select( const Matrix3 & mat0, const Matrix3 & mat1, bool select1 )
\r
425 select( mat0.getCol0(), mat1.getCol0(), select1 ),
\r
426 select( mat0.getCol1(), mat1.getCol1(), select1 ),
\r
427 select( mat0.getCol2(), mat1.getCol2(), select1 )
\r
431 #ifdef _VECTORMATH_DEBUG
\r
433 inline void print( const Matrix3 & mat )
\r
435 print( mat.getRow( 0 ) );
\r
436 print( mat.getRow( 1 ) );
\r
437 print( mat.getRow( 2 ) );
\r
440 inline void print( const Matrix3 & mat, const char * name )
\r
442 printf("%s:\n", name);
\r
448 inline Matrix4::Matrix4( const Matrix4 & mat )
\r
456 inline Matrix4::Matrix4( float scalar )
\r
458 mCol0 = Vector4( scalar );
\r
459 mCol1 = Vector4( scalar );
\r
460 mCol2 = Vector4( scalar );
\r
461 mCol3 = Vector4( scalar );
\r
464 inline Matrix4::Matrix4( const Transform3 & mat )
\r
466 mCol0 = Vector4( mat.getCol0(), 0.0f );
\r
467 mCol1 = Vector4( mat.getCol1(), 0.0f );
\r
468 mCol2 = Vector4( mat.getCol2(), 0.0f );
\r
469 mCol3 = Vector4( mat.getCol3(), 1.0f );
\r
472 inline Matrix4::Matrix4( const Vector4 & _col0, const Vector4 & _col1, const Vector4 & _col2, const Vector4 & _col3 )
\r
480 inline Matrix4::Matrix4( const Matrix3 & mat, const Vector3 & translateVec )
\r
482 mCol0 = Vector4( mat.getCol0(), 0.0f );
\r
483 mCol1 = Vector4( mat.getCol1(), 0.0f );
\r
484 mCol2 = Vector4( mat.getCol2(), 0.0f );
\r
485 mCol3 = Vector4( translateVec, 1.0f );
\r
488 inline Matrix4::Matrix4( const Quat & unitQuat, const Vector3 & translateVec )
\r
491 mat = Matrix3( unitQuat );
\r
492 mCol0 = Vector4( mat.getCol0(), 0.0f );
\r
493 mCol1 = Vector4( mat.getCol1(), 0.0f );
\r
494 mCol2 = Vector4( mat.getCol2(), 0.0f );
\r
495 mCol3 = Vector4( translateVec, 1.0f );
\r
498 inline Matrix4 & Matrix4::setCol0( const Vector4 & _col0 )
\r
504 inline Matrix4 & Matrix4::setCol1( const Vector4 & _col1 )
\r
510 inline Matrix4 & Matrix4::setCol2( const Vector4 & _col2 )
\r
516 inline Matrix4 & Matrix4::setCol3( const Vector4 & _col3 )
\r
522 inline Matrix4 & Matrix4::setCol( int col, const Vector4 & vec )
\r
524 *(&mCol0 + col) = vec;
\r
528 inline Matrix4 & Matrix4::setRow( int row, const Vector4 & vec )
\r
530 mCol0.setElem( row, vec.getElem( 0 ) );
\r
531 mCol1.setElem( row, vec.getElem( 1 ) );
\r
532 mCol2.setElem( row, vec.getElem( 2 ) );
\r
533 mCol3.setElem( row, vec.getElem( 3 ) );
\r
537 inline Matrix4 & Matrix4::setElem( int col, int row, float val )
\r
540 tmpV3_0 = this->getCol( col );
\r
541 tmpV3_0.setElem( row, val );
\r
542 this->setCol( col, tmpV3_0 );
\r
546 inline float Matrix4::getElem( int col, int row ) const
\r
548 return this->getCol( col ).getElem( row );
\r
551 inline const Vector4 Matrix4::getCol0( ) const
\r
556 inline const Vector4 Matrix4::getCol1( ) const
\r
561 inline const Vector4 Matrix4::getCol2( ) const
\r
566 inline const Vector4 Matrix4::getCol3( ) const
\r
571 inline const Vector4 Matrix4::getCol( int col ) const
\r
573 return *(&mCol0 + col);
\r
576 inline const Vector4 Matrix4::getRow( int row ) const
\r
578 return Vector4( mCol0.getElem( row ), mCol1.getElem( row ), mCol2.getElem( row ), mCol3.getElem( row ) );
\r
581 inline Vector4 & Matrix4::operator []( int col )
\r
583 return *(&mCol0 + col);
\r
586 inline const Vector4 Matrix4::operator []( int col ) const
\r
588 return *(&mCol0 + col);
\r
591 inline Matrix4 & Matrix4::operator =( const Matrix4 & mat )
\r
600 inline const Matrix4 transpose( const Matrix4 & mat )
\r
603 Vector4( mat.getCol0().getX(), mat.getCol1().getX(), mat.getCol2().getX(), mat.getCol3().getX() ),
\r
604 Vector4( mat.getCol0().getY(), mat.getCol1().getY(), mat.getCol2().getY(), mat.getCol3().getY() ),
\r
605 Vector4( mat.getCol0().getZ(), mat.getCol1().getZ(), mat.getCol2().getZ(), mat.getCol3().getZ() ),
\r
606 Vector4( mat.getCol0().getW(), mat.getCol1().getW(), mat.getCol2().getW(), mat.getCol3().getW() )
\r
610 inline const Matrix4 inverse( const Matrix4 & mat )
\r
612 Vector4 res0, res1, res2, res3;
\r
613 float mA, mB, mC, mD, mE, mF, mG, mH, mI, mJ, mK, mL, mM, mN, mO, mP, tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, detInv;
\r
614 mA = mat.getCol0().getX();
\r
615 mB = mat.getCol0().getY();
\r
616 mC = mat.getCol0().getZ();
\r
617 mD = mat.getCol0().getW();
\r
618 mE = mat.getCol1().getX();
\r
619 mF = mat.getCol1().getY();
\r
620 mG = mat.getCol1().getZ();
\r
621 mH = mat.getCol1().getW();
\r
622 mI = mat.getCol2().getX();
\r
623 mJ = mat.getCol2().getY();
\r
624 mK = mat.getCol2().getZ();
\r
625 mL = mat.getCol2().getW();
\r
626 mM = mat.getCol3().getX();
\r
627 mN = mat.getCol3().getY();
\r
628 mO = mat.getCol3().getZ();
\r
629 mP = mat.getCol3().getW();
\r
630 tmp0 = ( ( mK * mD ) - ( mC * mL ) );
\r
631 tmp1 = ( ( mO * mH ) - ( mG * mP ) );
\r
632 tmp2 = ( ( mB * mK ) - ( mJ * mC ) );
\r
633 tmp3 = ( ( mF * mO ) - ( mN * mG ) );
\r
634 tmp4 = ( ( mJ * mD ) - ( mB * mL ) );
\r
635 tmp5 = ( ( mN * mH ) - ( mF * mP ) );
\r
636 res0.setX( ( ( ( mJ * tmp1 ) - ( mL * tmp3 ) ) - ( mK * tmp5 ) ) );
\r
637 res0.setY( ( ( ( mN * tmp0 ) - ( mP * tmp2 ) ) - ( mO * tmp4 ) ) );
\r
638 res0.setZ( ( ( ( mD * tmp3 ) + ( mC * tmp5 ) ) - ( mB * tmp1 ) ) );
\r
639 res0.setW( ( ( ( mH * tmp2 ) + ( mG * tmp4 ) ) - ( mF * tmp0 ) ) );
\r
640 detInv = ( 1.0f / ( ( ( ( mA * res0.getX() ) + ( mE * res0.getY() ) ) + ( mI * res0.getZ() ) ) + ( mM * res0.getW() ) ) );
\r
641 res1.setX( ( mI * tmp1 ) );
\r
642 res1.setY( ( mM * tmp0 ) );
\r
643 res1.setZ( ( mA * tmp1 ) );
\r
644 res1.setW( ( mE * tmp0 ) );
\r
645 res3.setX( ( mI * tmp3 ) );
\r
646 res3.setY( ( mM * tmp2 ) );
\r
647 res3.setZ( ( mA * tmp3 ) );
\r
648 res3.setW( ( mE * tmp2 ) );
\r
649 res2.setX( ( mI * tmp5 ) );
\r
650 res2.setY( ( mM * tmp4 ) );
\r
651 res2.setZ( ( mA * tmp5 ) );
\r
652 res2.setW( ( mE * tmp4 ) );
\r
653 tmp0 = ( ( mI * mB ) - ( mA * mJ ) );
\r
654 tmp1 = ( ( mM * mF ) - ( mE * mN ) );
\r
655 tmp2 = ( ( mI * mD ) - ( mA * mL ) );
\r
656 tmp3 = ( ( mM * mH ) - ( mE * mP ) );
\r
657 tmp4 = ( ( mI * mC ) - ( mA * mK ) );
\r
658 tmp5 = ( ( mM * mG ) - ( mE * mO ) );
\r
659 res2.setX( ( ( ( mL * tmp1 ) - ( mJ * tmp3 ) ) + res2.getX() ) );
\r
660 res2.setY( ( ( ( mP * tmp0 ) - ( mN * tmp2 ) ) + res2.getY() ) );
\r
661 res2.setZ( ( ( ( mB * tmp3 ) - ( mD * tmp1 ) ) - res2.getZ() ) );
\r
662 res2.setW( ( ( ( mF * tmp2 ) - ( mH * tmp0 ) ) - res2.getW() ) );
\r
663 res3.setX( ( ( ( mJ * tmp5 ) - ( mK * tmp1 ) ) + res3.getX() ) );
\r
664 res3.setY( ( ( ( mN * tmp4 ) - ( mO * tmp0 ) ) + res3.getY() ) );
\r
665 res3.setZ( ( ( ( mC * tmp1 ) - ( mB * tmp5 ) ) - res3.getZ() ) );
\r
666 res3.setW( ( ( ( mG * tmp0 ) - ( mF * tmp4 ) ) - res3.getW() ) );
\r
667 res1.setX( ( ( ( mK * tmp3 ) - ( mL * tmp5 ) ) - res1.getX() ) );
\r
668 res1.setY( ( ( ( mO * tmp2 ) - ( mP * tmp4 ) ) - res1.getY() ) );
\r
669 res1.setZ( ( ( ( mD * tmp5 ) - ( mC * tmp3 ) ) + res1.getZ() ) );
\r
670 res1.setW( ( ( ( mH * tmp4 ) - ( mG * tmp2 ) ) + res1.getW() ) );
\r
679 inline const Matrix4 affineInverse( const Matrix4 & mat )
\r
681 Transform3 affineMat;
\r
682 affineMat.setCol0( mat.getCol0().getXYZ( ) );
\r
683 affineMat.setCol1( mat.getCol1().getXYZ( ) );
\r
684 affineMat.setCol2( mat.getCol2().getXYZ( ) );
\r
685 affineMat.setCol3( mat.getCol3().getXYZ( ) );
\r
686 return Matrix4( inverse( affineMat ) );
\r
689 inline const Matrix4 orthoInverse( const Matrix4 & mat )
\r
691 Transform3 affineMat;
\r
692 affineMat.setCol0( mat.getCol0().getXYZ( ) );
\r
693 affineMat.setCol1( mat.getCol1().getXYZ( ) );
\r
694 affineMat.setCol2( mat.getCol2().getXYZ( ) );
\r
695 affineMat.setCol3( mat.getCol3().getXYZ( ) );
\r
696 return Matrix4( orthoInverse( affineMat ) );
\r
699 inline float determinant( const Matrix4 & mat )
\r
701 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;
\r
702 mA = mat.getCol0().getX();
\r
703 mB = mat.getCol0().getY();
\r
704 mC = mat.getCol0().getZ();
\r
705 mD = mat.getCol0().getW();
\r
706 mE = mat.getCol1().getX();
\r
707 mF = mat.getCol1().getY();
\r
708 mG = mat.getCol1().getZ();
\r
709 mH = mat.getCol1().getW();
\r
710 mI = mat.getCol2().getX();
\r
711 mJ = mat.getCol2().getY();
\r
712 mK = mat.getCol2().getZ();
\r
713 mL = mat.getCol2().getW();
\r
714 mM = mat.getCol3().getX();
\r
715 mN = mat.getCol3().getY();
\r
716 mO = mat.getCol3().getZ();
\r
717 mP = mat.getCol3().getW();
\r
718 tmp0 = ( ( mK * mD ) - ( mC * mL ) );
\r
719 tmp1 = ( ( mO * mH ) - ( mG * mP ) );
\r
720 tmp2 = ( ( mB * mK ) - ( mJ * mC ) );
\r
721 tmp3 = ( ( mF * mO ) - ( mN * mG ) );
\r
722 tmp4 = ( ( mJ * mD ) - ( mB * mL ) );
\r
723 tmp5 = ( ( mN * mH ) - ( mF * mP ) );
\r
724 dx = ( ( ( mJ * tmp1 ) - ( mL * tmp3 ) ) - ( mK * tmp5 ) );
\r
725 dy = ( ( ( mN * tmp0 ) - ( mP * tmp2 ) ) - ( mO * tmp4 ) );
\r
726 dz = ( ( ( mD * tmp3 ) + ( mC * tmp5 ) ) - ( mB * tmp1 ) );
\r
727 dw = ( ( ( mH * tmp2 ) + ( mG * tmp4 ) ) - ( mF * tmp0 ) );
\r
728 return ( ( ( ( mA * dx ) + ( mE * dy ) ) + ( mI * dz ) ) + ( mM * dw ) );
\r
731 inline const Matrix4 Matrix4::operator +( const Matrix4 & mat ) const
\r
734 ( mCol0 + mat.mCol0 ),
\r
735 ( mCol1 + mat.mCol1 ),
\r
736 ( mCol2 + mat.mCol2 ),
\r
737 ( mCol3 + mat.mCol3 )
\r
741 inline const Matrix4 Matrix4::operator -( const Matrix4 & mat ) const
\r
744 ( mCol0 - mat.mCol0 ),
\r
745 ( mCol1 - mat.mCol1 ),
\r
746 ( mCol2 - mat.mCol2 ),
\r
747 ( mCol3 - mat.mCol3 )
\r
751 inline Matrix4 & Matrix4::operator +=( const Matrix4 & mat )
\r
753 *this = *this + mat;
\r
757 inline Matrix4 & Matrix4::operator -=( const Matrix4 & mat )
\r
759 *this = *this - mat;
\r
763 inline const Matrix4 Matrix4::operator -( ) const
\r
773 inline const Matrix4 absPerElem( const Matrix4 & mat )
\r
776 absPerElem( mat.getCol0() ),
\r
777 absPerElem( mat.getCol1() ),
\r
778 absPerElem( mat.getCol2() ),
\r
779 absPerElem( mat.getCol3() )
\r
783 inline const Matrix4 Matrix4::operator *( float scalar ) const
\r
786 ( mCol0 * scalar ),
\r
787 ( mCol1 * scalar ),
\r
788 ( mCol2 * scalar ),
\r
793 inline Matrix4 & Matrix4::operator *=( float scalar )
\r
795 *this = *this * scalar;
\r
799 inline const Matrix4 operator *( float scalar, const Matrix4 & mat )
\r
801 return mat * scalar;
\r
804 inline const Vector4 Matrix4::operator *( const Vector4 & vec ) const
\r
807 ( ( ( ( mCol0.getX() * vec.getX() ) + ( mCol1.getX() * vec.getY() ) ) + ( mCol2.getX() * vec.getZ() ) ) + ( mCol3.getX() * vec.getW() ) ),
\r
808 ( ( ( ( mCol0.getY() * vec.getX() ) + ( mCol1.getY() * vec.getY() ) ) + ( mCol2.getY() * vec.getZ() ) ) + ( mCol3.getY() * vec.getW() ) ),
\r
809 ( ( ( ( mCol0.getZ() * vec.getX() ) + ( mCol1.getZ() * vec.getY() ) ) + ( mCol2.getZ() * vec.getZ() ) ) + ( mCol3.getZ() * vec.getW() ) ),
\r
810 ( ( ( ( mCol0.getW() * vec.getX() ) + ( mCol1.getW() * vec.getY() ) ) + ( mCol2.getW() * vec.getZ() ) ) + ( mCol3.getW() * vec.getW() ) )
\r
814 inline const Vector4 Matrix4::operator *( const Vector3 & vec ) const
\r
817 ( ( ( mCol0.getX() * vec.getX() ) + ( mCol1.getX() * vec.getY() ) ) + ( mCol2.getX() * vec.getZ() ) ),
\r
818 ( ( ( mCol0.getY() * vec.getX() ) + ( mCol1.getY() * vec.getY() ) ) + ( mCol2.getY() * vec.getZ() ) ),
\r
819 ( ( ( mCol0.getZ() * vec.getX() ) + ( mCol1.getZ() * vec.getY() ) ) + ( mCol2.getZ() * vec.getZ() ) ),
\r
820 ( ( ( mCol0.getW() * vec.getX() ) + ( mCol1.getW() * vec.getY() ) ) + ( mCol2.getW() * vec.getZ() ) )
\r
824 inline const Vector4 Matrix4::operator *( const Point3 & pnt ) const
\r
827 ( ( ( ( mCol0.getX() * pnt.getX() ) + ( mCol1.getX() * pnt.getY() ) ) + ( mCol2.getX() * pnt.getZ() ) ) + mCol3.getX() ),
\r
828 ( ( ( ( mCol0.getY() * pnt.getX() ) + ( mCol1.getY() * pnt.getY() ) ) + ( mCol2.getY() * pnt.getZ() ) ) + mCol3.getY() ),
\r
829 ( ( ( ( mCol0.getZ() * pnt.getX() ) + ( mCol1.getZ() * pnt.getY() ) ) + ( mCol2.getZ() * pnt.getZ() ) ) + mCol3.getZ() ),
\r
830 ( ( ( ( mCol0.getW() * pnt.getX() ) + ( mCol1.getW() * pnt.getY() ) ) + ( mCol2.getW() * pnt.getZ() ) ) + mCol3.getW() )
\r
834 inline const Matrix4 Matrix4::operator *( const Matrix4 & mat ) const
\r
837 ( *this * mat.mCol0 ),
\r
838 ( *this * mat.mCol1 ),
\r
839 ( *this * mat.mCol2 ),
\r
840 ( *this * mat.mCol3 )
\r
844 inline Matrix4 & Matrix4::operator *=( const Matrix4 & mat )
\r
846 *this = *this * mat;
\r
850 inline const Matrix4 Matrix4::operator *( const Transform3 & tfrm ) const
\r
853 ( *this * tfrm.getCol0() ),
\r
854 ( *this * tfrm.getCol1() ),
\r
855 ( *this * tfrm.getCol2() ),
\r
856 ( *this * Point3( tfrm.getCol3() ) )
\r
860 inline Matrix4 & Matrix4::operator *=( const Transform3 & tfrm )
\r
862 *this = *this * tfrm;
\r
866 inline const Matrix4 mulPerElem( const Matrix4 & mat0, const Matrix4 & mat1 )
\r
869 mulPerElem( mat0.getCol0(), mat1.getCol0() ),
\r
870 mulPerElem( mat0.getCol1(), mat1.getCol1() ),
\r
871 mulPerElem( mat0.getCol2(), mat1.getCol2() ),
\r
872 mulPerElem( mat0.getCol3(), mat1.getCol3() )
\r
876 inline const Matrix4 Matrix4::identity( )
\r
886 inline Matrix4 & Matrix4::setUpper3x3( const Matrix3 & mat3 )
\r
888 mCol0.setXYZ( mat3.getCol0() );
\r
889 mCol1.setXYZ( mat3.getCol1() );
\r
890 mCol2.setXYZ( mat3.getCol2() );
\r
894 inline const Matrix3 Matrix4::getUpper3x3( ) const
\r
903 inline Matrix4 & Matrix4::setTranslation( const Vector3 & translateVec )
\r
905 mCol3.setXYZ( translateVec );
\r
909 inline const Vector3 Matrix4::getTranslation( ) const
\r
911 return mCol3.getXYZ( );
\r
914 inline const Matrix4 Matrix4::rotationX( float radians )
\r
917 s = sinf( radians );
\r
918 c = cosf( radians );
\r
921 Vector4( 0.0f, c, s, 0.0f ),
\r
922 Vector4( 0.0f, -s, c, 0.0f ),
\r
927 inline const Matrix4 Matrix4::rotationY( float radians )
\r
930 s = sinf( radians );
\r
931 c = cosf( radians );
\r
933 Vector4( c, 0.0f, -s, 0.0f ),
\r
935 Vector4( s, 0.0f, c, 0.0f ),
\r
940 inline const Matrix4 Matrix4::rotationZ( float radians )
\r
943 s = sinf( radians );
\r
944 c = cosf( radians );
\r
946 Vector4( c, s, 0.0f, 0.0f ),
\r
947 Vector4( -s, c, 0.0f, 0.0f ),
\r
953 inline const Matrix4 Matrix4::rotationZYX( const Vector3 & radiansXYZ )
\r
955 float sX, cX, sY, cY, sZ, cZ, tmp0, tmp1;
\r
956 sX = sinf( radiansXYZ.getX() );
\r
957 cX = cosf( radiansXYZ.getX() );
\r
958 sY = sinf( radiansXYZ.getY() );
\r
959 cY = cosf( radiansXYZ.getY() );
\r
960 sZ = sinf( radiansXYZ.getZ() );
\r
961 cZ = cosf( radiansXYZ.getZ() );
\r
962 tmp0 = ( cZ * sY );
\r
963 tmp1 = ( sZ * sY );
\r
965 Vector4( ( cZ * cY ), ( sZ * cY ), -sY, 0.0f ),
\r
966 Vector4( ( ( tmp0 * sX ) - ( sZ * cX ) ), ( ( tmp1 * sX ) + ( cZ * cX ) ), ( cY * sX ), 0.0f ),
\r
967 Vector4( ( ( tmp0 * cX ) + ( sZ * sX ) ), ( ( tmp1 * cX ) - ( cZ * sX ) ), ( cY * cX ), 0.0f ),
\r
972 inline const Matrix4 Matrix4::rotation( float radians, const Vector3 & unitVec )
\r
974 float x, y, z, s, c, oneMinusC, xy, yz, zx;
\r
975 s = sinf( radians );
\r
976 c = cosf( radians );
\r
977 x = unitVec.getX();
\r
978 y = unitVec.getY();
\r
979 z = unitVec.getZ();
\r
983 oneMinusC = ( 1.0f - c );
\r
985 Vector4( ( ( ( x * x ) * oneMinusC ) + c ), ( ( xy * oneMinusC ) + ( z * s ) ), ( ( zx * oneMinusC ) - ( y * s ) ), 0.0f ),
\r
986 Vector4( ( ( xy * oneMinusC ) - ( z * s ) ), ( ( ( y * y ) * oneMinusC ) + c ), ( ( yz * oneMinusC ) + ( x * s ) ), 0.0f ),
\r
987 Vector4( ( ( zx * oneMinusC ) + ( y * s ) ), ( ( yz * oneMinusC ) - ( x * s ) ), ( ( ( z * z ) * oneMinusC ) + c ), 0.0f ),
\r
992 inline const Matrix4 Matrix4::rotation( const Quat & unitQuat )
\r
994 return Matrix4( Transform3::rotation( unitQuat ) );
\r
997 inline const Matrix4 Matrix4::scale( const Vector3 & scaleVec )
\r
1000 Vector4( scaleVec.getX(), 0.0f, 0.0f, 0.0f ),
\r
1001 Vector4( 0.0f, scaleVec.getY(), 0.0f, 0.0f ),
\r
1002 Vector4( 0.0f, 0.0f, scaleVec.getZ(), 0.0f ),
\r
1007 inline const Matrix4 appendScale( const Matrix4 & mat, const Vector3 & scaleVec )
\r
1010 ( mat.getCol0() * scaleVec.getX( ) ),
\r
1011 ( mat.getCol1() * scaleVec.getY( ) ),
\r
1012 ( mat.getCol2() * scaleVec.getZ( ) ),
\r
1017 inline const Matrix4 prependScale( const Vector3 & scaleVec, const Matrix4 & mat )
\r
1020 scale4 = Vector4( scaleVec, 1.0f );
\r
1022 mulPerElem( mat.getCol0(), scale4 ),
\r
1023 mulPerElem( mat.getCol1(), scale4 ),
\r
1024 mulPerElem( mat.getCol2(), scale4 ),
\r
1025 mulPerElem( mat.getCol3(), scale4 )
\r
1029 inline const Matrix4 Matrix4::translation( const Vector3 & translateVec )
\r
1032 Vector4::xAxis( ),
\r
1033 Vector4::yAxis( ),
\r
1034 Vector4::zAxis( ),
\r
1035 Vector4( translateVec, 1.0f )
\r
1039 inline const Matrix4 Matrix4::lookAt( const Point3 & eyePos, const Point3 & lookAtPos, const Vector3 & upVec )
\r
1041 Matrix4 m4EyeFrame;
\r
1042 Vector3 v3X, v3Y, v3Z;
\r
1043 v3Y = normalize( upVec );
\r
1044 v3Z = normalize( ( eyePos - lookAtPos ) );
\r
1045 v3X = normalize( cross( v3Y, v3Z ) );
\r
1046 v3Y = cross( v3Z, v3X );
\r
1047 m4EyeFrame = Matrix4( Vector4( v3X ), Vector4( v3Y ), Vector4( v3Z ), Vector4( eyePos ) );
\r
1048 return orthoInverse( m4EyeFrame );
\r
1051 inline const Matrix4 Matrix4::perspective( float fovyRadians, float aspect, float zNear, float zFar )
\r
1053 float f, rangeInv;
\r
1054 f = tanf( ( (float)( _VECTORMATH_PI_OVER_2 ) - ( 0.5f * fovyRadians ) ) );
\r
1055 rangeInv = ( 1.0f / ( zNear - zFar ) );
\r
1057 Vector4( ( f / aspect ), 0.0f, 0.0f, 0.0f ),
\r
1058 Vector4( 0.0f, f, 0.0f, 0.0f ),
\r
1059 Vector4( 0.0f, 0.0f, ( ( zNear + zFar ) * rangeInv ), -1.0f ),
\r
1060 Vector4( 0.0f, 0.0f, ( ( ( zNear * zFar ) * rangeInv ) * 2.0f ), 0.0f )
\r
1064 inline const Matrix4 Matrix4::frustum( float left, float right, float bottom, float top, float zNear, float zFar )
\r
1066 float sum_rl, sum_tb, sum_nf, inv_rl, inv_tb, inv_nf, n2;
\r
1067 sum_rl = ( right + left );
\r
1068 sum_tb = ( top + bottom );
\r
1069 sum_nf = ( zNear + zFar );
\r
1070 inv_rl = ( 1.0f / ( right - left ) );
\r
1071 inv_tb = ( 1.0f / ( top - bottom ) );
\r
1072 inv_nf = ( 1.0f / ( zNear - zFar ) );
\r
1073 n2 = ( zNear + zNear );
\r
1075 Vector4( ( n2 * inv_rl ), 0.0f, 0.0f, 0.0f ),
\r
1076 Vector4( 0.0f, ( n2 * inv_tb ), 0.0f, 0.0f ),
\r
1077 Vector4( ( sum_rl * inv_rl ), ( sum_tb * inv_tb ), ( sum_nf * inv_nf ), -1.0f ),
\r
1078 Vector4( 0.0f, 0.0f, ( ( n2 * inv_nf ) * zFar ), 0.0f )
\r
1082 inline const Matrix4 Matrix4::orthographic( float left, float right, float bottom, float top, float zNear, float zFar )
\r
1084 float sum_rl, sum_tb, sum_nf, inv_rl, inv_tb, inv_nf;
\r
1085 sum_rl = ( right + left );
\r
1086 sum_tb = ( top + bottom );
\r
1087 sum_nf = ( zNear + zFar );
\r
1088 inv_rl = ( 1.0f / ( right - left ) );
\r
1089 inv_tb = ( 1.0f / ( top - bottom ) );
\r
1090 inv_nf = ( 1.0f / ( zNear - zFar ) );
\r
1092 Vector4( ( inv_rl + inv_rl ), 0.0f, 0.0f, 0.0f ),
\r
1093 Vector4( 0.0f, ( inv_tb + inv_tb ), 0.0f, 0.0f ),
\r
1094 Vector4( 0.0f, 0.0f, ( inv_nf + inv_nf ), 0.0f ),
\r
1095 Vector4( ( -sum_rl * inv_rl ), ( -sum_tb * inv_tb ), ( sum_nf * inv_nf ), 1.0f )
\r
1099 inline const Matrix4 select( const Matrix4 & mat0, const Matrix4 & mat1, bool select1 )
\r
1102 select( mat0.getCol0(), mat1.getCol0(), select1 ),
\r
1103 select( mat0.getCol1(), mat1.getCol1(), select1 ),
\r
1104 select( mat0.getCol2(), mat1.getCol2(), select1 ),
\r
1105 select( mat0.getCol3(), mat1.getCol3(), select1 )
\r
1109 #ifdef _VECTORMATH_DEBUG
\r
1111 inline void print( const Matrix4 & mat )
\r
1113 print( mat.getRow( 0 ) );
\r
1114 print( mat.getRow( 1 ) );
\r
1115 print( mat.getRow( 2 ) );
\r
1116 print( mat.getRow( 3 ) );
\r
1119 inline void print( const Matrix4 & mat, const char * name )
\r
1121 printf("%s:\n", name);
\r
1127 inline Transform3::Transform3( const Transform3 & tfrm )
\r
1129 mCol0 = tfrm.mCol0;
\r
1130 mCol1 = tfrm.mCol1;
\r
1131 mCol2 = tfrm.mCol2;
\r
1132 mCol3 = tfrm.mCol3;
\r
1135 inline Transform3::Transform3( float scalar )
\r
1137 mCol0 = Vector3( scalar );
\r
1138 mCol1 = Vector3( scalar );
\r
1139 mCol2 = Vector3( scalar );
\r
1140 mCol3 = Vector3( scalar );
\r
1143 inline Transform3::Transform3( const Vector3 & _col0, const Vector3 & _col1, const Vector3 & _col2, const Vector3 & _col3 )
\r
1151 inline Transform3::Transform3( const Matrix3 & tfrm, const Vector3 & translateVec )
\r
1153 this->setUpper3x3( tfrm );
\r
1154 this->setTranslation( translateVec );
\r
1157 inline Transform3::Transform3( const Quat & unitQuat, const Vector3 & translateVec )
\r
1159 this->setUpper3x3( Matrix3( unitQuat ) );
\r
1160 this->setTranslation( translateVec );
\r
1163 inline Transform3 & Transform3::setCol0( const Vector3 & _col0 )
\r
1169 inline Transform3 & Transform3::setCol1( const Vector3 & _col1 )
\r
1175 inline Transform3 & Transform3::setCol2( const Vector3 & _col2 )
\r
1181 inline Transform3 & Transform3::setCol3( const Vector3 & _col3 )
\r
1187 inline Transform3 & Transform3::setCol( int col, const Vector3 & vec )
\r
1189 *(&mCol0 + col) = vec;
\r
1193 inline Transform3 & Transform3::setRow( int row, const Vector4 & vec )
\r
1195 mCol0.setElem( row, vec.getElem( 0 ) );
\r
1196 mCol1.setElem( row, vec.getElem( 1 ) );
\r
1197 mCol2.setElem( row, vec.getElem( 2 ) );
\r
1198 mCol3.setElem( row, vec.getElem( 3 ) );
\r
1202 inline Transform3 & Transform3::setElem( int col, int row, float val )
\r
1205 tmpV3_0 = this->getCol( col );
\r
1206 tmpV3_0.setElem( row, val );
\r
1207 this->setCol( col, tmpV3_0 );
\r
1211 inline float Transform3::getElem( int col, int row ) const
\r
1213 return this->getCol( col ).getElem( row );
\r
1216 inline const Vector3 Transform3::getCol0( ) const
\r
1221 inline const Vector3 Transform3::getCol1( ) const
\r
1226 inline const Vector3 Transform3::getCol2( ) const
\r
1231 inline const Vector3 Transform3::getCol3( ) const
\r
1236 inline const Vector3 Transform3::getCol( int col ) const
\r
1238 return *(&mCol0 + col);
\r
1241 inline const Vector4 Transform3::getRow( int row ) const
\r
1243 return Vector4( mCol0.getElem( row ), mCol1.getElem( row ), mCol2.getElem( row ), mCol3.getElem( row ) );
\r
1246 inline Vector3 & Transform3::operator []( int col )
\r
1248 return *(&mCol0 + col);
\r
1251 inline const Vector3 Transform3::operator []( int col ) const
\r
1253 return *(&mCol0 + col);
\r
1256 inline Transform3 & Transform3::operator =( const Transform3 & tfrm )
\r
1258 mCol0 = tfrm.mCol0;
\r
1259 mCol1 = tfrm.mCol1;
\r
1260 mCol2 = tfrm.mCol2;
\r
1261 mCol3 = tfrm.mCol3;
\r
1265 inline const Transform3 inverse( const Transform3 & tfrm )
\r
1267 Vector3 tmp0, tmp1, tmp2, inv0, inv1, inv2;
\r
1269 tmp0 = cross( tfrm.getCol1(), tfrm.getCol2() );
\r
1270 tmp1 = cross( tfrm.getCol2(), tfrm.getCol0() );
\r
1271 tmp2 = cross( tfrm.getCol0(), tfrm.getCol1() );
\r
1272 detinv = ( 1.0f / dot( tfrm.getCol2(), tmp2 ) );
\r
1273 inv0 = Vector3( ( tmp0.getX() * detinv ), ( tmp1.getX() * detinv ), ( tmp2.getX() * detinv ) );
\r
1274 inv1 = Vector3( ( tmp0.getY() * detinv ), ( tmp1.getY() * detinv ), ( tmp2.getY() * detinv ) );
\r
1275 inv2 = Vector3( ( tmp0.getZ() * detinv ), ( tmp1.getZ() * detinv ), ( tmp2.getZ() * detinv ) );
\r
1276 return Transform3(
\r
1280 Vector3( ( -( ( inv0 * tfrm.getCol3().getX() ) + ( ( inv1 * tfrm.getCol3().getY() ) + ( inv2 * tfrm.getCol3().getZ() ) ) ) ) )
\r
1284 inline const Transform3 orthoInverse( const Transform3 & tfrm )
\r
1286 Vector3 inv0, inv1, inv2;
\r
1287 inv0 = Vector3( tfrm.getCol0().getX(), tfrm.getCol1().getX(), tfrm.getCol2().getX() );
\r
1288 inv1 = Vector3( tfrm.getCol0().getY(), tfrm.getCol1().getY(), tfrm.getCol2().getY() );
\r
1289 inv2 = Vector3( tfrm.getCol0().getZ(), tfrm.getCol1().getZ(), tfrm.getCol2().getZ() );
\r
1290 return Transform3(
\r
1294 Vector3( ( -( ( inv0 * tfrm.getCol3().getX() ) + ( ( inv1 * tfrm.getCol3().getY() ) + ( inv2 * tfrm.getCol3().getZ() ) ) ) ) )
\r
1298 inline const Transform3 absPerElem( const Transform3 & tfrm )
\r
1300 return Transform3(
\r
1301 absPerElem( tfrm.getCol0() ),
\r
1302 absPerElem( tfrm.getCol1() ),
\r
1303 absPerElem( tfrm.getCol2() ),
\r
1304 absPerElem( tfrm.getCol3() )
\r
1308 inline const Vector3 Transform3::operator *( const Vector3 & vec ) const
\r
1311 ( ( ( mCol0.getX() * vec.getX() ) + ( mCol1.getX() * vec.getY() ) ) + ( mCol2.getX() * vec.getZ() ) ),
\r
1312 ( ( ( mCol0.getY() * vec.getX() ) + ( mCol1.getY() * vec.getY() ) ) + ( mCol2.getY() * vec.getZ() ) ),
\r
1313 ( ( ( mCol0.getZ() * vec.getX() ) + ( mCol1.getZ() * vec.getY() ) ) + ( mCol2.getZ() * vec.getZ() ) )
\r
1317 inline const Point3 Transform3::operator *( const Point3 & pnt ) const
\r
1320 ( ( ( ( mCol0.getX() * pnt.getX() ) + ( mCol1.getX() * pnt.getY() ) ) + ( mCol2.getX() * pnt.getZ() ) ) + mCol3.getX() ),
\r
1321 ( ( ( ( mCol0.getY() * pnt.getX() ) + ( mCol1.getY() * pnt.getY() ) ) + ( mCol2.getY() * pnt.getZ() ) ) + mCol3.getY() ),
\r
1322 ( ( ( ( mCol0.getZ() * pnt.getX() ) + ( mCol1.getZ() * pnt.getY() ) ) + ( mCol2.getZ() * pnt.getZ() ) ) + mCol3.getZ() )
\r
1326 inline const Transform3 Transform3::operator *( const Transform3 & tfrm ) const
\r
1328 return Transform3(
\r
1329 ( *this * tfrm.mCol0 ),
\r
1330 ( *this * tfrm.mCol1 ),
\r
1331 ( *this * tfrm.mCol2 ),
\r
1332 Vector3( ( *this * Point3( tfrm.mCol3 ) ) )
\r
1336 inline Transform3 & Transform3::operator *=( const Transform3 & tfrm )
\r
1338 *this = *this * tfrm;
\r
1342 inline const Transform3 mulPerElem( const Transform3 & tfrm0, const Transform3 & tfrm1 )
\r
1344 return Transform3(
\r
1345 mulPerElem( tfrm0.getCol0(), tfrm1.getCol0() ),
\r
1346 mulPerElem( tfrm0.getCol1(), tfrm1.getCol1() ),
\r
1347 mulPerElem( tfrm0.getCol2(), tfrm1.getCol2() ),
\r
1348 mulPerElem( tfrm0.getCol3(), tfrm1.getCol3() )
\r
1352 inline const Transform3 Transform3::identity( )
\r
1354 return Transform3(
\r
1355 Vector3::xAxis( ),
\r
1356 Vector3::yAxis( ),
\r
1357 Vector3::zAxis( ),
\r
1362 inline Transform3 & Transform3::setUpper3x3( const Matrix3 & tfrm )
\r
1364 mCol0 = tfrm.getCol0();
\r
1365 mCol1 = tfrm.getCol1();
\r
1366 mCol2 = tfrm.getCol2();
\r
1370 inline const Matrix3 Transform3::getUpper3x3( ) const
\r
1372 return Matrix3( mCol0, mCol1, mCol2 );
\r
1375 inline Transform3 & Transform3::setTranslation( const Vector3 & translateVec )
\r
1377 mCol3 = translateVec;
\r
1381 inline const Vector3 Transform3::getTranslation( ) const
\r
1386 inline const Transform3 Transform3::rotationX( float radians )
\r
1389 s = sinf( radians );
\r
1390 c = cosf( radians );
\r
1391 return Transform3(
\r
1392 Vector3::xAxis( ),
\r
1393 Vector3( 0.0f, c, s ),
\r
1394 Vector3( 0.0f, -s, c ),
\r
1399 inline const Transform3 Transform3::rotationY( float radians )
\r
1402 s = sinf( radians );
\r
1403 c = cosf( radians );
\r
1404 return Transform3(
\r
1405 Vector3( c, 0.0f, -s ),
\r
1406 Vector3::yAxis( ),
\r
1407 Vector3( s, 0.0f, c ),
\r
1412 inline const Transform3 Transform3::rotationZ( float radians )
\r
1415 s = sinf( radians );
\r
1416 c = cosf( radians );
\r
1417 return Transform3(
\r
1418 Vector3( c, s, 0.0f ),
\r
1419 Vector3( -s, c, 0.0f ),
\r
1420 Vector3::zAxis( ),
\r
1425 inline const Transform3 Transform3::rotationZYX( const Vector3 & radiansXYZ )
\r
1427 float sX, cX, sY, cY, sZ, cZ, tmp0, tmp1;
\r
1428 sX = sinf( radiansXYZ.getX() );
\r
1429 cX = cosf( radiansXYZ.getX() );
\r
1430 sY = sinf( radiansXYZ.getY() );
\r
1431 cY = cosf( radiansXYZ.getY() );
\r
1432 sZ = sinf( radiansXYZ.getZ() );
\r
1433 cZ = cosf( radiansXYZ.getZ() );
\r
1434 tmp0 = ( cZ * sY );
\r
1435 tmp1 = ( sZ * sY );
\r
1436 return Transform3(
\r
1437 Vector3( ( cZ * cY ), ( sZ * cY ), -sY ),
\r
1438 Vector3( ( ( tmp0 * sX ) - ( sZ * cX ) ), ( ( tmp1 * sX ) + ( cZ * cX ) ), ( cY * sX ) ),
\r
1439 Vector3( ( ( tmp0 * cX ) + ( sZ * sX ) ), ( ( tmp1 * cX ) - ( cZ * sX ) ), ( cY * cX ) ),
\r
1444 inline const Transform3 Transform3::rotation( float radians, const Vector3 & unitVec )
\r
1446 return Transform3( Matrix3::rotation( radians, unitVec ), Vector3( 0.0f ) );
\r
1449 inline const Transform3 Transform3::rotation( const Quat & unitQuat )
\r
1451 return Transform3( Matrix3( unitQuat ), Vector3( 0.0f ) );
\r
1454 inline const Transform3 Transform3::scale( const Vector3 & scaleVec )
\r
1456 return Transform3(
\r
1457 Vector3( scaleVec.getX(), 0.0f, 0.0f ),
\r
1458 Vector3( 0.0f, scaleVec.getY(), 0.0f ),
\r
1459 Vector3( 0.0f, 0.0f, scaleVec.getZ() ),
\r
1464 inline const Transform3 appendScale( const Transform3 & tfrm, const Vector3 & scaleVec )
\r
1466 return Transform3(
\r
1467 ( tfrm.getCol0() * scaleVec.getX( ) ),
\r
1468 ( tfrm.getCol1() * scaleVec.getY( ) ),
\r
1469 ( tfrm.getCol2() * scaleVec.getZ( ) ),
\r
1474 inline const Transform3 prependScale( const Vector3 & scaleVec, const Transform3 & tfrm )
\r
1476 return Transform3(
\r
1477 mulPerElem( tfrm.getCol0(), scaleVec ),
\r
1478 mulPerElem( tfrm.getCol1(), scaleVec ),
\r
1479 mulPerElem( tfrm.getCol2(), scaleVec ),
\r
1480 mulPerElem( tfrm.getCol3(), scaleVec )
\r
1484 inline const Transform3 Transform3::translation( const Vector3 & translateVec )
\r
1486 return Transform3(
\r
1487 Vector3::xAxis( ),
\r
1488 Vector3::yAxis( ),
\r
1489 Vector3::zAxis( ),
\r
1494 inline const Transform3 select( const Transform3 & tfrm0, const Transform3 & tfrm1, bool select1 )
\r
1496 return Transform3(
\r
1497 select( tfrm0.getCol0(), tfrm1.getCol0(), select1 ),
\r
1498 select( tfrm0.getCol1(), tfrm1.getCol1(), select1 ),
\r
1499 select( tfrm0.getCol2(), tfrm1.getCol2(), select1 ),
\r
1500 select( tfrm0.getCol3(), tfrm1.getCol3(), select1 )
\r
1504 #ifdef _VECTORMATH_DEBUG
\r
1506 inline void print( const Transform3 & tfrm )
\r
1508 print( tfrm.getRow( 0 ) );
\r
1509 print( tfrm.getRow( 1 ) );
\r
1510 print( tfrm.getRow( 2 ) );
\r
1513 inline void print( const Transform3 & tfrm, const char * name )
\r
1515 printf("%s:\n", name);
\r
1521 inline Quat::Quat( const Matrix3 & tfrm )
\r
1523 float trace, radicand, scale, xx, yx, zx, xy, yy, zy, xz, yz, zz, tmpx, tmpy, tmpz, tmpw, qx, qy, qz, qw;
\r
1524 int negTrace, ZgtX, ZgtY, YgtX;
\r
1525 int largestXorY, largestYorZ, largestZorX;
\r
1527 xx = tfrm.getCol0().getX();
\r
1528 yx = tfrm.getCol0().getY();
\r
1529 zx = tfrm.getCol0().getZ();
\r
1530 xy = tfrm.getCol1().getX();
\r
1531 yy = tfrm.getCol1().getY();
\r
1532 zy = tfrm.getCol1().getZ();
\r
1533 xz = tfrm.getCol2().getX();
\r
1534 yz = tfrm.getCol2().getY();
\r
1535 zz = tfrm.getCol2().getZ();
\r
1537 trace = ( ( xx + yy ) + zz );
\r
1539 negTrace = ( trace < 0.0f );
\r
1543 largestXorY = ( !ZgtX || !ZgtY ) && negTrace;
\r
1544 largestYorZ = ( YgtX || ZgtX ) && negTrace;
\r
1545 largestZorX = ( ZgtY || !YgtX ) && negTrace;
\r
1547 if ( largestXorY )
\r
1552 if ( largestYorZ )
\r
1557 if ( largestZorX )
\r
1563 radicand = ( ( ( xx + yy ) + zz ) + 1.0f );
\r
1564 scale = ( 0.5f * ( 1.0f / sqrtf( radicand ) ) );
\r
1566 tmpx = ( ( zy - yz ) * scale );
\r
1567 tmpy = ( ( xz - zx ) * scale );
\r
1568 tmpz = ( ( yx - xy ) * scale );
\r
1569 tmpw = ( radicand * scale );
\r
1575 if ( largestXorY )
\r
1582 if ( largestYorZ )
\r
1598 inline const Matrix3 outer( const Vector3 & tfrm0, const Vector3 & tfrm1 )
\r
1601 ( tfrm0 * tfrm1.getX( ) ),
\r
1602 ( tfrm0 * tfrm1.getY( ) ),
\r
1603 ( tfrm0 * tfrm1.getZ( ) )
\r
1607 inline const Matrix4 outer( const Vector4 & tfrm0, const Vector4 & tfrm1 )
\r
1610 ( tfrm0 * tfrm1.getX( ) ),
\r
1611 ( tfrm0 * tfrm1.getY( ) ),
\r
1612 ( tfrm0 * tfrm1.getZ( ) ),
\r
1613 ( tfrm0 * tfrm1.getW( ) )
\r
1617 inline const Vector3 rowMul( const Vector3 & vec, const Matrix3 & mat )
\r
1620 ( ( ( vec.getX() * mat.getCol0().getX() ) + ( vec.getY() * mat.getCol0().getY() ) ) + ( vec.getZ() * mat.getCol0().getZ() ) ),
\r
1621 ( ( ( vec.getX() * mat.getCol1().getX() ) + ( vec.getY() * mat.getCol1().getY() ) ) + ( vec.getZ() * mat.getCol1().getZ() ) ),
\r
1622 ( ( ( vec.getX() * mat.getCol2().getX() ) + ( vec.getY() * mat.getCol2().getY() ) ) + ( vec.getZ() * mat.getCol2().getZ() ) )
\r
1626 inline const Matrix3 crossMatrix( const Vector3 & vec )
\r
1629 Vector3( 0.0f, vec.getZ(), -vec.getY() ),
\r
1630 Vector3( -vec.getZ(), 0.0f, vec.getX() ),
\r
1631 Vector3( vec.getY(), -vec.getX(), 0.0f )
\r
1635 inline const Matrix3 crossMatrixMul( const Vector3 & vec, const Matrix3 & mat )
\r
1637 return Matrix3( cross( vec, mat.getCol0() ), cross( vec, mat.getCol1() ), cross( vec, mat.getCol2() ) );
\r
1640 } // namespace Aos
\r
1641 } // namespace Vectormath
\r