-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstraints.cpp
More file actions
438 lines (341 loc) · 11.5 KB
/
constraints.cpp
File metadata and controls
438 lines (341 loc) · 11.5 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
* This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad).
* Copyright (c) 2022-2023 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 "constraints.h"
#include "matrix.h"
#include <QDebug>
namespace Constraint {
using Eigen::Vector3d;
using Eigen::Vector2cd;
using Eigen::Matrix;
#ifdef QT_DEBUG
static const double T_ZERO = 1e-5;
#endif
MatrixXd ConstraintBase::Rot_ab( const VectorXd &a,
const VectorXd &b)
{
Q_ASSERT( a.size()==b.size() );
#ifdef QT_DEBUG
Q_ASSERT( std::fabs( a.norm()-1.) < T_ZERO );
Q_ASSERT( std::fabs( b.norm()-1.) < T_ZERO );
#endif
return MatrixXd::Identity( a.size(),a.size())
+2*b*a.adjoint()
-(a+b)*(a+b).adjoint()/(1.+a.dot(b));
}
MatrixXd ConstraintBase::null( const VectorXd & xs )
{
// cf. PCV, eq. (A.120)
//if ( fabs(xs.norm()-1.) > T_ZERO )
// qDebug() << xs;
#ifdef QT_DEBUG
QString what = QStringLiteral("norm(x) = %1").arg( QString::number(xs.norm()) );
Q_ASSERT_X( fabs(xs.norm()-1.) <= T_ZERO,
"null(x)",
what.toStdString().data() ) ;
#endif
Eigen::Index 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();
VectorXd check = JJ.adjoint()*xs;
#ifdef QT_DEBUG
Q_ASSERT_X( check.norm() <= T_ZERO, Q_FUNC_INFO, "not a zero vector");
#endif
return JJ;
}
ConstraintBase::ConstraintBase() {
// qDebug().noquote() << Q_FUNC_INFO;
m_status = UNEVAL;
m_enforced = false;
}
std::shared_ptr<ConstraintBase> ConstraintBase::clone() const
{
// qDebug() << Q_FUNC_INFO;
std::shared_ptr<ConstraintBase> ptr = doClone();
auto & r = *ptr; // .get();
assert( typeid(r) == typeid(*this)
&& "ConstraintBase: doClone() incorrectly overridden" );
return ptr;
}
MatrixXd Orthogonal::Jacobian( const VectorXi & idxx,
const VectorXd &l0,
const VectorXd &l) const
{
Vector3d a0 = l0.segment( 3*idxx(0), 3);
Vector3d b0 = l0.segment( 3*idxx(1), 3);
Vector3d a = l.segment( 3*idxx(0), 3);
Vector3d b = l.segment( 3*idxx(1), 3);
Matrix<double,1,2> JJa;
Matrix<double,1,2> JJb;
JJa = b0.adjoint()*CC()*Rot_ab(a0,a)*null(a0);
JJb = a0.adjoint()*CC()*Rot_ab(b0,b)*null(b0);
MatrixXd Tmp(1,4);
Tmp << JJa, JJb;
return Tmp;
}
VectorXd Orthogonal::contradict( const VectorXi &idx,
const VectorXd &l0) const
{
Vector3d a = l0.segment (3*idx(0), 3);
Vector3d b = l0.segment (3*idx(1), 3);
VectorXd tmp(1);
tmp << a.dot( CC()*b );
return tmp;
}
//! Diag([1,1,0])
Matrix3d Orthogonal::CC()
{
const static Matrix3d tmp = (Matrix3d() << 1,0,0, 0,1,0, 0,0,0).finished();
return tmp;
}
std::shared_ptr<ConstraintBase> Orthogonal::doClone() const
{
auto T = std::make_shared<Orthogonal>();
T->setStatus( this->status() );
T->setEnforced( this->enforced());
return std::move(T);
}
MatrixXd Copunctual::Jacobian( const VectorXi & idx,
const VectorXd & l0,
const VectorXd & l) const
{
Vector3d a0 = l0.segment( 3*idx(0),3);
Vector3d b0 = l0.segment( 3*idx(1),3);
Vector3d c0 = l0.segment( 3*idx(2),3);
Vector3d a = l.segment( 3*idx(0),3);
Vector3d b = l.segment( 3*idx(1),3);
Vector3d c = l.segment( 3*idx(2),3);
Matrix3d MM = (Matrix3d() << a0,b0,c0 ).finished(); // [a0,b0,c0]
Matrix3d Adju = cof3(MM).adjoint(); // adjugate(MM)
MatrixXd Tmp(1,6);
Tmp << Adju.row(0)*Rot_ab(a0,a)*null(a0),
Adju.row(1)*Rot_ab(b0,b)*null(b0),
Adju.row(2)*Rot_ab(c0,c)*null(c0);
return Tmp;
}
VectorXd Copunctual::contradict( const VectorXi & idx,
const VectorXd & l0) const
{
Vector3d a = l0.segment( 3*idx(0), 3 );
Vector3d b = l0.segment( 3*idx(1), 3 );
Vector3d c = l0.segment( 3*idx(2), 3 );
Matrix3d MM = (Matrix3d() << a,b,c).finished(); // [a0,b0,c0]
VectorXd tmp(1);
tmp << MM.determinant();
return tmp;
}
//! 3x3 cofactor matrix, i.e., transposed adjugate
Matrix3d Copunctual::cof3( const Matrix3d & MM ) const
{
Matrix3d Cof;
Cof(0,0) = +MM(1,1)*MM(2,2) -MM(2,1)*MM(1,2);
Cof(0,1) = -MM(1,0)*MM(2,2) +MM(2,0)*MM(1,2);
Cof(0,2) = +MM(1,0)*MM(2,1) -MM(2,0)*MM(1,1);
Cof(1,0) = -MM(0,1)*MM(2,2) +MM(2,1)*MM(0,2);
Cof(1,1) = +MM(0,0)*MM(2,2) -MM(2,0)*MM(0,2);
Cof(1,2) = -MM(0,0)*MM(2,1) +MM(2,0)*MM(0,1);
Cof(2,0) = +MM(0,1)*MM(1,2) -MM(1,1)*MM(0,2);
Cof(2,1) = -MM(0,0)*MM(1,2) +MM(1,0)*MM(0,2);
Cof(2,2) = +MM(0,0)*MM(1,1) -MM(1,0)*MM(0,1);
return Cof;
}
std::shared_ptr<ConstraintBase> Copunctual::doClone() const
{
auto T = std::make_shared<Copunctual>();
T->setStatus( this->status() );
T->setEnforced( this->enforced() );
return std::move(T);
}
MatrixXd Identical::Jacobian( const VectorXi & idx,
const VectorXd & l0,
const VectorXd & l) const
{
Vector3d a0 = l0.segment( 3*idx(0),3 );
Vector3d b0 = l0.segment( 3*idx(1),3 );
Vector3d a = l.segment( 3*idx(0),3 );
Vector3d b = l.segment( 3*idx(1),3 );
Eigen::FullPivLU<MatrixXd> LU; // identical
Matrix<double,3,2> JJ; // identical
int idx1;
int idx2;
a0.cwiseAbs().maxCoeff( &idx1 );
b0.cwiseAbs().maxCoeff( &idx2 );
Q_ASSERT( sign( a0(idx1) )==sign( b0(idx2) ) );
Q_ASSERT( sameSign( a0(idx1), b0(idx2) ) );
LU.compute(a0.adjoint());
JJ = LU.kernel(); // JJ = null( a');
// d2 = JJ.adjoint()*(a -b); // (10.141)
Matrix<double,2,3> JJa = null(a0).adjoint() * Rot_ab(a,a0);
Matrix<double,2,3> JJb = null(b0).adjoint() * Rot_ab(b,b0);
MatrixXd Tmp(2,4);
Tmp << JJ.adjoint()*JJa.adjoint(), -JJ.adjoint()*JJb.adjoint();
return Tmp;
}
VectorXd Identical::contradict( const VectorXi & idx,
const VectorXd & l0) const
{
Vector3d a0 = l0.segment( 3*idx(0),3 );
Vector3d b0 = l0.segment( 3*idx(1),3 );
Eigen::FullPivLU<MatrixXd> LU;
// check sign ............................................
int idx1;
int idx2;
a0.head(2).cwiseAbs().maxCoeff( &idx1 );
b0.head(2).cwiseAbs().maxCoeff( &idx2 );
Q_ASSERT( sameSign(a0(idx1), b0(idx2)) );
// LU.compute( a0.adjoint() );
// Matrix<double,3,2> JJ = LU.kernel(); // JJ = null( a0');
Matrix<double,3,2> JJ = null(a0);
Eigen::Vector2d d2 = JJ.adjoint()*(a0 -b0); // (10.141)
return d2;
}
std::shared_ptr<ConstraintBase> Identical::doClone() const
{
auto T = std::make_shared<Identical>();
T->setStatus( this->status() );
T->setEnforced( this->enforced());
return std::move(T);
}
MatrixXd Parallel::Jacobian( const VectorXi & idx,
const VectorXd & l0,
const VectorXd & l) const
{
Vector3d a0 = l0.segment( 3*idx(0),3 );
Vector3d b0 = l0.segment( 3*idx(1),3 );
Vector3d a = l.segment( 3*idx(0),3 );
Vector3d b = l.segment( 3*idx(1),3 );
// Matrix<double,1,2> JJa;
// Matrix<double,1,2> JJb;
Matrix<double,1,2> JJa = -b0.adjoint()*S3()*Rot_ab(a0,a)*null(a0);
Matrix<double,1,2> JJb = a0.adjoint()*S3()*Rot_ab(b0,b)*null(b0);
MatrixXd Tmp(1,4);
Tmp << JJa, JJb;
return Tmp;
}
VectorXd Parallel::contradict( const VectorXi & idx,
const VectorXd & l0) const
{
Vector3d a = l0.segment( 3*idx(0),3 );
Vector3d b = l0.segment( 3*idx(1),3 );
VectorXd tmp(1,1);
tmp << a.dot( S3()*b );
return tmp;
}
std::shared_ptr<ConstraintBase> Parallel::doClone() const
{
auto T = std::make_shared<Parallel>();
T->setStatus( this->status() );
T->setEnforced( this->enforced());
return std::move(T);
}
//! skew(e_3), e_3=[0,0,1]'
Matrix3d Parallel::S3()
{
const static Matrix3d tmp = (Matrix3d() << 0,-1,0, +1,0,0, 0,0,0).finished();
return tmp;
}
std::shared_ptr<ConstraintBase> Vertical::doClone() const
{
auto T = std::make_shared<Vertical>();
T->setStatus( this->status() );
T->setEnforced( this->enforced());
return std::move(T);
}
//! e_2 = [0,1,0]'
Vector3d Vertical::e2()
{
const static Vector3d tmp = (Vector3d() << 0,1,0).finished();
return tmp;
}
MatrixXd Vertical::Jacobian( const VectorXi &idx,
const VectorXd &l0,
const VectorXd &l) const
{
Vector3d a0 = l0.segment( 3*idx(0), 3);
Vector3d a = l.segment( 3*idx(0), 3);
// returns a scalar, not a 1-vector:
// return e2().dot( Rot_ab(a0,a)*null(a0) );
return e2().adjoint()* Rot_ab(a0,a)*null(a0);
}
VectorXd Vertical::contradict( const VectorXi &idx,
const VectorXd &l0) const
{
VectorXd tmp(1);
tmp << l0.segment( 3*idx(0)+1, 1); // 2nd element
return tmp;
}
VectorXd Diagonal::contradict( const VectorXi &idx,
const VectorXd &l0) const
{
Vector3d l = l0.segment( 3*idx(0), 3);
VectorXd tmp(1); // scalar as vector
tmp << abs(l(0)) - abs( l(1)); // abs(a)-abs(b)
return tmp;
}
std::shared_ptr<ConstraintBase> Diagonal::doClone() const
{
auto T = std::make_shared<Diagonal>();
T->setStatus( this->status() );
T->setEnforced( this->enforced());
return std::move(T);
}
MatrixXd Diagonal::Jacobian( const VectorXi &idx,
const VectorXd &l0,
const VectorXd &l) const
{
// abs(a)-abs(b) = 0, l=[a,b,c]', J = [ sign(a), -sign(b), 0]
Vector3d a0 = l0.segment( 3*idx(0), 3);
Vector3d a = l.segment( 3*idx(0), 3);
Eigen::RowVector3d JJ = (Eigen::RowVector3d() << sign(a0(0)),-sign(a0(1)),0).finished();
return JJ * Rot_ab(a0,a) * null(a0);
}
std::shared_ptr<ConstraintBase> Horizontal::doClone() const
{
auto T = std::make_shared<Horizontal>();
T->setStatus( this->status() );
T->setEnforced( this->enforced());
return std::move(T);
}
VectorXd Horizontal::contradict( const VectorXi &idx,
const VectorXd &l0) const
{
VectorXd tmp(1); // scalar as vector
tmp << l0.segment( 3*idx(0), 1); // 1st element of 3-vector
return tmp;
}
//! e_1 = [1,0,0]'
Vector3d Horizontal::e1()
{
const static Vector3d tmp = (Vector3d() << 1,0,0).finished();
return tmp;
}
MatrixXd Horizontal::Jacobian( const VectorXi &idx,
const VectorXd &l0,
const VectorXd &l) const
{
Vector3d a0 = l0.segment( 3*idx(0), 3);
Vector3d a = l.segment( 3*idx(0), 3);
return e1().adjoint()*Rot_ab(a0,a)*null(a0);
}
} // namespace Constraint