Ponca  cb266528645085a12930c827fbb8336cd937ecbb
Point Cloud Analysis library
Loading...
Searching...
No Matches
Eigen2x2Solver.h
1/*
2This Source Code Form is subject to the terms of the Mozilla Public
3 License, v. 2.0. If a copy of the MPL was not distributed with this
4 file, You can obtain one at http://mozilla.org/MPL/2.0/.
5*/
6
7#pragma once
8
9#include <Eigen/Dense>
10#include <Eigen/Core>
11
12#include "../defines.h"
13#include "../floatutils.h"
14
15namespace Ponca
16{
17 namespace internal
18 {
25 template <typename Scalar>
27 {
28 public:
29 using Matrix2 = Eigen::Matrix<Scalar, 2, 2>;
30 using Vector2 = Eigen::Matrix<Scalar, 2, 1>;
31
32 PONCA_MULTIARCH inline void computeDirect(const Matrix2& _w)
33 {
34 PONCA_MULTIARCH_STD_MATH(sqrt);
35 PONCA_MULTIARCH_STD_MATH(max);
36
37 // Eigenvalues: roots of k^2 - tr(w) k + det(w) = 0
38 const Scalar tr = Scalar(0.5) * (_w(0, 0) + _w(1, 1)); // 0.5 here reduces mult count
39 const Scalar det = _w(0, 0) * _w(1, 1) - _w(0, 1) * _w(1, 0);
40 const Scalar delta = sqrt(max(tr * tr - det, Scalar(0)));
41 m_eivals << tr - delta, tr + delta;
42
43 const Scalar tol = Eigen::NumTraits<Scalar>::dummy_precision() * (_w.cwiseAbs().maxCoeff() + Scalar(1));
44
45 // Eigenvectors, pick an orthonormal pair when w = kI as every vector is an eigenvector...
46 // (v1 first, so that the fallback for v0 can be made orthogonal to it)
47 Vector2 v0, v1;
48 if (!eigenvector(_w, m_eivals(1), tol, v1))
49 v1 = Vector2::UnitY();
50 // A symmetric matrix has orthogonal eigenvectors: take v0 orthogonal to v1 instead of computing it, as
51 // computing it independently is ill-conditioned near umbilics (eigengap ~ 0)
52 if (_w(0, 1) == _w(1, 0) || !eigenvector(_w, m_eivals(0), tol, v0))
53 v0 = Vector2(v1.y(), -v1.x());
54
55 m_eivecs.col(0) = v0;
56 m_eivecs.col(1) = v1;
57 }
58
60 PONCA_MULTIARCH [[nodiscard]] inline const Vector2& eigenvalues() const { return m_eivals; }
61
63 PONCA_MULTIARCH [[nodiscard]] inline const Matrix2& eigenvectors() const { return m_eivecs; }
64
65 PONCA_MULTIARCH [[nodiscard]] inline bool operator==(const Eigen2x2Solver<Scalar>& _other) const
66 {
67 return internal::isApprox(m_eivals, _other.m_eivals) && internal::isApprox(m_eivecs, _other.m_eivecs);
68 }
69
70 private:
71 Vector2 m_eivals{Vector2::Zero()};
72 Matrix2 m_eivecs{Matrix2::Identity()};
73
75 PONCA_MULTIARCH static inline bool eigenvector(const Matrix2& _w, Scalar _k, Scalar _tol, Vector2& _v)
76 {
77 const Vector2 v0(_w(0, 1), _k - _w(0, 0)); // 1st candidate, 0 if w = kI
78 const Vector2 v1(_k - _w(1, 1), _w(1, 0)); // 2cd candidate, 0 if w = kI
79
80 // Use the one with the highest norm avoid numerical instabilities
81 _v = v0.squaredNorm() > v1.squaredNorm() ? v0 : v1;
82
83 const Scalar n = _v.norm();
84 if (n <= _tol)
85 return false;
86
87 _v /= n;
88 return true;
89 }
90 };
91 } // namespace internal
92} // namespace Ponca
93
Closed-form eigen decomposition of a 2x2 matrix with real eigenvalues, not necessarily symmetric.
const Vector2 & eigenvalues() const
Eigenvalues, in increasing order.
const Matrix2 & eigenvectors() const
Normalized eigenvectors (columns), in the order of eigenvalues().
Copyright (c) 2022 Jacques-Olivier Lachaud (jacques-olivier.lachaud@univ-savoie.fr) Laboratory of Mat...
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition concepts.h:11