examples/calibrate3d/TooN/vclasses.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 #ifndef __VCLASSES_HH
00020 #define __VCLASSES_HH
00021 
00022 #include <assert.h>
00023 
00025 //                       //
00026 // Actual Vector classes //
00027 //                       //
00029 
00030 template <int Size>
00031 class Vector : public FixedVector<Size, FixedVAccessor<Size,typename SizeTraits<Size>::get_zone> > {
00032  public:
00033   // default constructor does nothing
00034   inline Vector(){}
00035 
00036   // constructor from c-style array
00037   inline Vector(const double from[Size]){
00038     memcpy(this->my_values,from,Size*sizeof(double));
00039   }
00040 
00041   // constructor from 1-ary operator
00042   template <class T, class Op>
00043     inline Vector(const T& arg, const Operator<Op>&){Op::eval(*this,arg);}
00044 
00045   // constructor from 2-ary operator
00046   template <class LHS, class RHS, class Op>
00047     inline Vector(const LHS& lhs, const RHS& rhs, const Operator<Op>&){Op::eval(*this,lhs,rhs);}
00048 
00049   // constructor from correct sized FixedVector
00050   template<class Accessor>
00051   inline Vector(const FixedVector<Size,Accessor>& from){
00052     FixedVector<Size, FixedVAccessor<Size,typename SizeTraits<Size>::get_zone> >::operator=(from);
00053   }
00054 
00055   // constructor from any DynamicVector
00056   template<class Accessor>
00057     inline Vector(const DynamicVector<Accessor>& from){
00058     assert(from.size() == Size);
00059     FixedVector<Size, FixedVAccessor<Size,typename SizeTraits<Size>::get_zone> >::operator=(from);
00060   }
00061 
00062   template <class Accessor> inline Vector<Size>& operator=(const FixedVector<Size,Accessor>& fv) {
00063     *this = Vector<Size>(fv);
00064     return *this;
00065   }
00066 
00067   template <class Accessor> inline Vector<Size>& operator=(const DynamicVector<Accessor>& dv) {
00068     *this = Vector<Size>(dv);
00069     return *this;
00070   }
00071 
00072   // vector magic assignment operators (uses comma style) - insertion style defined in vbase.hh
00073   VectorMagic::VectorFiller<1,Size, Vector<Size>, VectorMagic::CommaStyle> operator=(double t) {
00074     (*this)[0] = t;
00075     return VectorMagic::VectorFiller<1,Size, Vector<Size>, VectorMagic::CommaStyle>(*this);
00076   }
00077 
00078   template <int N> VectorMagic::VectorFiller<N,Size, Vector<Size>,VectorMagic::CommaStyle> operator=(const VectorMagic::ComponentPlaceHolder<N>& t) {
00079     return VectorMagic::VectorFiller<N,Size, Vector<Size>,VectorMagic::CommaStyle>(*this);
00080   }
00081 
00082 };
00083 
00084 
00085 
00086 
00087 
00088 template <>
00089 class Vector<> : public DynamicVector<DynamicVAccessor> {
00090  public:
00091   Vector(){
00092         this->my_size = 0;
00093         this->my_values = NULL;
00094   }
00095 
00096   inline void resize(int new_size)
00097   {
00098         if(this->my_size != new_size)
00099         {
00100                 delete[] this->my_values;
00101                 this->my_size = new_size;
00102                 this->my_values = new double[this->my_size];
00103         }
00104   }
00105 
00106     template <class Accessor> inline void assign(const VectorBase<Accessor>& v) {
00107         resize(v.size());
00108         DynamicVector<DynamicVAccessor>::operator=(v);
00109     }
00110   Vector(int Size) {
00111     this->my_size=Size; this->my_values = new double[Size];
00112   }
00113 
00114   inline Vector(int Size, double* from){
00115     this->my_size=Size;
00116     this->my_values = new double[Size];
00117     memcpy(this->my_values,from,Size*sizeof(double));
00118   }
00119 
00120   inline ~Vector(){
00121     delete[] this->my_values;
00122   }
00123 
00124   // constructor from 1-ary operator
00125   template <class T, class Op>
00126   Vector(const T& arg, const Operator<Op>&){
00127     Op::eval(*this,arg);
00128   }
00129 
00130   // constructor from 2-ary operator
00131   template <class LHS, class RHS, class Op>
00132   Vector(const LHS& lhs, const RHS& rhs, const Operator<Op>&){
00133     Op::eval(*this,lhs,rhs);
00134   }
00135 
00136   // constructor from any VectorBase
00137   template<class Accessor>
00138   Vector(const VectorBase<Accessor>& from){
00139     this->my_size = from.size();
00140     this->my_values = new double[this->my_size];
00141     DynamicVector<DynamicVAccessor>::operator=(from);
00142   }
00143 
00144   // trap copy constructor here
00145   Vector(const Vector& from){
00146     this->my_size = from.my_size;
00147     this->my_values = new double[this->my_size];
00148     memcpy(this->my_values,from.my_values,this->my_size*sizeof(double));
00149   }
00150 };
00151 
00152 struct VSizer{
00153   static inline void set_size(Vector<>& v, int size){
00154     v.my_size=size;
00155     v.my_values=new double[size];
00156   }
00157 };
00158 
00159 
00160 #endif

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