|
| 1 | +/* |
| 2 | + * This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad). |
| 3 | + * Copyright (c) 2022-2026 Jochen Meidow, Fraunhofer IOSB |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | + |
| 20 | +#ifndef UELEMENT_H |
| 21 | +#define UELEMENT_H |
| 22 | + |
| 23 | +#include <Eigen/Core> |
| 24 | +#include <Eigen/Dense> |
| 25 | + |
| 26 | +#include <cassert> |
| 27 | +#include <utility> |
| 28 | + |
| 29 | +#include "geometry/skew.h" |
| 30 | +#include "kernel.h" |
| 31 | +#include "matfun.h" |
| 32 | +#include "statistics/iscov.h" |
| 33 | + |
| 34 | + |
| 35 | +//! Uncertain geometric entities |
| 36 | +namespace Uncertain { |
| 37 | + |
| 38 | +using Eigen::Matrix; |
| 39 | +using Eigen::Vector; |
| 40 | + |
| 41 | +using Geometry::skew; |
| 42 | + |
| 43 | +using Matfun::null; |
| 44 | +using Matfun::sign; |
| 45 | + |
| 46 | +using Stats::isCovMat; |
| 47 | + |
| 48 | + |
| 49 | +//! Base class for uncertain geometric elements, represented by homogeneous N-vectors |
| 50 | +template <int N> |
| 51 | +class uElement |
| 52 | +{ |
| 53 | + |
| 54 | +public: |
| 55 | + //! Construct geometric element with N-vector and its covariance matrix |
| 56 | + uElement( Vector<double,N> z, Matrix<double,N,N> Sigma_zz) |
| 57 | + : m_val(std::move(z)), m_cov(std::move(Sigma_zz)) |
| 58 | + { |
| 59 | + assert( m_val.size()==m_cov.cols() ); |
| 60 | + assert( isCovMat( m_cov) ); |
| 61 | + } |
| 62 | + uElement ( const uElement &) = default; //!< Copy constructor |
| 63 | + uElement( uElement &&) = default; //!< Move constructor |
| 64 | + uElement & operator= ( const uElement &&) = delete; //!< Move assignment |
| 65 | + |
| 66 | + ~uElement() = default; |
| 67 | + |
| 68 | + void normalizeSpherical(); |
| 69 | + |
| 70 | + //! Get covariance matrix |
| 71 | + [[nodiscard]] Matrix<double,N,N> Cov() const {return m_cov;} |
| 72 | + |
| 73 | + //! Get homogeneous N-vector representing the geometric element |
| 74 | + [[nodiscard]] Vector<double,N> v() const {return m_val;} |
| 75 | + |
| 76 | + //! Get i-th element of the homogeneous N-vector |
| 77 | + [[nodiscard]] double v( const int i) const {return m_val(i);} |
| 78 | + |
| 79 | + [[nodiscard]] bool isIdenticalTo( const uElement &s, double T) const; |
| 80 | + |
| 81 | +protected: |
| 82 | + uElement() = default; //!< default constructor |
| 83 | + uElement & operator= ( const uElement &) = default; //!< copy assignment |
| 84 | + |
| 85 | +private: |
| 86 | + Vector<double,N> m_val; //!< homogeneous N-vector representing the element |
| 87 | + Matrix<double,N,N> m_cov; //!< homogeneous NxN covariance matrix |
| 88 | +}; |
| 89 | + |
| 90 | + |
| 91 | +//! Spherically normalize the entity |
| 92 | +template <int N> |
| 93 | +void uElement<N>::normalizeSpherical() |
| 94 | +{ |
| 95 | + static const Matrix<double,N,N> Id = Matrix<double,N,N>::Identity(); |
| 96 | + assert( m_val.norm() > 0 ); |
| 97 | + const Matrix<double,N,N> Jac = (Id-m_val*m_val.transpose()/m_val.squaredNorm())/m_val.norm(); |
| 98 | + m_cov = Jac*m_cov*Jac.transpose(); |
| 99 | + m_val.normalize(); // x = x /norm(x) |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +//! Check if uncertain element is identical with other uncertain element |
| 104 | +template<int N> |
| 105 | +bool uElement<N>::isIdenticalTo(const uElement & us, |
| 106 | + const double T) const |
| 107 | +{ |
| 108 | + uElement a(*this); |
| 109 | + uElement b(us); |
| 110 | + |
| 111 | + // fix ambiguities |
| 112 | + a.normalizeSpherical(); |
| 113 | + b.normalizeSpherical(); |
| 114 | + |
| 115 | + int idx = 0; // visitor |
| 116 | + a.v().cwiseAbs().maxCoeff( &idx ); // [~,idx] = max( abs(a) ) |
| 117 | + a.m_val *= sign( a.v(idx) ); |
| 118 | + b.m_val *= sign( b.v(idx) ); |
| 119 | + |
| 120 | + const Matrix<double,N,N-1> Jac = null(a.v()); // (A.120) |
| 121 | + const Vector<double,N-1> d = Jac.transpose()*(a.v()-b.v() ); // (10.141) |
| 122 | + const Matrix<double,N-1,N-1> Sigma_dd = Jac.transpose()*(a.Cov()+b.Cov())*Jac; |
| 123 | + |
| 124 | + return d.dot( Sigma_dd.ldlt().solve(d) ) < T; |
| 125 | +} |
| 126 | + |
| 127 | +} // namespace Uncertain |
| 128 | + |
| 129 | + |
| 130 | +#endif // UELEMENT_H |
0 commit comments