-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusegment.cpp
More file actions
449 lines (357 loc) · 12.4 KB
/
usegment.cpp
File metadata and controls
449 lines (357 loc) · 12.4 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
439
440
441
442
443
444
445
446
447
448
449
/*
* 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 "upoint.h"
#include "usegment.h"
#include "ustraightline.h"
#include <QDebug>
using Eigen::Matrix3d;
using Eigen::VectorXi;
using Eigen::Vector3d;
using Eigen::VectorXd;
static const double T_ZERO = 1e-7;
namespace Uncertain {
class uPoint;
//! Input operator
QDataStream & operator<< (QDataStream & out, const Aabb & bbox);
//! Output operator
QDataStream & operator>> (QDataStream & in, Aabb & bbox);
//! Diag([1,1,0])
Matrix3d uStraightLineSegment::CC()
{
static Matrix3d tmp = ( Matrix3d() << 1,0,0,0,1,0,0,0,0 ).finished();
return tmp;
}
//! Get uncertain endpoint x in homogeneous coordinates
uPoint uStraightLineSegment::ux() const
{
Matrix<double, 3,6> JJ;
Matrix<double, 6,6> Cov = m_Cov_tt.topLeftCorner(6,6);
Matrix3d Sl = skew( hl() );
JJ.leftCols(3) = -skew( hm() );
JJ.rightCols(3) = Sl;
// x = Sl*m:
return { Sl*hm(), JJ*Cov*JJ.adjoint() };
}
//! Get uncertain endpoint y in homogeneous coordinates
uPoint uStraightLineSegment::uy() const
{
Matrix<double,3,9> JJ;
Matrix3d Sl = skew( hl() );
JJ << -skew( hn() ), Matrix3d::Zero(), Sl;
return { Sl*hn(), JJ*Cov_tt()*JJ.adjoint() };
}
//! Construction of an uncertain straight line segment via two uncorrelated endpoints
uStraightLineSegment::uStraightLineSegment( const uPoint & ux,
const uPoint & uy)
: m_bounding_box( ux.bbox().united( uy.bbox()) )
{
// (1) 9-parameter vector t = [l; m; n];
const Matrix3d Sx = skew( ux.v() );
const Matrix3d Sy = skew( uy.v() );
const Matrix3d UUx = Sx*CC()*Sx;
const Matrix3d UUy = Sy*CC()*Sy;
const Vector3d l = Sx*uy.v();
Q_ASSERT( fabs( l.norm() ) > T_ZERO );
assert( fabs( l.norm() ) > T_ZERO && "identical points.");
double xh = ux.v()(2);
double yh = uy.v()(2);
// Vector9d t;
m_t.segment( 0, 3) = l;
m_t.segment( 3, 3) = +sign(yh)*UUx*uy.v();
m_t.segment( 6, 3) = -sign(xh)*UUy*ux.v();
// (2) 9x9 covariance matrix
//Matrix3d JJlx = -Sy;
//Matrix3d JJly = Sx;
Matrix3d JJmx = -sign(yh)*( Sx*CC()*Sy + skew( CC()*l ));
Matrix3d JJmy = +sign(yh)*UUx;
Matrix3d JJnx = -sign(xh)*UUy;
Matrix3d JJny = +sign(xh)*( Sy*CC()*Sx -skew( CC()*l ));
/* JJ = [ ...
JJlx, JJly; ...
JJmx, JJmy; ...
JJnx, JJny]; */
Matrix<double, 9,6> JJ;
JJ.block(0,0,3,3) = -Sy; // = JJlx
JJ.block(0,3,3,3) = Sx; // = JJly;
JJ.block(3,0,3,3) = JJmx;
JJ.block(3,3,3,3) = JJmy;
JJ.block(6,0,3,3) = JJnx;
JJ.block(6,3,3,3) = JJny;
Matrix<double,6,6> Cov_pp;
Cov_pp.setZero();
Cov_pp.block(0,0,3,3) = ux.Cov();
Cov_pp.block(3,3,3,3) = uy.Cov();
m_Cov_tt = JJ*Cov_pp*JJ.transpose();
// return { t, JJ*Cov_pp*JJ.transpose()};
}
//! Check if the 'this' and the uncertain straight line segment 'ut' intersect (deterministic)
bool uStraightLineSegment::intersects( const uStraightLineSegment & ut) const
{
double d1 = ut.hx().dot( hl() );
double d2 = ut.hy().dot( hl() );
if ( sameSign(d1,d2) ) {
return false;
}
double d3 = hx().dot( ut.hl() );
double d4 = hy().dot( ut.hl() );
// redundant boolean literal in conditional return statement:
/* if ( sameSign( d3,d4) )
return false;
return true; */
return !sameSign(d3,d4);
}
//! Check if one of the endpoints of the uncertain straight line segment 'other' is incident.
bool uStraightLineSegment::touches( const uStraightLineSegment & other,
const double T_dist,
const double T_in) const
{
if ( other.touchedBy( ux(), T_dist, T_in) ) {
return true;
}
if ( other.touchedBy( uy(), T_dist, T_in) ) {
return true;
}
return false;
}
//! Check if the uncertain point 'ux' is incident.
bool uStraightLineSegment::touchedBy( const uPoint & ux,
const double T_dist,
const double T_in) const
{
if ( !ux.isIncidentWith( ul(), T_in ) ) { // dist(x,l)
return false;
}
uDistance ud1 = ux.distanceAlgebraicTo( um() ); // dist(x,m)
if ( !ud1.isLessThanZero(T_dist) ) {
return false;
}
uDistance ud2 = ux.distanceAlgebraicTo( un() ); // dist(x,n)
if ( !ud2.isGreaterThanZero(T_dist) ) {
return false;
}
return true;
}
//! Get the endpoints connecting uncertain straight line l = S(x)*y
uStraightLine uStraightLineSegment::ul() const
{
Matrix3d Cov_ll = m_Cov_tt.topLeftCorner(3,3);
return { m_t.head(3), Cov_ll };
}
//! Get the delimiting uncertain straight line m in homogeneous coordinates.
uStraightLine uStraightLineSegment::um() const
{
Matrix3d Cov_mm = m_Cov_tt.block(3,3,3,3);
return { m_t.segment(3,3), Cov_mm};
}
//! Get the delimiting uncertain straight line n in homogeneous coordinates.
uStraightLine uStraightLineSegment::un() const
{
Matrix3d Cov_nn = m_Cov_tt.bottomRightCorner(3,3);
return { m_t.tail(3), Cov_nn};
}
//! Get the endpoint x in homogeneous coordinates
Vector3d uStraightLineSegment::hx() const
{
Vector3d l = m_t.head(3);
Vector3d m = m_t.segment(3,3);
return skew( l )*m;
}
//! Get the endpoint y in homogeneous coordinates
Vector3d uStraightLineSegment::hy() const
{
Vector3d l = m_t.head(3);
Vector3d n = m_t.tail(3);
return skew( l )*n;
}
//! Check if the uncertain straight line segment is vertical.
bool uStraightLineSegment::isVertical( const double T) const
{
return ul().isVertical( T);
}
//! Check if the uncertain straight line segment is horizontal.
bool uStraightLineSegment::isHorizontal( const double T) const
{
return ul().isHorizontal( T);
}
//! Check if the uncertain straight line segment is diagonal.
bool uStraightLineSegment::isDiagonal( const double T) const
{
return ul().isDiagonal( T);
}
//! Check if uncertain straight line segment is orthogonal to uncertaint straight line segment 'ut'
bool uStraightLineSegment::isOrthogonalTo( const uStraightLine & um,
const double T_q) const
{
return ul().isOrthogonalTo( um, T_q );
}
//! Check if uncertain straight line segment is parallel to uncertaint straight line segment 'ut'
bool uStraightLineSegment::isParallelTo( const uStraightLine & um,
const double T) const
{
return ul().isParallelTo( um, T );
}
//! Check if uncertain straight lines 'this' and 'ut' are identical
bool uStraightLineSegment::straightLineIsIdenticalTo( const uStraightLine & um,
const double T) const
{
return ul().isIdenticalTo( um, T );
}
//! Move endpoint x along l to intersection point of m and l.
bool uStraightLineSegment::move_x_to( const Vector3d & m)
{
Vector3d l = hl().normalized();
Vector3d z = l.cross( m.normalized() ); // intersection point z
if ( z.norm()<T_ZERO ) {
return false; // l==m
}
// Q_ASSERT_X( z.norm() > T_ZERO, "move x", "l == m.");
Vector2d dx = x() -z.head(2)/z(2);
Matrix3d HH = ( Matrix3d() << 1,0,dx(0), 0,1,dx(1), 0,0,1).finished();
Matrix9d TT = Matrix9d::Identity();
TT.block(3,3,3,3) = HH.adjoint(); // m
transform( TT);
return true;
}
//! Move endpoint y along l to intersection point of n and l.
bool uStraightLineSegment::move_y_to( const Vector3d & n )
{
// intersection point z ............................
Vector3d l = hl().normalized();
Vector3d z = l.cross( n.normalized() );
if ( z.norm() < T_ZERO) {
return false; // l==n
}
// Q_ASSERT_X( z.norm() > T_ZERO, "move y", "l == n.");
Vector2d dx = y() -z.head(2)/z(2);
Matrix3d HH = (Matrix3d() << 1,0,dx(0), 0,1,dx(1), 0,0,1).finished();
Matrix9d TT = Matrix9d::Identity();
TT.block(6,6,3,3) = HH.adjoint(); // n
transform( TT);
return true;
}
//! Check if the uncertain straight line of 'this' is copunctual with the two uncertaint straight lines of 'us' and 'ut'
bool uStraightLineSegment::isCopunctualWith( const uStraightLine & um,
const uStraightLine & un,
const double T) const
{
return ul().isCopunctualWith( um, un, T);
}
//! Transform uncertain straight line segment via 9x9 transformation matrix for t = [l',m',n']'.
void uStraightLineSegment::transform( const Matrix9d & TT)
{
m_t = TT*m_t;
m_Cov_tt = TT*m_Cov_tt*TT.adjoint();
}
/* Matlab: [~,idx] = sort(x);
VectorXi uStraightLineSegment::indices_of_sorting( const VectorXd &v)
{
// size_t N = v.size();
// std::vector<int> idx( N );
// std::generate( idx.begin(), idx.end(),
// [n=N-1] () mutable { return n--; } );
// auto comparator = [&v](unsigned int a, unsigned int b){ return v[a] < v[b]; };
// std::sort( idx.begin(), idx.end(), comparator);
// return idx;
Eigen::Index N = v.size();
VectorXi idx = VectorXi::LinSpaced( N,0,static_cast<int>(N-1) ); // [0,1,...,N-1]
auto comparator = [&v](unsigned int a, unsigned int b){ return v[a] < v[b]; };
std::sort( idx.begin(), idx.end(), comparator);
return idx;
}*/
QDataStream & operator<< (QDataStream & out, const Aabb & bbox)
{
out << bbox.x_min() << bbox.x_max()
<< bbox.y_min() << bbox.y_max();
return out;
}
QDataStream & operator>> (QDataStream & in, Aabb & bbox)
{
double x_min;
double x_max;
double y_min;
double y_max;
in >> x_min >> x_max >> y_min >> y_max;
bbox = Aabb( x_min, x_max, y_min, y_max);
return in;
}
QDataStream & operator>> ( QDataStream & in, Eigen::Matrix<double,9,1> & v);
//! Overloaded >>operator for 9-vectors
QDataStream & operator>> ( QDataStream & in, Eigen::Matrix<double,9,1> & v)
{
for ( int i=0; i<9; i++) { in >> v[i]; }
return in;
}
QDataStream & operator>> ( QDataStream & in, Eigen::Matrix<double,9,9> & MM);
//! Overloaded >>operator for 9x9 matrices
QDataStream & operator>> ( QDataStream & in, Eigen::Matrix<double,9,9> & MM)
{
for ( int i=0; i<9; i++) {
for ( int j=0; j<9; j++) {
in >> MM(i,j);
}
}
return in;
}
QDataStream & operator<< ( QDataStream & out, const Eigen::Matrix<double,9,1> &v);
//! Overloaded <<operator for 9-vectors
QDataStream & operator<< ( QDataStream & out, const Eigen::Matrix<double,9,1> &v)
{
//qDebug() << Q_FUNC_INFO;
for ( int i=0; i<9; i++) { out << v[i]; }
return out;
}
QDataStream & operator<< ( QDataStream & out, const Eigen::Matrix<double,9,9> &MM);
//! Overloaded <<operator for 9x9 matrices
QDataStream & operator<< ( QDataStream & out, const Eigen::Matrix<double,9,9> &MM)
{
qDebug() << Q_FUNC_INFO;
for ( int i=0; i<9; i++) {
for (int j=0; j<9; j++) {
out << MM(i,j);
}
}
return out;
}
//! Serialization of the uncertain straight line segment t=[l',m',n']' and its bounding box
void uStraightLineSegment::serialize( QDataStream & out ) const
{
qDebug() << Q_FUNC_INFO;
out << m_t;
out << m_Cov_tt;
out << m_bounding_box;
}
//! Deserialization of uncertain straight line segment and its bounding box
bool uStraightLineSegment::deserialize( QDataStream & in )
{
// qDebug() << Q_FUNC_INFO;
in >> m_t;
in >> m_Cov_tt;
in >> m_bounding_box;
return in.status()==0;
}
//! Create uncertain straight line segment (for deserialization)
std::shared_ptr<uStraightLineSegment>
uStraightLineSegment::create()
{
return std::shared_ptr<uStraightLineSegment>(
new uStraightLineSegment()
);
// TODO(meijoc)
// return std::make_shared<uStraightLineSegment>(); // why not?
}
} // namespace Uncertain