Point Cloud Library (PCL)  1.11.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 
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  const auto nr_points = cloud_src.size ();
55  if (cloud_tgt.size () != nr_points)
56  {
57  PCL_ERROR("[pcl::TransformationEstimation2D::estimateRigidTransformation] Number "
58  "or points in source (%zu) differs than target (%zu)!\n",
59  static_cast<std::size_t>(nr_points),
60  static_cast<std::size_t>(cloud_tgt.size()));
61  return;
62  }
63 
64  ConstCloudIterator<PointSource> source_it (cloud_src);
65  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
66  estimateRigidTransformation (source_it, target_it, transformation_matrix);
67 }
68 
69 
70 template <typename PointSource, typename PointTarget, typename Scalar> void
72  const pcl::PointCloud<PointSource> &cloud_src,
73  const std::vector<int> &indices_src,
74  const pcl::PointCloud<PointTarget> &cloud_tgt,
75  Matrix4 &transformation_matrix) const
76 {
77  if (indices_src.size () != cloud_tgt.size ())
78  {
79  PCL_ERROR("[pcl::Transformation2D::estimateRigidTransformation] Number or points "
80  "in source (%zu) differs than target (%zu)!\n",
81  indices_src.size(),
82  static_cast<std::size_t>(cloud_tgt.size()));
83  return;
84  }
85 
86  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
87  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
88  estimateRigidTransformation (source_it, target_it, transformation_matrix);
89 }
90 
91 
92 template <typename PointSource, typename PointTarget, typename Scalar> inline void
94  const pcl::PointCloud<PointSource> &cloud_src,
95  const std::vector<int> &indices_src,
96  const pcl::PointCloud<PointTarget> &cloud_tgt,
97  const std::vector<int> &indices_tgt,
98  Matrix4 &transformation_matrix) const
99 {
100  if (indices_src.size () != indices_tgt.size ())
101  {
102  PCL_ERROR ("[pcl::TransformationEstimation2D::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), indices_tgt.size ());
103  return;
104  }
105 
106  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
107  ConstCloudIterator<PointTarget> target_it (cloud_tgt, indices_tgt);
108  estimateRigidTransformation (source_it, target_it, transformation_matrix);
109 }
110 
111 
112 template <typename PointSource, typename PointTarget, typename Scalar> void
114  const pcl::PointCloud<PointSource> &cloud_src,
115  const pcl::PointCloud<PointTarget> &cloud_tgt,
116  const pcl::Correspondences &correspondences,
117  Matrix4 &transformation_matrix) const
118 {
119  ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true);
120  ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false);
121  estimateRigidTransformation (source_it, target_it, transformation_matrix);
122 }
123 
124 
125 template <typename PointSource, typename PointTarget, typename Scalar> inline void
129  Matrix4 &transformation_matrix) const
130 {
131  source_it.reset (); target_it.reset ();
132 
133  Eigen::Matrix<Scalar, 4, 1> centroid_src, centroid_tgt;
134  // Estimate the centroids of source, target
135  compute3DCentroid (source_it, centroid_src);
136  compute3DCentroid (target_it, centroid_tgt);
137  source_it.reset (); target_it.reset ();
138 
139  // ignore z component
140  centroid_src[2] = 0.0f;
141  centroid_tgt[2] = 0.0f;
142  // Subtract the centroids from source, target
143  Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> cloud_src_demean, cloud_tgt_demean;
144  demeanPointCloud (source_it, centroid_src, cloud_src_demean);
145  demeanPointCloud (target_it, centroid_tgt, cloud_tgt_demean);
146 
147  getTransformationFromCorrelation (cloud_src_demean, centroid_src, cloud_tgt_demean, centroid_tgt, transformation_matrix);
148 }
149 
150 
151 template <typename PointSource, typename PointTarget, typename Scalar> void
153  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_src_demean,
154  const Eigen::Matrix<Scalar, 4, 1> &centroid_src,
155  const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> &cloud_tgt_demean,
156  const Eigen::Matrix<Scalar, 4, 1> &centroid_tgt,
157  Matrix4 &transformation_matrix) const
158 {
159  transformation_matrix.setIdentity ();
160 
161  // Assemble the correlation matrix H = source * target'
162  Eigen::Matrix<Scalar, 3, 3> H = (cloud_src_demean * cloud_tgt_demean.transpose ()).topLeftCorner (3, 3);
163 
164  float angle = std::atan2 ((H (0, 1) - H (1, 0)), (H(0, 0) + H (1, 1)));
165 
166  Eigen::Matrix<Scalar, 3, 3> R (Eigen::Matrix<Scalar, 3, 3>::Identity ());
167  R (0, 0) = R (1, 1) = std::cos (angle);
168  R (0, 1) = -std::sin (angle);
169  R (1, 0) = std::sin (angle);
170 
171  // Return the correct transformation
172  transformation_matrix.topLeftCorner (3, 3).matrix () = R;
173  const Eigen::Matrix<Scalar, 3, 1> Rc (R * centroid_src.head (3).matrix ());
174  transformation_matrix.block (0, 3, 3, 1).matrix () = centroid_tgt.head (3) - Rc;
175 }
176 
177 } // namespace registration
178 } // namespace pcl
179 
180 #endif // PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_2D_HPP_
181 
Iterator class for point clouds with or without given indices.
void estimateRigidTransformation(const pcl::PointCloud< PointSource > &cloud_src, const std::vector< int > &indices_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.
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
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.
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
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