ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
translation_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 "util/alignment.h"
15 #include "util/logging.h"
16 #include "util/types.h"
17 
18 namespace colmap {
19 
20 // Estimate a N-D translation transformation between point pairs.
21 template <int kDim>
23 public:
24  typedef Eigen::Matrix<double, kDim, 1> X_t;
25  typedef Eigen::Matrix<double, kDim, 1> Y_t;
26  typedef Eigen::Matrix<double, kDim, 1> M_t;
27 
28  // The minimum number of samples needed to estimate a model.
29  static const int kMinNumSamples = 1;
30 
31  // Estimate the 2D translation transform.
32  //
33  // @param points1 Set of corresponding source 2D points.
34  // @param points2 Set of corresponding destination 2D points.
35  //
36  // @return Translation vector.
37  static std::vector<M_t> Estimate(const std::vector<X_t>& points1,
38  const std::vector<Y_t>& points2);
39 
40  // Calculate the squared translation error.
41  //
42  // @param points1 Set of corresponding source 2D points.
43  // @param points2 Set of corresponding destination 2D points.
44  // @param translation Translation vector.
45  // @param residuals Output vector of residuals for each point pair.
46  static void Residuals(const std::vector<X_t>& points1,
47  const std::vector<Y_t>& points2,
48  const M_t& translation,
49  std::vector<double>* residuals);
50 };
51 
53 // Implementation
55 
56 template <int kDim>
57 std::vector<typename TranslationTransformEstimator<kDim>::M_t>
58 TranslationTransformEstimator<kDim>::Estimate(const std::vector<X_t>& points1,
59  const std::vector<Y_t>& points2) {
60  CHECK_EQ(points1.size(), points2.size());
61 
62  X_t mean_src = X_t::Zero();
63  Y_t mean_dst = Y_t::Zero();
64 
65  for (size_t i = 0; i < points1.size(); ++i) {
66  mean_src += points1[i];
67  mean_dst += points2[i];
68  }
69 
70  mean_src /= points1.size();
71  mean_dst /= points2.size();
72 
73  std::vector<M_t> models(1);
74  models[0] = mean_dst - mean_src;
75 
76  return models;
77 }
78 
79 template <int kDim>
81  const std::vector<X_t>& points1,
82  const std::vector<Y_t>& points2,
83  const M_t& translation,
84  std::vector<double>* residuals) {
85  CHECK_EQ(points1.size(), points2.size());
86 
87  residuals->resize(points1.size());
88 
89  for (size_t i = 0; i < points1.size(); ++i) {
90  const M_t diff = points2[i] - points1[i] - translation;
91  (*residuals)[i] = diff.squaredNorm();
92  }
93 }
94 
95 } // namespace colmap
Eigen::Matrix< double, kDim, 1 > X_t
Eigen::Matrix< double, kDim, 1 > Y_t
static void Residuals(const std::vector< X_t > &points1, const std::vector< Y_t > &points2, const M_t &translation, std::vector< double > *residuals)
Eigen::Matrix< double, kDim, 1 > M_t
static std::vector< M_t > Estimate(const std::vector< X_t > &points1, const std::vector< Y_t > &points2)