core/vgl/algo/vgl_line_2d_regression.h
Go to the documentation of this file.
00001 // This is core/vgl/algo/vgl_line_2d_regression.h
00002 #ifndef vgl_line_2d_regression_h_
00003 #define vgl_line_2d_regression_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief A helper class for line fitting. Holds the 2d_regression data
00010 //
00011 // \author
00012 //   April 08, 2003 - J.L. Mundy, (a port of fsm's osl/osl_ortho_regress class)
00013 //
00014 // \verbatim
00015 //  Modifications
00016 //   12 Aug 2003 - Amitha Perera. Add some more documentation
00017 // \endverbatim
00018 
00019 #include <vgl/vgl_point_2d.h>
00020 #include <vgl/vgl_line_2d.h>
00021 
00022 //: A class to hold the line 2d_regression data and actual fitting code.
00023 //
00024 // In addition to fitting a line to a set of points (orthogonal
00025 // regression), it is designed to help with incremental fitting. You
00026 // can inexpensively add and remove points. This class does not store
00027 // the points; it merely stores enough aggregate information to
00028 // estimate the line parameters.
00029 //
00030 template <class T>
00031 class vgl_line_2d_regression
00032 {
00033   // Data members
00034   unsigned int npts_;//!< number of points in the regression data
00035   vgl_line_2d<T> line_;//!< the fitted line
00036   T Sx_, Sy_, Sxx_, Sxy_, Syy_;//!< partial sums
00037   double squared_error_;//!< an estimate of the squared error
00038  public:
00039   vgl_line_2d_regression();
00040   ~vgl_line_2d_regression() {}
00041 
00042   //: The number of points added.
00043   inline unsigned int get_n_pts() const { return npts_; }
00044 
00045   //: Add a point to the 2d_regression
00046   void increment_partial_sums(const T x, const T y);
00047 
00048   //: Remove a point from the 2d_regression
00049   //
00050   //  This should be a previously added point, although this cannot be
00051   //  verified.
00052   void decrement_partial_sums(const T x, const T y);
00053 
00054   //: Clear 2d_regression sums
00055   //
00056   //  This will reset the object to the freshly constructed state of having
00057   //  zero points.
00058   void clear();
00059 
00060   //: Get fitting error for a given line
00061   double get_rms_error(const T a, const T b, const T c);
00062 
00063   //: Get fitting error for current fitted line
00064   double get_rms_error();
00065 
00066   //: Initialize estimated fitting error
00067   void init_rms_error_est();
00068 
00069   //: Get estimated fitting error if the point (x, y) were added to the fit
00070   //
00071   //  You must call init_rms_error_est() to initialize the running
00072   //  totals before the first use of this function.
00073   //
00074   //  If \a increment is true, the running totals are updated as if
00075   //  the point \a p was added to the point set. It does not update
00076   //  the point set, however, so the point will not affect subsequent
00077   //  line estimation.
00078   //
00079   double get_rms_error_est(vgl_point_2d<T> const& p, bool increment=true);
00080 
00081   //: Get the fitted line
00082   vgl_line_2d<T> get_line() const { return line_; }
00083 
00084   //: Fit a line to the current point set
00085   bool fit();
00086 
00087   //: Fit a line to the current point set constrained to pass through (x,y).
00088   bool fit_constrained(T x, T y);
00089 };
00090 
00091 #define VGL_LINE_2D_REGRESSION_INSTANTIATE(T) extern "please include vgl/algo/vgl_line_2d_regression.txx first"
00092 
00093 #endif // vgl_line_2d_regression_h_