contrib/oxl/osl/osl_ortho_regress.h
Go to the documentation of this file.
00001 // This is oxl/osl/osl_ortho_regress.h
00002 #ifndef osl_ortho_regress_h_
00003 #define osl_ortho_regress_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 // .NAME osl_ortho_regress
00008 // .INCLUDE osl/osl_ortho_regress.h
00009 // .FILE osl_ortho_regress.cxx
00010 //
00011 // .SECTION Description
00012 // An osl_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 // There is no need to store any fitting costs, "current
00021 // fit" etc on this object.
00022 //
00023 // \author fsm
00024 
00025 class osl_ortho_regress
00026 {
00027  public:
00028   unsigned S1;                  // S_00
00029   double Sx, Sy;                // S_10, S_01
00030   double Sxx, Sxy, Syy;         // S_20, S_11, S_02
00031 
00032   void reset() {
00033     S1 = 0;
00034     Sx = Sy = 0;
00035     Sxx = Sxy = Syy =0;
00036   }
00037 
00038   osl_ortho_regress() { reset(); }
00039 
00040   ~osl_ortho_regress() { }
00041 
00042   void add_point(double x, double y) {
00043     ++S1;
00044     Sx += x; Sy += y;
00045     Sxx += x*x; Sxy += x*y; Syy += y*y;
00046   }
00047   void add_points(double const *, double const *, unsigned);
00048   void add_points(float  const *, float  const *, unsigned);
00049 
00050   void remove_point(double x, double y) {
00051     --S1;
00052     Sx -= x; Sy -= y;
00053     Sxx -= x*x; Sxy -= x*y; Syy -= y*y;
00054   }
00055   void remove_points(double const *, double const *, unsigned);
00056   void remove_points(float  const *, float  const *, unsigned);
00057 
00058   double cost(double a, double b, double c) const;
00059   double rms_cost(double a, double b, double c) const;
00060 
00061   // by address (preferred)
00062   bool fit(double *a, double *b, double *c) const
00063     { return fit(*a, *b, *c); }
00064   bool fit_constrained(double x, double y, double *a, double *b, double *c) const
00065     { return fit_constrained(x, y, *a, *b, *c); }
00066 
00067  protected:
00068   // by reference
00069   bool fit(double &a, double &b, double &c) const;
00070   bool fit_constrained(double x, double y, double &a, double &b, double &c) const;
00071 };
00072 
00073 #endif // osl_ortho_regress_h_