Point Cloud Library (PCL)  1.9.1-dev
vector_average.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, 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 #ifndef PCL_COMMON_VECTOR_AVERAGE_IMPL_HPP_
39 #define PCL_COMMON_VECTOR_AVERAGE_IMPL_HPP_
40 
41 namespace pcl
42 {
43  template <typename real, int dimension>
45  {
46  reset();
47  }
48 
49  template <typename real, int dimension>
51  {
52  noOfSamples_ = 0;
53  accumulatedWeight_ = 0.0;
54  mean_.fill(0);
55  covariance_.fill(0);
56  }
57 
58  template <typename real, int dimension>
59  inline void VectorAverage<real, dimension>::add(const Eigen::Matrix<real, dimension, 1>& sample, real weight) {
60  if (weight == 0.0f)
61  return;
62 
63  ++noOfSamples_;
64  accumulatedWeight_ += weight;
65  real alpha = weight/accumulatedWeight_;
66 
67  Eigen::Matrix<real, dimension, 1> diff = sample - mean_;
68  covariance_ = (covariance_ + (diff * diff.transpose())*alpha)*(1.0f-alpha);
69 
70  mean_ += (diff)*alpha;
71 
72  //if (std::isnan(covariance_(0,0)))
73  //{
74  //std::cout << PVARN(weight);
75  //exit(0);
76  //}
77  }
78 
79  template <typename real, int dimension>
80  inline void VectorAverage<real, dimension>::doPCA(Eigen::Matrix<real, dimension, 1>& eigen_values, Eigen::Matrix<real, dimension, 1>& eigen_vector1,
81  Eigen::Matrix<real, dimension, 1>& eigen_vector2, Eigen::Matrix<real, dimension, 1>& eigen_vector3) const
82  {
83  // The following step is necessary for cases where the values in the covariance matrix are small
84  // In this case float accuracy is nor enough to calculate the eigenvalues and eigenvectors.
85  //Eigen::Matrix<double, dimension, dimension> tmp_covariance = covariance_.template cast<double>();
86  //Eigen::SelfAdjointEigenSolver<Eigen::Matrix<double, dimension, dimension> > ei_symm(tmp_covariance);
87  //eigen_values = ei_symm.eigenvalues().template cast<real>();
88  //Eigen::Matrix<real, dimension, dimension> eigen_vectors = ei_symm.eigenvectors().template cast<real>();
89 
90  //std::cout << "My covariance is \n"<<covariance_<<"\n";
91  //std::cout << "My mean is \n"<<mean_<<"\n";
92  //std::cout << "My Eigenvectors \n"<<eigen_vectors<<"\n";
93 
94  Eigen::SelfAdjointEigenSolver<Eigen::Matrix<real, dimension, dimension> > ei_symm(covariance_);
95  eigen_values = ei_symm.eigenvalues();
96  Eigen::Matrix<real, dimension, dimension> eigen_vectors = ei_symm.eigenvectors();
97 
98  eigen_vector1 = eigen_vectors.col(0);
99  eigen_vector2 = eigen_vectors.col(1);
100  eigen_vector3 = eigen_vectors.col(2);
101  }
102 
103  template <typename real, int dimension>
104  inline void VectorAverage<real, dimension>::doPCA(Eigen::Matrix<real, dimension, 1>& eigen_values) const
105  {
106  // The following step is necessary for cases where the values in the covariance matrix are small
107  // In this case float accuracy is nor enough to calculate the eigenvalues and eigenvectors.
108  //Eigen::Matrix<double, dimension, dimension> tmp_covariance = covariance_.template cast<double>();
109  //Eigen::SelfAdjointEigenSolver<Eigen::Matrix<double, dimension, dimension> > ei_symm(tmp_covariance, false);
110  //eigen_values = ei_symm.eigenvalues().template cast<real>();
111 
112  Eigen::SelfAdjointEigenSolver<Eigen::Matrix<real, dimension, dimension> > ei_symm(covariance_, false);
113  eigen_values = ei_symm.eigenvalues();
114  }
115 
116  template <typename real, int dimension>
117  inline void VectorAverage<real, dimension>::getEigenVector1(Eigen::Matrix<real, dimension, 1>& eigen_vector1) const
118  {
119  // The following step is necessary for cases where the values in the covariance matrix are small
120  // In this case float accuracy is nor enough to calculate the eigenvalues and eigenvectors.
121  //Eigen::Matrix<double, dimension, dimension> tmp_covariance = covariance_.template cast<double>();
122  //Eigen::SelfAdjointEigenSolver<Eigen::Matrix<double, dimension, dimension> > ei_symm(tmp_covariance);
123  //eigen_values = ei_symm.eigenvalues().template cast<real>();
124  //Eigen::Matrix<real, dimension, dimension> eigen_vectors = ei_symm.eigenvectors().template cast<real>();
125 
126  //std::cout << "My covariance is \n"<<covariance_<<"\n";
127  //std::cout << "My mean is \n"<<mean_<<"\n";
128  //std::cout << "My Eigenvectors \n"<<eigen_vectors<<"\n";
129 
130  Eigen::SelfAdjointEigenSolver<Eigen::Matrix<real, dimension, dimension> > ei_symm(covariance_);
131  Eigen::Matrix<real, dimension, dimension> eigen_vectors = ei_symm.eigenvectors();
132  eigen_vector1 = eigen_vectors.col(0);
133  }
134 
135 
136  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
137  // Special cases for real=float & dimension=3 -> Partial specialization does not work with class templates. :( //
138  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
139  ///////////
140  // float //
141  ///////////
142  template <>
143  inline void VectorAverage<float, 3>::doPCA(Eigen::Matrix<float, 3, 1>& eigen_values, Eigen::Matrix<float, 3, 1>& eigen_vector1,
144  Eigen::Matrix<float, 3, 1>& eigen_vector2, Eigen::Matrix<float, 3, 1>& eigen_vector3) const
145  {
146  //std::cout << "Using specialized 3x3 version of doPCA!\n";
147  Eigen::Matrix<float, 3, 3> eigen_vectors;
148  eigen33(covariance_, eigen_vectors, eigen_values);
149  eigen_vector1 = eigen_vectors.col(0);
150  eigen_vector2 = eigen_vectors.col(1);
151  eigen_vector3 = eigen_vectors.col(2);
152  }
153  template <>
154  inline void VectorAverage<float, 3>::doPCA(Eigen::Matrix<float, 3, 1>& eigen_values) const
155  {
156  //std::cout << "Using specialized 3x3 version of doPCA!\n";
157  computeRoots (covariance_, eigen_values);
158  }
159  template <>
160  inline void VectorAverage<float, 3>::getEigenVector1(Eigen::Matrix<float, 3, 1>& eigen_vector1) const
161  {
162  //std::cout << "Using specialized 3x3 version of doPCA!\n";
163  Eigen::Vector3f::Scalar eigen_value;
164  Eigen::Vector3f eigen_vector;
165  eigen33(covariance_, eigen_value, eigen_vector);
166  eigen_vector1 = eigen_vector;
167  }
168 
169  ////////////
170  // double //
171  ////////////
172  template <>
173  inline void VectorAverage<double, 3>::doPCA(Eigen::Matrix<double, 3, 1>& eigen_values, Eigen::Matrix<double, 3, 1>& eigen_vector1,
174  Eigen::Matrix<double, 3, 1>& eigen_vector2, Eigen::Matrix<double, 3, 1>& eigen_vector3) const
175  {
176  //std::cout << "Using specialized 3x3 version of doPCA!\n";
177  Eigen::Matrix<double, 3, 3> eigen_vectors;
178  eigen33(covariance_, eigen_vectors, eigen_values);
179  eigen_vector1 = eigen_vectors.col(0);
180  eigen_vector2 = eigen_vectors.col(1);
181  eigen_vector3 = eigen_vectors.col(2);
182  }
183  template <>
184  inline void VectorAverage<double, 3>::doPCA(Eigen::Matrix<double, 3, 1>& eigen_values) const
185  {
186  //std::cout << "Using specialized 3x3 version of doPCA!\n";
187  computeRoots (covariance_, eigen_values);
188  }
189  template <>
190  inline void VectorAverage<double, 3>::getEigenVector1(Eigen::Matrix<double, 3, 1>& eigen_vector1) const
191  {
192  //std::cout << "Using specialized 3x3 version of doPCA!\n";
193  Eigen::Vector3d::Scalar eigen_value;
194  Eigen::Vector3d eigen_vector;
195  eigen33(covariance_, eigen_value, eigen_vector);
196  eigen_vector1 = eigen_vector;
197  }
198 } // END namespace
199 
200 #endif
201 
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
void reset()
Reset the object to work with a new data set.
void computeRoots(const Matrix &m, Roots &roots)
computes the roots of the characteristic polynomial of the input matrix m, which are the eigenvalues ...
Definition: eigen.hpp:64
VectorAverage()
Constructor - dimension gives the size of the vectors to work with.
void doPCA(VectorType &eigen_values, VectorType &eigen_vector1, VectorType &eigen_vector2, VectorType &eigen_vector3) const
Do Principal component analysis.
void eigen33(const Matrix &mat, typename Matrix::Scalar &eigenvalue, Vector &eigenvector)
determines the eigenvector and eigenvalue of the smallest eigenvalue of the symmetric positive semi d...
Definition: eigen.hpp:291
void add(const VectorType &sample, real weight=1.0)
Add a new sample.
void getEigenVector1(VectorType &eigen_vector1) const
Get the eigenvector corresponding to the smallest eigenvalue.