Ponca  7d8ac87a7de01d881c9fde3c42e397b44bffb901
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>
12{
13 if (Base::finalize() == STABLE)
14 {
15 if (Base::plane().isValid())
16 Base::m_eCurrentState = CONFLICT_ERROR_FOUND;
17 Base::setPlane(Base::m_solver.eigenvectors().col(0), Base::barycenterLocal());
18 }
19
20 return Base::m_eCurrentState;
21}
22
23template <class DataPoint, class _NFilter, typename T>
25 DataPoint, _NFilter, T>::worldToTangentPlane(const VectorType& _q, bool _isPositionVector) const
26{
27 return Base::m_solver.eigenvectors().transpose() *
28 Base::getNeighborFilter().convertToLocalBasis(_q, _isPositionVector);
29}
30
31template <class DataPoint, class _NFilter, typename T>
33 DataPoint, _NFilter, T>::tangentPlaneToWorld(const VectorType& _lq, bool _isPositionVector) const
34{
35 return Base::getNeighborFilter().convertToGlobalBasis(Base::m_solver.eigenvectors().transpose().inverse() * _lq,
37}
38
39template <class DataPoint, class _NFilter, int DiffType, typename T>
41{
42 PONCA_MULTIARCH_CU_STD_FUNC(numeric_limits);
43
44 Base::finalize();
45 // Test if base finalize end on a viable case (stable / unstable)
46 if (this->isReady())
47 {
48 VectorType barycenter = Base::barycenterLocal();
49 VectorArray dBarycenter = Base::barycenterDerivatives();
50
51 // pre-compute shifted eigenvalues to apply the pseudo inverse of C - lambda_0 I
52 Scalar epsilon = Scalar(2) * Eigen::NumTraits<Scalar>::epsilon();
54
55 // This is where the limitation to 3d comes from.
56 // \fixme Replace shift in 2d subspace by any subspace with co-dimension 1
57 Eigen::Matrix<Scalar, 2, 1> shifted_eivals =
58 Base::m_solver.eigenvalues().template tail<2>().array() - Base::m_solver.eigenvalues()(0);
60 shifted_eivals(0) = 0;
62 shifted_eivals(1) = 0;
63
64 for (int k = 0; k < Base::NbDerivatives; ++k)
65 {
66 VectorType normal = Base::primitiveGradient();
67 // The derivative of 'normal' is the derivative of the smallest eigenvector.
68 // Since the covariance matrix is real and symmetric, it is equal to:
69 // n' = - (C - lambda_0 I)^+ C' n
70 // Where ^+ denotes the pseudo-inverse.
71 // Since we already performed the eigenvalue decomposition of the matrix C,
72 // we can directly apply the pseudo inverse by observing that:
73 // (C - lambda_0 I) = V (L - lambda_0 I) V^T
74 // where V is the eigenvector matrix, and L the eigenvalue diagonal matrix.
75 Eigen::Matrix<Scalar, 2, 1> z =
76 -Base::m_solver.eigenvectors().template rightCols<2>().transpose() * (Base::m_dCov[k] * normal);
77 if (shifted_eivals(0) > 0)
78 z(0) /= shifted_eivals(0);
79 if (shifted_eivals(1) > 0)
80 z(1) /= shifted_eivals(1);
81 m_dNormal.col(k) = Base::m_solver.eigenvectors().template rightCols<2>() * z;
82
84 if (k > 0 || !Base::isScaleDer())
85 dDiff(Base::isScaleDer() ? k - 1 : k) += 1;
86 m_dDist(k) = m_dNormal.col(k).dot(barycenter) + normal.dot(dDiff);
87 }
88 }
89
90 return Base::m_eCurrentState;
91}
Aggregator class used to declare specialized structures using CRTP.
Definition basket.h:318
[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.
Ponca::MeanPosition T barycenter() const
where are all the point samples in 's neighborhood
Definition mean.h:47
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