Point Cloud Library (PCL)  1.9.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/pcl_macros.h>
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  using VectorType = Eigen::Matrix<real, dimension, 1>;
57  using MatrixType = Eigen::Matrix<real, dimension, dimension>;
58  //-----CONSTRUCTOR&DESTRUCTOR-----
59  /** Constructor - dimension gives the size of the vectors to work with. */
60  VectorAverage ();
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  VectorType& getMean () const { return mean_;}
70 
71  /** Get the covariance matrix of the added vectors */
72  inline const
73  MatrixType& 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 VectorType& sample, real weight=1.0);
86 
87  /** Do Principal component analysis */
88  inline void
89  doPCA (VectorType& eigen_values, VectorType& eigen_vector1,
90  VectorType& eigen_vector2, VectorType& eigen_vector3) const;
91 
92  /** Do Principal component analysis */
93  inline void
94  doPCA (VectorType& eigen_values) const;
95 
96  /** Get the eigenvector corresponding to the smallest eigenvalue */
97  inline void
98  getEigenVector1 (VectorType& eigen_vector1) const;
99 
101 
102  //-----VARIABLES-----
103 
104 
105  protected:
106  //-----METHODS-----
107  //-----VARIABLES-----
108  unsigned int noOfSamples_ = 0;
110  VectorType mean_ = VectorType::Identity ();
111  MatrixType covariance_ = MatrixType::Identity ();
112  };
113 
117 } // END namespace
118 
119 #include <pcl/common/impl/vector_average.hpp>
const VectorType & getMean() const
Get the mean of the added vectors.
real getAccumulatedWeight() const
Get the summed up weight of all added vectors.
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.
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: pcl_macros.h:359
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.