00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef __MX__TEMPX_H_
00017 #define __MX__TEMPX_H_
00018
00019 #include<iostream>
00020 #include<cstdlib>
00021 #include<cmath>
00022 #include "mx_exception.h"
00023
00024
00025
00026
00027
00028 namespace mx
00029 {
00030
00031
00032 template<typename Type, size_t vector_size>
00033 class mxExpandableVector {
00034
00035 public:
00036 typedef struct _vsize {
00037 Type data[vector_size];
00038 } data_vector;
00039
00040 enum { VECTOR_SIZE = vector_size };
00041
00042 mxExpandableVector();
00043 ~mxExpandableVector() { }
00044
00045
00046 mxExpandableVector(const mxExpandableVector<Type, vector_size> &t);
00047 mxExpandableVector<Type, vector_size> &operator=(const mxExpandableVector<Type, vector_size> &t);
00048 void scaleVector(Type amount);
00049 void setVector(const mxExpandableVector<Type, vector_size> &value);
00050 mxExpandableVector<Type, vector_size> scaleNewVector(Type amount);
00051 data_vector &getVector() const { return vector_data; }
00052
00053 mxExpandableVector<Type, vector_size> &operator+=(const mxExpandableVector<Type, vector_size> &);
00054
00055 mxExpandableVector<Type, vector_size> &operator+(const mxExpandableVector<Type, vector_size> &);
00056
00057
00058
00059 friend mxExpandableVector<Type, vector_size> operator+(const mxExpandableVector<Type, vector_size> &x1, const mxExpandableVector<Type, vector_size> &x2)
00060 {
00061 mxExpandableVector<Type, vector_size> temp(x1);
00062 temp+=x2;
00063 return temp;
00064 }
00065
00066 mxExpandableVector<Type, vector_size> &operator-=(const mxExpandableVector<Type, vector_size> &);
00067 mxExpandableVector<Type, vector_size> &operator-(const mxExpandableVector<Type, vector_size> &);
00068
00069 friend mxExpandableVector<Type, vector_size> operator-(const mxExpandableVector<Type, vector_size> &x1, const mxExpandableVector<Type, vector_size> &x2)
00070 {
00071 mxExpandableVector<Type, vector_size> temp(x1);
00072 temp-=x2;
00073 return temp;
00074 }
00075
00076
00077 mxExpandableVector<Type, vector_size> &operator*=(const mxExpandableVector<Type, vector_size> &);
00078 mxExpandableVector<Type, vector_size> &operator*(const mxExpandableVector<Type, vector_size> &);
00079
00080 friend mxExpandableVector<Type, vector_size> operator*(const mxExpandableVector<Type, vector_size> &x1, const mxExpandableVector<Type, vector_size> &x2)
00081 {
00082 mxExpandableVector<Type, vector_size> temp(x1);
00083 temp*=x2;
00084 return temp;
00085 }
00086 mxExpandableVector<Type, vector_size> &operator/(const mxExpandableVector<Type, vector_size> &);
00087
00088
00089 friend mxExpandableVector<Type, vector_size> operator/(const mxExpandableVector<Type, vector_size> &x1, const mxExpandableVector<Type, vector_size> &x2)
00090 {
00091 mxExpandableVector<Type, vector_size> temp(x1);
00092 temp/=x2;
00093 return temp;
00094 }
00095
00096
00097 mxExpandableVector<Type, vector_size> &operator/=(const mxExpandableVector<Type, vector_size> &);
00098
00099 Type &operator[](unsigned int index);
00100
00101
00102 float DotProduct(const mxExpandableVector<Type, vector_size> &v);
00103 float Length();
00104 void Normalize();
00105 float Cos(const mxExpandableVector<Type, vector_size> &v);
00106 void Build(const mxExpandableVector<Type, vector_size> &v);
00107 mxExpandableVector<Type, vector_size> Build(mxExpandableVector<Type, vector_size> &v1, mxExpandableVector<Type, vector_size> v2);
00108
00109 friend std::ostream &operator<<(std::ostream &out, const mxExpandableVector<Type, vector_size> &v)
00110 {
00111
00112 out << "<";
00113 unsigned int i;
00114 for(i=0; i < mxExpandableVector<Type, vector_size>::VECTOR_SIZE-1; i++) out << v.vector_data.data[i] << ",";
00115 out << v.vector_data.data[i] << ">";
00116 return out;
00117 }
00118
00119 protected:
00120 data_vector vector_data;
00121
00122
00123 };
00124
00125
00126
00127 template<typename Type, size_t size>
00128 class vector4D : public mxExpandableVector<Type, size> {
00129 public:
00130 vector4D(const vector4D<Type,size> &v);
00131 vector4D();
00132
00133 Type &x,&y,&z,&w;
00134
00135 };
00136
00137 template<typename Type, size_t size>
00138 class vector2D : public mxExpandableVector<Type, size> {
00139 public:
00140 vector2D(const vector2D<Type, size> &v);
00141 vector2D();
00142
00143 Type &x, &y;
00144 void setPos(Type sx, Type sy) { x = sx, y = sy; }
00145 void getPos(Type &sx,Type &sy) { sx = x, sy = y; }
00146
00147 };
00148
00149 template<typename Type, size_t vector_size>
00150 vector2D<Type, vector_size>::vector2D() : x(this->vector_data.data[0]), y(this->vector_data.data[1])
00151 {
00152 this->vector_data.data[0] = 0;
00153 this->vector_data.data[1] = 0;
00154 }
00155
00156 template<typename Type, size_t vsize>
00157 vector2D<Type, vsize>::vector2D(const vector2D<Type, vsize> &v) : vector2D()
00158 {
00159 setVector(v);
00160 }
00161
00162
00163
00164 template<typename Type, size_t vector_size>
00165 vector4D<Type, vector_size>::vector4D() : x(this->vector_data.data[0]), y(this->vector_data.data[1]),z(this->vector_data.data[2]), w(this->vector_data.data[3])
00166 {
00167 this->vector_data.data[0] = 0;
00168 this->vector_data.data[1] = 0;
00169 this->vector_data.data[2] = 0;
00170 this->vector_data.data[3] = 0;
00171
00172 }
00173 template<typename Type, size_t vector_size>
00174 vector4D<Type,vector_size>::vector4D(const vector4D<Type,vector_size> &v) : vector4D()
00175 {
00176 setVector(v);
00177 }
00178
00179
00180 template<typename Type, size_t vector_size>
00181 mxExpandableVector<Type, vector_size>::mxExpandableVector()
00182 {
00183
00184 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] = 0;
00185
00186 }
00187
00188
00189
00190
00191 template<typename Type, size_t vector_size>
00192 mxExpandableVector<Type,vector_size>::mxExpandableVector(const mxExpandableVector<Type, vector_size> &t)
00193 {
00194 setVector(t);
00195
00196 }
00197
00198
00199 template<typename Type, size_t vector_size>
00200 mxExpandableVector<Type, vector_size> &mxExpandableVector<Type, vector_size>::operator=(const mxExpandableVector<Type, vector_size> &t)
00201 {
00202 setVector(t);
00203 return *this;
00204 }
00205
00206
00207
00208 template<typename Type, size_t vector_size>
00209 void mxExpandableVector<Type, vector_size>::setVector(const mxExpandableVector<Type, vector_size> &value)
00210 {
00211 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] = value.vector_data.data[i];
00212
00213 }
00214
00215 template<typename Type, size_t vector_size>
00216 void mxExpandableVector<Type,vector_size>::scaleVector(Type amount)
00217 {
00218 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] *= amount;
00219 }
00220
00221 template<typename Type, size_t vector_size>
00222 mxExpandableVector<Type, vector_size> mxExpandableVector<Type,vector_size>::scaleNewVector(Type amount)
00223 {
00224 mxExpandableVector<Type, vector_size> v = *this;
00225 for(unsigned int i = 0; i < VECTOR_SIZE; i++) v.vector_data.data[i] *= amount;
00226 return v;
00227
00228 }
00229
00230
00231 template<typename Type, size_t vector_size>
00232 float mxExpandableVector<Type, vector_size>::DotProduct(const mxExpandableVector<Type, vector_size> &value)
00233 {
00234
00235 float values = 0;
00236
00237 for(unsigned int i = 0; i < VECTOR_SIZE; i++)
00238 {
00239 float temp = (float) vector_data.data[i]*value.vector_data[i];
00240 values += temp;
00241 }
00242
00243 return values;
00244 }
00245
00246
00247 template<typename Type, size_t vector_size>
00248 mxExpandableVector<Type, vector_size> &mxExpandableVector<Type, vector_size>::operator+=(const mxExpandableVector<Type, vector_size> &v)
00249 {
00250
00251 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] += v.vector_data[i];
00252 return *this;
00253
00254 }
00255
00256
00257 template<typename Type, size_t vector_size>
00258 mxExpandableVector<Type, vector_size> &mxExpandableVector<Type, vector_size>::operator+(const mxExpandableVector<Type, vector_size> &v)
00259 {
00260
00261 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] += v.vector_data[i];
00262 return *this;
00263
00264 }
00265
00266 template<typename Type, size_t vector_size>
00267 mxExpandableVector<Type, vector_size> &mxExpandableVector<Type, vector_size>::operator-=(const mxExpandableVector<Type, vector_size> &v)
00268 {
00269
00270
00271 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] -= v.vector_data[i];
00272 return *this;
00273
00274
00275 }
00276
00277 template<typename Type, size_t vector_size>
00278 mxExpandableVector<Type, vector_size> &mxExpandableVector<Type, vector_size>::operator-(const mxExpandableVector<Type, vector_size> &v)
00279 {
00280
00281 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] -= v.vector_data[i];
00282 return *this;
00283
00284 }
00285
00286 template<typename Type, size_t vector_size>
00287 mxExpandableVector<Type, vector_size> &mxExpandableVector<Type, vector_size>::operator*=(const mxExpandableVector<Type, vector_size> &v)
00288 {
00289
00290 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] *= v.vector_data[i];
00291 return *this;
00292
00293
00294 }
00295
00296 template<typename Type, size_t vector_size>
00297 mxExpandableVector<Type, vector_size> &mxExpandableVector<Type, vector_size>::operator*(const mxExpandableVector<Type, vector_size> &v)
00298 {
00299
00300
00301 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] *= v.vector_data[i];
00302 return *this;
00303
00304
00305 }
00306
00307 template<typename Type, size_t vector_size>
00308 mxExpandableVector<Type, vector_size> &mxExpandableVector<Type, vector_size>::operator/(const mxExpandableVector<Type, vector_size> &v)
00309 {
00310
00311
00312 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] /= v.vector_data[i];
00313 return *this;
00314
00315 }
00316
00317
00318 template<typename Type, size_t vector_size>
00319 mxExpandableVector<Type, vector_size> &mxExpandableVector<Type, vector_size>::operator/=(const mxExpandableVector<Type, vector_size> &v)
00320 {
00321
00322 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] /= v.vector_data[i];
00323 return *this;
00324
00325 }
00326
00327 template<typename Type, size_t vector_size>
00328 float mxExpandableVector<Type, vector_size>::Length()
00329 {
00330
00331 float total = 0;
00332
00333 for(unsigned int i = 0; i < VECTOR_SIZE; i++)
00334 {
00335
00336 float value = (float) vector_data.data[i]*vector_data.data[i];
00337 total += value;
00338 }
00339
00340 return sqrtf(total);
00341 }
00342
00343 template<typename Type, size_t vector_size>
00344 void mxExpandableVector<Type, vector_size>::Normalize()
00345 {
00346
00347 float length = Length();
00348 if ( length < float(1E-5))
00349 return;
00350 float inv = 1/length;
00351
00352 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] *= inv;
00353 }
00354
00355
00356 template<typename Type, size_t vector_size>
00357 float mxExpandableVector<Type, vector_size>::Cos(const mxExpandableVector<Type, vector_size> &v)
00358 {
00359
00360 float dot = DotProduct(v);
00361 return (dot / Length() * v.Length());
00362
00363 }
00364
00365
00366
00367
00368
00369 template<typename Type, size_t vector_size>
00370 Type &mxExpandableVector<Type, vector_size>::operator[](unsigned int index)
00371 {
00372 if(!(index < VECTOR_SIZE))
00373 throw mx::mxException<std::string>(" Error out of vector bounds \n ");
00374
00375 return vector_data.data[index];
00376
00377 }
00378
00379 template<typename Type, size_t vector_size>
00380 void mxExpandableVector<Type, vector_size>::Build(const mxExpandableVector<Type, vector_size> &v)
00381 {
00382 for(unsigned int i = 0; i < VECTOR_SIZE; i++) vector_data.data[i] = v.vector_data[i] - vector_data[i];
00383
00384 }
00385
00386
00387 template<typename Type, size_t vector_size> mxExpandableVector<Type, vector_size>
00388 mxExpandableVector<Type, vector_size>::Build(mxExpandableVector<Type, vector_size> &v1, mxExpandableVector<Type, vector_size> v2)
00389 {
00390 mxExpandableVector<Type,vector_size> temp;
00391
00392 for(unsigned int i = 0; i < VECTOR_SIZE; i++) temp.vector_data[i] = v2.vector_data[i] - v1.vector_data[i];
00393
00394 return temp;
00395 }
00396
00397 typedef vector4D<float, 4> mxfVector;
00398 typedef vector4D<int, 4> mxiVector;
00399 typedef vector2D<int, 2> mxiPoint;
00400 typedef vector2D<int, 2> mxfPoint;
00401 }
00402
00403
00404
00405
00406 #endif
00407
00408