Main Page   Compound List   File List   Compound Members  

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

Generated on Sun Dec 16 15:49:30 2001 for Fourier Volume Renderer by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001