Point Cloud Library (PCL)  1.14.0-dev
transformation_estimation_svd.hpp
1 /*
2  * Software License Agreement (BSD License)
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  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id$
38  *
39  */
40 
41 #ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_
42 #define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_
43 
44 #include <pcl/common/eigen.h>
45 
46 namespace pcl {
47 
48 namespace registration {
49 
50 template <typename PointSource, typename PointTarget, typename Scalar>
51 inline void
54  const pcl::PointCloud<PointTarget>& cloud_tgt,
55  Matrix4& transformation_matrix) const
56 {
57  const auto nr_points = cloud_src.size();
58  if (cloud_tgt.size() != nr_points) {
59  PCL_ERROR("[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number "
60  "or points in source (%zu) differs than target (%zu)!\n",
61  static_cast<std::size_t>(nr_points),
62  static_cast<std::size_t>(cloud_tgt.size()));
63  return;
64  }
65 
66  ConstCloudIterator<PointSource> source_it(cloud_src);
67  ConstCloudIterator<PointTarget> target_it(cloud_tgt);
68  estimateRigidTransformation(source_it, target_it, transformation_matrix);
69 }
70 
71 template <typename PointSource, typename PointTarget, typename Scalar>
72 void
75  const pcl::Indices& indices_src,
76  const pcl::PointCloud<PointTarget>& cloud_tgt,
77  Matrix4& transformation_matrix) const
78 {
79  if (indices_src.size() != cloud_tgt.size()) {
80  PCL_ERROR("[pcl::TransformationSVD::estimateRigidTransformation] Number or points "
81  "in source (%zu) differs than target (%zu)!\n",
82  indices_src.size(),
83  static_cast<std::size_t>(cloud_tgt.size()));
84  return;
85  }
86 
87  ConstCloudIterator<PointSource> source_it(cloud_src, indices_src);
88  ConstCloudIterator<PointTarget> target_it(cloud_tgt);
89  estimateRigidTransformation(source_it, target_it, transformation_matrix);
90 }
91 
92 template <typename PointSource, typename PointTarget, typename Scalar>
93 inline void
96  const pcl::Indices& indices_src,
97  const pcl::PointCloud<PointTarget>& cloud_tgt,
98  const pcl::Indices& indices_tgt,
99  Matrix4& transformation_matrix) const
100 {
101  if (indices_src.size() != indices_tgt.size()) {
102  PCL_ERROR("[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number "
103  "or points in source (%zu) differs than target (%zu)!\n",
104  indices_src.size(),
105  indices_tgt.size());
106  return;
107  }
108 
109  ConstCloudIterator<PointSource> source_it(cloud_src, indices_src);
110  ConstCloudIterator<PointTarget> target_it(cloud_tgt, indices_tgt);
111  estimateRigidTransformation(source_it, target_it, transformation_matrix);
112 }
113 
114 template <typename PointSource, typename PointTarget, typename Scalar>
115 void
118  const pcl::PointCloud<PointTarget>& cloud_tgt,
119  const pcl::Correspondences& correspondences,
120  Matrix4& transformation_matrix) const
121 {
122  ConstCloudIterator<PointSource> source_it(cloud_src, correspondences, true);
123  ConstCloudIterator<PointTarget> target_it(cloud_tgt, correspondences, false);
124  estimateRigidTransformation(source_it, target_it, transformation_matrix);
125 }
126 
127 template <typename PointSource, typename PointTarget, typename Scalar>
128 inline void
132  Matrix4& transformation_matrix) const
133 {
134  // Convert to Eigen format
135  const int npts = static_cast<int>(source_it.size());
136 
137  if (use_umeyama_) {
138  Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_src(3, npts);
139  Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_tgt(3, npts);
140 
141  for (int i = 0; i < npts; ++i) {
142  cloud_src(0, i) = source_it->x;
143  cloud_src(1, i) = source_it->y;
144  cloud_src(2, i) = source_it->z;
145  ++source_it;
146 
147  cloud_tgt(0, i) = target_it->x;
148  cloud_tgt(1, i) = target_it->y;
149  cloud_tgt(2, i) = target_it->z;
150  ++target_it;
151  }
152 
153  // Call Umeyama directly from Eigen (PCL patched version until Eigen is released)
154  transformation_matrix = pcl::umeyama(cloud_src, cloud_tgt, false);
155  }
156  else {
157  source_it.reset();
158  target_it.reset();
159  // <cloud_src,cloud_src> is the source dataset
160  transformation_matrix.setIdentity();
161 
162  Eigen::Matrix<Scalar, 4, 1> centroid_src, centroid_tgt;
163  // Estimate the centroids of source, target
164  compute3DCentroid(source_it, centroid_src);
165  compute3DCentroid(target_it, centroid_tgt);
166  source_it.reset();
167  target_it.reset();
168 
169  // Subtract the centroids from source, target
170  Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> cloud_src_demean,
171  cloud_tgt_demean;
172  demeanPointCloud(source_it, centroid_src, cloud_src_demean);
173  demeanPointCloud(target_it, centroid_tgt, cloud_tgt_demean);
174 
175  getTransformationFromCorrelation(cloud_src_demean,
176  centroid_src,
177  cloud_tgt_demean,
178  centroid_tgt,
179  transformation_matrix);
180  }
181 }
182 
183 template <typename PointSource, typename PointTarget, typename Scalar>
184 void
187  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& cloud_src_demean,
188  const Eigen::Matrix<Scalar, 4, 1>& centroid_src,
189  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& cloud_tgt_demean,
190  const Eigen::Matrix<Scalar, 4, 1>& centroid_tgt,
191  Matrix4& transformation_matrix) const
192 {
193  transformation_matrix.setIdentity();
194 
195  // Assemble the correlation matrix H = source * target'
196  Eigen::Matrix<Scalar, 3, 3> H =
197  (cloud_src_demean * cloud_tgt_demean.transpose()).template topLeftCorner<3, 3>();
198 
199  // Compute the Singular Value Decomposition
200  Eigen::JacobiSVD<Eigen::Matrix<Scalar, 3, 3>> svd(
201  H, Eigen::ComputeFullU | Eigen::ComputeFullV);
202  Eigen::Matrix<Scalar, 3, 3> u = svd.matrixU();
203  Eigen::Matrix<Scalar, 3, 3> v = svd.matrixV();
204 
205  // Compute R = V * U'
206  if (u.determinant() * v.determinant() < 0) {
207  for (int x = 0; x < 3; ++x)
208  v(x, 2) *= -1;
209  }
210 
211  Eigen::Matrix<Scalar, 3, 3> R = v * u.transpose();
212 
213  // Return the correct transformation
214  transformation_matrix.template topLeftCorner<3, 3>() = R;
215  const Eigen::Matrix<Scalar, 3, 1> Rc(R * centroid_src.template head<3>());
216  transformation_matrix.template block<3, 1>(0, 3) =
217  centroid_tgt.template head<3>() - Rc;
218 
220  size_t N = cloud_src_demean.cols();
221  PCL_DEBUG("[pcl::registration::TransformationEstimationSVD::"
222  "getTransformationFromCorrelation] Loss: %.10e\n",
223  (cloud_tgt_demean - R * cloud_src_demean).squaredNorm() / N);
224  }
225 }
226 
227 } // namespace registration
228 } // namespace pcl
229 
230 //#define PCL_INSTANTIATE_TransformationEstimationSVD(T,U) template class PCL_EXPORTS
231 // pcl::registration::TransformationEstimationSVD<T,U>;
232 
233 #endif /* PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_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.
std::size_t size() const
Definition: point_cloud.h:443
typename TransformationEstimation< PointSource, PointTarget, Scalar >::Matrix4 Matrix4
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, Matrix4 &transformation_matrix) const
Obtain a 4x4 rigid transformation matrix from a correlation matrix H = src.
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.
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
unsigned int compute3DCentroid(ConstCloudIterator< PointT > &cloud_iterator, Eigen::Matrix< Scalar, 4, 1 > &centroid)
Compute the 3D (X-Y-Z) centroid of a set of points and return it as a 3D vector.
Definition: centroid.hpp:57
PCL_EXPORTS bool isVerbosityLevelEnabled(VERBOSITY_LEVEL severity)
is verbosity level enabled?
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
Eigen::internal::umeyama_transform_matrix_type< Derived, OtherDerived >::type umeyama(const Eigen::MatrixBase< Derived > &src, const Eigen::MatrixBase< OtherDerived > &dst, bool with_scaling=false)
Returns the transformation between two point sets.
Definition: eigen.hpp:670
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133