contrib/gel/vdgl/vdgl_ortho_regress.h
Go to the documentation of this file.
00001 // This is gel/vdgl/vdgl_ortho_regress.h
00002 #ifndef vdgl_ortho_regress_h_
00003 #define vdgl_ortho_regress_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 // .NAME vdgl_ortho_regress
00008 // .INCLUDE vdgl/vdgl_ortho_regress.h
00009 // .FILE vdgl_ortho_regress.cxx
00010 //
00011 // .SECTION Description
00012 // A vdgl_ortho_regress object performs least-squares
00013 // fitting of straight lines to point sets.
00014 //
00015 // Internally, the object maintains only the moments
00016 //   S_{mn} = \sum_i x_i^m y_i^n
00017 // and this is sufficient for both unconstrained and
00018 // constrained (fit line through a point) fitting.
00019 //
00020 
00021 class vdgl_ortho_regress
00022 {
00023  public:
00024   unsigned S1;                  // S_00
00025   double Sx, Sy;                // S_10, S_01
00026   double Sxx, Sxy, Syy;         // S_20, S_11, S_02
00027 
00028   void reset() {
00029     S1 = 0;
00030     Sx = Sy = 0;
00031     Sxx = Sxy = Syy =0;
00032   }
00033 
00034   vdgl_ortho_regress() { reset(); }
00035 
00036   ~vdgl_ortho_regress() { }
00037 
00038   void add_point(double x, double y) {
00039     ++S1;
00040     Sx += x; Sy += y;
00041     Sxx += x*x; Sxy += x*y; Syy += y*y;
00042   }
00043   void add_points(double const *, double const *, unsigned);
00044   void add_points(float  const *, float  const *, unsigned);
00045 
00046   void remove_point(double x, double y) {
00047     --S1;
00048     Sx -= x; Sy -= y;
00049     Sxx -= x*x; Sxy -= x*y; Syy -= y*y;
00050   }
00051   void remove_points(double const *, double const *, unsigned);
00052   void remove_points(float  const *, float  const *, unsigned);
00053 
00054   double cost(double a, double b, double c) const;
00055   double rms_cost(double a, double b, double c) const;
00056 
00057   // by address (preferred)
00058   bool fit(double *a, double *b, double *c) const
00059     { return fit(*a, *b, *c); }
00060   bool fit_constrained(double x, double y, double *a, double *b, double *c) const
00061     { return fit_constrained(x, y, *a, *b, *c); }
00062 
00063  protected:
00064   // by reference
00065   bool fit(double &a, double &b, double &c) const;
00066   bool fit_constrained(double x, double y, double &a, double &b, double &c) const;
00067 };
00068 
00069 #endif // vdgl_ortho_regress_h_