Point Cloud Library (PCL)  1.9.0-dev
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.points.size () != 3 || cloud_tgt.points.size () != 3)
51  {
52  PCL_ERROR ("[pcl::TransformationEstimation3Point::estimateRigidTransformation] Number of points in source (%lu) and target (%lu) must be 3!\n",
53  cloud_src.points.size (), cloud_tgt.points.size ());
54  return;
55  }
56 
57  ConstCloudIterator<PointSource> source_it (cloud_src);
58  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
59  estimateRigidTransformation (source_it, target_it, transformation_matrix);
60 }
61 
62 ///////////////////////////////////////////////////////////////////////////////////////////
63 template <typename PointSource, typename PointTarget, typename Scalar> void
65  const pcl::PointCloud<PointSource> &cloud_src,
66  const std::vector<int> &indices_src,
67  const pcl::PointCloud<PointTarget> &cloud_tgt,
68  Matrix4 &transformation_matrix) const
69 {
70  if (indices_src.size () != 3 || cloud_tgt.points.size () != 3)
71  {
72  PCL_ERROR ("[pcl::TransformationEstimation3Point::estimateRigidTransformation] Number of indices in source (%lu) and points in target (%lu) must be 3!\n",
73  indices_src.size (), cloud_tgt.points.size ());
74  return;
75  }
76 
77  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
78  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
79  estimateRigidTransformation (source_it, target_it, transformation_matrix);
80 }
81 
82 ///////////////////////////////////////////////////////////////////////////////////////////
83 template <typename PointSource, typename PointTarget, typename Scalar> inline void
85  const pcl::PointCloud<PointSource> &cloud_src,
86  const std::vector<int> &indices_src,
87  const pcl::PointCloud<PointTarget> &cloud_tgt,
88  const std::vector<int> &indices_tgt,
89  Matrix4 &transformation_matrix) const
90 {
91  if (indices_src.size () != 3 || indices_tgt.size () != 3)
92  {
93  PCL_ERROR ("[pcl::TransformationEstimation3Point::estimateRigidTransformation] Number of indices in source (%lu) and target (%lu) must be 3!\n",
94  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  if (correspondences.size () != 3)
112  {
113  PCL_ERROR ("[pcl::TransformationEstimation3Point::estimateRigidTransformation] Number of correspondences (%lu) must be 3!\n",
114  correspondences.size ());
115  return;
116  }
117 
118  ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true);
119  ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false);
120  estimateRigidTransformation (source_it, target_it, transformation_matrix);
121 }
122 
123 ///////////////////////////////////////////////////////////////////////////////////////////
124 template <typename PointSource, typename PointTarget, typename Scalar> inline void
128  Matrix4 &transformation_matrix) const
129 {
130  transformation_matrix.setIdentity ();
131  source_it.reset ();
132  target_it.reset ();
133 
134  Eigen::Matrix <Scalar, 4, 1> source_mean, target_mean;
135  pcl::compute3DCentroid (source_it, source_mean);
136  pcl::compute3DCentroid (target_it, target_mean);
137 
138  source_it.reset ();
139  target_it.reset ();
140 
141  Eigen::Matrix <Scalar, Eigen::Dynamic, Eigen::Dynamic> source_demean, target_demean;
142  pcl::demeanPointCloud (source_it, source_mean, source_demean, 3);
143  pcl::demeanPointCloud (target_it, target_mean, target_demean, 3);
144 
145  source_it.reset ();
146  target_it.reset ();
147 
148  Eigen::Matrix <Scalar, 3, 1> s1 = source_demean.col (1).head (3) - source_demean.col (0).head (3);
149  s1.normalize ();
150 
151  Eigen::Matrix <Scalar, 3, 1> s2 = source_demean.col (2).head (3) - source_demean.col (0).head (3);
152  s2 -= s2.dot (s1) * s1;
153  s2.normalize ();
154 
155  Eigen::Matrix <Scalar, 3, 3> source_rot;
156  source_rot.col (0) = s1;
157  source_rot.col (1) = s2;
158  source_rot.col (2) = s1.cross (s2);
159 
160  Eigen::Matrix <Scalar, 3, 1> t1 = target_demean.col (1).head (3) - target_demean.col (0).head (3);
161  t1.normalize ();
162 
163  Eigen::Matrix <Scalar, 3, 1> t2 = target_demean.col (2).head (3) - target_demean.col (0).head (3);
164  t2 -= t2.dot (t1) * t1;
165  t2.normalize ();
166 
167  Eigen::Matrix <Scalar, 3, 3> target_rot;
168  target_rot.col (0) = t1;
169  target_rot.col (1) = t2;
170  target_rot.col (2) = t1.cross (t2);
171 
172  //Eigen::Matrix <Scalar, 3, 3> R = source_rot * target_rot.transpose ();
173  Eigen::Matrix <Scalar, 3, 3> R = target_rot * source_rot.transpose ();
174  transformation_matrix.topLeftCorner (3, 3) = R;
175  //transformation_matrix.block (0, 3, 3, 1) = source_mean.head (3) - R * target_mean.head (3);
176  transformation_matrix.block (0, 3, 3, 1) = target_mean.head (3) - R * source_mean.head (3);
177 }
178 
179 //#define PCL_INSTANTIATE_TransformationEstimation3Point(T,U) template class PCL_EXPORTS pcl::registration::TransformationEstimation3Point<T,U>;
180 
181 #endif // PCL_REGISTRATION_IMPL_TRANSFORMATION_ESTIMATION_3POINT_H_
Iterator class for point clouds with or without given indices.
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
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
virtual void estimateRigidTransformation(const pcl::PointCloud< PointSource > &cloud_src, const pcl::PointCloud< PointTarget > &cloud_tgt, Matrix4 &transformation_matrix) const
Estimate a rigid rotation transformation between a source and a target point cloud.
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:50