Vector_wocomments.h

00001 #ifndef __vector_h__
00002 #define __vector_h__
00003 
00004 #include <math.h>
00005 #include "globals.h"
00006 #ifndef PI
00007         #define PI 3.1415926535897;
00008 #endif
00009 class Vector                                            // same data-layout as engine's vec3_t,
00010 {                                                               //              which is a vec_t[3]
00011 public:
00012         // Construction/destruction
00013         inline Vector(void)                                                             { this->x=0;this->y=0;this->z=0;}
00014         inline Vector(float X, float Y, float Z)                { this->x = X; this->y = Y; this->z = Z;                                                }
00015         inline Vector(const Vector& v)                                  { this->x = v.x; this->y = v.y; this->z = v.z;                          } 
00016         inline Vector(float rgfl[3])                                    { this->x = rgfl[0]; this->y = rgfl[1]; this->z = rgfl[2];      }
00017         inline Vector(Vector spitze, Vector schaft)
00018         {
00019                 Vector v = (spitze - schaft);
00020                 x = v.x; 
00021                 y = v.y;
00022                 z = v.z;
00023         }
00024         // Operators
00025         inline Vector operator-(void) const                             { return Vector(-x,-y,-z);                              }
00026         inline int operator==(const Vector& v) const    { return x==v.x && y==v.y && z==v.z;    }
00027         inline int operator!=(const Vector& v) const    { return !(*this==v);                                   }
00028         inline Vector operator+(const Vector& v) const  { return Vector(x+v.x, y+v.y, z+v.z);   }
00029         inline Vector operator-(const Vector& v) const  { return Vector(x-v.x, y-v.y, z-v.z);   }
00030         inline Vector operator*(float fl) const                 { return Vector(x*fl, y*fl, z*fl);              }
00031         inline Vector operator/(float fl) const                 { return Vector(x/fl, y/fl, z/fl);              }
00032 
00033         // Methods
00034         inline void copyToArray(float* rgfl) const              { rgfl[0] = x, rgfl[1] = y, rgfl[2] = z; }
00035         inline float length(void) const                                 { return sqrt(x*x + y*y + z*z); }
00036         //operator float *()                                                            { return &x; } // Vectors will now automatically convert to float * when needed
00037         //operator const float *() const                                        { return &x; } // Vectors will now automatically convert to float * when needed
00038         /*float clampTo(float min, float max, float Val)
00039         {
00040                 if (Val > max) Val = max;
00041                 else 
00042                         if(Val < min) Val = min;
00043 
00044                 return Val;
00045         }  ;
00046         */
00047         Vector normalize(void) const
00048         {
00049                 float flLen = this->length();
00050                 if (flLen == 0) return Vector(0,0,0); // ????
00051                 return Vector(x / flLen, y / flLen, z / flLen);
00052         };
00053         inline float dotProduct(const Vector& b) 
00054         { 
00055                 return(x*b.x + y*b.y + z*b.z); 
00056         }   ;
00057         inline Vector crossProduct(const Vector& b) 
00058         { 
00059                 return Vector( y*b.z - z*b.y, z*b.x - x*b.z, x*b.y - y*b.x ); 
00060         }  ;
00061         /*
00062         void rotatePoint(float angle, float x, float y, float z)
00063         {
00064                 float bg = (angle/180)*PI;
00065                 Vector* vNewView = new Vector();
00066                 
00067                 // Calculate the sine and cosine of the angle once
00068                 float cosTheta = (float)cos(bg);
00069                 float sinTheta = (float)sin(bg);
00070 
00071                 // Find the new x position for the new rotated point
00072                 vNewView->x  = (cosTheta + (1 - cosTheta) * x * x)              * this->x;
00073                 vNewView->x += ((1 - cosTheta) * x * y - z * sinTheta)  * this->y;
00074                 vNewView->x += ((1 - cosTheta) * x * z + y * sinTheta)  * this->z;
00075 
00076                 // Find the new y position for the new rotated point
00077                 vNewView->y  = ((1 - cosTheta) * x * y + z * sinTheta)  * this->x;
00078                 vNewView->y += (cosTheta + (1 - cosTheta) * y * y)              * this->y;
00079                 vNewView->y += ((1 - cosTheta) * y * z - x * sinTheta)  * this->z;
00080 
00081                 // Find the new z position for the new rotated point
00082                 vNewView->z  = ((1 - cosTheta) * x * z - y * sinTheta)  * this->x;
00083                 vNewView->z += ((1 - cosTheta) * y * z + x * sinTheta)  * this->y;
00084                 vNewView->z += (cosTheta + (1 - cosTheta) * z * z)              * this->z;
00085 
00086                 // Now we just add the newly rotated vector to our position to set
00087                 // our new rotated view of our camera.
00088                 this->x= vNewView->x;
00089                 this->y= vNewView->y;
00090                 this->z= vNewView->z;
00091         }       ;
00092         */
00093         void rotateX(float angle)
00094         {
00095                 float bg = (angle/180)*PI;
00096                 float x= this->x;
00097                 float y= this->y;
00098                 float z= this->z;
00099 
00100                 float cosTheta = (float)cos(bg);
00101                 float sinTheta = (float)sin(bg);
00102  
00103                 this->x= x;
00104                 this->y= clampTo(-1,1,y*cosTheta - z *sinTheta );
00105                 this->z= clampTo(-1,1,y*sinTheta + z *cosTheta) ;
00106                 this->normalize();
00107 
00108 
00109         }        ;
00110         static float clampTo(float min, float max, float Val)
00111         {
00112                 if (Val > max) {Val = max; }
00113                 else {
00114                         if(Val < min){ Val = min;}
00115                         }
00116 
00117                         return Val;
00118         };
00119         void rotateY(float angle)
00120         {
00121                 float bg = (angle/180)*PI;
00122                 float x= this->x;
00123                 float y= this->y;
00124                 float z= this->z;
00125                 float cosTheta = (float)cos(bg);
00126                 float sinTheta = (float)sin(bg);
00127  
00128                 this->x= clampTo(-1,1,x*cosTheta + z*sinTheta);
00129                 this->y= y;
00130                 this->z= clampTo(-1,1,-1*x*sinTheta + z *cosTheta) ;
00131         }        ;
00132         void rotateZ(float angle)
00133         {
00134         
00135                 float bg = (angle/180)*PI;
00136                 float x= this->x;
00137                 float y= this->y;
00138                 float z= this->z;
00139 
00140                 float cosTheta = (float)cos(bg);
00141                 float sinTheta = (float)sin(bg);
00142  
00143                 this->x= clampTo(-1,1,x*cosTheta - y*sinTheta);
00144                 this->y= clampTo(-1,1,x*sinTheta + y *cosTheta );
00145                 this->z= z;
00146                 this->normalize();
00147 
00148         }        ;
00149         
00150         // Members
00151         float x, y, z;
00152 };
00153 
00154 // 2D Vector
00155 class CVector2 
00156 {
00157 public:
00158         CVector2(){x=0;y=0;}
00159         inline CVector2(float X, float Y){x=X;y=Y;}
00160         //Spitze - Schaft
00161         CVector2(CVector2 spitze, CVector2 schaft)
00162         {
00163                 CVector2 v = (spitze - schaft);
00164                 x = v.x; 
00165                 y = v.y;
00166         }
00167         // Spitze - Schaft
00168         CVector2(float x1,float y1,float x2,float y2)
00169         {
00170                 CVector2 v = (CVector2(x1,y1) - CVector2(x2,y2));
00171                 x = v.x; 
00172                 y = v.y;
00173         }
00174         inline CVector2 operator-(void) const                           { return CVector2(-x,-y);                               }
00175         inline int operator==(const CVector2& v) const  { return x==v.x && y==v.y;      }
00176         inline int operator!=(const CVector2& v) const  { return !(*this==v);                                   }
00177         inline CVector2 operator+(const CVector2& v) const      { return CVector2(x+v.x, y+v.y);        }
00178         inline CVector2 operator-(const CVector2& v) const      { return CVector2(x-v.x, y-v.y);        }
00179         inline CVector2 operator*(float fl) const                       { return CVector2(x*fl, y*fl);          }
00180         inline CVector2 operator/(float fl) const                       { return CVector2(x/fl, y/fl);          }
00181         inline float length(void) const                                         { return sqrt(x*x + y*y); }
00182         inline CVector2 normalize(void) const                                   { float len = length(); return CVector2(x/len,y/len); }
00183         float angle(CVector2 v1)
00184         { 
00185                 float a = (x*v1.x + y*v1.y)/(this->length() * v1.length());     
00186                 //cout << "a:"<< a << "\n";
00187                 return acos(a); 
00188         }
00189 
00190 
00191         float x, y;
00192 
00193 
00194 };
00195 
00196 
00197 
00198 #endif

Generated on Wed Dec 7 14:42:11 2005 for VisUE by  doxygen 1.4.5