-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqsegment.cpp
More file actions
500 lines (405 loc) · 13.6 KB
/
qsegment.cpp
File metadata and controls
500 lines (405 loc) · 13.6 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* 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/>.
*/
#include "conics.h"
#include "global.h"
#include "qsegment.h"
#include "upoint.h"
#include "ustraightline.h"
#include <Eigen/Core>
#include <Eigen/Eigenvalues>
#include <QDebug>
#include <QGraphicsItem>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include "qassert.h"
#include "qnamespace.h"
#include "qtdeprecationdefinitions.h"
#include "qtpreprocessorsupport.h"
#include "qtypes.h"
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <utility>
namespace QEntity {
using Eigen::Matrix3d;
using Eigen::Vector3d;
using Uncertain::uStraightLine;
using Uncertain::uPoint;
bool QConstrained::s_show = true;
bool QConstrained::s_showColor = false;
bool QUnconstrained::s_show = false;
bool QSegment::s_showUncertainty = false;
QPen QSegment::s_penSelected = QPen();
namespace {
QPen & myQPen();
//! Initialized pen upon first call to the function
QPen & myQPen()
{
static QPen p;
return p;
}
} // namespace
QPen QUnconstrained::s_defaultPen = myQPen();
QPen QConstrained::s_defaultPen = QPen();
// static const double T_ZERO = 1e-7;
QSegment::QSegment( QGraphicsItem * parent )
: QGraphicsItem(parent)
{
// qDebug() << Q_FUNC_INFO;
setFlag( ItemIsSelectable, true);
setFlag( ItemIsMovable, false);
setFlag( ItemSendsGeometryChanges, false);
setFlag( ItemClipsChildrenToShape, true );
}
QSegment::QSegment(const uPoint &ux, const uPoint &uy)
{
// qDebug() << Q_FUNC_INFO;
Vector3d const xh = ux.v().normalized();
Vector3d const yh = uy.v().normalized();
Q_ASSERT_X( xh.cross(yh).norm() > FLT_EPSILON,
Q_FUNC_INFO,
"identical end-points"); // TODO(meijoc)
setFlag( ItemIsSelectable, true);
setFlag( ItemIsMovable, false);
setFlag( ItemSendsGeometryChanges, false);
setFlag(ItemClipsChildrenToShape, true);
setShape( ux, uy );
qreal const halfWidth = getSelectionOffset( ux, uy);
createSelectionPolygon( halfWidth);
}
/*QSegment & QSegment::operator= ( const QSegment &other )
{
// qDebug() << Q_FUNC_INFO;
if ( &other != this ) {
ellipse_ = other.ellipse_;
branch_ = other.branch_;
line_ = other.line_;
}
return *this;
}*/
void QSegment::paint( QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED( widget )
Q_UNUSED( option )
// qDebug() << Q_FUNC_INFO << boundingRect() << showUncertainty_;
// ??? painter->setClipRect( boundingRect());
painter->drawLine(line_);
if ( s_showUncertainty ) {
painter->drawPolygon( ellipse_.first );
painter->drawPolygon( ellipse_.second);
painter->drawPolyline( branch_.first );
painter->drawPolyline( branch_.second );
}
if ( isSelected() ) {
painter->setPen( s_penSelected );
painter->drawPolygon( minBBox );
}
}
QRectF QSegment::boundingRect() const
{
// qDebug() << Q_FUNC_INFO;
QRectF result = minBBox.boundingRect();
result.adjust( -pen_.width(),
-pen_.width(),
+pen_.width(),
+pen_.width() );
if ( !result.isValid() ) {
result = QRectF( line_.p1(),line_.p2() ).normalized();
}
return result;
}
QPainterPath QSegment::shape() const
{
QPainterPath ret;
ret.addPolygon( minBBox );
return ret;
}
QPolygonF QSegment::toPoly(std::pair<Eigen::VectorXd, Eigen::VectorXd> p)
{
// int const N = static_cast<int>( p.first.size() );
const Eigen::Index N = p.first.size();
QPolygonF poly( N );
for ( int i=0; i<N; i++) {
poly[i] = QPointF( p.first(i), p.second(i));
}
return poly;
}
double QSegment::getSelectionOffset(const uPoint &ux, const uPoint &uy)
{
double m = 0;
uPoint const u1 = ux.euclidean();
Eigen::EigenSolver<Eigen::Matrix2d> const eig1(u1.Cov().topLeftCorner(2, 2));
m = std::max(m, eig1.eigenvalues().real().cwiseAbs().maxCoeff());
uPoint const u2 = uy.euclidean();
Eigen::EigenSolver<Eigen::Matrix2d> const eig2(u2.Cov().topLeftCorner(2, 2));
m = std::max( m, eig2.eigenvalues().real().cwiseAbs().maxCoeff() );
return 1000*sqrt(4.6052*m); // chi2inv(0.9, 2)
}
void QSegment::setShape( const uPoint &ux,
const uPoint &uy)
{
// qDebug() << Q_FUNC_INFO;
const double k2 = 4.6; // ch2inv(0.9,2) TODO(meijoc)
const int nSupport = 64;
Vector3d xh = ux.v().normalized();
Vector3d yh = uy.v().normalized();
// check .........................................
Q_ASSERT_X( xh.cross(yh).norm() > FLT_EPSILON,
Q_FUNC_INFO,
"identical end-points"); // TODO(meijoc)
if ( std::fabs( xh(2) )>0. && std::fabs( yh(2) )>0. ) {
line_.setLine( 1000*xh(0)/xh(2), 1000*xh(1)/xh(2),
1000*yh(0)/yh(2), 1000*yh(1)/yh(2) );
}
if ( k2<=0.) {
ellipse_.first.clear();
ellipse_.second.clear();
branch_.first.clear();
branch_.second.clear(); //update();
return;
}
// unconditioning .........................................
const Matrix3d TT = (Matrix3d() << 1000, 0, 0, 0, 1000, 0, 0, 0, 1).finished();
uPoint const x2 = ux.transformed(TT);
uPoint const y2 = uy.transformed(TT);
Q_ASSERT( x2.v().norm() > FLT_EPSILON );
Q_ASSERT( y2.v().norm() > FLT_EPSILON );
Q_ASSERT( x2.v().cross( y2.v() ).norm() > FLT_EPSILON );
// two ellipses ...............................................
uPoint ux2 = x2.euclidean();
Matrix3d CC = cof3( k2*ux2.Cov() -ux2.v()*ux2.v().adjoint() );
Conic::Ellipse const ell_x(CC);
ux2 = y2.euclidean();
CC = cof3( k2*ux2.Cov() -ux2.v()*ux2.v().adjoint() );
Conic::Ellipse const ell_y(CC);
ellipse_.first = toPoly( ell_x.poly( nSupport ) );
ellipse_.second = toPoly( ell_y.poly( nSupport ) );
// hyperbola ..................................................
// uStraightLine ul = x2.cross(y2);
uStraightLine ul(x2.cross(y2));
// uStraightLine ul = uStraightLine::cross(x2,y2);
ul = ul.euclidean();
CC = k2*ul.Cov() -ul.v()*ul.v().adjoint();
Conic::Hyperbola const hyp( CC );
// Two polar lines ............................................
Vector3d const lx = ell_y.polar(x2.v()).normalized();
Vector3d const ly = ell_x.polar( y2.v() ).normalized();
// Four tangent points ........................................
std::pair<Vector3d,Vector3d> pair1 = hyp.intersect( lx );
std::pair<Vector3d,Vector3d> pair2 = hyp.intersect( ly );
// Hyperbola: Axis and point of symmetry ......................
Vector3d symaxis = hyp.centerline();
const Vector3d x0 = hyp.center();
Vector3d m; // perpendicular m, passing the center
m(0) = -symaxis(1);
m(1) = +symaxis(0);
assert( std::fabs(x0(2))>0. );
m(2) = -x0.head(2).dot( m.head(2) )/ x0(2);
// checks before Euclidean normalization
assert( std::fabs( symaxis.head(2).norm() ) >0. );
assert( std::fabs( m.head(2).norm() ) >0. );
assert( std::fabs( pair1.first(2) ) >0. );
assert( std::fabs( pair1.second(2) ) >0. );
assert( std::fabs( pair2.first(2) ) >0. );
assert( std::fabs( pair2.second(2) ) >0. );
// Euclidean normalizations for efficient distance computations
symaxis /= symaxis.head(2).norm();
m /= m.head(2).norm();
pair1.first /= pair1.first(2);
pair1.second /= pair1.second(2);
pair2.first /= pair2.first(2);
pair2.second /= pair2.second(2);
// Euclidean distances of tangent points to axis a.
Eigen::Vector4d lr;
lr(0) = symaxis.dot( pair1.first );
lr(1) = symaxis.dot( pair1.second);
lr(2) = symaxis.dot( pair2.first );
lr(3) = symaxis.dot( pair2.second);
// orthogonal distances of the 4 tangent points to m.
Eigen::Vector4d ud;
ud(0) = m.dot( pair1.first);
ud(1) = m.dot( pair1.second);
ud(2) = m.dot( pair2.first);
ud(3) = m.dot( pair2.second);
Eigen::Vector4d d = Eigen::Vector4d::Zero();
for (int i=0; i<4; i++) {
if ( lr(i) < 0 && ud(i) < 0) {
d(2) = -ud(i);
}
else
if ( lr(i) < 0 && ud(i) > 0) {
d(3) = -ud(i);
}
else {
if ( lr(i) > 0 && ud(i) < 0) {
d(0) = -ud(i);
}
else {
if ( lr(i) > 0 && ud(i) > 0) {
d(1) = -ud(i);
}
}
}
}
// hyperbola: branches to plot
auto ab = hyp.lengthsSemiAxes();
double const aa = ab.first * ab.first;
double const bb = ab.second * ab.second;
// transformation branches (motion) ..........................
QTransform t;
t.translate( x0(0)/x0(2), x0(1)/x0(2) );
t.rotate( hyp.angle_deg(), Qt::ZAxis );
// first branch of hyperbola .................................
Eigen::VectorXd xi = Eigen::VectorXd::LinSpaced( nSupport, d(0),d(1) );
Eigen::VectorXd yi = xi;
branch_.first.clear();
for ( Eigen::Index i=0; i<xi.size(); i++ )
{
yi(i) = +sqrt( xi(i)*xi(i) *aa/(bb) +aa);
branch_.first << QPointF( xi(i),yi(i) );
}
branch_.first = t.map( branch_.first );
// second branch of hyperbola .................................
xi = Eigen::VectorXd::LinSpaced( nSupport, d(3), d(2) );
branch_.second.clear();
yi = -((xi.array().square() *aa/(bb)).array() +aa ).sqrt();
for ( Eigen::Index i=0; i<xi.size(); i++ ) {
branch_.second << QPointF( xi(i), yi(i) );
}
branch_.second = t.map( branch_.second );
}
void QConstrained::paint( QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(widget)
Q_UNUSED(option)
// qDebug() << Q_FUNC_INFO;
// ??? TODO painter->setClipRect( boundingRect());
QPen p = pen();
if ( s_showColor ) {
p.setColor( altColor);
}
painter->setPen( p);
QSegment::paint(painter,option,widget);
}
QUnconstrained::QUnconstrained( QGraphicsItem * /* parent */ )
{
// qDebug() << Q_FUNC_INFO;
setVisible( QUnconstrained::show() );
setPen( s_defaultPen );
}
QConstrained::QConstrained()
: altColor(Qt::black)
{
// qDebug() << Q_FUNC_INFO;
setVisible( QConstrained::show() );
setPen(s_defaultPen);
}
QConstrained::QConstrained(const uPoint &ux, const uPoint &uy)
: QSegment(ux, uy)
, altColor(Qt::black)
{
setVisible( QConstrained::show() );
setPen( s_defaultPen );
}
void QUnconstrained::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget)
{
Q_UNUSED(widget)
Q_UNUSED(option)
// qDebug() << Q_FUNC_INFO;
// ??? TODO painter->setClipRect( boundingRect());
painter->setPen( pen() );
QSegment::paint(painter,option,widget);
}
void QConstrained::setAltColor(const QColor & col)
{
altColor = col;
}
void QSegment::serialize( QDataStream & out ) const
{
// qDebug() << Q_FUNC_INFO;
out << pen_;
out << line_;
out << branch_.first;
out << branch_.second;
out << ellipse_.first;
out << ellipse_.second;
out << minBBox;
}
bool QSegment::deserialize( QDataStream &in )
{
// qDebug() << Q_FUNC_INFO;
in >> pen_;
in >> line_;
in >> branch_.first;
in >> branch_.second;
in >> ellipse_.first;
in >> ellipse_.second;
in >> minBBox;
return in.status()==0;
}
void QSegment::mousePressEvent( QGraphicsSceneMouseEvent * event )
{
// qDebug() << Q_FUNC_INFO;
if ( event->button() == Qt::RightButton ) {
setSelected( !isSelected());
}
}
void QSegment::createSelectionPolygon( const qreal halfWidth)
{
// qDebug() << Q_FUNC_INFO;
QRectF const rect(-line().length() / 2 - halfWidth,
-halfWidth,
line().length() + 2 * halfWidth,
2 * halfWidth);
QPolygonF const poly(rect);
QTransform t;
t.translate( line().center().x(),
line().center().y() );
t.rotate( -line().angle());
minBBox.clear();
minBBox = t.map( poly);
}
QUnconstrained::QUnconstrained( const uPoint & ux,
const uPoint & uy)
: QSegment(ux,uy)
{
setVisible( QUnconstrained::show() );
setPen( s_defaultPen );
}
Matrix3d QSegment::cof3(const Matrix3d &MM)
{
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;
}
} // namespace QEntity