Main Page   Compound List   File List   Compound Members  

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

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