12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760 |
- /*
- ===========================================================================
- Doom 3 BFG Edition GPL Source Code
- Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
- This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
- Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
- In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
- If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
- ===========================================================================
- */
- #ifndef __MATH_MATRIX_H__
- #define __MATH_MATRIX_H__
- /*
- ===============================================================================
- Matrix classes, all matrices are row-major except idMat3
- ===============================================================================
- */
- #define MATRIX_INVERSE_EPSILON 1e-14
- #define MATRIX_EPSILON 1e-6
- class idAngles;
- class idQuat;
- class idCQuat;
- class idRotation;
- class idMat4;
- //===============================================================
- //
- // idMat2 - 2x2 matrix
- //
- //===============================================================
- class idMat2 {
- public:
- idMat2();
- explicit idMat2( const idVec2 &x, const idVec2 &y );
- explicit idMat2( const float xx, const float xy, const float yx, const float yy );
- explicit idMat2( const float src[ 2 ][ 2 ] );
- const idVec2 & operator[]( int index ) const;
- idVec2 & operator[]( int index );
- idMat2 operator-() const;
- idMat2 operator*( const float a ) const;
- idVec2 operator*( const idVec2 &vec ) const;
- idMat2 operator*( const idMat2 &a ) const;
- idMat2 operator+( const idMat2 &a ) const;
- idMat2 operator-( const idMat2 &a ) const;
- idMat2 & operator*=( const float a );
- idMat2 & operator*=( const idMat2 &a );
- idMat2 & operator+=( const idMat2 &a );
- idMat2 & operator-=( const idMat2 &a );
- friend idMat2 operator*( const float a, const idMat2 &mat );
- friend idVec2 operator*( const idVec2 &vec, const idMat2 &mat );
- friend idVec2 & operator*=( idVec2 &vec, const idMat2 &mat );
- bool Compare( const idMat2 &a ) const; // exact compare, no epsilon
- bool Compare( const idMat2 &a, const float epsilon ) const; // compare with epsilon
- bool operator==( const idMat2 &a ) const; // exact compare, no epsilon
- bool operator!=( const idMat2 &a ) const; // exact compare, no epsilon
- void Zero();
- void Identity();
- bool IsIdentity( const float epsilon = MATRIX_EPSILON ) const;
- bool IsSymmetric( const float epsilon = MATRIX_EPSILON ) const;
- bool IsDiagonal( const float epsilon = MATRIX_EPSILON ) const;
- float Trace() const;
- float Determinant() const;
- idMat2 Transpose() const; // returns transpose
- idMat2 & TransposeSelf();
- idMat2 Inverse() const; // returns the inverse ( m * m.Inverse() = identity )
- bool InverseSelf(); // returns false if determinant is zero
- idMat2 InverseFast() const; // returns the inverse ( m * m.Inverse() = identity )
- bool InverseFastSelf(); // returns false if determinant is zero
- int GetDimension() const;
- const float * ToFloatPtr() const;
- float * ToFloatPtr();
- const char * ToString( int precision = 2 ) const;
- private:
- idVec2 mat[ 2 ];
- };
- extern idMat2 mat2_zero;
- extern idMat2 mat2_identity;
- #define mat2_default mat2_identity
- ID_INLINE idMat2::idMat2() {
- }
- ID_INLINE idMat2::idMat2( const idVec2 &x, const idVec2 &y ) {
- mat[ 0 ].x = x.x; mat[ 0 ].y = x.y;
- mat[ 1 ].x = y.x; mat[ 1 ].y = y.y;
- }
- ID_INLINE idMat2::idMat2( const float xx, const float xy, const float yx, const float yy ) {
- mat[ 0 ].x = xx; mat[ 0 ].y = xy;
- mat[ 1 ].x = yx; mat[ 1 ].y = yy;
- }
- ID_INLINE idMat2::idMat2( const float src[ 2 ][ 2 ] ) {
- memcpy( mat, src, 2 * 2 * sizeof( float ) );
- }
- ID_INLINE const idVec2 &idMat2::operator[]( int index ) const {
- //assert( ( index >= 0 ) && ( index < 2 ) );
- return mat[ index ];
- }
- ID_INLINE idVec2 &idMat2::operator[]( int index ) {
- //assert( ( index >= 0 ) && ( index < 2 ) );
- return mat[ index ];
- }
- ID_INLINE idMat2 idMat2::operator-() const {
- return idMat2( -mat[0][0], -mat[0][1],
- -mat[1][0], -mat[1][1] );
- }
- ID_INLINE idVec2 idMat2::operator*( const idVec2 &vec ) const {
- return idVec2(
- mat[ 0 ].x * vec.x + mat[ 0 ].y * vec.y,
- mat[ 1 ].x * vec.x + mat[ 1 ].y * vec.y );
- }
- ID_INLINE idMat2 idMat2::operator*( const idMat2 &a ) const {
- return idMat2(
- mat[0].x * a[0].x + mat[0].y * a[1].x,
- mat[0].x * a[0].y + mat[0].y * a[1].y,
- mat[1].x * a[0].x + mat[1].y * a[1].x,
- mat[1].x * a[0].y + mat[1].y * a[1].y );
- }
- ID_INLINE idMat2 idMat2::operator*( const float a ) const {
- return idMat2(
- mat[0].x * a, mat[0].y * a,
- mat[1].x * a, mat[1].y * a );
- }
- ID_INLINE idMat2 idMat2::operator+( const idMat2 &a ) const {
- return idMat2(
- mat[0].x + a[0].x, mat[0].y + a[0].y,
- mat[1].x + a[1].x, mat[1].y + a[1].y );
- }
-
- ID_INLINE idMat2 idMat2::operator-( const idMat2 &a ) const {
- return idMat2(
- mat[0].x - a[0].x, mat[0].y - a[0].y,
- mat[1].x - a[1].x, mat[1].y - a[1].y );
- }
- ID_INLINE idMat2 &idMat2::operator*=( const float a ) {
- mat[0].x *= a; mat[0].y *= a;
- mat[1].x *= a; mat[1].y *= a;
- return *this;
- }
- ID_INLINE idMat2 &idMat2::operator*=( const idMat2 &a ) {
- float x, y;
- x = mat[0].x; y = mat[0].y;
- mat[0].x = x * a[0].x + y * a[1].x;
- mat[0].y = x * a[0].y + y * a[1].y;
- x = mat[1].x; y = mat[1].y;
- mat[1].x = x * a[0].x + y * a[1].x;
- mat[1].y = x * a[0].y + y * a[1].y;
- return *this;
- }
- ID_INLINE idMat2 &idMat2::operator+=( const idMat2 &a ) {
- mat[0].x += a[0].x; mat[0].y += a[0].y;
- mat[1].x += a[1].x; mat[1].y += a[1].y;
- return *this;
- }
- ID_INLINE idMat2 &idMat2::operator-=( const idMat2 &a ) {
- mat[0].x -= a[0].x; mat[0].y -= a[0].y;
- mat[1].x -= a[1].x; mat[1].y -= a[1].y;
- return *this;
- }
- ID_INLINE idVec2 operator*( const idVec2 &vec, const idMat2 &mat ) {
- return mat * vec;
- }
- ID_INLINE idMat2 operator*( const float a, idMat2 const &mat ) {
- return mat * a;
- }
- ID_INLINE idVec2 &operator*=( idVec2 &vec, const idMat2 &mat ) {
- vec = mat * vec;
- return vec;
- }
- ID_INLINE bool idMat2::Compare( const idMat2 &a ) const {
- if ( mat[0].Compare( a[0] ) &&
- mat[1].Compare( a[1] ) ) {
- return true;
- }
- return false;
- }
- ID_INLINE bool idMat2::Compare( const idMat2 &a, const float epsilon ) const {
- if ( mat[0].Compare( a[0], epsilon ) &&
- mat[1].Compare( a[1], epsilon ) ) {
- return true;
- }
- return false;
- }
- ID_INLINE bool idMat2::operator==( const idMat2 &a ) const {
- return Compare( a );
- }
- ID_INLINE bool idMat2::operator!=( const idMat2 &a ) const {
- return !Compare( a );
- }
- ID_INLINE void idMat2::Zero() {
- mat[0].Zero();
- mat[1].Zero();
- }
- ID_INLINE void idMat2::Identity() {
- *this = mat2_identity;
- }
- ID_INLINE bool idMat2::IsIdentity( const float epsilon ) const {
- return Compare( mat2_identity, epsilon );
- }
- ID_INLINE bool idMat2::IsSymmetric( const float epsilon ) const {
- return ( idMath::Fabs( mat[0][1] - mat[1][0] ) < epsilon );
- }
- ID_INLINE bool idMat2::IsDiagonal( const float epsilon ) const {
- if ( idMath::Fabs( mat[0][1] ) > epsilon ||
- idMath::Fabs( mat[1][0] ) > epsilon ) {
- return false;
- }
- return true;
- }
- ID_INLINE float idMat2::Trace() const {
- return ( mat[0][0] + mat[1][1] );
- }
- ID_INLINE float idMat2::Determinant() const {
- return mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];
- }
- ID_INLINE idMat2 idMat2::Transpose() const {
- return idMat2( mat[0][0], mat[1][0],
- mat[0][1], mat[1][1] );
- }
- ID_INLINE idMat2 &idMat2::TransposeSelf() {
- float tmp;
- tmp = mat[0][1];
- mat[0][1] = mat[1][0];
- mat[1][0] = tmp;
- return *this;
- }
- ID_INLINE idMat2 idMat2::Inverse() const {
- idMat2 invMat;
- invMat = *this;
- verify( invMat.InverseSelf() );
- return invMat;
- }
- ID_INLINE idMat2 idMat2::InverseFast() const {
- idMat2 invMat;
- invMat = *this;
- verify( invMat.InverseFastSelf() );
- return invMat;
- }
- ID_INLINE int idMat2::GetDimension() const {
- return 4;
- }
- ID_INLINE const float *idMat2::ToFloatPtr() const {
- return mat[0].ToFloatPtr();
- }
- ID_INLINE float *idMat2::ToFloatPtr() {
- return mat[0].ToFloatPtr();
- }
- //===============================================================
- //
- // idMat3 - 3x3 matrix
- //
- // NOTE: matrix is column-major
- //
- //===============================================================
- class idMat3 {
- public:
- idMat3();
- explicit idMat3( const idVec3 &x, const idVec3 &y, const idVec3 &z );
- explicit idMat3( const float xx, const float xy, const float xz, const float yx, const float yy, const float yz, const float zx, const float zy, const float zz );
- explicit idMat3( const float src[ 3 ][ 3 ] );
- const idVec3 & operator[]( int index ) const;
- idVec3 & operator[]( int index );
- idMat3 operator-() const;
- idMat3 operator*( const float a ) const;
- idVec3 operator*( const idVec3 &vec ) const;
- idMat3 operator*( const idMat3 &a ) const;
- idMat3 operator+( const idMat3 &a ) const;
- idMat3 operator-( const idMat3 &a ) const;
- idMat3 & operator*=( const float a );
- idMat3 & operator*=( const idMat3 &a );
- idMat3 & operator+=( const idMat3 &a );
- idMat3 & operator-=( const idMat3 &a );
- friend idMat3 operator*( const float a, const idMat3 &mat );
- friend idVec3 operator*( const idVec3 &vec, const idMat3 &mat );
- friend idVec3 & operator*=( idVec3 &vec, const idMat3 &mat );
- bool Compare( const idMat3 &a ) const; // exact compare, no epsilon
- bool Compare( const idMat3 &a, const float epsilon ) const; // compare with epsilon
- bool operator==( const idMat3 &a ) const; // exact compare, no epsilon
- bool operator!=( const idMat3 &a ) const; // exact compare, no epsilon
- void Zero();
- void Identity();
- bool IsIdentity( const float epsilon = MATRIX_EPSILON ) const;
- bool IsSymmetric( const float epsilon = MATRIX_EPSILON ) const;
- bool IsDiagonal( const float epsilon = MATRIX_EPSILON ) const;
- bool IsRotated() const;
- void ProjectVector( const idVec3 &src, idVec3 &dst ) const;
- void UnprojectVector( const idVec3 &src, idVec3 &dst ) const;
- bool FixDegeneracies(); // fix degenerate axial cases
- bool FixDenormals(); // change tiny numbers to zero
- float Trace() const;
- float Determinant() const;
- idMat3 OrthoNormalize() const;
- idMat3 & OrthoNormalizeSelf();
- idMat3 Transpose() const; // returns transpose
- idMat3 & TransposeSelf();
- idMat3 Inverse() const; // returns the inverse ( m * m.Inverse() = identity )
- bool InverseSelf(); // returns false if determinant is zero
- idMat3 InverseFast() const; // returns the inverse ( m * m.Inverse() = identity )
- bool InverseFastSelf(); // returns false if determinant is zero
- idMat3 TransposeMultiply( const idMat3 &b ) const;
- idMat3 InertiaTranslate( const float mass, const idVec3 ¢erOfMass, const idVec3 &translation ) const;
- idMat3 & InertiaTranslateSelf( const float mass, const idVec3 ¢erOfMass, const idVec3 &translation );
- idMat3 InertiaRotate( const idMat3 &rotation ) const;
- idMat3 & InertiaRotateSelf( const idMat3 &rotation );
- int GetDimension() const;
- idAngles ToAngles() const;
- idQuat ToQuat() const;
- idCQuat ToCQuat() const;
- idRotation ToRotation() const;
- idMat4 ToMat4() const;
- idVec3 ToAngularVelocity() const;
- const float * ToFloatPtr() const;
- float * ToFloatPtr();
- const char * ToString( int precision = 2 ) const;
- friend void TransposeMultiply( const idMat3 &inv, const idMat3 &b, idMat3 &dst );
- friend idMat3 SkewSymmetric( idVec3 const &src );
- private:
- idVec3 mat[ 3 ];
- };
- extern idMat3 mat3_zero;
- extern idMat3 mat3_identity;
- #define mat3_default mat3_identity
- ID_INLINE idMat3::idMat3() {
- }
- ID_INLINE idMat3::idMat3( const idVec3 &x, const idVec3 &y, const idVec3 &z ) {
- mat[ 0 ].x = x.x; mat[ 0 ].y = x.y; mat[ 0 ].z = x.z;
- mat[ 1 ].x = y.x; mat[ 1 ].y = y.y; mat[ 1 ].z = y.z;
- mat[ 2 ].x = z.x; mat[ 2 ].y = z.y; mat[ 2 ].z = z.z;
- }
- ID_INLINE idMat3::idMat3( const float xx, const float xy, const float xz, const float yx, const float yy, const float yz, const float zx, const float zy, const float zz ) {
- mat[ 0 ].x = xx; mat[ 0 ].y = xy; mat[ 0 ].z = xz;
- mat[ 1 ].x = yx; mat[ 1 ].y = yy; mat[ 1 ].z = yz;
- mat[ 2 ].x = zx; mat[ 2 ].y = zy; mat[ 2 ].z = zz;
- }
- ID_INLINE idMat3::idMat3( const float src[ 3 ][ 3 ] ) {
- memcpy( mat, src, 3 * 3 * sizeof( float ) );
- }
- ID_INLINE const idVec3 &idMat3::operator[]( int index ) const {
- //assert( ( index >= 0 ) && ( index < 3 ) );
- return mat[ index ];
- }
- ID_INLINE idVec3 &idMat3::operator[]( int index ) {
- //assert( ( index >= 0 ) && ( index < 3 ) );
- return mat[ index ];
- }
- ID_INLINE idMat3 idMat3::operator-() const {
- return idMat3( -mat[0][0], -mat[0][1], -mat[0][2],
- -mat[1][0], -mat[1][1], -mat[1][2],
- -mat[2][0], -mat[2][1], -mat[2][2] );
- }
- ID_INLINE idVec3 idMat3::operator*( const idVec3 &vec ) const {
- return idVec3(
- mat[ 0 ].x * vec.x + mat[ 1 ].x * vec.y + mat[ 2 ].x * vec.z,
- mat[ 0 ].y * vec.x + mat[ 1 ].y * vec.y + mat[ 2 ].y * vec.z,
- mat[ 0 ].z * vec.x + mat[ 1 ].z * vec.y + mat[ 2 ].z * vec.z );
- }
- ID_INLINE idMat3 idMat3::operator*( const idMat3 &a ) const {
- int i, j;
- const float *m1Ptr, *m2Ptr;
- float *dstPtr;
- idMat3 dst;
- m1Ptr = reinterpret_cast<const float *>(this);
- m2Ptr = reinterpret_cast<const float *>(&a);
- dstPtr = reinterpret_cast<float *>(&dst);
- for ( i = 0; i < 3; i++ ) {
- for ( j = 0; j < 3; j++ ) {
- *dstPtr = m1Ptr[0] * m2Ptr[ 0 * 3 + j ]
- + m1Ptr[1] * m2Ptr[ 1 * 3 + j ]
- + m1Ptr[2] * m2Ptr[ 2 * 3 + j ];
- dstPtr++;
- }
- m1Ptr += 3;
- }
- return dst;
- }
- ID_INLINE idMat3 idMat3::operator*( const float a ) const {
- return idMat3(
- mat[0].x * a, mat[0].y * a, mat[0].z * a,
- mat[1].x * a, mat[1].y * a, mat[1].z * a,
- mat[2].x * a, mat[2].y * a, mat[2].z * a );
- }
- ID_INLINE idMat3 idMat3::operator+( const idMat3 &a ) const {
- return idMat3(
- mat[0].x + a[0].x, mat[0].y + a[0].y, mat[0].z + a[0].z,
- mat[1].x + a[1].x, mat[1].y + a[1].y, mat[1].z + a[1].z,
- mat[2].x + a[2].x, mat[2].y + a[2].y, mat[2].z + a[2].z );
- }
-
- ID_INLINE idMat3 idMat3::operator-( const idMat3 &a ) const {
- return idMat3(
- mat[0].x - a[0].x, mat[0].y - a[0].y, mat[0].z - a[0].z,
- mat[1].x - a[1].x, mat[1].y - a[1].y, mat[1].z - a[1].z,
- mat[2].x - a[2].x, mat[2].y - a[2].y, mat[2].z - a[2].z );
- }
- ID_INLINE idMat3 &idMat3::operator*=( const float a ) {
- mat[0].x *= a; mat[0].y *= a; mat[0].z *= a;
- mat[1].x *= a; mat[1].y *= a; mat[1].z *= a;
- mat[2].x *= a; mat[2].y *= a; mat[2].z *= a;
- return *this;
- }
- ID_INLINE idMat3 &idMat3::operator*=( const idMat3 &a ) {
- int i, j;
- const float *m2Ptr;
- float *m1Ptr, dst[3];
- m1Ptr = reinterpret_cast<float *>(this);
- m2Ptr = reinterpret_cast<const float *>(&a);
- for ( i = 0; i < 3; i++ ) {
- for ( j = 0; j < 3; j++ ) {
- dst[j] = m1Ptr[0] * m2Ptr[ 0 * 3 + j ]
- + m1Ptr[1] * m2Ptr[ 1 * 3 + j ]
- + m1Ptr[2] * m2Ptr[ 2 * 3 + j ];
- }
- m1Ptr[0] = dst[0]; m1Ptr[1] = dst[1]; m1Ptr[2] = dst[2];
- m1Ptr += 3;
- }
- return *this;
- }
- ID_INLINE idMat3 &idMat3::operator+=( const idMat3 &a ) {
- mat[0].x += a[0].x; mat[0].y += a[0].y; mat[0].z += a[0].z;
- mat[1].x += a[1].x; mat[1].y += a[1].y; mat[1].z += a[1].z;
- mat[2].x += a[2].x; mat[2].y += a[2].y; mat[2].z += a[2].z;
- return *this;
- }
- ID_INLINE idMat3 &idMat3::operator-=( const idMat3 &a ) {
- mat[0].x -= a[0].x; mat[0].y -= a[0].y; mat[0].z -= a[0].z;
- mat[1].x -= a[1].x; mat[1].y -= a[1].y; mat[1].z -= a[1].z;
- mat[2].x -= a[2].x; mat[2].y -= a[2].y; mat[2].z -= a[2].z;
- return *this;
- }
- ID_INLINE idVec3 operator*( const idVec3 &vec, const idMat3 &mat ) {
- return mat * vec;
- }
- ID_INLINE idMat3 operator*( const float a, const idMat3 &mat ) {
- return mat * a;
- }
- ID_INLINE idVec3 &operator*=( idVec3 &vec, const idMat3 &mat ) {
- float x = mat[ 0 ].x * vec.x + mat[ 1 ].x * vec.y + mat[ 2 ].x * vec.z;
- float y = mat[ 0 ].y * vec.x + mat[ 1 ].y * vec.y + mat[ 2 ].y * vec.z;
- vec.z = mat[ 0 ].z * vec.x + mat[ 1 ].z * vec.y + mat[ 2 ].z * vec.z;
- vec.x = x;
- vec.y = y;
- return vec;
- }
- ID_INLINE bool idMat3::Compare( const idMat3 &a ) const {
- if ( mat[0].Compare( a[0] ) &&
- mat[1].Compare( a[1] ) &&
- mat[2].Compare( a[2] ) ) {
- return true;
- }
- return false;
- }
- ID_INLINE bool idMat3::Compare( const idMat3 &a, const float epsilon ) const {
- if ( mat[0].Compare( a[0], epsilon ) &&
- mat[1].Compare( a[1], epsilon ) &&
- mat[2].Compare( a[2], epsilon ) ) {
- return true;
- }
- return false;
- }
- ID_INLINE bool idMat3::operator==( const idMat3 &a ) const {
- return Compare( a );
- }
- ID_INLINE bool idMat3::operator!=( const idMat3 &a ) const {
- return !Compare( a );
- }
- ID_INLINE void idMat3::Zero() {
- memset( mat, 0, sizeof( idMat3 ) );
- }
- ID_INLINE void idMat3::Identity() {
- *this = mat3_identity;
- }
- ID_INLINE bool idMat3::IsIdentity( const float epsilon ) const {
- return Compare( mat3_identity, epsilon );
- }
- ID_INLINE bool idMat3::IsSymmetric( const float epsilon ) const {
- if ( idMath::Fabs( mat[0][1] - mat[1][0] ) > epsilon ) {
- return false;
- }
- if ( idMath::Fabs( mat[0][2] - mat[2][0] ) > epsilon ) {
- return false;
- }
- if ( idMath::Fabs( mat[1][2] - mat[2][1] ) > epsilon ) {
- return false;
- }
- return true;
- }
- ID_INLINE bool idMat3::IsDiagonal( const float epsilon ) const {
- if ( idMath::Fabs( mat[0][1] ) > epsilon ||
- idMath::Fabs( mat[0][2] ) > epsilon ||
- idMath::Fabs( mat[1][0] ) > epsilon ||
- idMath::Fabs( mat[1][2] ) > epsilon ||
- idMath::Fabs( mat[2][0] ) > epsilon ||
- idMath::Fabs( mat[2][1] ) > epsilon ) {
- return false;
- }
- return true;
- }
- ID_INLINE bool idMat3::IsRotated() const {
- return !Compare( mat3_identity );
- }
- ID_INLINE void idMat3::ProjectVector( const idVec3 &src, idVec3 &dst ) const {
- dst.x = src * mat[ 0 ];
- dst.y = src * mat[ 1 ];
- dst.z = src * mat[ 2 ];
- }
- ID_INLINE void idMat3::UnprojectVector( const idVec3 &src, idVec3 &dst ) const {
- dst = mat[ 0 ] * src.x + mat[ 1 ] * src.y + mat[ 2 ] * src.z;
- }
- ID_INLINE bool idMat3::FixDegeneracies() {
- bool r = mat[0].FixDegenerateNormal();
- r |= mat[1].FixDegenerateNormal();
- r |= mat[2].FixDegenerateNormal();
- return r;
- }
- ID_INLINE bool idMat3::FixDenormals() {
- bool r = mat[0].FixDenormals();
- r |= mat[1].FixDenormals();
- r |= mat[2].FixDenormals();
- return r;
- }
- ID_INLINE float idMat3::Trace() const {
- return ( mat[0][0] + mat[1][1] + mat[2][2] );
- }
- ID_INLINE idMat3 idMat3::OrthoNormalize() const {
- idMat3 ortho;
- ortho = *this;
- ortho[ 0 ].Normalize();
- ortho[ 2 ].Cross( mat[ 0 ], mat[ 1 ] );
- ortho[ 2 ].Normalize();
- ortho[ 1 ].Cross( mat[ 2 ], mat[ 0 ] );
- ortho[ 1 ].Normalize();
- return ortho;
- }
- ID_INLINE idMat3 &idMat3::OrthoNormalizeSelf() {
- mat[ 0 ].Normalize();
- mat[ 2 ].Cross( mat[ 0 ], mat[ 1 ] );
- mat[ 2 ].Normalize();
- mat[ 1 ].Cross( mat[ 2 ], mat[ 0 ] );
- mat[ 1 ].Normalize();
- return *this;
- }
- ID_INLINE idMat3 idMat3::Transpose() const {
- return idMat3( mat[0][0], mat[1][0], mat[2][0],
- mat[0][1], mat[1][1], mat[2][1],
- mat[0][2], mat[1][2], mat[2][2] );
- }
- ID_INLINE idMat3 &idMat3::TransposeSelf() {
- float tmp0, tmp1, tmp2;
- tmp0 = mat[0][1];
- mat[0][1] = mat[1][0];
- mat[1][0] = tmp0;
- tmp1 = mat[0][2];
- mat[0][2] = mat[2][0];
- mat[2][0] = tmp1;
- tmp2 = mat[1][2];
- mat[1][2] = mat[2][1];
- mat[2][1] = tmp2;
- return *this;
- }
- ID_INLINE idMat3 idMat3::Inverse() const {
- idMat3 invMat;
- invMat = *this;
- verify( invMat.InverseSelf() );
- return invMat;
- }
- ID_INLINE idMat3 idMat3::InverseFast() const {
- idMat3 invMat;
- invMat = *this;
- verify( invMat.InverseFastSelf() );
- return invMat;
- }
- ID_INLINE idMat3 idMat3::TransposeMultiply( const idMat3 &b ) const {
- return idMat3( mat[0].x * b[0].x + mat[1].x * b[1].x + mat[2].x * b[2].x,
- mat[0].x * b[0].y + mat[1].x * b[1].y + mat[2].x * b[2].y,
- mat[0].x * b[0].z + mat[1].x * b[1].z + mat[2].x * b[2].z,
- mat[0].y * b[0].x + mat[1].y * b[1].x + mat[2].y * b[2].x,
- mat[0].y * b[0].y + mat[1].y * b[1].y + mat[2].y * b[2].y,
- mat[0].y * b[0].z + mat[1].y * b[1].z + mat[2].y * b[2].z,
- mat[0].z * b[0].x + mat[1].z * b[1].x + mat[2].z * b[2].x,
- mat[0].z * b[0].y + mat[1].z * b[1].y + mat[2].z * b[2].y,
- mat[0].z * b[0].z + mat[1].z * b[1].z + mat[2].z * b[2].z );
- }
- ID_INLINE void TransposeMultiply( const idMat3 &transpose, const idMat3 &b, idMat3 &dst ) {
- dst[0].x = transpose[0].x * b[0].x + transpose[1].x * b[1].x + transpose[2].x * b[2].x;
- dst[0].y = transpose[0].x * b[0].y + transpose[1].x * b[1].y + transpose[2].x * b[2].y;
- dst[0].z = transpose[0].x * b[0].z + transpose[1].x * b[1].z + transpose[2].x * b[2].z;
- dst[1].x = transpose[0].y * b[0].x + transpose[1].y * b[1].x + transpose[2].y * b[2].x;
- dst[1].y = transpose[0].y * b[0].y + transpose[1].y * b[1].y + transpose[2].y * b[2].y;
- dst[1].z = transpose[0].y * b[0].z + transpose[1].y * b[1].z + transpose[2].y * b[2].z;
- dst[2].x = transpose[0].z * b[0].x + transpose[1].z * b[1].x + transpose[2].z * b[2].x;
- dst[2].y = transpose[0].z * b[0].y + transpose[1].z * b[1].y + transpose[2].z * b[2].y;
- dst[2].z = transpose[0].z * b[0].z + transpose[1].z * b[1].z + transpose[2].z * b[2].z;
- }
- ID_INLINE idMat3 SkewSymmetric( idVec3 const &src ) {
- return idMat3( 0.0f, -src.z, src.y, src.z, 0.0f, -src.x, -src.y, src.x, 0.0f );
- }
- ID_INLINE int idMat3::GetDimension() const {
- return 9;
- }
- ID_INLINE const float *idMat3::ToFloatPtr() const {
- return mat[0].ToFloatPtr();
- }
- ID_INLINE float *idMat3::ToFloatPtr() {
- return mat[0].ToFloatPtr();
- }
- //===============================================================
- //
- // idMat4 - 4x4 matrix
- //
- //===============================================================
- class idMat4 {
- public:
- idMat4();
- explicit idMat4( const idVec4 &x, const idVec4 &y, const idVec4 &z, const idVec4 &w );
- explicit idMat4(const float xx, const float xy, const float xz, const float xw,
- const float yx, const float yy, const float yz, const float yw,
- const float zx, const float zy, const float zz, const float zw,
- const float wx, const float wy, const float wz, const float ww );
- explicit idMat4( const idMat3 &rotation, const idVec3 &translation );
- explicit idMat4( const float src[ 4 ][ 4 ] );
- const idVec4 & operator[]( int index ) const;
- idVec4 & operator[]( int index );
- idMat4 operator*( const float a ) const;
- idVec4 operator*( const idVec4 &vec ) const;
- idVec3 operator*( const idVec3 &vec ) const;
- idMat4 operator*( const idMat4 &a ) const;
- idMat4 operator+( const idMat4 &a ) const;
- idMat4 operator-( const idMat4 &a ) const;
- idMat4 & operator*=( const float a );
- idMat4 & operator*=( const idMat4 &a );
- idMat4 & operator+=( const idMat4 &a );
- idMat4 & operator-=( const idMat4 &a );
- friend idMat4 operator*( const float a, const idMat4 &mat );
- friend idVec4 operator*( const idVec4 &vec, const idMat4 &mat );
- friend idVec3 operator*( const idVec3 &vec, const idMat4 &mat );
- friend idVec4 & operator*=( idVec4 &vec, const idMat4 &mat );
- friend idVec3 & operator*=( idVec3 &vec, const idMat4 &mat );
- bool Compare( const idMat4 &a ) const; // exact compare, no epsilon
- bool Compare( const idMat4 &a, const float epsilon ) const; // compare with epsilon
- bool operator==( const idMat4 &a ) const; // exact compare, no epsilon
- bool operator!=( const idMat4 &a ) const; // exact compare, no epsilon
- void Zero();
- void Identity();
- bool IsIdentity( const float epsilon = MATRIX_EPSILON ) const;
- bool IsSymmetric( const float epsilon = MATRIX_EPSILON ) const;
- bool IsDiagonal( const float epsilon = MATRIX_EPSILON ) const;
- bool IsRotated() const;
- void ProjectVector( const idVec4 &src, idVec4 &dst ) const;
- void UnprojectVector( const idVec4 &src, idVec4 &dst ) const;
- float Trace() const;
- float Determinant() const;
- idMat4 Transpose() const; // returns transpose
- idMat4 & TransposeSelf();
- idMat4 Inverse() const; // returns the inverse ( m * m.Inverse() = identity )
- bool InverseSelf(); // returns false if determinant is zero
- idMat4 InverseFast() const; // returns the inverse ( m * m.Inverse() = identity )
- bool InverseFastSelf(); // returns false if determinant is zero
- idMat4 TransposeMultiply( const idMat4 &b ) const;
- int GetDimension() const;
- const float * ToFloatPtr() const;
- float * ToFloatPtr();
- const char * ToString( int precision = 2 ) const;
- private:
- idVec4 mat[ 4 ];
- };
- extern idMat4 mat4_zero;
- extern idMat4 mat4_identity;
- #define mat4_default mat4_identity
- ID_INLINE idMat4::idMat4() {
- }
- ID_INLINE idMat4::idMat4( const idVec4 &x, const idVec4 &y, const idVec4 &z, const idVec4 &w ) {
- mat[ 0 ] = x;
- mat[ 1 ] = y;
- mat[ 2 ] = z;
- mat[ 3 ] = w;
- }
- ID_INLINE idMat4::idMat4( const float xx, const float xy, const float xz, const float xw,
- const float yx, const float yy, const float yz, const float yw,
- const float zx, const float zy, const float zz, const float zw,
- const float wx, const float wy, const float wz, const float ww ) {
- mat[0][0] = xx; mat[0][1] = xy; mat[0][2] = xz; mat[0][3] = xw;
- mat[1][0] = yx; mat[1][1] = yy; mat[1][2] = yz; mat[1][3] = yw;
- mat[2][0] = zx; mat[2][1] = zy; mat[2][2] = zz; mat[2][3] = zw;
- mat[3][0] = wx; mat[3][1] = wy; mat[3][2] = wz; mat[3][3] = ww;
- }
- ID_INLINE idMat4::idMat4( const idMat3 &rotation, const idVec3 &translation ) {
- // NOTE: idMat3 is transposed because it is column-major
- mat[ 0 ][ 0 ] = rotation[0][0];
- mat[ 0 ][ 1 ] = rotation[1][0];
- mat[ 0 ][ 2 ] = rotation[2][0];
- mat[ 0 ][ 3 ] = translation[0];
- mat[ 1 ][ 0 ] = rotation[0][1];
- mat[ 1 ][ 1 ] = rotation[1][1];
- mat[ 1 ][ 2 ] = rotation[2][1];
- mat[ 1 ][ 3 ] = translation[1];
- mat[ 2 ][ 0 ] = rotation[0][2];
- mat[ 2 ][ 1 ] = rotation[1][2];
- mat[ 2 ][ 2 ] = rotation[2][2];
- mat[ 2 ][ 3 ] = translation[2];
- mat[ 3 ][ 0 ] = 0.0f;
- mat[ 3 ][ 1 ] = 0.0f;
- mat[ 3 ][ 2 ] = 0.0f;
- mat[ 3 ][ 3 ] = 1.0f;
- }
- ID_INLINE idMat4::idMat4( const float src[ 4 ][ 4 ] ) {
- memcpy( mat, src, 4 * 4 * sizeof( float ) );
- }
- ID_INLINE const idVec4 &idMat4::operator[]( int index ) const {
- //assert( ( index >= 0 ) && ( index < 4 ) );
- return mat[ index ];
- }
- ID_INLINE idVec4 &idMat4::operator[]( int index ) {
- //assert( ( index >= 0 ) && ( index < 4 ) );
- return mat[ index ];
- }
- ID_INLINE idMat4 idMat4::operator*( const float a ) const {
- return idMat4(
- mat[0].x * a, mat[0].y * a, mat[0].z * a, mat[0].w * a,
- mat[1].x * a, mat[1].y * a, mat[1].z * a, mat[1].w * a,
- mat[2].x * a, mat[2].y * a, mat[2].z * a, mat[2].w * a,
- mat[3].x * a, mat[3].y * a, mat[3].z * a, mat[3].w * a );
- }
- ID_INLINE idVec4 idMat4::operator*( const idVec4 &vec ) const {
- return idVec4(
- mat[ 0 ].x * vec.x + mat[ 0 ].y * vec.y + mat[ 0 ].z * vec.z + mat[ 0 ].w * vec.w,
- mat[ 1 ].x * vec.x + mat[ 1 ].y * vec.y + mat[ 1 ].z * vec.z + mat[ 1 ].w * vec.w,
- mat[ 2 ].x * vec.x + mat[ 2 ].y * vec.y + mat[ 2 ].z * vec.z + mat[ 2 ].w * vec.w,
- mat[ 3 ].x * vec.x + mat[ 3 ].y * vec.y + mat[ 3 ].z * vec.z + mat[ 3 ].w * vec.w );
- }
- ID_INLINE idVec3 idMat4::operator*( const idVec3 &vec ) const {
- float s = mat[ 3 ].x * vec.x + mat[ 3 ].y * vec.y + mat[ 3 ].z * vec.z + mat[ 3 ].w;
- if ( s == 0.0f ) {
- return idVec3( 0.0f, 0.0f, 0.0f );
- }
- if ( s == 1.0f ) {
- return idVec3(
- mat[ 0 ].x * vec.x + mat[ 0 ].y * vec.y + mat[ 0 ].z * vec.z + mat[ 0 ].w,
- mat[ 1 ].x * vec.x + mat[ 1 ].y * vec.y + mat[ 1 ].z * vec.z + mat[ 1 ].w,
- mat[ 2 ].x * vec.x + mat[ 2 ].y * vec.y + mat[ 2 ].z * vec.z + mat[ 2 ].w );
- }
- else {
- float invS = 1.0f / s;
- return idVec3(
- (mat[ 0 ].x * vec.x + mat[ 0 ].y * vec.y + mat[ 0 ].z * vec.z + mat[ 0 ].w) * invS,
- (mat[ 1 ].x * vec.x + mat[ 1 ].y * vec.y + mat[ 1 ].z * vec.z + mat[ 1 ].w) * invS,
- (mat[ 2 ].x * vec.x + mat[ 2 ].y * vec.y + mat[ 2 ].z * vec.z + mat[ 2 ].w) * invS );
- }
- }
- ID_INLINE idMat4 idMat4::operator*( const idMat4 &a ) const {
- int i, j;
- const float *m1Ptr, *m2Ptr;
- float *dstPtr;
- idMat4 dst;
- m1Ptr = reinterpret_cast<const float *>(this);
- m2Ptr = reinterpret_cast<const float *>(&a);
- dstPtr = reinterpret_cast<float *>(&dst);
- for ( i = 0; i < 4; i++ ) {
- for ( j = 0; j < 4; j++ ) {
- *dstPtr = m1Ptr[0] * m2Ptr[ 0 * 4 + j ]
- + m1Ptr[1] * m2Ptr[ 1 * 4 + j ]
- + m1Ptr[2] * m2Ptr[ 2 * 4 + j ]
- + m1Ptr[3] * m2Ptr[ 3 * 4 + j ];
- dstPtr++;
- }
- m1Ptr += 4;
- }
- return dst;
- }
- ID_INLINE idMat4 idMat4::operator+( const idMat4 &a ) const {
- return idMat4(
- mat[0].x + a[0].x, mat[0].y + a[0].y, mat[0].z + a[0].z, mat[0].w + a[0].w,
- mat[1].x + a[1].x, mat[1].y + a[1].y, mat[1].z + a[1].z, mat[1].w + a[1].w,
- mat[2].x + a[2].x, mat[2].y + a[2].y, mat[2].z + a[2].z, mat[2].w + a[2].w,
- mat[3].x + a[3].x, mat[3].y + a[3].y, mat[3].z + a[3].z, mat[3].w + a[3].w );
- }
-
- ID_INLINE idMat4 idMat4::operator-( const idMat4 &a ) const {
- return idMat4(
- mat[0].x - a[0].x, mat[0].y - a[0].y, mat[0].z - a[0].z, mat[0].w - a[0].w,
- mat[1].x - a[1].x, mat[1].y - a[1].y, mat[1].z - a[1].z, mat[1].w - a[1].w,
- mat[2].x - a[2].x, mat[2].y - a[2].y, mat[2].z - a[2].z, mat[2].w - a[2].w,
- mat[3].x - a[3].x, mat[3].y - a[3].y, mat[3].z - a[3].z, mat[3].w - a[3].w );
- }
- ID_INLINE idMat4 &idMat4::operator*=( const float a ) {
- mat[0].x *= a; mat[0].y *= a; mat[0].z *= a; mat[0].w *= a;
- mat[1].x *= a; mat[1].y *= a; mat[1].z *= a; mat[1].w *= a;
- mat[2].x *= a; mat[2].y *= a; mat[2].z *= a; mat[2].w *= a;
- mat[3].x *= a; mat[3].y *= a; mat[3].z *= a; mat[3].w *= a;
- return *this;
- }
- ID_INLINE idMat4 &idMat4::operator*=( const idMat4 &a ) {
- *this = (*this) * a;
- return *this;
- }
- ID_INLINE idMat4 &idMat4::operator+=( const idMat4 &a ) {
- mat[0].x += a[0].x; mat[0].y += a[0].y; mat[0].z += a[0].z; mat[0].w += a[0].w;
- mat[1].x += a[1].x; mat[1].y += a[1].y; mat[1].z += a[1].z; mat[1].w += a[1].w;
- mat[2].x += a[2].x; mat[2].y += a[2].y; mat[2].z += a[2].z; mat[2].w += a[2].w;
- mat[3].x += a[3].x; mat[3].y += a[3].y; mat[3].z += a[3].z; mat[3].w += a[3].w;
- return *this;
- }
- ID_INLINE idMat4 &idMat4::operator-=( const idMat4 &a ) {
- mat[0].x -= a[0].x; mat[0].y -= a[0].y; mat[0].z -= a[0].z; mat[0].w -= a[0].w;
- mat[1].x -= a[1].x; mat[1].y -= a[1].y; mat[1].z -= a[1].z; mat[1].w -= a[1].w;
- mat[2].x -= a[2].x; mat[2].y -= a[2].y; mat[2].z -= a[2].z; mat[2].w -= a[2].w;
- mat[3].x -= a[3].x; mat[3].y -= a[3].y; mat[3].z -= a[3].z; mat[3].w -= a[3].w;
- return *this;
- }
- ID_INLINE idMat4 operator*( const float a, const idMat4 &mat ) {
- return mat * a;
- }
- ID_INLINE idVec4 operator*( const idVec4 &vec, const idMat4 &mat ) {
- return mat * vec;
- }
- ID_INLINE idVec3 operator*( const idVec3 &vec, const idMat4 &mat ) {
- return mat * vec;
- }
- ID_INLINE idVec4 &operator*=( idVec4 &vec, const idMat4 &mat ) {
- vec = mat * vec;
- return vec;
- }
- ID_INLINE idVec3 &operator*=( idVec3 &vec, const idMat4 &mat ) {
- vec = mat * vec;
- return vec;
- }
- ID_INLINE bool idMat4::Compare( const idMat4 &a ) const {
- dword i;
- const float *ptr1, *ptr2;
- ptr1 = reinterpret_cast<const float *>(mat);
- ptr2 = reinterpret_cast<const float *>(a.mat);
- for ( i = 0; i < 4*4; i++ ) {
- if ( ptr1[i] != ptr2[i] ) {
- return false;
- }
- }
- return true;
- }
- ID_INLINE bool idMat4::Compare( const idMat4 &a, const float epsilon ) const {
- dword i;
- const float *ptr1, *ptr2;
- ptr1 = reinterpret_cast<const float *>(mat);
- ptr2 = reinterpret_cast<const float *>(a.mat);
- for ( i = 0; i < 4*4; i++ ) {
- if ( idMath::Fabs( ptr1[i] - ptr2[i] ) > epsilon ) {
- return false;
- }
- }
- return true;
- }
- ID_INLINE bool idMat4::operator==( const idMat4 &a ) const {
- return Compare( a );
- }
- ID_INLINE bool idMat4::operator!=( const idMat4 &a ) const {
- return !Compare( a );
- }
- ID_INLINE void idMat4::Zero() {
- memset( mat, 0, sizeof( idMat4 ) );
- }
- ID_INLINE void idMat4::Identity() {
- *this = mat4_identity;
- }
- ID_INLINE bool idMat4::IsIdentity( const float epsilon ) const {
- return Compare( mat4_identity, epsilon );
- }
- ID_INLINE bool idMat4::IsSymmetric( const float epsilon ) const {
- for ( int i = 1; i < 4; i++ ) {
- for ( int j = 0; j < i; j++ ) {
- if ( idMath::Fabs( mat[i][j] - mat[j][i] ) > epsilon ) {
- return false;
- }
- }
- }
- return true;
- }
- ID_INLINE bool idMat4::IsDiagonal( const float epsilon ) const {
- for ( int i = 0; i < 4; i++ ) {
- for ( int j = 0; j < 4; j++ ) {
- if ( i != j && idMath::Fabs( mat[i][j] ) > epsilon ) {
- return false;
- }
- }
- }
- return true;
- }
- ID_INLINE bool idMat4::IsRotated() const {
- if ( !mat[ 0 ][ 1 ] && !mat[ 0 ][ 2 ] &&
- !mat[ 1 ][ 0 ] && !mat[ 1 ][ 2 ] &&
- !mat[ 2 ][ 0 ] && !mat[ 2 ][ 1 ] ) {
- return false;
- }
- return true;
- }
- ID_INLINE void idMat4::ProjectVector( const idVec4 &src, idVec4 &dst ) const {
- dst.x = src * mat[ 0 ];
- dst.y = src * mat[ 1 ];
- dst.z = src * mat[ 2 ];
- dst.w = src * mat[ 3 ];
- }
- ID_INLINE void idMat4::UnprojectVector( const idVec4 &src, idVec4 &dst ) const {
- dst = mat[ 0 ] * src.x + mat[ 1 ] * src.y + mat[ 2 ] * src.z + mat[ 3 ] * src.w;
- }
- ID_INLINE float idMat4::Trace() const {
- return ( mat[0][0] + mat[1][1] + mat[2][2] + mat[3][3] );
- }
- ID_INLINE idMat4 idMat4::Inverse() const {
- idMat4 invMat;
- invMat = *this;
- verify( invMat.InverseSelf() );
- return invMat;
- }
- ID_INLINE idMat4 idMat4::InverseFast() const {
- idMat4 invMat;
- invMat = *this;
- verify( invMat.InverseFastSelf() );
- return invMat;
- }
- ID_INLINE idMat4 idMat3::ToMat4() const {
- // NOTE: idMat3 is transposed because it is column-major
- return idMat4( mat[0][0], mat[1][0], mat[2][0], 0.0f,
- mat[0][1], mat[1][1], mat[2][1], 0.0f,
- mat[0][2], mat[1][2], mat[2][2], 0.0f,
- 0.0f, 0.0f, 0.0f, 1.0f );
- }
- ID_INLINE int idMat4::GetDimension() const {
- return 16;
- }
- ID_INLINE const float *idMat4::ToFloatPtr() const {
- return mat[0].ToFloatPtr();
- }
- ID_INLINE float *idMat4::ToFloatPtr() {
- return mat[0].ToFloatPtr();
- }
- //===============================================================
- //
- // idMat5 - 5x5 matrix
- //
- //===============================================================
- class idMat5 {
- public:
- idMat5();
- explicit idMat5( const idVec5 &v0, const idVec5 &v1, const idVec5 &v2, const idVec5 &v3, const idVec5 &v4 );
- explicit idMat5( const float src[ 5 ][ 5 ] );
- const idVec5 & operator[]( int index ) const;
- idVec5 & operator[]( int index );
- idMat5 operator*( const float a ) const;
- idVec5 operator*( const idVec5 &vec ) const;
- idMat5 operator*( const idMat5 &a ) const;
- idMat5 operator+( const idMat5 &a ) const;
- idMat5 operator-( const idMat5 &a ) const;
- idMat5 & operator*=( const float a );
- idMat5 & operator*=( const idMat5 &a );
- idMat5 & operator+=( const idMat5 &a );
- idMat5 & operator-=( const idMat5 &a );
- friend idMat5 operator*( const float a, const idMat5 &mat );
- friend idVec5 operator*( const idVec5 &vec, const idMat5 &mat );
- friend idVec5 & operator*=( idVec5 &vec, const idMat5 &mat );
- bool Compare( const idMat5 &a ) const; // exact compare, no epsilon
- bool Compare( const idMat5 &a, const float epsilon ) const; // compare with epsilon
- bool operator==( const idMat5 &a ) const; // exact compare, no epsilon
- bool operator!=( const idMat5 &a ) const; // exact compare, no epsilon
- void Zero();
- void Identity();
- bool IsIdentity( const float epsilon = MATRIX_EPSILON ) const;
- bool IsSymmetric( const float epsilon = MATRIX_EPSILON ) const;
- bool IsDiagonal( const float epsilon = MATRIX_EPSILON ) const;
- float Trace() const;
- float Determinant() const;
- idMat5 Transpose() const; // returns transpose
- idMat5 & TransposeSelf();
- idMat5 Inverse() const; // returns the inverse ( m * m.Inverse() = identity )
- bool InverseSelf(); // returns false if determinant is zero
- idMat5 InverseFast() const; // returns the inverse ( m * m.Inverse() = identity )
- bool InverseFastSelf(); // returns false if determinant is zero
- int GetDimension() const;
- const float * ToFloatPtr() const;
- float * ToFloatPtr();
- const char * ToString( int precision = 2 ) const;
- private:
- idVec5 mat[ 5 ];
- };
- extern idMat5 mat5_zero;
- extern idMat5 mat5_identity;
- #define mat5_default mat5_identity
- ID_INLINE idMat5::idMat5() {
- }
- ID_INLINE idMat5::idMat5( const float src[ 5 ][ 5 ] ) {
- memcpy( mat, src, 5 * 5 * sizeof( float ) );
- }
- ID_INLINE idMat5::idMat5( const idVec5 &v0, const idVec5 &v1, const idVec5 &v2, const idVec5 &v3, const idVec5 &v4 ) {
- mat[0] = v0;
- mat[1] = v1;
- mat[2] = v2;
- mat[3] = v3;
- mat[4] = v4;
- }
- ID_INLINE const idVec5 &idMat5::operator[]( int index ) const {
- //assert( ( index >= 0 ) && ( index < 5 ) );
- return mat[ index ];
- }
- ID_INLINE idVec5 &idMat5::operator[]( int index ) {
- //assert( ( index >= 0 ) && ( index < 5 ) );
- return mat[ index ];
- }
- ID_INLINE idMat5 idMat5::operator*( const idMat5 &a ) const {
- int i, j;
- const float *m1Ptr, *m2Ptr;
- float *dstPtr;
- idMat5 dst;
- m1Ptr = reinterpret_cast<const float *>(this);
- m2Ptr = reinterpret_cast<const float *>(&a);
- dstPtr = reinterpret_cast<float *>(&dst);
- for ( i = 0; i < 5; i++ ) {
- for ( j = 0; j < 5; j++ ) {
- *dstPtr = m1Ptr[0] * m2Ptr[ 0 * 5 + j ]
- + m1Ptr[1] * m2Ptr[ 1 * 5 + j ]
- + m1Ptr[2] * m2Ptr[ 2 * 5 + j ]
- + m1Ptr[3] * m2Ptr[ 3 * 5 + j ]
- + m1Ptr[4] * m2Ptr[ 4 * 5 + j ];
- dstPtr++;
- }
- m1Ptr += 5;
- }
- return dst;
- }
- ID_INLINE idMat5 idMat5::operator*( const float a ) const {
- return idMat5(
- idVec5( mat[0][0] * a, mat[0][1] * a, mat[0][2] * a, mat[0][3] * a, mat[0][4] * a ),
- idVec5( mat[1][0] * a, mat[1][1] * a, mat[1][2] * a, mat[1][3] * a, mat[1][4] * a ),
- idVec5( mat[2][0] * a, mat[2][1] * a, mat[2][2] * a, mat[2][3] * a, mat[2][4] * a ),
- idVec5( mat[3][0] * a, mat[3][1] * a, mat[3][2] * a, mat[3][3] * a, mat[3][4] * a ),
- idVec5( mat[4][0] * a, mat[4][1] * a, mat[4][2] * a, mat[4][3] * a, mat[4][4] * a ) );
- }
- ID_INLINE idVec5 idMat5::operator*( const idVec5 &vec ) const {
- return idVec5(
- mat[0][0] * vec[0] + mat[0][1] * vec[1] + mat[0][2] * vec[2] + mat[0][3] * vec[3] + mat[0][4] * vec[4],
- mat[1][0] * vec[0] + mat[1][1] * vec[1] + mat[1][2] * vec[2] + mat[1][3] * vec[3] + mat[1][4] * vec[4],
- mat[2][0] * vec[0] + mat[2][1] * vec[1] + mat[2][2] * vec[2] + mat[2][3] * vec[3] + mat[2][4] * vec[4],
- mat[3][0] * vec[0] + mat[3][1] * vec[1] + mat[3][2] * vec[2] + mat[3][3] * vec[3] + mat[3][4] * vec[4],
- mat[4][0] * vec[0] + mat[4][1] * vec[1] + mat[4][2] * vec[2] + mat[4][3] * vec[3] + mat[4][4] * vec[4] );
- }
- ID_INLINE idMat5 idMat5::operator+( const idMat5 &a ) const {
- return idMat5(
- idVec5( mat[0][0] + a[0][0], mat[0][1] + a[0][1], mat[0][2] + a[0][2], mat[0][3] + a[0][3], mat[0][4] + a[0][4] ),
- idVec5( mat[1][0] + a[1][0], mat[1][1] + a[1][1], mat[1][2] + a[1][2], mat[1][3] + a[1][3], mat[1][4] + a[1][4] ),
- idVec5( mat[2][0] + a[2][0], mat[2][1] + a[2][1], mat[2][2] + a[2][2], mat[2][3] + a[2][3], mat[2][4] + a[2][4] ),
- idVec5( mat[3][0] + a[3][0], mat[3][1] + a[3][1], mat[3][2] + a[3][2], mat[3][3] + a[3][3], mat[3][4] + a[3][4] ),
- idVec5( mat[4][0] + a[4][0], mat[4][1] + a[4][1], mat[4][2] + a[4][2], mat[4][3] + a[4][3], mat[4][4] + a[4][4] ) );
- }
- ID_INLINE idMat5 idMat5::operator-( const idMat5 &a ) const {
- return idMat5(
- idVec5( mat[0][0] - a[0][0], mat[0][1] - a[0][1], mat[0][2] - a[0][2], mat[0][3] - a[0][3], mat[0][4] - a[0][4] ),
- idVec5( mat[1][0] - a[1][0], mat[1][1] - a[1][1], mat[1][2] - a[1][2], mat[1][3] - a[1][3], mat[1][4] - a[1][4] ),
- idVec5( mat[2][0] - a[2][0], mat[2][1] - a[2][1], mat[2][2] - a[2][2], mat[2][3] - a[2][3], mat[2][4] - a[2][4] ),
- idVec5( mat[3][0] - a[3][0], mat[3][1] - a[3][1], mat[3][2] - a[3][2], mat[3][3] - a[3][3], mat[3][4] - a[3][4] ),
- idVec5( mat[4][0] - a[4][0], mat[4][1] - a[4][1], mat[4][2] - a[4][2], mat[4][3] - a[4][3], mat[4][4] - a[4][4] ) );
- }
- ID_INLINE idMat5 &idMat5::operator*=( const float a ) {
- mat[0][0] *= a; mat[0][1] *= a; mat[0][2] *= a; mat[0][3] *= a; mat[0][4] *= a;
- mat[1][0] *= a; mat[1][1] *= a; mat[1][2] *= a; mat[1][3] *= a; mat[1][4] *= a;
- mat[2][0] *= a; mat[2][1] *= a; mat[2][2] *= a; mat[2][3] *= a; mat[2][4] *= a;
- mat[3][0] *= a; mat[3][1] *= a; mat[3][2] *= a; mat[3][3] *= a; mat[3][4] *= a;
- mat[4][0] *= a; mat[4][1] *= a; mat[4][2] *= a; mat[4][3] *= a; mat[4][4] *= a;
- return *this;
- }
- ID_INLINE idMat5 &idMat5::operator*=( const idMat5 &a ) {
- *this = *this * a;
- return *this;
- }
- ID_INLINE idMat5 &idMat5::operator+=( const idMat5 &a ) {
- mat[0][0] += a[0][0]; mat[0][1] += a[0][1]; mat[0][2] += a[0][2]; mat[0][3] += a[0][3]; mat[0][4] += a[0][4];
- mat[1][0] += a[1][0]; mat[1][1] += a[1][1]; mat[1][2] += a[1][2]; mat[1][3] += a[1][3]; mat[1][4] += a[1][4];
- mat[2][0] += a[2][0]; mat[2][1] += a[2][1]; mat[2][2] += a[2][2]; mat[2][3] += a[2][3]; mat[2][4] += a[2][4];
- mat[3][0] += a[3][0]; mat[3][1] += a[3][1]; mat[3][2] += a[3][2]; mat[3][3] += a[3][3]; mat[3][4] += a[3][4];
- mat[4][0] += a[4][0]; mat[4][1] += a[4][1]; mat[4][2] += a[4][2]; mat[4][3] += a[4][3]; mat[4][4] += a[4][4];
- return *this;
- }
- ID_INLINE idMat5 &idMat5::operator-=( const idMat5 &a ) {
- mat[0][0] -= a[0][0]; mat[0][1] -= a[0][1]; mat[0][2] -= a[0][2]; mat[0][3] -= a[0][3]; mat[0][4] -= a[0][4];
- mat[1][0] -= a[1][0]; mat[1][1] -= a[1][1]; mat[1][2] -= a[1][2]; mat[1][3] -= a[1][3]; mat[1][4] -= a[1][4];
- mat[2][0] -= a[2][0]; mat[2][1] -= a[2][1]; mat[2][2] -= a[2][2]; mat[2][3] -= a[2][3]; mat[2][4] -= a[2][4];
- mat[3][0] -= a[3][0]; mat[3][1] -= a[3][1]; mat[3][2] -= a[3][2]; mat[3][3] -= a[3][3]; mat[3][4] -= a[3][4];
- mat[4][0] -= a[4][0]; mat[4][1] -= a[4][1]; mat[4][2] -= a[4][2]; mat[4][3] -= a[4][3]; mat[4][4] -= a[4][4];
- return *this;
- }
- ID_INLINE idVec5 operator*( const idVec5 &vec, const idMat5 &mat ) {
- return mat * vec;
- }
- ID_INLINE idMat5 operator*( const float a, idMat5 const &mat ) {
- return mat * a;
- }
- ID_INLINE idVec5 &operator*=( idVec5 &vec, const idMat5 &mat ) {
- vec = mat * vec;
- return vec;
- }
- ID_INLINE bool idMat5::Compare( const idMat5 &a ) const {
- dword i;
- const float *ptr1, *ptr2;
- ptr1 = reinterpret_cast<const float *>(mat);
- ptr2 = reinterpret_cast<const float *>(a.mat);
- for ( i = 0; i < 5*5; i++ ) {
- if ( ptr1[i] != ptr2[i] ) {
- return false;
- }
- }
- return true;
- }
- ID_INLINE bool idMat5::Compare( const idMat5 &a, const float epsilon ) const {
- dword i;
- const float *ptr1, *ptr2;
- ptr1 = reinterpret_cast<const float *>(mat);
- ptr2 = reinterpret_cast<const float *>(a.mat);
- for ( i = 0; i < 5*5; i++ ) {
- if ( idMath::Fabs( ptr1[i] - ptr2[i] ) > epsilon ) {
- return false;
- }
- }
- return true;
- }
- ID_INLINE bool idMat5::operator==( const idMat5 &a ) const {
- return Compare( a );
- }
- ID_INLINE bool idMat5::operator!=( const idMat5 &a ) const {
- return !Compare( a );
- }
- ID_INLINE void idMat5::Zero() {
- memset( mat, 0, sizeof( idMat5 ) );
- }
- ID_INLINE void idMat5::Identity() {
- *this = mat5_identity;
- }
- ID_INLINE bool idMat5::IsIdentity( const float epsilon ) const {
- return Compare( mat5_identity, epsilon );
- }
- ID_INLINE bool idMat5::IsSymmetric( const float epsilon ) const {
- for ( int i = 1; i < 5; i++ ) {
- for ( int j = 0; j < i; j++ ) {
- if ( idMath::Fabs( mat[i][j] - mat[j][i] ) > epsilon ) {
- return false;
- }
- }
- }
- return true;
- }
- ID_INLINE bool idMat5::IsDiagonal( const float epsilon ) const {
- for ( int i = 0; i < 5; i++ ) {
- for ( int j = 0; j < 5; j++ ) {
- if ( i != j && idMath::Fabs( mat[i][j] ) > epsilon ) {
- return false;
- }
- }
- }
- return true;
- }
- ID_INLINE float idMat5::Trace() const {
- return ( mat[0][0] + mat[1][1] + mat[2][2] + mat[3][3] + mat[4][4] );
- }
- ID_INLINE idMat5 idMat5::Inverse() const {
- idMat5 invMat;
- invMat = *this;
- verify( invMat.InverseSelf() );
- return invMat;
- }
- ID_INLINE idMat5 idMat5::InverseFast() const {
- idMat5 invMat;
- invMat = *this;
- verify( invMat.InverseFastSelf() );
- return invMat;
- }
- ID_INLINE int idMat5::GetDimension() const {
- return 25;
- }
- ID_INLINE const float *idMat5::ToFloatPtr() const {
- return mat[0].ToFloatPtr();
- }
- ID_INLINE float *idMat5::ToFloatPtr() {
- return mat[0].ToFloatPtr();
- }
- //===============================================================
- //
- // idMat6 - 6x6 matrix
- //
- //===============================================================
- class idMat6 {
- public:
- idMat6();
- explicit idMat6( const idVec6 &v0, const idVec6 &v1, const idVec6 &v2, const idVec6 &v3, const idVec6 &v4, const idVec6 &v5 );
- explicit idMat6( const idMat3 &m0, const idMat3 &m1, const idMat3 &m2, const idMat3 &m3 );
- explicit idMat6( const float src[ 6 ][ 6 ] );
- const idVec6 & operator[]( int index ) const;
- idVec6 & operator[]( int index );
- idMat6 operator*( const float a ) const;
- idVec6 operator*( const idVec6 &vec ) const;
- idMat6 operator*( const idMat6 &a ) const;
- idMat6 operator+( const idMat6 &a ) const;
- idMat6 operator-( const idMat6 &a ) const;
- idMat6 & operator*=( const float a );
- idMat6 & operator*=( const idMat6 &a );
- idMat6 & operator+=( const idMat6 &a );
- idMat6 & operator-=( const idMat6 &a );
- friend idMat6 operator*( const float a, const idMat6 &mat );
- friend idVec6 operator*( const idVec6 &vec, const idMat6 &mat );
- friend idVec6 & operator*=( idVec6 &vec, const idMat6 &mat );
- bool Compare( const idMat6 &a ) const; // exact compare, no epsilon
- bool Compare( const idMat6 &a, const float epsilon ) const; // compare with epsilon
- bool operator==( const idMat6 &a ) const; // exact compare, no epsilon
- bool operator!=( const idMat6 &a ) const; // exact compare, no epsilon
- void Zero();
- void Identity();
- bool IsIdentity( const float epsilon = MATRIX_EPSILON ) const;
- bool IsSymmetric( const float epsilon = MATRIX_EPSILON ) const;
- bool IsDiagonal( const float epsilon = MATRIX_EPSILON ) const;
- idMat3 SubMat3( int n ) const;
- float Trace() const;
- float Determinant() const;
- idMat6 Transpose() const; // returns transpose
- idMat6 & TransposeSelf();
- idMat6 Inverse() const; // returns the inverse ( m * m.Inverse() = identity )
- bool InverseSelf(); // returns false if determinant is zero
- idMat6 InverseFast() const; // returns the inverse ( m * m.Inverse() = identity )
- bool InverseFastSelf(); // returns false if determinant is zero
- int GetDimension() const;
- const float * ToFloatPtr() const;
- float * ToFloatPtr();
- const char * ToString( int precision = 2 ) const;
- private:
- idVec6 mat[ 6 ];
- };
- extern idMat6 mat6_zero;
- extern idMat6 mat6_identity;
- #define mat6_default mat6_identity
- ID_INLINE idMat6::idMat6() {
- }
- ID_INLINE idMat6::idMat6( const idMat3 &m0, const idMat3 &m1, const idMat3 &m2, const idMat3 &m3 ) {
- mat[0] = idVec6( m0[0][0], m0[0][1], m0[0][2], m1[0][0], m1[0][1], m1[0][2] );
- mat[1] = idVec6( m0[1][0], m0[1][1], m0[1][2], m1[1][0], m1[1][1], m1[1][2] );
- mat[2] = idVec6( m0[2][0], m0[2][1], m0[2][2], m1[2][0], m1[2][1], m1[2][2] );
- mat[3] = idVec6( m2[0][0], m2[0][1], m2[0][2], m3[0][0], m3[0][1], m3[0][2] );
- mat[4] = idVec6( m2[1][0], m2[1][1], m2[1][2], m3[1][0], m3[1][1], m3[1][2] );
- mat[5] = idVec6( m2[2][0], m2[2][1], m2[2][2], m3[2][0], m3[2][1], m3[2][2] );
- }
- ID_INLINE idMat6::idMat6( const idVec6 &v0, const idVec6 &v1, const idVec6 &v2, const idVec6 &v3, const idVec6 &v4, const idVec6 &v5 ) {
- mat[0] = v0;
- mat[1] = v1;
- mat[2] = v2;
- mat[3] = v3;
- mat[4] = v4;
- mat[5] = v5;
- }
- ID_INLINE idMat6::idMat6( const float src[ 6 ][ 6 ] ) {
- memcpy( mat, src, 6 * 6 * sizeof( float ) );
- }
- ID_INLINE const idVec6 &idMat6::operator[]( int index ) const {
- //assert( ( index >= 0 ) && ( index < 6 ) );
- return mat[ index ];
- }
- ID_INLINE idVec6 &idMat6::operator[]( int index ) {
- //assert( ( index >= 0 ) && ( index < 6 ) );
- return mat[ index ];
- }
- ID_INLINE idMat6 idMat6::operator*( const idMat6 &a ) const {
- int i, j;
- const float *m1Ptr, *m2Ptr;
- float *dstPtr;
- idMat6 dst;
- m1Ptr = reinterpret_cast<const float *>(this);
- m2Ptr = reinterpret_cast<const float *>(&a);
- dstPtr = reinterpret_cast<float *>(&dst);
- for ( i = 0; i < 6; i++ ) {
- for ( j = 0; j < 6; j++ ) {
- *dstPtr = m1Ptr[0] * m2Ptr[ 0 * 6 + j ]
- + m1Ptr[1] * m2Ptr[ 1 * 6 + j ]
- + m1Ptr[2] * m2Ptr[ 2 * 6 + j ]
- + m1Ptr[3] * m2Ptr[ 3 * 6 + j ]
- + m1Ptr[4] * m2Ptr[ 4 * 6 + j ]
- + m1Ptr[5] * m2Ptr[ 5 * 6 + j ];
- dstPtr++;
- }
- m1Ptr += 6;
- }
- return dst;
- }
- ID_INLINE idMat6 idMat6::operator*( const float a ) const {
- return idMat6(
- idVec6( mat[0][0] * a, mat[0][1] * a, mat[0][2] * a, mat[0][3] * a, mat[0][4] * a, mat[0][5] * a ),
- idVec6( mat[1][0] * a, mat[1][1] * a, mat[1][2] * a, mat[1][3] * a, mat[1][4] * a, mat[1][5] * a ),
- idVec6( mat[2][0] * a, mat[2][1] * a, mat[2][2] * a, mat[2][3] * a, mat[2][4] * a, mat[2][5] * a ),
- idVec6( mat[3][0] * a, mat[3][1] * a, mat[3][2] * a, mat[3][3] * a, mat[3][4] * a, mat[3][5] * a ),
- idVec6( mat[4][0] * a, mat[4][1] * a, mat[4][2] * a, mat[4][3] * a, mat[4][4] * a, mat[4][5] * a ),
- idVec6( mat[5][0] * a, mat[5][1] * a, mat[5][2] * a, mat[5][3] * a, mat[5][4] * a, mat[5][5] * a ) );
- }
- ID_INLINE idVec6 idMat6::operator*( const idVec6 &vec ) const {
- return idVec6(
- mat[0][0] * vec[0] + mat[0][1] * vec[1] + mat[0][2] * vec[2] + mat[0][3] * vec[3] + mat[0][4] * vec[4] + mat[0][5] * vec[5],
- mat[1][0] * vec[0] + mat[1][1] * vec[1] + mat[1][2] * vec[2] + mat[1][3] * vec[3] + mat[1][4] * vec[4] + mat[1][5] * vec[5],
- mat[2][0] * vec[0] + mat[2][1] * vec[1] + mat[2][2] * vec[2] + mat[2][3] * vec[3] + mat[2][4] * vec[4] + mat[2][5] * vec[5],
- mat[3][0] * vec[0] + mat[3][1] * vec[1] + mat[3][2] * vec[2] + mat[3][3] * vec[3] + mat[3][4] * vec[4] + mat[3][5] * vec[5],
- mat[4][0] * vec[0] + mat[4][1] * vec[1] + mat[4][2] * vec[2] + mat[4][3] * vec[3] + mat[4][4] * vec[4] + mat[4][5] * vec[5],
- mat[5][0] * vec[0] + mat[5][1] * vec[1] + mat[5][2] * vec[2] + mat[5][3] * vec[3] + mat[5][4] * vec[4] + mat[5][5] * vec[5] );
- }
- ID_INLINE idMat6 idMat6::operator+( const idMat6 &a ) const {
- return idMat6(
- idVec6( mat[0][0] + a[0][0], mat[0][1] + a[0][1], mat[0][2] + a[0][2], mat[0][3] + a[0][3], mat[0][4] + a[0][4], mat[0][5] + a[0][5] ),
- idVec6( mat[1][0] + a[1][0], mat[1][1] + a[1][1], mat[1][2] + a[1][2], mat[1][3] + a[1][3], mat[1][4] + a[1][4], mat[1][5] + a[1][5] ),
- idVec6( mat[2][0] + a[2][0], mat[2][1] + a[2][1], mat[2][2] + a[2][2], mat[2][3] + a[2][3], mat[2][4] + a[2][4], mat[2][5] + a[2][5] ),
- idVec6( mat[3][0] + a[3][0], mat[3][1] + a[3][1], mat[3][2] + a[3][2], mat[3][3] + a[3][3], mat[3][4] + a[3][4], mat[3][5] + a[3][5] ),
- idVec6( mat[4][0] + a[4][0], mat[4][1] + a[4][1], mat[4][2] + a[4][2], mat[4][3] + a[4][3], mat[4][4] + a[4][4], mat[4][5] + a[4][5] ),
- idVec6( mat[5][0] + a[5][0], mat[5][1] + a[5][1], mat[5][2] + a[5][2], mat[5][3] + a[5][3], mat[5][4] + a[5][4], mat[5][5] + a[5][5] ) );
- }
- ID_INLINE idMat6 idMat6::operator-( const idMat6 &a ) const {
- return idMat6(
- idVec6( mat[0][0] - a[0][0], mat[0][1] - a[0][1], mat[0][2] - a[0][2], mat[0][3] - a[0][3], mat[0][4] - a[0][4], mat[0][5] - a[0][5] ),
- idVec6( mat[1][0] - a[1][0], mat[1][1] - a[1][1], mat[1][2] - a[1][2], mat[1][3] - a[1][3], mat[1][4] - a[1][4], mat[1][5] - a[1][5] ),
- idVec6( mat[2][0] - a[2][0], mat[2][1] - a[2][1], mat[2][2] - a[2][2], mat[2][3] - a[2][3], mat[2][4] - a[2][4], mat[2][5] - a[2][5] ),
- idVec6( mat[3][0] - a[3][0], mat[3][1] - a[3][1], mat[3][2] - a[3][2], mat[3][3] - a[3][3], mat[3][4] - a[3][4], mat[3][5] - a[3][5] ),
- idVec6( mat[4][0] - a[4][0], mat[4][1] - a[4][1], mat[4][2] - a[4][2], mat[4][3] - a[4][3], mat[4][4] - a[4][4], mat[4][5] - a[4][5] ),
- idVec6( mat[5][0] - a[5][0], mat[5][1] - a[5][1], mat[5][2] - a[5][2], mat[5][3] - a[5][3], mat[5][4] - a[5][4], mat[5][5] - a[5][5] ) );
- }
- ID_INLINE idMat6 &idMat6::operator*=( const float a ) {
- mat[0][0] *= a; mat[0][1] *= a; mat[0][2] *= a; mat[0][3] *= a; mat[0][4] *= a; mat[0][5] *= a;
- mat[1][0] *= a; mat[1][1] *= a; mat[1][2] *= a; mat[1][3] *= a; mat[1][4] *= a; mat[1][5] *= a;
- mat[2][0] *= a; mat[2][1] *= a; mat[2][2] *= a; mat[2][3] *= a; mat[2][4] *= a; mat[2][5] *= a;
- mat[3][0] *= a; mat[3][1] *= a; mat[3][2] *= a; mat[3][3] *= a; mat[3][4] *= a; mat[3][5] *= a;
- mat[4][0] *= a; mat[4][1] *= a; mat[4][2] *= a; mat[4][3] *= a; mat[4][4] *= a; mat[4][5] *= a;
- mat[5][0] *= a; mat[5][1] *= a; mat[5][2] *= a; mat[5][3] *= a; mat[5][4] *= a; mat[5][5] *= a;
- return *this;
- }
- ID_INLINE idMat6 &idMat6::operator*=( const idMat6 &a ) {
- *this = *this * a;
- return *this;
- }
- ID_INLINE idMat6 &idMat6::operator+=( const idMat6 &a ) {
- mat[0][0] += a[0][0]; mat[0][1] += a[0][1]; mat[0][2] += a[0][2]; mat[0][3] += a[0][3]; mat[0][4] += a[0][4]; mat[0][5] += a[0][5];
- mat[1][0] += a[1][0]; mat[1][1] += a[1][1]; mat[1][2] += a[1][2]; mat[1][3] += a[1][3]; mat[1][4] += a[1][4]; mat[1][5] += a[1][5];
- mat[2][0] += a[2][0]; mat[2][1] += a[2][1]; mat[2][2] += a[2][2]; mat[2][3] += a[2][3]; mat[2][4] += a[2][4]; mat[2][5] += a[2][5];
- mat[3][0] += a[3][0]; mat[3][1] += a[3][1]; mat[3][2] += a[3][2]; mat[3][3] += a[3][3]; mat[3][4] += a[3][4]; mat[3][5] += a[3][5];
- mat[4][0] += a[4][0]; mat[4][1] += a[4][1]; mat[4][2] += a[4][2]; mat[4][3] += a[4][3]; mat[4][4] += a[4][4]; mat[4][5] += a[4][5];
- mat[5][0] += a[5][0]; mat[5][1] += a[5][1]; mat[5][2] += a[5][2]; mat[5][3] += a[5][3]; mat[5][4] += a[5][4]; mat[5][5] += a[5][5];
- return *this;
- }
- ID_INLINE idMat6 &idMat6::operator-=( const idMat6 &a ) {
- mat[0][0] -= a[0][0]; mat[0][1] -= a[0][1]; mat[0][2] -= a[0][2]; mat[0][3] -= a[0][3]; mat[0][4] -= a[0][4]; mat[0][5] -= a[0][5];
- mat[1][0] -= a[1][0]; mat[1][1] -= a[1][1]; mat[1][2] -= a[1][2]; mat[1][3] -= a[1][3]; mat[1][4] -= a[1][4]; mat[1][5] -= a[1][5];
- mat[2][0] -= a[2][0]; mat[2][1] -= a[2][1]; mat[2][2] -= a[2][2]; mat[2][3] -= a[2][3]; mat[2][4] -= a[2][4]; mat[2][5] -= a[2][5];
- mat[3][0] -= a[3][0]; mat[3][1] -= a[3][1]; mat[3][2] -= a[3][2]; mat[3][3] -= a[3][3]; mat[3][4] -= a[3][4]; mat[3][5] -= a[3][5];
- mat[4][0] -= a[4][0]; mat[4][1] -= a[4][1]; mat[4][2] -= a[4][2]; mat[4][3] -= a[4][3]; mat[4][4] -= a[4][4]; mat[4][5] -= a[4][5];
- mat[5][0] -= a[5][0]; mat[5][1] -= a[5][1]; mat[5][2] -= a[5][2]; mat[5][3] -= a[5][3]; mat[5][4] -= a[5][4]; mat[5][5] -= a[5][5];
- return *this;
- }
- ID_INLINE idVec6 operator*( const idVec6 &vec, const idMat6 &mat ) {
- return mat * vec;
- }
- ID_INLINE idMat6 operator*( const float a, idMat6 const &mat ) {
- return mat * a;
- }
- ID_INLINE idVec6 &operator*=( idVec6 &vec, const idMat6 &mat ) {
- vec = mat * vec;
- return vec;
- }
- ID_INLINE bool idMat6::Compare( const idMat6 &a ) const {
- dword i;
- const float *ptr1, *ptr2;
- ptr1 = reinterpret_cast<const float *>(mat);
- ptr2 = reinterpret_cast<const float *>(a.mat);
- for ( i = 0; i < 6*6; i++ ) {
- if ( ptr1[i] != ptr2[i] ) {
- return false;
- }
- }
- return true;
- }
- ID_INLINE bool idMat6::Compare( const idMat6 &a, const float epsilon ) const {
- dword i;
- const float *ptr1, *ptr2;
- ptr1 = reinterpret_cast<const float *>(mat);
- ptr2 = reinterpret_cast<const float *>(a.mat);
- for ( i = 0; i < 6*6; i++ ) {
- if ( idMath::Fabs( ptr1[i] - ptr2[i] ) > epsilon ) {
- return false;
- }
- }
- return true;
- }
- ID_INLINE bool idMat6::operator==( const idMat6 &a ) const {
- return Compare( a );
- }
- ID_INLINE bool idMat6::operator!=( const idMat6 &a ) const {
- return !Compare( a );
- }
- ID_INLINE void idMat6::Zero() {
- memset( mat, 0, sizeof( idMat6 ) );
- }
- ID_INLINE void idMat6::Identity() {
- *this = mat6_identity;
- }
- ID_INLINE bool idMat6::IsIdentity( const float epsilon ) const {
- return Compare( mat6_identity, epsilon );
- }
- ID_INLINE bool idMat6::IsSymmetric( const float epsilon ) const {
- for ( int i = 1; i < 6; i++ ) {
- for ( int j = 0; j < i; j++ ) {
- if ( idMath::Fabs( mat[i][j] - mat[j][i] ) > epsilon ) {
- return false;
- }
- }
- }
- return true;
- }
- ID_INLINE bool idMat6::IsDiagonal( const float epsilon ) const {
- for ( int i = 0; i < 6; i++ ) {
- for ( int j = 0; j < 6; j++ ) {
- if ( i != j && idMath::Fabs( mat[i][j] ) > epsilon ) {
- return false;
- }
- }
- }
- return true;
- }
- ID_INLINE idMat3 idMat6::SubMat3( int n ) const {
- assert( n >= 0 && n < 4 );
- int b0 = ((n & 2) >> 1) * 3;
- int b1 = (n & 1) * 3;
- return idMat3(
- mat[b0 + 0][b1 + 0], mat[b0 + 0][b1 + 1], mat[b0 + 0][b1 + 2],
- mat[b0 + 1][b1 + 0], mat[b0 + 1][b1 + 1], mat[b0 + 1][b1 + 2],
- mat[b0 + 2][b1 + 0], mat[b0 + 2][b1 + 1], mat[b0 + 2][b1 + 2] );
- }
- ID_INLINE float idMat6::Trace() const {
- return ( mat[0][0] + mat[1][1] + mat[2][2] + mat[3][3] + mat[4][4] + mat[5][5] );
- }
- ID_INLINE idMat6 idMat6::Inverse() const {
- idMat6 invMat;
- invMat = *this;
- verify( invMat.InverseSelf() );
- return invMat;
- }
- ID_INLINE idMat6 idMat6::InverseFast() const {
- idMat6 invMat;
- invMat = *this;
- verify( invMat.InverseFastSelf() );
- return invMat;
- }
- ID_INLINE int idMat6::GetDimension() const {
- return 36;
- }
- ID_INLINE const float *idMat6::ToFloatPtr() const {
- return mat[0].ToFloatPtr();
- }
- ID_INLINE float *idMat6::ToFloatPtr() {
- return mat[0].ToFloatPtr();
- }
- #endif /* !__MATH_MATRIX_H__ */
|