Ponca  4d2a58fa5c6375adef5c4b208f4d47e016cecd6d
Point Cloud Analysis library
Loading...
Searching...
No Matches
covariance.hpp
1/*
2 Copyright (C) 2014 Nicolas Mellado <nmellado0@gmail.com>
3 Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
4
5 This Source Code Form is subject to the terms of the Mozilla Public
6 License, v. 2.0. If a copy of the MPL was not distributed with this
7 file, You can obtain one at http://mozilla.org/MPL/2.0/.
8*/
9
10template <class DataPoint, class _NFilter, typename T>
11 requires COVARIANCE_FIT_BASE_REQUIREMENTS
13{
14 Base::init();
15 m_cov.setZero();
16}
17
18template <class DataPoint, class _NFilter, typename T>
19 requires COVARIANCE_FIT_BASE_REQUIREMENTS
21 const DataPoint& attributes)
22{
23 Base::addLocalNeighbor(w, localQ, attributes);
24 m_cov += w * localQ * localQ.transpose();
25}
26
27template <class DataPoint, class _NFilter, typename T>
28 requires COVARIANCE_FIT_BASE_REQUIREMENTS
30{
31 // handle specific configurations
32 if (Base::finalize() != STABLE)
33 return Base::m_eCurrentState;
34 // With less than Dim neighbors the fitting is undefined
35 if (Base::getNumNeighbors() < DataPoint::Dim)
36 return Base::m_eCurrentState = UNDEFINED;
37
38 // Center the covariance on the centroid
39 auto centroid = Base::barycenterLocal();
40 m_cov = m_cov / Base::getWeightSum() - centroid * centroid.transpose();
41
42#ifdef __CUDACC__
43 m_solver.computeDirect(m_cov);
44#else
45 m_solver.compute(m_cov);
46#endif
47 Base::m_eCurrentState = (m_solver.info() == Eigen::Success ? STABLE : UNDEFINED);
48
49 return Base::m_eCurrentState;
50}
51
52template <class DataPoint, class _NFilter, typename T>
53 requires COVARIANCE_FIT_BASE_REQUIREMENTS
55{
56 return m_solver.eigenvalues()(0) / m_solver.eigenvalues().mean();
57}
58
59template <class DataPoint, class _WFunctor, typename T>
60 requires COVARIANCE_FIT_BASE_REQUIREMENTS
62{
63 PONCA_ASSERT(DataPoint::Dim == 3);
64 return (m_solver.eigenvalues()(1) - m_solver.eigenvalues()(0)) / m_solver.eigenvalues()(2);
65}
66
67template <class DataPoint, class _WFunctor, typename T>
70{
71 PONCA_ASSERT(DataPoint::Dim == 3);
72 return (m_solver.eigenvalues()(2) - m_solver.eigenvalues()(1)) / m_solver.eigenvalues()(2);
73}
74
75template <class DataPoint, class _WFunctor, typename T>
76 requires COVARIANCE_FIT_BASE_REQUIREMENTS
78{
79 PONCA_ASSERT(DataPoint::Dim == 3);
80 return (m_solver.eigenvalues()(0)) / m_solver.eigenvalues()(2);
81}
82
83template <class DataPoint, class _WFunctor, typename T>
84 requires COVARIANCE_FIT_BASE_REQUIREMENTS
86{
87 return (m_solver.eigenvalues()(2) - m_solver.eigenvalues()(0)) / m_solver.eigenvalues()(2);
88}
89
90template <class DataPoint, class _WFunctor, typename T>
91 requires COVARIANCE_FIT_BASE_REQUIREMENTS
93{
94 return -(m_solver.eigenvalues().array() * m_solver.eigenvalues().array().log()).matrix().sum();
95}
96
97template <class DataPoint, class _WFunctor, typename T>
98 requires COVARIANCE_FIT_BASE_REQUIREMENTS
100{
101 return m_solver.eigenvalues()(0);
102}
103
104template <class DataPoint, class _WFunctor, typename T>
105 requires COVARIANCE_FIT_BASE_REQUIREMENTS
107{
108 return m_solver.eigenvalues()(1);
109}
110
111template <class DataPoint, class _WFunctor, typename T>
112 requires COVARIANCE_FIT_BASE_REQUIREMENTS
114{
115 return m_solver.eigenvalues()(2);
116}
117
118template <class DataPoint, class _NFilter, int DiffType, typename T>
119 requires COVARIANCE_FIT_DER_REQUIREMENTS
121{
122 Base::init();
123
124 for (int k = 0; k < Base::NbDerivatives; ++k)
125 m_dCov[k].setZero();
126}
127
128template <class DataPoint, class _NFilter, int DiffType, typename T>
129 requires COVARIANCE_FIT_DER_REQUIREMENTS
131 const DataPoint& attributes, ScalarArray& dw)
132{
133 Base::addLocalNeighbor(w, localQ, attributes, dw);
134 for (int k = 0; k < Base::NbDerivatives; ++k)
135 m_dCov[k] += dw[k] * localQ * localQ.transpose();
136}
137
138template <class DataPoint, class _NFilter, int DiffType, typename T>
139 requires COVARIANCE_FIT_DER_REQUIREMENTS
141{
142 PONCA_MULTIARCH_STD_MATH(sqrt);
143
144 Base::finalize();
145 // Test if base finalize end on a viable case (stable / unstable)
146 if (this->isReady())
147 {
148 VectorType cog = Base::barycenterLocal();
149 MatrixType cogSq = cog * cog.transpose();
150 // \fixme Better use eigen here
151 for (int k = 0; k < Base::NbDerivatives; ++k)
152 {
153 // Finalize the computation of dCov.
154 m_dCov[k] = m_dCov[k] - cog * Base::m_dSumP.col(k).transpose() - Base::m_dSumP.col(k) * cog.transpose() +
155 Base::m_dSumW[k] * cogSq;
156 }
157 }
158
159 return Base::m_eCurrentState;
160}
161
Aggregator class used to declare specialized structures using CRTP.
Definition basket.h:260
Procedure that compute and decompose the covariance matrix of the neighbors positions.
Definition covariance.h:66
typename Base::VectorType VectorType
Alias to vector type.
Definition covariance.h:67
typename DataPoint::Scalar Scalar
Alias to scalar type.
Definition covariance.h:67
Internal generic class computing the derivatives of covariance matrix computed by covarianceBase.
Definition covariance.h:147
typename DataPoint::MatrixType MatrixType
Alias to matrix type.
Definition covariance.h:149
typename DataPoint::Scalar Scalar
Alias to scalar type.
Definition covariance.h:148
typename Base::ScalarArray ScalarArray
Alias to scalar derivatives array.
Definition covariance.h:150
typename Base::VectorType VectorType
Alias to vector type.
Definition covariance.h:148
FIT_RESULT
Enum corresponding to the state of a fitting method (and what the finalize function returns)
Definition enums.h:15
@ UNDEFINED
The fitting is undefined, you can't use it for valid results.
Definition enums.h:22
@ STABLE
The fitting is stable and ready to use.
Definition enums.h:17
FIT_RESULT compute(const IteratorBegin &begin, const IteratorEnd &end)
Convenience function for STL-like iterators Add neighbors stored in a container using STL-like iterat...
Definition basket.h:155