contrib/gel/vmal/vmal_multi_view_data.cxx
Go to the documentation of this file.
00001 // This is gel/vmal/vmal_multi_view_data.cxx
00002 #include "vmal_multi_view_data.h"
00003 
00004 #include <vtol/vtol_vertex_2d.h>
00005 #include <vtol/vtol_vertex_2d_sptr.h>
00006 #include <vtol/vtol_edge_2d.h>
00007 #include <vtol/vtol_edge_2d_sptr.h>
00008 
00009 template <class T>
00010 vmal_multi_view_data<T>::vmal_multi_view_data():
00011 nbviews_(0)
00012 {
00013 }
00014 
00015 template <class T>
00016 vmal_multi_view_data<T>::vmal_multi_view_data(int nbviews):
00017 nbviews_(nbviews),size_vect_ft_(0),matchnum_(-1),closed_track_(true),MVM(0)
00018 {
00019   //all_pts=new vcl_vector<T>();
00020 }
00021 
00022 template <class T>
00023 vmal_multi_view_data<T>::~vmal_multi_view_data()
00024 {
00025 }
00026 
00027 template <class T>
00028 void vmal_multi_view_data<T>::set_params(int nbviews)
00029 {
00030   nbviews_=nbviews;
00031 }
00032 
00033 template <class T>
00034 void vmal_multi_view_data<T>::set(int view_num,int matchnum,T feature)
00035 {
00036   all_pts.push_back(feature);
00037   if (matchnum>matchnum_)
00038   {
00039     matchnum_=matchnum;
00040     NViewMatch temp_match(nbviews_);
00041     temp_match[view_num]=size_vect_ft_;
00042     MVM.push_back(temp_match);
00043   }else {
00044     MVM[matchnum][view_num]=size_vect_ft_;
00045   }
00046   size_vect_ft_++;
00047 }
00048 
00049 template <class T>
00050 void vmal_multi_view_data<T>::set(int view_num,T feature)
00051 {
00052   if ((matchnum_>=0)&& (view_num>=0) && (view_num<nbviews_))
00053   {
00054     all_pts.push_back(feature);
00055     MVM[matchnum_][view_num]=size_vect_ft_;
00056     size_vect_ft_++;
00057   }
00058 }
00059 
00060 template <class T>
00061 void vmal_multi_view_data<T>::new_track()
00062 {
00063   if (closed_track_)
00064   {
00065     matchnum_++;
00066     NViewMatch temp_match(nbviews_);
00067     MVM.push_back(temp_match);
00068     closed_track_=false;
00069   }
00070 }
00071 
00072 template <class T>
00073 void vmal_multi_view_data<T>::close_track()
00074 {
00075     closed_track_=true;
00076 }
00077 
00078 
00079 template <class T>
00080 bool vmal_multi_view_data<T>::get_first_track(vcl_map<int,T,vcl_less<int> > & track)
00081 {
00082   matchnum_=0;
00083   return get_next_track(track);
00084 }
00085 
00086 template <class T>
00087 bool vmal_multi_view_data<T>::get_next_track(vcl_map<int,T,vcl_less<int> > & track)
00088 {
00089   if (int(MVM.size())>matchnum_)
00090   {
00091     track.clear();
00092     for (unsigned int i=0;i <MVM[matchnum_].size();i++)
00093     {
00094       int value=MVM[matchnum_][i];
00095       if (value !=NViewMatch::nomatch)
00096       {
00097         T tmp_edge=all_pts[value];
00098         typedef typename vcl_map<int,T,vcl_less<int> >::value_type value_type;
00099         track.insert(value_type(i,tmp_edge));
00100       }
00101     }
00102     matchnum_++;
00103     return true;
00104   }
00105   else
00106     return false;
00107 }
00108 
00109 //put in point_vector all the points that have been detected and
00110 //matched in at least two views in the view view_num
00111 template <class T>
00112 void vmal_multi_view_data<T>::get(int view_num,vcl_vector<T> &ft_vector)
00113 {
00114   if ((view_num>=0) && (view_num<nbviews_))
00115   {
00116   ft_vector.clear();
00117   NViewMatches::iterator iter;
00118   vcl_cerr << "MVM->size():"<<MVM.size()<<vcl_endl;
00119   for (iter=MVM.begin();iter!=MVM.end();iter++)
00120   {
00121     int ft_num=(*iter)[view_num];
00122     if (ft_num!=NViewMatch::nomatch)
00123     {
00124       T temp_ft=all_pts[ft_num];
00125       ft_vector.push_back(temp_ft);
00126     }
00127   }
00128   }
00129 }
00130 
00131 //Set point_vector1 and point_vector2 to the coordinates of points that
00132 //have been matched between thoses 2 views.
00133 template <class T>
00134 void vmal_multi_view_data<T>::get(int view_num1,int view_num2,
00135                   vcl_vector<T> &ft_vector1,
00136                   vcl_vector<T> &ft_vector2)
00137 {
00138   if ((view_num1>=0) && (view_num1<nbviews_) &&
00139     (view_num2>=0) && (view_num2<nbviews_) && (view_num2!=view_num1))
00140   {
00141   ft_vector1.clear();
00142   ft_vector2.clear();
00143   NViewMatches::iterator iter;
00144   for (iter=MVM.begin();iter!=MVM.end();iter++)
00145   {
00146     int ft_num1=(*iter)[view_num1];
00147     int ft_num2=(*iter)[view_num2];
00148     if ((ft_num1 != NViewMatch::nomatch) && (ft_num2!=NViewMatch::nomatch))
00149     {
00150       T temp_ft1=all_pts[ft_num1];
00151       T temp_ft2=all_pts[ft_num2];
00152       ft_vector1.push_back(temp_ft1);
00153       ft_vector2.push_back(temp_ft2);
00154     }
00155   }
00156   }
00157 }
00158 
00159 template <class T>
00160 void vmal_multi_view_data<T>::get(int view_num1,int view_num2,int view_num3,
00161                   vcl_vector<T> &ft_vector1,
00162                   vcl_vector<T> &ft_vector2,
00163                   vcl_vector<T> &ft_vector3)
00164 {
00165   if ((view_num1>=0) && (view_num1<nbviews_) &&
00166     (view_num2>=0) && (view_num2<nbviews_) &&
00167     (view_num3>=0) && (view_num3<nbviews_) &&
00168     (view_num2!=view_num1) && (view_num2!=view_num3) && (view_num3!=view_num1))
00169   {
00170   ft_vector1.clear();
00171   ft_vector2.clear();
00172   ft_vector3.clear();
00173 
00174   NViewMatches::iterator iter;
00175   for (iter=MVM.begin();iter!=MVM.end();iter++)
00176   {
00177     int ft_num1=(*iter)[view_num1];
00178     int ft_num2=(*iter)[view_num2];
00179     int ft_num3=(*iter)[view_num3];
00180     if (ft_num1 != NViewMatch::nomatch &&
00181         ft_num2 != NViewMatch::nomatch &&
00182         ft_num3 != NViewMatch::nomatch)
00183     {
00184       T temp_ft1=all_pts[ft_num1];
00185       T temp_ft2=all_pts[ft_num2];
00186       T temp_ft3=all_pts[ft_num3];
00187       ft_vector1.push_back(temp_ft1);
00188       ft_vector2.push_back(temp_ft2);
00189       ft_vector3.push_back(temp_ft3);
00190     }
00191   }
00192   }
00193 }
00194 
00195 template <class T>
00196 bool vmal_multi_view_data<T>::get_pred_match(int view_num,T obj,T & res)
00197 {
00198   if ((view_num>=0) && (view_num<nbviews_-1))
00199   {
00200     int i,j;
00201     NViewMatches::iterator iter;
00202     for (iter=MVM.begin();iter!=MVM.end();iter++)
00203     {
00204       i=(*iter)[view_num];
00205       j=(*iter)[view_num+1];
00206       if ((i!=NViewMatch::nomatch) && (j!=NViewMatch::nomatch))
00207         if (*all_pts[j]==*obj)
00208         {
00209           res=all_pts[i];
00210           return true;
00211         }
00212     }
00213   }
00214   return false;
00215 }
00216 
00217 template <class T>
00218 void vmal_multi_view_data<T>::remove(int view_num, T match)
00219 {
00220   if ((view_num>0) && (view_num<nbviews_))
00221   {
00222     int i;
00223     NViewMatches::iterator iter;
00224     for (iter=MVM.begin();iter!=MVM.end();iter++)
00225     {
00226       i=(*iter)[view_num];
00227       if (i!=NViewMatch::nomatch)
00228         if (*all_pts[i]==*match)
00229         {
00230           (*iter)[view_num]= NViewMatch::nomatch;
00231           if (view_num==1)
00232           {
00233             MVM.erase(iter);
00234             matchnum_--;
00235           }
00236         }
00237     }
00238   }
00239 }
00240 
00241 template <class T>
00242 vcl_ostream& vmal_multi_view_data<T>::print(vcl_ostream& str)
00243 {
00244   for (unsigned int j=0;j<MVM.size();j++)
00245   {
00246     for (int i=0;i<nbviews_;i++)
00247       if (MVM[j][i]<0)
00248       str<<"  "<<MVM[j][i];
00249       else if (MVM[j][i]<10)
00250       str<<"   "<<MVM[j][i];
00251       else if (MVM[j][i]<100)
00252       str<<"  "<<MVM[j][i];
00253       else
00254       str<<" "<<MVM[j][i];
00255     str<<vcl_endl;
00256   }
00257   return str;
00258 }
00259 
00260 template class vmal_multi_view_data<vtol_vertex_2d_sptr>;
00261 template class vmal_multi_view_data<vtol_edge_2d_sptr>;