-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstraints.h
More file actions
259 lines (206 loc) · 9.42 KB
/
constraints.h
File metadata and controls
259 lines (206 loc) · 9.42 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* 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/>.
*/
#ifndef CONSTRAINTS_H
#define CONSTRAINTS_H
#include <QDebug>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <memory>
#include "cstdint"
#include "matrix.h"
//! Geometric constraints (relations)
namespace Constraint {
using Eigen::Matrix3d;
using Eigen::MatrixXd;
using Eigen::Vector3d;
using Eigen::VectorXd;
using Eigen::VectorXi;
using Eigen::VectorXidx;
//! Base class for constraints
class ConstraintBase
{
public:
ConstraintBase & operator= ( ConstraintBase &&) = delete;
ConstraintBase( ConstraintBase &&) = delete;
ConstraintBase (const ConstraintBase & other) = delete;
ConstraintBase & operator= ( const ConstraintBase & other) = delete;
ConstraintBase();
virtual ~ConstraintBase() = default;
protected:
static MatrixXd null( const VectorXd &xs ); //!< Nullspace of row vector
static MatrixXd Rot_ab( const VectorXd &a,
const VectorXd &b); //!< Minimal rotation between two vectors as rotation matrix
public:
//! Status: unevaluated, required, obsolete (redundant)
enum Status : std::uint8_t { UNEVAL=0, REQUIRED, OBSOLETE };
// virtual void serialize( QDataStream &out) const; //!< Serialize (Qt)
// static std::shared_ptr<ConstraintBase> deserialize( QDataStream &in ); //!< Deserialization (Qt)
[[nodiscard]] virtual const char *type_name() const = 0; //!< Get type name of constraint
[[nodiscard]] virtual int dof() const = 0; //!< Get degrees of freedom for this constraint
[[nodiscard]] virtual int arity() const = 0; //!< Get number of involved entities, i.e., straight lines
[[nodiscard]] Status status() const { return m_status; } //!< Get status (required, obsolete, unevaluated)
[[nodiscard]] bool enforced() const { return m_enforced; } //!< Constraint is enforced?
[[nodiscard]] bool required() const { return m_status==REQUIRED; } //!< Constraint is required?
[[nodiscard]] bool obsolete() const { return m_status==OBSOLETE; } //!< Constraint is obsolete (redundant)?
[[nodiscard]] bool unevaluated() const { return m_status==UNEVAL; } //!< Constraint is unevaluated?
void setStatus( const Status s) { m_status = s; } //!< Set status (required, obsolete, unevaluated)
void setEnforced( const bool b) { m_enforced = b; } //!< Set status success of enforcement
//! Compute Jacobian w.r.t. observations
[[nodiscard]] virtual MatrixXd Jacobian( const Eigen::VectorXidx &idxx,
const VectorXd &l0, const VectorXd &l) const = 0;
//! Compute contradictions with adjusted observations
[[nodiscard]] virtual VectorXd contradict( const VectorXidx &idxx,
const VectorXd &l0 ) const = 0;
//! Check if constraint is of a certain, specified type
template<typename T>
bool is() { return ( dynamic_cast<T*>(this) != nullptr); }
//! Clone constraints via nonvirtual interface pattern
[[nodiscard]] virtual std::shared_ptr<ConstraintBase> clone() const = 0;
protected:
//! sign(0):=+1
template <typename T>
int sign(T val) const { return (T(0) <= val) - (val < T(0)); }
private:
Status m_status; // { UNEVAL=0 | REQUIRED | OBSOLETE };
bool m_enforced;
};
/*
* The curiously recurring template pattern (CRTP)
* is used to enable static polymorphism
* and to avoid duplicate clone functions in the derived classes.
*/
template <typename Derived>
class ConstraintCRTP : public ConstraintBase
{
public:
[[nodiscard]] std::shared_ptr<ConstraintBase> clone() const override {
auto T = std::make_shared<Derived>();
T->setStatus( this->status() );
T->setEnforced( this->enforced());
return T;
// return std::make_shared<Derived>(static_cast<Derived const&> (*this));
};
ConstraintCRTP( const ConstraintCRTP &) = delete;
ConstraintCRTP & operator= (const ConstraintCRTP &) = delete;
ConstraintCRTP & operator= (ConstraintCRTP &&) = delete;
~ConstraintCRTP() override = default;
private:
ConstraintCRTP() = default;
ConstraintCRTP(ConstraintCRTP&&) noexcept = default;
friend Derived;
};
//! Concurrence constraint (three copunctual straight lines)
class Copunctual : public ConstraintCRTP<Copunctual>
{
public:
[[nodiscard]] MatrixXd Jacobian( const VectorXidx &idxx, const VectorXd &l0, const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict( const VectorXidx &idxx, const VectorXd &l0) const override;
[[nodiscard]] int dof() const override {return s_dof;}
[[nodiscard]] int arity() const override {return s_arity;} //!< number of involved entities
private:
[[nodiscard]] const char* type_name() const override { return "copunctual"; }
static Matrix3d cof3(const Matrix3d &MM); //!< 3x3 cofactor matrix
static const int s_dof = 1;
static const int s_arity = 3;
};
//! Parallelism constraint
class Parallel : public ConstraintCRTP<Parallel>
{
public:
[[nodiscard]] MatrixXd Jacobian( const VectorXidx &idxx, const VectorXd &l0, const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict( const VectorXidx &idxx, const VectorXd &l0) const override;
[[nodiscard]] int dof() const override { return s_dof; }
[[nodiscard]] int arity() const override { return s_arity; }
private:
[[nodiscard]] const char* type_name() const override { return "parallel"; }
static Eigen::Matrix3d S3();
static const int s_dof = 1;
static const int s_arity = 2;
};
//! Vertical straight line
class Vertical : public ConstraintCRTP<Vertical>
{
public:
[[nodiscard]] MatrixXd Jacobian( const VectorXidx &idxx, const VectorXd &l0, const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict( const VectorXidx &idxx, const VectorXd &l0) const override;
[[nodiscard]] int dof() const override { return s_dof; }
[[nodiscard]] int arity() const override { return s_arity; }
private:
[[nodiscard]] const char* type_name() const override { return "vertical"; }
static const int s_dof = 1;
static const int s_arity = 1;
static Vector3d e2(); //!< [0,1,0]'
};
//! Horizontal straight line
class Horizontal : public ConstraintCRTP<Horizontal>
{
public:
[[nodiscard]] MatrixXd Jacobian( const VectorXidx &idxx, const VectorXd &l0, const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict( const VectorXidx &idxx, const VectorXd &l0) const override;
[[nodiscard]] int dof() const override { return s_dof; }
[[nodiscard]] int arity() const override { return s_arity; }
private:
[[nodiscard]] const char* type_name() const override { return "horizontal"; }
static const int s_dof = 1;
static const int s_arity = 1;
static Vector3d e1(); //!< [1,0,0]'
};
//! Diagonal straight line
class Diagonal : public ConstraintCRTP<Diagonal>
{
public:
[[nodiscard]] MatrixXd Jacobian( const VectorXidx &idxx, const VectorXd &l0, const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict( const VectorXidx &idxx, const VectorXd &l0) const override;
[[nodiscard]] int dof() const override { return s_dof; }
[[nodiscard]] int arity() const override { return s_arity; }
private:
[[nodiscard]] const char* type_name() const override { return "diagonal"; }
static const int s_dof = 1;
static const int s_arity = 1;
};
//! Orthogonallity constraint
class Orthogonal : public ConstraintCRTP<Orthogonal>
{
public:
[[nodiscard]] MatrixXd Jacobian( const VectorXidx &idxx, const VectorXd &l0, const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict( const VectorXidx &idxx, const VectorXd &l0) const override;
[[nodiscard]] int dof() const override { return s_dof; }
[[nodiscard]] int arity() const override { return s_arity; }
private:
[[nodiscard]] const char* type_name() const override { return "orthogonal"; }
static Matrix3d CC();
static const int s_dof = 1;
static const int s_arity = 2;
};
//! Identity constraint
class Identical : public ConstraintCRTP<Identical>
{
public:
[[nodiscard]] MatrixXd Jacobian( const VectorXidx &idxx, const VectorXd &l0, const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict( const VectorXidx &idxx, const VectorXd &l0) const override;
[[nodiscard]] int dof() const override {return s_dof;}
[[nodiscard]] int arity() const override {return s_arity;}
private:
[[nodiscard]] const char* type_name() const override {return "identical";}
static const int s_dof = 2;
static const int s_arity = 2;
template <typename T>
bool sameSign( T a, T b ) const { return a*b >= 0.; } // for debugging and assertion
};
} // namespace Constraint
#endif // CONSTRAINTS_H