Point Cloud Library (PCL)  1.9.1-dev
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 #include <pcl/common/eigen.h>
39 
40 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
41 inline void
43 {
44  no_of_samples_ = 0;
45  accumulated_weight_ = 0.0;
46  mean1_.fill(0);
47  mean2_.fill(0);
48  covariance_.fill(0);
49 }
50 
51 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
52 inline void
53 pcl::TransformationFromCorrespondences::add (const Eigen::Vector3f& point, const Eigen::Vector3f& corresponding_point,
54  float weight)
55 {
56  if (weight==0.0f)
57  return;
58 
60  accumulated_weight_ += weight;
61  float alpha = weight/accumulated_weight_;
62 
63  Eigen::Vector3f diff1 = point - mean1_, diff2 = corresponding_point - mean2_;
64  covariance_ = (1.0f-alpha)*(covariance_ + alpha * (diff2 * diff1.transpose()));
65 
66  mean1_ += alpha*(diff1);
67  mean2_ += alpha*(diff2);
68 }
69 
70 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
71 inline Eigen::Affine3f
73 {
74  //Eigen::JacobiSVD<Eigen::Matrix<float, 3, 3> > svd (covariance_, Eigen::ComputeFullU | Eigen::ComputeFullV);
75  Eigen::JacobiSVD<Eigen::Matrix<float, 3, 3> > svd (covariance_, Eigen::ComputeFullU | Eigen::ComputeFullV);
76  const Eigen::Matrix<float, 3, 3>& u = svd.matrixU(),
77  & v = svd.matrixV();
78  Eigen::Matrix<float, 3, 3> s;
79  s.setIdentity();
80  if (u.determinant()*v.determinant() < 0.0f)
81  s(2,2) = -1.0f;
82 
83  Eigen::Matrix<float, 3, 3> r = u * s * v.transpose();
84  Eigen::Vector3f t = mean2_ - r*mean1_;
85 
86  Eigen::Affine3f ret;
87  ret(0,0)=r(0,0); ret(0,1)=r(0,1); ret(0,2)=r(0,2); ret(0,3)=t(0);
88  ret(1,0)=r(1,0); ret(1,1)=r(1,1); ret(1,2)=r(1,2); ret(1,3)=t(1);
89  ret(2,0)=r(2,0); ret(2,1)=r(2,1); ret(2,2)=r(2,2); ret(2,3)=t(2);
90  ret(3,0)=0.0f; ret(3,1)=0.0f; ret(3,2)=0.0f; ret(3,3)=1.0f;
91 
92  return (ret);
93 }
void add(const Eigen::Vector3f &point, const Eigen::Vector3f &corresponding_point, float weight=1.0)
Add a new sample.
void reset()
Reset the object to work with a new data set.
Eigen::Affine3f getTransformation()
Calculate the transformation that will best transform the points into their correspondences.