GteApprHeightLine2.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 line to height data (x,f(x)). The line is of the
14 // form: (y - yAvr) = a*(x - xAvr), where (xAvr,yAvr) is the average of the
15 // sample points. The return value of Fit is 'true' iff the fit is successful
16 // (the input points are not degenerate to a single point). The mParameters
17 // values are ((xAvr,yAvr),(a,-1)) on success and ((0,0),(0,0)) on failure.
18 // The error for (x0,y0) is [a*(x0-xAvr)-(y0-yAvr)]^2.
19 
20 namespace gte
21 {
22 
23 template <typename Real>
25  :
26  public ApprQuery<Real, ApprHeightLine2<Real>, Vector2<Real>>
27 {
28 public:
29  // Initialize the model parameters to zero.
31 
32  // Basic fitting algorithm.
33  bool Fit(int numPoints, Vector2<Real> const* points);
34  std::pair<Vector2<Real>, Vector2<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(Vector2<Real> const& observation) const;
40  bool Fit(std::vector<Vector2<Real>> const& observations,
41  std::vector<int> const& indices);
42 
43 private:
44  std::pair<Vector2<Real>, Vector2<Real>> mParameters;
45 };
46 
47 
48 template <typename Real>
50 {
53 }
54 
55 template <typename Real>
56 bool ApprHeightLine2<Real>::Fit(int numPoints, Vector2<Real> const* points)
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;
70  for (int i = 0; i < numPoints; ++i)
71  {
72  Vector2<Real> diff = points[i] - mean;
73  covar00 += diff[0] * diff[0];
74  covar01 += diff[0] * diff[1];
75  }
76 
77  // Decompose the covariance matrix.
78  if (covar00 >(Real)0)
79  {
80  mParameters.first = mean;
81  mParameters.second[0] = covar01 / covar00;
82  mParameters.second[1] = (Real)-1;
83  return true;
84  }
85  }
86 
89  return false;
90 }
91 
92 template <typename Real>
93 std::pair<Vector2<Real>, Vector2<Real>> const&
95 {
96  return mParameters;
97 }
98 
99 template <typename Real>
101 {
102  return 2;
103 }
104 
105 template <typename Real>
106 Real ApprHeightLine2<Real>::Error(Vector2<Real> const& observation) const
107 {
108  Real d = Dot(observation - mParameters.first, mParameters.second);
109  Real error = d*d;
110  return error;
111 }
112 
113 template <typename Real>
115  std::vector<Vector2<Real>> const& observations,
116  std::vector<int> const& indices)
117 {
118  if (static_cast<int>(indices.size()) >= GetMinimumRequired())
119  {
120  // Compute the mean of the points.
122  for (auto index : indices)
123  {
124  mean += observations[index];
125  }
126  mean /= (Real)indices.size();
127 
128  // Compute the covariance matrix of the points.
129  Real covar00 = (Real)0, covar01 = (Real)0;
130  for (auto index : indices)
131  {
132  Vector2<Real> diff = observations[index] - mean;
133  covar00 += diff[0] * diff[0];
134  covar01 += diff[0] * diff[1];
135  }
136 
137  // Decompose the covariance matrix.
138  if (covar00 > (Real)0)
139  {
140  mParameters.first = mean;
141  mParameters.second[0] = covar01 / covar00;
142  mParameters.second[1] = (Real)-1;
143  return true;
144  }
145  }
146 
148  mParameters.second = Vector2<Real>::Zero();
149  return false;
150 }
151 
152 
153 }
int GetMinimumRequired() const
bool Fit(int numPoints, Vector2< Real > const *points)
std::pair< Vector2< Real >, Vector2< Real > > mParameters
GLfixed GLfixed GLint GLint GLfixed points
Definition: glext.h:4927
GLsizei GLenum const void * indices
Definition: glcorearb.h:401
static Vector Zero()
Real Error(Vector2< Real > const &observation) const
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
std::pair< Vector2< Real >, Vector2< Real > > const & GetParameters() const
GLuint index
Definition: glcorearb.h:781


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