tesseract  5.0.0-alpha-619-ge9db
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 ()
 
double get_b ()
 
double get_c ()
 

Detailed Description

Definition at line 24 of file quadlsq.h.

Constructor & Destructor Documentation

◆ QLSQ()

QLSQ::QLSQ ( )
inline

Definition at line 43 of file quadlsq.h.

45  { //get x squard

Member Function Documentation

◆ add()

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

Definition at line 53 of file quadlsq.cpp.

55  {
56  n++; // Count elements.
57  sigx += x; // Update accumulators.
58  sigy += y;
59  sigxx += x * x;
60  sigxy += x * y;
61  sigyy += y * y;
62  sigxxx += static_cast<long double>(x) * x * x;
63  sigxxy += static_cast<long double>(x) * x * y;

◆ clear()

void QLSQ::clear ( )

Definition at line 32 of file quadlsq.cpp.

33  { // initialize
34  a = 0.0;
35  b = 0.0;
36  c = 0.0;
37  n = 0; // No elements.
38  sigx = 0.0; // Zero accumulators.
39  sigy = 0.0;
40  sigxx = 0.0;
41  sigxy = 0.0;
42  sigyy = 0.0;
43  sigxxx = 0.0;
44  sigxxy = 0.0;
45  sigxxxx = 0.0;

◆ count()

int32_t QLSQ::count ( )
inline

Definition at line 54 of file quadlsq.h.

55  :
56  int32_t n; //no of elements

◆ fit()

void QLSQ::fit ( int  degree)

Definition at line 95 of file quadlsq.cpp.

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

◆ get_a()

double QLSQ::get_a ( )
inline

Definition at line 60 of file quadlsq.h.

◆ get_b()

double QLSQ::get_b ( )
inline

Definition at line 63 of file quadlsq.h.

◆ get_c()

double QLSQ::get_c ( )
inline

Definition at line 66 of file quadlsq.h.

◆ remove()

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

Definition at line 71 of file quadlsq.cpp.

74  {
75  if (n <= 0) {
76  tprintf("Can't remove an element from an empty QLSQ accumulator!\n");
77  return;
78  }
79  n--; // Count elements.
80  sigx -= x; // Update accumulators.
81  sigy -= y;
82  sigxx -= x * x;
83  sigxy -= x * y;
84  sigyy -= y * y;
85  sigxxx -= static_cast<long double>(x) * x * x;

The documentation for this class was generated from the following files:
kMinVariance
const long double kMinVariance
Definition: quadlsq.cpp:25
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34