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
00010 {
00011 public:
00012
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
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
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
00037
00038
00039
00040
00041
00042
00043
00044
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
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
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
00151 float x, y, z;
00152 };
00153
00154
00155 class CVector2
00156 {
00157 public:
00158 CVector2(){x=0;y=0;}
00159 inline CVector2(float X, float Y){x=X;y=Y;}
00160
00161 CVector2(CVector2 spitze, CVector2 schaft)
00162 {
00163 CVector2 v = (spitze - schaft);
00164 x = v.x;
00165 y = v.y;
00166 }
00167
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
00187 return acos(a);
00188 }
00189
00190
00191 float x, y;
00192
00193
00194 };
00195
00196
00197
00198 #endif