Point Cloud Library (PCL)  1.11.1
transformation_estimation_3point.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2014-, 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_IMPL_TRANSFORMATION_ESTIMATION_3POINT_H_
38 #define PCL_REGISTRATION_IMPL_TRANSFORMATION_ESTIMATION_3POINT_H_
39 
40 #include <pcl/common/eigen.h>
41 #include <pcl/registration/transformation_estimation_3point.h>
42 
43 ///////////////////////////////////////////////////////////////////////////////////////////
44 template <typename PointSource, typename PointTarget, typename Scalar> inline void
46  const pcl::PointCloud<PointSource> &cloud_src,
47  const pcl::PointCloud<PointTarget> &cloud_tgt,
48  Matrix4 &transformation_matrix) const
49 {
50  if (cloud_src.size () != 3 || cloud_tgt.size () != 3)
51  {
52  PCL_ERROR("[pcl::TransformationEstimation3Point::estimateRigidTransformation] "
53  "Number of points in source (%zu) and target (%zu) must be 3!\n",
54  static_cast<std::size_t>(cloud_src.size()),
55  static_cast<std::size_t>(cloud_tgt.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 () != 3 || cloud_tgt.size () != 3)
73  {
74  PCL_ERROR(
75  "[pcl::TransformationEstimation3Point::estimateRigidTransformation] Number of "
76  "indices in source (%zu) and points in target (%zu) must be 3!\n",
77  indices_src.size(),
78  static_cast<std::size_t>(cloud_tgt.size()));
79  return;
80  }
81 
82  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
83  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
84  estimateRigidTransformation (source_it, target_it, transformation_matrix);
85 }
86 
87 ///////////////////////////////////////////////////////////////////////////////////////////
88 template <typename PointSource, typename PointTarget, typename Scalar> inline void
90  const pcl::PointCloud<PointSource> &cloud_src,
91  const std::vector<int> &indices_src,
92  const pcl::PointCloud<PointTarget> &cloud_tgt,
93  const std::vector<int> &indices_tgt,
94  Matrix4 &transformation_matrix) const
95 {
96  if (indices_src.size () != 3 || indices_tgt.size () != 3)
97  {
98  PCL_ERROR ("[pcl::TransformationEstimation3Point::estimateRigidTransformation] Number of indices in source (%lu) and target (%lu) must be 3!\n",
99  indices_src.size (), indices_tgt.size ());
100  return;
101  }
102 
103  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
104  ConstCloudIterator<PointTarget> target_it (cloud_tgt, indices_tgt);
105  estimateRigidTransformation (source_it, target_it, transformation_matrix);
106 }
107 
108 ///////////////////////////////////////////////////////////////////////////////////////////
109 template <typename PointSource, typename PointTarget, typename Scalar> void
111  const pcl::PointCloud<PointSource> &cloud_src,
112  const pcl::PointCloud<PointTarget> &cloud_tgt,
113  const pcl::Correspondences &correspondences,
114  Matrix4 &transformation_matrix) const
115 {
116  if (correspondences.size () != 3)
117  {
118  PCL_ERROR ("[pcl::TransformationEstimation3Point::estimateRigidTransformation] Number of correspondences (%lu) must be 3!\n",
119  correspondences.size ());
120  return;
121  }
122 
123  ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true);
124  ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false);
125  estimateRigidTransformation (source_it, target_it, transformation_matrix);
126 }
127 
128 ///////////////////////////////////////////////////////////////////////////////////////////
129 template <typename PointSource, typename PointTarget, typename Scalar> inline void
133  Matrix4 &transformation_matrix) const
134 {
135  transformation_matrix.setIdentity ();
136  source_it.reset ();
137  target_it.reset ();
138 
139  Eigen::Matrix <Scalar, 4, 1> source_mean, target_mean;
140  pcl::compute3DCentroid (source_it, source_mean);
141  pcl::compute3DCentroid (target_it, target_mean);
142 
143  source_it.reset ();
144  target_it.reset ();
145 
146  Eigen::Matrix <Scalar, Eigen::Dynamic, Eigen::Dynamic> source_demean, target_demean;
147  pcl::demeanPointCloud (source_it, source_mean, source_demean, 3);
148  pcl::demeanPointCloud (target_it, target_mean, target_demean, 3);
149 
150  source_it.reset ();
151  target_it.reset ();
152 
153  Eigen::Matrix <Scalar, 3, 1> s1 = source_demean.col (1).head (3) - source_demean.col (0).head (3);
154  s1.normalize ();
155 
156  Eigen::Matrix <Scalar, 3, 1> s2 = source_demean.col (2).head (3) - source_demean.col (0).head (3);
157  s2 -= s2.dot (s1) * s1;
158  s2.normalize ();
159 
160  Eigen::Matrix <Scalar, 3, 3> source_rot;
161  source_rot.col (0) = s1;
162  source_rot.col (1) = s2;
163  source_rot.col (2) = s1.cross (s2);
164 
165  Eigen::Matrix <Scalar, 3, 1> t1 = target_demean.col (1).head (3) - target_demean.col (0).head (3);
166  t1.normalize ();
167 
168  Eigen::Matrix <Scalar, 3, 1> t2 = target_demean.col (2).head (3) - target_demean.col (0).head (3);
169  t2 -= t2.dot (t1) * t1;
170  t2.normalize ();
171 
172  Eigen::Matrix <Scalar, 3, 3> target_rot;
173  target_rot.col (0) = t1;
174  target_rot.col (1) = t2;
175  target_rot.col (2) = t1.cross (t2);
176 
177  //Eigen::Matrix <Scalar, 3, 3> R = source_rot * target_rot.transpose ();
178  Eigen::Matrix <Scalar, 3, 3> R = target_rot * source_rot.transpose ();
179  transformation_matrix.topLeftCorner (3, 3) = R;
180  //transformation_matrix.block (0, 3, 3, 1) = source_mean.head (3) - R * target_mean.head (3);
181  transformation_matrix.block (0, 3, 3, 1) = target_mean.head (3) - R * source_mean.head (3);
182 }
183 
184 //#define PCL_INSTANTIATE_TransformationEstimation3Point(T,U) template class PCL_EXPORTS pcl::registration::TransformationEstimation3Point<T,U>;
185 
186 #endif // PCL_REGISTRATION_IMPL_TRANSFORMATION_ESTIMATION_3POINT_H_
Iterator class for point clouds with or without given indices.
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 override
Estimate a rigid rotation transformation between a source and a target point cloud.
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