Main Page   Compound List   File List   Compound Members  

MyVector3x.h

00001 
00002 //define classname and constructor, for example class VECTOR3f. define SCALAR, VECTOR
00003 //Possible options
00004 //
00005 // #define VECTOR Vektor3f
00006 // #define SCALAR float
00007 // #define USEMATRIX
00008 // #define VECTORMATRIX Matrix4f
00009 // #define SCALARUNSIGNED
00010 
00011 public:
00012 
00013   union
00014   {
00015     SCALAR array[3];
00016     struct { SCALAR x, y, z; };  //Anonymous structure
00017     struct { SCALAR r, g, b; };  //Anonymous structure
00018   };
00019 
00020   inline void set(SCALAR px, SCALAR py, SCALAR pz)
00021   {
00022     x = px;
00023     y = py;
00024     z = pz;
00025   }
00026 
00027   inline void setNull()
00028   {
00029     x = y = z = 0;
00030   }
00031 
00032 
00033   //Operators for this VECTOR class
00034   SCALAR operator[](const long i)
00035   { //SCALAR array
00036     assert(i >= 0 && i < 3);
00037     return(array[i]);
00038   }
00039 
00040   SCALAR operator[](const unsigned long i)
00041   { //SCALAR array
00042     assert(i < 3);
00043     return(array[i]);
00044   }
00045 
00046   SCALAR operator[](const int i)
00047   { //SCALAR array
00048     assert(i >= 0 && i < 3);
00049     return(array[i]);
00050   }
00051 
00052   SCALAR operator[](const unsigned int i)
00053   { //SCALAR array
00054     assert(i < 3);
00055     return(array[i]);
00056   }
00057 
00058   SCALAR operator[](const short i)
00059   { //SCALAR array
00060     assert(i >= 0 && i < 3);
00061     return(array[i]);
00062   }
00063 
00064   SCALAR operator[](const unsigned short i)
00065   { //SCALAR array
00066     assert(i < 3);
00067     return(array[i]);
00068   }
00069 
00070   SCALAR operator[](const char i)
00071   { //SCALAR array
00072     assert(i >= 0 && i < 3);
00073     return(array[i]);
00074   }
00075 
00076   SCALAR operator[](const unsigned char i)
00077   { //SCALAR array
00078     assert(i < 3);
00079     return(array[i]);
00080   }
00081 
00082 
00083 
00084   inline bool operator==(VECTOR &pv)
00085   { //Are these VECTORs equal ?
00086     return(x == pv.x && y == pv.y && z == pv.z);
00087   }
00088 
00089   bool nearlyEqual(VECTOR& pv, SCALAR e)
00090   {
00091     return(fabs(x-pv.x) < e && fabs(y-pv.y) < e && fabs(z-pv.z) < e);
00092   }
00093 
00094 
00095 
00096   inline bool operator!=(VECTOR &pv)
00097   { //Are these VECTORs different ?
00098     return(x != pv.x || y != pv.y || z != pv.z);
00099   }
00100 
00101 #ifndef SCALARUNSIGNED
00102   inline VECTOR operator-()
00103   { //negate
00104     return(VECTOR(-x, -y, -z));
00105   }
00106 #endif
00107 
00108   inline VECTOR& operator=(VECTOR &pv)
00109   {
00110     x = pv.x;
00111     y = pv.y;
00112     z = pv.z;
00113     return(*this);
00114   }
00115 
00116   inline VECTOR& operator=(float &ps)
00117   {
00118     x = y = z = (SCALAR)ps;
00119     return(*this);
00120   }
00121 
00122   inline VECTOR& operator=(double &ps)
00123   {
00124     x = y = z = (SCALAR)ps;
00125     return(*this);
00126   }
00127 
00128   inline VECTOR& operator=(long &ps)
00129   {
00130     x = y = z = (SCALAR)ps;
00131     return(*this);
00132   }
00133 
00134   inline VECTOR& operator=(unsigned long &ps)
00135   {
00136     x = y = z = (SCALAR)ps;
00137     return(*this);
00138   }
00139 
00140   inline VECTOR& operator=(int &ps)
00141   {
00142     x = y = z = (SCALAR)ps;
00143     return(*this);
00144   }
00145 
00146   inline VECTOR& operator=(unsigned int &ps)
00147   {
00148     x = y = z = (SCALAR)ps;
00149     return(*this);
00150   }
00151 
00152   inline VECTOR& operator=(short &ps)
00153   {
00154     x = y = z = (SCALAR)ps;
00155     return(*this);
00156   }
00157 
00158   inline VECTOR& operator=(unsigned short &ps)
00159   {
00160     x = y = z = (SCALAR)ps;
00161     return(*this);
00162   }
00163 
00164   inline VECTOR& operator=(char &ps)
00165   {
00166     x = y = z = (SCALAR)ps;
00167     return(*this);
00168   }
00169 
00170   inline VECTOR& operator=(unsigned char &ps)
00171   {
00172     x = y = z = (SCALAR)ps;
00173     return(*this);
00174   }
00175 
00176 
00177 
00178   inline operator SCALAR*()
00179   {
00180     return(array);
00181   }
00182 
00183 
00185   //VECTOR x VECTOR operations
00186 
00187   inline VECTOR operator+(VECTOR &pv)
00188   { //VECTOR addition
00189     return(VECTOR(x + pv.x, y + pv.y, z + pv.z));
00190   }
00191 
00192   inline VECTOR& operator+=(VECTOR &pv)
00193   { //self VECTOR addition
00194     x += pv.x;
00195     y += pv.y;
00196     z += pv.z;
00197     return(*this);
00198   }
00199 
00200   inline VECTOR operator-(VECTOR &pv)
00201   { //VECTOR subtraction
00202     return(VECTOR(x - pv.x, y - pv.y, z - pv.z));
00203   }
00204 
00205   inline VECTOR& operator-=(VECTOR &pv)
00206   { //self VECTOR subtraction
00207     x -= pv.x;
00208     y -= pv.y;
00209     z -= pv.z;
00210     return(*this);
00211   }
00212 
00213 
00214   inline VECTOR operator*(VECTOR &pv)
00215   { //VECTOR multiply
00216     return(VECTOR(x * pv.x, y * pv.y, z * pv.z));
00217   }
00218 
00219   inline VECTOR& operator*=(VECTOR &pv)
00220   { //self VECTOR multiply
00221     x *= pv.x;
00222     y *= pv.y;
00223     z *= pv.z;
00224   }
00225 
00226   inline VECTOR operator/(VECTOR &pv)
00227   { //VECTOR division
00228     return(VECTOR(x / pv.x, y / pv.y, z / pv.z));
00229   }
00230 
00231   inline VECTOR& operator/=(VECTOR &pv)
00232   { //self VECTOR division
00233     x /= pv.x;
00234     y /= pv.y;
00235     z /= pv.z;
00236   }
00237 
00238 
00240   // Skalar operations
00241 
00242   inline VECTOR operator+(SCALAR s)
00243   { //VECTOR + Scalar
00244     return(VECTOR(x + s, y + s, z + s));
00245   }
00246 
00247   inline VECTOR& operator+=(SCALAR s)
00248   { //VECTOR + Scalar
00249     x += s;
00250     y += s;
00251     z += s;
00252   }
00253 
00254   inline VECTOR operator-(SCALAR s)
00255   { //VECTOR - Scalar
00256     return(VECTOR(x - s, y - s, z - s));
00257   }
00258 
00259   inline VECTOR& operator-=(SCALAR s)
00260   { //VECTOR - Scalar
00261     x -= s;
00262     y -= s;
00263     z -= s;
00264   }
00265 
00266   inline VECTOR operator*(SCALAR s)
00267   { //VECTOR * Scalar multiplication
00268     return(VECTOR(x * s, y * s, z * s));
00269   }
00270 
00271   inline VECTOR& operator*=(SCALAR s)
00272   { //VECTOR * Scalar multiplication
00273     x *= s;
00274     y *= s;
00275     z *= s;
00276     return(*this);
00277   }
00278 
00279   friend inline VECTOR operator*(SCALAR& s, VECTOR& pv);
00280 
00281   inline VECTOR operator/(SCALAR s)
00282   { //VECTOR / Scalar division
00283     return(VECTOR(x / s, y / s, z / s));
00284   }
00285 
00286   inline VECTOR& operator/=(SCALAR s)
00287   { //VECTOR / Scalar division
00288     x /= s;
00289     y /= s;
00290     z /= s;
00291     return(*this);
00292   }
00293 
00294 
00296   // Several helper methods
00297 
00298   inline void Normalize()
00299   {
00300     SCALAR f = getLength();
00301     x /= f;
00302     y /= f;
00303     z /= f;
00304   }
00305 
00306   inline void unit()
00307   {
00308     Normalize();
00309   }
00310 
00311 
00312   inline SCALAR getLength()
00313   {
00314     // Calculate vector magnitude |pv| = sqrt(x*x + y*y + z*z);
00315     return((SCALAR)sqrt(x*x + y*y + z*z));
00316   }
00317 
00318   inline SCALAR len()
00319   {
00320     return(getLength());
00321   }
00322 
00323   inline SCALAR getQuadLength()
00324   {
00325     // Calculate quadratic vector magnitude |pv|² = x*x + y*y + z*z;
00326     return(x*x + y*y + z*z);
00327   }
00328 
00329   inline SCALAR quadlen()
00330   {
00331     return(getQuadLength());
00332   }
00333 
00334 
00335   inline VECTOR getCrossProduct(VECTOR &pv)
00336   { //Calculates Cross product = Normal, //Computergraphics S.608
00337     return(VECTOR(y*pv.z - z*pv.y, z*pv.x - x*pv.z, x*pv.y - y*pv.x));
00338   }
00339 
00340   inline VECTOR cross(VECTOR &pv)
00341   {
00342     return(getCrossProduct(pv));
00343   }
00344 
00345 
00346   //value examples (if normalized): angle = 0 --> 1, angle = 90 --> 0, angle = 180 --> -1 usw.
00347   inline SCALAR getDotProduct(VECTOR &pv)
00348   { //Calculates Dot product = |v1|*|v2|*cos(theta), //Computergraphics S.607
00349     return(x*pv.x + y*pv.y + z*pv.z);  //= |v1|*|v2|*cos(theta)
00350   }
00351 
00352   inline SCALAR dot(VECTOR &pv)
00353   { 
00354     return(getDotProduct(pv));
00355   }
00356   
00357 
00358   inline SCALAR getAngle(VECTOR &pv)
00359   { //Calculates the angle between this two VECTORs
00360     return((SCALAR)(acos((double)getDotProduct(pv)) * (double)MUL_RAD_TO_DEG)); //convert from rad angle to deg angle
00361   }
00362 
00363   inline SCALAR angle(VECTOR &pv)
00364   {
00365     return(getAngle(pv));
00366   }
00367 
00368 
00369   inline SCALAR getDistance(VECTOR &pv)
00370   { //Calculates the distance between this two VECTORs
00371     VECTOR t(pv.x - x, pv.y - y, pv.z - z);
00372     return(t.getLength());
00373   }
00374 
00375   inline SCALAR getQuadDistance(VECTOR &pv)
00376   { //Calculates the quadric distance between this two VECTORs
00377     VECTOR t(pv.x - x, pv.y - y, pv.z - z);
00378     return(t.getQuadLength());
00379   }
00380 
00381 
00382   inline void RotateX(SCALAR angle)
00383   { //Computergraphics S.410
00384     double a = (double)angle * (double)MUL_DEG_TO_RAD;  //convert from deg to rad
00385     double vsin = sin(a);
00386     double vcos = cos(a);
00387     VECTOR t(x, y, z);  //temporary VECTOR
00388     
00389     y = (SCALAR)(vcos * (double)t.y - vsin * (double)t.z);
00390     z = (SCALAR)(vsin * (double)t.y + vcos * (double)t.z);
00391   }
00392 
00393   inline void RotateY(SCALAR angle)
00394   { //Computergraphics S.410
00395     double a = (double)angle * (double)MUL_DEG_TO_RAD;  //convert from deg to rad
00396     double vsin = sin(a);
00397     double vcos = cos(a);
00398     VECTOR t(x, y, z);  //temporary VECTOR
00399     
00400     z = (SCALAR)(vcos * (double)t.z - vsin * (double)t.x);
00401     x = (SCALAR)(vsin * (double)t.z + vcos * (double)t.x);
00402   }
00403 
00404   inline void RotateZ(SCALAR angle)
00405   { //Computergraphics S.410
00406     double a = (double)angle * (double)MUL_DEG_TO_RAD;  //convert from deg to rad
00407     double vsin = sin(a);
00408     double vcos = cos(a);
00409     VECTOR t(x, y, z);  //temporary VECTOR
00410     
00411     x = (SCALAR)(vcos * (double)t.x - vsin * (double)t.y);
00412     y = (SCALAR)(vsin * (double)t.x + vcos * (double)t.y);
00413   }
00414 
00415 
00416 #ifdef USEMATRIX
00417   inline void Rotate(SCALAR angle, VECTOR &pv)
00418   {
00419     VECTORMATRIX m;
00420     m.setRotate(angle, pv);
00421     *this = m * (*this);
00422   }
00423 
00424   inline void Rotate(SCALAR angle, SCALAR px, SCALAR py, SCALAR pz)
00425   {
00426     Rotate(angle, VECTOR(px, py, pz));
00427   }
00428 #endif
00429 
00430 
00431   inline void Translate(VECTOR &pv)
00432   {
00433     (*this) += pv;
00434   }
00435 
00436   inline void Translate(SCALAR px, SCALAR py, SCALAR pz)
00437   {
00438     x += px;
00439     y += py;
00440     z += pz;
00441   }
00442 
00443 
00444   inline void Scale(VECTOR &pv)
00445   {
00446     (*this) *= pv;
00447   }
00448 
00449   inline void Scale(SCALAR sx, SCALAR sy, SCALAR sz)
00450   {
00451     x *= sx;
00452     y *= sy;
00453     z *= sz;
00454   }
00455 };
00456 
00457 
00458 VECTOR operator*(SCALAR& s, VECTOR& pv)
00459 {
00460   return(pv * s);
00461 }

Generated on Tue Jan 22 19:13:38 2002 for FlowViz by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001