11 inline double drand48(
void) {
12 const double RS_SCALE = (1.0 / (1.0 + RAND_MAX));
16 d = (((rand () * RS_SCALE) + rand ()) * RS_SCALE + rand ()) * RS_SCALE;
28 template <
class T,
class I>
void randomTuple(
const std::vector<T>& cdf, std::vector<I>& t, T maxp) {
29 for (
size_t i=0; i<t.size(); i++) {
31 double x = drand48()* maxp;
32 size_t r = std::min(cdf.size()-1, (size_t)(std::lower_bound(cdf.begin(), cdf.end(), (T)x) - cdf.begin()));
33 for (
size_t j=0; j<i; j++)
42 for (
size_t i=0; i<t.size(); i++) {
44 size_t r = (size_t)(drand48() * bound);
45 for (
size_t j=0; j<i; j++)
76 template <
class Obs,
class Trans,
class Tol>
size_t find_RANSAC_inliers(
const std::vector<Obs>& observations,
const Tol& tolerance,
size_t N,
77 Trans& best, std::vector<bool>& inlier,
int sample_size = Trans::hypothesis_size )
79 std::vector<bool> thisInlier(observations.size());
81 std::vector<size_t> sample_index(sample_size);
82 std::vector<Obs> sample(sample_size);
85 for (
int i=0;i<sample_size; i++)
86 sample[i] = observations[sample_index[i]];
88 thisT.estimate(sample.begin(), sample.end());
90 for (
size_t i=0; i<observations.size(); i++) {
91 const Obs& o = observations[i];
92 if (thisT.isInlier(o, tolerance)) {
96 thisInlier[i] =
false;
98 if (score > bestScore) {
110 template <
class Obs,
class Trans,
class Tol>
size_t find_RANSAC_inliers(
const std::vector<Obs>& observations,
int sample_size,
const Tol& tolerance,
size_t N,
111 Trans& best, std::vector<bool>& inlier)
138 template <
class Obs,
class Trans,
class Tol>
double find_MSAC_inliers(
const std::vector<Obs>& observations,
const Tol& tolerance,
size_t N,
139 Trans& best, std::vector<bool>& inlier,
int sample_size = Trans::hypothesis_size)
141 std::vector<bool> thisInlier(observations.size());
142 const double toleranceSquared = tolerance * tolerance;
143 double bestScore = observations.size() * toleranceSquared;
145 std::vector<size_t> sample_index(sample_size);
146 std::vector<Obs> sample(sample_size);
149 for (
int i=0;i<sample_size; i++)
150 sample[i] = observations[sample_index[i]];
152 thisT.estimate(sample.begin(), sample.end());
154 for (
size_t i=0; i<observations.size(); i++) {
155 const Obs& o = observations[i];
156 const double s = thisT.score(o);
157 if (s < toleranceSquared) {
158 thisInlier[i] =
true;
161 thisInlier[i] =
false;
162 score += toleranceSquared;
165 if (score < bestScore) {
177 template <
class Obs,
class Trans,
class Tol>
double find_MSAC_inliers(
const std::vector<Obs>& observations,
int sample_size,
const Tol& tolerance,
size_t N,
178 Trans& best, std::vector<bool>& inlier)
186 return pow(
double(H), -
double(B)/N);
216 template <
class Obs,
class Trans,
class Tol,
class Prob>
218 Trans& best, std::vector<bool>& inlier,
int sample_size = Trans::hypothesis_size)
220 std::vector<Trans> hypotheses(N,best);
221 std::vector<std::pair<int,size_t> > score(N);
222 std::vector<size_t> sample_index(sample_size);
223 std::vector<Obs> sample(sample_size);
224 std::vector<double> cdf(observations.size());
225 cdf[0] = prob(observations[0]);
226 for (
size_t i=1; i<observations.size(); ++i)
227 cdf[i] = cdf[i-1] + prob(observations[i]);
228 const double psum = cdf.back();
230 for (
size_t i=0; i<hypotheses.size(); i++) {
233 for (
int s=0; s<sample_size; ++s)
234 sample[s] = observations[sample_index[s]];
236 while (!hypotheses[i].estimate(sample.begin(), sample.end()));
237 score[i] = std::make_pair(0,i);
240 const double factor =
getShrinkRatio(N, observations.size(), block_size);
241 while (m < observations.size()) {
242 size_t end = std::min(observations.size(), m+block_size);
243 for (
size_t i=0; i<score.size(); i++) {
244 const Trans& thisT = hypotheses[score[i].second];
246 for (
size_t j=m; j!=end; j++) {
247 if (thisT.isInlier(observations[j], tolerance))
252 unsigned int cutoff = (
unsigned int)(score.size() * factor);
255 std::nth_element(score.begin(), score.end(), score.begin()+cutoff, std::greater<std::pair<int,size_t> >());
256 score.resize(cutoff);
259 size_t best_index = std::max_element(score.begin(), score.end())->second;
260 best = hypotheses[best_index];
262 inlier.resize(observations.size());
263 for (
size_t i=0; i<observations.size(); i++) {
264 if (best.isInlier(observations[i], tolerance)) {
276 template <
class Obs,
class Trans,
class Tol,
class Prob>
278 Trans& best, std::vector<bool>& inlier)
308 Trans& best, std::vector<bool>& inlier,
int sample_size = Trans::hypothesis_size)
310 std::vector<Trans> hypotheses(N,best);
311 std::vector<std::pair<int,size_t> > score(N);
312 std::vector<size_t> sample_index(sample_size);
313 std::vector<Obs> sample(sample_size);
314 for (
size_t i=0; i<hypotheses.size(); i++) {
317 for (
int s=0; s<sample_size; ++s)
318 sample[s] = observations[sample_index[s]];
320 while (!hypotheses[i].estimate(sample.begin(), sample.end()));
321 score[i] = std::make_pair(0,i);
324 const double factor =
getShrinkRatio(N, observations.size(), block_size);
325 while (m < observations.size()) {
326 size_t end = std::min(observations.size(), m+block_size);
327 for (
size_t i=0; i<score.size(); i++) {
328 const Trans& thisT = hypotheses[score[i].second];
330 for (
size_t j=m; j!=end; j++) {
331 if (thisT.isInlier(observations[j], tolerance))
336 unsigned int cutoff = (
unsigned int)(score.size() * factor);
339 std::nth_element(score.begin(), score.end(), score.begin()+cutoff, std::greater<std::pair<int,size_t> >());
340 score.resize(cutoff);
343 size_t best_index = std::max_element(score.begin(), score.end())->second;
344 best = hypotheses[best_index];
346 inlier.resize(observations.size());
347 for (
size_t i=0; i<observations.size(); i++) {
348 if (best.isInlier(observations[i], tolerance)) {
360 template <
class Obs,
class Trans,
class Tol>
size_t find_RANSAC_inliers_breadth_first(
const std::vector<Obs>& observations,
int sample_size,
const Tol& tolerance,
size_t N,
size_t block_size,
361 Trans& best, std::vector<bool>& inlier)
372 template <
class T,
class U>
374 assert(v.size() <= flag.size());
376 while (i<v.size() && flag[i])
379 for (;i<v.size();i++) {
383 v.erase(v.begin() + j, v.end());