35 template <
typename Real,
typename ModelType,
typename ObservationType>
40 bool Fit(std::vector<ObservationType>
const& observations);
43 bool Fit(std::vector<ObservationType>
const& observations,
44 int const imin,
int const imax);
49 bool Fit(std::vector<ObservationType>
const& observations,
50 std::vector<int>
const&
indices,
int const numIndices);
55 ModelType& candidateModel,
56 std::vector<ObservationType>
const& observations,
57 int const numRequiredForGoodFit, Real
const maxErrorForGoodFit,
58 int const numIterations, std::vector<int>& bestConsensus,
59 ModelType& bestModel);
63 template <
typename Real,
typename ModelType,
typename ObservationType>
65 std::vector<ObservationType>
const& observations)
67 std::vector<int>
indices(observations.size());
74 return ((ModelType*)
this)->
Fit(observations, indices);
77 template <
typename Real,
typename ModelType,
typename ObservationType>
79 std::vector<ObservationType>
const& observations,
80 int const imin,
int const imax)
84 int numIndices = imax - imin + 1;
85 std::vector<int>
indices(numIndices);
87 for (
auto&
index : indices)
92 return ((ModelType*)
this)->Fit(observations, indices);
100 template <
typename Real,
typename ModelType,
typename ObservationType>
102 std::vector<ObservationType>
const& observations,
103 std::vector<int>
const&
indices,
int const numIndices)
105 int imax = std::min(numIndices, static_cast<int>(observations.size()));
106 std::vector<int> localindices(imax);
108 for (
auto&
index : localindices)
110 index = indices[i++];
113 return ((ModelType*)
this)->Fit(observations, indices);
116 template <
typename Real,
typename ModelType,
typename ObservationType>
118 ModelType& candidateModel,
119 std::vector<ObservationType>
const& observations,
120 int const numRequiredForGoodFit, Real
const maxErrorForGoodFit,
121 int const numIterations, std::vector<int>& bestConsensus,
122 ModelType& bestModel)
124 int const numObservations =
static_cast<int>(observations.size());
125 int const minRequired = candidateModel.GetMinimumRequired();
126 if (numObservations < minRequired)
138 std::vector<int> candidates(numObservations);
140 for (
auto&
c : candidates)
145 if (numObservations == minRequired)
150 bestConsensus = candidates;
151 return bestModel.Fit(observations);
154 int bestNumFittedObservations = minRequired;
156 for (
int i = 0; i < numIterations; ++i)
162 std::shuffle(candidates.begin(), candidates.end(),
163 std::default_random_engine());
166 if (candidateModel.Fit(observations, candidates, minRequired))
170 int numFittedObservations = minRequired;
171 for (j = minRequired; j < numObservations; ++j)
173 if (candidateModel.Error(observations[candidates[j]])
174 <= maxErrorForGoodFit)
176 std::swap(candidates[j],
177 candidates[numFittedObservations]);
178 ++numFittedObservations;
182 if (numFittedObservations >= numRequiredForGoodFit)
186 candidateModel.Fit(observations, candidates,
187 numFittedObservations);
188 if (numFittedObservations > bestNumFittedObservations)
192 bestModel = candidateModel;
193 bestConsensus.resize(numFittedObservations);
194 std::copy(candidates.begin(),
195 candidates.begin() + numFittedObservations,
196 bestConsensus.begin());
197 bestNumFittedObservations = numFittedObservations;
203 return bestNumFittedObservations >= numRequiredForGoodFit;
bool Fit(std::vector< ObservationType > const &observations)
static bool RANSAC(ModelType &candidateModel, std::vector< ObservationType > const &observations, int const numRequiredForGoodFit, Real const maxErrorForGoodFit, int const numIterations, std::vector< int > &bestConsensus, ModelType &bestModel)
GLsizei GLenum const void * indices