core/vgl/algo/vgl_fit_lines_2d.h
Go to the documentation of this file.
00001 // This is core/vgl/algo/vgl_fit_lines_2d.h
00002 #ifndef vgl_fit_lines_2d_h_
00003 #define vgl_fit_lines_2d_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Fits a contiguous set of line segments to a sampled curve
00010 // \author J.L. Mundy (reminiscent of Charlie's fit lines)
00011 // \date April 08, 2003
00012 //
00013 //  The parameters are:
00014 //  -  min_length - the smallest number of points to fit with a line seg
00015 //  -  tol - the threshold on mean square distance from points to line seg
00016 //  -  angle - the maximum angle between segments that could be merged
00017 //  A line segment is incrementally fit to the curve until the tolerance
00018 //  is exceeded. The line segment is output and a new line fit is started.
00019 //
00020 // \verbatim
00021 //  Modifications
00022 //   none
00023 // \endverbatim
00024 #include <vcl_vector.h>
00025 #include <vgl/vgl_point_2d.h>
00026 #include <vgl/vgl_line_segment_2d.h>
00027 
00028 template <class T>
00029 class vgl_fit_lines_2d
00030 {
00031   // Data Members--------------------------------------------------------------
00032  protected:
00033   bool verbose_;
00034   vcl_vector<vgl_point_2d<T> > curve_;
00035   vcl_vector<vgl_line_segment_2d<T> > segs_;
00036   vcl_vector<int> curve_indices_;
00037   unsigned int min_length_;
00038   T tol_;
00039  public:
00040 
00041   // Constructors/Initializers/Destructors-------------------------------------
00042 
00043   vgl_fit_lines_2d(unsigned int min_length = 10, T tol = 0.15);
00044 
00045   ~vgl_fit_lines_2d() {}
00046 
00047   // Operations----------------------------------------------------------------
00048   void set_verbose(bool verbose){verbose_ = verbose;}
00049   //: set parameters
00050   void set_min_fit_length(unsigned int min_fit_length){min_length_ = min_fit_length;}
00051   void set_rms_error_tol(T rms_error_tol){tol_ = rms_error_tol;}
00052 
00053   //: add a point to the curve
00054   void add_point(vgl_point_2d<T> const &p);
00055   void add_point(T x, T y);
00056 
00057   //: add an entire curve
00058   void add_curve(vcl_vector<vgl_point_2d<T> > const & curve){curve_=curve;}
00059 
00060   //: clear internal data
00061   void clear();
00062 
00063   //: the fitting method
00064   bool fit();
00065 
00066   // Data Access---------------------------------------------------------------
00067   vcl_vector<vgl_point_2d<T> >& get_points(){return curve_;}
00068   vcl_vector<vgl_line_segment_2d<T> >& get_line_segs(){return segs_;}
00069   //: This vector provides an index mapping each curve point to the line it belongs to
00070   //  An index of -1 indicates the curve point was not used in any line estimate
00071   vcl_vector<int>& get_indices() {return curve_indices_;}
00072  protected:
00073   //:output a line that fits from start to end
00074   void output(unsigned int start_index, unsigned int end_index);
00075 };
00076 
00077 #define VGL_FIT_LINES_2D_INSTANTIATE(T) extern "please include vgl/algo/vgl_fit_lines_2d.txx instead"
00078 
00079 #endif // vgl_fit_lines_2d_h_