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 #ifndef COLMAP_SRC_BASE_SIMILARITY_TRANSFORM_H_
33 #define COLMAP_SRC_BASE_SIMILARITY_TRANSFORM_H_
34 
35 #include <vector>
36 
37 #include <Eigen/Core>
38 #include <Eigen/Geometry>
39 
40 #include "util/alignment.h"
41 #include "util/types.h"
42 
43 namespace colmap {
44 
45 struct RANSACOptions;
46 class Reconstruction;
47 
48 // 3D similarity transformation with 7 degrees of freedom.
49 class SimilarityTransform3 {
50  public:
51   EIGEN_MAKE_ALIGNED_OPERATOR_NEW
52 
53   SimilarityTransform3();
54 
55   explicit SimilarityTransform3(const Eigen::Matrix3x4d& matrix);
56 
57   explicit SimilarityTransform3(
58       const Eigen::Transform<double, 3, Eigen::Affine>& transform);
59 
60   SimilarityTransform3(const double scale, const Eigen::Vector4d& qvec,
61                        const Eigen::Vector3d& tvec);
62 
63   bool Estimate(const std::vector<Eigen::Vector3d>& src,
64                 const std::vector<Eigen::Vector3d>& dst);
65 
66   SimilarityTransform3 Inverse() const;
67 
68   void TransformPoint(Eigen::Vector3d* xyz) const;
69   void TransformPose(Eigen::Vector4d* qvec, Eigen::Vector3d* tvec) const;
70 
71   Eigen::Matrix4d Matrix() const;
72   double Scale() const;
73   Eigen::Vector4d Rotation() const;
74   Eigen::Vector3d Translation() const;
75 
76  private:
77   Eigen::Transform<double, 3, Eigen::Affine> transform_;
78 };
79 
80 // Robustly compute alignment between reconstructions by finding images that
81 // are registered in both reconstructions. The alignment is then estimated
82 // robustly inside RANSAC from corresponding projection centers. An alignment
83 // is verified by reprojecting common 3D point observations.
84 // The min_inlier_observations threshold determines how many observations
85 // in a common image must reproject within the given threshold.
86 bool ComputeAlignmentBetweenReconstructions(
87     const Reconstruction& src_reconstruction,
88     const Reconstruction& ref_reconstruction,
89     const double min_inlier_observations, const double max_reproj_error,
90     Eigen::Matrix3x4d* alignment);
91 
92 }  // namespace colmap
93 
94 EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION_CUSTOM(colmap::SimilarityTransform3)
95 
96 #endif  // COLMAP_SRC_BASE_SIMILARITY_TRANSFORM_H_
97