ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
loransac_test.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 #define TEST_NAME "optim/ransac"
33 #include "util/testing.h"
34 
35 #include <Eigen/Core>
36 #include <Eigen/Geometry>
37 
38 #include "base/pose.h"
41 #include "optim/loransac.h"
42 #include "util/random.h"
43 
44 using namespace colmap;
45 
46 BOOST_AUTO_TEST_CASE(TestReport) {
48  SimilarityTransformEstimator<3>>::Report report;
49  BOOST_CHECK_EQUAL(report.success, false);
50  BOOST_CHECK_EQUAL(report.num_trials, 0);
51  BOOST_CHECK_EQUAL(report.support.num_inliers, 0);
52  BOOST_CHECK_EQUAL(report.support.residual_sum,
53  std::numeric_limits<double>::max());
54  BOOST_CHECK_EQUAL(report.inlier_mask.size(), 0);
55 }
56 
57 BOOST_AUTO_TEST_CASE(TestSimilarityTransform) {
58  SetPRNGSeed(0);
59 
60  const size_t num_samples = 1000;
61  const size_t num_outliers = 400;
62 
63  // Create some arbitrary transformation.
64  const SimilarityTransform3 orig_tform(2, ComposeIdentityQuaternion(),
65  Eigen::Vector3d(100, 10, 10));
66 
67  // Generate exact data
68  std::vector<Eigen::Vector3d> src;
69  std::vector<Eigen::Vector3d> dst;
70  for (size_t i = 0; i < num_samples; ++i) {
71  src.emplace_back(i, std::sqrt(i) + 2, std::sqrt(2 * i + 2));
72  dst.push_back(src.back());
73  orig_tform.TransformPoint(&dst.back());
74  }
75 
76  // Add some faulty data.
77  for (size_t i = 0; i < num_outliers; ++i) {
78  dst[i] = Eigen::Vector3d(RandomReal(-3000.0, -2000.0),
79  RandomReal(-4000.0, -3000.0),
80  RandomReal(-5000.0, -4000.0));
81  }
82 
83  // Robustly estimate transformation using RANSAC.
84  RANSACOptions options;
85  options.max_error = 10;
87  ransac(options);
88  const auto report = ransac.Estimate(src, dst);
89 
90  BOOST_CHECK_EQUAL(report.success, true);
91  BOOST_CHECK_GT(report.num_trials, 0);
92 
93  // Make sure outliers were detected correctly.
94  BOOST_CHECK_EQUAL(report.support.num_inliers, num_samples - num_outliers);
95  for (size_t i = 0; i < num_samples; ++i) {
96  if (i < num_outliers) {
97  BOOST_CHECK(!report.inlier_mask[i]);
98  } else {
99  BOOST_CHECK(report.inlier_mask[i]);
100  }
101  }
102 
103  // Make sure original transformation is estimated correctly.
104  const double matrix_diff =
105  (orig_tform.Matrix().topLeftCorner<3, 4>() - report.model).norm();
106  BOOST_CHECK(std::abs(matrix_diff) < 1e-6);
107 }
Report Estimate(const std::vector< typename Estimator::X_t > &X, const std::vector< typename Estimator::Y_t > &Y)
Definition: loransac.h:72
Eigen::Matrix4d Matrix() const
void TransformPoint(Eigen::Vector3d *xyz) const
const double * e
BOOST_AUTO_TEST_CASE(TestReport)
void SetPRNGSeed(unsigned seed)
Definition: random.cc:40
Eigen::Vector4d ComposeIdentityQuaternion()
Definition: pose.h:209
T RandomReal(const T min, const T max)
Definition: random.h:75