1 #include <cpExtensions/QT/SimpleMPRWidget.h>
3 #ifdef cpExtensions_QT4
5 #include <cpExtensions/ui_SimpleMPRWidget.h>
7 #include <vtkProperty.h>
8 #include <vtkRendererCollection.h>
10 double cpExtensions::QT::SimpleMPRWidget::
23 // -------------------------------------------------------------------------
24 cpExtensions::QT::SimpleMPRWidget::
25 SimpleMPRWidget( QWidget* parent )
27 m_UI( new Ui::SimpleMPRWidget ),
30 this->m_UI->setupUi( this );
32 // Configure VTK widgets
33 this->m_VTK[ 0 ] = this->m_UI->VTK01;
34 this->m_VTK[ 1 ] = this->m_UI->VTK00;
35 this->m_VTK[ 2 ] = this->m_UI->VTK10;
36 this->m_VTK[ 3 ] = this->m_UI->VTK11;
38 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
39 this->m_MPRObjects->SetRenderWindows(
40 this->m_VTK[ 0 ]->GetRenderWindow( ),
41 this->m_VTK[ 1 ]->GetRenderWindow( ),
42 this->m_VTK[ 2 ]->GetRenderWindow( ),
43 this->m_VTK[ 3 ]->GetRenderWindow( )
48 this->m_UI->TopSplitter, SIGNAL( splitterMoved( int, int ) ),
49 this, SLOT( _SyncBottom( int, int ) )
52 this->m_UI->BottomSplitter, SIGNAL( splitterMoved( int, int ) ),
53 this, SLOT( _SyncTop( int, int ) )
57 // -------------------------------------------------------------------------
58 cpExtensions::QT::SimpleMPRWidget::
61 // TODO: this causes a segfault (?)
65 // -------------------------------------------------------------------------
66 unsigned int cpExtensions::QT::SimpleMPRWidget::
67 GetNumberOfData( ) const
69 return( this->m_Data.size( ) );
72 // -------------------------------------------------------------------------
73 bool cpExtensions::QT::SimpleMPRWidget::
75 vtkImageData* data, const std::string& name,
76 const std::string& parent
82 auto iIt = this->m_Data.find( name );
83 if( iIt == this->m_Data.end( ) )
87 auto pIt = this->m_Data.find( parent );
88 if( pIt == this->m_Data.end( ) )
94 this->m_Data[ name ].SetImageData( data );
97 auto iIt = this->m_Data.find( name );
98 if( iIt != this->m_Data.end( ) )
101 dynamic_cast< vtkActor* >( iIt->second.GetMeshActor( ) );
104 unsigned int idx = this->m_Data.size( ) % 8;
105 actor->GetProperty( )->SetColor(
106 Self::cm_Colors[ idx ][ 0 ],
107 Self::cm_Colors[ idx ][ 1 ],
108 Self::cm_Colors[ idx ][ 2 ]
116 // TODO: this->_UpdateTreeItem( name, parent );
123 // -------------------------------------------------------------------------
124 bool cpExtensions::QT::SimpleMPRWidget::
125 AddData( vtkPolyData* data, const std::string& name )
130 auto iIt = this->m_Data.find( name );
131 if( iIt == this->m_Data.end( ) )
134 this->m_Data[ name ].SetPolyData( data );
137 auto iIt = this->m_Data.find( name );
138 if( iIt != this->m_Data.end( ) )
141 dynamic_cast< vtkActor* >( iIt->second.GetMeshActor( ) );
144 unsigned int idx = this->m_Data.size( ) % 8;
145 actor->GetProperty( )->SetColor(
146 Self::cm_Colors[ idx ][ 0 ],
147 Self::cm_Colors[ idx ][ 1 ],
148 Self::cm_Colors[ idx ][ 2 ]
156 // TODO: this->_UpdateTreeItem( name, parent );
163 // -------------------------------------------------------------------------
164 const std::string& cpExtensions::QT::SimpleMPRWidget::
165 GetMainImage( ) const
167 return( this->m_MainImage );
170 // -------------------------------------------------------------------------
171 bool cpExtensions::QT::SimpleMPRWidget::
172 SetMainImage( const std::string& name )
174 auto iIt = this->m_Data.find( name );
175 if( iIt != this->m_Data.end( ) )
177 if( iIt->second.Tag == Data::IMAGE )
179 this->m_MainImage = name;
189 // -------------------------------------------------------------------------
190 void cpExtensions::QT::SimpleMPRWidget::
191 DeleteData( const std::string& name )
193 auto iIt = this->m_Data.find( name );
194 if( iIt != this->m_Data.end( ) )
196 this->m_Data.erase( iIt );
199 std::vector< std::string > to_erase;
200 auto tIt = this->m_Tree.begin( );
201 for( ; tIt != this->m_Tree.end( ); ++tIt )
202 if( tIt->second == name )
203 to_erase.push_back( tIt->first );
206 tIt = this->m_Tree.find( name );
207 if( tIt != this->m_Tree.end( ) )
208 this->m_Tree.erase( tIt );
211 auto dIt = to_erase.begin( );
212 for( ; dIt != to_erase.end( ); ++dIt )
213 this->DeleteData( *dIt );
215 // Delete from tree widget
217 QTreeWidgetItem* item = this->_FindItemInTree( name );
219 this->m_UI->LoadedData->removeItemWidget( item, 0 );
222 // Reset main image, just in case
223 if( this->m_Data.size( ) == 0 )
224 this->m_MainImage = "";
229 // -------------------------------------------------------------------------
230 void cpExtensions::QT::SimpleMPRWidget::
233 this->m_MPRObjects->Clear( );
234 this->m_Data.clear( );
235 this->m_Tree.clear( );
236 // TODO: this->m_UI->LoadedData->clear( );
237 this->m_MainImage = "";
240 // -------------------------------------------------------------------------
241 void cpExtensions::QT::SimpleMPRWidget::
243 const std::string& name, const double& r, const double& g, const double& b
247 auto iIt = this->m_Data.find( name );
248 if( iIt == this->m_Data.end( ) )
251 if( iIt->second.Tag == Data::IMAGE )
254 else if( iIt->second.Tag == Data::MESH )
260 // -------------------------------------------------------------------------
261 void cpExtensions::QT::SimpleMPRWidget::
262 ShowData( const std::string& name )
264 auto iIt = this->m_Data.find( name );
265 if( iIt == this->m_Data.end( ) )
268 if( iIt->second.Tag == Data::IMAGE )
270 if( name == this->m_MainImage )
271 this->m_MPRObjects->SetInputImage( iIt->second.Image );
274 unsigned int i = ( this->m_MPRObjects->GetNumberOfImages( ) - 1 ) % 8;
275 this->m_MPRObjects->AddBinaryImage(
277 Self::cm_Colors[ i ][ 0 ],
278 Self::cm_Colors[ i ][ 1 ],
279 Self::cm_Colors[ i ][ 2 ]
282 this->m_MPRObjects->Show( );
284 else if( iIt->second.Tag == Data::MESH )
287 this->m_VTK[ 3 ]->GetRenderWindow( )->
288 GetRenderers( )->GetFirstRenderer( );
291 ren->AddActor( iIt->second.GetMeshActor( ) );
292 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
297 // -------------------------------------------------------------------------
298 void cpExtensions::QT::SimpleMPRWidget::
299 HideData( const std::string& name )
303 // -------------------------------------------------------------------------
304 vtkRenderWindowInteractor* cpExtensions::QT::SimpleMPRWidget::
305 GetInteractor( unsigned int i )
309 if( this->m_VTK[ i ] != NULL )
310 return( this->m_VTK[ i ]->GetInteractor( ) );
318 // -------------------------------------------------------------------------
320 bool cpExtensions::QT::SimpleMPRWidget::
323 const std::string& name,
324 const std::string& parent
328 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
329 if( new_item == NULL )
332 // Associate new data
333 this->m_Images[ name ] = image;
334 this->m_Tree[ name ] = parent;
336 // Show image and return
337 this->m_MPRObjects->AddImage( image );
341 // -------------------------------------------------------------------------
342 bool cpExtensions::QT::SimpleMPRWidget::
345 const std::string& name,
346 const std::string& parent,
347 const double& r, const double& g, const double& b
351 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
352 if( new_item == NULL )
355 // Associate new data
356 this->m_Images[ name ] = image;
357 this->m_Tree[ name ] = parent;
359 // Show image and return
360 this->m_MPRObjects->AddImage( image );
364 // -------------------------------------------------------------------------
365 bool cpExtensions::QT::SimpleMPRWidget::
368 const std::string& name,
369 const std::string& parent
373 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
374 if( new_item == NULL )
377 // Associate new data
378 PolyDataActor* actor = new PolyDataActor( mesh );
379 this->m_Meshes[ name ] = actor;
380 this->m_Tree[ name ] = parent;
383 this->_Add3DActor( actor->Actor );
387 // -------------------------------------------------------------------------
388 bool cpExtensions::QT::SimpleMPRWidget::
391 const std::string& name,
392 const std::string& parent,
393 const double& r, const double& g, const double& b
399 // -------------------------------------------------------------------------
400 void cpExtensions::QT::SimpleMPRWidget::
403 this->m_MPRObjects->ClearAll( );
404 this->m_Images.clear( );
405 this->m_Meshes.clear( );
409 // -------------------------------------------------------------------------
410 std::string cpExtensions::QT::SimpleMPRWidget::
411 GetSelectedData( ) const
414 QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
416 return( item->text( 0 ).toStdString( ) );
422 // -------------------------------------------------------------------------
424 QTreeWidgetItem* cpExtensions::QT::SimpleMPRWidget::
425 _FindItemInTree( const std::string& name ) const
427 QList< QTreeWidgetItem* > items =
428 this->m_UI->LoadedData->findItems(
429 name.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
431 if( items.size( ) > 0 )
432 return( items[ 0 ] );
437 // -------------------------------------------------------------------------
438 QTreeWidgetItem* cpExtensions::QT::SimpleMPRWidget::
439 _UpdateTreeItem( const std::string& name, const std::string& parent )
442 QTreeWidgetItem* new_item = NULL;
445 QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
446 if( parent_item != NULL )
448 QTreeWidgetItem* old_item = this->_FindItemInTree( name );
449 if( old_item == NULL )
452 new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
453 parent_item->setExpanded( true );
461 new_item = new QTreeWidgetItem(
462 ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
464 this->m_UI->LoadedData->addTopLevelItem( new_item );
471 // -------------------------------------------------------------------------
473 void cpExtensions::QT::SimpleMPRWidget::
474 _Add3DActor( vtkProp3D* prop )
477 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
480 ren->AddActor( prop );
481 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
485 // -------------------------------------------------------------------------
486 void cpExtensions::QT::SimpleMPRWidget::
487 _SyncBottom( int a, int b )
489 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
492 // -------------------------------------------------------------------------
493 void cpExtensions::QT::SimpleMPRWidget::
494 _SyncTop( int a, int b )
496 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
499 // -------------------------------------------------------------------------
500 cpExtensions::QT::SimpleMPRWidget::PolyDataActor::
510 // -------------------------------------------------------------------------
511 cpExtensions::QT::SimpleMPRWidget::PolyDataActor::
514 if( this->Actor != NULL ) this->Actor->Delete( );
515 if( this->Mapper != NULL ) this->Mapper->Delete( );
516 if( this->Stripper != NULL ) this->Stripper->Delete( );
517 if( this->Normals != NULL ) this->Normals->Delete( );
520 // -------------------------------------------------------------------------
521 void cpExtensions::QT::SimpleMPRWidget::PolyDataActor::
522 Configure( vtkPolyData* pd )
528 pd->GetScalarRange( range );
530 this->Normals = vtkPolyDataNormals::New( );
531 this->Stripper = vtkStripper::New( );
532 this->Mapper = vtkPolyDataMapper::New( );
533 this->Actor = vtkQuadricLODActor::New( );
536 this->Normals->SetInputData( pd );
537 this->Normals->SetFeatureAngle( 60.0 );
538 this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
539 this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
540 this->Mapper->UseLookupTableScalarRangeOff( );
541 this->Mapper->SetScalarRange(
542 range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
544 this->Actor->SetMapper( this->Mapper );
545 this->Actor->DeferLODConstructionOff( );
548 // -------------------------------------------------------------------------
549 cpExtensions::QT::SimpleMPRWidget::Data::
552 this->Tag = Data::IMAGE;
556 // -------------------------------------------------------------------------
557 cpExtensions::QT::SimpleMPRWidget::Data::
562 // -------------------------------------------------------------------------
563 cpExtensions::QT::SimpleMPRWidget::
564 Data& cpExtensions::QT::SimpleMPRWidget::Data::
565 operator=( const Data& data )
567 this->Tag = data.Tag;
568 if( this->Tag == Data::IMAGE )
569 this->Image = data.Image;
570 else if( this->Tag == Data::MESH )
571 this->Mesh = data.Mesh;
575 // -------------------------------------------------------------------------
576 void cpExtensions::QT::SimpleMPRWidget::Data::
577 SetImageData( vtkImageData* data )
579 this->Tag = Data::IMAGE;
583 // -------------------------------------------------------------------------
584 void cpExtensions::QT::SimpleMPRWidget::Data::
585 SetPolyData( vtkPolyData* data )
587 this->Tag = Data::MESH;
588 this->Mesh.Configure( data );
591 // -------------------------------------------------------------------------
592 vtkImageData* cpExtensions::QT::SimpleMPRWidget::Data::
595 if( this->Tag == Data::IMAGE )
596 return( this->Image );
601 // -------------------------------------------------------------------------
602 vtkPolyData* cpExtensions::QT::SimpleMPRWidget::Data::
605 if( this->Tag == Data::MESH )
606 return( this->Mesh.Mesh );
611 // -------------------------------------------------------------------------
612 vtkProp* cpExtensions::QT::SimpleMPRWidget::Data::
615 if( this->Tag == Data::MESH )
616 return( this->Mesh.Actor );
621 #endif // cpExtensions_QT4