1 #include <cpPlugins/Interface/BaseMPRWidget.h>
3 #ifdef cpPlugins_Interface_QT4
5 #include <cpPlugins/Interface/ui_BaseMPRWidget.h>
6 #include <cpPlugins/Interface/DataObject.h>
7 #include <cpPlugins/Interface/Image.h>
8 #include <cpPlugins/Interface/Mesh.h>
9 #include <QTreeWidgetItem>
10 #include <vtkRendererCollection.h>
12 double cpPlugins::Interface::BaseMPRWidget::
25 // -------------------------------------------------------------------------
26 cpPlugins::Interface::BaseMPRWidget::
27 BaseMPRWidget( QWidget* parent )
29 m_UI( new Ui::BaseMPRWidget ),
32 this->m_UI->setupUi( this );
34 // Configure VTK widgets
35 this->m_VTK[ 0 ] = this->m_UI->VTK01;
36 this->m_VTK[ 1 ] = this->m_UI->VTK00;
37 this->m_VTK[ 2 ] = this->m_UI->VTK10;
38 this->m_VTK[ 3 ] = this->m_UI->VTK11;
40 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
41 this->m_MPRObjects->SetRenderWindows(
42 this->m_VTK[ 0 ]->GetRenderWindow( ),
43 this->m_VTK[ 1 ]->GetRenderWindow( ),
44 this->m_VTK[ 2 ]->GetRenderWindow( ),
45 this->m_VTK[ 3 ]->GetRenderWindow( )
50 this->m_UI->TopSplitter, SIGNAL( splitterMoved( int, int ) ),
51 this, SLOT( _SyncBottom( int, int ) )
54 this->m_UI->BottomSplitter, SIGNAL( splitterMoved( int, int ) ),
55 this, SLOT( _SyncTop( int, int ) )
59 // -------------------------------------------------------------------------
60 cpPlugins::Interface::BaseMPRWidget::
66 // -------------------------------------------------------------------------
67 bool cpPlugins::Interface::BaseMPRWidget::
69 cpPlugins::Interface::DataObject* data, const std::string& name,
70 const std::string& parent
76 auto iIt = this->m_Data.find( name );
77 if( iIt == this->m_Data.end( ) )
81 auto pIt = this->m_Data.find( parent );
82 if( pIt == this->m_Data.end( ) )
88 this->m_Data[ name ].SetSourceDataObject( data );
91 this->_UpdateTreeItem( name, parent );
98 // -------------------------------------------------------------------------
99 const std::string& cpPlugins::Interface::BaseMPRWidget::
100 GetMainImage( ) const
102 return( this->m_MainImage );
105 // -------------------------------------------------------------------------
106 bool cpPlugins::Interface::BaseMPRWidget::
107 SetMainImage( const std::string& name )
109 auto iIt = this->m_Data.find( name );
110 if( iIt != this->m_Data.end( ) )
112 if( iIt->second.Tag == Data::IMAGE )
114 this->m_MainImage = name;
124 // -------------------------------------------------------------------------
125 void cpPlugins::Interface::BaseMPRWidget::
126 DeleteData( const std::string& name )
128 auto iIt = this->m_Data.find( name );
129 if( iIt != this->m_Data.end( ) )
131 this->m_Data.erase( iIt );
134 std::vector< std::string > to_erase;
135 auto tIt = this->m_Tree.begin( );
136 for( ; tIt != this->m_Tree.end( ); ++tIt )
137 if( tIt->second == name )
138 to_erase.push_back( tIt->first );
141 tIt = this->m_Tree.find( name );
142 if( tIt != this->m_Tree.end( ) )
143 this->m_Tree.erase( tIt );
146 auto dIt = to_erase.begin( );
147 for( ; dIt != to_erase.end( ); ++dIt )
148 this->DeleteData( *dIt );
150 // Delete from tree widget
151 QTreeWidgetItem* item = this->_FindItemInTree( name );
153 this->m_UI->LoadedData->removeItemWidget( item, 0 );
155 // Reset main image, just in case
156 if( this->m_Data.size( ) == 0 )
157 this->m_MainImage = "";
162 // -------------------------------------------------------------------------
163 void cpPlugins::Interface::BaseMPRWidget::
166 this->m_MPRObjects->Clear( );
167 this->m_Data.clear( );
168 this->m_Tree.clear( );
169 this->m_UI->LoadedData->clear( );
170 this->m_MainImage = "";
173 // -------------------------------------------------------------------------
174 void cpPlugins::Interface::BaseMPRWidget::
176 const std::string& name, const double& r, const double& g, const double& b
180 auto iIt = this->m_Data.find( name );
181 if( iIt == this->m_Data.end( ) )
184 if( iIt->second.Tag == Data::IMAGE )
187 else if( iIt->second.Tag == Data::MESH )
193 // -------------------------------------------------------------------------
194 void cpPlugins::Interface::BaseMPRWidget::
195 ShowData( const std::string& name )
197 auto iIt = this->m_Data.find( name );
198 if( iIt == this->m_Data.end( ) )
201 if( iIt->second.Tag == Data::IMAGE )
203 if( name == this->m_MainImage )
204 this->m_MPRObjects->SetInputImage( iIt->second.Image );
207 unsigned int i = ( this->m_MPRObjects->GetNumberOfImages( ) - 1 ) % 8;
208 this->m_MPRObjects->AddBinaryImage(
210 Self::cm_Colors[ i ][ 0 ],
211 Self::cm_Colors[ i ][ 1 ],
212 Self::cm_Colors[ i ][ 2 ]
215 this->m_MPRObjects->Show( );
217 else if( iIt->second.Tag == Data::MESH )
221 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
224 ren->AddActor( iIt->second.GetMeshActor( ) );
225 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
230 // -------------------------------------------------------------------------
231 void cpPlugins::Interface::BaseMPRWidget::
232 HideData( const std::string& name )
236 // -------------------------------------------------------------------------
237 vtkRenderWindowInteractor* cpPlugins::Interface::BaseMPRWidget::
238 GetInteractor( unsigned int i )
242 if( this->m_VTK[ i ] != NULL )
243 return( this->m_VTK[ i ]->GetInteractor( ) );
251 // -------------------------------------------------------------------------
253 bool cpPlugins::Interface::BaseMPRWidget::
256 const std::string& name,
257 const std::string& parent
261 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
262 if( new_item == NULL )
265 // Associate new data
266 this->m_Images[ name ] = image;
267 this->m_Tree[ name ] = parent;
269 // Show image and return
270 this->m_MPRObjects->AddImage( image );
274 // -------------------------------------------------------------------------
275 bool cpPlugins::Interface::BaseMPRWidget::
278 const std::string& name,
279 const std::string& parent,
280 const double& r, const double& g, const double& b
284 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
285 if( new_item == NULL )
288 // Associate new data
289 this->m_Images[ name ] = image;
290 this->m_Tree[ name ] = parent;
292 // Show image and return
293 this->m_MPRObjects->AddImage( image );
297 // -------------------------------------------------------------------------
298 bool cpPlugins::Interface::BaseMPRWidget::
301 const std::string& name,
302 const std::string& parent
306 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
307 if( new_item == NULL )
310 // Associate new data
311 PolyDataActor* actor = new PolyDataActor( mesh );
312 this->m_Meshes[ name ] = actor;
313 this->m_Tree[ name ] = parent;
316 this->_Add3DActor( actor->Actor );
320 // -------------------------------------------------------------------------
321 bool cpPlugins::Interface::BaseMPRWidget::
324 const std::string& name,
325 const std::string& parent,
326 const double& r, const double& g, const double& b
332 // -------------------------------------------------------------------------
333 void cpPlugins::Interface::BaseMPRWidget::
336 this->m_MPRObjects->ClearAll( );
337 this->m_Images.clear( );
338 this->m_Meshes.clear( );
342 // -------------------------------------------------------------------------
343 std::string cpPlugins::Interface::BaseMPRWidget::
344 GetSelectedData( ) const
346 QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
348 return( item->text( 0 ).toStdString( ) );
353 // -------------------------------------------------------------------------
354 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
355 _FindItemInTree( const std::string& name ) const
357 QList< QTreeWidgetItem* > items =
358 this->m_UI->LoadedData->findItems(
359 name.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
361 if( items.size( ) > 0 )
362 return( items[ 0 ] );
367 // -------------------------------------------------------------------------
368 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
369 _UpdateTreeItem( const std::string& name, const std::string& parent )
372 QTreeWidgetItem* new_item = NULL;
375 QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
376 if( parent_item != NULL )
378 QTreeWidgetItem* old_item = this->_FindItemInTree( name );
379 if( old_item == NULL )
382 new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
383 parent_item->setExpanded( true );
391 new_item = new QTreeWidgetItem(
392 ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
394 this->m_UI->LoadedData->addTopLevelItem( new_item );
400 // -------------------------------------------------------------------------
402 void cpPlugins::Interface::BaseMPRWidget::
403 _Add3DActor( vtkProp3D* prop )
406 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
409 ren->AddActor( prop );
410 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
414 // -------------------------------------------------------------------------
415 void cpPlugins::Interface::BaseMPRWidget::
416 _SyncBottom( int a, int b )
418 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
421 // -------------------------------------------------------------------------
422 void cpPlugins::Interface::BaseMPRWidget::
423 _SyncTop( int a, int b )
425 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
428 // -------------------------------------------------------------------------
429 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
439 // -------------------------------------------------------------------------
440 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
443 if( this->Actor != NULL ) this->Actor->Delete( );
444 if( this->Mapper != NULL ) this->Mapper->Delete( );
445 if( this->Stripper != NULL ) this->Stripper->Delete( );
446 if( this->Normals != NULL ) this->Normals->Delete( );
449 // -------------------------------------------------------------------------
450 void cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
451 Configure( vtkPolyData* pd )
457 pd->GetScalarRange( range );
459 this->Normals = vtkPolyDataNormals::New( );
460 this->Stripper = vtkStripper::New( );
461 this->Mapper = vtkPolyDataMapper::New( );
462 this->Actor = vtkQuadricLODActor::New( );
465 this->Normals->SetInputData( pd );
466 this->Normals->SetFeatureAngle( 60.0 );
467 this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
468 this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
469 this->Mapper->UseLookupTableScalarRangeOff( );
470 this->Mapper->SetScalarRange(
471 range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
473 this->Actor->SetMapper( this->Mapper );
474 this->Actor->DeferLODConstructionOff( );
477 // -------------------------------------------------------------------------
478 cpPlugins::Interface::BaseMPRWidget::Data::
481 this->Tag = Data::IMAGE;
486 // -------------------------------------------------------------------------
487 cpPlugins::Interface::BaseMPRWidget::Data::
492 // -------------------------------------------------------------------------
493 cpPlugins::Interface::BaseMPRWidget::
494 Data& cpPlugins::Interface::BaseMPRWidget::Data::
495 operator=( const Data& data )
497 this->Tag = data.Tag;
498 this->Source = data.Source;
499 if( this->Tag == Data::IMAGE )
500 this->Image = data.Image;
501 else if( this->Tag == Data::MESH )
502 this->Mesh = data.Mesh;
506 // -------------------------------------------------------------------------
507 cpPlugins::Interface::
508 DataObject* cpPlugins::Interface::BaseMPRWidget::Data::
509 GetSourceDataObject( )
511 return( this->Source );
514 // -------------------------------------------------------------------------
515 const cpPlugins::Interface::
516 DataObject* cpPlugins::Interface::BaseMPRWidget::Data::
517 GetSourceDataObject( ) const
519 return( this->Source );
522 // -------------------------------------------------------------------------
523 void cpPlugins::Interface::BaseMPRWidget::Data::
524 SetSourceDataObject( cpPlugins::Interface::DataObject* dobj )
526 typedef cpPlugins::Interface::Image _TImage;
527 typedef cpPlugins::Interface::Mesh _TMesh;
530 _TImage* image = dynamic_cast< _TImage* >( dobj );
533 this->Tag = Data::IMAGE;
534 this->Image = image->GetVTK< vtkImageData >( );
538 _TMesh* mesh = dynamic_cast< _TMesh* >( dobj );
542 this->Tag = Data::MESH;
543 this->Mesh.Configure( mesh->GetVTK< vtkPolyData >( ) );
548 // -------------------------------------------------------------------------
549 vtkImageData* cpPlugins::Interface::BaseMPRWidget::Data::
552 if( this->Tag == Data::IMAGE )
553 return( this->Image );
558 // -------------------------------------------------------------------------
559 vtkPolyData* cpPlugins::Interface::BaseMPRWidget::Data::
562 if( this->Tag == Data::MESH )
563 return( this->Mesh.Mesh );
568 // -------------------------------------------------------------------------
569 vtkProp* cpPlugins::Interface::BaseMPRWidget::Data::
572 if( this->Tag == Data::MESH )
573 return( this->Mesh.Actor );
578 #endif // cpPlugins_Interface_QT4