examples/homography/TooN/vbase.hh

00001 
00002 /*
00003          Copyright (C) 2005 Tom Drummond
00004 
00005      This library is free software; you can redistribute it and/or
00006      modify it under the terms of the GNU Lesser General Public
00007      License as published by the Free Software Foundation; either
00008      version 2.1 of the License, or (at your option) any later version.
00009 
00010      This library is distributed in the hope that it will be useful,
00011      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013      Lesser General Public License for more details.
00014 
00015      You should have received a copy of the GNU Lesser General Public
00016      License along with this library; if not, write to the Free Software
00017      Foundation, Inc.
00018      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019 */
00020 #ifndef __VBASE_HH
00021 #define __VBASE_HH
00022 
00023 // VectorBase //
00024 #include <assert.h>
00025 
00026 template <class Accessor>
00027 struct VectorBase : public Accessor {
00028   const double* get_data_ptr()const{return Accessor::my_values;}
00029   double* get_data_ptr(){return Accessor::my_values;}
00030 
00031 };
00032 
00033 // operator ostream& <<
00034 template <class Accessor>
00035 std::ostream& operator << (std::ostream& os, const VectorBase<Accessor>& v){
00036   for (int i=0; i<v.size(); i++){
00037     os << std::setw(12) << v[i] << "  ";
00038   }
00039   return os;
00040 }
00041 
00042 // operator istream& >>
00043 template <class Accessor>
00044 std::istream& operator >> (std::istream& is, VectorBase<Accessor>& v){
00045   for (int i=0; i<v.size(); i++){
00046     is >>  v[i];
00047   }
00048   return is;
00049 }
00050 
00051 template <class Accessor1, class Accessor2> struct VectorCopy;
00052 
00054 //                                                    //
00055 // Fixed and Dynamic Vector classes                   //
00056 // all the arithmetic and assignment                  //
00057 // operations are applied to these                    //
00058 //                                                    //
00060 
00061 template <int Size, class Accessor>
00062 struct FixedVector : public VectorBase<Accessor> {
00063   // assignment from correct sized FixedVector
00064   template<class Accessor2>
00065   inline FixedVector& operator=(const FixedVector<Size,Accessor2>& from){
00066     VectorCopy<Accessor, Accessor2>::eval(*this,from);
00067     return *this;
00068   }
00069 
00070   // copy assignment
00071   inline FixedVector& operator=(const FixedVector& from){
00072     VectorCopy<Accessor,Accessor>::eval(*this,from);
00073     return *this;
00074   }
00075 
00076   // assignment from any DynamicVector
00077   template<class Accessor2>
00078     inline FixedVector& operator=(const DynamicVector<Accessor2>& from){
00079     assert(from.size() == Size);
00080     VectorCopy<Accessor, Accessor2>::eval(*this, from);
00081     return *this;
00082   }
00083 
00084   // assignment from a double - uses vector magic
00085   VectorMagic::VectorFiller<1,Size, FixedVector<Size, Accessor>, VectorMagic::CommaStyle> operator=(double t) {
00086     (*this)[0] = t;
00087     return VectorMagic::VectorFiller<1,Size, FixedVector<Size, Accessor>, VectorMagic::CommaStyle>(*this);
00088   }
00089 
00090   // insertion operators - uses vector magic
00091   VectorMagic::VectorFiller<1,Size, FixedVector<Size,Accessor>, VectorMagic::InsertionStyle> operator<<(double t) {
00092     (*this)[0] = t;
00093     return VectorMagic::VectorFiller<1,Size, FixedVector<Size,Accessor>, VectorMagic::InsertionStyle>(*this);
00094   }
00095 
00096   template <int N> VectorMagic::VectorFiller<N,Size, FixedVector<Size,Accessor>, VectorMagic::InsertionStyle> operator<<(const VectorMagic::ComponentPlaceHolder<N>& t) {
00097     return VectorMagic::VectorFiller<N,Size, FixedVector<Size,Accessor>, VectorMagic::InsertionStyle>(*this);
00098   }
00099 
00100   template <int N> VectorMagic::VectorFiller<N,Size, FixedVector<Size,Accessor>, VectorMagic::InsertionStyle> operator<<(const Vector<N>& t) {
00101     (*this).template slice<0,N>() = t;
00102     return VectorMagic::VectorFiller<N,Size, FixedVector<Size,Accessor>, VectorMagic::InsertionStyle>(*this);
00103   }
00104 
00105     static void dummy() {}
00106 
00107   template<class A, int I> FixedVector<Size, Accessor>& operator=(const VectorMagic::VectorCreator<A,I>& v)
00108   {
00109     v.assign(*this);
00110     return *this;
00111   }
00112 
00113 };
00114 
00115 template <class Accessor>
00116 struct DynamicVector : public VectorBase<Accessor>{
00117   typedef VectorBase<Accessor> parent;
00118   // assignment from any VectorBase
00119   template<class Accessor2>
00120   DynamicVector& operator=(const VectorBase<Accessor2>& from){
00121     assert(parent::my_size == from.size());
00122     VectorCopy<Accessor,Accessor2>::eval(*this,from);
00123     return *this;
00124   }
00125 
00126   // repeated for explicit copy assignment
00127   DynamicVector& operator=(const DynamicVector& from){
00128     assert(parent::my_size == from.size());
00129     VectorCopy<Accessor,Accessor>::eval(*this,from);
00130     return *this;
00131   }
00132     operator DynamicVector& () { return *this; }
00133     operator const DynamicVector& () const { return *this; }
00134 
00135   template<class A, int I> DynamicVector<Accessor> & operator=(const VectorMagic::VectorCreator<A,I>& v)
00136   {
00137     v.assign(*this);
00138     return *this;
00139   }
00140 
00141 };
00142 
00143 
00144 // Special kinds of DynamicVector only constructed internally
00145 // e.g. from DynamicMAccessor<>::operator[]
00146 
00147 template <class V> struct NonConst : public V {
00148   inline operator const V&() const { return *this; }
00149   inline operator V&() { return *this; }
00150   template <class T> inline NonConst& operator=(const T& t) { V::operator=(t);  return *this; }
00151 };
00152 
00153 #include <TooN/vaccessor.hh>
00154 
00155 
00156 // Data copying //
00157 
00158 template <class Accessor1, class Accessor2>
00159 struct VectorCopy {
00160   inline static void eval(VectorBase<Accessor1>& to, const VectorBase<Accessor2>& from){
00161     for(int i=0; i<from.size(); i++){
00162       to[i]=from[i];
00163     }
00164   }
00165 };
00166 
00167 template <int Size, class Zone1, class Zone2>
00168 struct VectorCopy<FixedVAccessor<Size,Zone1>,FixedVAccessor<Size,Zone2> > {
00169   inline static void eval(VectorBase<FixedVAccessor<Size,Zone1> >& to,
00170                           const VectorBase<FixedVAccessor<Size,Zone2> >& from){
00171     memcpy(to.get_data_ptr(),from.get_data_ptr(),Size*sizeof(double));
00172   }
00173 };
00174 
00175 template<>
00176 struct VectorCopy<DynamicVAccessor,DynamicVAccessor>{
00177   inline static void eval(VectorBase<DynamicVAccessor>& to,
00178                           const VectorBase<DynamicVAccessor>& from){
00179     memcpy(to.get_data_ptr(),from.get_data_ptr(),from.size()*sizeof(double));
00180   }
00181 };
00182 
00183 
00184 #endif

Generated on Fri Feb 22 18:26:55 2008 for QVision by  doxygen 1.5.3