Point Cloud Library (PCL)  1.7.0
vector_average.h
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_H
39 #define PCL_COMMON_VECTOR_AVERAGE_H
40 
41 #include <pcl/common/eigen.h>
42 
43 namespace pcl
44 {
45  /** \brief Calculates the weighted average and the covariance matrix
46  *
47  * A class to calculate the weighted average and the covariance matrix of a set of vectors with given weights.
48  * The original data is not saved. Mean and covariance are calculated iteratively.
49  * \author Bastian Steder
50  * \ingroup common
51  */
52  template <typename real, int dimension>
54  {
55  public:
56  //-----CONSTRUCTOR&DESTRUCTOR-----
57  /** Constructor - dimension gives the size of the vectors to work with. */
58  VectorAverage ();
59  /** Destructor */
61 
62  //-----METHODS-----
63  /** Reset the object to work with a new data set */
64  inline void
65  reset ();
66 
67  /** Get the mean of the added vectors */
68  inline const
69  Eigen::Matrix<real, dimension, 1>& getMean () const { return mean_;}
70 
71  /** Get the covariance matrix of the added vectors */
72  inline const
73  Eigen::Matrix<real, dimension, dimension>& getCovariance () const { return covariance_;}
74 
75  /** Get the summed up weight of all added vectors */
76  inline real
78 
79  /** Get the number of added vectors */
80  inline unsigned int
82 
83  /** Add a new sample */
84  inline void
85  add (const Eigen::Matrix<real, dimension, 1>& sample, real weight=1.0);
86 
87  /** Do Principal component analysis */
88  inline void
89  doPCA (Eigen::Matrix<real, dimension, 1>& eigen_values, Eigen::Matrix<real, dimension, 1>& eigen_vector1,
90  Eigen::Matrix<real, dimension, 1>& eigen_vector2, Eigen::Matrix<real, dimension, 1>& eigen_vector3) const;
91 
92  /** Do Principal component analysis */
93  inline void
94  doPCA (Eigen::Matrix<real, dimension, 1>& eigen_values) const;
95 
96  /** Get the eigenvector corresponding to the smallest eigenvalue */
97  inline void
98  getEigenVector1 (Eigen::Matrix<real, dimension, 1>& eigen_vector1) const;
99 
100  //-----VARIABLES-----
101 
102  protected:
103  //-----METHODS-----
104  //-----VARIABLES-----
105  unsigned int noOfSamples_;
107  Eigen::Matrix<real, dimension, 1> mean_;
108  Eigen::Matrix<real, dimension, dimension> covariance_;
109  };
110 
114 } // END namespace
115 
116 #include <pcl/common/impl/vector_average.hpp>
117 
118 #endif // #ifndef PCL_VECTOR_AVERAGE_H
119