Vector.hpp

Go to the documentation of this file.
00001 #ifndef LOPER_VECTOR
00002 #define LOPER_VECTOR
00003 
00014 #include <valarray>
00015 #include <Matrix.hpp>
00016 
00017 
00019 template< typename T >
00020 class Vector : public std::valarray< T >
00021 {
00022 public:
00023     typedef Vector< T > OurType;
00024 /*
00025  * Constructors
00026  */
00027     Vector () {}
00028     Vector (int numElements) { resize (numElements); }
00029     Vector (const std::valarray< T > & v);
00030     void operator=(const std::valarray< T > & v);
00031     
00032 /*
00033  * Calculations
00034  */
00035     double getLength()                          const;
00036     T dotProduct (const OurType & v)            const;
00037     OurType crossProduct (const OurType &v2)    const;
00038     Matrix< T > outerProduct (const OurType &v) const;
00039     OurType operator* (const Matrix< T > &m)    const;
00040 
00041 /*
00042  * Mutators
00043  */
00044     void setToZero();
00045     void print(const char *title=NULL) const;
00046     OurType operator-(const OurType &v) const;
00047 
00048 /*
00049  * Accessors
00050  */
00051     T & operator()(int index)      { return this->operator[](index); }
00052     T & at (int index)             { return this->operator[](index); }
00053     T operator() (int index) const { return this->operator[](index); }
00054     T at (int index)         const { return this->operator[](index); }
00055 
00056     bool operator==(const OurType & v) const;
00057     bool operator!=(const OurType & v) const;
00058 };
00059 
00060 
00061 /*******************************************************************
00062  *                        COMMON TYPEDEF'S                         *
00063  *******************************************************************/
00064 
00065 typedef Vector< float  >        FloatVec;
00066 typedef Vector< double >        DoubleVec;
00067 typedef Vector< int    >        IntVec;
00068 typedef Vector< unsigned char > UbyteVec;
00069 
00070 
00071 
00072 /*******************************************************************
00073  *                      METHOD DEFINITIONS                         *
00074  *******************************************************************/
00075 
00076 template< typename T >
00077 Vector< T >::Vector (const std::valarray< T > & v)
00078 {
00079     *this = v;
00080 }
00081 
00082 template< typename T >
00083 void Vector< T >::operator=(const std::valarray< T > & v)
00084 {
00085     resize(v.size());
00086     for (int i = 0; i < size(); i++)
00087         this->at(i) = v[i];
00088 }
00089 
00090 
00091 template< typename T >
00092 double Vector< T >::getLength() const 
00093 { 
00094     return sqrt (dotProduct(*this)); 
00095 }
00096 
00097 
00098 
00099 template< typename T >
00100 T Vector< T >::dotProduct (const OurType & v) const
00101 {
00102     if (v.size() != size())
00103         throw exception ("Vector::dotProduct(): size mismatch");
00104 
00105     OurType a = *this;
00106     a *= v;
00107     return a.sum();
00108 }
00109 
00110 
00111 
00112 template< typename T >
00113 Vector< T > Vector< T >::crossProduct (const OurType &v2) const
00114 {
00115     if (this->size() != 3 || v2.size() != 3)
00116         throw exception("Vector::crossProduct(): must have 3 elements.");
00117 
00118     OurType result(3);
00119     const OurType & v1 = *this;
00120     result[0] = v1[1]*v2[2] - v1[2]*v2[1];
00121     result[1] = v1[2]*v2[0] - v1[0]*v2[2];
00122     result[2] = v1[0]*v2[1] - v1[1]*v2[0];  
00123 
00124     return result;
00125 }
00126 
00127 
00128 
00129 template< typename T >
00130 void Vector< T >::print(const char *title) const
00131 {
00132     printf("\nVector %s\n", title?title:"");
00133 
00134     for (int i = 0; i < size(); i++)
00135     {
00136         double d = this->at(i);
00137         printf("%.3lf  ", d);
00138         Sleep(0);
00139     }
00140 
00141     printf("\n\n");
00142 }
00143 
00144 
00145 
00146 template< typename T >
00147 void Vector< T >::setToZero()
00148 {
00149     for (unsigned int i = 0; i < size(); i++)
00150         (*this)[i] = (T)0;
00151 }
00152 
00153 
00154 
00155 template< typename T >
00156 bool Vector< T >::operator!=(const OurType & v) const
00157 {
00158     return (!(v == *this));
00159 }
00160 
00161 
00162 
00163 template< typename T >
00164 bool Vector< T >::operator==(const OurType & v) const
00165 {
00166     if (size() != v.size())
00167         return false;
00168     for (int i = 0; i < size(); i++)
00169         if (this->at(i) != v[i])
00170             return false;
00171     return true;
00172 }
00173 
00174 
00175 
00176 template< typename T >
00177 Vector< T > Vector< T >::operator-(const OurType &v) const
00178 {
00179     OurType result = *this;
00180     result -= v;
00181     return result;
00182 }
00183 
00184 
00185 
00186 template< typename T >
00187 Matrix< T > Vector< T >::outerProduct (const OurType &v) const
00188 {
00189     Matrix< T > result(size(), size());
00190 
00191     for (int destRow = 0; destRow < size(); destRow++)
00192     {
00193         for (int destCol = 0; destCol < size(); destCol++)
00194         {
00195             result(destRow, destCol) = this->at(destRow) * this->at(destCol);
00196         }
00197     }
00198 
00199     return result;
00200 }
00201 
00202 
00203 
00204 template< typename T >
00205 Vector< T > Vector< T >::operator* (const Matrix< T > &m) const
00206 {
00207     Vector< T > result(m.getWidth());
00208     result.setToZero();
00209 
00210     for (int col = 0; col < m.getWidth(); col++)
00211     {
00212         for (int row = 0; row < m.getHeight(); row++)
00213             result(col) += this->at(row) * m(row,col);
00214     }
00215 
00216     return result;
00217 }
00218 
00219 
00220 
00221 #endif // LOPER_VECTOR

Generated on Mon May 26 11:19:29 2003 for CamChecker by doxygen1.3