Point Cloud Library (PCL)  1.14.1-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>
45 inline void
48  const pcl::PointCloud<PointTarget>& cloud_tgt,
49  Matrix4& transformation_matrix) const
50 {
51  if (cloud_src.size() != 3 || cloud_tgt.size() != 3) {
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>
66 void
69  const pcl::Indices& indices_src,
70  const pcl::PointCloud<PointTarget>& cloud_tgt,
71  Matrix4& transformation_matrix) const
72 {
73  if (indices_src.size() != 3 || cloud_tgt.size() != 3) {
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>
89 inline void
92  const pcl::Indices& indices_src,
93  const pcl::PointCloud<PointTarget>& cloud_tgt,
94  const pcl::Indices& indices_tgt,
95  Matrix4& transformation_matrix) const
96 {
97  if (indices_src.size() != 3 || indices_tgt.size() != 3) {
98  PCL_ERROR("[pcl::TransformationEstimation3Point::estimateRigidTransformation] "
99  "Number of indices in source (%lu) and target (%lu) must be 3!\n",
100  indices_src.size(),
101  indices_tgt.size());
102  return;
103  }
104 
105  ConstCloudIterator<PointSource> source_it(cloud_src, indices_src);
106  ConstCloudIterator<PointTarget> target_it(cloud_tgt, indices_tgt);
107  estimateRigidTransformation(source_it, target_it, transformation_matrix);
108 }
109 
110 ///////////////////////////////////////////////////////////////////////////////////////////
111 template <typename PointSource, typename PointTarget, typename Scalar>
112 void
115  const pcl::PointCloud<PointTarget>& cloud_tgt,
116  const pcl::Correspondences& correspondences,
117  Matrix4& transformation_matrix) const
118 {
119  if (correspondences.size() != 3) {
120  PCL_ERROR("[pcl::TransformationEstimation3Point::estimateRigidTransformation] "
121  "Number of correspondences (%lu) must be 3!\n",
122  correspondences.size());
123  return;
124  }
125 
126  ConstCloudIterator<PointSource> source_it(cloud_src, correspondences, true);
127  ConstCloudIterator<PointTarget> target_it(cloud_tgt, correspondences, false);
128  estimateRigidTransformation(source_it, target_it, transformation_matrix);
129 }
130 
131 ///////////////////////////////////////////////////////////////////////////////////////////
132 template <typename PointSource, typename PointTarget, typename Scalar>
133 inline void
137  Matrix4& transformation_matrix) const
138 {
139  transformation_matrix.setIdentity();
140  source_it.reset();
141  target_it.reset();
142 
143  Eigen::Matrix<Scalar, 4, 1> source_mean, target_mean;
144  pcl::compute3DCentroid(source_it, source_mean);
145  pcl::compute3DCentroid(target_it, target_mean);
146 
147  source_it.reset();
148  target_it.reset();
149 
150  Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> source_demean, target_demean;
151  pcl::demeanPointCloud(source_it, source_mean, source_demean, 3);
152  pcl::demeanPointCloud(target_it, target_mean, target_demean, 3);
153 
154  source_it.reset();
155  target_it.reset();
156 
157  Eigen::Matrix<Scalar, 3, 1> s1 =
158  source_demean.col(1).template head<3>() - source_demean.col(0).template head<3>();
159  s1.normalize();
160 
161  Eigen::Matrix<Scalar, 3, 1> s2 =
162  source_demean.col(2).template head<3>() - source_demean.col(0).template head<3>();
163  s2 -= s2.dot(s1) * s1;
164  s2.normalize();
165 
166  Eigen::Matrix<Scalar, 3, 3> source_rot;
167  source_rot.col(0) = s1;
168  source_rot.col(1) = s2;
169  source_rot.col(2) = s1.cross(s2);
170 
171  Eigen::Matrix<Scalar, 3, 1> t1 =
172  target_demean.col(1).template head<3>() - target_demean.col(0).template head<3>();
173  t1.normalize();
174 
175  Eigen::Matrix<Scalar, 3, 1> t2 =
176  target_demean.col(2).template head<3>() - target_demean.col(0).template head<3>();
177  t2 -= t2.dot(t1) * t1;
178  t2.normalize();
179 
180  Eigen::Matrix<Scalar, 3, 3> target_rot;
181  target_rot.col(0) = t1;
182  target_rot.col(1) = t2;
183  target_rot.col(2) = t1.cross(t2);
184 
185  // Eigen::Matrix <Scalar, 3, 3> R = source_rot * target_rot.transpose ();
186  Eigen::Matrix<Scalar, 3, 3> R = target_rot * source_rot.transpose();
187  transformation_matrix.template topLeftCorner<3, 3>() = R;
188  // transformation_matrix.block<3, 1>(0, 3) = source_mean.head<3>() - R *
189  // target_mean.head<3>();
190  transformation_matrix.template block<3, 1>(0, 3) =
191  target_mean.template head<3>() - R * source_mean.template head<3>();
192 }
193 
194 //#define PCL_INSTANTIATE_TransformationEstimation3Point(T,U) template class PCL_EXPORTS
195 // pcl::registration::TransformationEstimation3Point<T,U>;
196 
197 #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:443
void estimateRigidTransformation(const pcl::PointCloud< PointSource > &cloud_src, const pcl::Indices &indices_src, const pcl::PointCloud< PointTarget > &cloud_tgt, const pcl::Indices &indices_tgt, Matrix4 &transformation_matrix) const override
Estimate a rigid rotation transformation between a source and a target point cloud.
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.
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:933
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:57
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133