Point Cloud Library (PCL)  1.9.1-dev
transformation_estimation_dq.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  *
38  */
39 #ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_DQ_HPP_
40 #define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_DQ_HPP_
41 
42 #include <pcl/common/eigen.h>
43 
44 ///////////////////////////////////////////////////////////////////////////////////////////
45 template <typename PointSource, typename PointTarget, typename Scalar> inline void
47  const pcl::PointCloud<PointSource> &cloud_src,
48  const pcl::PointCloud<PointTarget> &cloud_tgt,
49  Matrix4 &transformation_matrix) const
50 {
51  std::size_t nr_points = cloud_src.points.size ();
52  if (cloud_tgt.points.size () != nr_points)
53  {
54  PCL_ERROR ("[pcl::TransformationEstimationDQ::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", nr_points, cloud_tgt.points.size ());
55  return;
56  }
57 
58  ConstCloudIterator<PointSource> source_it (cloud_src);
59  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
60  estimateRigidTransformation (source_it, target_it, transformation_matrix);
61 }
62 
63 ///////////////////////////////////////////////////////////////////////////////////////////
64 template <typename PointSource, typename PointTarget, typename Scalar> void
66  const pcl::PointCloud<PointSource> &cloud_src,
67  const std::vector<int> &indices_src,
68  const pcl::PointCloud<PointTarget> &cloud_tgt,
69  Matrix4 &transformation_matrix) const
70 {
71  if (indices_src.size () != cloud_tgt.points.size ())
72  {
73  PCL_ERROR ("[pcl::TransformationDQ::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", 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 () != indices_tgt.size ())
92  {
93  PCL_ERROR ("[pcl::TransformationEstimationDQ::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), indices_tgt.size ());
94  return;
95  }
96 
97  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
98  ConstCloudIterator<PointTarget> target_it (cloud_tgt, indices_tgt);
99  estimateRigidTransformation (source_it, target_it, transformation_matrix);
100 }
101 
102 ///////////////////////////////////////////////////////////////////////////////////////////
103 template <typename PointSource, typename PointTarget, typename Scalar> void
105  const pcl::PointCloud<PointSource> &cloud_src,
106  const pcl::PointCloud<PointTarget> &cloud_tgt,
107  const pcl::Correspondences &correspondences,
108  Matrix4 &transformation_matrix) const
109 {
110  ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true);
111  ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false);
112  estimateRigidTransformation (source_it, target_it, transformation_matrix);
113 }
114 
115 ///////////////////////////////////////////////////////////////////////////////////////////
116 template <typename PointSource, typename PointTarget, typename Scalar> inline void
120  Matrix4 &transformation_matrix) const
121 {
122  const int npts = static_cast <int> (source_it.size ());
123 
124  transformation_matrix.setIdentity ();
125 
126  // dual quaternion optimization
127  Eigen::Matrix<Scalar,4,4> C1 = Eigen::Matrix<Scalar,4,4>::Zero();
128  Eigen::Matrix<Scalar,4,4> C2 = Eigen::Matrix<Scalar,4,4>::Zero();
129  Scalar *c1 = C1.data();
130  Scalar *c2 = C2.data();
131 
132  for( int i=0; i<npts; i++ ) {
133  const PointSource &a = *source_it;
134  const PointTarget &b = *target_it;
135  const Scalar axbx = a.x*b.x;
136  const Scalar ayby = a.y*b.y;
137  const Scalar azbz = a.z*b.z;
138  const Scalar axby = a.x*b.y;
139  const Scalar aybx = a.y*b.x;
140  const Scalar axbz = a.x*b.z;
141  const Scalar azbx = a.z*b.x;
142  const Scalar aybz = a.y*b.z;
143  const Scalar azby = a.z*b.y;
144  c1[0] += axbx - azbz - ayby;
145  c1[5] += ayby - azbz - axbx;
146  c1[10]+= azbz - axbx - ayby;
147  c1[15]+= axbx + ayby + azbz;
148  c1[1] += axby + aybx;
149  c1[2] += axbz + azbx;
150  c1[3] += aybz - azby;
151  c1[6] += azby + aybz;
152  c1[7] += azbx - axbz;
153  c1[11]+= axby - aybx;
154 
155  c2[1] += a.z + b.z;
156  c2[2] -= a.y + b.y;
157  c2[3] += a.x - b.x;
158  c2[6] += a.x + b.x;
159  c2[7] += a.y - b.y;
160  c2[11]+= a.z - b.z;
161  source_it++;
162  target_it++;
163  }
164 
165  c1[4] = c1[1];
166  c1[8] = c1[2];
167  c1[9] = c1[6];
168  c1[12]= c1[3];
169  c1[13]= c1[7];
170  c1[14]= c1[11];
171  c2[4] = -c2[1];
172  c2[8] = -c2[2];
173  c2[12]= -c2[3];
174  c2[9] = -c2[6];
175  c2[13]= -c2[7];
176  c2[14]= -c2[11];
177 
178  C1 *= -2.0f;
179  C2 *= 2.0f;
180 
181  const Eigen::Matrix<Scalar,4,4> A = (0.25f/float(npts))*C2.transpose()*C2 - C1;
182 
183  const Eigen::EigenSolver< Eigen::Matrix<Scalar,4,4> > es(A);
184 
185  ptrdiff_t i;
186  es.eigenvalues().real().maxCoeff(&i);
187  const Eigen::Matrix<Scalar,4,1> qmat = es.eigenvectors().col(i).real();
188  const Eigen::Matrix<Scalar,4,1> smat = -(0.5f/float(npts))*C2*qmat;
189 
190  const Eigen::Quaternion<Scalar> q( qmat(3), qmat(0), qmat(1), qmat(2) );
191  const Eigen::Quaternion<Scalar> s( smat(3), smat(0), smat(1), smat(2) );
192 
193  const Eigen::Quaternion<Scalar> t = s*q.conjugate();
194 
195  const Eigen::Matrix<Scalar,3,3> R( q.toRotationMatrix() );
196 
197  for( int i=0; i<3; ++i )
198  for( int j=0; j<3; ++j)
199  transformation_matrix(i,j) = R(i,j);
200 
201  transformation_matrix(0,3) = -t.x();
202  transformation_matrix(1,3) = -t.y();
203  transformation_matrix(2,3) = -t.z();
204 }
205 
206 #endif /* PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_DQ_HPP_ */
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:394
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 using dual quatern...
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
std::size_t size() const
Size of the range the iterator is going through.