contrib/gel/vmal/vmal_track_lines.cxx
Go to the documentation of this file.
00001 #include "vmal_track_lines.h"
00002 
00003 #include <vnl/vnl_math.h> // for pi
00004 #include <vnl/vnl_double_2.h>
00005 
00006 #include <vtol/vtol_edge_2d.h>
00007 
00008 #include <vcl_cmath.h>
00009 #include <vcl_algorithm.h> // for vcl_min
00010 
00011 #include <vmal/vmal_lines_correlation.h>
00012 #include <vmal/vmal_refine_lines.h>
00013 #include <vmal/vmal_operators.h>
00014 #include <vmal/vmal_convert_vtol.h>
00015 
00016 #define PI vnl_math::pi
00017 
00018 vmal_track_lines::vmal_track_lines()
00019 {
00020 }
00021 
00022 vmal_track_lines::~vmal_track_lines()
00023 {
00024 }
00025 
00026 void vmal_track_lines::track_lines(const vcl_vector<vcl_vector<vtol_edge_2d_sptr>*>* fit_lines,
00027                                    const vcl_vector<vcl_vector<vtol_edge_2d_sptr>*>* transformed_lines,
00028                                    const vcl_vector<vil1_image> &images,
00029                                    const vcl_vector<vnl_double_3x3> &homo,
00030                                    vmal_multi_view_data_edge_sptr matches)
00031 {
00032   theta_=0.0873;//0.0873;
00033   radius_=5.0;
00034   vmal_multi_view_data_edge_sptr tmp_matches;
00035   tmp_matches=new vmal_multi_view_data<vtol_edge_2d_sptr>(matches->get_nb_views());
00036 
00037   if (fit_lines->size()==(transformed_lines->size()+1))
00038   {
00039     //int match_num=0;
00040     unsigned int min_line=0;
00041     double min_dist=-1;
00042     bool match=false;
00043     bool replace=false;
00044     vtol_edge_2d_sptr cur_fl;
00045     vtol_edge_2d_sptr cur_tl;
00046 
00047     for (unsigned int i=0;i<(*transformed_lines)[0]->size();i++)
00048     {
00049       bool found=true;
00050       unsigned int match_line=i;
00051       unsigned int view_num=0;
00052       tmp_matches->new_track();
00053       while ((view_num < transformed_lines->size()) && found)
00054       {
00055         found=false;
00056         cur_tl=(*(*transformed_lines)[view_num])[match_line];
00057         //double tl1x=cur_tl->v1()->cast_to_vertex_2d()->x();
00058         //double tl2x=cur_tl->v2()->cast_to_vertex_2d()->x();
00059         //double tl1y=cur_tl->v1()->cast_to_vertex_2d()->y();
00060         //double tl2y=cur_tl->v2()->cast_to_vertex_2d()->y();
00061         vtol_edge_2d_sptr other_match;
00062         for (unsigned int j=0; j<(*fit_lines)[view_num+1]->size(); ++j)
00063         {
00064           cur_fl=(*(*fit_lines)[view_num+1])[j];
00065           //double fl1x=cur_fl->v1()->cast_to_vertex_2d()->x();
00066           //double fl2x=cur_fl->v2()->cast_to_vertex_2d()->x();
00067           //double fl1y=cur_fl->v1()->cast_to_vertex_2d()->y();
00068           //double fl2y=cur_fl->v2()->cast_to_vertex_2d()->y();
00069 
00070           double angle=seg_angle(cur_tl,cur_fl);
00071           if (angle<theta_)
00072           {
00073             if (belong(cur_tl,cur_fl))
00074             {
00075               double cur_dist;
00076               //int size=(*fit_lines)[view_num]->size();
00077               vtol_edge_2d_sptr pred_fl=(*(*fit_lines)[view_num])[match_line];
00078               cost_function(pred_fl,
00079                             cur_tl,
00080                             cur_fl,
00081                             images[view_num],
00082                             images[view_num+1],
00083                             homo[view_num], cur_dist);
00084               if (min_dist==-1)//Initial value
00085                 min_dist=cur_dist;
00086               if ((cur_dist<=min_dist) && (cur_dist!=-1))
00087               {
00088                 //look if this line have already been matched.
00089                 //If so, perform a test between those two lines
00090                 //to see which one is the best. It can also decide
00091                 //that they both suit.
00092                 if (tmp_matches->get_pred_match(view_num,cur_fl,other_match))
00093                 {
00094                   vtol_edge_2d_sptr t_other_match=find_transfo(other_match,*(*fit_lines)[view_num],*(*transformed_lines)[view_num]);
00095                   double other_dist;
00096                   cost_function(other_match,
00097                                 t_other_match,
00098                                 cur_fl,
00099                                 images[view_num],
00100                                 images[view_num+1],
00101                                 homo[view_num], other_dist);
00102                   //double ol1x=t_other_match->v1()->cast_to_vertex_2d()->x();
00103                   //double ol2x=t_other_match->v2()->cast_to_vertex_2d()->x();
00104                   //double ol1y=t_other_match->v1()->cast_to_vertex_2d()->y();
00105                   //double ol2y=t_other_match->v2()->cast_to_vertex_2d()->y();
00106 
00107                   //int choice=is_cur_best(cur_tl,cur_fl,t_other_match);
00108                   int choice;
00109                   if (other_dist<cur_dist)
00110                       choice=-1;
00111                     else
00112                       choice=1;
00113                   if (choice==1) //the new is better
00114                   {
00115                     found=true;
00116                     min_dist=cur_dist;
00117                     min_line=j;
00118                     replace=true;
00119                   }
00120                   else if (choice==0) //they both suit
00121                   {
00122                     found=true;
00123                     min_dist=cur_dist;
00124                     min_line=j;
00125                     replace=false;
00126                   }
00127                   else if (choice==-1) //the old match is the best
00128                   {
00129                     found=false;
00130                     min_dist=cur_dist;
00131                     min_line=j;
00132                     replace=false;
00133                   }
00134                 }
00135                 else
00136                 {
00137                   found=true;
00138                   min_dist=cur_dist;
00139                   min_line=j;
00140                   replace=false;
00141                 }
00142               }
00143             }
00144           }
00145         }
00146         if (found)
00147         {
00148           if (replace)
00149           {
00150             tmp_matches->remove(view_num+1, (*(*fit_lines)[view_num+1])[min_line]);
00151             replace=false;
00152           }
00153           tmp_matches->set(view_num,(*(*fit_lines)[view_num])[match_line]);
00154           match=true;
00155           view_num++;
00156         }
00157         match_line=min_line;
00158         min_dist=-1 ;
00159       }
00160       if (match)
00161       {
00162         vtol_edge_2d_sptr p=(*(*fit_lines)[view_num])[match_line];
00163         tmp_matches->set(view_num,p);
00164         match=false;
00165         tmp_matches->close_track();
00166       }
00167     }
00168   }
00169   sort_lines(tmp_matches,matches);
00170   matches->print(vcl_cerr);
00171 }
00172 
00173 double vmal_track_lines::seg_angle(vtol_edge_2d_sptr trans_line,vtol_edge_2d_sptr fit_line)
00174 {
00175   double vect_tlx=(trans_line->v2()->cast_to_vertex_2d()->x())-(trans_line->v1()->cast_to_vertex_2d()->x());
00176   double vect_tly=(trans_line->v2()->cast_to_vertex_2d()->y())-(trans_line->v1()->cast_to_vertex_2d()->y());
00177 
00178   double vect_flx=(fit_line->v2()->cast_to_vertex_2d()->x())-(fit_line->v1()->cast_to_vertex_2d()->x());
00179   double vect_fly=(fit_line->v2()->cast_to_vertex_2d()->y())-(fit_line->v1()->cast_to_vertex_2d()->y());
00180 
00181   vnl_double_2 vect_tl(vect_tlx,vect_tly);
00182 
00183   vnl_double_2 vect_fl(vect_flx,vect_fly);
00184 
00185   double alpha=angle(vect_tl,vect_fl);
00186 
00187   return alpha;
00188 }
00189 
00190 bool vmal_track_lines::belong(vtol_edge_2d_sptr trans_line,vtol_edge_2d_sptr fit_line)
00191 {
00192   double tl1x=trans_line->v1()->cast_to_vertex_2d()->x();
00193   double tl2x=trans_line->v2()->cast_to_vertex_2d()->x();
00194   double vect_tlx=tl2x-tl1x;
00195 
00196   double tl1y=trans_line->v1()->cast_to_vertex_2d()->y();
00197   double tl2y=trans_line->v2()->cast_to_vertex_2d()->y();
00198   double vect_tly=tl2y-tl1y;
00199 
00200   vnl_double_2 vect_tl(vect_tlx,vect_tly);
00201   vnl_double_2 norma=vect_tl.normalize();
00202 
00203   double fl1x=fit_line->v1()->cast_to_vertex_2d()->x();
00204   double fl2x=fit_line->v2()->cast_to_vertex_2d()->x();
00205 
00206   double fl1y=fit_line->v1()->cast_to_vertex_2d()->y();
00207   double fl2y=fit_line->v2()->cast_to_vertex_2d()->y();
00208 
00209   //defined the first bounding segment
00210   double bound1_tl1x=tl1x+(-norma[1]*radius_);
00211   double bound1_tl1y=tl1y+(norma[0]*radius_);
00212   double bound1_tl2x=tl2x+(-norma[1]*radius_);
00213   double bound1_tl2y=tl2y+(norma[0]*radius_);
00214 
00215   //defined the second bounding segment
00216   double bound2_tl1x=tl1x+(norma[1]*radius_);
00217   double bound2_tl1y=tl1y+(-norma[0]*radius_);
00218   double bound2_tl2x=tl2x+(norma[1]*radius_);
00219   double bound2_tl2y=tl2y+(-norma[0]*radius_);
00220 
00221 
00222   if (vmal_operators::cross_seg(bound1_tl1x, bound1_tl1y, bound2_tl1x, bound2_tl1y,
00223                                 fl1x, fl1y, fl2x, fl2y))
00224 
00225     return true;
00226   else if (vmal_operators::cross_seg(bound1_tl2x, bound1_tl2y, bound2_tl2x,bound2_tl2y,
00227                                      fl1x, fl1y,fl2x, fl2y))
00228     return true;
00229   else //test if at least one of the extremities of the fit line
00230      //is inside the bounding rectangle.
00231   {
00232     double x1,y1,x2,y2;
00233     vmal_operators::project_point(fl1x,fl1y,bound1_tl1x,bound1_tl1y,bound1_tl2x,bound1_tl2y,&x1,&y1);
00234     vmal_operators::project_point(fl1x,fl1y,bound2_tl1x,bound2_tl1y,bound2_tl2x,bound2_tl2y,&x2,&y2);
00235     if ((x1!=-1)&&(x2!=-1))
00236     {
00237       if (((x1-fl1x)*(x2-fl1x)+(y1-fl1y)*(y2-fl1y))<0)
00238         return true;
00239     }
00240     vmal_operators::project_point(fl2x,fl2y,bound1_tl1x,bound1_tl1y,bound1_tl2x,bound1_tl2y,&x1,&y1);
00241     vmal_operators::project_point(fl2x,fl2y,bound2_tl1x,bound2_tl1y,bound2_tl2x,bound2_tl2y,&x2,&y2);
00242     if ((x1!=-1)&&(x2!=-1))
00243     {
00244       if (((x1-fl2x)*(x2-fl2x)+(y1-fl2y)*(y2-fl2y))<0)
00245         return true;
00246     }
00247   }
00248   return false;
00249 }
00250 
00251 double vmal_track_lines::dist(vtol_edge_2d_sptr trans_line,vtol_edge_2d_sptr fit_line)
00252 {
00253   double tl1x=trans_line->v1()->cast_to_vertex_2d()->x();
00254   double tl2x=trans_line->v2()->cast_to_vertex_2d()->x();
00255   double tl1y=trans_line->v1()->cast_to_vertex_2d()->y();
00256   double tl2y=trans_line->v2()->cast_to_vertex_2d()->y();
00257   double vect_tlx=tl2x-tl1x;
00258   double vect_tly=tl2y-tl1y;
00259   vnl_double_2 vect_tl(vect_tlx,vect_tly);
00260   vect_tl=vect_tl.normalize();
00261 
00262   double fl1x=fit_line->v1()->cast_to_vertex_2d()->x();
00263   double fl2x=fit_line->v2()->cast_to_vertex_2d()->x();
00264   double fl1y=fit_line->v1()->cast_to_vertex_2d()->y();
00265   double fl2y=fit_line->v2()->cast_to_vertex_2d()->y();
00266   double vect_flx=fl2x-fl1x;
00267   double vect_fly=fl2y-fl1y;
00268   vnl_double_2 vect_fl(vect_flx,vect_fly);
00269   vect_fl=vect_fl.normalize();
00270 
00271   double dist;
00272   double distover;
00273 
00274   double x1,y1;
00275   double x2,y2;
00276   double x3,y3;
00277   double x4,y4;
00278 
00279   double dist1=vmal_operators::project_point(fl1x,fl1y,tl1x,tl1y,tl2x,tl2y,&x1,&y1);
00280   double dist2=vmal_operators::project_point(fl2x,fl2y,tl1x,tl1y,tl2x,tl2y,&x2,&y2);
00281   double dist3=vmal_operators::project_point(tl1x,tl1y,fl1x,fl1y,fl2x,fl2y,&x3,&y3);
00282   double dist4=vmal_operators::project_point(tl2x,tl2y,fl1x,fl1y,fl2x,fl2y,&x4,&y4);
00283 
00284   if (dist1==-1)
00285     return -1;
00286 
00287   if (x1!=-1 && x2!=-1) // the first segment totally project on the second
00288   {
00289     if (x3!=-1)
00290     {
00291       dist=dist3+vcl_min(dist1,dist2);
00292       if (dist1<dist2)
00293       distover=vcl_sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))+
00294              vcl_sqrt((x3-fl1x)*(x3-fl1x)+(y3-fl1y)*(y3-fl1y));
00295       else
00296       distover=vcl_sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))+
00297              vcl_sqrt((x3-fl2x)*(x3-fl2x)+(y3-fl2y)*(y3-fl2y));
00298     }
00299     else if (x4!=-1)
00300     {
00301       dist=dist4+vcl_min(dist1,dist2);
00302       if (dist1<dist2)
00303       distover=vcl_sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))+
00304              vcl_sqrt((x4-fl1x)*(x4-fl1x)+(y4-fl1y)*(y4-fl1y));
00305       else
00306       distover=vcl_sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))+
00307              vcl_sqrt((x4-fl2x)*(x4-fl2x)+(y4-fl2y)*(y4-fl2y));
00308     }
00309     else
00310     {
00311       dist=dist1+dist2;
00312       distover=vcl_sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))+
00313              vcl_sqrt((fl1x-fl2x)*(fl1x-fl2x)+(fl1y-fl2y)*(fl1y-fl2y));
00314     }
00315   }
00316   else if (x1!=-1) //case 2
00317   {
00318     dist=dist1;
00319 
00320     if ((x3!=-1)&&(x4!=-1))
00321     {
00322       dist+=vcl_min(dist3,dist4);
00323       if (dist3<dist4)
00324       distover=vcl_sqrt((x3-fl1x)*(x3-fl1x)+(y3-fl1y)*(y3-fl1y))+
00325              vcl_sqrt((x1-tl1x)*(x1-tl1x)+(y1-tl1y)*(y1-tl1y));
00326       else
00327       distover=vcl_sqrt((x4-fl1x)*(x4-fl1x)+(y4-fl1y)*(y4-fl1y))+
00328              vcl_sqrt((x1-tl2x)*(x1-tl2x)+(y1-tl2y)*(y1-tl2y));
00329     }
00330     else if (x3!=-1)
00331     {
00332       dist+=dist3;
00333       distover=vcl_sqrt((x3-fl1x)*(x3-fl1x)+(y3-fl1y)*(y3-fl1y))+
00334              vcl_sqrt((x1-tl1x)*(x1-tl1x)+(y1-tl1y)*(y1-tl1y));
00335     }
00336     else if (x4!=-1)
00337     {
00338       dist+=dist4;
00339       distover=vcl_sqrt((x4-fl1x)*(x4-fl1x)+(y4-fl1y)*(y4-fl1y))+
00340              vcl_sqrt((x1-tl2x)*(x1-tl2x)+(y1-tl2y)*(y1-tl2y));
00341     }
00342     else
00343     {
00344     // dist=2*dist;
00345     // distover=vcl_min(vcl_sqrt((x1-tl2x)*(x1-tl2x)+(y1-tl2y)*(y1-tl2y)),
00346           // vcl_sqrt((x1-tl1x)*(x1-tl1x)+(y1-tl1y)*(y1-tl1y)));
00347       dist=-1;
00348       distover=1;
00349     }
00350   }
00351   else if (x2!=-1)
00352   {
00353     dist=dist2;
00354 
00355     if ((x3!=-1)&&(x4!=-1))
00356     {
00357       dist+=vcl_min(dist3,dist4);
00358       if (dist3<dist4)
00359       distover=vcl_sqrt((x3-fl2x)*(x3-fl2x)+(y3-fl2y)*(y3-fl2y))+
00360              vcl_sqrt((x2-tl1x)*(x2-tl1x)+(y2-tl1y)*(y2-tl1y));
00361       else
00362       distover=vcl_sqrt((x4-fl2x)*(x4-fl2x)+(y4-fl2y)*(y4-fl2y))+
00363              vcl_sqrt((x2-tl2x)*(x2-tl2x)+(y2-tl2y)*(y2-tl2y));
00364     }
00365     else if (x3!=-1)
00366     {
00367       dist+=dist3;
00368       distover=vcl_sqrt((x3-fl2x)*(x3-fl2x)+(y3-fl2y)*(y3-fl2y))+
00369              vcl_sqrt((x2-tl1x)*(x2-tl1x)+(y2-tl1y)*(y2-tl1y));
00370     }
00371     else if (x4!=-1)
00372     {
00373       dist+=dist4;
00374       distover=vcl_sqrt((x4-fl2x)*(x4-fl2x)+(y4-fl2y)*(y4-fl2y))+
00375              vcl_sqrt((x2-tl2x)*(x2-tl2x)+(y2-tl2y)*(y2-tl2y));
00376     }
00377     else
00378     {
00379       dist=-1;
00380       distover=1;
00381     }
00382   }
00383   else
00384   {
00385     dist=dist3+dist4;
00386     distover=vcl_sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4))+
00387          vcl_sqrt((tl1x-tl2x)*(tl1x-tl2x)+(tl1y-tl2y)*(tl1y-tl2y));
00388   }
00389 
00390 #if 0
00391   double length=vcl_sqrt((tl1x-tl2x)*(tl1x-tl2x)+(tl1y-tl2y)*(tl1y-tl2y))
00392               + vcl_sqrt((fl1x-fl2x)*(fl1x-fl2x)+(fl1y-fl2y)*(fl1y-fl2y));
00393 #endif
00394 
00395   return dist/distover;
00396 }
00397 
00398 int vmal_track_lines::is_cur_best(vtol_edge_2d_sptr trans_line,vtol_edge_2d_sptr fit_line,vtol_edge_2d_sptr other_line)
00399 {
00400   double tl1x=trans_line->v1()->cast_to_vertex_2d()->x();
00401   double tl2x=trans_line->v2()->cast_to_vertex_2d()->x();
00402   double tl1y=trans_line->v1()->cast_to_vertex_2d()->y();
00403   double tl2y=trans_line->v2()->cast_to_vertex_2d()->y();
00404 #if 0
00405   double fl1x=fit_line->v1()->cast_to_vertex_2d()->x();
00406   double fl2x=fit_line->v2()->cast_to_vertex_2d()->x();
00407   double fl1y=fit_line->v1()->cast_to_vertex_2d()->y();
00408   double fl2y=fit_line->v2()->cast_to_vertex_2d()->y();
00409 #endif
00410   double ol1x=other_line->v1()->cast_to_vertex_2d()->x();
00411   double ol2x=other_line->v2()->cast_to_vertex_2d()->x();
00412   double ol1y=other_line->v1()->cast_to_vertex_2d()->y();
00413   double ol2y=other_line->v2()->cast_to_vertex_2d()->y();
00414 
00415   double x1,y1;
00416   double x2,y2;
00417   double x3,y3;
00418   double x4,y4;
00419 
00420   vmal_operators::project_point(tl1x,tl1y,ol1x,ol1y,ol2x,ol2y,&x1,&y1);
00421   vmal_operators::project_point(tl2x,tl2y,ol1x,ol1y,ol2x,ol2y,&x2,&y2);
00422   vmal_operators::project_point(ol1x,ol1y,tl1x,tl1y,tl2x,tl2y,&x3,&y3);
00423   vmal_operators::project_point(ol2x,ol2y,tl1x,tl1y,tl2x,tl2y,&x4,&y4);
00424 
00425 #if 0
00426   if ((x1==-1)&&(x2==-1)&&(x3==-1)&&(x4==-1))
00427     return 0;
00428   else
00429 #endif
00430   {
00431     double dist1=dist(trans_line,fit_line);
00432     double dist2=dist(other_line,fit_line);
00433     if (dist1<dist2)
00434       return 1;
00435     else
00436       return -1;
00437   }
00438 }
00439 
00440 vtol_edge_2d_sptr vmal_track_lines::find_transfo(vtol_edge_2d_sptr line,
00441                                                  vcl_vector<vtol_edge_2d_sptr>& fit_lines,
00442                                                  const vcl_vector<vtol_edge_2d_sptr>& transformed_lines
00443                                                 )
00444 {
00445   vcl_vector<vtol_edge_2d_sptr>::iterator iter;
00446   int i=0;
00447   for (iter=fit_lines.begin(); iter!=fit_lines.end(); ++iter)
00448   {
00449     if (*(*iter)==*line)
00450       return transformed_lines[i];
00451     i++;
00452   }
00453   return NULL;
00454 }
00455 
00456 void vmal_track_lines::sort_lines(vmal_multi_view_data_edge_sptr matches,
00457                                   vmal_multi_view_data_edge_sptr sorted_matches)
00458 {
00459   bool still_track;
00460   vcl_map<int,vtol_edge_2d_sptr,vcl_less<int> > track;
00461   still_track=matches->get_first_track(track);
00462   while (still_track)
00463   {
00464     sorted_matches->new_track();
00465     vcl_map<int,vtol_edge_2d_sptr,vcl_less<int> >::iterator iter1;
00466     vcl_map<int,vtol_edge_2d_sptr,vcl_less<int> >::iterator iter2=track.begin();
00467     iter2++;
00468     for (iter1=track.begin(); iter2!=track.end(); ++iter1)
00469     {
00470       int key1=(*iter1).first;
00471       vtol_edge_2d_sptr value0=(*iter1).second;
00472       int key2=(*iter2).first;
00473       vtol_edge_2d_sptr value1=(*iter2).second;
00474 
00475       vtol_edge_2d_sptr out0;
00476       vtol_edge_2d_sptr out1;
00477 
00478       sort_a_pair_of_line(value0, value1, out0, out1);
00479 
00480       sorted_matches->set(key1,out0);
00481       sorted_matches->set(key2,out1);
00482       iter2++;
00483     }
00484     sorted_matches->close_track();
00485     still_track=matches->get_next_track(track);
00486   }
00487 }
00488 
00489 void vmal_track_lines::sort_a_pair_of_line(vtol_edge_2d_sptr line0,
00490                                            vtol_edge_2d_sptr line1,
00491                                            vtol_edge_2d_sptr &new_line0,
00492                                            vtol_edge_2d_sptr &new_line1)
00493 {
00494   double cur_1x=line0->v1()->cast_to_vertex_2d()->x();
00495   double cur_2x=line0->v2()->cast_to_vertex_2d()->x();
00496   double cur_1y=line0->v1()->cast_to_vertex_2d()->y();
00497   double cur_2y=line0->v2()->cast_to_vertex_2d()->y();
00498   vnl_double_2 cur(cur_2x-cur_1x,cur_2y-cur_1y);
00499 
00500   double next_1x=line1->v1()->cast_to_vertex_2d()->x();
00501   double next_2x=line1->v2()->cast_to_vertex_2d()->x();
00502   double next_1y=line1->v1()->cast_to_vertex_2d()->y();
00503   double next_2y=line1->v2()->cast_to_vertex_2d()->y();
00504   vnl_double_2 next(next_2x-next_1x,next_2y-next_1y);
00505 
00506   if (dot_product(cur,next)<0)
00507   {
00508     new_line0=new vtol_edge_2d(cur_1x,cur_1y,cur_2x,cur_2y);
00509     new_line1=new vtol_edge_2d(next_2x,next_2y,next_1x,next_1y);
00510   }
00511   else
00512   {
00513     new_line0=new vtol_edge_2d(cur_1x,cur_1y,cur_2x,cur_2y);
00514     new_line1=new vtol_edge_2d(next_1x,next_1y,next_2x,next_2y);
00515   }
00516 }
00517 
00518 
00519 double vmal_track_lines::lines_correlation(vtol_edge_2d_sptr line0,
00520                                            vtol_edge_2d_sptr line1,
00521                                            const vnl_double_3x3 & H,
00522                                            vil1_memory_image_of<vxl_byte> &image0,
00523                                            vil1_memory_image_of<vxl_byte> &image1)
00524 {
00525   vtol_edge_2d_sptr s_line0;
00526   vtol_edge_2d_sptr s_line1;
00527   // sort the lines so that their end-points match
00528   sort_a_pair_of_line(line0, line1, s_line0, s_line1);
00529 
00530   vnl_double_3 s_line0_p, s_line0_q;
00531   vnl_double_3 s_line1_p, s_line1_q;
00532 
00533   convert_line_double_3(s_line0, s_line0_p, s_line0_q);
00534   convert_line_double_3(s_line1, s_line1_p, s_line1_q);
00535 
00536   vnl_double_3 r_line0_p, r_line0_q;
00537   vnl_double_3 r_line1_p, r_line1_q;
00538 
00539   // refine the lines so that their length are the same
00540   vmal_refine_lines ref;
00541   ref.refine_lines_min_h(s_line0_p, s_line0_q,
00542                          s_line1_p, s_line1_q,
00543                          H,
00544                          r_line0_p, r_line0_q,
00545                          r_line1_p, r_line1_q);
00546 
00547   vmal_lines_correlation correl;
00548   vnl_double_3 trans;
00549   double res;
00550   res=correl.find_min_corr(r_line0_p, r_line0_q,
00551                            r_line1_p, r_line1_q,
00552                            image0, image1,
00553                            trans);
00554   return res;
00555 }
00556 
00557 
00558 void vmal_track_lines::cost_function(vtol_edge_2d_sptr line0,
00559                                      vtol_edge_2d_sptr t_line0,
00560                                      vtol_edge_2d_sptr line1,
00561                                      const vil1_image &image0,
00562                                      const vil1_image &image1,
00563                                      const vnl_double_3x3 homo,
00564                                      double &result)
00565 {
00566   vil1_memory_image_of<vxl_byte> i0;
00567   vil1_memory_image_of<vxl_byte> i1;
00568   convert_grey_memory_image(image0,i0);
00569   convert_grey_memory_image(image1,i1);
00570   result=lines_correlation(line0, line1, homo, i0, i1);
00571 #if 0 // TODO ?
00572   result=dist(t_line0, line1);
00573   double alpha=0.5;
00574   result=result*alpha+(1-alpha);
00575 #endif
00576 }