GteApprParaboloid3.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
10 #include <Mathematics/GteMatrix.h>
11 #include <Mathematics/GteVector3.h>
13 
14 // Least-squares fit of a paraboloid to a set of point. The paraboloid is
15 // of the form z = c0*x^2+c1*x*y+c2*y^2+c3*x+c4*y+c5. A successful fit is
16 // indicated by return value of 'true'.
17 //
18 // Given a set of samples (x_i,y_i,z_i) for 0 <= i < N, and assuming
19 // that the true values lie on a paraboloid
20 // z = p0*x*x + p1*x*y + p2*y*y + p3*x + p4*y + p5 = Dot(P,Q(x,y))
21 // where P = (p0,p1,p2,p3,p4,p5) and Q(x,y) = (x*x,x*y,y*y,x,y,1),
22 // select P to minimize the sum of squared errors
23 // E(P) = sum_{i=0}^{N-1} [Dot(P,Q_i)-z_i]^2
24 // where Q_i = Q(x_i,y_i).
25 //
26 // The minimum occurs when the gradient of E is the zero vector,
27 // grad(E) = 2 sum_{i=0}^{N-1} [Dot(P,Q_i)-z_i] Q_i = 0
28 // Some algebra converts this to a system of 6 equations in 6 unknowns:
29 // [(sum_{i=0}^{N-1} Q_i Q_i^t] P = sum_{i=0}^{N-1} z_i Q_i
30 // The product Q_i Q_i^t is a product of the 6x1 matrix Q_i with the
31 // 1x6 matrix Q_i^t, the result being a 6x6 matrix.
32 //
33 // Define the 6x6 symmetric matrix A = sum_{i=0}^{N-1} Q_i Q_i^t and the 6x1
34 // vector B = sum_{i=0}^{N-1} z_i Q_i. The choice for P is the solution to
35 // the linear system of equations A*P = B. The entries of A and B indicate
36 // summations over the appropriate product of variables. For example,
37 // s(x^3 y) = sum_{i=0}^{N-1} x_i^3 y_i.
38 //
39 // +- -++ + +- -+
40 // | s(x^4) s(x^3 y) s(x^2 y^2) s(x^3) s(x^2 y) s(x^2) ||p0| |s(z x^2)|
41 // | s(x^2 y^2) s(x y^3) s(x^2 y) s(x y^2) s(x y) ||p1| |s(z x y)|
42 // | s(y^4) s(x y^2) s(y^3) s(y^2) ||p2| = |s(z y^2)|
43 // | s(x^2) s(x y) s(x) ||p3| |s(z x) |
44 // | s(y^2) s(y) ||p4| |s(z y) |
45 // | s(1) ||p5| |s(z) |
46 // +- -++ + +- -+
47 
48 namespace gte
49 {
50 
51 template <typename Real>
53 {
54 public:
55  bool operator()(int numPoints, Vector3<Real> const* points,
56  Real coefficients[6]);
57 };
58 
59 
60 template <typename Real>
62  Vector3<Real> const* points, Real coefficients[6])
63 {
66  for (int i = 0; i < numPoints; i++)
67  {
68  Real x2 = points[i][0] * points[i][0];
69  Real xy = points[i][0] * points[i][1];
70  Real y2 = points[i][1] * points[i][1];
71  Real zx = points[i][2] * points[i][0];
72  Real zy = points[i][2] * points[i][1];
73  Real x3 = points[i][0] * x2;
74  Real x2y = x2*points[i][1];
75  Real xy2 = points[i][0] * y2;
76  Real y3 = points[i][1] * y2;
77  Real zx2 = points[i][2] * x2;
78  Real zxy = points[i][2] * xy;
79  Real zy2 = points[i][2] * y2;
80  Real x4 = x2*x2;
81  Real x3y = x3*points[i][1];
82  Real x2y2 = x2*y2;
83  Real xy3 = points[i][0] * y3;
84  Real y4 = y2*y2;
85 
86  A(0, 0) += x4;
87  A(0, 1) += x3y;
88  A(0, 2) += x2y2;
89  A(0, 3) += x3;
90  A(0, 4) += x2y;
91  A(0, 5) += x2;
92  A(1, 2) += xy3;
93  A(1, 4) += xy2;
94  A(1, 5) += xy;
95  A(2, 2) += y4;
96  A(2, 4) += y3;
97  A(2, 5) += y2;
98  A(3, 3) += x2;
99  A(3, 5) += points[i][0];
100  A(4, 5) += points[i][1];
101 
102  B[0] += zx2;
103  B[1] += zxy;
104  B[2] += zy2;
105  B[3] += zx;
106  B[4] += zy;
107  B[5] += points[i][2];
108  }
109 
110  A(1, 0) = A(0, 1);
111  A(1, 1) = A(0, 2);
112  A(1, 3) = A(0, 4);
113  A(2, 0) = A(0, 2);
114  A(2, 1) = A(1, 2);
115  A(2, 3) = A(1, 4);
116  A(3, 0) = A(0, 3);
117  A(3, 1) = A(1, 3);
118  A(3, 2) = A(2, 3);
119  A(3, 4) = A(1, 5);
120  A(4, 0) = A(0, 4);
121  A(4, 1) = A(1, 4);
122  A(4, 2) = A(2, 4);
123  A(4, 3) = A(3, 4);
124  A(4, 4) = A(2, 5);
125  A(5, 0) = A(0, 5);
126  A(5, 1) = A(1, 5);
127  A(5, 2) = A(2, 5);
128  A(5, 3) = A(3, 5);
129  A(5, 4) = A(4, 5);
130  A(5, 5) = static_cast<Real>(numPoints);
131 
132  return LinearSystem<Real>().Solve(6, &A[0], &B[0], &coefficients[0]);
133 }
134 
135 
136 }
GLfixed GLfixed GLfixed y2
Definition: glext.h:4952
GLfixed GLfixed GLint GLint GLfixed points
Definition: glext.h:4927
bool operator()(int numPoints, Vector3< Real > const *points, Real coefficients[6])
GLfixed GLfixed x2
Definition: glext.h:4952


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59