Ponca  aa50bfdf187919869239c5b44b748842569114c1
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 _WFunctor, typename T>
13{
14 if (Base::finalize() == STABLE) {
15 if (Base::plane().isValid()) Base::m_eCurrentState = CONFLICT_ERROR_FOUND;
16 Base::setPlane(Base::m_solver.eigenvectors().col(0), Base::barycenterLocal());
17 }
18
19 return Base::m_eCurrentState;
20}
21
22template < class DataPoint, class _WFunctor, typename T>
23template <bool ignoreTranslation>
26{
27 if (ignoreTranslation)
28 return Base::m_solver.eigenvectors().transpose() * _q;
29 else {
30 // apply rotation and translation to get uv coordinates
31 return Base::m_solver.eigenvectors().transpose() * (Base::m_w.convertToLocalBasis(_q));
32 }
33}
34
35template < class DataPoint, class _WFunctor, typename T>
36template <bool ignoreTranslation>
37typename CovariancePlaneFitImpl<DataPoint, _WFunctor, T>::VectorType
38CovariancePlaneFitImpl<DataPoint, _WFunctor, T>::tangentPlaneToWorld (const VectorType& _lq) const
39{
40 if (ignoreTranslation)
41 return Base::m_solver.eigenvectors().transpose().inverse() * _lq;
42 else {
43 return Base::m_w.convertToGlobalBasis(Base::m_solver.eigenvectors().transpose().inverse() * _lq);
44 }
45}
46
47
48
49template < class DataPoint, class _WFunctor, int DiffType, typename T>
52{
53 PONCA_MULTIARCH_STD_MATH(sqrt);
54
55 Base::finalize();
56 // Test if base finalize end on a viable case (stable / unstable)
57 if (this->isReady())
58 {
59 VectorType barycenter = Base::barycenterLocal();
60 VectorArray dBarycenter = Base::barycenterDerivatives();
61
62 // pre-compute shifted eigenvalues to apply the pseudo inverse of C - lambda_0 I
63 Scalar epsilon = Scalar(2) * Eigen::NumTraits<Scalar>::epsilon();
64 Scalar consider_as_zero = Scalar(2) * std::numeric_limits<Scalar>::denorm_min();
65
66 // This is where the limitation to 3d comes from.
67 // \fixme Replace shift in 2d subspace by any subspace with co-dimension 1
68 Eigen::Matrix<Scalar,2,1> shifted_eivals = Base::m_solver.eigenvalues().template tail<2>().array() - Base::m_solver.eigenvalues()(0);
69 if(shifted_eivals(0) < consider_as_zero || shifted_eivals(0) < epsilon * shifted_eivals(1)) shifted_eivals(0) = 0;
70 if(shifted_eivals(1) < consider_as_zero) shifted_eivals(1) = 0;
71
72
73 for(int k=0; k<Base::NbDerivatives; ++k)
74 {
75 VectorType normal = Base::primitiveGradient();
76 // The derivative of 'normal' is the derivative of the smallest eigenvector.
77 // Since the covariance matrix is real and symmetric, it is equal to:
78 // n' = - (C - lambda_0 I)^+ C' n
79 // Where ^+ denotes the pseudo-inverse.
80 // Since we already performed the eigenvalue decomposition of the matrix C,
81 // we can directly apply the pseudo inverse by observing that:
82 // (C - lambda_0 I) = V (L - lambda_0 I) V^T
83 // where V is the eigenvector matrix, and L the eigenvalue diagonal matrix.
84 Eigen::Matrix<Scalar,2,1> z = - Base::m_solver.eigenvectors().template rightCols<2>().transpose() * (Base::m_dCov[k] * normal);
85 if(shifted_eivals(0)>0) z(0) /= shifted_eivals(0);
86 if(shifted_eivals(1)>0) 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 // \fixme we shouldn't need this normalization, however currently the derivatives are overestimated by a factor 2
95 m_dNormal /= Scalar(2.);
96 }
97 }
98
99 return Base::m_eCurrentState;
100}
[CovariancePlaneFit Definition]
typename DataPoint::Scalar Scalar
Alias to scalar type.
typename Base::VectorType VectorType
Alias to vector type.
typename Base::VectorArray VectorArray
Alias to vector derivatives array.
Plane fitting procedure using only points position.
typename Base::VectorType VectorType
Alias to vector type.
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
@ STABLE
The fitting is stable and ready to use.
Definition: enums.h:17