pca.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 distribution within ptData
  37. /// ```
  38. #pragma once
  39. // Dependency:
  40. #include "../glm.hpp"
  41. #include "../ext/scalar_relational.hpp"
  42. #ifndef GLM_ENABLE_EXPERIMENTAL
  43. # error "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."
  44. #elif GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED)
  45. # pragma message("GLM: GLM_GTX_pca extension included")
  46. #endif
  47. namespace glm {
  48. /// @addtogroup gtx_pca
  49. /// @{
  50. /// Compute a covariance matrix form an array of relative coordinates `v` (e.g., relative to the center of gravity of the object)
  51. /// @param v Points to a memory holding `n` times vectors
  52. /// @param n Number of points in v
  53. template<length_t D, typename T, qualifier Q>
  54. GLM_INLINE mat<D, D, T, Q> computeCovarianceMatrix(vec<D, T, Q> const* v, size_t n);
  55. /// Compute a covariance matrix form an array of absolute coordinates `v` and a precomputed center of gravity `c`
  56. /// @param v Points to a memory holding `n` times vectors
  57. /// @param n Number of points in v
  58. /// @param c Precomputed center of gravity
  59. template<length_t D, typename T, qualifier Q>
  60. GLM_INLINE mat<D, D, T, Q> computeCovarianceMatrix(vec<D, T, Q> const* v, size_t n, vec<D, T, Q> const& c);
  61. /// 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)
  62. /// Dereferencing an iterator of type I must yield a `vec&lt;D, T, Q%gt;`
  63. template<length_t D, typename T, qualifier Q, typename I>
  64. GLM_FUNC_DECL mat<D, D, T, Q> computeCovarianceMatrix(I const& b, I const& e);
  65. /// 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`
  66. /// Dereferencing an iterator of type I must yield a `vec&lt;D, T, Q%gt;`
  67. template<length_t D, typename T, qualifier Q, typename I>
  68. GLM_FUNC_DECL mat<D, D, T, Q> computeCovarianceMatrix(I const& b, I const& e, vec<D, T, Q> const& c);
  69. /// 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.
  70. /// Note: the data in `outEigenvalues` and `outEigenvectors` are in matching order, i.e. `outEigenvector[i]` is the Eigenvector of the Eigenvalue `outEigenvalue[i]`.
  71. /// This is a numeric implementation to find the Eigenvalues, using 'QL decomposition` (variant of QR decomposition: https://en.wikipedia.org/wiki/QR_decomposition).
  72. ///
  73. /// @param[in] covarMat A symmetric, real-valued covariance matrix, e.g. computed from computeCovarianceMatrix
  74. /// @param[out] outEigenvalues Vector to receive the found eigenvalues
  75. /// @param[out] outEigenvectors Matrix to receive the found eigenvectors corresponding to the found eigenvalues, as column vectors
  76. /// @return The number of eigenvalues found, usually D if the precondition of the covariance matrix is met.
  77. template<length_t D, typename T, qualifier Q>
  78. GLM_FUNC_DECL unsigned int findEigenvaluesSymReal
  79. (
  80. mat<D, D, T, Q> const& covarMat,
  81. vec<D, T, Q>& outEigenvalues,
  82. mat<D, D, T, Q>& outEigenvectors
  83. );
  84. /// Sorts a group of Eigenvalues&Eigenvectors, for largest Eigenvalue to smallest Eigenvalue.
  85. /// The data in `outEigenvalues` and `outEigenvectors` are assumed to be matching order, i.e. `outEigenvector[i]` is the Eigenvector of the Eigenvalue `outEigenvalue[i]`.
  86. template<typename T, qualifier Q>
  87. GLM_FUNC_DISCARD_DECL void sortEigenvalues(vec<2, T, Q>& eigenvalues, mat<2, 2, T, Q>& eigenvectors);
  88. /// Sorts a group of Eigenvalues&Eigenvectors, for largest Eigenvalue to smallest Eigenvalue.
  89. /// The data in `outEigenvalues` and `outEigenvectors` are assumed to be matching order, i.e. `outEigenvector[i]` is the Eigenvector of the Eigenvalue `outEigenvalue[i]`.
  90. template<typename T, qualifier Q>
  91. GLM_FUNC_DISCARD_DECL void sortEigenvalues(vec<3, T, Q>& eigenvalues, mat<3, 3, T, Q>& eigenvectors);
  92. /// Sorts a group of Eigenvalues&Eigenvectors, for largest Eigenvalue to smallest Eigenvalue.
  93. /// The data in `outEigenvalues` and `outEigenvectors` are assumed to be matching order, i.e. `outEigenvector[i]` is the Eigenvector of the Eigenvalue `outEigenvalue[i]`.
  94. template<typename T, qualifier Q>
  95. GLM_FUNC_DISCARD_DECL void sortEigenvalues(vec<4, T, Q>& eigenvalues, mat<4, 4, T, Q>& eigenvectors);
  96. /// @}
  97. }//namespace glm
  98. #include "pca.inl"