ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
similarity_transform_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 "base/similarity_transform"
33 #include "util/testing.h"
34 
35 #include <Eigen/Core>
36 
37 #include "base/pose.h"
39 
40 #include <fstream>
41 
42 using namespace colmap;
43 
44 BOOST_AUTO_TEST_CASE(TestDefaultInitialization) {
45  const SimilarityTransform3 tform;
46 
47  BOOST_CHECK_EQUAL(tform.Scale(), 1);
48 
49  BOOST_CHECK_EQUAL(tform.Rotation()[0], 1);
50  BOOST_CHECK_EQUAL(tform.Rotation()[1], 0);
51  BOOST_CHECK_EQUAL(tform.Rotation()[2], 0);
52  BOOST_CHECK_EQUAL(tform.Rotation()[3], 0);
53 
54  BOOST_CHECK_EQUAL(tform.Translation()[0], 0);
55  BOOST_CHECK_EQUAL(tform.Translation()[1], 0);
56  BOOST_CHECK_EQUAL(tform.Translation()[2], 0);
57 }
58 
59 BOOST_AUTO_TEST_CASE(TestInitialization) {
60  const Eigen::Vector4d qvec =
61  NormalizeQuaternion(Eigen::Vector4d(0.1, 0.3, 0.2, 0.4));
62 
63  const SimilarityTransform3 tform(2, qvec, Eigen::Vector3d(100, 10, 0.5));
64 
65  BOOST_CHECK_CLOSE(tform.Scale(), 2, 1e-10);
66 
67  BOOST_CHECK_CLOSE(tform.Rotation()[0], qvec(0), 1e-10);
68  BOOST_CHECK_CLOSE(tform.Rotation()[1], qvec(1), 1e-10);
69  BOOST_CHECK_CLOSE(tform.Rotation()[2], qvec(2), 1e-10);
70  BOOST_CHECK_CLOSE(tform.Rotation()[3], qvec(3), 1e-10);
71 
72  BOOST_CHECK_CLOSE(tform.Translation()[0], 100, 1e-10);
73  BOOST_CHECK_CLOSE(tform.Translation()[1], 10, 1e-10);
74  BOOST_CHECK_CLOSE(tform.Translation()[2], 0.5, 1e-10);
75 }
76 
77 void TestEstimationWithNumCoords(const size_t num_coords) {
78  const SimilarityTransform3 orig_tform(2, Eigen::Vector4d(0.1, 0.3, 0.2, 0.4),
79  Eigen::Vector3d(100, 10, 0.5));
80 
81  std::vector<Eigen::Vector3d> src;
82  std::vector<Eigen::Vector3d> dst;
83 
84  for (size_t i = 0; i < num_coords; ++i) {
85  src.emplace_back(i, i + 2, i * i);
86  dst.push_back(src.back());
87  orig_tform.TransformPoint(&dst.back());
88  }
89 
90  SimilarityTransform3 est_tform;
91  BOOST_CHECK(est_tform.Estimate(src, dst));
92 
93  BOOST_CHECK((orig_tform.Matrix() - est_tform.Matrix()).norm() < 1e-6);
94 
95  std::vector<Eigen::Vector3d> invalid_src_dst(3, Eigen::Vector3d::Zero());
96  BOOST_CHECK(!est_tform.Estimate(invalid_src_dst, invalid_src_dst));
97 }
98 
99 BOOST_AUTO_TEST_CASE(TestEstimation) {
102 }
103 
104 BOOST_AUTO_TEST_CASE(TestFromFile) {
105  // Create transform file
106  const std::string path = "test_from_file_transform.txt";
107  {
108  std::ofstream out(path);
109  out << "0.0 2.0 0.0 3.0 0.0 0.0 2.0 4.0 2.0 0.0 0.0 5.0 0.0 0.0 0.0 1.0"
110  << std::endl;
111  }
113  BOOST_CHECK_CLOSE(tform.Scale(), 2.0, 1e-10);
114  BOOST_CHECK_LE((tform.Translation() - Eigen::Vector3d(3.0, 4.0, 5.0)).norm(),
115  1e-6);
116  BOOST_CHECK_LE(
117  (tform.Rotation() - Eigen::Vector4d(-0.5, 0.5, 0.5, 0.5)).norm(), 1e-6);
118 }
static SimilarityTransform3 FromFile(const std::string &path)
bool Estimate(const std::vector< Eigen::Vector3d > &src, const std::vector< Eigen::Vector3d > &dst)
Eigen::Matrix4d Matrix() const
Eigen::Vector4d Rotation() const
void TransformPoint(Eigen::Vector3d *xyz) const
Eigen::Vector3d Translation() const
const double * e
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
static const std::string path
Definition: PointCloud.cpp:59
Eigen::Vector4d NormalizeQuaternion(const Eigen::Vector4d &qvec)
Definition: pose.cc:82
BOOST_AUTO_TEST_CASE(TestDefaultInitialization)
void TestEstimationWithNumCoords(const size_t num_coords)