AndroMeta  2.0.0
AndroMeta/include/AndroMeta/MPLVector.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2009-2012 AndroMeta LLC. All rights reserved.
00003  *
00004  * AndroMeta LLC retains all intellectual property and proprietary 
00005  * rights to this software and related documentation and any modifications 
00006  * thereto.  Any use, reproduction, disclosure, or distribution of this 
00007  * software and related documentation without an express license agreement 
00008  * from AndroMeta LLC is strictly prohibited.
00009  * 
00010  */
00011 
00019 #ifndef ANDROMETA_MPL_VECTOR_H
00020 #define ANDROMETA_MPL_VECTOR_H
00021 
00022 #include <AndroMeta/MVar.h>
00023 
00024 #include <cmath>
00025 
00026 namespace Meta{
00027 
00033   template<typename T, size_t N>
00034   class MPLVector{
00035   public:
00036 
00040     MPLVector(){
00041       
00042     }
00043    
00049     MPLVector(const mvec& v){
00050       if(v.size() != N){
00051         throw MError("MPLVector::MPLVector: size mismatch");
00052       }
00053       
00054       for(size_t i = 0; i < N; ++i){
00055         vc_[i] = v[i];
00056       }
00057     }
00058 
00064     MPLVector(const mvar& v){
00065       if(v.size() != N){
00066         throw MError("MPLVector::MPLVector: size mismatch");
00067       }
00068       
00069       for(size_t i = 0; i < N; ++i){
00070         vc_[i] = v[i];
00071       }
00072     }
00073     
00079     MPLVector<T,N>& operator=(const mvec& v){
00080       if(v.size() != size()){
00081         throw MError("MPLVector::operator=: size mismatch");
00082       }
00083       
00084       for(size_t i = 0; i < size(); ++i){
00085         vc_[i] = v[i];
00086       }
00087       
00088       return *this;
00089     }
00090 
00096     MPLVector<T,N>& operator=(const mvar& v){
00097       if(v.size() != size()){
00098         throw MError("MPLVector::operator=: size mismatch");
00099       }
00100       
00101       for(size_t i = 0; i < size(); ++i){
00102         vc_[i] = v[i];
00103       }
00104       
00105       return *this;
00106     }
00107     
00108     MPLVector<T,N>& operator=(T x){
00109       for(size_t i = 0; i < size(); ++i){
00110         vc_[i] = x;
00111       }
00112       
00113       return *this;
00114     }
00115     
00119     static size_t size(){
00120       return N;
00121     }
00122 
00123     T& operator[](size_t i){
00124       return vc_[i];
00125     }
00126 
00127     const T& operator[](size_t i) const{
00128       return vc_[i];
00129     }
00130     
00131     MPLVector<T,N>& operator+=(const MPLVector<T,N>& v){
00132       for(size_t i = 0; i < N; ++i){
00133         vc_[i] += v.vc_[i];
00134       }
00135       
00136       return *this;
00137     }
00138 
00139     MPLVector<T,N>& operator+=(T x){
00140       for(size_t i = 0; i < N; ++i){
00141         vc_[i] += x;
00142       }
00143       
00144       return *this;
00145     }
00146     
00147     MPLVector<T,N>& operator-=(const MPLVector<T,N>& v){
00148       for(size_t i = 0; i < N; ++i){
00149         vc_[i] -= v.vc_[i];
00150       }
00151       
00152       return *this;
00153     }
00154 
00155     MPLVector<T,N>& operator-=(T x){
00156       for(size_t i = 0; i < N; ++i){
00157         vc_[i] -= x;
00158       }
00159       
00160       return *this;
00161     }
00162     
00163     MPLVector<T,N>& operator*=(const MPLVector<T,N>& v){
00164       for(size_t i = 0; i < N; ++i){
00165         vc_[i] *= v.vc_[i];
00166       }
00167       
00168       return *this;
00169     }
00170     
00171     MPLVector<T,N>& operator*=(T x){
00172       for(size_t i = 0; i < N; ++i){
00173         vc_[i] *= x;
00174       }
00175       
00176       return *this;
00177     }
00178     
00179     MPLVector<T,N>& operator/=(const MPLVector<T,N>& v){
00180       for(size_t i = 0; i < N; ++i){
00181         vc_[i] /= v.vc_[i];
00182       }
00183       
00184       return *this;
00185     }
00186     
00187     MPLVector<T,N>& operator/=(T x){
00188       for(size_t i = 0; i < N; ++i){
00189         vc_[i] /= x;
00190       }
00191       
00192       return *this;
00193     }
00194 
00198     void normalize(){
00199       T d = 0;
00200       
00201       for(size_t i = 0; i < N; ++i){
00202         d += vc_[i] * vc_[i];
00203       }
00204       
00205       d = sqrt(d);
00206       
00207       for(size_t i = 0; i < N; ++i){
00208         vc_[i] /= d;
00209       }
00210     }
00211     
00212   private:
00213     T vc_[N] __attribute__ ((aligned (16)));
00214   };
00215 
00219   template<typename T, size_t N>
00220   std::ostream& operator<<(std::ostream& ostr, const MPLVector<T,N>& v){
00221     ostr << "[";
00222     for(size_t i = 0; i < N; ++i){
00223       if(i > 0){
00224         ostr << ","; 
00225       }
00226       ostr << v[i];
00227     }
00228     ostr << "]";
00229     return ostr;
00230   }
00231   
00232   namespace MFunc{
00233     
00238     template<typename T, size_t N>
00239     MPLVector<T,N> crossProduct(const MPLVector<T,N>& u, const MPLVector<T,N>& v){
00240       MPLVector<T,N> r;
00241       
00242       r[0] = u[1]*v[2] - u[2]*v[1];
00243       r[1] = u[2]*v[0] - u[0]*v[2];
00244       r[2] = u[0]*v[1] - u[1]*v[0];
00245       
00246       return r;
00247     }
00248     
00252     template<typename T, size_t N>
00253     T dotProduct(const MPLVector<T,N>& u, const MPLVector<T,N>& v){
00254       T r = 0;
00255       
00256       for(size_t i = 0; i < N; ++i){
00257         r += u[i]*v[i];
00258       }
00259       
00260       return r;
00261     }
00262     
00267     template<typename T, size_t N>
00268     T mdistance(const MPLVector<T,N>& u, const MPLVector<T,N>& v){
00269       T d = 0;
00270       for(size_t i = 0; i < N; ++i){
00271         T di = u[i] - v[i];
00272         di *= di;
00273         d += di;
00274       }
00275       return sqrt(d);
00276     }    
00277   } // end namespace MFunc
00278   
00279   namespace MPLVec{
00280 
00286     typedef MPLVector<int32_t,2> int2;
00287     typedef MPLVector<int32_t,3> int3;
00288     typedef MPLVector<int32_t,4> int4;
00289     typedef MPLVector<int32_t,8> int8;
00290     typedef MPLVector<int32_t,16> int16;
00291     
00292     typedef MPLVector<int64_t,2> long2;
00293     typedef MPLVector<int64_t,3> long3;
00294     typedef MPLVector<int64_t,4> long4;
00295     typedef MPLVector<int64_t,8> long8;
00296     typedef MPLVector<int64_t,16> long16;
00297 
00298     typedef MPLVector<float,2> float2;
00299     typedef MPLVector<float,3> float3;
00300     typedef MPLVector<float,4> float4;
00301     typedef MPLVector<float,8> float8;
00302     typedef MPLVector<float,16> float16;
00303 
00304     typedef MPLVector<double,2> double2;
00305     typedef MPLVector<double,3> double3;
00306     typedef MPLVector<double,4> double4;
00307     typedef MPLVector<double,8> double8;
00308     typedef MPLVector<double,16> double16;
00309 
00310   } // end namespace MPLVec
00311 
00312 } // end namespace Meta
00313 
00314 #endif // ANDROMETA_MPL_VECTOR_H