![[]](/images/special/trans.gif)
LibN2L-4 Library Code ReferenceClassesCompounds Files Members Method Index Full Reference cVector3.hGo to the documentation of this file.00001 /************************************************************************ 00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library 00003 Copyright (C) 2002 Aaron Cameron 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 A copy of the GNU Lesser General Public License has been provided with 00020 this library in the file 'COPYING'. 00021 00022 Contact information for the author of this library has been provided 00023 with this library in the file 'AUTHOR'. 00024 ************************************************************************/ 00025 00026 #ifndef _n2l4_cVector3_h 00027 #define _n2l4_cVector3_h 00028 00029 #include <math.h> 00030 #include <GL/gl.h> 00031 #include "geometry/misc.h" 00032 00033 #include "geometry/cVector2.h" 00034 00035 /******************************************************************************/ 00036 namespace n2l 00037 { 00042 template <class TComponent, class TComponentDiff, class TMagnitude> 00043 class cVector3 00044 { 00045 public: 00046 typedef TComponent tComponent; 00047 typedef TComponentDiff tComponentDiff; 00048 typedef TMagnitude tMagnitude; 00049 00050 typedef cVector3<TComponent,TComponentDiff,TMagnitude> tThisVector; 00051 00052 typedef cVector3<TComponentDiff,TComponentDiff,TMagnitude> tVectorDiff; 00053 00054 typedef cVector2<tComponent,tComponentDiff,tMagnitude> tThisVector2D; 00055 00056 // Constructors 00057 /**********************************************************************/ 00058 cVector3() {} 00059 00060 /**********************************************************************/ 00061 cVector3( const tThisVector &i_v3 ) 00062 { 00063 mComp[0] = i_v3.mComp[0]; 00064 mComp[1] = i_v3.mComp[1]; 00065 mComp[2] = i_v3.mComp[2]; 00066 } 00067 00068 /**********************************************************************/ 00069 cVector3( const tThisVector2D &iV ) 00070 { 00071 mComp[0] = iV.mComp[0]; 00072 mComp[1] = iV.mComp[1]; 00073 mComp[2] = 0; 00074 } 00075 00076 /**********************************************************************/ 00077 cVector3( const tComponent &iX, const tComponent &iY = 0.0f, 00078 const tComponent &iZ = 0.0f ) 00079 { 00080 mComp[0] = iX; 00081 mComp[1] = iY; 00082 mComp[2] = iZ; 00083 } 00084 00085 00086 /**********************************************************************/ 00087 ~cVector3() {} 00088 00089 /**********************************************************************/ 00090 // Assignments 00091 tThisVector &setX( const tComponent &iX ) { 00092 mComp[0] = iX; 00093 return *this; 00094 } 00095 00096 tThisVector &setY( const tComponent &iY ) { 00097 mComp[1] = iY; 00098 return *this; 00099 } 00100 00101 tThisVector &setZ( const tComponent &iZ ) { 00102 mComp[2] = iZ; 00103 return *this; 00104 } 00105 00106 tComponent &x( const tComponent &iX ) { 00107 mComp[0] = iX; 00108 return mComp[0]; 00109 } 00110 00111 tComponent &y( const tComponent &iY ) { 00112 mComp[1] = iY; 00113 return mComp[1]; 00114 } 00115 00116 tComponent &z( const tComponent &iZ ) { 00117 mComp[2] = iZ; 00118 return mComp[2]; 00119 } 00120 00121 tThisVector &set( const tComponent *iComp ) { 00122 memmove( (void*)(mComp), (void*)(iComp), sizeof(tComponent)*3 ); 00123 return *this; 00124 } 00125 00126 /**********************************************************************/ 00127 tThisVector & operator =( const tThisVector & iV ) 00128 { 00129 mComp[0] = iV.mComp[0]; 00130 mComp[1] = iV.mComp[1]; 00131 mComp[2] = iV.mComp[2]; 00132 return *this; 00133 } 00134 00135 /**********************************************************************/ 00136 tThisVector &operator =( const tThisVector2D &iV ) 00137 { 00138 mComp[0] = iV.mComp[0]; 00139 mComp[1] = iV.mComp[1]; 00140 mComp[2] = 0; 00141 return *this; 00142 } 00143 00144 /**********************************************************************/ 00145 tThisVector &set( const tComponent iX, const tComponent iY, 00146 const tComponent iZ ) 00147 { 00148 mComp[0] = iX; mComp[1] = iY; mComp[2] = iZ; 00149 return *this; 00150 } 00151 00152 /**********************************************************************/ 00153 // Views 00154 const tComponent getX() const { return mComp[0]; } 00155 const tComponent getY() const { return mComp[1]; } 00156 const tComponent getZ() const { return mComp[2]; } 00157 // Newer interface 00158 inline const tComponent &x() const { return mComp[0]; } 00159 inline const tComponent &y() const { return mComp[1]; } 00160 inline const tComponent &z() const { return mComp[2]; } 00161 00162 inline tComponent &x() { return mComp[0]; } 00163 inline tComponent &y() { return mComp[1]; } 00164 inline tComponent &z() { return mComp[2]; } 00165 00166 inline tComponent & operator[]( const tUint iIndex ) { 00167 return mComp[iIndex]; 00168 } 00169 00170 inline const tComponent & operator[]( const tUint iIndex ) const { 00171 return mComp[iIndex]; 00172 } 00173 00174 /**********************************************************************/ 00175 const tBool containsSpecialNumber() const 00176 { 00177 if (mComp[0]-mComp[0]!=0 || mComp[1]-mComp[1]!=0 || 00178 mComp[2]-mComp[2]!=0) 00179 return true; 00180 else 00181 return false; 00182 } 00183 00184 /**********************************************************************/ 00185 const tBool containsNaN() const 00186 { 00187 if (mComp[0]!=mComp[0] || mComp[1]!=mComp[1] || 00188 mComp[2]!=mComp[2]) 00189 return true; 00190 else 00191 return false; 00192 } 00193 00194 /**********************************************************************/ 00195 void fixSpecialNumbers() 00196 { 00197 fixNaN(); 00198 if (mComp[0]-mComp[0]!=0) mComp[0] = 1.0e+35f; 00199 if (mComp[1]-mComp[1]!=0) mComp[1] = 1.0e+35f; 00200 if (mComp[2]-mComp[2]!=0) mComp[2] = 1.0e+35f; 00201 } 00202 00203 /**********************************************************************/ 00204 void fixNaN() 00205 { 00206 if (mComp[0]!=mComp[0]) mComp[0] = 0; 00207 if (mComp[1]!=mComp[1]) mComp[1] = 0; 00208 if (mComp[2]!=mComp[2]) mComp[2] = 0; 00209 00210 } 00211 00212 /**********************************************************************/ 00213 void get( tComponent & o_x, tComponent & o_y, tComponent & o_z ) const 00214 { 00215 o_x = mComp[0]; o_y = mComp[1]; o_z = mComp[2]; 00216 } 00217 00218 /**********************************************************************/ 00219 const tThisVector operator +( const tThisVector & i_v ) const 00220 { 00221 return tThisVector( mComp[0]+i_v.mComp[0], mComp[1]+i_v.mComp[1], 00222 mComp[2]+i_v.mComp[2]); 00223 } 00224 00225 /**********************************************************************/ 00226 void matchSigns( const tThisVector &iV ) 00227 { 00228 if (iV.x()<0.0f) { 00229 if (mComp[0]>0.0f) mComp[0] *= -1.0f; 00230 } else 00231 if (mComp[0]<0.0f) mComp[0] *= -1.0f; 00232 00233 if (iV.y()<0.0f) { 00234 if (mComp[1]>0.0f) mComp[1] *= -1.0f; 00235 } else 00236 if (mComp[1]<0.0f) mComp[1] *= -1.0f; 00237 00238 if (iV.z()<0.0f) { 00239 if (mComp[2]>0.0f) mComp[2] *= -1.0f; 00240 } else 00241 if (mComp[2]<0.0f) mComp[2] *= -1.0f; 00242 } 00243 00244 /**********************************************************************/ 00245 const tThisVector operator -( const tThisVector & i_v ) const 00246 { 00247 return tThisVector( mComp[0]-i_v.mComp[0], mComp[1]-i_v.mComp[1], 00248 mComp[2]-i_v.mComp[2]); 00249 } 00250 00251 00252 /**********************************************************************/ 00253 const tThisVector operator *( const tMagnitude &i_mag ) const 00254 { 00255 return tThisVector( mComp[0]*i_mag, mComp[1]*i_mag, 00256 mComp[2]*i_mag); 00257 } 00258 00259 /**********************************************************************/ 00260 const tThisVector operator *( const tThisVector & i_v ) const 00261 { 00262 return tThisVector( mComp[0]*i_v.mComp[0], mComp[1]*i_v.mComp[1], 00263 mComp[2]*i_v.mComp[2]); 00264 } 00265 00266 /**********************************************************************/ 00267 const tThisVector operator /( const tThisVector & i_v ) const 00268 { 00269 return tThisVector( mComp[0]/i_v.mComp[0], mComp[1]/i_v.mComp[1], 00270 mComp[2]/i_v.mComp[2]); 00271 } 00272 00273 /**********************************************************************/ 00274 const tThisVector operator /( const tMagnitude &iMag ) const 00275 { 00276 return tThisVector( mComp[0]/iMag, mComp[1]/iMag, mComp[2]/iMag ); 00277 } 00278 00279 /**********************************************************************/ 00280 void operator *=( const tMagnitude &i_mag ) 00281 { 00282 mComp[0]*=i_mag; 00283 mComp[1]*=i_mag; 00284 mComp[2]*=i_mag; 00285 } 00286 00287 /**********************************************************************/ 00288 void operator /=( const tMagnitude &i_mag ) 00289 { 00290 mComp[0]/=i_mag; 00291 mComp[1]/=i_mag; 00292 mComp[2]/=i_mag; 00293 } 00294 00295 /**********************************************************************/ 00296 tThisVector & operator +=( const tThisVector & i_v ) 00297 { 00298 mComp[0] += i_v.mComp[0]; 00299 mComp[1] += i_v.mComp[1]; 00300 mComp[2] += i_v.mComp[2]; 00301 return *this; 00302 } 00303 00304 /**********************************************************************/ 00305 tThisVector & operator +=( const tMagnitude &iMag ) 00306 { 00307 mComp[0] += iMag; 00308 mComp[1] += iMag; 00309 mComp[2] += iMag; 00310 return *this; 00311 } 00312 00313 /**********************************************************************/ 00314 tThisVector & operator -=( const tMagnitude &iMag ) 00315 { 00316 mComp[0] -= iMag; 00317 mComp[1] -= iMag; 00318 mComp[2] -= iMag; 00319 return *this; 00320 } 00321 00322 /**********************************************************************/ 00323 tThisVector & operator -=( const tThisVector & iV ) 00324 { 00325 mComp[0] -= iV.mComp[0]; 00326 mComp[1] -= iV.mComp[1]; 00327 mComp[2] -= iV.mComp[2]; 00328 return *this; 00329 } 00330 00331 00332 /**********************************************************************/ 00333 void operator *=( const tThisVector & i_v ) 00334 { 00335 mComp[0] *= i_v.mComp[0]; 00336 mComp[1] *= i_v.mComp[1]; 00337 mComp[2] *= i_v.mComp[2]; 00338 } 00339 00340 00341 /**********************************************************************/ 00342 const tBool operator ==( const tThisVector & iV ) const 00343 { 00344 return (mComp[0]==iV.mComp[0] && mComp[1]==iV.mComp[1] && 00345 mComp[2]==iV.mComp[2]); 00346 } 00347 00348 00349 /**********************************************************************/ 00350 const tBool operator !=( const tThisVector & iV ) const 00351 { 00352 return (mComp[0]!=iV.mComp[0] || mComp[1]!=iV.mComp[1] || 00353 mComp[2]!=iV.mComp[2]); 00354 } 00355 00356 /**********************************************************************/ 00357 void normalize() 00358 { 00359 const tMagnitude Mag = magnitude(); 00360 mComp[0] /= Mag; 00361 mComp[1] /= Mag; 00362 mComp[2] /= Mag; 00363 } 00364 00365 00366 /**********************************************************************/ 00367 const tThisVector normalized() const 00368 { 00369 const tMagnitude Mag = magnitude(); 00370 return tThisVector( mComp[0]/Mag, mComp[1]/Mag, mComp[2]/Mag ); 00371 } 00372 00373 /**********************************************************************/ 00374 const tMagnitude magnitude() const 00375 { 00376 return sqrt( mComp[0]*mComp[0] + mComp[1]*mComp[1] + 00377 mComp[2]*mComp[2] ); 00378 } 00379 00380 /**********************************************************************/ 00381 const tMagnitude maxComp() const { 00382 return n2lMax(n2lMax(mComp[0],mComp[1]),mComp[2]); 00383 } 00384 00385 /**********************************************************************/ 00386 const tMagnitude maxCompMag() const { 00387 return n2lMax(n2lMax(fabs(mComp[0]), 00388 fabs(mComp[1])),fabs(mComp[2])); 00389 } 00390 00391 /**********************************************************************/ 00392 // Show this vector how to become its difference 00393 operator tVectorDiff() const { 00394 return tVectorDiff(mComp[0],mComp[1],mComp[2]); 00395 } 00396 00397 00398 /**********************************************************************/ 00401 void rotateD( const tFloat & iAngle, const tThisVector &iV ) 00402 { 00403 rotateR( degToRad(iAngle), iV.x(),iV.y(),iV.z() ); 00404 } 00405 00406 /**********************************************************************/ 00410 void rotateD( const tFloat &iAngle, const tFloat &iX, 00411 const tFloat &iY, const tFloat &iZ ) 00412 { 00413 rotateR( degToRad(iAngle), iX,iY,iZ ); 00414 } 00415 00416 /**********************************************************************/ 00417 void rotateR( const tFloat &iAngle, tThisVector &iAV ) 00418 { 00419 rotateR( iAngle, iAV.x(), iAV.y(), iAV.z() ); 00420 } 00421 00422 /**********************************************************************/ 00426 void rotateR( const tFloat &iAngle, const tFloat &iX, 00427 const tFloat &iY, const tFloat &iZ ) 00428 { 00429 const tFloat cosTheta = tFloat(cos(iAngle)); 00430 const tFloat sinTheta = tFloat(sin(iAngle)); 00431 tFloat nX,nY,nZ; 00432 nX = (cosTheta+(1-cosTheta)*iX*iX)*mComp[0]; 00433 nX += ((1-cosTheta)*iX*iY-iZ*sinTheta)*mComp[1]; 00434 nX += ((1-cosTheta)*iX*iZ+iY*sinTheta)*mComp[2]; 00435 00436 nY = ((1-cosTheta)*iX*iY+iZ*sinTheta)*mComp[0]; 00437 nY += (cosTheta+(1-cosTheta)*iY*iY)*mComp[1]; 00438 nY += ((1-cosTheta)*iY*iZ-iX*sinTheta)*mComp[2]; 00439 00440 nZ = ((1-cosTheta)*iX*iZ-iY*sinTheta)*mComp[0]; 00441 nZ += ((1-cosTheta)*iY*iZ+iX*sinTheta)*mComp[1]; 00442 nZ += (cosTheta+(1-cosTheta)*iZ*iZ)*mComp[2]; 00443 00444 mComp[0] = nX; 00445 mComp[1] = nY; 00446 mComp[2] = nZ; 00447 } 00448 00449 /**********************************************************************/ 00450 const tThisVector cross( const tThisVector &iB ) const 00451 { 00452 return tThisVector( (mComp[1]*iB.mComp[2])-(mComp[2]*iB.mComp[1]), 00453 (mComp[2]*iB.mComp[0])-(mComp[0]*iB.mComp[2]), 00454 (mComp[0]*iB.mComp[1])-(mComp[1]*iB.mComp[0]) ); 00455 } 00456 00457 /**********************************************************************/ 00458 const tComponent dot( const tThisVector &iB ) const 00459 { 00460 return (mComp[0]*iB.mComp[0])+(mComp[1]*iB.mComp[1])+ 00461 (mComp[2]*iB.mComp[2]); 00462 } 00463 00464 /**********************************************************************/ 00467 const tThisVector perpendicular() const 00468 { 00469 const tComponent AbsX(mComp[0]<0?-mComp[0]:mComp[0]); 00470 const tComponent AbsY(mComp[1]<0?-mComp[1]:mComp[1]); 00471 const tComponent AbsZ(mComp[2]<0?-mComp[2]:mComp[2]); 00472 if (AbsX < AbsY && AbsX < AbsZ ) { 00473 return tThisVector( 0,-mComp[2],mComp[1] ); 00474 } else if (AbsY < AbsX && AbsY < AbsZ ) { 00475 return tThisVector( -mComp[2],0,mComp[0] ); 00476 } else { 00477 return tThisVector( -mComp[1],mComp[0],0 ); 00478 } 00479 } 00480 00481 /**********************************************************************/ 00485 const tFloat anglefD( const tThisVector &iB ) const 00486 { 00487 return radToDeg( anglefR(iB) ); 00488 } 00489 00490 00491 /**********************************************************************/ 00495 const tFloat anglefR( const tThisVector &iB ) const 00496 { 00497 return acos( dot(iB)/(magnitude()*iB.magnitude()) ); 00498 } 00499 00500 /**********************************************************************/ 00508 // const tFloat anglefR( const tThisVector & iB ) const 00509 // { 00510 // return acos( dot(iB)/(magnitude()*iB.magnitude()) ); 00511 // } 00512 00513 00514 /**********************************************************************/ 00517 /* 00518 THIS METHOD IS INCOMPLETE 00519 const tThisVector vProjectOn( const tThisVector & iB ) const 00520 { 00521 const tFloat BMag iB.magnitude(); 00522 return (dot(iB)/(BMag*BMag)); 00523 } 00524 */ 00525 00526 00527 /**********************************************************************/ 00528 const tString dump() const { 00529 tString str("tVector3("); 00530 str += asString(mComp[0]); 00531 str += ","; 00532 str += asString(mComp[1]); 00533 str += ","; 00534 str += asString(mComp[2]); 00535 str += ")"; 00536 return str; 00537 } 00538 00539 00540 /**********************************************************************/ 00541 void glVertex3f() const 00542 { 00543 ::glVertex3fv( mComp ); 00544 } 00545 00546 00547 /**********************************************************************/ 00548 void glNormal3f() const 00549 { 00550 ::glNormal3fv( mComp ); 00551 } 00552 00553 /**********************************************************************/ 00554 void glTexCoord2f() const 00555 { 00556 ::glTexCoord2fv( mComp ); 00557 } 00558 00559 /**********************************************************************/ 00560 void glTranslatef() const 00561 { 00562 ::glTranslatef( mComp[0], mComp[1], mComp[2] ); 00563 } 00564 00565 /**********************************************************************/ 00566 void glScalef() const 00567 { 00568 ::glScalef( mComp[0], mComp[1], mComp[2] ); 00569 } 00570 00571 /**********************************************************************/ 00572 // Use this method with risk. 00573 tComponent *const data() { return mComp; } 00574 const tComponent *const data() const { return mComp; } 00575 00576 private: 00577 tComponent mComp[3]; 00578 00579 }; // class 00580 00581 } // namespace n2l 00582 00583 00584 00585 #endif |