ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
similarity_transform.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Eigen/Core>
11 #include <Eigen/Geometry>
12 #include <vector>
13 
14 #include "base/projection.h"
15 #include "util/alignment.h"
16 #include "util/logging.h"
17 #include "util/types.h"
18 
19 namespace colmap {
20 
21 // N-D similarity transform estimator from corresponding point pairs in the
22 // source and destination coordinate systems.
23 //
24 // This algorithm is based on the following paper:
25 //
26 // S. Umeyama. Least-Squares Estimation of Transformation Parameters
27 // Between Two Point Patterns. IEEE Transactions on Pattern Analysis and
28 // Machine Intelligence, Volume 13 Issue 4, Page 376-380, 1991.
29 // http://www.stanford.edu/class/cs273/refs/umeyama.pdf
30 //
31 // and uses the Eigen implementation.
32 template <int kDim, bool kEstimateScale = true>
34 public:
35  typedef Eigen::Matrix<double, kDim, 1> X_t;
36  typedef Eigen::Matrix<double, kDim, 1> Y_t;
37  typedef Eigen::Matrix<double, kDim, kDim + 1> M_t;
38 
39  // The minimum number of samples needed to estimate a model. Note that
40  // this only returns the true minimal sample in the two-dimensional case.
41  // For higher dimensions, the system will alway be over-determined.
42  static const int kMinNumSamples = kDim;
43 
44  // Estimate the similarity transform.
45  //
46  // @param src Set of corresponding source points.
47  // @param dst Set of corresponding destination points.
48  //
49  // @return 4x4 homogeneous transformation matrix.
50  static std::vector<M_t> Estimate(const std::vector<X_t>& src,
51  const std::vector<Y_t>& dst);
52 
53  // Calculate the transformation error for each corresponding point pair.
54  //
55  // Residuals are defined as the squared transformation error when
56  // transforming the source to the destination coordinates.
57  //
58  // @param src Set of corresponding points in the source coordinate
59  // system as a Nx3 matrix.
60  // @param dst Set of corresponding points in the destination
61  // coordinate system as a Nx3 matrix.
62  // @param matrix 4x4 homogeneous transformation matrix.
63  // @param residuals Output vector of residuals for each point pair.
64  static void Residuals(const std::vector<X_t>& src,
65  const std::vector<Y_t>& dst,
66  const M_t& matrix,
67  std::vector<double>* residuals);
68 };
69 
71 // Implementation
73 
74 template <int kDim, bool kEstimateScale>
75 std::vector<typename SimilarityTransformEstimator<kDim, kEstimateScale>::M_t>
77  const std::vector<X_t>& src, const std::vector<Y_t>& dst) {
78  CHECK_EQ(src.size(), dst.size());
79 
80  Eigen::Matrix<double, kDim, Eigen::Dynamic> src_mat(kDim, src.size());
81  Eigen::Matrix<double, kDim, Eigen::Dynamic> dst_mat(kDim, dst.size());
82  for (size_t i = 0; i < src.size(); ++i) {
83  src_mat.col(i) = src[i];
84  dst_mat.col(i) = dst[i];
85  }
86 
87  const auto model = Eigen::umeyama(src_mat, dst_mat, kEstimateScale)
88  .topLeftCorner(kDim, kDim + 1);
89 
90  if (model.array().isNaN().any()) {
91  return std::vector<M_t>{};
92  }
93 
94  return {model};
95 }
96 
97 template <int kDim, bool kEstimateScale>
99  const std::vector<X_t>& src,
100  const std::vector<Y_t>& dst,
101  const M_t& matrix,
102  std::vector<double>* residuals) {
103  CHECK_EQ(src.size(), dst.size());
104 
105  residuals->resize(src.size());
106 
107  for (size_t i = 0; i < src.size(); ++i) {
108  const Y_t dst_transformed = matrix * src[i].homogeneous();
109  (*residuals)[i] = (dst[i] - dst_transformed).squaredNorm();
110  }
111 }
112 
113 } // namespace colmap
static void Residuals(const std::vector< X_t > &src, const std::vector< Y_t > &dst, const M_t &matrix, std::vector< double > *residuals)
Eigen::Matrix< double, kDim, 1 > X_t
Eigen::Matrix< double, kDim, 1 > Y_t
static std::vector< M_t > Estimate(const std::vector< X_t > &src, const std::vector< Y_t > &dst)
Eigen::Matrix< double, kDim, kDim+1 > M_t