ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
sprt.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 
32 #include "optim/sprt.h"
33 
34 namespace colmap {
35 
36 SPRT::SPRT(const Options& options) { Update(options); }
37 
38 void SPRT::Update(const Options& options) {
39  options_ = options;
40  delta_epsilon_ = options.delta / options.epsilon;
41  delta_1_epsilon_1_ = (1 - options.delta) / (1 - options.epsilon);
42  UpdateDecisionThreshold();
43 }
44 
45 bool SPRT::Evaluate(const std::vector<double>& residuals,
46  const double max_residual, size_t* num_inliers,
47  size_t* num_eval_samples) {
48  num_inliers = 0;
49 
50  double likelihood_ratio = 1;
51 
52  for (size_t i = 0; i < residuals.size(); ++i) {
53  if (std::abs(residuals[i]) <= max_residual) {
54  num_inliers += 1;
55  likelihood_ratio *= delta_epsilon_;
56  } else {
57  likelihood_ratio *= delta_1_epsilon_1_;
58  }
59 
60  if (likelihood_ratio > decision_threshold_) {
61  *num_eval_samples = i + 1;
62  return false;
63  }
64  }
65 
66  *num_eval_samples = residuals.size();
67 
68  return true;
69 }
70 
71 void SPRT::UpdateDecisionThreshold() {
72  // Equation 2
73  const double C = (1 - options_.delta) *
74  std::log((1 - options_.delta) / (1 - options_.epsilon)) +
75  options_.delta * std::log(options_.delta / options_.epsilon);
76 
77  // Equation 6
78  const double A0 =
79  options_.eval_time_ratio * C / options_.num_models_per_sample + 1;
80 
81  double A = A0;
82 
83  const double kEps = 1.5e-8;
84 
85  // Compute A using the recursive relation
86  // A* = lim(n->inf) A
87  // The series typically converges within 4 iterations
88 
89  for (size_t i = 0; i < 100; ++i) {
90  const double A1 = A0 + std::log(A);
91 
92  if (std::abs(A1 - A) < kEps) {
93  break;
94  }
95 
96  A = A1;
97  }
98 
99  decision_threshold_ = A;
100 }
101 
102 } // namespace colmap
bool Evaluate(const std::vector< double > &residuals, const double max_residual, size_t *num_inliers, size_t *num_eval_samples)
Definition: sprt.cc:45
void Update(const Options &options)
Definition: sprt.cc:38
SPRT(const Options &options)
Definition: sprt.cc:36
double eval_time_ratio
Definition: sprt.h:32
int num_models_per_sample
Definition: sprt.h:37
double epsilon
Definition: sprt.h:27