31
31
#include < QMessageBox>
32
32
#include < QTableView>
33
33
#include < QStatusBar>
34
+ #include < Inventor/SoPickedPoint.h>
34
35
#include < Inventor/nodes/SoDrawStyle.h>
35
36
#include < Inventor/Qt/SoQt.h>
36
37
#include < Inventor/VRMLnodes/SoVRMLAppearance.h>
37
38
#include < Inventor/VRMLnodes/SoVRMLMaterial.h>
38
- #include < Inventor/VRMLnodes/SoVRMLShape .h>
39
+ #include < Inventor/VRMLnodes/SoVRMLTransform .h>
39
40
#include < rl/sg/Body.h>
40
41
#include < rl/sg/DepthScene.h>
41
42
#include < rl/sg/DistanceScene.h>
42
43
#include < rl/sg/Model.h>
43
44
#include < rl/sg/Shape.h>
44
45
#include < rl/sg/SimpleScene.h>
45
46
#include < rl/sg/XmlFactory.h>
47
+ #include < rl/sg/so/Body.h>
48
+ #include < rl/sg/so/Shape.h>
46
49
47
50
#if QT_VERSION >= 0x050200
48
51
#include < QCommandLineParser>
68
71
#include " BodyModel.h"
69
72
#include " MainWindow.h"
70
73
#include " SoGradientBackground.h"
74
+ #include " SoMaterialHighlightRenderAction.h"
71
75
72
76
MainWindow::MainWindow (QWidget* parent, Qt::WindowFlags f) :
73
77
QMainWindow(parent, f),
74
78
collisionScene(),
75
79
viewScene(),
76
- body( 0 ),
80
+ bodyModel( new BodyModel( this ) ),
77
81
depthCoordinate(nullptr ),
78
82
depthLabel(new QLabel(this )),
79
83
depthLineSet(nullptr ),
@@ -85,15 +89,23 @@ MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags f) :
85
89
engine(),
86
90
filename(),
87
91
gradientBackground(),
88
- model(0 ),
92
+ highlightRenderAction(nullptr ),
93
+ root(nullptr ),
94
+ selected(nullptr ),
95
+ selection(nullptr ),
89
96
simpleLabel(new QLabel(this )),
97
+ view2collision(),
90
98
viewer(nullptr )
91
99
{
92
100
MainWindow::singleton = this ;
93
101
94
102
SoQt::init (this );
95
103
SoDB::init ();
96
104
SoGradientBackground::initClass ();
105
+ SoMaterialHighlightRenderAction::initClass ();
106
+
107
+ this ->root = new SoSeparator ();
108
+ this ->root ->ref ();
97
109
98
110
this ->parseCommandLine ();
99
111
@@ -145,11 +157,22 @@ MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags f) :
145
157
this ->gradientBackground ->ref ();
146
158
this ->gradientBackground ->color0 .setValue (0 .8f , 0 .8f , 0 .8f );
147
159
this ->gradientBackground ->color1 .setValue (1 .0f , 1 .0f , 1 .0f );
148
- this ->viewScene ->root ->insertChild (this ->gradientBackground , 0 );
160
+ this ->root ->insertChild (this ->gradientBackground , 0 );
161
+
162
+ this ->selection = new SoSelection ();
163
+ this ->selection ->addSelectionCallback (MainWindow::selectionCallback, this );
164
+ this ->selection ->addDeselectionCallback (MainWindow::deselectionCallback, this );
165
+ this ->selection ->setPickFilterCallback (MainWindow::pickFilterCallback, this );
166
+ this ->selection ->policy = SoSelection::SINGLE;
167
+ this ->selection ->addChild (this ->viewScene ->root );
168
+ this ->root ->addChild (this ->selection );
169
+
170
+ this ->highlightRenderAction = new SoMaterialHighlightRenderAction ();
149
171
150
172
this ->viewer = new SoQtExaminerViewer (this , nullptr , true , SoQtFullViewer::BUILD_POPUP);
151
173
this ->viewer ->setFeedbackVisibility (true );
152
- this ->viewer ->setSceneGraph (this ->viewScene ->root );
174
+ this ->viewer ->setGLRenderAction (this ->highlightRenderAction );
175
+ this ->viewer ->setSceneGraph (this ->root );
153
176
this ->viewer ->setTransparencyType (SoGLRenderAction::SORTED_OBJECT_BLEND);
154
177
155
178
this ->setCentralWidget (this ->viewer ->getWidget ());
@@ -161,16 +184,18 @@ MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags f) :
161
184
factory.load (this ->filename .toStdString (), this ->collisionScene .get ());
162
185
factory.load (this ->filename .toStdString (), this ->viewScene .get ());
163
186
187
+ for (rl::sg::Scene::Iterator i = this ->viewScene ->begin (), j = this ->collisionScene ->begin (); i != this ->viewScene ->end (); ++i, ++j)
188
+ {
189
+ for (rl::sg::Model::Iterator k = (*i)->begin (), l = (*j)->begin (); k != (*i)->end (); ++k, ++l)
190
+ {
191
+ this ->view2collision [*k] = *l;
192
+ }
193
+ }
194
+
164
195
this ->viewer ->viewAll ();
165
196
166
197
BodyDelegate* bodyDelegate = new BodyDelegate (this );
167
198
168
- BodyModel* bodyModel = new BodyModel (this );
169
- bodyModel->setBody (
170
- this ->viewScene ->getModel (this ->model )->getBody (this ->body ),
171
- this ->collisionScene ->getModel (this ->model )->getBody (this ->body )
172
- );
173
-
174
199
QTableView* bodyView = new QTableView (this );
175
200
#if QT_VERSION >= 0x050000
176
201
bodyView->horizontalHeader ()->setSectionResizeMode (QHeaderView::Stretch);
@@ -180,14 +205,14 @@ MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags f) :
180
205
bodyView->horizontalHeader ()->hide ();
181
206
bodyView->setAlternatingRowColors (true );
182
207
bodyView->setItemDelegate (bodyDelegate);
183
- bodyView->setModel (bodyModel);
208
+ bodyView->setModel (this -> bodyModel );
184
209
185
210
QDockWidget* bodyDockWidget = new QDockWidget (this );
186
211
bodyDockWidget->resize (160 , 240 );
187
212
bodyDockWidget->setFeatures (QDockWidget::NoDockWidgetFeatures | QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);
188
213
bodyDockWidget->setFloating (true );
189
214
bodyDockWidget->setWidget (bodyView);
190
- bodyDockWidget->setWindowTitle (" Model[ " + QString::number ( this -> model ) + " ]-> Body[ " + QString::number ( this -> body ) + " ] " );
215
+ bodyDockWidget->setWindowTitle (" Body" );
191
216
192
217
SoDrawStyle* drawStyle = new SoDrawStyle ();
193
218
drawStyle->lineWidth = 0 .0f ;
@@ -253,15 +278,36 @@ MainWindow::MainWindow(QWidget* parent, Qt::WindowFlags f) :
253
278
this ->statusBar ()->addWidget (this ->depthLabel );
254
279
}
255
280
281
+ if (this ->viewScene ->getNumModels () > 0 )
282
+ {
283
+ rl::sg::Model* model = this ->viewScene ->getModel (0 );
284
+
285
+ if (model->getNumBodies () > 0 )
286
+ {
287
+ rl::sg::Body* body = model->getBody (0 );
288
+ this ->selection ->select (dynamic_cast <rl::sg::so::Body*>(body)->root );
289
+ }
290
+ }
291
+
256
292
this ->test ();
257
293
}
258
294
259
295
MainWindow::~MainWindow ()
260
296
{
261
- this ->gradientBackground ->unref ();
297
+ this ->root ->unref ();
262
298
MainWindow::singleton = nullptr ;
263
299
}
264
300
301
+ void
302
+ MainWindow::deselectionCallback (void * data, SoPath* path)
303
+ {
304
+ MainWindow* mainWindow = static_cast <MainWindow*>(data);
305
+ mainWindow->bodyModel ->setBody (nullptr , nullptr );
306
+ mainWindow->selected = nullptr ;
307
+ mainWindow->selection ->touch ();
308
+ mainWindow->test ();
309
+ }
310
+
265
311
MainWindow*
266
312
MainWindow::instance ()
267
313
{
@@ -300,30 +346,15 @@ MainWindow::parseCommandLine()
300
346
engines.sort ();
301
347
302
348
#if QT_VERSION >= 0x050200
303
- QCommandLineOption bodyOption (QStringList (" body" ), " Sets active body." , " body" );
304
349
QCommandLineOption engineOption (QStringList (" engine" ), " Sets collision engine." , engines.join (" |" ));
305
- QCommandLineOption modelOption (QStringList (" model" ), " Sets model of active body." , " model" );
306
350
307
351
QCommandLineParser parser;
308
- parser.addOption (bodyOption);
309
352
parser.addOption (engineOption);
310
353
const QCommandLineOption helpOption = parser.addHelpOption ();
311
- parser.addOption (modelOption);
312
354
parser.addPositionalArgument (" filename" , " " , " [filename]" );
313
355
314
356
parser.process (QCoreApplication::arguments ());
315
357
316
- if (parser.isSet (bodyOption))
317
- {
318
- bool ok;
319
- this ->body = parser.value (bodyOption).toUInt (&ok);
320
-
321
- if (!ok)
322
- {
323
- parser.showHelp ();
324
- }
325
- }
326
-
327
358
if (parser.isSet (engineOption))
328
359
{
329
360
QString engine = parser.value (engineOption);
@@ -336,17 +367,6 @@ MainWindow::parseCommandLine()
336
367
this ->engine = engine;
337
368
}
338
369
339
- if (parser.isSet (modelOption))
340
- {
341
- bool ok;
342
- this ->model = parser.value (modelOption).toUInt (&ok);
343
-
344
- if (!ok)
345
- {
346
- parser.showHelp ();
347
- }
348
- }
349
-
350
370
if (parser.positionalArguments ().size () > 1 )
351
371
{
352
372
parser.showHelp ();
@@ -357,30 +377,20 @@ MainWindow::parseCommandLine()
357
377
this ->filename = parser.positionalArguments ()[0 ];
358
378
}
359
379
#else
360
- QRegExp bodyRegExp (" --body=(\\ d*)" );
361
380
QRegExp engineRegExp (" --engine=(" + engines.join (" |" ) + " )" );
362
381
QRegExp helpRegExp (" --help" );
363
- QRegExp modelRegExp (" --model=(\\ d*)" );
364
382
365
383
for (int i = 1 ; i < QApplication::arguments ().size (); ++i)
366
384
{
367
- if (-1 != bodyRegExp.indexIn (QApplication::arguments ()[i]))
368
- {
369
- this ->body = bodyRegExp.cap (1 ).toUInt ();
370
- }
371
- else if (-1 != engineRegExp.indexIn (QApplication::arguments ()[i]))
385
+ if (-1 != engineRegExp.indexIn (QApplication::arguments ()[i]))
372
386
{
373
387
this ->engine = engineRegExp.cap (1 );
374
388
}
375
389
else if (-1 != helpRegExp.indexIn (QApplication::arguments ()[i]))
376
390
{
377
- QMessageBox::information (this , " Usage" , " rlCollisionDemo [--body=<body>] [-- engine=<" + engines.join (" |" ) + " >] [--help] [--model=<model> ] [filename]" );
391
+ QMessageBox::information (this , " Usage" , " rlCollisionDemo [--engine=<" + engines.join (" |" ) + " >] [--help] [filename]" );
378
392
exit (0 );
379
393
}
380
- else if (-1 != modelRegExp.indexIn (QApplication::arguments ()[i]))
381
- {
382
- this ->model = modelRegExp.cap (1 ).toUInt ();
383
- }
384
394
else
385
395
{
386
396
this ->filename = QApplication::arguments ()[i];
@@ -389,6 +399,38 @@ MainWindow::parseCommandLine()
389
399
#endif
390
400
}
391
401
402
+ SoPath*
403
+ MainWindow::pickFilterCallback (void * data, const SoPickedPoint* pick)
404
+ {
405
+ SoFullPath* path = static_cast <SoFullPath*>(pick->getPath ());
406
+ path->pop ();
407
+ path->pop ();
408
+ path->pop ();
409
+ return path;
410
+ }
411
+
412
+ void
413
+ MainWindow::selectionCallback (void * data, SoPath* path)
414
+ {
415
+ MainWindow* mainWindow = static_cast <MainWindow*>(data);
416
+
417
+ if (path->getTail ()->isOfType (SoVRMLTransform::getClassTypeId ()))
418
+ {
419
+ SoVRMLTransform* vrmlTransform = static_cast <SoVRMLTransform*>(path->getTail ());
420
+
421
+ if (nullptr != vrmlTransform->getUserData ())
422
+ {
423
+ rl::sg::so::Body* view = static_cast <rl::sg::so::Body*>(vrmlTransform->getUserData ());
424
+ rl::sg::Body* collision = dynamic_cast <rl::sg::Body*>(mainWindow->view2collision [view]);
425
+ mainWindow->bodyModel ->setBody (collision, view);
426
+ mainWindow->selected = view;
427
+ }
428
+ }
429
+
430
+ mainWindow->selection ->touch ();
431
+ mainWindow->test ();
432
+ }
433
+
392
434
void
393
435
MainWindow::test ()
394
436
{
@@ -400,7 +442,17 @@ MainWindow::test()
400
442
this ->depthLineSet ->coordIndex .setNum (0 );
401
443
this ->depthCoordinate ->point .setNum (0 );
402
444
403
- rl::sg::Body* body = this ->collisionScene ->getModel (this ->model )->getBody (this ->body );
445
+ rl::sg::Base* collision = this ->view2collision [this ->selected ];
446
+
447
+ if (nullptr == this ->selected || nullptr == collision)
448
+ {
449
+ this ->simpleLabel ->hide ();
450
+ this ->distanceLabel ->hide ();
451
+ this ->depthLabel ->hide ();
452
+ return ;
453
+ }
454
+
455
+ rl::sg::Body* body = dynamic_cast <rl::sg::Body*>(collision);
404
456
405
457
if (rl::sg::SimpleScene* simpleScene = dynamic_cast <rl::sg::SimpleScene*>(this ->collisionScene .get ()))
406
458
{
@@ -420,19 +472,20 @@ MainWindow::test()
420
472
}
421
473
}
422
474
475
+ this ->simpleLabel ->show ();
423
476
this ->simpleLabel ->setText (" Collisions: " + QString::number (collisions));
424
477
425
478
if (collisions > 0 )
426
479
{
427
- this ->viewer -> setBackgroundColor (SbColor (0.5 , 0 , 0 ));
428
- this ->gradientBackground -> color0 . setValue ( 0 . 5f , 0 .0f , 0 .0f );
429
- this ->gradientBackground -> color1 . setValue ( 1 .0f , 1 .0f , 1 .0f );
480
+ this ->highlightRenderAction -> setDiffuseColor (SbColor (0 .8f , 0 . 0f , 0 . 0f ));
481
+ this ->highlightRenderAction -> setEmissiveColor ( SbColor ( 0 . 25f , 0 .0f , 0 .0f ) );
482
+ this ->highlightRenderAction -> setSpecularColor ( SbColor ( 1 .0f , 0 .0f , 0 .0f ) );
430
483
}
431
484
else
432
485
{
433
- this ->viewer -> setBackgroundColor (SbColor (0 , 0 , 0 ));
434
- this ->gradientBackground -> color0 . setValue ( 0 . 8f , 0 .8f , 0 .8f );
435
- this ->gradientBackground -> color1 . setValue ( 1 .0f , 1 .0f , 1 .0f );
486
+ this ->highlightRenderAction -> setDiffuseColor (SbColor (0 . 8f , 0 . 8f , 0 . 8f ));
487
+ this ->highlightRenderAction -> setEmissiveColor ( SbColor ( 0 . 0f , 0 .0f , 0 .25f ) );
488
+ this ->highlightRenderAction -> setSpecularColor ( SbColor ( 0 .0f , 0 .0f , 1 .0f ) );
436
489
}
437
490
}
438
491
@@ -462,6 +515,7 @@ MainWindow::test()
462
515
}
463
516
}
464
517
518
+ this ->distanceLabel ->show ();
465
519
this ->distanceLabel ->setText (" Distance: " + QString::number (distance));
466
520
467
521
if (distance > 0 && distance < std::numeric_limits<rl::math::Real>::max ())
@@ -520,6 +574,7 @@ MainWindow::test()
520
574
}
521
575
}
522
576
577
+ this ->depthLabel ->show ();
523
578
this ->depthLabel ->setText (" Depth: " + QString::number (depth));
524
579
525
580
if (depth > 0 )
0 commit comments