vgl_line_2d_regression.h
Go to the documentation of this file.
1 // This is core/vgl/algo/vgl_line_2d_regression.h
2 #ifndef vgl_line_2d_regression_h_
3 #define vgl_line_2d_regression_h_
4 //:
5 // \file
6 // \brief A helper class for line fitting. Holds the 2d_regression data
7 //
8 // \author
9 // April 08, 2003 - J.L. Mundy, (a port of fsm's osl/osl_ortho_regress class)
10 //
11 // \verbatim
12 // Modifications
13 // 12 Aug 2003 - Amitha Perera. Add some more documentation
14 // \endverbatim
15 
16 #include <vgl/vgl_point_2d.h>
17 #include <vgl/vgl_line_2d.h>
18 
19 //: A class to hold the line 2d_regression data and actual fitting code.
20 //
21 // In addition to fitting a line to a set of points (orthogonal
22 // regression), it is designed to help with incremental fitting. You
23 // can inexpensively add and remove points. This class does not store
24 // the points; it merely stores enough aggregate information to
25 // estimate the line parameters.
26 //
27 template <class T>
29 {
30  // Data members
31  unsigned int npts_;//!< number of points in the regression data
32  vgl_line_2d<T> line_;//!< the fitted line
33  T Sx_, Sy_, Sxx_, Sxy_, Syy_;//!< partial sums
34  double squared_error_;//!< an estimate of the squared error
35  public:
37  ~vgl_line_2d_regression() = default;
38 
39  //: The number of points added.
40  inline unsigned int get_n_pts() const { return npts_; }
41 
42  //: Add a point to the 2d_regression
43  void increment_partial_sums(const T x, const T y);
44 
45  //: Remove a point from the 2d_regression
46  //
47  // This should be a previously added point, although this cannot be
48  // verified.
49  void decrement_partial_sums(const T x, const T y);
50 
51  //: Clear 2d_regression sums
52  //
53  // This will reset the object to the freshly constructed state of having
54  // zero points.
55  void clear();
56 
57  //: Get fitting error for a given line
58  double get_rms_error(const T a, const T b, const T c);
59 
60  //: Get fitting error for current fitted line
61  double get_rms_error();
62 
63  //: Initialize estimated fitting error
64  void init_rms_error_est();
65 
66  //: Get estimated fitting error if the point (x, y) were added to the fit
67  //
68  // You must call init_rms_error_est() to initialize the running
69  // totals before the first use of this function.
70  //
71  // If \a increment is true, the running totals are updated as if
72  // the point \a p was added to the point set. It does not update
73  // the point set, however, so the point will not affect subsequent
74  // line estimation.
75  //
76  double get_rms_error_est(vgl_point_2d<T> const& p, bool increment=true);
77 
78  //: Get the fitted line
79  vgl_line_2d<T> get_line() const { return line_; }
80 
81  //: Fit a line to the current point set
82  bool fit();
83 
84  //: Fit a line to the current point set constrained to pass through (x,y).
85  bool fit_constrained(T x, T y);
86 };
87 
88 #define VGL_LINE_2D_REGRESSION_INSTANTIATE(T) extern "please include vgl/algo/vgl_line_2d_regression.hxx first"
89 
90 #endif // vgl_line_2d_regression_h_
a point in 2D nonhomogeneous space
bool fit()
Fit a line to the current point set.
double get_rms_error_est(vgl_point_2d< T > const &p, bool increment=true)
Get estimated fitting error if the point (x, y) were added to the fit.
unsigned int get_n_pts() const
The number of points added.
A class to hold the line 2d_regression data and actual fitting code.
Definition: vgl_algo_fwd.h:7
void increment_partial_sums(const T x, const T y)
Add a point to the 2d_regression.
void init_rms_error_est()
Initialize estimated fitting error.
void clear()
Clear 2d_regression sums.
void decrement_partial_sums(const T x, const T y)
Remove a point from the 2d_regression.
double get_rms_error()
Get fitting error for current fitted line.
vgl_line_2d< T > line_
the fitted line
double squared_error_
an estimate of the squared error
bool fit_constrained(T x, T y)
Fit a line to the current point set constrained to pass through (x,y).
vgl_line_2d< T > get_line() const
Get the fitted line.
unsigned int npts_
number of points in the regression data
~vgl_line_2d_regression()=default