ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
progressive_sampler.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
33 
34 #include <numeric>
35 
36 #include "util/misc.h"
37 #include "util/random.h"
38 
39 namespace colmap {
40 
41 ProgressiveSampler::ProgressiveSampler(const size_t num_samples)
42  : num_samples_(num_samples),
43  total_num_samples_(0),
44  t_(0),
45  n_(0),
46  T_n_(0),
47  T_n_p_(0) {}
48 
49 void ProgressiveSampler::Initialize(const size_t total_num_samples) {
50  CHECK_LE(num_samples_, total_num_samples);
51  total_num_samples_ = total_num_samples;
52 
53  t_ = 0;
54  n_ = num_samples_;
55 
56  // Number of iterations before PROSAC behaves like RANSAC. Default value
57  // is chosen according to the recommended value in the paper.
58  const size_t kNumProgressiveIterations = 200000;
59 
60  // Compute T_n using recurrent relation in equation 3 (first part).
61  T_n_ = kNumProgressiveIterations;
62  T_n_p_ = 1.0;
63  for (size_t i = 0; i < num_samples_; ++i) {
64  T_n_ *= static_cast<double>(num_samples_ - i) / (total_num_samples_ - i);
65  }
66 }
67 
69  return std::numeric_limits<size_t>::max();
70 }
71 
72 std::vector<size_t> ProgressiveSampler::Sample() {
73  t_ += 1;
74 
75  // Compute T_n_p_ using recurrent relation in equation 3 (second part).
76  if (t_ == T_n_p_ && n_ < total_num_samples_) {
77  const double T_n_plus_1 = T_n_ * (n_ + 1.0) / (n_ + 1.0 - num_samples_);
78  T_n_p_ += std::ceil(T_n_plus_1 - T_n_);
79  T_n_ = T_n_plus_1;
80  n_ += 1;
81  }
82 
83  // Decide how many samples to draw from which part of the data as
84  // specified in equation 5.
85  size_t num_random_samples = num_samples_;
86  size_t max_random_sample_idx = n_ - 1;
87  if (T_n_p_ >= t_) {
88  num_random_samples -= 1;
89  max_random_sample_idx -= 1;
90  }
91 
92  // Draw semi-random samples as described in algorithm 1.
93  std::vector<size_t> sampled_idxs;
94  sampled_idxs.reserve(num_samples_);
95  for (size_t i = 0; i < num_random_samples; ++i) {
96  while (true) {
97  const size_t random_idx =
98  RandomInteger<uint32_t>(0, max_random_sample_idx);
99  if (!VectorContainsValue(sampled_idxs, random_idx)) {
100  sampled_idxs.push_back(random_idx);
101  break;
102  }
103  }
104  }
105 
106  // In progressive sampling mode, the last element is mandatory.
107  if (T_n_p_ >= t_) {
108  sampled_idxs.push_back(n_);
109  }
110 
111  return sampled_idxs;
112 }
113 
114 } // namespace colmap
void Initialize(const size_t total_num_samples) override
std::vector< size_t > Sample() override
ProgressiveSampler(const size_t num_samples)
MiniVec< float, N > ceil(const MiniVec< float, N > &a)
Definition: MiniVec.h:89
bool VectorContainsValue(const std::vector< T > &vector, const T value)
Definition: misc.h:136