00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #if !defined(AFX_FixedVector_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_)
00022 #define AFX_FixedVector_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_
00023
00024 #include <common/DefinesAssert.h>
00025 #include <common/Vector.h>
00026 #include <common/fixed.h>
00027
00028 class FixedVector
00029 {
00030 public:
00031 FixedVector()
00032 {
00033 V[0] = V[1] = V[2] = fixed(0);
00034 }
00035
00036 FixedVector(fixed a, fixed b, fixed c)
00037 {
00038 V[0] = a;
00039 V[1] = b;
00040 V[2] = c;
00041 }
00042
00043 FixedVector(const FixedVector &v)
00044 {
00045 V[0] = ((FixedVector &) v)[0];
00046 V[1] = ((FixedVector &) v)[1];
00047 V[2] = ((FixedVector &) v)[2];
00048 }
00049
00050 void zero()
00051 {
00052 V[0] = V[1] = V[2] = fixed(0);
00053 }
00054
00055 FixedVector Normalize()
00056 {
00057 fixed mag = Magnitude();
00058 FixedVector v;
00059 if (mag == fixed(0)) mag = fixed(true, 1);
00060 v = (*this) / mag;
00061 return v;
00062 }
00063
00064 fixed Magnitude();
00065
00066 FixedVector get2DPerp()
00067 {
00068 FixedVector v(V[1], -V[0], 0);
00069 return v;
00070 }
00071
00072 fixed dotP(const FixedVector &Vin)
00073 {
00074 FixedVector &V1 = (*this);
00075 FixedVector &V2 = (FixedVector &) Vin;
00076 return (V1.V[0] * V2.V[0]) + (V1.V[1] * V2.V[1]) + (V1.V[2] * V2.V[2]);
00077 }
00078
00079 void StoreInvert()
00080 {
00081 V[0] = -V[0];
00082 V[1] = -V[1];
00083 V[2] = -V[2];
00084 }
00085
00086 fixed Max()
00087 {
00088 return MAX(V[0], MAX(V[1], V[2]));
00089 }
00090
00091 void StoreNormalize()
00092 {
00093 fixed mag = Magnitude();
00094 if (mag == fixed(0)) mag = fixed(true, 1);
00095 (*this) /= mag;
00096 }
00097
00098 FixedVector operator+(const fixed m)
00099 {
00100 FixedVector v(V[0]+m, V[1]+m, V[2]+m);
00101 return v;
00102 }
00103
00104 FixedVector operator+(const FixedVector &Vin)
00105 {
00106 FixedVector v(
00107 ((FixedVector &) Vin).V[0] + V[0],
00108 ((FixedVector &) Vin).V[1] + V[1],
00109 ((FixedVector &) Vin).V[2] + V[2]);
00110 return v;
00111 }
00112
00113 FixedVector operator-(const fixed m)
00114 {
00115 FixedVector v(V[0]-m, V[1]-m, V[2]-m);
00116 return v;
00117 }
00118
00119 FixedVector operator-(const FixedVector &Vin)
00120 {
00121 FixedVector v(
00122 V[0] - ((FixedVector &) Vin).V[0],
00123 V[1] - ((FixedVector &) Vin).V[1],
00124 V[2] - ((FixedVector &) Vin).V[2]);
00125 return v;
00126 }
00127
00128 void operator*=(const FixedVector &Vin)
00129 {
00130 fixed a = V[1] * ((FixedVector &)Vin)[2] - V[2] * ((FixedVector &)Vin)[1];
00131 fixed b = V[2] * ((FixedVector &)Vin)[0] - V[0] * ((FixedVector &)Vin)[2];
00132 fixed c = V[0] * ((FixedVector &)Vin)[1] - V[1] * ((FixedVector &)Vin)[0];
00133 V[0] = a;
00134 V[1] = b;
00135 V[2] = c;
00136 }
00137
00138 FixedVector operator*(const FixedVector &Vin)
00139 {
00140 FixedVector v(
00141 V[1] * ((FixedVector &)Vin)[2] - V[2] * ((FixedVector &)Vin)[1],
00142 V[2] * ((FixedVector &)Vin)[0] - V[0] * ((FixedVector &)Vin)[2],
00143 V[0] * ((FixedVector &)Vin)[1] - V[1] * ((FixedVector &)Vin)[0]);
00144
00145 return v;
00146 }
00147
00148 FixedVector operator*(const fixed a)
00149 {
00150 FixedVector v(V[0]*a, V[1]*a, V[2]*a);
00151 return v;
00152 }
00153
00154 FixedVector operator/(const fixed a)
00155 {
00156 const fixed b = (fixed(a)==fixed(0)?fixed(true, 1):a);
00157 FixedVector v(V[0]/b, V[1]/b, V[2]/b);
00158 return v;
00159 }
00160
00161 FixedVector operator-()
00162 {
00163 fixed a = -V[0];
00164 FixedVector v(-V[0], -V[1], -V[2]);
00165 return v;
00166 }
00167
00168 Vector &asVector()
00169 {
00170 static Vector a[10];
00171 static unsigned int count = 0;
00172
00173 Vector &b = a[++count % 10];
00174 b[0] = V[0].asFloat();
00175 b[1] = V[1].asFloat();
00176 b[2] = V[2].asFloat();
00177 return b;
00178 }
00179
00180 void asVector(Vector &dest)
00181 {
00182 dest[0] = V[0].asFloat();
00183 dest[1] = V[1].asFloat();
00184 dest[2] = V[2].asFloat();
00185 }
00186
00187 static FixedVector &fromVector(Vector &vec)
00188 {
00189 static FixedVector result;
00190 result[0] = fixed::fromFloat(vec[0]);
00191 result[1] = fixed::fromFloat(vec[1]);
00192 result[2] = fixed::fromFloat(vec[2]);
00193 return result;
00194 }
00195
00196 void operator*=(const fixed a)
00197 {
00198 V[0] *= a;
00199 V[1] *= a;
00200 V[2] *= a;
00201 }
00202
00203 void operator/=(const fixed a)
00204 {
00205 const fixed b = (fixed(a)==fixed(0)?fixed(true, 1):a);
00206 V[0] /= b;
00207 V[1] /= b;
00208 V[2] /= b;
00209 }
00210
00211 void operator+=(const fixed a)
00212 {
00213 V[0] += a;
00214 V[1] += a;
00215 V[2] += a;
00216 }
00217
00218 void operator+=(const FixedVector &Vin)
00219 {
00220 V[0] += Vin.V[0];
00221 V[1] += Vin.V[1];
00222 V[2] += Vin.V[2];
00223 }
00224
00225 void operator-=(const fixed a)
00226 {
00227 (*this) += -fixed(a);
00228 }
00229
00230 void operator-=(const FixedVector &Vin)
00231 {
00232 V[0] -= Vin.V[0];
00233 V[1] -= Vin.V[1];
00234 V[2] -= Vin.V[2];
00235 }
00236
00237 bool operator==(const FixedVector &Vin)
00238 {
00239 return (
00240 ((FixedVector &) Vin).V[0]==V[0] &&
00241 ((FixedVector &) Vin).V[1]==V[1] &&
00242 ((FixedVector &) Vin).V[2]==V[2]);
00243 }
00244
00245 bool operator!=(const FixedVector &Vin1)
00246 {
00247 return !((*this) == Vin1);
00248 }
00249
00250 fixed &operator[](const int m) { DIALOG_ASSERT(m<=2); return V[m]; }
00251 fixed const &operator[](const int m) const { DIALOG_ASSERT(m<=2); return V[m]; }
00252
00253 operator fixed*() { return V; }
00254 static FixedVector &getNullVector();
00255
00256 protected:
00257 fixed V[3];
00258
00259 };
00260
00261 #endif // !defined(AFX_FixedVector_H__AD959187_7A1C_11D2_957C_00A0C9A4CA3E__INCLUDED_)
00262