Point Cloud Library (PCL)  1.11.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 
38 #ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_2D_HPP_
39 #define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_2D_HPP_
40 
41 
42 namespace pcl
43 {
44 
45 namespace registration
46 {
47 
48 template <typename PointSource, typename PointTarget, typename Scalar> inline void
50  const pcl::PointCloud<PointSource> &cloud_src,
51  const pcl::PointCloud<PointTarget> &cloud_tgt,
52  Matrix4 &transformation_matrix) const
53 {
54  std::size_t nr_points = cloud_src.points.size ();
55  if (cloud_tgt.points.size () != nr_points)
56  {
57  PCL_ERROR ("[pcl::TransformationEstimation2D::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", nr_points, cloud_tgt.points.size ());
58  return;
59  }
60 
61  ConstCloudIterator<PointSource> source_it (cloud_src);
62  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
63  estimateRigidTransformation (source_it, target_it, transformation_matrix);
64 }
65 
66 
67 template <typename PointSource, typename PointTarget, typename Scalar> void
69  const pcl::PointCloud<PointSource> &cloud_src,
70  const std::vector<int> &indices_src,
71  const pcl::PointCloud<PointTarget> &cloud_tgt,
72  Matrix4 &transformation_matrix) const
73 {
74  if (indices_src.size () != cloud_tgt.points.size ())
75  {
76  PCL_ERROR ("[pcl::Transformation2D::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), cloud_tgt.points.size ());
77  return;
78  }
79 
80  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
81  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
82  estimateRigidTransformation (source_it, target_it, transformation_matrix);
83 }
84 
85 
86 template <typename PointSource, typename PointTarget, typename Scalar> inline void
88  const pcl::PointCloud<PointSource> &cloud_src,
89  const std::vector<int> &indices_src,
90  const pcl::PointCloud<PointTarget> &cloud_tgt,
91  const std::vector<int> &indices_tgt,
92  Matrix4 &transformation_matrix) const
93 {
94  if (indices_src.size () != indices_tgt.size ())
95  {
96  PCL_ERROR ("[pcl::TransformationEstimation2D::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), indices_tgt.size ());
97  return;
98  }
99 
100  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
101  ConstCloudIterator<PointTarget> target_it (cloud_tgt, indices_tgt);
102  estimateRigidTransformation (source_it, target_it, transformation_matrix);
103 }
104 
105 
106 template <typename PointSource, typename PointTarget, typename Scalar> void
108  const pcl::PointCloud<PointSource> &cloud_src,
109  const pcl::PointCloud<PointTarget> &cloud_tgt,
110  const pcl::Correspondences &correspondences,
111  Matrix4 &transformation_matrix) const
112 {
113  ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true);
114  ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false);
115  estimateRigidTransformation (source_it, target_it, transformation_matrix);
116 }
117 
118 
119 template <typename PointSource, typename PointTarget, typename Scalar> inline void
123  Matrix4 &transformation_matrix) const
124 {
125  source_it.reset (); target_it.reset ();
126 
127  Eigen::Matrix<Scalar, 4, 1> centroid_src, centroid_tgt;
128  // Estimate the centroids of source, target
129  compute3DCentroid (source_it, centroid_src);
130  compute3DCentroid (target_it, centroid_tgt);
131  source_it.reset (); target_it.reset ();
132 
133  // ignore z component
134  centroid_src[2] = 0.0f;
135  centroid_tgt[2] = 0.0f;
136  // Subtract the centroids from source, target
137  Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> cloud_src_demean, cloud_tgt_demean;
138  demeanPointCloud (source_it, centroid_src, cloud_src_demean);
139  demeanPointCloud (target_it, centroid_tgt, cloud_tgt_demean);
140 
141  getTransformationFromCorrelation (cloud_src_demean, centroid_src, cloud_tgt_demean, centroid_tgt, transformation_matrix);
142 }
143 
144 
145 template <typename PointSource, typename PointTarget, typename Scalar> void
147  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_src_demean,
148  const Eigen::Matrix<Scalar, 4, 1> &centroid_src,
149  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_tgt_demean,
150  const Eigen::Matrix<Scalar, 4, 1> &centroid_tgt,
151  Matrix4 &transformation_matrix) const
152 {
153  transformation_matrix.setIdentity ();
154 
155  // Assemble the correlation matrix H = source * target'
156  Eigen::Matrix<Scalar, 3, 3> H = (cloud_src_demean * cloud_tgt_demean.transpose ()).topLeftCorner (3, 3);
157 
158  float angle = std::atan2 ((H (0, 1) - H (1, 0)), (H(0, 0) + H (1, 1)));
159 
160  Eigen::Matrix<Scalar, 3, 3> R (Eigen::Matrix<Scalar, 3, 3>::Identity ());
161  R (0, 0) = R (1, 1) = std::cos (angle);
162  R (0, 1) = -std::sin (angle);
163  R (1, 0) = std::sin (angle);
164 
165  // Return the correct transformation
166  transformation_matrix.topLeftCorner (3, 3).matrix () = R;
167  const Eigen::Matrix<Scalar, 3, 1> Rc (R * centroid_src.head (3).matrix ());
168  transformation_matrix.block (0, 3, 3, 1).matrix () = centroid_tgt.head (3) - Rc;
169 }
170 
171 } // namespace registration
172 } // namespace pcl
173 
174 #endif // PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_2D_HPP_
175 
void estimateRigidTransformation(const pcl::PointCloud< PointSource > &cloud_src, const pcl::PointCloud< PointTarget > &cloud_tgt, Matrix4 &transformation_matrix) const
Estimate a rigid transformation between a source and a target point cloud in 2D.
Iterator class for point clouds with or without given indices.
virtual void estimateRigidTransformation(const pcl::PointCloud< PointSource > &cloud_src, const std::vector< int > &indices_src, const pcl::PointCloud< PointTarget > &cloud_tgt, const std::vector< int > &indices_tgt, Matrix4 &transformation_matrix) const
Estimate a rigid transformation between a source and a target point cloud in 2D.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
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'.
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:631
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
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