Ponca  8e4373a7fc557bbfb1afb9210d70f03872388d04
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
10#include PONCA_MULTIARCH_INCLUDE_STD(cmath)
11#include PONCA_MULTIARCH_INCLUDE_STD(limits)
12
13template < class DataPoint, class _NFilter, typename T>
16{
17 if (Base::finalize() == STABLE) {
18 if (Base::plane().isValid()) Base::m_eCurrentState = CONFLICT_ERROR_FOUND;
19 Base::setPlane(Base::m_solver.eigenvectors().col(0), Base::barycenterLocal());
20 }
21
22 return Base::m_eCurrentState;
23}
24
25template < class DataPoint, class _NFilter, typename T>
28{
29 return Base::m_solver.eigenvectors().transpose() * (Base::getNeighborFilter().convertToLocalBasis(_q));
30}
31
32template < class DataPoint, class _NFilter, typename T>
35{
36 return Base::getNeighborFilter().convertToGlobalBasis(Base::m_solver.eigenvectors().transpose().inverse() * _lq);
37}
38
39
40
41template < class DataPoint, class _NFilter, int DiffType, typename T>
44{
45 PONCA_MULTIARCH_STD_MATH(sqrt);
46 PONCA_MULTIARCH_STD_MATH(numeric_limits);
47
48 Base::finalize();
49 // Test if base finalize end on a viable case (stable / unstable)
50 if (this->isReady())
51 {
52 VectorType barycenter = Base::barycenterLocal();
53 VectorArray dBarycenter = Base::barycenterDerivatives();
54
55 // pre-compute shifted eigenvalues to apply the pseudo inverse of C - lambda_0 I
56 Scalar epsilon = Scalar(2) * Eigen::NumTraits<Scalar>::epsilon();
57 Scalar consider_as_zero = Scalar(2) * numeric_limits<Scalar>::denorm_min();
58
59 // This is where the limitation to 3d comes from.
60 // \fixme Replace shift in 2d subspace by any subspace with co-dimension 1
61 Eigen::Matrix<Scalar,2,1> shifted_eivals = Base::m_solver.eigenvalues().template tail<2>().array() - Base::m_solver.eigenvalues()(0);
62 if(shifted_eivals(0) < consider_as_zero || shifted_eivals(0) < epsilon * shifted_eivals(1)) shifted_eivals(0) = 0;
63 if(shifted_eivals(1) < consider_as_zero) shifted_eivals(1) = 0;
64
65
66 for(int k=0; k<Base::NbDerivatives; ++k)
67 {
68 VectorType normal = Base::primitiveGradient();
69 // The derivative of 'normal' is the derivative of the smallest eigenvector.
70 // Since the covariance matrix is real and symmetric, it is equal to:
71 // n' = - (C - lambda_0 I)^+ C' n
72 // Where ^+ denotes the pseudo-inverse.
73 // Since we already performed the eigenvalue decomposition of the matrix C,
74 // we can directly apply the pseudo inverse by observing that:
75 // (C - lambda_0 I) = V (L - lambda_0 I) V^T
76 // where V is the eigenvector matrix, and L the eigenvalue diagonal matrix.
77 Eigen::Matrix<Scalar,2,1> z = - Base::m_solver.eigenvectors().template rightCols<2>().transpose() * (Base::m_dCov[k] * normal);
78 if(shifted_eivals(0)>0) z(0) /= shifted_eivals(0);
79 if(shifted_eivals(1)>0) z(1) /= shifted_eivals(1);
80 m_dNormal.col(k) = Base::m_solver.eigenvectors().template rightCols<2>() * z;
81
82 VectorType dDiff = dBarycenter.col(k);
83 if(k>0 || !Base::isScaleDer())
84 dDiff(Base::isScaleDer() ? k-1 : k) += 1;
85 m_dDist(k) = m_dNormal.col(k).dot(barycenter) + normal.dot(dDiff);
86
87 // \fixme we shouldn't need this normalization, however currently the derivatives are overestimated by a factor 2
88 m_dNormal /= Scalar(2.);
89 }
90 }
91
92 return Base::m_eCurrentState;
93}
[CovariancePlaneFit Definition]
typename Base::VectorArray VectorArray
Alias to vector derivatives array.
typename Base::VectorType VectorType
Alias to vector type.
typename DataPoint::Scalar Scalar
Alias to scalar type.
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