Point Cloud Library (PCL)  1.11.1
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 
47 namespace pcl
48 {
49 
50 namespace registration
51 {
52 
53 template <typename PointSource, typename PointTarget, typename Scalar> inline void
55  const pcl::PointCloud<PointSource> &cloud_src,
56  const pcl::PointCloud<PointTarget> &cloud_tgt,
57  Matrix4 &transformation_matrix) const
58 {
59  const auto nr_points = cloud_src.size ();
60  if (cloud_tgt.size () != nr_points)
61  {
62  PCL_ERROR("[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number "
63  "or points in source (%zu) differs than target (%zu)!\n",
64  static_cast<std::size_t>(nr_points),
65  static_cast<std::size_t>(cloud_tgt.size()));
66  return;
67  }
68 
69  ConstCloudIterator<PointSource> source_it (cloud_src);
70  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
71  estimateRigidTransformation (source_it, target_it, transformation_matrix);
72 }
73 
74 
75 template <typename PointSource, typename PointTarget, typename Scalar> void
77  const pcl::PointCloud<PointSource> &cloud_src,
78  const std::vector<int> &indices_src,
79  const pcl::PointCloud<PointTarget> &cloud_tgt,
80  Matrix4 &transformation_matrix) const
81 {
82  if (indices_src.size () != cloud_tgt.size ())
83  {
84  PCL_ERROR("[pcl::TransformationSVD::estimateRigidTransformation] Number or points "
85  "in source (%zu) differs than target (%zu)!\n",
86  indices_src.size(),
87  static_cast<std::size_t>(cloud_tgt.size()));
88  return;
89  }
90 
91  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
92  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
93  estimateRigidTransformation (source_it, target_it, transformation_matrix);
94 }
95 
96 
97 template <typename PointSource, typename PointTarget, typename Scalar> inline void
99  const pcl::PointCloud<PointSource> &cloud_src,
100  const std::vector<int> &indices_src,
101  const pcl::PointCloud<PointTarget> &cloud_tgt,
102  const std::vector<int> &indices_tgt,
103  Matrix4 &transformation_matrix) const
104 {
105  if (indices_src.size () != indices_tgt.size ())
106  {
107  PCL_ERROR("[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number "
108  "or points in source (%zu) differs than target (%zu)!\n",
109  indices_src.size(),
110  indices_tgt.size());
111  return;
112  }
113 
114  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
115  ConstCloudIterator<PointTarget> target_it (cloud_tgt, indices_tgt);
116  estimateRigidTransformation (source_it, target_it, transformation_matrix);
117 }
118 
119 
120 template <typename PointSource, typename PointTarget, typename Scalar> void
122  const pcl::PointCloud<PointSource> &cloud_src,
123  const pcl::PointCloud<PointTarget> &cloud_tgt,
124  const pcl::Correspondences &correspondences,
125  Matrix4 &transformation_matrix) const
126 {
127  ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true);
128  ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false);
129  estimateRigidTransformation (source_it, target_it, transformation_matrix);
130 }
131 
132 
133 template <typename PointSource, typename PointTarget, typename Scalar> inline void
137  Matrix4 &transformation_matrix) const
138 {
139  // Convert to Eigen format
140  const int npts = static_cast <int> (source_it.size ());
141 
142 
143 
144  if (use_umeyama_)
145  {
146  Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_src (3, npts);
147  Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_tgt (3, npts);
148 
149  for (int i = 0; i < npts; ++i)
150  {
151  cloud_src (0, i) = source_it->x;
152  cloud_src (1, i) = source_it->y;
153  cloud_src (2, i) = source_it->z;
154  ++source_it;
155 
156  cloud_tgt (0, i) = target_it->x;
157  cloud_tgt (1, i) = target_it->y;
158  cloud_tgt (2, i) = target_it->z;
159  ++target_it;
160  }
161 
162  // Call Umeyama directly from Eigen (PCL patched version until Eigen is released)
163  transformation_matrix = pcl::umeyama (cloud_src, cloud_tgt, false);
164  }
165  else
166  {
167  source_it.reset (); target_it.reset ();
168  // <cloud_src,cloud_src> is the source dataset
169  transformation_matrix.setIdentity ();
170 
171  Eigen::Matrix<Scalar, 4, 1> centroid_src, centroid_tgt;
172  // Estimate the centroids of source, target
173  compute3DCentroid (source_it, centroid_src);
174  compute3DCentroid (target_it, centroid_tgt);
175  source_it.reset (); target_it.reset ();
176 
177  // Subtract the centroids from source, target
178  Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> cloud_src_demean, cloud_tgt_demean;
179  demeanPointCloud (source_it, centroid_src, cloud_src_demean);
180  demeanPointCloud (target_it, centroid_tgt, cloud_tgt_demean);
181 
182  getTransformationFromCorrelation (cloud_src_demean, centroid_src, cloud_tgt_demean, centroid_tgt, transformation_matrix);
183  }
184 }
185 
186 
187 template <typename PointSource, typename PointTarget, typename Scalar> void
189  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_src_demean,
190  const Eigen::Matrix<Scalar, 4, 1> &centroid_src,
191  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_tgt_demean,
192  const Eigen::Matrix<Scalar, 4, 1> &centroid_tgt,
193  Matrix4 &transformation_matrix) const
194 {
195  transformation_matrix.setIdentity ();
196 
197  // Assemble the correlation matrix H = source * target'
198  Eigen::Matrix<Scalar, 3, 3> H = (cloud_src_demean * cloud_tgt_demean.transpose ()).topLeftCorner (3, 3);
199 
200  // Compute the Singular Value Decomposition
201  Eigen::JacobiSVD<Eigen::Matrix<Scalar, 3, 3> > svd (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  {
208  for (int x = 0; x < 3; ++x)
209  v (x, 2) *= -1;
210  }
211 
212  Eigen::Matrix<Scalar, 3, 3> R = v * u.transpose ();
213 
214  // Return the correct transformation
215  transformation_matrix.topLeftCorner (3, 3) = R;
216  const Eigen::Matrix<Scalar, 3, 1> Rc (R * centroid_src.head (3));
217  transformation_matrix.block (0, 3, 3, 1) = centroid_tgt.head (3) - Rc;
218 }
219 
220 } // namespace registration
221 } // namespace pcl
222 
223 //#define PCL_INSTANTIATE_TransformationEstimationSVD(T,U) template class PCL_EXPORTS pcl::registration::TransformationEstimationSVD<T,U>;
224 
225 #endif /* PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_ */
226 
Iterator class for point clouds with or without given indices.
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...
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:660
std::size_t size() const
Definition: point_cloud.h:459
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:627
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 * tgt&#39;.
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
std::size_t size() const
Size of the range the iterator is going through.
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:56