pca.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /// @ref gtx_pca
  2. /// @file glm/gtx/pca.hpp
  3. ///
  4. /// @see core (dependence)
  5. /// @see ext_scalar_relational (dependence)
  6. ///
  7. /// @defgroup gtx_pca GLM_GTX_pca
  8. /// @ingroup gtx
  9. ///
  10. /// Include <glm/gtx/pca.hpp> to use the features of this extension.
  11. ///
  12. /// Implements functions required for fundamental 'princple component analysis' in 2D, 3D, and 4D:
  13. /// 1) Computing a covariance matrics from a list of _relative_ position vectors
  14. /// 2) Compute the eigenvalues and eigenvectors of the covariance matrics
  15. /// This is useful, e.g., to compute an object-aligned bounding box from vertices of an object.
  16. /// https://en.wikipedia.org/wiki/Principal_component_analysis
  17. ///
  18. /// Example:
  19. /// ```
  20. /// std::vector<glm::dvec3> ptData;
  21. /// // ... fill ptData with some point data, e.g. vertices
  22. ///
  23. /// glm::dvec3 center = computeCenter(ptData);
  24. ///
  25. /// glm::dmat3 covarMat = glm::computeCovarianceMatrix(ptData.data(), ptData.size(), center);
  26. ///
  27. /// glm::dvec3 evals;
  28. /// glm::dmat3 evecs;
  29. /// int evcnt = glm::findEigenvaluesSymReal(covarMat, evals, evecs);
  30. ///
  31. /// if(evcnt != 3)
  32. /// // ... error handling
  33. ///
  34. /// glm::sortEigenvalues(evals, evecs);
  35. ///
  36. /// // ... now evecs[0] points in the direction (symmetric) of the largest spatial distribuion within ptData
  37. /// ```
  38. #pragma once
  39. // Dependency:
  40. #include "../glm.hpp"
  41. #include "../ext/scalar_relational.hpp"
  42. #if GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED)
  43. # ifndef GLM_ENABLE_EXPERIMENTAL
  44. # pragma message("GLM: GLM_GTX_pca is an experimental extension and may change in the future. Use #define GLM_ENABLE_EXPERIMENTAL before including it, if you really want to use it.")
  45. # else
  46. # pragma message("GLM: GLM_GTX_pca extension included")
  47. # endif
  48. #endif
  49. namespace glm {
  50. /// @addtogroup gtx_pca
  51. /// @{
  52. /// Compute a covariance matrix form an array of relative coordinates `v` (e.g., relative to the center of gravity of the object)
  53. /// @param v Points to a memory holding `n` times vectors
  54. template<length_t D, typename T, qualifier Q>
  55. GLM_INLINE mat<D, D, T, Q> computeCovarianceMatrix(vec<D, T, Q> const* v, size_t n);
  56. /// Compute a covariance matrix form an array of absolute coordinates `v` and a precomputed center of gravity `c`
  57. /// @param v Points to a memory holding `n` times vectors
  58. template<length_t D, typename T, qualifier Q>
  59. GLM_INLINE mat<D, D, T, Q> computeCovarianceMatrix(vec<D, T, Q> const* v, size_t n, vec<D, T, Q> const& c);
  60. /// Compute a covariance matrix form a pair of iterators `b` (begin) and `e` (end) of a container with relative coordinates (e.g., relative to the center of gravity of the object)
  61. /// Dereferencing an iterator of type I must yield a `vec&lt;D, T, Q%gt;`
  62. template<length_t D, typename T, qualifier Q, typename I>
  63. GLM_FUNC_DECL mat<D, D, T, Q> computeCovarianceMatrix(I const& b, I const& e);
  64. /// Compute a covariance matrix form a pair of iterators `b` (begin) and `e` (end) of a container with absolute coordinates and a precomputed center of gravity `c`
  65. /// Dereferencing an iterator of type I must yield a `vec&lt;D, T, Q%gt;`
  66. template<length_t D, typename T, qualifier Q, typename I>
  67. GLM_FUNC_DECL mat<D, D, T, Q> computeCovarianceMatrix(I const& b, I const& e, vec<D, T, Q> const& c);
  68. /// Assuming the provided covariance matrix `covarMat` is symmetric and real-valued, this function find the `D` Eigenvalues of the matrix, and also provides the corresponding Eigenvectors.
  69. /// Note: the data in `outEigenvalues` and `outEigenvectors` are in matching order, i.e. `outEigenvector[i]` is the Eigenvector of the Eigenvalue `outEigenvalue[i]`.
  70. /// This is a numeric implementation to find the Eigenvalues, using 'QL decomposition` (variant of QR decomposition: https://en.wikipedia.org/wiki/QR_decomposition).
  71. /// @param covarMat A symmetric, real-valued covariance matrix, e.g. computed from `computeCovarianceMatrix`.
  72. /// @param outEigenvalues Vector to receive the found eigenvalues
  73. /// @param outEigenvectors Matrix to receive the found eigenvectors corresponding to the found eigenvalues, as column vectors
  74. /// @return The number of eigenvalues found, usually D if the precondition of the covariance matrix is met.
  75. template<length_t D, typename T, qualifier Q>
  76. GLM_FUNC_DECL unsigned int findEigenvaluesSymReal
  77. (
  78. mat<D, D, T, Q> const& covarMat,
  79. vec<D, T, Q>& outEigenvalues,
  80. mat<D, D, T, Q>& outEigenvectors
  81. );
  82. /// Sorts a group of Eigenvalues&Eigenvectors, for largest Eigenvalue to smallest Eigenvalue.
  83. /// The data in `outEigenvalues` and `outEigenvectors` are assumed to be matching order, i.e. `outEigenvector[i]` is the Eigenvector of the Eigenvalue `outEigenvalue[i]`.
  84. template<typename T, qualifier Q>
  85. GLM_INLINE void sortEigenvalues(vec<2, T, Q>& eigenvalues, mat<2, 2, T, Q>& eigenvectors);
  86. /// Sorts a group of Eigenvalues&Eigenvectors, for largest Eigenvalue to smallest Eigenvalue.
  87. /// The data in `outEigenvalues` and `outEigenvectors` are assumed to be matching order, i.e. `outEigenvector[i]` is the Eigenvector of the Eigenvalue `outEigenvalue[i]`.
  88. template<typename T, qualifier Q>
  89. GLM_INLINE void sortEigenvalues(vec<3, T, Q>& eigenvalues, mat<3, 3, T, Q>& eigenvectors);
  90. /// Sorts a group of Eigenvalues&Eigenvectors, for largest Eigenvalue to smallest Eigenvalue.
  91. /// The data in `outEigenvalues` and `outEigenvectors` are assumed to be matching order, i.e. `outEigenvector[i]` is the Eigenvector of the Eigenvalue `outEigenvalue[i]`.
  92. template<typename T, qualifier Q>
  93. GLM_INLINE void sortEigenvalues(vec<4, T, Q>& eigenvalues, mat<4, 4, T, Q>& eigenvectors);
  94. /// @}
  95. }//namespace glm
  96. #include "pca.inl"