GteApprHeightPlane3.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 
12 
13 // Least-squares fit of a plane to height data (x,y,f(x,y)). The plane is of
14 // the form (z - zAvr) = a*(x - xAvr) + b*(y - yAvr), where (xAvr,yAvr,zAvr)
15 // is the average of the sample points. The return value is 'true' iff the
16 // fit is successful (the input points are noncollinear). The mParameters
17 // values are ((xAvr,yAvr,zAvr),(a,b,-1)) on success and ((0,0,0),(0,0,0)) on
18 // failure. The error for (x0,y0,z0) is [a*(x0-xAvr)+b*(y0-yAvr)-(z0-zAvr)]^2.
19 
20 namespace gte
21 {
22 
23 template <typename Real>
25  :
26  public ApprQuery<Real, ApprHeightPlane3<Real>, Vector3<Real>>
27 {
28 public:
29  // Initialize the model parameters to zero.
31 
32  // Basic fitting algorithm.
33  bool Fit(int numPoints, Vector3<Real> const* points);
34  std::pair<Vector3<Real>, Vector3<Real>> const& GetParameters() const;
35 
36  // Functions called by ApprQuery::RANSAC. See GteApprQuery.h for a
37  // detailed description.
38  int GetMinimumRequired() const;
39  Real Error(Vector3<Real> const& observation) const;
40  bool Fit(std::vector<Vector3<Real>> const& observations,
41  std::vector<int> const& indices);
42 
43 private:
44  std::pair<Vector3<Real>, Vector3<Real>> mParameters;
45 };
46 
47 
48 template <typename Real>
50 {
53 }
54 
55 template <typename Real>
57 {
58  if (numPoints >= GetMinimumRequired() && points)
59  {
60  // Compute the mean of the points.
62  for (int i = 0; i < numPoints; ++i)
63  {
64  mean += points[i];
65  }
66  mean /= (Real)numPoints;
67 
68  // Compute the covariance matrix of the points.
69  Real covar00 = (Real)0, covar01 = (Real)0, covar02 = (Real)0;
70  Real covar11 = (Real)0, covar12 = (Real)0;
71  for (int i = 0; i < numPoints; ++i)
72  {
73  Vector3<Real> diff = points[i] - mean;
74  covar00 += diff[0] * diff[0];
75  covar01 += diff[0] * diff[1];
76  covar02 += diff[0] * diff[2];
77  covar11 += diff[1] * diff[1];
78  covar12 += diff[1] * diff[2];
79  }
80 
81  // Decompose the covariance matrix.
82  Real det = covar00*covar11 - covar01*covar01;
83  if (det != (Real)0)
84  {
85  Real invDet = ((Real)1) / det;
86  mParameters.first = mean;
87  mParameters.second[0] =
88  (covar11*covar02 - covar01*covar12)*invDet;
89  mParameters.second[1] =
90  (covar00*covar12 - covar01*covar02)*invDet;
91  mParameters.second[2] = (Real)-1;
92  return true;
93  }
94  }
95 
98  return false;
99 }
100 
101 template <typename Real>
102 std::pair<Vector3<Real>, Vector3<Real>> const&
104 {
105  return mParameters;
106 }
107 
108 template <typename Real>
110 {
111  return 3;
112 }
113 
114 template <typename Real>
115 Real ApprHeightPlane3<Real>::Error(Vector3<Real> const& observation) const
116 {
117  Real d = Dot(observation - mParameters.first, mParameters.second);
118  Real error = d*d;
119  return error;
120 }
121 
122 template <typename Real>
124  std::vector<Vector3<Real>> const& observations,
125  std::vector<int> const& indices)
126 {
127  if (static_cast<int>(indices.size()) >= GetMinimumRequired())
128  {
129  // Compute the mean of the points.
131  for (auto index : indices)
132  {
133  mean += observations[index];
134  }
135  mean /= (Real)indices.size();
136 
137  // Compute the covariance matrix of the points.
138  Real covar00 = (Real)0, covar01 = (Real)0, covar02 = (Real)0;
139  Real covar11 = (Real)0, covar12 = (Real)0;
140  for (auto index : indices)
141  {
142  Vector3<Real> diff = observations[index] - mean;
143  covar00 += diff[0] * diff[0];
144  covar01 += diff[0] * diff[1];
145  covar02 += diff[0] * diff[2];
146  covar11 += diff[1] * diff[1];
147  covar12 += diff[1] * diff[2];
148  }
149 
150  // Decompose the covariance matrix.
151  Real det = covar00*covar11 - covar01*covar01;
152  if (det != (Real)0)
153  {
154  Real invDet = ((Real)1) / det;
155  mParameters.first = mean;
156  mParameters.second[0] =
157  (covar11*covar02 - covar01*covar12)*invDet;
158  mParameters.second[1] =
159  (covar00*covar12 - covar01*covar02)*invDet;
160  mParameters.second[2] = (Real)-1;
161  return true;
162  }
163  }
164 
166  mParameters.second = Vector3<Real>::Zero();
167  return false;
168 }
169 
170 
171 }
GLfixed GLfixed GLint GLint GLfixed points
Definition: glext.h:4927
GLsizei GLenum const void * indices
Definition: glcorearb.h:401
static Vector Zero()
Definition: GteVector.h:295
std::pair< Vector3< Real >, Vector3< Real > > mParameters
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
Real Error(Vector3< Real > const &observation) const
GLuint index
Definition: glcorearb.h:781
std::pair< Vector3< Real >, Vector3< Real > > const & GetParameters() const
bool Fit(int numPoints, Vector3< Real > const *points)


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