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