00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #if !defined(AFX_VECTOR_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_)
00022 #define AFX_VECTOR_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_
00023
00024 #include <common/Defines.h>
00025 #include <math.h>
00026
00027 class Vector
00028 {
00029 public:
00030 Vector()
00031 {
00032 V[0] = V[1] = V[2] = 0.0f;
00033 }
00034
00035 Vector(const Vector &v)
00036 {
00037 V[0] = ((Vector &) v)[0];
00038 V[1] = ((Vector &) v)[1];
00039 V[2] = ((Vector &) v)[2];
00040 }
00041
00042 Vector(const float Pt[3])
00043 {
00044 V[0] = Pt[0];
00045 V[1] = Pt[1];
00046 V[2] = Pt[2];
00047 }
00048
00049 Vector(const float ang, const double length)
00050 {
00051 V[0] = (float) sin(ang / 180.0f * 3.14f) * float(length);
00052 V[1] = (float) cos(ang / 180.0f * 3.14f) * float(length);
00053 V[2] = 0.0f;
00054 }
00055
00056 Vector(const float ptA, const float ptB, const float ptC=0.0f)
00057 {
00058 V[0] = ptA;
00059 V[1] = ptB;
00060 V[2] = ptC;
00061 }
00062
00063 Vector(const int ptA, const int ptB, const int ptC=0)
00064 {
00065 V[0] = (float) ptA;
00066 V[1] = (float) ptB;
00067 V[2] = (float) ptC;
00068 }
00069
00070 void initialise(const float a, const float b, const float c)
00071 {
00072 V[0] = a;
00073 V[1] = b;
00074 V[2] = c;
00075 }
00076
00077 Vector Normalize()
00078 {
00079 float mag = Magnitude();
00080 Vector v;
00081 if (mag == 0.0f) mag = 0.00001f;
00082 v = (*this) / mag;
00083 return v;
00084 }
00085
00086 Vector Normalize2D()
00087 {
00088 float mag = float(sqrt(V[0]*V[0] + V[1]*V[1]));
00089 Vector v;
00090 if (mag == 0.0f) mag = 0.00001f;
00091 v = (*this) / mag;
00092 return v;
00093 }
00094
00095 float Magnitude()
00096 {
00097 return float(sqrt(MagnitudeSquared()));
00098 }
00099
00100 float Magnitude2d()
00101 {
00102 return float(sqrt(Magnitude2dSquared()));
00103 }
00104
00105 float Magnitude2dSquared()
00106 {
00107 return V[0]*V[0] + V[1]*V[1];
00108 }
00109
00110 float MagnitudeSquared()
00111 {
00112 return V[0]*V[0] + V[1]*V[1] + V[2]*V[2];
00113 }
00114
00115 float dotP(const Vector &Vin)
00116 {
00117 Vector &V1 = (*this);
00118 Vector &V2 = (Vector &) Vin;
00119 return (V1.V[0] * V2.V[0]) + (V1.V[1] * V2.V[1]) + (V1.V[2] * V2.V[2]);
00120 }
00121
00122 float Max()
00123 {
00124 return MAX(V[0], MAX(V[1], V[2]));
00125 }
00126
00127 Vector get2DPerp()
00128 {
00129 Vector v(V[1], -V[0], 0.0f);
00130 return v;
00131 }
00132
00133 void StoreInvert()
00134 {
00135 V[0] = -V[0];
00136 V[1] = -V[1];
00137 V[2] = -V[2];
00138 }
00139
00140 void StoreNormalize()
00141 {
00142 float mag = Magnitude();
00143 if (mag == 0.0f) mag = 0.00001f;
00144 (*this) /= mag;
00145 }
00146
00147 void zero()
00148 {
00149 V[0] = V[1] = V[2] = 0.0f;
00150 }
00151
00152 Vector operator+(const float m)
00153 {
00154 Vector v(V[0]+m, V[1]+m, V[2]+m);
00155 return v;
00156 }
00157
00158 Vector operator+(const Vector &Vin)
00159 {
00160 Vector v(Vin.V[0] + V[0], Vin.V[1] + V[1], Vin.V[2] + V[2]);
00161 return v;
00162 }
00163
00164 Vector operator-(const float m)
00165 {
00166 Vector v(V[0]-m, V[1]-m, V[2]-m);
00167 return v;
00168 }
00169
00170 Vector operator-(const Vector &Vin)
00171 {
00172 Vector v(V[0] - Vin.V[0], V[1] - Vin.V[1], V[2] - Vin.V[2]);
00173 return v;
00174 }
00175
00176 Vector operator*(const float a)
00177 {
00178 Vector v(V[0]*a, V[1]*a, V[2]*a);
00179 return v;
00180 }
00181
00182 Vector operator*(const Vector &Vin)
00183 {
00184 Vector v(V[1] * ((Vector &)Vin)[2] - V[2] * ((Vector &)Vin)[1],
00185 V[2] * ((Vector &)Vin)[0] - V[0] * ((Vector &)Vin)[2],
00186 V[0] * ((Vector &)Vin)[1] - V[1] * ((Vector &)Vin)[0]);
00187
00188 return v;
00189 }
00190
00191 Vector operator/(const float a)
00192 {
00193 const float b = (a==0.0f?0.00001f:a);
00194 Vector v(V[0]/b, V[1]/b, V[2]/b);
00195 return v;
00196 }
00197
00198 Vector operator/(const Vector &Vin)
00199 {
00200 float a = ((Vector &)Vin)[0];
00201 float b = ((Vector &)Vin)[1];
00202 float c = ((Vector &)Vin)[2];
00203
00204 const float a2 = (a==0.0f?0.00001f:a);
00205 const float b2 = (b==0.0f?0.00001f:b);
00206 const float c2 = (c==0.0f?0.00001f:c);
00207
00208 Vector v(V[0]/ a2, V[1]/ b2, V[2]/ c2);
00209 return v;
00210 }
00211
00212 Vector operator-()
00213 {
00214 Vector v(-V[0], -V[1], -V[2]);
00215 return v;
00216 }
00217
00218 void operator*=(const float a)
00219 {
00220 V[0] *= a;
00221 V[1] *= a;
00222 V[2] *= a;
00223 }
00224
00225 void operator*=(const Vector &Vin)
00226 {
00227 float a = V[1] * ((Vector &)Vin)[2] - V[2] * ((Vector &)Vin)[1];
00228 float b = V[2] * ((Vector &)Vin)[0] - V[0] * ((Vector &)Vin)[2];
00229 float c = V[0] * ((Vector &)Vin)[1] - V[1] * ((Vector &)Vin)[0];
00230 V[0] = a;
00231 V[1] = b;
00232 V[2] = c;
00233 }
00234
00235 void operator/=(const float a)
00236 {
00237 const float b = (a==0.0f?0.00001f:a);
00238 V[0] /= b;
00239 V[1] /= b;
00240 V[2] /= b;
00241 }
00242
00243 void operator/=(const Vector &Vin)
00244 {
00245 float a = ((Vector &)Vin)[0];
00246 float b = ((Vector &)Vin)[1];
00247 float c = ((Vector &)Vin)[2];
00248
00249 const float a2 = (a==0.0f?0.00001f:a);
00250 const float b2 = (b==0.0f?0.00001f:b);
00251 const float c2 = (c==0.0f?0.00001f:c);
00252
00253 V[0] /= a2;
00254 V[1] /= b2;
00255 V[2] /= c2;
00256 }
00257
00258 void operator+=(const float a)
00259 {
00260 V[0] += a;
00261 V[1] += a;
00262 V[2] += a;
00263 }
00264
00265 void operator+=(const Vector &Vin)
00266 {
00267 V[0] += Vin.V[0];
00268 V[1] += Vin.V[1];
00269 V[2] += Vin.V[2];
00270 }
00271
00272 void operator-=(const float a)
00273 {
00274 (*this) += -a;
00275 }
00276
00277 void operator-=(const Vector &Vin)
00278 {
00279 V[0] -= Vin.V[0];
00280 V[1] -= Vin.V[1];
00281 V[2] -= Vin.V[2];
00282 }
00283
00284 bool operator==(const Vector &Vin1)
00285 {
00286 return (Vin1.V[0]==V[0] && Vin1.V[1]==V[1] && Vin1.V[2]==V[2]);
00287 }
00288
00289 bool operator!=(const Vector &Vin1)
00290 {
00291 return !((*this) == Vin1);
00292 }
00293
00294 float &operator[](const int m) { DIALOG_ASSERT(m<=2); return V[m]; }
00295 float const &operator[](const int m) const { DIALOG_ASSERT(m<=2); return V[m]; }
00296
00297 operator float*() { return V; }
00298 static Vector &getNullVector();
00299
00300 protected:
00301 float V[3];
00302
00303 };
00304
00305 #endif // !defined(AFX_VECTOR_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_)
00306