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