Point Cloud Library (PCL)  1.10.1-dev
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 #pragma once
39 
40 #include <pcl/memory.h>
41 #include <pcl/pcl_macros.h>
42 #include <pcl/common/eigen.h>
43 
44 namespace pcl
45 {
46  /** \brief Calculates the weighted average and the covariance matrix
47  *
48  * A class to calculate the weighted average and the covariance matrix of a set of vectors with given weights.
49  * The original data is not saved. Mean and covariance are calculated iteratively.
50  * \author Bastian Steder
51  * \ingroup common
52  */
53  template <typename real, int dimension>
55  {
56  public:
57  using VectorType = Eigen::Matrix<real, dimension, 1>;
58  using MatrixType = Eigen::Matrix<real, dimension, dimension>;
59  //-----CONSTRUCTOR&DESTRUCTOR-----
60  /** Constructor - dimension gives the size of the vectors to work with. */
61  VectorAverage ();
62 
63  //-----METHODS-----
64  /** Reset the object to work with a new data set */
65  inline void
66  reset ();
67 
68  /** Get the mean of the added vectors */
69  inline const
70  VectorType& getMean () const { return mean_;}
71 
72  /** Get the covariance matrix of the added vectors */
73  inline const
74  MatrixType& getCovariance () const { return covariance_;}
75 
76  /** Get the summed up weight of all added vectors */
77  inline real
79 
80  /** Get the number of added vectors */
81  inline unsigned int
83 
84  /** Add a new sample */
85  inline void
86  add (const VectorType& sample, real weight=1.0);
87 
88  /** Do Principal component analysis */
89  inline void
90  doPCA (VectorType& eigen_values, VectorType& eigen_vector1,
91  VectorType& eigen_vector2, VectorType& eigen_vector3) const;
92 
93  /** Do Principal component analysis */
94  inline void
95  doPCA (VectorType& eigen_values) const;
96 
97  /** Get the eigenvector corresponding to the smallest eigenvalue */
98  inline void
99  getEigenVector1 (VectorType& eigen_vector1) const;
100 
102 
103  //-----VARIABLES-----
104 
105 
106  protected:
107  //-----METHODS-----
108  //-----VARIABLES-----
109  unsigned int noOfSamples_ = 0;
111  VectorType mean_ = VectorType::Identity ();
112  MatrixType covariance_ = MatrixType::Identity ();
113  };
114 
118 } // END namespace
119 
120 #include <pcl/common/impl/vector_average.hpp>
const VectorType & getMean() const
Get the mean of the added vectors.
Defines functions, macros and traits for allocating and using memory.
real getAccumulatedWeight() const
Get the summed up weight of all added vectors.
void reset()
Reset the object to work with a new data set.
VectorAverage()
Constructor - dimension gives the size of the vectors to work with.
#define PCL_MAKE_ALIGNED_OPERATOR_NEW
Macro to signal a class requires a custom allocator.
Definition: memory.h:65
Eigen::Matrix< real, dimension, 1 > VectorType
unsigned int getNoOfSamples()
Get the number of added vectors.
MatrixType covariance_
void doPCA(VectorType &eigen_values, VectorType &eigen_vector1, VectorType &eigen_vector2, VectorType &eigen_vector3) const
Do Principal component analysis.
unsigned int noOfSamples_
Eigen::Matrix< real, dimension, dimension > MatrixType
void add(const VectorType &sample, real weight=1.0)
Add a new sample.
Calculates the weighted average and the covariance matrix.
Defines all the PCL and non-PCL macros used.
const MatrixType & getCovariance() const
Get the covariance matrix of the added vectors.
void getEigenVector1(VectorType &eigen_vector1) const
Get the eigenvector corresponding to the smallest eigenvalue.