contrib/gel/vdgl/vdgl_fit_lines.cxx
Go to the documentation of this file.
00001 // This is gel/vdgl/vdgl_fit_lines.cxx
00002 #include "vdgl_fit_lines.h"
00003 //:
00004 // \file
00005 #include <vsol/vsol_line_2d.h>
00006 #include <vdgl/vdgl_digital_curve.h>
00007 #include <vdgl/vdgl_interpolator.h>
00008 #include <vdgl/vdgl_interpolator_sptr.h>
00009 #include <vdgl/vdgl_edgel_chain.h>
00010 #include <vdgl/vdgl_edgel_chain_sptr.h>
00011 #include <vgl/vgl_point_2d.h>
00012 #include <vgl/vgl_line_segment_2d.h>
00013 
00014 //--------------------------------------------------------------------------
00015 //:
00016 //  Convert each digital curve to a set of vgl_point_2d<double> and add
00017 //  them to vgl linear regression fitter point set. A set of line segments
00018 //  are computed that fit the point set within a specified mean square
00019 //  tolerance.  The resulting vgl_line_segment_2d<double> segments
00020 //  are converted back to vsol geometry.
00021 //
00022 bool vdgl_fit_lines::fit_lines()
00023 {
00024   if (line_segs_.size() > 0) // fit_lines() has already been called
00025     return true;
00026   if (!curves_.size())
00027     return false;
00028   fitter_.set_min_fit_length(min_fit_length_);
00029   fitter_.set_rms_error_tol(rms_distance_);
00030   for (vcl_vector<vdgl_digital_curve_sptr>::iterator eit = curves_.begin();
00031        eit != curves_.end(); eit++)
00032   {
00033     vdgl_digital_curve_sptr dc = (*eit);
00034     vdgl_interpolator_sptr intp = dc->get_interpolator();
00035     vdgl_edgel_chain_sptr ec = intp->get_edgel_chain();
00036     fitter_.clear();
00037     int nedgl = ec->size();
00038     for (int i=0; i<nedgl; i++)
00039     {
00040       vgl_point_2d<double> p((*ec)[i].x(), (*ec)[i].y());
00041       fitter_.add_point(p);
00042     }
00043 
00044     fitter_.fit();
00045     vcl_vector<vgl_line_segment_2d<double> >& segs = fitter_.get_line_segs();
00046     for (vcl_vector<vgl_line_segment_2d<double> >::iterator sit=segs.begin();
00047          sit != segs.end(); sit++)
00048     {
00049       vsol_line_2d_sptr line = new vsol_line_2d(*sit);
00050       line_segs_.push_back(line);
00051     }
00052   }
00053   return true;
00054 }