tesseract  5.0.0
tesseract::QLSQ Class Reference

#include <quadlsq.h>

Public Member Functions

 QLSQ ()
 
void clear ()
 
void add (double x, double y)
 
void remove (double x, double y)
 
int32_t count ()
 
void fit (int degree)
 
double get_a () const
 
double get_b () const
 
double get_c () const
 

Detailed Description

Definition at line 26 of file quadlsq.h.

Constructor & Destructor Documentation

◆ QLSQ()

tesseract::QLSQ::QLSQ ( )
inline

Definition at line 28 of file quadlsq.h.

28  { // constructor
29  clear(); // set to zeros
30  }
void clear()
Definition: quadlsq.cpp:37

Member Function Documentation

◆ add()

void tesseract::QLSQ::add ( double  x,
double  y 
)

Definition at line 58 of file quadlsq.cpp.

58  {
59  n++; // Count elements.
60  sigx += x; // Update accumulators.
61  sigy += y;
62  sigxx += x * x;
63  sigxy += x * y;
64  sigyy += y * y;
65  sigxxx += static_cast<long double>(x) * x * x;
66  sigxxy += static_cast<long double>(x) * x * y;
67  sigxxxx += static_cast<long double>(x) * x * x * x;
68 }

◆ clear()

void tesseract::QLSQ::clear ( )

Definition at line 37 of file quadlsq.cpp.

37  { // initialize
38  a = 0.0;
39  b = 0.0;
40  c = 0.0;
41  n = 0; // No elements.
42  sigx = 0.0; // Zero accumulators.
43  sigy = 0.0;
44  sigxx = 0.0;
45  sigxy = 0.0;
46  sigyy = 0.0;
47  sigxxx = 0.0;
48  sigxxy = 0.0;
49  sigxxxx = 0.0;
50 }

◆ count()

int32_t tesseract::QLSQ::count ( )
inline

Definition at line 39 of file quadlsq.h.

39  { // no of elements
40  return n;
41  }

◆ fit()

void tesseract::QLSQ::fit ( int  degree)

Definition at line 100 of file quadlsq.cpp.

100  {
101  long double x_variance =
102  static_cast<long double>(sigxx) * n - static_cast<long double>(sigx) * sigx;
103 
104  // Note: for computational efficiency, we do not normalize the variance,
105  // covariance and cube variance here as they are in the same order in both
106  // nominators and denominators. However, we need be careful in value range
107  // check.
108  if (x_variance < kMinVariance * n * n || degree < 1 || n < 2) {
109  // We cannot calculate b reliably so forget a and b, and just work on c.
110  a = b = 0.0;
111  if (n >= 1 && degree >= 0) {
112  c = sigy / n;
113  } else {
114  c = 0.0;
115  }
116  return;
117  }
118  long double top96 = 0.0; // Accurate top.
119  long double bottom96 = 0.0; // Accurate bottom.
120  long double cubevar = sigxxx * n - static_cast<long double>(sigxx) * sigx;
121  long double covariance =
122  static_cast<long double>(sigxy) * n - static_cast<long double>(sigx) * sigy;
123 
124  if (n >= 4 && degree >= 2) {
125  top96 = cubevar * covariance;
126  top96 += x_variance * (static_cast<long double>(sigxx) * sigy - sigxxy * n);
127 
128  bottom96 = cubevar * cubevar;
129  bottom96 -= x_variance * (sigxxxx * n - static_cast<long double>(sigxx) * sigxx);
130  }
131  if (bottom96 >= kMinVariance * n * n * n * n) {
132  // Denominators looking good
133  a = top96 / bottom96;
134  top96 = covariance - cubevar * a;
135  b = top96 / x_variance;
136  } else {
137  // Forget a, and concentrate on b.
138  a = 0.0;
139  b = covariance / x_variance;
140  }
141  c = (sigy - a * sigxx - b * sigx) / n;
142 }
const long double kMinVariance
Definition: quadlsq.cpp:29

◆ get_a()

double tesseract::QLSQ::get_a ( ) const
inline

Definition at line 45 of file quadlsq.h.

45  { // get x squard
46  return a;
47  }

◆ get_b()

double tesseract::QLSQ::get_b ( ) const
inline

Definition at line 48 of file quadlsq.h.

48  { // get x squard
49  return b;
50  }

◆ get_c()

double tesseract::QLSQ::get_c ( ) const
inline

Definition at line 51 of file quadlsq.h.

51  { // get x squard
52  return c;
53  }

◆ remove()

void tesseract::QLSQ::remove ( double  x,
double  y 
)

Definition at line 76 of file quadlsq.cpp.

76  {
77  if (n <= 0) {
78  tprintf("Can't remove an element from an empty QLSQ accumulator!\n");
79  return;
80  }
81  n--; // Count elements.
82  sigx -= x; // Update accumulators.
83  sigy -= y;
84  sigxx -= x * x;
85  sigxy -= x * y;
86  sigyy -= y * y;
87  sigxxx -= static_cast<long double>(x) * x * x;
88  sigxxy -= static_cast<long double>(x) * x * y;
89  sigxxxx -= static_cast<long double>(x) * x * x * x;
90 }
void tprintf(const char *format,...)
Definition: tprintf.cpp:41

The documentation for this class was generated from the following files: