Point Cloud Library (PCL)  1.7.1
transformation_estimation_2D.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 #ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_2D_HPP_
38 #define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_2D_HPP_
39 
40 ///////////////////////////////////////////////////////////////////////////////////////////
41 template <typename PointSource, typename PointTarget, typename Scalar> inline void
43  const pcl::PointCloud<PointSource> &cloud_src,
44  const pcl::PointCloud<PointTarget> &cloud_tgt,
45  Matrix4 &transformation_matrix) const
46 {
47  size_t nr_points = cloud_src.points.size ();
48  if (cloud_tgt.points.size () != nr_points)
49  {
50  PCL_ERROR ("[pcl::TransformationEstimation2D::estimateRigidTransformation] Number or points in source (%zu) differs than target (%zu)!\n", nr_points, cloud_tgt.points.size ());
51  return;
52  }
53 
54  ConstCloudIterator<PointSource> source_it (cloud_src);
55  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
56  estimateRigidTransformation (source_it, target_it, transformation_matrix);
57 }
58 
59 ///////////////////////////////////////////////////////////////////////////////////////////
60 template <typename PointSource, typename PointTarget, typename Scalar> void
62  const pcl::PointCloud<PointSource> &cloud_src,
63  const std::vector<int> &indices_src,
64  const pcl::PointCloud<PointTarget> &cloud_tgt,
65  Matrix4 &transformation_matrix) const
66 {
67  if (indices_src.size () != cloud_tgt.points.size ())
68  {
69  PCL_ERROR ("[pcl::Transformation2D::estimateRigidTransformation] Number or points in source (%zu) differs than target (%zu)!\n", indices_src.size (), cloud_tgt.points.size ());
70  return;
71  }
72 
73  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
74  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
75  estimateRigidTransformation (source_it, target_it, transformation_matrix);
76 }
77 
78 
79 ///////////////////////////////////////////////////////////////////////////////////////////
80 template <typename PointSource, typename PointTarget, typename Scalar> inline void
82  const pcl::PointCloud<PointSource> &cloud_src,
83  const std::vector<int> &indices_src,
84  const pcl::PointCloud<PointTarget> &cloud_tgt,
85  const std::vector<int> &indices_tgt,
86  Matrix4 &transformation_matrix) const
87 {
88  if (indices_src.size () != indices_tgt.size ())
89  {
90  PCL_ERROR ("[pcl::TransformationEstimation2D::estimateRigidTransformation] Number or points in source (%zu) differs than target (%zu)!\n", indices_src.size (), indices_tgt.size ());
91  return;
92  }
93 
94  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
95  ConstCloudIterator<PointTarget> target_it (cloud_tgt, indices_tgt);
96  estimateRigidTransformation (source_it, target_it, transformation_matrix);
97 }
98 
99 ///////////////////////////////////////////////////////////////////////////////////////////
100 template <typename PointSource, typename PointTarget, typename Scalar> void
102  const pcl::PointCloud<PointSource> &cloud_src,
103  const pcl::PointCloud<PointTarget> &cloud_tgt,
104  const pcl::Correspondences &correspondences,
105  Matrix4 &transformation_matrix) const
106 {
107  ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true);
108  ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false);
109  estimateRigidTransformation (source_it, target_it, transformation_matrix);
110 }
111 
112 ///////////////////////////////////////////////////////////////////////////////////////////
113 template <typename PointSource, typename PointTarget, typename Scalar> inline void
117  Matrix4 &transformation_matrix) const
118 {
119  source_it.reset (); target_it.reset ();
120 
121  Eigen::Matrix<Scalar, 4, 1> centroid_src, centroid_tgt;
122  // Estimate the centroids of source, target
123  compute3DCentroid (source_it, centroid_src);
124  compute3DCentroid (target_it, centroid_tgt);
125  source_it.reset (); target_it.reset ();
126 
127  // ignore z component
128  centroid_src[2] = 0.0f;
129  centroid_tgt[2] = 0.0f;
130  // Subtract the centroids from source, target
131  Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> cloud_src_demean, cloud_tgt_demean;
132  demeanPointCloud (source_it, centroid_src, cloud_src_demean);
133  demeanPointCloud (target_it, centroid_tgt, cloud_tgt_demean);
134 
135  getTransformationFromCorrelation (cloud_src_demean, centroid_src, cloud_tgt_demean, centroid_tgt, transformation_matrix);
136 }
137 
138 ///////////////////////////////////////////////////////////////////////////////////////////
139 template <typename PointSource, typename PointTarget, typename Scalar> void
141  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_src_demean,
142  const Eigen::Matrix<Scalar, 4, 1> &centroid_src,
143  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_tgt_demean,
144  const Eigen::Matrix<Scalar, 4, 1> &centroid_tgt,
145  Matrix4 &transformation_matrix) const
146 {
147  transformation_matrix.setIdentity ();
148 
149  // Assemble the correlation matrix H = source * target'
150  Eigen::Matrix<Scalar, 3, 3> H = (cloud_src_demean * cloud_tgt_demean.transpose ()).topLeftCorner (3, 3);
151 
152  float angle = atan2 ((H (0, 1) - H (1, 0)), (H(0, 0) + H (1, 1)));
153 
154  Eigen::Matrix<Scalar, 3, 3> R (Eigen::Matrix<Scalar, 3, 3>::Identity ());
155  R (0, 0) = R (1, 1) = cos (angle);
156  R (0, 1) = -sin (angle);
157  R (1, 0) = sin (angle);
158 
159  // Return the correct transformation
160  transformation_matrix.topLeftCorner (3, 3).matrix () = R;
161  const Eigen::Matrix<Scalar, 3, 1> Rc (R * centroid_src.head (3).matrix ());
162  transformation_matrix.block (0, 3, 3, 1).matrix () = centroid_tgt.head (3) - Rc;
163 }
164 
165 #endif // PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_2D_HPP_