-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuncertain.cpp
More file actions
139 lines (109 loc) · 4.04 KB
/
uncertain.cpp
File metadata and controls
139 lines (109 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad).
* Copyright (c) 2022-2025 Jochen Meidow, Fraunhofer IOSB
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "uncertain.h"
#include "qlogging.h"
#include "usegment.h"
#include "ustraightline.h"
#include <QDebug>
#include <QStringLiteral>
#include "qassert.h"
#include "qtdeprecationdefinitions.h"
#include <cassert>
#include <cfloat>
#include <cmath>
#include <Eigen/Core>
#include <Eigen/Eigenvalues>
namespace Uncertain {
//! Nullspace of row vector
Matrix<double,3,2> BasicEntity2D::null( const Vector3d &xs )
{
// cf. PCV, eq. (A.120)
//if ( fabs(xs.norm()-1.) > T_ZERO )
// qDebug() << xs;
#ifdef QT_DEBUG
QString const what = QStringLiteral("norm(x) = %1").arg(QString::number(xs.norm()));
Q_ASSERT_X(std::fabs(xs.norm() - 1.) <= FLT_EPSILON, Q_FUNC_INFO, what.toStdString().data());
#endif
Eigen::Index const N = xs.size();
VectorXd x0 = xs.head(N-1);
double xN = xs(N-1);
if ( xN < 0.0 ) {
x0 = -x0;
xN = -xN;
}
MatrixXd JJ( N, N-1);
JJ.topRows(N-1) = MatrixXd::Identity(N-1,N-1) -x0*x0.adjoint()/(1.+xN);
JJ.bottomRows(1) = -x0.adjoint();
const VectorXd check = JJ.adjoint()*xs;
Q_ASSERT_X( check.norm() <= FLT_EPSILON,
Q_FUNC_INFO,
"not a zero vector");
return JJ;
}
//! Check if matrix MM is a proper covariance matrix
bool isCovMat( const MatrixXd & MM )
{
// qDebug() << Q_FUNC_INFO;
const Eigen::SelfAdjointEigenSolver<MatrixXd> eig( MM, Eigen::ComputeEigenvectors);
Eigen::VectorXcd ev = eig.eigenvalues();
#ifdef QT_DEBUG
if ( (ev.real().array() < -DBL_EPSILON ).any() ) {
for ( Eigen::Index i=0; i< ev.size(); i++) {
qDebug().noquote() << QStringLiteral( "(%1,%2)")
.arg( ev(i).real() )
.arg( ev(i).imag() );
}
}
#endif
return (ev.real().array() >= -DBL_EPSILON ).all();
}
//! Spherically normalize the entity
void BasicEntity2D::normalizeSpherical()
{
// qDebug() << Q_FUNC_INFO;
const Matrix3d I = Matrix3d::Identity();
assert(m_val.norm() > 0.0);
Matrix3d const Jac = (I - m_val*m_val.adjoint()/m_val.squaredNorm()) / m_val.norm();
m_cov = Jac*m_cov*Jac.adjoint();
m_val.normalize(); // x = x /norm(x)
}
//! Check if uncertainty entity is identical with uncertain entity 'us'
bool BasicEntity2D::isIdenticalTo( const BasicEntity2D & us,
const double T) const
{
BasicEntity2D a(*this);
BasicEntity2D b(us); // copies
// fix ambiguities
a.normalizeSpherical();
b.normalizeSpherical();
int idx = 0; // visitor
a.v().cwiseAbs().maxCoeff( &idx ); // [~,idx] = max( abs(a) )
a.m_val *= sign( a.v()(idx) ); // a = a*sign( a(idx) );
b.m_val *= sign( b.v()(idx) ); // b = b*sign( b(idx) );
Matrix<double, 3, 2> const JJ = null(a.v()); // (A.120)
Vector2d const d = JJ.adjoint()*( a.v() -b.v() ); // (10.141)
Eigen::Matrix2d const Cov_dd = JJ.adjoint() * (a.Cov() + b.Cov()) * JJ;
// return d.adjoint()*Cov_dd.inverse()*d < T; // dof = 2
return d.dot(Cov_dd.inverse()*d) < T; // dof = 2
}
//! Skew-symmetric cross product matrix S(x): a x b = S(a)*b
Matrix3d skew( const Vector3d & x)
{
return (Matrix3d() << 0.,-x(2),x(1), x(2),0.,-x(0), -x(1),x(0),0.).finished();
}
} // namespace Uncertain