Point Cloud Library (PCL)  1.11.1
transformation_from_correspondences.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder(s) nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Id$
35  *
36  */
37 
38 #pragma once
39 
40 #include <pcl/common/eigen.h>
41 #include <pcl/common/transformation_from_correspondences.h>
42 
43 
44 namespace pcl
45 {
46 
47 inline void
49 {
50  no_of_samples_ = 0;
51  accumulated_weight_ = 0.0;
52  mean1_.fill(0);
53  mean2_.fill(0);
54  covariance_.fill(0);
55 }
56 
57 
58 inline void
59 TransformationFromCorrespondences::add (const Eigen::Vector3f& point, const Eigen::Vector3f& corresponding_point,
60  float weight)
61 {
62  if (weight==0.0f)
63  return;
64 
66  accumulated_weight_ += weight;
67  float alpha = weight/accumulated_weight_;
68 
69  Eigen::Vector3f diff1 = point - mean1_, diff2 = corresponding_point - mean2_;
70  covariance_ = (1.0f-alpha)*(covariance_ + alpha * (diff2 * diff1.transpose()));
71 
72  mean1_ += alpha*(diff1);
73  mean2_ += alpha*(diff2);
74 }
75 
76 
77 inline Eigen::Affine3f
79 {
80  //Eigen::JacobiSVD<Eigen::Matrix<float, 3, 3> > svd (covariance_, Eigen::ComputeFullU | Eigen::ComputeFullV);
81  Eigen::JacobiSVD<Eigen::Matrix<float, 3, 3> > svd (covariance_, Eigen::ComputeFullU | Eigen::ComputeFullV);
82  const Eigen::Matrix<float, 3, 3>& u = svd.matrixU(),
83  & v = svd.matrixV();
84  Eigen::Matrix<float, 3, 3> s;
85  s.setIdentity();
86  if (u.determinant()*v.determinant() < 0.0f)
87  s(2,2) = -1.0f;
88 
89  Eigen::Matrix<float, 3, 3> r = u * s * v.transpose();
90  Eigen::Vector3f t = mean2_ - r*mean1_;
91 
92  Eigen::Affine3f ret;
93  ret(0,0)=r(0,0); ret(0,1)=r(0,1); ret(0,2)=r(0,2); ret(0,3)=t(0);
94  ret(1,0)=r(1,0); ret(1,1)=r(1,1); ret(1,2)=r(1,2); ret(1,3)=t(1);
95  ret(2,0)=r(2,0); ret(2,1)=r(2,1); ret(2,2)=r(2,2); ret(2,3)=t(2);
96  ret(3,0)=0.0f; ret(3,1)=0.0f; ret(3,2)=0.0f; ret(3,3)=1.0f;
97 
98  return (ret);
99 }
100 
101 } // namespace pcl
102 
pcl
Definition: convolution.h:46
pcl::TransformationFromCorrespondences::no_of_samples_
unsigned int no_of_samples_
Definition: transformation_from_correspondences.h:82
pcl::TransformationFromCorrespondences::accumulated_weight_
float accumulated_weight_
Definition: transformation_from_correspondences.h:83
pcl::TransformationFromCorrespondences::reset
void reset()
Reset the object to work with a new data set.
Definition: transformation_from_correspondences.hpp:48
pcl::TransformationFromCorrespondences::covariance_
Eigen::Matrix< float, 3, 3 > covariance_
Definition: transformation_from_correspondences.h:86
pcl::TransformationFromCorrespondences::getTransformation
Eigen::Affine3f getTransformation()
Calculate the transformation that will best transform the points into their correspondences.
Definition: transformation_from_correspondences.hpp:78
pcl::TransformationFromCorrespondences::mean2_
Eigen::Vector3f mean2_
Definition: transformation_from_correspondences.h:85
pcl::TransformationFromCorrespondences::mean1_
Eigen::Vector3f mean1_
Definition: transformation_from_correspondences.h:84
pcl::TransformationFromCorrespondences::add
void add(const Eigen::Vector3f &point, const Eigen::Vector3f &corresponding_point, float weight=1.0)
Add a new sample.
Definition: transformation_from_correspondences.hpp:59