OpenWalnut  1.5.0dev
WPrincipalComponentAnalysis.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WPRINCIPALCOMPONENTANALYSIS_H
26 #define WPRINCIPALCOMPONENTANALYSIS_H
27 
28 #include <vector>
29 #include <Eigen/Dense> //TODO(aschwarzkopf): Consider reimplementing Eigen from your JAVA implementation.
30 #include "WCovarianceSolver.h"
31 #include "core/common/math/linearAlgebra/WVectorFixed.h"
32 #include "core/common/math/linearAlgebra/WPosition.h"
33 
34 using std::vector;
35 using std::complex;
36 using std::cout;
37 using std::endl;
38 using Eigen::EigenSolver;
39 using Eigen::MatrixXcd;
40 using Eigen::VectorXcd;
41 
42 /**
43  * This is a class that analyzes the point distribution of point couds using the
44  * Principal Component Analysis. This class puts out vectors of main point distribution
45  * directions and how much they are distributed in that directions.
46  *
47  * The algorithms are based mainly on Covariance calculation and Eigen Value and Vector
48  * analysis. General about Principal Component Analysis:
49  * http://georgemdallas.wordpress.com/2013/10/30/principal-component-
50  * analysis-4-dummies-eigenvectors-eigenvalues-and-dimension-reduction/
51  * See principle of algorithm here:
52  * http://www.cs.otago.ac.nz/cosc453/student_tutorials/principal_components.pdf
53  */
55 {
56 public:
57  /**
58  * Creates the Principal Component Analysis instance.
59  */
61  /**
62  * Destroys the Principal Component Analysis instance.
63  */
65  /**
66  * Analyzes a point data set using the Principal Component Analysis algorithm.
67  * \param inputData Point data to be analyzed.
68  */
69  void analyzeData( const vector<WPosition>& inputData );
70  /**
71  * Returns the mean coordinate of all input points.
72  * \return The mean coordinate of all input points.
73  */
75  /**
76  * Returns the point distribution directions. The first one is the strongest and
77  * the last is the weakest point distribution direction. Their index corresponds to
78  * getVariance().
79  * \return Point distribution directions.
80  */
81  vector<WVector3d> getEigenVectors();
82  /**
83  * Returns Eigen Values (equals how much the directions of getDirections() are
84  * distributed). Its index corresponds to the indices of that method. The strengths
85  * are sorted descending.
86  * \return Distribution strength in a direction.
87  */
88  vector<double> getEigenValues();
89 
90 private:
91  /**
92  * Does the actual Eigen Value and Vector Analysis.
93  */
94  void extractEigenData();
95  /**
96  * Sorts the Point distribution direction by the descending strength.
97  */
99  /**
100  * Replaces two Eigen Vectors and its Variances by each other.
101  * \param eigenVectorIndex1 Index of the first Eigen Value and Vector
102  * \param eigenVectorIndex2 Index of the second Eigen Value and Vector
103  */
104  void swapEigenVectors( size_t eigenVectorIndex1, size_t eigenVectorIndex2 );
105 
106  /**
107  * Point count of the input data.
108  */
110  /**
111  * Dimension count used in the input data.
112  */
113  size_t m_dimensions;
114 
115  /**
116  * Instance to analyze the covariances of the point data between each dimension pair.
117  */
119  /**
120  * Main point distribution directions. After process they're sorted by their strength descending.
121  */
122  vector<WVector3d> m_eigenVectors;
123  /**
124  * Lambda values for A*x-Lambda*x=0.
125  */
126  vector<double> m_eigenValues;
127  /**
128  * Instance for solving Eigen Values and Vectors.
129  */
130  EigenSolver<MatrixXd> m_eigenSolver;
131  /**
132  * Indicator whether calculation is still valid.
133  */
134  bool m_isValidPCA; //TODO(aschwarzkopf): Still no effect.
135 };
136 
137 #endif // WPRINCIPALCOMPONENTANALYSIS_H
Calculates covariances of a point data set.
This only is a 3d double vector.
This is a class that analyzes the point distribution of point couds using the Principal Component Ana...
void extractEigenData()
Does the actual Eigen Value and Vector Analysis.
size_t m_dimensions
Dimension count used in the input data.
void sortByVarianceDescending()
Sorts the Point distribution direction by the descending strength.
vector< double > getEigenValues()
Returns Eigen Values (equals how much the directions of getDirections() are distributed).
vector< WVector3d > m_eigenVectors
Main point distribution directions.
virtual ~WPrincipalComponentAnalysis()
Destroys the Principal Component Analysis instance.
WPosition getMean()
Returns the mean coordinate of all input points.
vector< double > m_eigenValues
Lambda values for A*x-Lambda*x=0.
void swapEigenVectors(size_t eigenVectorIndex1, size_t eigenVectorIndex2)
Replaces two Eigen Vectors and its Variances by each other.
bool m_isValidPCA
Indicator whether calculation is still valid.
WCovarianceSolver m_covarianceSolver
Instance to analyze the covariances of the point data between each dimension pair.
size_t m_inputPointCount
Point count of the input data.
vector< WVector3d > getEigenVectors()
Returns the point distribution directions.
WPrincipalComponentAnalysis()
Creates the Principal Component Analysis instance.
EigenSolver< MatrixXd > m_eigenSolver
Instance for solving Eigen Values and Vectors.
void analyzeData(const vector< WPosition > &inputData)
Analyzes a point data set using the Principal Component Analysis algorithm.