12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003 |
- /*
- ===========================================================================
- Doom 3 GPL Source Code
- Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
- This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
- Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
- In addition, the Doom 3 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 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_VECTOR_H__
- #define __MATH_VECTOR_H__
- /*
- ===============================================================================
- Vector classes
- ===============================================================================
- */
- #define VECTOR_EPSILON 0.001f
- class idAngles;
- class idPolar3;
- class idMat3;
- //===============================================================
- //
- // idVec2 - 2D vector
- //
- //===============================================================
- class idVec2 {
- public:
- float x;
- float y;
- idVec2( void );
- explicit idVec2( const float x, const float y );
- void Set( const float x, const float y );
- void Zero( void );
- float operator[]( int index ) const;
- float & operator[]( int index );
- idVec2 operator-() const;
- float operator*( const idVec2 &a ) const;
- idVec2 operator*( const float a ) const;
- idVec2 operator/( const float a ) const;
- idVec2 operator+( const idVec2 &a ) const;
- idVec2 operator-( const idVec2 &a ) const;
- idVec2 & operator+=( const idVec2 &a );
- idVec2 & operator-=( const idVec2 &a );
- idVec2 & operator/=( const idVec2 &a );
- idVec2 & operator/=( const float a );
- idVec2 & operator*=( const float a );
- friend idVec2 operator*( const float a, const idVec2 b );
- bool Compare( const idVec2 &a ) const; // exact compare, no epsilon
- bool Compare( const idVec2 &a, const float epsilon ) const; // compare with epsilon
- bool operator==( const idVec2 &a ) const; // exact compare, no epsilon
- bool operator!=( const idVec2 &a ) const; // exact compare, no epsilon
- float Length( void ) const;
- float LengthFast( void ) const;
- float LengthSqr( void ) const;
- float Normalize( void ); // returns length
- float NormalizeFast( void ); // returns length
- idVec2 & Truncate( float length ); // cap length
- void Clamp( const idVec2 &min, const idVec2 &max );
- void Snap( void ); // snap to closest integer value
- void SnapInt( void ); // snap towards integer (floor)
- int GetDimension( void ) const;
- const float * ToFloatPtr( void ) const;
- float * ToFloatPtr( void );
- const char * ToString( int precision = 2 ) const;
- void Lerp( const idVec2 &v1, const idVec2 &v2, const float l );
- };
- extern idVec2 vec2_origin;
- #define vec2_zero vec2_origin
- ID_INLINE idVec2::idVec2( void ) {
- }
- ID_INLINE idVec2::idVec2( const float x, const float y ) {
- this->x = x;
- this->y = y;
- }
- ID_INLINE void idVec2::Set( const float x, const float y ) {
- this->x = x;
- this->y = y;
- }
- ID_INLINE void idVec2::Zero( void ) {
- x = y = 0.0f;
- }
- ID_INLINE bool idVec2::Compare( const idVec2 &a ) const {
- return ( ( x == a.x ) && ( y == a.y ) );
- }
- ID_INLINE bool idVec2::Compare( const idVec2 &a, const float epsilon ) const {
- if ( idMath::Fabs( x - a.x ) > epsilon ) {
- return false;
- }
-
- if ( idMath::Fabs( y - a.y ) > epsilon ) {
- return false;
- }
- return true;
- }
- ID_INLINE bool idVec2::operator==( const idVec2 &a ) const {
- return Compare( a );
- }
- ID_INLINE bool idVec2::operator!=( const idVec2 &a ) const {
- return !Compare( a );
- }
- ID_INLINE float idVec2::operator[]( int index ) const {
- return ( &x )[ index ];
- }
- ID_INLINE float& idVec2::operator[]( int index ) {
- return ( &x )[ index ];
- }
- ID_INLINE float idVec2::Length( void ) const {
- return ( float )idMath::Sqrt( x * x + y * y );
- }
- ID_INLINE float idVec2::LengthFast( void ) const {
- float sqrLength;
- sqrLength = x * x + y * y;
- return sqrLength * idMath::RSqrt( sqrLength );
- }
- ID_INLINE float idVec2::LengthSqr( void ) const {
- return ( x * x + y * y );
- }
- ID_INLINE float idVec2::Normalize( void ) {
- float sqrLength, invLength;
- sqrLength = x * x + y * y;
- invLength = idMath::InvSqrt( sqrLength );
- x *= invLength;
- y *= invLength;
- return invLength * sqrLength;
- }
- ID_INLINE float idVec2::NormalizeFast( void ) {
- float lengthSqr, invLength;
- lengthSqr = x * x + y * y;
- invLength = idMath::RSqrt( lengthSqr );
- x *= invLength;
- y *= invLength;
- return invLength * lengthSqr;
- }
- ID_INLINE idVec2 &idVec2::Truncate( float length ) {
- float length2;
- float ilength;
- if ( !length ) {
- Zero();
- }
- else {
- length2 = LengthSqr();
- if ( length2 > length * length ) {
- ilength = length * idMath::InvSqrt( length2 );
- x *= ilength;
- y *= ilength;
- }
- }
- return *this;
- }
- ID_INLINE void idVec2::Clamp( const idVec2 &min, const idVec2 &max ) {
- if ( x < min.x ) {
- x = min.x;
- } else if ( x > max.x ) {
- x = max.x;
- }
- if ( y < min.y ) {
- y = min.y;
- } else if ( y > max.y ) {
- y = max.y;
- }
- }
- ID_INLINE void idVec2::Snap( void ) {
- x = floor( x + 0.5f );
- y = floor( y + 0.5f );
- }
- ID_INLINE void idVec2::SnapInt( void ) {
- x = float( int( x ) );
- y = float( int( y ) );
- }
- ID_INLINE idVec2 idVec2::operator-() const {
- return idVec2( -x, -y );
- }
-
- ID_INLINE idVec2 idVec2::operator-( const idVec2 &a ) const {
- return idVec2( x - a.x, y - a.y );
- }
- ID_INLINE float idVec2::operator*( const idVec2 &a ) const {
- return x * a.x + y * a.y;
- }
- ID_INLINE idVec2 idVec2::operator*( const float a ) const {
- return idVec2( x * a, y * a );
- }
- ID_INLINE idVec2 idVec2::operator/( const float a ) const {
- float inva = 1.0f / a;
- return idVec2( x * inva, y * inva );
- }
- ID_INLINE idVec2 operator*( const float a, const idVec2 b ) {
- return idVec2( b.x * a, b.y * a );
- }
- ID_INLINE idVec2 idVec2::operator+( const idVec2 &a ) const {
- return idVec2( x + a.x, y + a.y );
- }
- ID_INLINE idVec2 &idVec2::operator+=( const idVec2 &a ) {
- x += a.x;
- y += a.y;
- return *this;
- }
- ID_INLINE idVec2 &idVec2::operator/=( const idVec2 &a ) {
- x /= a.x;
- y /= a.y;
- return *this;
- }
- ID_INLINE idVec2 &idVec2::operator/=( const float a ) {
- float inva = 1.0f / a;
- x *= inva;
- y *= inva;
- return *this;
- }
- ID_INLINE idVec2 &idVec2::operator-=( const idVec2 &a ) {
- x -= a.x;
- y -= a.y;
- return *this;
- }
- ID_INLINE idVec2 &idVec2::operator*=( const float a ) {
- x *= a;
- y *= a;
- return *this;
- }
- ID_INLINE int idVec2::GetDimension( void ) const {
- return 2;
- }
- ID_INLINE const float *idVec2::ToFloatPtr( void ) const {
- return &x;
- }
- ID_INLINE float *idVec2::ToFloatPtr( void ) {
- return &x;
- }
- //===============================================================
- //
- // idVec3 - 3D vector
- //
- //===============================================================
- class idVec3 {
- public:
- float x;
- float y;
- float z;
- idVec3( void );
- explicit idVec3( const float x, const float y, const float z );
- void Set( const float x, const float y, const float z );
- void Zero( void );
- float operator[]( const int index ) const;
- float & operator[]( const int index );
- idVec3 operator-() const;
- idVec3 & operator=( const idVec3 &a ); // required because of a msvc 6 & 7 bug
- float operator*( const idVec3 &a ) const;
- idVec3 operator*( const float a ) const;
- idVec3 operator/( const float a ) const;
- idVec3 operator+( const idVec3 &a ) const;
- idVec3 operator-( const idVec3 &a ) const;
- idVec3 & operator+=( const idVec3 &a );
- idVec3 & operator-=( const idVec3 &a );
- idVec3 & operator/=( const idVec3 &a );
- idVec3 & operator/=( const float a );
- idVec3 & operator*=( const float a );
- friend idVec3 operator*( const float a, const idVec3 b );
- bool Compare( const idVec3 &a ) const; // exact compare, no epsilon
- bool Compare( const idVec3 &a, const float epsilon ) const; // compare with epsilon
- bool operator==( const idVec3 &a ) const; // exact compare, no epsilon
- bool operator!=( const idVec3 &a ) const; // exact compare, no epsilon
- bool FixDegenerateNormal( void ); // fix degenerate axial cases
- bool FixDenormals( void ); // change tiny numbers to zero
- idVec3 Cross( const idVec3 &a ) const;
- idVec3 & Cross( const idVec3 &a, const idVec3 &b );
- float Length( void ) const;
- float LengthSqr( void ) const;
- float LengthFast( void ) const;
- float Normalize( void ); // returns length
- float NormalizeFast( void ); // returns length
- idVec3 & Truncate( float length ); // cap length
- void Clamp( const idVec3 &min, const idVec3 &max );
- void Snap( void ); // snap to closest integer value
- void SnapInt( void ); // snap towards integer (floor)
- int GetDimension( void ) const;
- float ToYaw( void ) const;
- float ToPitch( void ) const;
- idAngles ToAngles( void ) const;
- idPolar3 ToPolar( void ) const;
- idMat3 ToMat3( void ) const; // vector should be normalized
- const idVec2 & ToVec2( void ) const;
- idVec2 & ToVec2( void );
- const float * ToFloatPtr( void ) const;
- float * ToFloatPtr( void );
- const char * ToString( int precision = 2 ) const;
- void NormalVectors( idVec3 &left, idVec3 &down ) const; // vector should be normalized
- void OrthogonalBasis( idVec3 &left, idVec3 &up ) const;
- void ProjectOntoPlane( const idVec3 &normal, const float overBounce = 1.0f );
- bool ProjectAlongPlane( const idVec3 &normal, const float epsilon, const float overBounce = 1.0f );
- void ProjectSelfOntoSphere( const float radius );
- void Lerp( const idVec3 &v1, const idVec3 &v2, const float l );
- void SLerp( const idVec3 &v1, const idVec3 &v2, const float l );
- };
- extern idVec3 vec3_origin;
- #define vec3_zero vec3_origin
- ID_INLINE idVec3::idVec3( void ) {
- }
- ID_INLINE idVec3::idVec3( const float x, const float y, const float z ) {
- this->x = x;
- this->y = y;
- this->z = z;
- }
- ID_INLINE float idVec3::operator[]( const int index ) const {
- return ( &x )[ index ];
- }
- ID_INLINE float &idVec3::operator[]( const int index ) {
- return ( &x )[ index ];
- }
- ID_INLINE void idVec3::Set( const float x, const float y, const float z ) {
- this->x = x;
- this->y = y;
- this->z = z;
- }
- ID_INLINE void idVec3::Zero( void ) {
- x = y = z = 0.0f;
- }
- ID_INLINE idVec3 idVec3::operator-() const {
- return idVec3( -x, -y, -z );
- }
- ID_INLINE idVec3 &idVec3::operator=( const idVec3 &a ) {
- x = a.x;
- y = a.y;
- z = a.z;
- return *this;
- }
- ID_INLINE idVec3 idVec3::operator-( const idVec3 &a ) const {
- return idVec3( x - a.x, y - a.y, z - a.z );
- }
- ID_INLINE float idVec3::operator*( const idVec3 &a ) const {
- return x * a.x + y * a.y + z * a.z;
- }
- ID_INLINE idVec3 idVec3::operator*( const float a ) const {
- return idVec3( x * a, y * a, z * a );
- }
- ID_INLINE idVec3 idVec3::operator/( const float a ) const {
- float inva = 1.0f / a;
- return idVec3( x * inva, y * inva, z * inva );
- }
- ID_INLINE idVec3 operator*( const float a, const idVec3 b ) {
- return idVec3( b.x * a, b.y * a, b.z * a );
- }
- ID_INLINE idVec3 idVec3::operator+( const idVec3 &a ) const {
- return idVec3( x + a.x, y + a.y, z + a.z );
- }
- ID_INLINE idVec3 &idVec3::operator+=( const idVec3 &a ) {
- x += a.x;
- y += a.y;
- z += a.z;
- return *this;
- }
- ID_INLINE idVec3 &idVec3::operator/=( const idVec3 &a ) {
- x /= a.x;
- y /= a.y;
- z /= a.z;
- return *this;
- }
- ID_INLINE idVec3 &idVec3::operator/=( const float a ) {
- float inva = 1.0f / a;
- x *= inva;
- y *= inva;
- z *= inva;
- return *this;
- }
- ID_INLINE idVec3 &idVec3::operator-=( const idVec3 &a ) {
- x -= a.x;
- y -= a.y;
- z -= a.z;
- return *this;
- }
- ID_INLINE idVec3 &idVec3::operator*=( const float a ) {
- x *= a;
- y *= a;
- z *= a;
- return *this;
- }
- ID_INLINE bool idVec3::Compare( const idVec3 &a ) const {
- return ( ( x == a.x ) && ( y == a.y ) && ( z == a.z ) );
- }
- ID_INLINE bool idVec3::Compare( const idVec3 &a, const float epsilon ) const {
- if ( idMath::Fabs( x - a.x ) > epsilon ) {
- return false;
- }
-
- if ( idMath::Fabs( y - a.y ) > epsilon ) {
- return false;
- }
- if ( idMath::Fabs( z - a.z ) > epsilon ) {
- return false;
- }
- return true;
- }
- ID_INLINE bool idVec3::operator==( const idVec3 &a ) const {
- return Compare( a );
- }
- ID_INLINE bool idVec3::operator!=( const idVec3 &a ) const {
- return !Compare( a );
- }
- ID_INLINE float idVec3::NormalizeFast( void ) {
- float sqrLength, invLength;
- sqrLength = x * x + y * y + z * z;
- invLength = idMath::RSqrt( sqrLength );
- x *= invLength;
- y *= invLength;
- z *= invLength;
- return invLength * sqrLength;
- }
- ID_INLINE bool idVec3::FixDegenerateNormal( void ) {
- if ( x == 0.0f ) {
- if ( y == 0.0f ) {
- if ( z > 0.0f ) {
- if ( z != 1.0f ) {
- z = 1.0f;
- return true;
- }
- } else {
- if ( z != -1.0f ) {
- z = -1.0f;
- return true;
- }
- }
- return false;
- } else if ( z == 0.0f ) {
- if ( y > 0.0f ) {
- if ( y != 1.0f ) {
- y = 1.0f;
- return true;
- }
- } else {
- if ( y != -1.0f ) {
- y = -1.0f;
- return true;
- }
- }
- return false;
- }
- } else if ( y == 0.0f ) {
- if ( z == 0.0f ) {
- if ( x > 0.0f ) {
- if ( x != 1.0f ) {
- x = 1.0f;
- return true;
- }
- } else {
- if ( x != -1.0f ) {
- x = -1.0f;
- return true;
- }
- }
- return false;
- }
- }
- if ( idMath::Fabs( x ) == 1.0f ) {
- if ( y != 0.0f || z != 0.0f ) {
- y = z = 0.0f;
- return true;
- }
- return false;
- } else if ( idMath::Fabs( y ) == 1.0f ) {
- if ( x != 0.0f || z != 0.0f ) {
- x = z = 0.0f;
- return true;
- }
- return false;
- } else if ( idMath::Fabs( z ) == 1.0f ) {
- if ( x != 0.0f || y != 0.0f ) {
- x = y = 0.0f;
- return true;
- }
- return false;
- }
- return false;
- }
- ID_INLINE bool idVec3::FixDenormals( void ) {
- bool denormal = false;
- if ( fabs( x ) < 1e-30f ) {
- x = 0.0f;
- denormal = true;
- }
- if ( fabs( y ) < 1e-30f ) {
- y = 0.0f;
- denormal = true;
- }
- if ( fabs( z ) < 1e-30f ) {
- z = 0.0f;
- denormal = true;
- }
- return denormal;
- }
- ID_INLINE idVec3 idVec3::Cross( const idVec3 &a ) const {
- return idVec3( y * a.z - z * a.y, z * a.x - x * a.z, x * a.y - y * a.x );
- }
- ID_INLINE idVec3 &idVec3::Cross( const idVec3 &a, const idVec3 &b ) {
- x = a.y * b.z - a.z * b.y;
- y = a.z * b.x - a.x * b.z;
- z = a.x * b.y - a.y * b.x;
- return *this;
- }
- ID_INLINE float idVec3::Length( void ) const {
- return ( float )idMath::Sqrt( x * x + y * y + z * z );
- }
- ID_INLINE float idVec3::LengthSqr( void ) const {
- return ( x * x + y * y + z * z );
- }
- ID_INLINE float idVec3::LengthFast( void ) const {
- float sqrLength;
- sqrLength = x * x + y * y + z * z;
- return sqrLength * idMath::RSqrt( sqrLength );
- }
- ID_INLINE float idVec3::Normalize( void ) {
- float sqrLength, invLength;
- sqrLength = x * x + y * y + z * z;
- invLength = idMath::InvSqrt( sqrLength );
- x *= invLength;
- y *= invLength;
- z *= invLength;
- return invLength * sqrLength;
- }
- ID_INLINE idVec3 &idVec3::Truncate( float length ) {
- float length2;
- float ilength;
- if ( !length ) {
- Zero();
- }
- else {
- length2 = LengthSqr();
- if ( length2 > length * length ) {
- ilength = length * idMath::InvSqrt( length2 );
- x *= ilength;
- y *= ilength;
- z *= ilength;
- }
- }
- return *this;
- }
- ID_INLINE void idVec3::Clamp( const idVec3 &min, const idVec3 &max ) {
- if ( x < min.x ) {
- x = min.x;
- } else if ( x > max.x ) {
- x = max.x;
- }
- if ( y < min.y ) {
- y = min.y;
- } else if ( y > max.y ) {
- y = max.y;
- }
- if ( z < min.z ) {
- z = min.z;
- } else if ( z > max.z ) {
- z = max.z;
- }
- }
- ID_INLINE void idVec3::Snap( void ) {
- x = floor( x + 0.5f );
- y = floor( y + 0.5f );
- z = floor( z + 0.5f );
- }
- ID_INLINE void idVec3::SnapInt( void ) {
- x = float( int( x ) );
- y = float( int( y ) );
- z = float( int( z ) );
- }
- ID_INLINE int idVec3::GetDimension( void ) const {
- return 3;
- }
- ID_INLINE const idVec2 &idVec3::ToVec2( void ) const {
- return *reinterpret_cast<const idVec2 *>(this);
- }
- ID_INLINE idVec2 &idVec3::ToVec2( void ) {
- return *reinterpret_cast<idVec2 *>(this);
- }
- ID_INLINE const float *idVec3::ToFloatPtr( void ) const {
- return &x;
- }
- ID_INLINE float *idVec3::ToFloatPtr( void ) {
- return &x;
- }
- ID_INLINE void idVec3::NormalVectors( idVec3 &left, idVec3 &down ) const {
- float d;
- d = x * x + y * y;
- if ( !d ) {
- left[0] = 1;
- left[1] = 0;
- left[2] = 0;
- } else {
- d = idMath::InvSqrt( d );
- left[0] = -y * d;
- left[1] = x * d;
- left[2] = 0;
- }
- down = left.Cross( *this );
- }
- ID_INLINE void idVec3::OrthogonalBasis( idVec3 &left, idVec3 &up ) const {
- float l, s;
- if ( idMath::Fabs( z ) > 0.7f ) {
- l = y * y + z * z;
- s = idMath::InvSqrt( l );
- up[0] = 0;
- up[1] = z * s;
- up[2] = -y * s;
- left[0] = l * s;
- left[1] = -x * up[2];
- left[2] = x * up[1];
- }
- else {
- l = x * x + y * y;
- s = idMath::InvSqrt( l );
- left[0] = -y * s;
- left[1] = x * s;
- left[2] = 0;
- up[0] = -z * left[1];
- up[1] = z * left[0];
- up[2] = l * s;
- }
- }
- ID_INLINE void idVec3::ProjectOntoPlane( const idVec3 &normal, const float overBounce ) {
- float backoff;
-
- backoff = *this * normal;
-
- if ( overBounce != 1.0 ) {
- if ( backoff < 0 ) {
- backoff *= overBounce;
- } else {
- backoff /= overBounce;
- }
- }
- *this -= backoff * normal;
- }
- ID_INLINE bool idVec3::ProjectAlongPlane( const idVec3 &normal, const float epsilon, const float overBounce ) {
- idVec3 cross;
- float len;
- cross = this->Cross( normal ).Cross( (*this) );
- // normalize so a fixed epsilon can be used
- cross.Normalize();
- len = normal * cross;
- if ( idMath::Fabs( len ) < epsilon ) {
- return false;
- }
- cross *= overBounce * ( normal * (*this) ) / len;
- (*this) -= cross;
- return true;
- }
- //===============================================================
- //
- // idVec4 - 4D vector
- //
- //===============================================================
- class idVec4 {
- public:
- float x;
- float y;
- float z;
- float w;
- idVec4( void );
- explicit idVec4( const float x, const float y, const float z, const float w );
- void Set( const float x, const float y, const float z, const float w );
- void Zero( void );
- float operator[]( const int index ) const;
- float & operator[]( const int index );
- idVec4 operator-() const;
- float operator*( const idVec4 &a ) const;
- idVec4 operator*( const float a ) const;
- idVec4 operator/( const float a ) const;
- idVec4 operator+( const idVec4 &a ) const;
- idVec4 operator-( const idVec4 &a ) const;
- idVec4 & operator+=( const idVec4 &a );
- idVec4 & operator-=( const idVec4 &a );
- idVec4 & operator/=( const idVec4 &a );
- idVec4 & operator/=( const float a );
- idVec4 & operator*=( const float a );
- friend idVec4 operator*( const float a, const idVec4 b );
- bool Compare( const idVec4 &a ) const; // exact compare, no epsilon
- bool Compare( const idVec4 &a, const float epsilon ) const; // compare with epsilon
- bool operator==( const idVec4 &a ) const; // exact compare, no epsilon
- bool operator!=( const idVec4 &a ) const; // exact compare, no epsilon
- float Length( void ) const;
- float LengthSqr( void ) const;
- float Normalize( void ); // returns length
- float NormalizeFast( void ); // returns length
- int GetDimension( void ) const;
- const idVec2 & ToVec2( void ) const;
- idVec2 & ToVec2( void );
- const idVec3 & ToVec3( void ) const;
- idVec3 & ToVec3( void );
- const float * ToFloatPtr( void ) const;
- float * ToFloatPtr( void );
- const char * ToString( int precision = 2 ) const;
- void Lerp( const idVec4 &v1, const idVec4 &v2, const float l );
- };
- extern idVec4 vec4_origin;
- #define vec4_zero vec4_origin
- ID_INLINE idVec4::idVec4( void ) {
- }
- ID_INLINE idVec4::idVec4( const float x, const float y, const float z, const float w ) {
- this->x = x;
- this->y = y;
- this->z = z;
- this->w = w;
- }
- ID_INLINE void idVec4::Set( const float x, const float y, const float z, const float w ) {
- this->x = x;
- this->y = y;
- this->z = z;
- this->w = w;
- }
- ID_INLINE void idVec4::Zero( void ) {
- x = y = z = w = 0.0f;
- }
- ID_INLINE float idVec4::operator[]( int index ) const {
- return ( &x )[ index ];
- }
- ID_INLINE float& idVec4::operator[]( int index ) {
- return ( &x )[ index ];
- }
- ID_INLINE idVec4 idVec4::operator-() const {
- return idVec4( -x, -y, -z, -w );
- }
- ID_INLINE idVec4 idVec4::operator-( const idVec4 &a ) const {
- return idVec4( x - a.x, y - a.y, z - a.z, w - a.w );
- }
- ID_INLINE float idVec4::operator*( const idVec4 &a ) const {
- return x * a.x + y * a.y + z * a.z + w * a.w;
- }
- ID_INLINE idVec4 idVec4::operator*( const float a ) const {
- return idVec4( x * a, y * a, z * a, w * a );
- }
- ID_INLINE idVec4 idVec4::operator/( const float a ) const {
- float inva = 1.0f / a;
- return idVec4( x * inva, y * inva, z * inva, w * inva );
- }
- ID_INLINE idVec4 operator*( const float a, const idVec4 b ) {
- return idVec4( b.x * a, b.y * a, b.z * a, b.w * a );
- }
- ID_INLINE idVec4 idVec4::operator+( const idVec4 &a ) const {
- return idVec4( x + a.x, y + a.y, z + a.z, w + a.w );
- }
- ID_INLINE idVec4 &idVec4::operator+=( const idVec4 &a ) {
- x += a.x;
- y += a.y;
- z += a.z;
- w += a.w;
- return *this;
- }
- ID_INLINE idVec4 &idVec4::operator/=( const idVec4 &a ) {
- x /= a.x;
- y /= a.y;
- z /= a.z;
- w /= a.w;
- return *this;
- }
- ID_INLINE idVec4 &idVec4::operator/=( const float a ) {
- float inva = 1.0f / a;
- x *= inva;
- y *= inva;
- z *= inva;
- w *= inva;
- return *this;
- }
- ID_INLINE idVec4 &idVec4::operator-=( const idVec4 &a ) {
- x -= a.x;
- y -= a.y;
- z -= a.z;
- w -= a.w;
- return *this;
- }
- ID_INLINE idVec4 &idVec4::operator*=( const float a ) {
- x *= a;
- y *= a;
- z *= a;
- w *= a;
- return *this;
- }
- ID_INLINE bool idVec4::Compare( const idVec4 &a ) const {
- return ( ( x == a.x ) && ( y == a.y ) && ( z == a.z ) && w == a.w );
- }
- ID_INLINE bool idVec4::Compare( const idVec4 &a, const float epsilon ) const {
- if ( idMath::Fabs( x - a.x ) > epsilon ) {
- return false;
- }
-
- if ( idMath::Fabs( y - a.y ) > epsilon ) {
- return false;
- }
- if ( idMath::Fabs( z - a.z ) > epsilon ) {
- return false;
- }
- if ( idMath::Fabs( w - a.w ) > epsilon ) {
- return false;
- }
- return true;
- }
- ID_INLINE bool idVec4::operator==( const idVec4 &a ) const {
- return Compare( a );
- }
- ID_INLINE bool idVec4::operator!=( const idVec4 &a ) const {
- return !Compare( a );
- }
- ID_INLINE float idVec4::Length( void ) const {
- return ( float )idMath::Sqrt( x * x + y * y + z * z + w * w );
- }
- ID_INLINE float idVec4::LengthSqr( void ) const {
- return ( x * x + y * y + z * z + w * w );
- }
- ID_INLINE float idVec4::Normalize( void ) {
- float sqrLength, invLength;
- sqrLength = x * x + y * y + z * z + w * w;
- invLength = idMath::InvSqrt( sqrLength );
- x *= invLength;
- y *= invLength;
- z *= invLength;
- w *= invLength;
- return invLength * sqrLength;
- }
- ID_INLINE float idVec4::NormalizeFast( void ) {
- float sqrLength, invLength;
- sqrLength = x * x + y * y + z * z + w * w;
- invLength = idMath::RSqrt( sqrLength );
- x *= invLength;
- y *= invLength;
- z *= invLength;
- w *= invLength;
- return invLength * sqrLength;
- }
- ID_INLINE int idVec4::GetDimension( void ) const {
- return 4;
- }
- ID_INLINE const idVec2 &idVec4::ToVec2( void ) const {
- return *reinterpret_cast<const idVec2 *>(this);
- }
- ID_INLINE idVec2 &idVec4::ToVec2( void ) {
- return *reinterpret_cast<idVec2 *>(this);
- }
- ID_INLINE const idVec3 &idVec4::ToVec3( void ) const {
- return *reinterpret_cast<const idVec3 *>(this);
- }
- ID_INLINE idVec3 &idVec4::ToVec3( void ) {
- return *reinterpret_cast<idVec3 *>(this);
- }
- ID_INLINE const float *idVec4::ToFloatPtr( void ) const {
- return &x;
- }
- ID_INLINE float *idVec4::ToFloatPtr( void ) {
- return &x;
- }
- //===============================================================
- //
- // idVec5 - 5D vector
- //
- //===============================================================
- class idVec5 {
- public:
- float x;
- float y;
- float z;
- float s;
- float t;
- idVec5( void );
- explicit idVec5( const idVec3 &xyz, const idVec2 &st );
- explicit idVec5( const float x, const float y, const float z, const float s, const float t );
- float operator[]( int index ) const;
- float & operator[]( int index );
- idVec5 & operator=( const idVec3 &a );
- int GetDimension( void ) const;
- const idVec3 & ToVec3( void ) const;
- idVec3 & ToVec3( void );
- const float * ToFloatPtr( void ) const;
- float * ToFloatPtr( void );
- const char * ToString( int precision = 2 ) const;
- void Lerp( const idVec5 &v1, const idVec5 &v2, const float l );
- };
- extern idVec5 vec5_origin;
- #define vec5_zero vec5_origin
- ID_INLINE idVec5::idVec5( void ) {
- }
- ID_INLINE idVec5::idVec5( const idVec3 &xyz, const idVec2 &st ) {
- x = xyz.x;
- y = xyz.y;
- z = xyz.z;
- s = st[0];
- t = st[1];
- }
- ID_INLINE idVec5::idVec5( const float x, const float y, const float z, const float s, const float t ) {
- this->x = x;
- this->y = y;
- this->z = z;
- this->s = s;
- this->t = t;
- }
- ID_INLINE float idVec5::operator[]( int index ) const {
- return ( &x )[ index ];
- }
- ID_INLINE float& idVec5::operator[]( int index ) {
- return ( &x )[ index ];
- }
- ID_INLINE idVec5 &idVec5::operator=( const idVec3 &a ) {
- x = a.x;
- y = a.y;
- z = a.z;
- s = t = 0;
- return *this;
- }
- ID_INLINE int idVec5::GetDimension( void ) const {
- return 5;
- }
- ID_INLINE const idVec3 &idVec5::ToVec3( void ) const {
- return *reinterpret_cast<const idVec3 *>(this);
- }
- ID_INLINE idVec3 &idVec5::ToVec3( void ) {
- return *reinterpret_cast<idVec3 *>(this);
- }
- ID_INLINE const float *idVec5::ToFloatPtr( void ) const {
- return &x;
- }
- ID_INLINE float *idVec5::ToFloatPtr( void ) {
- return &x;
- }
- //===============================================================
- //
- // idVec6 - 6D vector
- //
- //===============================================================
- class idVec6 {
- public:
- idVec6( void );
- explicit idVec6( const float *a );
- explicit idVec6( const float a1, const float a2, const float a3, const float a4, const float a5, const float a6 );
- void Set( const float a1, const float a2, const float a3, const float a4, const float a5, const float a6 );
- void Zero( void );
- float operator[]( const int index ) const;
- float & operator[]( const int index );
- idVec6 operator-() const;
- idVec6 operator*( const float a ) const;
- idVec6 operator/( const float a ) const;
- float operator*( const idVec6 &a ) const;
- idVec6 operator-( const idVec6 &a ) const;
- idVec6 operator+( const idVec6 &a ) const;
- idVec6 & operator*=( const float a );
- idVec6 & operator/=( const float a );
- idVec6 & operator+=( const idVec6 &a );
- idVec6 & operator-=( const idVec6 &a );
- friend idVec6 operator*( const float a, const idVec6 b );
- bool Compare( const idVec6 &a ) const; // exact compare, no epsilon
- bool Compare( const idVec6 &a, const float epsilon ) const; // compare with epsilon
- bool operator==( const idVec6 &a ) const; // exact compare, no epsilon
- bool operator!=( const idVec6 &a ) const; // exact compare, no epsilon
- float Length( void ) const;
- float LengthSqr( void ) const;
- float Normalize( void ); // returns length
- float NormalizeFast( void ); // returns length
- int GetDimension( void ) const;
- const idVec3 & SubVec3( int index ) const;
- idVec3 & SubVec3( int index );
- const float * ToFloatPtr( void ) const;
- float * ToFloatPtr( void );
- const char * ToString( int precision = 2 ) const;
- private:
- float p[6];
- };
- extern idVec6 vec6_origin;
- #define vec6_zero vec6_origin
- extern idVec6 vec6_infinity;
- ID_INLINE idVec6::idVec6( void ) {
- }
- ID_INLINE idVec6::idVec6( const float *a ) {
- memcpy( p, a, 6 * sizeof( float ) );
- }
- ID_INLINE idVec6::idVec6( const float a1, const float a2, const float a3, const float a4, const float a5, const float a6 ) {
- p[0] = a1;
- p[1] = a2;
- p[2] = a3;
- p[3] = a4;
- p[4] = a5;
- p[5] = a6;
- }
- ID_INLINE idVec6 idVec6::operator-() const {
- return idVec6( -p[0], -p[1], -p[2], -p[3], -p[4], -p[5] );
- }
- ID_INLINE float idVec6::operator[]( const int index ) const {
- return p[index];
- }
- ID_INLINE float &idVec6::operator[]( const int index ) {
- return p[index];
- }
- ID_INLINE idVec6 idVec6::operator*( const float a ) const {
- return idVec6( p[0]*a, p[1]*a, p[2]*a, p[3]*a, p[4]*a, p[5]*a );
- }
- ID_INLINE float idVec6::operator*( const idVec6 &a ) const {
- return p[0] * a[0] + p[1] * a[1] + p[2] * a[2] + p[3] * a[3] + p[4] * a[4] + p[5] * a[5];
- }
- ID_INLINE idVec6 idVec6::operator/( const float a ) const {
- float inva;
- assert( a != 0.0f );
- inva = 1.0f / a;
- return idVec6( p[0]*inva, p[1]*inva, p[2]*inva, p[3]*inva, p[4]*inva, p[5]*inva );
- }
- ID_INLINE idVec6 idVec6::operator+( const idVec6 &a ) const {
- return idVec6( p[0] + a[0], p[1] + a[1], p[2] + a[2], p[3] + a[3], p[4] + a[4], p[5] + a[5] );
- }
- ID_INLINE idVec6 idVec6::operator-( const idVec6 &a ) const {
- return idVec6( p[0] - a[0], p[1] - a[1], p[2] - a[2], p[3] - a[3], p[4] - a[4], p[5] - a[5] );
- }
- ID_INLINE idVec6 &idVec6::operator*=( const float a ) {
- p[0] *= a;
- p[1] *= a;
- p[2] *= a;
- p[3] *= a;
- p[4] *= a;
- p[5] *= a;
- return *this;
- }
- ID_INLINE idVec6 &idVec6::operator/=( const float a ) {
- float inva;
- assert( a != 0.0f );
- inva = 1.0f / a;
- p[0] *= inva;
- p[1] *= inva;
- p[2] *= inva;
- p[3] *= inva;
- p[4] *= inva;
- p[5] *= inva;
- return *this;
- }
- ID_INLINE idVec6 &idVec6::operator+=( const idVec6 &a ) {
- p[0] += a[0];
- p[1] += a[1];
- p[2] += a[2];
- p[3] += a[3];
- p[4] += a[4];
- p[5] += a[5];
- return *this;
- }
- ID_INLINE idVec6 &idVec6::operator-=( const idVec6 &a ) {
- p[0] -= a[0];
- p[1] -= a[1];
- p[2] -= a[2];
- p[3] -= a[3];
- p[4] -= a[4];
- p[5] -= a[5];
- return *this;
- }
- ID_INLINE idVec6 operator*( const float a, const idVec6 b ) {
- return b * a;
- }
- ID_INLINE bool idVec6::Compare( const idVec6 &a ) const {
- return ( ( p[0] == a[0] ) && ( p[1] == a[1] ) && ( p[2] == a[2] ) &&
- ( p[3] == a[3] ) && ( p[4] == a[4] ) && ( p[5] == a[5] ) );
- }
- ID_INLINE bool idVec6::Compare( const idVec6 &a, const float epsilon ) const {
- if ( idMath::Fabs( p[0] - a[0] ) > epsilon ) {
- return false;
- }
-
- if ( idMath::Fabs( p[1] - a[1] ) > epsilon ) {
- return false;
- }
- if ( idMath::Fabs( p[2] - a[2] ) > epsilon ) {
- return false;
- }
- if ( idMath::Fabs( p[3] - a[3] ) > epsilon ) {
- return false;
- }
- if ( idMath::Fabs( p[4] - a[4] ) > epsilon ) {
- return false;
- }
- if ( idMath::Fabs( p[5] - a[5] ) > epsilon ) {
- return false;
- }
- return true;
- }
- ID_INLINE bool idVec6::operator==( const idVec6 &a ) const {
- return Compare( a );
- }
- ID_INLINE bool idVec6::operator!=( const idVec6 &a ) const {
- return !Compare( a );
- }
- ID_INLINE void idVec6::Set( const float a1, const float a2, const float a3, const float a4, const float a5, const float a6 ) {
- p[0] = a1;
- p[1] = a2;
- p[2] = a3;
- p[3] = a4;
- p[4] = a5;
- p[5] = a6;
- }
- ID_INLINE void idVec6::Zero( void ) {
- p[0] = p[1] = p[2] = p[3] = p[4] = p[5] = 0.0f;
- }
- ID_INLINE float idVec6::Length( void ) const {
- return ( float )idMath::Sqrt( p[0] * p[0] + p[1] * p[1] + p[2] * p[2] + p[3] * p[3] + p[4] * p[4] + p[5] * p[5] );
- }
- ID_INLINE float idVec6::LengthSqr( void ) const {
- return ( p[0] * p[0] + p[1] * p[1] + p[2] * p[2] + p[3] * p[3] + p[4] * p[4] + p[5] * p[5] );
- }
- ID_INLINE float idVec6::Normalize( void ) {
- float sqrLength, invLength;
- sqrLength = p[0] * p[0] + p[1] * p[1] + p[2] * p[2] + p[3] * p[3] + p[4] * p[4] + p[5] * p[5];
- invLength = idMath::InvSqrt( sqrLength );
- p[0] *= invLength;
- p[1] *= invLength;
- p[2] *= invLength;
- p[3] *= invLength;
- p[4] *= invLength;
- p[5] *= invLength;
- return invLength * sqrLength;
- }
- ID_INLINE float idVec6::NormalizeFast( void ) {
- float sqrLength, invLength;
- sqrLength = p[0] * p[0] + p[1] * p[1] + p[2] * p[2] + p[3] * p[3] + p[4] * p[4] + p[5] * p[5];
- invLength = idMath::RSqrt( sqrLength );
- p[0] *= invLength;
- p[1] *= invLength;
- p[2] *= invLength;
- p[3] *= invLength;
- p[4] *= invLength;
- p[5] *= invLength;
- return invLength * sqrLength;
- }
- ID_INLINE int idVec6::GetDimension( void ) const {
- return 6;
- }
- ID_INLINE const idVec3 &idVec6::SubVec3( int index ) const {
- return *reinterpret_cast<const idVec3 *>(p + index * 3);
- }
- ID_INLINE idVec3 &idVec6::SubVec3( int index ) {
- return *reinterpret_cast<idVec3 *>(p + index * 3);
- }
- ID_INLINE const float *idVec6::ToFloatPtr( void ) const {
- return p;
- }
- ID_INLINE float *idVec6::ToFloatPtr( void ) {
- return p;
- }
- //===============================================================
- //
- // idVecX - arbitrary sized vector
- //
- // The vector lives on 16 byte aligned and 16 byte padded memory.
- //
- // NOTE: due to the temporary memory pool idVecX cannot be used by multiple threads
- //
- //===============================================================
- #define VECX_MAX_TEMP 1024
- #define VECX_QUAD( x ) ( ( ( ( x ) + 3 ) & ~3 ) * sizeof( float ) )
- #define VECX_CLEAREND() int s = size; while( s < ( ( s + 3) & ~3 ) ) { p[s++] = 0.0f; }
- #define VECX_ALLOCA( n ) ( (float *) _alloca16( VECX_QUAD( n ) ) )
- #define VECX_SIMD
- class idVecX {
- friend class idMatX;
- public:
- idVecX( void );
- explicit idVecX( int length );
- explicit idVecX( int length, float *data );
- ~idVecX( void );
- float operator[]( const int index ) const;
- float & operator[]( const int index );
- idVecX operator-() const;
- idVecX & operator=( const idVecX &a );
- idVecX operator*( const float a ) const;
- idVecX operator/( const float a ) const;
- float operator*( const idVecX &a ) const;
- idVecX operator-( const idVecX &a ) const;
- idVecX operator+( const idVecX &a ) const;
- idVecX & operator*=( const float a );
- idVecX & operator/=( const float a );
- idVecX & operator+=( const idVecX &a );
- idVecX & operator-=( const idVecX &a );
- friend idVecX operator*( const float a, const idVecX b );
- bool Compare( const idVecX &a ) const; // exact compare, no epsilon
- bool Compare( const idVecX &a, const float epsilon ) const; // compare with epsilon
- bool operator==( const idVecX &a ) const; // exact compare, no epsilon
- bool operator!=( const idVecX &a ) const; // exact compare, no epsilon
- void SetSize( int size );
- void ChangeSize( int size, bool makeZero = false );
- int GetSize( void ) const { return size; }
- void SetData( int length, float *data );
- void Zero( void );
- void Zero( int length );
- void Random( int seed, float l = 0.0f, float u = 1.0f );
- void Random( int length, int seed, float l = 0.0f, float u = 1.0f );
- void Negate( void );
- void Clamp( float min, float max );
- idVecX & SwapElements( int e1, int e2 );
- float Length( void ) const;
- float LengthSqr( void ) const;
- idVecX Normalize( void ) const;
- float NormalizeSelf( void );
- int GetDimension( void ) const;
- const idVec3 & SubVec3( int index ) const;
- idVec3 & SubVec3( int index );
- const idVec6 & SubVec6( int index ) const;
- idVec6 & SubVec6( int index );
- const float * ToFloatPtr( void ) const;
- float * ToFloatPtr( void );
- const char * ToString( int precision = 2 ) const;
- private:
- int size; // size of the vector
- int alloced; // if -1 p points to data set with SetData
- float * p; // memory the vector is stored
- static float temp[VECX_MAX_TEMP+4]; // used to store intermediate results
- static float * tempPtr; // pointer to 16 byte aligned temporary memory
- static int tempIndex; // index into memory pool, wraps around
- private:
- void SetTempSize( int size );
- };
- ID_INLINE idVecX::idVecX( void ) {
- size = alloced = 0;
- p = NULL;
- }
- ID_INLINE idVecX::idVecX( int length ) {
- size = alloced = 0;
- p = NULL;
- SetSize( length );
- }
- ID_INLINE idVecX::idVecX( int length, float *data ) {
- size = alloced = 0;
- p = NULL;
- SetData( length, data );
- }
- ID_INLINE idVecX::~idVecX( void ) {
- // if not temp memory
- if ( p && ( p < idVecX::tempPtr || p >= idVecX::tempPtr + VECX_MAX_TEMP ) && alloced != -1 ) {
- Mem_Free16( p );
- }
- }
- ID_INLINE float idVecX::operator[]( const int index ) const {
- assert( index >= 0 && index < size );
- return p[index];
- }
- ID_INLINE float &idVecX::operator[]( const int index ) {
- assert( index >= 0 && index < size );
- return p[index];
- }
- ID_INLINE idVecX idVecX::operator-() const {
- int i;
- idVecX m;
- m.SetTempSize( size );
- for ( i = 0; i < size; i++ ) {
- m.p[i] = -p[i];
- }
- return m;
- }
- ID_INLINE idVecX &idVecX::operator=( const idVecX &a ) {
- SetSize( a.size );
- #ifdef VECX_SIMD
- SIMDProcessor->Copy16( p, a.p, a.size );
- #else
- memcpy( p, a.p, a.size * sizeof( float ) );
- #endif
- idVecX::tempIndex = 0;
- return *this;
- }
- ID_INLINE idVecX idVecX::operator+( const idVecX &a ) const {
- idVecX m;
- assert( size == a.size );
- m.SetTempSize( size );
- #ifdef VECX_SIMD
- SIMDProcessor->Add16( m.p, p, a.p, size );
- #else
- int i;
- for ( i = 0; i < size; i++ ) {
- m.p[i] = p[i] + a.p[i];
- }
- #endif
- return m;
- }
- ID_INLINE idVecX idVecX::operator-( const idVecX &a ) const {
- idVecX m;
- assert( size == a.size );
- m.SetTempSize( size );
- #ifdef VECX_SIMD
- SIMDProcessor->Sub16( m.p, p, a.p, size );
- #else
- int i;
- for ( i = 0; i < size; i++ ) {
- m.p[i] = p[i] - a.p[i];
- }
- #endif
- return m;
- }
- ID_INLINE idVecX &idVecX::operator+=( const idVecX &a ) {
- assert( size == a.size );
- #ifdef VECX_SIMD
- SIMDProcessor->AddAssign16( p, a.p, size );
- #else
- int i;
- for ( i = 0; i < size; i++ ) {
- p[i] += a.p[i];
- }
- #endif
- idVecX::tempIndex = 0;
- return *this;
- }
- ID_INLINE idVecX &idVecX::operator-=( const idVecX &a ) {
- assert( size == a.size );
- #ifdef VECX_SIMD
- SIMDProcessor->SubAssign16( p, a.p, size );
- #else
- int i;
- for ( i = 0; i < size; i++ ) {
- p[i] -= a.p[i];
- }
- #endif
- idVecX::tempIndex = 0;
- return *this;
- }
- ID_INLINE idVecX idVecX::operator*( const float a ) const {
- idVecX m;
- m.SetTempSize( size );
- #ifdef VECX_SIMD
- SIMDProcessor->Mul16( m.p, p, a, size );
- #else
- int i;
- for ( i = 0; i < size; i++ ) {
- m.p[i] = p[i] * a;
- }
- #endif
- return m;
- }
- ID_INLINE idVecX &idVecX::operator*=( const float a ) {
- #ifdef VECX_SIMD
- SIMDProcessor->MulAssign16( p, a, size );
- #else
- int i;
- for ( i = 0; i < size; i++ ) {
- p[i] *= a;
- }
- #endif
- return *this;
- }
- ID_INLINE idVecX idVecX::operator/( const float a ) const {
- assert( a != 0.0f );
- return (*this) * ( 1.0f / a );
- }
- ID_INLINE idVecX &idVecX::operator/=( const float a ) {
- assert( a != 0.0f );
- (*this) *= ( 1.0f / a );
- return *this;
- }
- ID_INLINE idVecX operator*( const float a, const idVecX b ) {
- return b * a;
- }
- ID_INLINE float idVecX::operator*( const idVecX &a ) const {
- int i;
- float sum = 0.0f;
- assert( size == a.size );
- for ( i = 0; i < size; i++ ) {
- sum += p[i] * a.p[i];
- }
- return sum;
- }
- ID_INLINE bool idVecX::Compare( const idVecX &a ) const {
- int i;
- assert( size == a.size );
- for ( i = 0; i < size; i++ ) {
- if ( p[i] != a.p[i] ) {
- return false;
- }
- }
- return true;
- }
- ID_INLINE bool idVecX::Compare( const idVecX &a, const float epsilon ) const {
- int i;
- assert( size == a.size );
- for ( i = 0; i < size; i++ ) {
- if ( idMath::Fabs( p[i] - a.p[i] ) > epsilon ) {
- return false;
- }
- }
- return true;
- }
- ID_INLINE bool idVecX::operator==( const idVecX &a ) const {
- return Compare( a );
- }
- ID_INLINE bool idVecX::operator!=( const idVecX &a ) const {
- return !Compare( a );
- }
- ID_INLINE void idVecX::SetSize( int newSize ) {
- int alloc = ( newSize + 3 ) & ~3;
- if ( alloc > alloced && alloced != -1 ) {
- if ( p ) {
- Mem_Free16( p );
- }
- p = (float *) Mem_Alloc16( alloc * sizeof( float ) );
- alloced = alloc;
- }
- size = newSize;
- VECX_CLEAREND();
- }
- ID_INLINE void idVecX::ChangeSize( int newSize, bool makeZero ) {
- int alloc = ( newSize + 3 ) & ~3;
- if ( alloc > alloced && alloced != -1 ) {
- float *oldVec = p;
- p = (float *) Mem_Alloc16( alloc * sizeof( float ) );
- alloced = alloc;
- if ( oldVec ) {
- for ( int i = 0; i < size; i++ ) {
- p[i] = oldVec[i];
- }
- Mem_Free16( oldVec );
- }
- if ( makeZero ) {
- // zero any new elements
- for ( int i = size; i < newSize; i++ ) {
- p[i] = 0.0f;
- }
- }
- }
- size = newSize;
- VECX_CLEAREND();
- }
- ID_INLINE void idVecX::SetTempSize( int newSize ) {
- size = newSize;
- alloced = ( newSize + 3 ) & ~3;
- assert( alloced < VECX_MAX_TEMP );
- if ( idVecX::tempIndex + alloced > VECX_MAX_TEMP ) {
- idVecX::tempIndex = 0;
- }
- p = idVecX::tempPtr + idVecX::tempIndex;
- idVecX::tempIndex += alloced;
- VECX_CLEAREND();
- }
- ID_INLINE void idVecX::SetData( int length, float *data ) {
- if ( p && ( p < idVecX::tempPtr || p >= idVecX::tempPtr + VECX_MAX_TEMP ) && alloced != -1 ) {
- Mem_Free16( p );
- }
- assert( ( ( (int) data ) & 15 ) == 0 ); // data must be 16 byte aligned
- p = data;
- size = length;
- alloced = -1;
- VECX_CLEAREND();
- }
- ID_INLINE void idVecX::Zero( void ) {
- #ifdef VECX_SIMD
- SIMDProcessor->Zero16( p, size );
- #else
- memset( p, 0, size * sizeof( float ) );
- #endif
- }
- ID_INLINE void idVecX::Zero( int length ) {
- SetSize( length );
- #ifdef VECX_SIMD
- SIMDProcessor->Zero16( p, length );
- #else
- memset( p, 0, size * sizeof( float ) );
- #endif
- }
- ID_INLINE void idVecX::Random( int seed, float l, float u ) {
- int i;
- float c;
- idRandom rnd( seed );
- c = u - l;
- for ( i = 0; i < size; i++ ) {
- p[i] = l + rnd.RandomFloat() * c;
- }
- }
- ID_INLINE void idVecX::Random( int length, int seed, float l, float u ) {
- int i;
- float c;
- idRandom rnd( seed );
- SetSize( length );
- c = u - l;
- for ( i = 0; i < size; i++ ) {
- p[i] = l + rnd.RandomFloat() * c;
- }
- }
- ID_INLINE void idVecX::Negate( void ) {
- #ifdef VECX_SIMD
- SIMDProcessor->Negate16( p, size );
- #else
- int i;
- for ( i = 0; i < size; i++ ) {
- p[i] = -p[i];
- }
- #endif
- }
- ID_INLINE void idVecX::Clamp( float min, float max ) {
- int i;
- for ( i = 0; i < size; i++ ) {
- if ( p[i] < min ) {
- p[i] = min;
- } else if ( p[i] > max ) {
- p[i] = max;
- }
- }
- }
- ID_INLINE idVecX &idVecX::SwapElements( int e1, int e2 ) {
- float tmp;
- tmp = p[e1];
- p[e1] = p[e2];
- p[e2] = tmp;
- return *this;
- }
- ID_INLINE float idVecX::Length( void ) const {
- int i;
- float sum = 0.0f;
- for ( i = 0; i < size; i++ ) {
- sum += p[i] * p[i];
- }
- return idMath::Sqrt( sum );
- }
- ID_INLINE float idVecX::LengthSqr( void ) const {
- int i;
- float sum = 0.0f;
- for ( i = 0; i < size; i++ ) {
- sum += p[i] * p[i];
- }
- return sum;
- }
- ID_INLINE idVecX idVecX::Normalize( void ) const {
- int i;
- idVecX m;
- float invSqrt, sum = 0.0f;
- m.SetTempSize( size );
- for ( i = 0; i < size; i++ ) {
- sum += p[i] * p[i];
- }
- invSqrt = idMath::InvSqrt( sum );
- for ( i = 0; i < size; i++ ) {
- m.p[i] = p[i] * invSqrt;
- }
- return m;
- }
- ID_INLINE float idVecX::NormalizeSelf( void ) {
- float invSqrt, sum = 0.0f;
- int i;
- for ( i = 0; i < size; i++ ) {
- sum += p[i] * p[i];
- }
- invSqrt = idMath::InvSqrt( sum );
- for ( i = 0; i < size; i++ ) {
- p[i] *= invSqrt;
- }
- return invSqrt * sum;
- }
- ID_INLINE int idVecX::GetDimension( void ) const {
- return size;
- }
- ID_INLINE idVec3 &idVecX::SubVec3( int index ) {
- assert( index >= 0 && index * 3 + 3 <= size );
- return *reinterpret_cast<idVec3 *>(p + index * 3);
- }
- ID_INLINE const idVec3 &idVecX::SubVec3( int index ) const {
- assert( index >= 0 && index * 3 + 3 <= size );
- return *reinterpret_cast<const idVec3 *>(p + index * 3);
- }
- ID_INLINE idVec6 &idVecX::SubVec6( int index ) {
- assert( index >= 0 && index * 6 + 6 <= size );
- return *reinterpret_cast<idVec6 *>(p + index * 6);
- }
- ID_INLINE const idVec6 &idVecX::SubVec6( int index ) const {
- assert( index >= 0 && index * 6 + 6 <= size );
- return *reinterpret_cast<const idVec6 *>(p + index * 6);
- }
- ID_INLINE const float *idVecX::ToFloatPtr( void ) const {
- return p;
- }
- ID_INLINE float *idVecX::ToFloatPtr( void ) {
- return p;
- }
- //===============================================================
- //
- // idPolar3
- //
- //===============================================================
- class idPolar3 {
- public:
- float radius, theta, phi;
- idPolar3( void );
- explicit idPolar3( const float radius, const float theta, const float phi );
- void Set( const float radius, const float theta, const float phi );
- float operator[]( const int index ) const;
- float & operator[]( const int index );
- idPolar3 operator-() const;
- idPolar3 & operator=( const idPolar3 &a );
- idVec3 ToVec3( void ) const;
- };
- ID_INLINE idPolar3::idPolar3( void ) {
- }
- ID_INLINE idPolar3::idPolar3( const float radius, const float theta, const float phi ) {
- assert( radius > 0 );
- this->radius = radius;
- this->theta = theta;
- this->phi = phi;
- }
-
- ID_INLINE void idPolar3::Set( const float radius, const float theta, const float phi ) {
- assert( radius > 0 );
- this->radius = radius;
- this->theta = theta;
- this->phi = phi;
- }
- ID_INLINE float idPolar3::operator[]( const int index ) const {
- return ( &radius )[ index ];
- }
- ID_INLINE float &idPolar3::operator[]( const int index ) {
- return ( &radius )[ index ];
- }
- ID_INLINE idPolar3 idPolar3::operator-() const {
- return idPolar3( radius, -theta, -phi );
- }
- ID_INLINE idPolar3 &idPolar3::operator=( const idPolar3 &a ) {
- radius = a.radius;
- theta = a.theta;
- phi = a.phi;
- return *this;
- }
- ID_INLINE idVec3 idPolar3::ToVec3( void ) const {
- float sp, cp, st, ct;
- idMath::SinCos( phi, sp, cp );
- idMath::SinCos( theta, st, ct );
- return idVec3( cp * radius * ct, cp * radius * st, radius * sp );
- }
- /*
- ===============================================================================
- Old 3D vector macros, should no longer be used.
- ===============================================================================
- */
- #define DotProduct( a, b) ((a)[0]*(b)[0]+(a)[1]*(b)[1]+(a)[2]*(b)[2])
- #define VectorSubtract( a, b, c ) ((c)[0]=(a)[0]-(b)[0],(c)[1]=(a)[1]-(b)[1],(c)[2]=(a)[2]-(b)[2])
- #define VectorAdd( a, b, c ) ((c)[0]=(a)[0]+(b)[0],(c)[1]=(a)[1]+(b)[1],(c)[2]=(a)[2]+(b)[2])
- #define VectorScale( v, s, o ) ((o)[0]=(v)[0]*(s),(o)[1]=(v)[1]*(s),(o)[2]=(v)[2]*(s))
- #define VectorMA( v, s, b, o ) ((o)[0]=(v)[0]+(b)[0]*(s),(o)[1]=(v)[1]+(b)[1]*(s),(o)[2]=(v)[2]+(b)[2]*(s))
- #define VectorCopy( a, b ) ((b)[0]=(a)[0],(b)[1]=(a)[1],(b)[2]=(a)[2])
- #endif /* !__MATH_VECTOR_H__ */
|