Point Cloud Library (PCL)  1.7.0
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 #ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_
41 #define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_
42 
43 #include <pcl/common/eigen.h>
44 
45 ///////////////////////////////////////////////////////////////////////////////////////////
46 template <typename PointSource, typename PointTarget, typename Scalar> inline void
48  const pcl::PointCloud<PointSource> &cloud_src,
49  const pcl::PointCloud<PointTarget> &cloud_tgt,
50  Matrix4 &transformation_matrix) const
51 {
52  size_t nr_points = cloud_src.points.size ();
53  if (cloud_tgt.points.size () != nr_points)
54  {
55  PCL_ERROR ("[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number or points in source (%zu) differs than target (%zu)!\n", nr_points, cloud_tgt.points.size ());
56  return;
57  }
58 
59  ConstCloudIterator<PointSource> source_it (cloud_src);
60  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
61  estimateRigidTransformation (source_it, target_it, transformation_matrix);
62 }
63 
64 ///////////////////////////////////////////////////////////////////////////////////////////
65 template <typename PointSource, typename PointTarget, typename Scalar> void
67  const pcl::PointCloud<PointSource> &cloud_src,
68  const std::vector<int> &indices_src,
69  const pcl::PointCloud<PointTarget> &cloud_tgt,
70  Matrix4 &transformation_matrix) const
71 {
72  if (indices_src.size () != cloud_tgt.points.size ())
73  {
74  PCL_ERROR ("[pcl::TransformationSVD::estimateRigidTransformation] Number or points in source (%zu) differs than target (%zu)!\n", indices_src.size (), cloud_tgt.points.size ());
75  return;
76  }
77 
78  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
79  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
80  estimateRigidTransformation (source_it, target_it, transformation_matrix);
81 }
82 
83 ///////////////////////////////////////////////////////////////////////////////////////////
84 template <typename PointSource, typename PointTarget, typename Scalar> inline void
86  const pcl::PointCloud<PointSource> &cloud_src,
87  const std::vector<int> &indices_src,
88  const pcl::PointCloud<PointTarget> &cloud_tgt,
89  const std::vector<int> &indices_tgt,
90  Matrix4 &transformation_matrix) const
91 {
92  if (indices_src.size () != indices_tgt.size ())
93  {
94  PCL_ERROR ("[pcl::TransformationEstimationSVD::estimateRigidTransformation] Number or points in source (%zu) differs than target (%zu)!\n", indices_src.size (), indices_tgt.size ());
95  return;
96  }
97 
98  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
99  ConstCloudIterator<PointTarget> target_it (cloud_tgt, indices_tgt);
100  estimateRigidTransformation (source_it, target_it, transformation_matrix);
101 }
102 
103 ///////////////////////////////////////////////////////////////////////////////////////////
104 template <typename PointSource, typename PointTarget, typename Scalar> void
106  const pcl::PointCloud<PointSource> &cloud_src,
107  const pcl::PointCloud<PointTarget> &cloud_tgt,
108  const pcl::Correspondences &correspondences,
109  Matrix4 &transformation_matrix) const
110 {
111  ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true);
112  ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false);
113  estimateRigidTransformation (source_it, target_it, transformation_matrix);
114 }
115 
116 ///////////////////////////////////////////////////////////////////////////////////////////
117 template <typename PointSource, typename PointTarget, typename Scalar> inline void
121  Matrix4 &transformation_matrix) const
122 {
123  // Convert to Eigen format
124  const int npts = static_cast <int> (source_it.size ());
125 
126  Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_src (3, npts);
127  Eigen::Matrix<Scalar, 3, Eigen::Dynamic> cloud_tgt (3, npts);
128 
129  for (int i = 0; i < npts; ++i)
130  {
131  cloud_src (0, i) = source_it->x;
132  cloud_src (1, i) = source_it->y;
133  cloud_src (2, i) = source_it->z;
134  ++source_it;
135 
136  cloud_tgt (0, i) = target_it->x;
137  cloud_tgt (1, i) = target_it->y;
138  cloud_tgt (2, i) = target_it->z;
139  ++target_it;
140  }
141 
142  if (use_umeyama_)
143  {
144  // Call Umeyama directly from Eigen (PCL patched version until Eigen is released)
145  transformation_matrix = pcl::umeyama (cloud_src, cloud_tgt, false);
146  }
147  else
148  {
149  source_it.reset (); target_it.reset ();
150  // <cloud_src,cloud_src> is the source dataset
151  transformation_matrix.setIdentity ();
152 
153  Eigen::Matrix<Scalar, 4, 1> centroid_src, centroid_tgt;
154  // Estimate the centroids of source, target
155  compute3DCentroid (source_it, centroid_src);
156  compute3DCentroid (target_it, centroid_tgt);
157  source_it.reset (); target_it.reset ();
158 
159  // Subtract the centroids from source, target
160  Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> cloud_src_demean, cloud_tgt_demean;
161  demeanPointCloud (source_it, centroid_src, cloud_src_demean);
162  demeanPointCloud (target_it, centroid_tgt, cloud_tgt_demean);
163 
164  getTransformationFromCorrelation (cloud_src_demean, centroid_src, cloud_tgt_demean, centroid_tgt, transformation_matrix);
165  }
166 }
167 
168 ///////////////////////////////////////////////////////////////////////////////////////////
169 template <typename PointSource, typename PointTarget, typename Scalar> void
171  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_src_demean,
172  const Eigen::Matrix<Scalar, 4, 1> &centroid_src,
173  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_tgt_demean,
174  const Eigen::Matrix<Scalar, 4, 1> &centroid_tgt,
175  Matrix4 &transformation_matrix) const
176 {
177  transformation_matrix.setIdentity ();
178 
179  // Assemble the correlation matrix H = source * target'
180  Eigen::Matrix<Scalar, 3, 3> H = (cloud_src_demean * cloud_tgt_demean.transpose ()).topLeftCorner (3, 3);
181 
182  // Compute the Singular Value Decomposition
183  Eigen::JacobiSVD<Eigen::Matrix<Scalar, 3, 3> > svd (H, Eigen::ComputeFullU | Eigen::ComputeFullV);
184  Eigen::Matrix<Scalar, 3, 3> u = svd.matrixU ();
185  Eigen::Matrix<Scalar, 3, 3> v = svd.matrixV ();
186 
187  // Compute R = V * U'
188  if (u.determinant () * v.determinant () < 0)
189  {
190  for (int x = 0; x < 3; ++x)
191  v (x, 2) *= -1;
192  }
193 
194  Eigen::Matrix<Scalar, 3, 3> R = v * u.transpose ();
195 
196  // Return the correct transformation
197  transformation_matrix.topLeftCorner (3, 3) = R;
198  const Eigen::Matrix<Scalar, 3, 1> Rc (R * centroid_src.head (3));
199  transformation_matrix.block (0, 3, 3, 1) = centroid_tgt.head (3) - Rc;
200 }
201 
202 //#define PCL_INSTANTIATE_TransformationEstimationSVD(T,U) template class PCL_EXPORTS pcl::registration::TransformationEstimationSVD<T,U>;
203 
204 #endif /* PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_SVD_HPP_ */