Point Cloud Library (PCL) 1.15.1-dev
Loading...
Searching...
No Matches
transformation_estimation_point_to_point_robust.hpp
1/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Point Cloud Library (PCL) - www.pointclouds.org
5 * Copyright (c) 2010, Willow Garage, Inc.
6 * Copyright (c) 2012-, Open Perception, Inc.
7 *
8 * All rights reserved.
9 */
10
11#ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_POINT_TO_POINT_ROBUST_HPP_
12#define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_POINT_TO_POINT_ROBUST_HPP_
13
14#include <pcl/common/eigen.h>
15
16#include <limits>
17
18namespace pcl {
19
20namespace registration {
21
22template <typename PointSource, typename PointTarget, typename Scalar>
23inline void
26 const pcl::PointCloud<PointTarget>& cloud_tgt,
27 Matrix4& transformation_matrix) const
28{
29 const auto nr_points = cloud_src.size();
30 if (cloud_tgt.size() != nr_points) {
31 PCL_ERROR("[pcl::TransformationEstimationPointToPointRobust::"
32 "estimateRigidTransformation] Number "
33 "of points in source (%zu) differs than target (%zu)!\n",
34 static_cast<std::size_t>(nr_points),
35 static_cast<std::size_t>(cloud_tgt.size()));
36 return;
37 }
38
39 ConstCloudIterator<PointSource> source_it(cloud_src);
40 ConstCloudIterator<PointTarget> target_it(cloud_tgt);
41 estimateRigidTransformation(source_it, target_it, transformation_matrix);
42}
43
44template <typename PointSource, typename PointTarget, typename Scalar>
45void
48 const pcl::Indices& indices_src,
49 const pcl::PointCloud<PointTarget>& cloud_tgt,
50 Matrix4& transformation_matrix) const
51{
52 if (indices_src.size() != cloud_tgt.size()) {
53 PCL_ERROR("[pcl::TransformationEstimationPointToPointRobust::"
54 "estimateRigidTransformation] Number of points "
55 "in source (%zu) differs than target (%zu)!\n",
56 indices_src.size(),
57 static_cast<std::size_t>(cloud_tgt.size()));
58 return;
59 }
60
61 ConstCloudIterator<PointSource> source_it(cloud_src, indices_src);
62 ConstCloudIterator<PointTarget> target_it(cloud_tgt);
63 estimateRigidTransformation(source_it, target_it, transformation_matrix);
64}
65
66template <typename PointSource, typename PointTarget, typename Scalar>
67inline void
70 const pcl::Indices& indices_src,
71 const pcl::PointCloud<PointTarget>& cloud_tgt,
72 const pcl::Indices& indices_tgt,
73 Matrix4& transformation_matrix) const
74{
75 if (indices_src.size() != indices_tgt.size()) {
76 PCL_ERROR("[pcl::TransformationEstimationPointToPointRobust::"
77 "estimateRigidTransformation] Number of points "
78 "in source (%zu) differs than target (%zu)!\n",
79 indices_src.size(),
80 indices_tgt.size());
81 return;
82 }
83
84 ConstCloudIterator<PointSource> source_it(cloud_src, indices_src);
85 ConstCloudIterator<PointTarget> target_it(cloud_tgt, indices_tgt);
86 estimateRigidTransformation(source_it, target_it, transformation_matrix);
87}
88
89template <typename PointSource, typename PointTarget, typename Scalar>
90void
93 const pcl::PointCloud<PointTarget>& cloud_tgt,
94 const pcl::Correspondences& correspondences,
95 Matrix4& transformation_matrix) const
96{
97 ConstCloudIterator<PointSource> source_it(cloud_src, correspondences, true);
98 ConstCloudIterator<PointTarget> target_it(cloud_tgt, correspondences, false);
99 estimateRigidTransformation(source_it, target_it, transformation_matrix);
100}
101
102template <typename PointSource, typename PointTarget, typename Scalar>
103inline void
107 Matrix4& transformation_matrix) const
108{
109 // Convert to Eigen format
110 const int npts = static_cast<int>(source_it.size());
111 if (npts == 0) {
112 PCL_ERROR("[pcl::TransformationEstimationPointToPointRobust::"
113 "estimateRigidTransformation] Empty correspondence set.\n");
114 return;
115 }
116 source_it.reset();
117 target_it.reset();
118 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> weights(npts);
119 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> square_distances(npts);
120 for (int i = 0; i < npts; i++) {
121 Scalar dx = source_it->x - target_it->x;
122 Scalar dy = source_it->y - target_it->y;
123 Scalar dz = source_it->z - target_it->z;
124 Scalar dist2 = dx * dx + dy * dy + dz * dz;
125 square_distances[i] = dist2;
126
127 source_it++;
128 target_it++;
129 }
130
131 const Scalar epsilon = std::numeric_limits<Scalar>::epsilon();
132 Scalar sigma2;
133 if (sigma_ < 0)
134 sigma2 = std::max(square_distances.maxCoeff() / Scalar(9.0), epsilon);
135 else
136 sigma2 = std::max(sigma_ * sigma_, epsilon);
137
138 for (int i = 0; i < npts; i++) {
139 weights[i] = std::exp(-square_distances[i] / (Scalar(2.0) * sigma2));
140 }
141 const Scalar weights_sum = weights.sum();
142 if (weights_sum > epsilon)
143 weights /= weights_sum;
144 else
145 weights.setConstant(Scalar(1.0) / static_cast<Scalar>(npts));
146
147 source_it.reset();
148 target_it.reset();
149 // <cloud_src,cloud_src> is the source dataset
150 transformation_matrix.setIdentity();
151
152 Eigen::Matrix<Scalar, 4, 1> centroid_src, centroid_tgt;
153 // Estimate the centroids of source, target
154 computeWeighted3DCentroid(source_it, weights, centroid_src);
155 computeWeighted3DCentroid(target_it, weights, centroid_tgt);
156 source_it.reset();
157 target_it.reset();
158
159 // Subtract the centroids from source, target
160 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> cloud_src_demean,
161 cloud_tgt_demean;
162 demeanPointCloud(source_it, centroid_src, cloud_src_demean);
163 demeanPointCloud(target_it, centroid_tgt, cloud_tgt_demean);
164
165 getTransformationFromCorrelation(cloud_src_demean,
166 centroid_src,
167 cloud_tgt_demean,
168 centroid_tgt,
169 weights,
170 transformation_matrix);
171}
172
173template <typename PointSource, typename PointTarget, typename Scalar>
174void
177 const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& cloud_src_demean,
178 const Eigen::Matrix<Scalar, 4, 1>& centroid_src,
179 const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& cloud_tgt_demean,
180 const Eigen::Matrix<Scalar, 4, 1>& centroid_tgt,
181 const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>& weights,
182 Matrix4& transformation_matrix) const
183{
184 transformation_matrix.setIdentity();
185
186 // Assemble the correlation matrix H = source * weights * target'
187 Eigen::Matrix<Scalar, 3, 3> H =
188 (cloud_src_demean * weights.asDiagonal() * cloud_tgt_demean.transpose())
189 .template topLeftCorner<3, 3>();
190
191 // Compute the Singular Value Decomposition
192 Eigen::JacobiSVD<Eigen::Matrix<Scalar, 3, 3>> svd(
193 H, Eigen::ComputeFullU | Eigen::ComputeFullV);
194 Eigen::Matrix<Scalar, 3, 3> u = svd.matrixU();
195 Eigen::Matrix<Scalar, 3, 3> v = svd.matrixV();
196
197 // Compute R = V * U'
198 if (u.determinant() * v.determinant() < 0) {
199 for (int x = 0; x < 3; ++x)
200 v(x, 2) *= -1;
201 }
202
203 Eigen::Matrix<Scalar, 3, 3> R = v * u.transpose();
204
205 // Return the correct transformation
206 transformation_matrix.template topLeftCorner<3, 3>() = R;
207 const Eigen::Matrix<Scalar, 3, 1> Rc(R * centroid_src.template head<3>());
208 transformation_matrix.template block<3, 1>(0, 3) =
209 centroid_tgt.template head<3>() - Rc;
210
212 const auto N = cloud_src_demean.cols();
213 if (N > 0) {
214 PCL_DEBUG("[pcl::registration::TransformationEstimationPointToPointRobust::"
215 "getTransformationFromCorrelation] Loss: %.10e\n",
216 (cloud_tgt_demean.template topRows<3>() -
217 R * cloud_src_demean.template topRows<3>())
218 .squaredNorm() /
219 static_cast<double>(N));
220 }
221 }
222}
223
224template <typename PointSource, typename PointTarget, typename Scalar>
225template <typename PointT>
226inline unsigned int
229 const Eigen::Matrix<Scalar, Eigen::Dynamic, 1>& weights,
230 Eigen::Matrix<Scalar, 4, 1>& centroid) const
231{
232 Eigen::Matrix<Scalar, 4, 1> accumulator{0, 0, 0, 0};
233
234 unsigned int cp = 0;
235 Scalar sum_weights = Scalar(0);
236 Eigen::Index weight_idx = 0;
237
238 // For each point in the cloud
239 // If the data is dense, we don't need to check for NaN
240 while (cloud_iterator.isValid()) {
241 // Check if the point is invalid
242 if (pcl::isFinite(*cloud_iterator)) {
243 const Scalar w = weights[weight_idx];
244 accumulator[0] += w * cloud_iterator->x;
245 accumulator[1] += w * cloud_iterator->y;
246 accumulator[2] += w * cloud_iterator->z;
247 sum_weights += w;
248 ++cp;
249 }
250 ++weight_idx;
251 ++cloud_iterator;
252 }
253
254 if (cp > 0 && sum_weights > std::numeric_limits<Scalar>::epsilon()) {
255 centroid = accumulator / sum_weights;
256 centroid[3] = 1;
257 }
258 return (cp);
259}
260
261} // namespace registration
262} // namespace pcl
263
264#endif /* PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_POINT_TO_POINT_ROBUST_HPP_ */
Iterator class for point clouds with or without given indices.
std::size_t size() const
Size of the range the iterator is going through.
PointCloud represents the base class in PCL for storing collections of 3D points.
std::size_t size() const
typename TransformationEstimation< PointSource, PointTarget, Scalar >::Matrix4 Matrix4
void estimateRigidTransformation(const pcl::PointCloud< PointSource > &cloud_src, const pcl::PointCloud< PointTarget > &cloud_tgt, Matrix4 &transformation_matrix) const override
Estimate a rigid rotation transformation between a source and a target point cloud using SVD.
virtual void getTransformationFromCorrelation(const Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &cloud_src_demean, const Eigen::Matrix< Scalar, 4, 1 > &centroid_src, const Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > &cloud_tgt_demean, const Eigen::Matrix< Scalar, 4, 1 > &centroid_tgt, const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &weights, Matrix4 &transformation_matrix) const
Obtain a 4x4 rigid transformation matrix from a correlation matrix H = src.
unsigned int computeWeighted3DCentroid(ConstCloudIterator< PointT > &cloud_iterator, const Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > &weights, Eigen::Matrix< Scalar, 4, 1 > &centroid) const
Compute the 3D (X-Y-Z) centroid of a set of weighted points and return it as a 3D vector.
void demeanPointCloud(ConstCloudIterator< PointT > &cloud_iterator, const Eigen::Matrix< Scalar, 4, 1 > &centroid, pcl::PointCloud< PointT > &cloud_out, int npts=0)
Subtract a centroid from a point cloud and return the de-meaned representation.
Definition centroid.hpp:933
PCL_EXPORTS bool isVerbosityLevelEnabled(VERBOSITY_LEVEL severity)
is verbosity level enabled?
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition point_tests.h:56
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition types.h:133