-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstraints.h
More file actions
304 lines (246 loc) · 9.22 KB
/
constraints.h
File metadata and controls
304 lines (246 loc) · 9.22 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad).
* Copyright (c) 2022-2026 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 <Eigen/Core>
#include <Eigen/Dense>
#include <functional>
#include <map>
#include <memory>
//! Geometric constraints (relations)
namespace Constraint {
using Eigen::MatrixXd;
using Eigen::Dynamic;
using Eigen::Index;
using Eigen::Vector;
using Eigen::VectorXd;
//! Base class for constraints
class ConstraintBase
{
public:
ConstraintBase & operator= ( ConstraintBase &&) = delete;
ConstraintBase( ConstraintBase &&) = delete;
ConstraintBase (const ConstraintBase & other) = default;
ConstraintBase & operator= ( const ConstraintBase & other) = delete;
ConstraintBase() = default;
virtual ~ConstraintBase() = default;
[[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
//! Compute Jacobian w.r.t. observations
[[nodiscard]] virtual MatrixXd Jacobian(
const Vector<Index,Dynamic> &idxx,
const VectorXd &l0,
const VectorXd &l) const = 0;
//! Compute contradictions with adjusted observations
[[nodiscard]] virtual VectorXd contradict(
const Vector<Index,Dynamic> &idxx,
const VectorXd &l0 ) const = 0;
//! Check if constraint is of a certain, specified type
template<typename T>
[[nodiscard]] bool isInstanceOf() const {
return ( dynamic_cast<T const*>(this) != nullptr);
}
//! Clone constraints via nonvirtual interface pattern
[[nodiscard]] virtual std::shared_ptr<ConstraintBase> clone() const = 0;
};
/*
* 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 {
return std::make_shared<Derived>(static_cast<Derived const&> (*this));
};
ConstraintCRTP & operator= (const ConstraintCRTP &) = delete;
ConstraintCRTP & operator= (ConstraintCRTP &&) = delete;
~ConstraintCRTP() override = default;
private:
ConstraintCRTP( const ConstraintCRTP &) = default;
ConstraintCRTP() = default;
ConstraintCRTP(ConstraintCRTP&&) noexcept = default;
friend Derived;
using VectorXidx = Vector<Index,Dynamic>;
};
//! Concurrence constraint (three copunctual straight lines)
class Copunctual : public ConstraintCRTP<Copunctual>
{
public:
[[nodiscard]] MatrixXd Jacobian(
const Vector<Index,Dynamic> &idxx,
const VectorXd &l0,
const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict(
const Vector<Index,Dynamic> &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 const int s_dof = 1;
static const int s_arity = 3;
};
//! Parallelism constraint
class Parallel : public ConstraintCRTP<Parallel>
{
public:
[[nodiscard]] MatrixXd Jacobian(
const Vector<Index,Dynamic> &idxx,
const VectorXd &l0,
const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict(
const Vector<Index,Dynamic> &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 const int s_dof = 1;
static const int s_arity = 2;
};
//! Vertical straight line
class Vertical : public ConstraintCRTP<Vertical>
{
public:
[[nodiscard]] MatrixXd Jacobian(
const Vector<Index,Dynamic> &idxx,
const VectorXd &l0,
const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict(
const Vector<Index,Dynamic> &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;
};
//! Horizontal straight line
class Horizontal : public ConstraintCRTP<Horizontal>
{
public:
[[nodiscard]] MatrixXd Jacobian(
const Vector<Index,Dynamic> &idxx,
const VectorXd &l0,
const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict(
const Vector<Index,Dynamic> &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;
};
//! Diagonal straight line
class Diagonal : public ConstraintCRTP<Diagonal>
{
public:
[[nodiscard]] MatrixXd Jacobian(
const Vector<Index,Dynamic> &idxx,
const VectorXd &l0,
const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict(
const Vector<Index,Dynamic> &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;
};
//! Orthogonality constraint
class Orthogonal : public ConstraintCRTP<Orthogonal>
{
public:
[[nodiscard]] MatrixXd Jacobian(
const Vector<Index,Dynamic> &idxx,
const VectorXd &l0,
const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict(
const Vector<Index,Dynamic> &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 const int s_dof = 1;
static const int s_arity = 2;
};
/*
//! Identity constraint
class Identical : public ConstraintCRTP<Identical>
{
public:
[[nodiscard]] MatrixXd Jacobian(
const Vector<Index,Dynamic> &idxx,
const VectorXd &l0,
const VectorXd &l) const override;
[[nodiscard]] VectorXd contradict(
const Vector<Index,Dynamic> &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;
};
*/
//! Factory for constraints, as singleton
class Factory {
private:
std::map<char, std::function <std::shared_ptr<ConstraintBase>()> > m_map;
~Factory() { m_map.clear(); }
public:
Factory(const Factory & other) = delete; // not clonable
Factory(Factory && other) = delete; // not movable
Factory & operator= (Factory && other) = delete;
Factory & operator= (const Factory & other) = delete;
Factory() {
// qDebug() << Q_FUNC_INFO;
// qDebug() << typeid(Horizontal).name();
// qDebug() << typeid(Horizontal).hash_code();
m_map['h'] = [] {return std::make_shared<Horizontal>(); };
m_map['v'] = [] {return std::make_shared<Vertical>(); };
m_map['d'] = [] {return std::make_shared<Diagonal>(); };
m_map['o'] = [] {return std::make_shared<Orthogonal>(); };
m_map['c'] = [] {return std::make_shared<Copunctual>(); };
m_map['p'] = [] {return std::make_shared<Parallel>(); };
}
static Factory *getInstance(){
static Factory instance;
return & instance;
}
std::shared_ptr<ConstraintBase> create(const char c) {
// assert( m_map.find(c) != m_map.end() && "unknown key"); // C++20: contains(c)
if ( m_map.find(c) == m_map.end() ) {
return nullptr;
}
return m_map[c]();
}
};
} // namespace Constraint
#endif // CONSTRAINTS_H