Point Cloud Library (PCL)  1.11.0
transformation_estimation_dual_quaternion.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 
40 #ifndef PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_DQ_HPP_
41 #define PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_DQ_HPP_
42 
43 #include <pcl/common/eigen.h>
44 
45 
46 namespace pcl
47 {
48 
49 namespace registration
50 {
51 
52 template <typename PointSource, typename PointTarget, typename Scalar> inline void
54  const pcl::PointCloud<PointSource> &cloud_src,
55  const pcl::PointCloud<PointTarget> &cloud_tgt,
56  Matrix4 &transformation_matrix) const
57 {
58  std::size_t nr_points = cloud_src.points.size ();
59  if (cloud_tgt.points.size () != nr_points)
60  {
61  PCL_ERROR ("[pcl::TransformationEstimationDualQuaternion::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", nr_points, cloud_tgt.points.size ());
62  return;
63  }
64 
65  ConstCloudIterator<PointSource> source_it (cloud_src);
66  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
67  estimateRigidTransformation (source_it, target_it, transformation_matrix);
68 }
69 
70 
71 template <typename PointSource, typename PointTarget, typename Scalar> void
73  const pcl::PointCloud<PointSource> &cloud_src,
74  const std::vector<int> &indices_src,
75  const pcl::PointCloud<PointTarget> &cloud_tgt,
76  Matrix4 &transformation_matrix) const
77 {
78  if (indices_src.size () != cloud_tgt.points.size ())
79  {
80  PCL_ERROR ("[pcl::TransformationDQ::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), cloud_tgt.points.size ());
81  return;
82  }
83 
84  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
85  ConstCloudIterator<PointTarget> target_it (cloud_tgt);
86  estimateRigidTransformation (source_it, target_it, transformation_matrix);
87 }
88 
89 
90 template <typename PointSource, typename PointTarget, typename Scalar> inline void
92  const pcl::PointCloud<PointSource> &cloud_src,
93  const std::vector<int> &indices_src,
94  const pcl::PointCloud<PointTarget> &cloud_tgt,
95  const std::vector<int> &indices_tgt,
96  Matrix4 &transformation_matrix) const
97 {
98  if (indices_src.size () != indices_tgt.size ())
99  {
100  PCL_ERROR ("[pcl::TransformationEstimationDualQuaternion::estimateRigidTransformation] Number or points in source (%lu) differs than target (%lu)!\n", indices_src.size (), indices_tgt.size ());
101  return;
102  }
103 
104  ConstCloudIterator<PointSource> source_it (cloud_src, indices_src);
105  ConstCloudIterator<PointTarget> target_it (cloud_tgt, indices_tgt);
106  estimateRigidTransformation (source_it, target_it, transformation_matrix);
107 }
108 
109 
110 template <typename PointSource, typename PointTarget, typename Scalar> void
112  const pcl::PointCloud<PointSource> &cloud_src,
113  const pcl::PointCloud<PointTarget> &cloud_tgt,
114  const pcl::Correspondences &correspondences,
115  Matrix4 &transformation_matrix) const
116 {
117  ConstCloudIterator<PointSource> source_it (cloud_src, correspondences, true);
118  ConstCloudIterator<PointTarget> target_it (cloud_tgt, correspondences, false);
119  estimateRigidTransformation (source_it, target_it, transformation_matrix);
120 }
121 
122 
123 template <typename PointSource, typename PointTarget, typename Scalar> inline void
127  Matrix4 &transformation_matrix) const
128 {
129  const int npts = static_cast<int> (source_it.size ());
130 
131  transformation_matrix.setIdentity ();
132 
133  // dual quaternion optimization
134  Eigen::Matrix<double, 4, 4> C1 = Eigen::Matrix<double, 4, 4>::Zero ();
135  Eigen::Matrix<double, 4, 4> C2 = Eigen::Matrix<double, 4, 4>::Zero ();
136  double* c1 = C1.data ();
137  double* c2 = C2.data ();
138 
139  for (int i = 0; i < npts; ++i)
140  {
141  const PointSource& a = *source_it;
142  const PointTarget& b = *target_it;
143  const double axbx = a.x * b.x;
144  const double ayby = a.y * b.y;
145  const double azbz = a.z * b.z;
146  const double axby = a.x * b.y;
147  const double aybx = a.y * b.x;
148  const double axbz = a.x * b.z;
149  const double azbx = a.z * b.x;
150  const double aybz = a.y * b.z;
151  const double azby = a.z * b.y;
152  c1[0] += axbx - azbz - ayby;
153  c1[5] += ayby - azbz - axbx;
154  c1[10] += azbz - axbx - ayby;
155  c1[15] += axbx + ayby + azbz;
156  c1[1] += axby + aybx;
157  c1[2] += axbz + azbx;
158  c1[3] += aybz - azby;
159  c1[6] += azby + aybz;
160  c1[7] += azbx - axbz;
161  c1[11] += axby - aybx;
162 
163  c2[1] += a.z + b.z;
164  c2[2] -= a.y + b.y;
165  c2[3] += a.x - b.x;
166  c2[6] += a.x + b.x;
167  c2[7] += a.y - b.y;
168  c2[11] += a.z - b.z;
169  source_it++;
170  target_it++;
171  }
172 
173  c1[4] = c1[1];
174  c1[8] = c1[2];
175  c1[9] = c1[6];
176  c1[12] = c1[3];
177  c1[13] = c1[7];
178  c1[14] = c1[11];
179  c2[4] = -c2[1];
180  c2[8] = -c2[2];
181  c2[12] = -c2[3];
182  c2[9] = -c2[6];
183  c2[13] = -c2[7];
184  c2[14] = -c2[11];
185 
186  C1 *= -2.0;
187  C2 *= 2.0;
188 
189  const Eigen::Matrix<double, 4, 4> A = (0.25 / double (npts)) * C2.transpose () * C2 - C1;
190 
191  const Eigen::EigenSolver<Eigen::Matrix<double, 4, 4> > es (A);
192 
193  ptrdiff_t i;
194  es.eigenvalues ().real ().maxCoeff (&i);
195  const Eigen::Matrix<double, 4, 1> qmat = es.eigenvectors ().col (i).real ();
196  const Eigen::Matrix<double, 4, 1> smat = - (0.5 / double (npts)) * C2 * qmat;
197 
198  const Eigen::Quaternion<double> q (qmat (3), qmat (0), qmat (1), qmat (2));
199  const Eigen::Quaternion<double> s (smat (3), smat (0), smat (1), smat (2));
200 
201  const Eigen::Quaternion<double> t = s * q.conjugate ();
202 
203  const Eigen::Matrix<double, 3, 3> R (q.toRotationMatrix ());
204 
205  for (int i = 0; i < 3; ++i)
206  for (int j = 0; j < 3; ++j)
207  transformation_matrix (i, j) = R (i, j);
208 
209  transformation_matrix (0, 3) = - t.x ();
210  transformation_matrix (1, 3) = - t.y ();
211  transformation_matrix (2, 3) = - t.z ();
212 }
213 
214 } // namespace registration
215 } // namespace pcl
216 
217 #endif /* PCL_REGISTRATION_TRANSFORMATION_ESTIMATION_DQ_HPP_ */
218 
pcl
Definition: convolution.h:46
pcl::PointCloud::points
std::vector< PointT, Eigen::aligned_allocator< PointT > > points
The point data.
Definition: point_cloud.h:410
pcl::registration::TransformationEstimationDualQuaternion::Matrix4
typename TransformationEstimation< PointSource, PointTarget, Scalar >::Matrix4 Matrix4
Definition: transformation_estimation_dual_quaternion.h:63
pcl::registration::TransformationEstimationDualQuaternion::estimateRigidTransformation
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 using dual quatern...
Definition: transformation_estimation_dual_quaternion.hpp:53
pcl::PointCloud< PointSource >
pcl::ConstCloudIterator::size
std::size_t size() const
Size of the range the iterator is going through.
Definition: cloud_iterator.hpp:537
pcl::ConstCloudIterator
Iterator class for point clouds with or without given indices.
Definition: cloud_iterator.h:121
pcl::Correspondences
std::vector< pcl::Correspondence, Eigen::aligned_allocator< pcl::Correspondence > > Correspondences
Definition: correspondence.h:88