Skip to content

Commit a790e7b

Browse files
committed
new template base class
1 parent 0ab7587 commit a790e7b

File tree

6 files changed

+151
-18
lines changed

6 files changed

+151
-18
lines changed

src/greasepad.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ HEADERS += \
8181
statistics/uniform.h \
8282
uncertain/quncertain.h \
8383
uncertain/udistance.h \
84+
uncertain/uelement.h \
8485
uncertain/uncertain.h \
8586
uncertain/upoint.h \
8687
uncertain/usegment.h \

src/uncertain/uelement.h

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

src/uncertain/upoint.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "matfun.h"
2121
#include "statistics/iscov.h"
2222
#include "udistance.h"
23-
#include "uncertain.h"
23+
#include "uncertain/uelement.h"
2424
#include "upoint.h"
2525
#include "ustraightline.h"
2626

@@ -38,7 +38,9 @@
3838

3939
using Eigen::Matrix;
4040
using Eigen::Matrix2d;
41+
using Eigen::Matrix3d;
4142
using Eigen::Vector2d;
43+
using Eigen::Vector3d;
4244

4345
using Matfun::sign;
4446
using Matfun::cof3;
@@ -51,7 +53,7 @@ namespace Uncertain {
5153

5254
//! Construction of uncertain point via 3-vector x and its covariance matrix
5355
uPoint::uPoint( const Vector3d & x, const Matrix3d & Sigma_xx)
54-
: BasicEntity2D (x, Sigma_xx)
56+
: uElement (x, Sigma_xx)
5557
{
5658
// qDebug() << Q_FUNC_INFO;
5759
}

src/uncertain/upoint.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
#ifndef UPOINT_H
2020
#define UPOINT_H
2121

22+
#include "Eigen/Core"
23+
2224
#include "geometry/aabb.h" // axis-aligned bounding box
23-
#include "uncertain.h"
25+
#include "uelement.h"
2426

2527

2628
namespace Uncertain {
@@ -32,18 +34,18 @@ using Geometry::Aabb;
3234

3335

3436
//! Uncertain point
35-
class uPoint : public BasicEntity2D
37+
class uPoint : public uElement<3>
3638
{
3739
public:
3840
uPoint() = delete;
39-
uPoint( const Vector3d & x,
40-
const Matrix3d & Sigma_xx);
41+
uPoint( const Eigen::Vector3d & x,
42+
const Eigen::Matrix3d & Sigma_xx);
4143

4244
[[nodiscard]] Aabb<double,2> bbox() const;
43-
[[nodiscard]] Matrix3d conicMatrix(double) const;
45+
[[nodiscard]] Eigen::Matrix3d conicMatrix(double) const;
4446
[[nodiscard]] uPoint euclidean() const;
4547
[[nodiscard]] uPoint sphericalNormalized() const;
46-
[[nodiscard]] uPoint transformed( const Matrix3d & TT) const;
48+
[[nodiscard]] uPoint transformed( const Eigen::Matrix3d & TT) const;
4749
[[nodiscard]] uStraightLine cross( const uPoint &) const;
4850
[[nodiscard]] uDistance distanceAlgebraicTo( const uStraightLine & ul) const;
4951
[[nodiscard]] uDistance distanceEuclideanTo( const uStraightLine & ul) const;

src/uncertain/ustraightline.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include "matfun.h"
2525
#include "statistics/iscov.h"
26-
#include "uncertain.h"
26+
#include "uncertain/uelement.h"
2727
#include "upoint.h"
2828
#include "ustraightline.h"
2929

@@ -55,7 +55,7 @@ namespace Uncertain {
5555
//! Uncertain straight line, defined by a 3-vector and its covariance matrix
5656
uStraightLine::uStraightLine(const Vector3d & l,
5757
const Matrix3d & Sigma_ll)
58-
: BasicEntity2D( l, Sigma_ll )
58+
: uElement( l, Sigma_ll )
5959
{
6060
/* check: null(Sigma_ll)
6161
Eigen::FullPivLU<MatrixXd> lu(Sigma_ll);

src/uncertain/ustraightline.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,28 @@
2121

2222
#include "Eigen/Core"
2323

24-
#include "uncertain.h"
24+
#include "uelement.h"
2525

2626
#include <cmath>
2727

2828

2929
namespace Uncertain {
3030

31-
using Eigen::VectorXd;
32-
3331
class uPoint;
3432

35-
3633
//! Uncertain straight line
37-
class uStraightLine : public BasicEntity2D
34+
class uStraightLine : public uElement<3>
3835
{
3936
public:
4037
uStraightLine() = default;
41-
uStraightLine( const Vector3d & l, const Matrix3d & Sigma_ll);
38+
uStraightLine( const Eigen::Vector3d & l, const Eigen::Matrix3d & Sigma_ll);
4239

43-
[[nodiscard]] static uStraightLine estim( const VectorXd & xi, const VectorXd & yi);
40+
[[nodiscard]] static uStraightLine
41+
estim( const Eigen::VectorXd & xi, const Eigen::VectorXd & yi);
4442

4543
[[nodiscard]] uStraightLine euclidean() const;
4644
[[nodiscard]] uStraightLine sphericalNormalized() const;
47-
[[nodiscard]] Matrix3d conicMatrix(double) const;
45+
[[nodiscard]] Eigen::Matrix3d conicMatrix(double) const;
4846

4947
//! Angle between this straight line and the x-axis in radians
5048
[[nodiscard]] double angle_rad() const { return std::atan2( v(1),v(0) ); }

0 commit comments

Comments
 (0)