1 // This is core/vgl/algo/vgl_fit_lines_2d.h
2 #ifndef vgl_fit_lines_2d_h_
3 #define vgl_fit_lines_2d_h_
4 //:
5 // \file
6 // \brief Fits a contiguous set of line segments to a sampled curve
7 // \author J.L. Mundy (reminiscent of Charlie's fit lines)
8 // \date April 08, 2003
9 //
10 //  The parameters are:
11 //  -  min_length - the smallest number of points to fit with a line seg
12 //  -  tol - the threshold on mean square distance from points to line seg
13 //  -  angle - the maximum angle between segments that could be merged
14 //  A line segment is incrementally fit to the curve until the tolerance
15 //  is exceeded. The line segment is output and a new line fit is started.
16 //
17 // \verbatim
18 //  Modifications
19 //   none
20 // \endverbatim
21 #include <vector>
22 #ifdef _MSC_VER
23 #  include <vcl_msvc_warnings.h>
24 #endif
25 #include <vgl/vgl_point_2d.h>
26 #include <vgl/vgl_line_segment_2d.h>
27 
28 template <class T>
29 class vgl_fit_lines_2d
30 {
31   // Data Members--------------------------------------------------------------
32  protected:
33   bool verbose_;
34   std::vector<vgl_point_2d<T> > curve_;
35   std::vector<vgl_line_segment_2d<T> > segs_;
36   std::vector<int> curve_indices_;
37   unsigned int min_length_;
38   T tol_;
39  public:
40 
41   // Constructors/Initializers/Destructors-------------------------------------
42 
43   vgl_fit_lines_2d(unsigned int min_length = 10, T tol = 0.15);
44 
45   ~vgl_fit_lines_2d() = default;
46 
47   // Operations----------------------------------------------------------------
set_verbose(bool verbose)48   void set_verbose(bool verbose){verbose_ = verbose;}
49   //: set parameters
set_min_fit_length(unsigned int min_fit_length)50   void set_min_fit_length(unsigned int min_fit_length){min_length_ = min_fit_length;}
set_rms_error_tol(T rms_error_tol)51   void set_rms_error_tol(T rms_error_tol){tol_ = rms_error_tol;}
52 
53   //: add a point to the curve
54   void add_point(vgl_point_2d<T> const &p);
55   void add_point(T x, T y);
56 
57   //: add an entire curve
add_curve(std::vector<vgl_point_2d<T>> const & curve)58   void add_curve(std::vector<vgl_point_2d<T> > const & curve){curve_=curve;}
59 
60   //: clear internal data
61   void clear();
62 
63   //: the fitting method
64   bool fit();
65 
66   // Data Access---------------------------------------------------------------
get_points()67   std::vector<vgl_point_2d<T> >& get_points(){return curve_;}
get_line_segs()68   std::vector<vgl_line_segment_2d<T> >& get_line_segs(){return segs_;}
69   //: This vector provides an index mapping each curve point to the line it belongs to
70   //  An index of -1 indicates the curve point was not used in any line estimate
get_indices()71   std::vector<int>& get_indices() {return curve_indices_;}
72  protected:
73   //:output a line that fits from start to end
74   void output(unsigned int start_index, unsigned int end_index);
75 };
76 
77 #define VGL_FIT_LINES_2D_INSTANTIATE(T) extern "please include vgl/algo/vgl_fit_lines_2d.hxx instead"
78 
79 #endif // vgl_fit_lines_2d_h_
80