Ponca  cb266528645085a12930c827fbb8336cd937ecbb
Point Cloud Analysis library
Loading...
Searching...
No Matches
covariancePlaneFit.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_PLANE_FIT_REQUIREMENTS
12FIT_RESULT CovariancePlaneFitImpl<DataPoint, _NFilter, T>::finalize()
13{
14 if (Base::finalize() == STABLE)
15 {
16 if (Base::plane().isValid())
17 Base::m_eCurrentState = CONFLICT_ERROR_FOUND;
18 Base::setPlane(Base::m_solver.eigenvectors().col(0), Base::barycenterLocal());
19 }
20
21 return Base::m_eCurrentState;
22}
23
24template <class DataPoint, class _NFilter, typename T>
25 requires COVARIANCE_PLANE_FIT_REQUIREMENTS
26typename CovariancePlaneFitImpl<DataPoint, _NFilter, T>::VectorType CovariancePlaneFitImpl<
27 DataPoint, _NFilter, T>::worldToTangentPlane(const VectorType& _q, bool _isPositionVector) const
28{
29 return Base::m_solver.eigenvectors().transpose() *
30 Base::getNeighborFrame().convertToLocalBasis(_q, _isPositionVector);
31}
32
33template <class DataPoint, class _NFilter, typename T>
34 requires COVARIANCE_PLANE_FIT_REQUIREMENTS
35typename CovariancePlaneFitImpl<DataPoint, _NFilter, T>::VectorType CovariancePlaneFitImpl<
36 DataPoint, _NFilter, T>::tangentPlaneToWorld(const VectorType& _lq, bool _isPositionVector) const
37{
38 return Base::getNeighborFrame().convertToGlobalBasis(Base::m_solver.eigenvectors().transpose().inverse() * _lq,
39 _isPositionVector);
40}
41
42template <class DataPoint, class _NFilter, int DiffType, typename T>
43 requires COVARIANCE_PLANE_DER_REQUIREMENTS
44FIT_RESULT CovariancePlaneDerImpl<DataPoint, _NFilter, DiffType, T>::finalize()
45{
46 PONCA_MULTIARCH_CU_STD_FUNC(numeric_limits);
47
48 // Checked below
49 (void)Base::finalize();
50
51 // Test if base finalize end on a viable case (stable / unstable)
52 if (this->isReady())
53 {
54 VectorType barycenter = Base::barycenterLocal();
55 VectorArray dBarycenter = Base::barycenterDerivatives();
56
57 // pre-compute shifted eigenvalues to apply the pseudo inverse of C - lambda_0 I
58 Scalar epsilon = Scalar(2) * Eigen::NumTraits<Scalar>::epsilon();
59 Scalar consider_as_zero = Scalar(2) * numeric_limits<Scalar>::denorm_min();
60
61 // This is where the limitation to 3d comes from.
62 // \fixme Replace shift in 2d subspace by any subspace with co-dimension 1
63 Eigen::Matrix<Scalar, 2, 1> shifted_eivals =
64 Base::m_solver.eigenvalues().template tail<2>().array() - Base::m_solver.eigenvalues()(0);
65 if (shifted_eivals(0) < consider_as_zero || shifted_eivals(0) < epsilon * shifted_eivals(1))
66 shifted_eivals(0) = 0;
67 if (shifted_eivals(1) < consider_as_zero)
68 shifted_eivals(1) = 0;
69
70 for (int k = 0; k < Base::NbDerivatives; ++k)
71 {
72 VectorType normal = Base::primitiveGradient(Base::getNeighborFilter().center());
73 // The derivative of 'normal' is the derivative of the smallest eigenvector.
74 // Since the covariance matrix is real and symmetric, it is equal to:
75 // n' = - (C - lambda_0 I)^+ C' n
76 // Where ^+ denotes the pseudo-inverse.
77 // Since we already performed the eigenvalue decomposition of the matrix C,
78 // we can directly apply the pseudo inverse by observing that:
79 // (C - lambda_0 I) = V (L - lambda_0 I) V^T
80 // where V is the eigenvector matrix, and L the eigenvalue diagonal matrix.
81 Eigen::Matrix<Scalar, 2, 1> z =
82 -Base::m_solver.eigenvectors().template rightCols<2>().transpose() * (Base::m_dCov[k] * normal);
83 if (shifted_eivals(0) > 0)
84 z(0) /= shifted_eivals(0);
85 if (shifted_eivals(1) > 0)
86 z(1) /= shifted_eivals(1);
87 m_dNormal.col(k) = Base::m_solver.eigenvectors().template rightCols<2>() * z;
88
89 VectorType dDiff = dBarycenter.col(k);
90 if (k > 0 || !Base::isScaleDer())
91 dDiff(Base::isScaleDer() ? k - 1 : k) += 1;
92 m_dDist(k) = m_dNormal.col(k).dot(barycenter) + normal.dot(dDiff);
93 }
94 }
95
96 return Base::m_eCurrentState;
97}
Ponca::MeanPosition T barycenter() const
where are all the point samples in 's neighborhood
Definition mean.h:46
FIT_RESULT
Enum corresponding to the state of a fitting method (and what the finalize function returns).
Definition enums.h:15
@ CONFLICT_ERROR_FOUND
Multiple classes of the fitting procedure initialize the primitive.
Definition enums.h:27