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::
64 // -------------------------------------------------------------------------
65 unsigned int cpExtensions::QT::SimpleMPRWidget::
66 GetNumberOfData( ) const
68 return( this->m_Data.size( ) );
71 // -------------------------------------------------------------------------
72 bool cpExtensions::QT::SimpleMPRWidget::
74 vtkImageData* data, const std::string& name,
75 const std::string& parent
81 auto iIt = this->m_Data.find( name );
82 if( iIt == this->m_Data.end( ) )
86 auto pIt = this->m_Data.find( parent );
87 if( pIt == this->m_Data.end( ) )
93 this->m_Data[ name ].SetImageData( data );
96 auto iIt = this->m_Data.find( name );
97 if( iIt != this->m_Data.end( ) )
100 dynamic_cast< vtkActor* >( iIt->second.GetMeshActor( ) );
103 unsigned int idx = this->m_Data.size( ) % 8;
104 actor->GetProperty( )->SetColor(
105 Self::cm_Colors[ idx ][ 0 ],
106 Self::cm_Colors[ idx ][ 1 ],
107 Self::cm_Colors[ idx ][ 2 ]
115 // TODO: this->_UpdateTreeItem( name, parent );
122 // -------------------------------------------------------------------------
123 bool cpExtensions::QT::SimpleMPRWidget::
124 AddData( vtkPolyData* data, const std::string& name )
129 auto iIt = this->m_Data.find( name );
130 if( iIt == this->m_Data.end( ) )
133 this->m_Data[ name ].SetPolyData( data );
136 auto iIt = this->m_Data.find( name );
137 if( iIt != this->m_Data.end( ) )
140 dynamic_cast< vtkActor* >( iIt->second.GetMeshActor( ) );
143 unsigned int idx = this->m_Data.size( ) % 8;
144 actor->GetProperty( )->SetColor(
145 Self::cm_Colors[ idx ][ 0 ],
146 Self::cm_Colors[ idx ][ 1 ],
147 Self::cm_Colors[ idx ][ 2 ]
155 // TODO: this->_UpdateTreeItem( name, parent );
162 // -------------------------------------------------------------------------
163 const std::string& cpExtensions::QT::SimpleMPRWidget::
164 GetMainImage( ) const
166 return( this->m_MainImage );
169 // -------------------------------------------------------------------------
170 bool cpExtensions::QT::SimpleMPRWidget::
171 SetMainImage( const std::string& name )
173 auto iIt = this->m_Data.find( name );
174 if( iIt != this->m_Data.end( ) )
176 if( iIt->second.Tag == Data::IMAGE )
178 this->m_MainImage = name;
188 // -------------------------------------------------------------------------
189 void cpExtensions::QT::SimpleMPRWidget::
190 DeleteData( const std::string& name )
192 auto iIt = this->m_Data.find( name );
193 if( iIt != this->m_Data.end( ) )
195 this->m_Data.erase( iIt );
198 std::vector< std::string > to_erase;
199 auto tIt = this->m_Tree.begin( );
200 for( ; tIt != this->m_Tree.end( ); ++tIt )
201 if( tIt->second == name )
202 to_erase.push_back( tIt->first );
205 tIt = this->m_Tree.find( name );
206 if( tIt != this->m_Tree.end( ) )
207 this->m_Tree.erase( tIt );
210 auto dIt = to_erase.begin( );
211 for( ; dIt != to_erase.end( ); ++dIt )
212 this->DeleteData( *dIt );
214 // Delete from tree widget
216 QTreeWidgetItem* item = this->_FindItemInTree( name );
218 this->m_UI->LoadedData->removeItemWidget( item, 0 );
221 // Reset main image, just in case
222 if( this->m_Data.size( ) == 0 )
223 this->m_MainImage = "";
228 // -------------------------------------------------------------------------
229 void cpExtensions::QT::SimpleMPRWidget::
232 this->m_MPRObjects->Clear( );
233 this->m_Data.clear( );
234 this->m_Tree.clear( );
235 // TODO: this->m_UI->LoadedData->clear( );
236 this->m_MainImage = "";
239 // -------------------------------------------------------------------------
240 void cpExtensions::QT::SimpleMPRWidget::
242 const std::string& name, const double& r, const double& g, const double& b
246 auto iIt = this->m_Data.find( name );
247 if( iIt == this->m_Data.end( ) )
250 if( iIt->second.Tag == Data::IMAGE )
253 else if( iIt->second.Tag == Data::MESH )
259 // -------------------------------------------------------------------------
260 void cpExtensions::QT::SimpleMPRWidget::
261 ShowData( const std::string& name )
263 auto iIt = this->m_Data.find( name );
264 if( iIt == this->m_Data.end( ) )
267 if( iIt->second.Tag == Data::IMAGE )
269 if( name == this->m_MainImage )
270 this->m_MPRObjects->SetInputImage( iIt->second.Image );
273 unsigned int i = ( this->m_MPRObjects->GetNumberOfImages( ) - 1 ) % 8;
274 this->m_MPRObjects->AddBinaryImage(
276 Self::cm_Colors[ i ][ 0 ],
277 Self::cm_Colors[ i ][ 1 ],
278 Self::cm_Colors[ i ][ 2 ]
281 this->m_MPRObjects->Show( );
283 else if( iIt->second.Tag == Data::MESH )
286 this->m_VTK[ 3 ]->GetRenderWindow( )->
287 GetRenderers( )->GetFirstRenderer( );
290 ren->AddActor( iIt->second.GetMeshActor( ) );
291 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
296 // -------------------------------------------------------------------------
297 void cpExtensions::QT::SimpleMPRWidget::
298 HideData( const std::string& name )
302 // -------------------------------------------------------------------------
303 void cpExtensions::QT::SimpleMPRWidget::
304 SetWindowLevel( const double& w, const double& l )
306 this->m_MPRObjects->SetWindowLevel( w, l );
309 // -------------------------------------------------------------------------
310 double cpExtensions::QT::SimpleMPRWidget::
313 return( this->m_MPRObjects->GetWindow( ) );
316 // -------------------------------------------------------------------------
317 double cpExtensions::QT::SimpleMPRWidget::
320 return( this->m_MPRObjects->GetLevel( ) );
323 // -------------------------------------------------------------------------
324 vtkRenderWindowInteractor* cpExtensions::QT::SimpleMPRWidget::
325 GetInteractor( unsigned int i )
329 if( this->m_VTK[ i ] != NULL )
330 return( this->m_VTK[ i ]->GetInteractor( ) );
338 // -------------------------------------------------------------------------
340 bool cpExtensions::QT::SimpleMPRWidget::
343 const std::string& name,
344 const std::string& parent
348 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
349 if( new_item == NULL )
352 // Associate new data
353 this->m_Images[ name ] = image;
354 this->m_Tree[ name ] = parent;
356 // Show image and return
357 this->m_MPRObjects->AddImage( image );
361 // -------------------------------------------------------------------------
362 bool cpExtensions::QT::SimpleMPRWidget::
365 const std::string& name,
366 const std::string& parent,
367 const double& r, const double& g, const double& b
371 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
372 if( new_item == NULL )
375 // Associate new data
376 this->m_Images[ name ] = image;
377 this->m_Tree[ name ] = parent;
379 // Show image and return
380 this->m_MPRObjects->AddImage( image );
384 // -------------------------------------------------------------------------
385 bool cpExtensions::QT::SimpleMPRWidget::
388 const std::string& name,
389 const std::string& parent
393 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
394 if( new_item == NULL )
397 // Associate new data
398 PolyDataActor* actor = new PolyDataActor( mesh );
399 this->m_Meshes[ name ] = actor;
400 this->m_Tree[ name ] = parent;
403 this->_Add3DActor( actor->Actor );
407 // -------------------------------------------------------------------------
408 bool cpExtensions::QT::SimpleMPRWidget::
411 const std::string& name,
412 const std::string& parent,
413 const double& r, const double& g, const double& b
419 // -------------------------------------------------------------------------
420 void cpExtensions::QT::SimpleMPRWidget::
423 this->m_MPRObjects->ClearAll( );
424 this->m_Images.clear( );
425 this->m_Meshes.clear( );
429 // -------------------------------------------------------------------------
430 std::string cpExtensions::QT::SimpleMPRWidget::
431 GetSelectedData( ) const
434 QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
436 return( item->text( 0 ).toStdString( ) );
442 // -------------------------------------------------------------------------
444 QTreeWidgetItem* cpExtensions::QT::SimpleMPRWidget::
445 _FindItemInTree( const std::string& name ) const
447 QList< QTreeWidgetItem* > items =
448 this->m_UI->LoadedData->findItems(
449 name.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
451 if( items.size( ) > 0 )
452 return( items[ 0 ] );
457 // -------------------------------------------------------------------------
458 QTreeWidgetItem* cpExtensions::QT::SimpleMPRWidget::
459 _UpdateTreeItem( const std::string& name, const std::string& parent )
462 QTreeWidgetItem* new_item = NULL;
465 QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
466 if( parent_item != NULL )
468 QTreeWidgetItem* old_item = this->_FindItemInTree( name );
469 if( old_item == NULL )
472 new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
473 parent_item->setExpanded( true );
481 new_item = new QTreeWidgetItem(
482 ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
484 this->m_UI->LoadedData->addTopLevelItem( new_item );
491 // -------------------------------------------------------------------------
493 void cpExtensions::QT::SimpleMPRWidget::
494 _Add3DActor( vtkProp3D* prop )
497 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
500 ren->AddActor( prop );
501 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
505 // -------------------------------------------------------------------------
506 void cpExtensions::QT::SimpleMPRWidget::
507 _SyncBottom( int a, int b )
509 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
512 // -------------------------------------------------------------------------
513 void cpExtensions::QT::SimpleMPRWidget::
514 _SyncTop( int a, int b )
516 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
519 // -------------------------------------------------------------------------
520 cpExtensions::QT::SimpleMPRWidget::PolyDataActor::
530 // -------------------------------------------------------------------------
531 cpExtensions::QT::SimpleMPRWidget::PolyDataActor::
534 if( this->Actor != NULL ) this->Actor->Delete( );
535 if( this->Mapper != NULL ) this->Mapper->Delete( );
536 if( this->Stripper != NULL ) this->Stripper->Delete( );
537 if( this->Normals != NULL ) this->Normals->Delete( );
540 // -------------------------------------------------------------------------
541 void cpExtensions::QT::SimpleMPRWidget::PolyDataActor::
542 Configure( vtkPolyData* pd )
548 pd->GetScalarRange( range );
550 this->Normals = vtkPolyDataNormals::New( );
551 this->Stripper = vtkStripper::New( );
552 this->Mapper = vtkPolyDataMapper::New( );
553 this->Actor = vtkQuadricLODActor::New( );
556 this->Normals->SetInputData( pd );
557 this->Normals->SetFeatureAngle( 60.0 );
558 this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
559 this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
560 this->Mapper->UseLookupTableScalarRangeOff( );
561 this->Mapper->SetScalarRange(
562 range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
564 this->Actor->SetMapper( this->Mapper );
565 this->Actor->GetProperty( )->SetPointSize( 10 );
566 this->Actor->DeferLODConstructionOff( );
569 // -------------------------------------------------------------------------
570 cpExtensions::QT::SimpleMPRWidget::Data::
573 this->Tag = Data::IMAGE;
577 // -------------------------------------------------------------------------
578 cpExtensions::QT::SimpleMPRWidget::Data::
583 // -------------------------------------------------------------------------
584 cpExtensions::QT::SimpleMPRWidget::
585 Data& cpExtensions::QT::SimpleMPRWidget::Data::
586 operator=( const Data& data )
588 this->Tag = data.Tag;
589 if( this->Tag == Data::IMAGE )
590 this->Image = data.Image;
591 else if( this->Tag == Data::MESH )
592 this->Mesh = data.Mesh;
596 // -------------------------------------------------------------------------
597 void cpExtensions::QT::SimpleMPRWidget::Data::
598 SetImageData( vtkImageData* data )
600 this->Tag = Data::IMAGE;
604 // -------------------------------------------------------------------------
605 void cpExtensions::QT::SimpleMPRWidget::Data::
606 SetPolyData( vtkPolyData* data )
608 this->Tag = Data::MESH;
609 this->Mesh.Configure( data );
612 // -------------------------------------------------------------------------
613 vtkImageData* cpExtensions::QT::SimpleMPRWidget::Data::
616 if( this->Tag == Data::IMAGE )
617 return( this->Image );
622 // -------------------------------------------------------------------------
623 vtkPolyData* cpExtensions::QT::SimpleMPRWidget::Data::
626 if( this->Tag == Data::MESH )
627 return( this->Mesh.Mesh );
632 // -------------------------------------------------------------------------
633 vtkProp* cpExtensions::QT::SimpleMPRWidget::Data::
636 if( this->Tag == Data::MESH )
637 return( this->Mesh.Actor );
642 #endif // cpExtensions_QT4