Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FitPolynom.h
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
24 #ifndef _FITPOLYNOM_H
25 #define _FITPOLYNOM_H
26 
27 #include "hugin_math/Matrix3.h"
28 
29 namespace vigra_ext
30 {
31 
35 template <typename M>
36 double calcDeterminant3(const M &m)
37 {
38  return m[0][0] * m[1][1] * m[2][2]
39  + m[0][1] * m[1][2] * m[2][0]
40  + m[0][2] * m[1][0] * m[2][1]
41  - m[2][0] * m[1][1] * m[0][2]
42  - m[2][1] * m[1][2] * m[0][0]
43  - m[2][2] * m[1][0] * m[0][1];
44 }
45 
54 template <class T>
55 void FitPolynom(T x, T xend, T y, double & a, double & b, double & c)
56  {
57  size_t n = xend - x;
58  // calculate various sums.
59  double sx=0;
60  double sx2=0;
61  double sx3=0;
62  double sx4=0;
63  double sy=0;
64  double sxy=0;
65  double sx2y=0;
66  T xi,yi;
67  for (xi=x, yi=y; xi != xend; ++xi, ++yi) {
68  double tx = *xi;
69  double ty = *yi;
70  double t = tx;
71  sx += tx;
72  sy += ty;
73  sxy += tx * ty;
74  tx = tx * t;
75  sx2 += tx;
76  sx2y += tx * ty;
77  tx = tx * t;
78  sx3 +=tx;
79  sx4 += tx * t;
80  }
81 
82  // X*A=Y
83  Matrix3 X;
84  X.m[0][0] = n;
85  X.m[0][1] = sx;
86  X.m[0][2] = sx2;
87  X.m[1][0] = sx;
88  X.m[1][1] = sx2;
89  X.m[1][2] = sx3;
90  X.m[2][0] = sx2;
91  X.m[2][1] = sx3;
92  X.m[2][2] = sx4;
93 
94  // calculate det(X)
95  double D = X.Determinant(); //calcDeterminant3(X.m);
96 
97  // matrix to calculate a;
98  X.m[0][0] = sy;
99  X.m[0][1] = sx;
100  X.m[0][2] = sx2;
101  X.m[1][0] = sxy;
102  X.m[1][1] = sx2;
103  X.m[1][2] = sx3;
104  X.m[2][0] = sx2y;
105  X.m[2][1] = sx3;
106  X.m[2][2] = sx4;
107  double A = X.Determinant();
108  a = A / D;
109 
110  // matrix to calculate b
111  X.m[0][0] = n;
112  X.m[0][1] = sy;
113  X.m[0][2] = sx2;
114  X.m[1][0] = sx;
115  X.m[1][1] = sxy;
116  X.m[1][2] = sx3;
117  X.m[2][0] = sx2;
118  X.m[2][1] = sx2y;
119  X.m[2][2] = sx4;
120  b = X.Determinant() / D;
121 
122  // matrix to calculate c
123  X.m[0][0] = n;
124  X.m[0][1] = sx;
125  X.m[0][2] = sy;
126  X.m[1][0] = sx;
127  X.m[1][1] = sx2;
128  X.m[1][2] = sxy;
129  X.m[2][0] = sx2;
130  X.m[2][1] = sx3;
131  X.m[2][2] = sx2y;
132  c = X.Determinant() / D;
133  }
134 
135 }
136 
137 #endif // _FITPOLYNOM_H
double m[3][3]
we define the Matrix3 as 3 colums of 3 rows
Definition: Matrix3.h:41
double Determinant() const
get the determinant
Definition: Matrix3.h:134
double X
static const double A(-0.75)
general : Matrix3 is a class for handling 3x3 Matrix manipulation.
Definition: Matrix3.h:37
void FitPolynom(T x, T xend, T y, double &a, double &b, double &c)
fit a second order polynom to a data set
Definition: FitPolynom.h:55
double calcDeterminant3(const M &m)
calculate the determinat of a 3x3 matrix using the sarrus formula
Definition: FitPolynom.h:36