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 // -------------------------------------------------------------------------
13 cpPlugins::Interface::BaseMPRWidget::
14 BaseMPRWidget( QWidget* parent )
16 m_UI( new Ui::BaseMPRWidget ),
19 this->m_UI->setupUi( this );
21 // Configure VTK widgets
22 this->m_VTK[ 0 ] = this->m_UI->VTK01;
23 this->m_VTK[ 1 ] = this->m_UI->VTK00;
24 this->m_VTK[ 2 ] = this->m_UI->VTK10;
25 this->m_VTK[ 3 ] = this->m_UI->VTK11;
27 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
28 this->m_MPRObjects->SetRenderWindows(
29 this->m_VTK[ 0 ]->GetRenderWindow( ),
30 this->m_VTK[ 1 ]->GetRenderWindow( ),
31 this->m_VTK[ 2 ]->GetRenderWindow( ),
32 this->m_VTK[ 3 ]->GetRenderWindow( )
37 this->m_UI->TopSplitter, SIGNAL( splitterMoved( int, int ) ),
38 this, SLOT( _SyncBottom( int, int ) )
41 this->m_UI->BottomSplitter, SIGNAL( splitterMoved( int, int ) ),
42 this, SLOT( _SyncTop( int, int ) )
46 // -------------------------------------------------------------------------
47 cpPlugins::Interface::BaseMPRWidget::
53 // -------------------------------------------------------------------------
54 bool cpPlugins::Interface::BaseMPRWidget::
56 cpPlugins::Interface::DataObject* data, const std::string& name,
57 const std::string& parent
63 auto iIt = this->m_Data.find( name );
64 if( iIt == this->m_Data.end( ) )
68 auto pIt = this->m_Data.find( parent );
69 if( pIt == this->m_Data.end( ) )
75 this->m_Data[ name ].SetSourceDataObject( data );
78 this->_UpdateTreeItem( name, parent );
85 // -------------------------------------------------------------------------
86 bool cpPlugins::Interface::BaseMPRWidget::
87 SetMainImage( const std::string& name )
89 auto iIt = this->m_Data.find( name );
90 if( iIt != this->m_Data.end( ) )
92 if( iIt->second.Tag == Data::IMAGE )
94 this->m_MainImage = name;
104 // -------------------------------------------------------------------------
105 void cpPlugins::Interface::BaseMPRWidget::
106 DeleteData( const std::string& name )
108 auto iIt = this->m_Data.find( name );
109 if( iIt != this->m_Data.end( ) )
111 this->m_Data.erase( iIt );
114 std::vector< std::string > to_erase;
115 auto tIt = this->m_Tree.begin( );
116 for( ; tIt != this->m_Tree.end( ); ++tIt )
117 if( tIt->second == name )
118 to_erase.push_back( tIt->first );
121 tIt = this->m_Tree.find( name );
122 if( tIt != this->m_Tree.end( ) )
123 this->m_Tree.erase( tIt );
126 auto dIt = to_erase.begin( );
127 for( ; dIt != to_erase.end( ); ++dIt )
128 this->DeleteData( *dIt );
130 // Delete from tree widget
131 QTreeWidgetItem* item = this->_FindItemInTree( name );
133 this->m_UI->LoadedData->removeItemWidget( item, 0 );
135 // Reset main image, just in case
136 if( this->m_Data.size( ) == 0 )
137 this->m_MainImage = "";
142 // -------------------------------------------------------------------------
143 void cpPlugins::Interface::BaseMPRWidget::
146 this->m_MPRObjects->Clear( );
147 this->m_Data.clear( );
148 this->m_Tree.clear( );
149 this->m_UI->LoadedData->clear( );
150 this->m_MainImage = "";
153 // -------------------------------------------------------------------------
154 void cpPlugins::Interface::BaseMPRWidget::
156 const std::string& name, const double& r, const double& g, const double& b
160 auto iIt = this->m_Data.find( name );
161 if( iIt == this->m_Data.end( ) )
164 if( iIt->second.Tag == Data::IMAGE )
167 else if( iIt->second.Tag == Data::MESH )
173 // -------------------------------------------------------------------------
174 void cpPlugins::Interface::BaseMPRWidget::
175 ShowData( const std::string& name )
177 auto iIt = this->m_Data.find( name );
178 if( iIt == this->m_Data.end( ) )
181 if( iIt->second.Tag == Data::IMAGE )
183 if( name == this->m_MainImage )
184 this->m_MPRObjects->SetInputImage( iIt->second.Image );
186 this->m_MPRObjects->AddBinaryImage( iIt->second.Image, 1, 0, 0 );
187 this->m_MPRObjects->Show( );
189 else if( iIt->second.Tag == Data::MESH )
193 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
196 ren->AddActor( iIt->second.GetMeshActor( ) );
197 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
202 // -------------------------------------------------------------------------
203 void cpPlugins::Interface::BaseMPRWidget::
204 HideData( const std::string& name )
208 // -------------------------------------------------------------------------
209 vtkRenderWindowInteractor* cpPlugins::Interface::BaseMPRWidget::
210 GetInteractor( unsigned int i )
214 if( this->m_VTK[ i ] != NULL )
215 return( this->m_VTK[ i ]->GetInteractor( ) );
223 // -------------------------------------------------------------------------
225 bool cpPlugins::Interface::BaseMPRWidget::
228 const std::string& name,
229 const std::string& parent
233 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
234 if( new_item == NULL )
237 // Associate new data
238 this->m_Images[ name ] = image;
239 this->m_Tree[ name ] = parent;
241 // Show image and return
242 this->m_MPRObjects->AddImage( image );
246 // -------------------------------------------------------------------------
247 bool cpPlugins::Interface::BaseMPRWidget::
250 const std::string& name,
251 const std::string& parent,
252 const double& r, const double& g, const double& b
256 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
257 if( new_item == NULL )
260 // Associate new data
261 this->m_Images[ name ] = image;
262 this->m_Tree[ name ] = parent;
264 // Show image and return
265 this->m_MPRObjects->AddImage( image );
269 // -------------------------------------------------------------------------
270 bool cpPlugins::Interface::BaseMPRWidget::
273 const std::string& name,
274 const std::string& parent
278 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
279 if( new_item == NULL )
282 // Associate new data
283 PolyDataActor* actor = new PolyDataActor( mesh );
284 this->m_Meshes[ name ] = actor;
285 this->m_Tree[ name ] = parent;
288 this->_Add3DActor( actor->Actor );
292 // -------------------------------------------------------------------------
293 bool cpPlugins::Interface::BaseMPRWidget::
296 const std::string& name,
297 const std::string& parent,
298 const double& r, const double& g, const double& b
304 // -------------------------------------------------------------------------
305 void cpPlugins::Interface::BaseMPRWidget::
308 this->m_MPRObjects->ClearAll( );
309 this->m_Images.clear( );
310 this->m_Meshes.clear( );
314 // -------------------------------------------------------------------------
315 std::string cpPlugins::Interface::BaseMPRWidget::
316 GetSelectedData( ) const
318 QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
320 return( item->text( 0 ).toStdString( ) );
325 // -------------------------------------------------------------------------
326 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
327 _FindItemInTree( const std::string& name ) const
329 QList< QTreeWidgetItem* > items =
330 this->m_UI->LoadedData->findItems( name.c_str( ), Qt::MatchExactly );
331 if( items.size( ) > 0 )
332 return( items[ 0 ] );
337 // -------------------------------------------------------------------------
338 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
339 _UpdateTreeItem( const std::string& name, const std::string& parent )
342 QTreeWidgetItem* new_item = NULL;
345 QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
346 if( parent_item != NULL )
348 QTreeWidgetItem* old_item = this->_FindItemInTree( name );
349 if( old_item == NULL )
352 new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
353 parent_item->setExpanded( true );
361 new_item = new QTreeWidgetItem(
362 ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
364 this->m_UI->LoadedData->addTopLevelItem( new_item );
370 // -------------------------------------------------------------------------
372 void cpPlugins::Interface::BaseMPRWidget::
373 _Add3DActor( vtkProp3D* prop )
376 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
379 ren->AddActor( prop );
380 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
384 // -------------------------------------------------------------------------
385 void cpPlugins::Interface::BaseMPRWidget::
386 _SyncBottom( int a, int b )
388 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
391 // -------------------------------------------------------------------------
392 void cpPlugins::Interface::BaseMPRWidget::
393 _SyncTop( int a, int b )
395 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
398 // -------------------------------------------------------------------------
399 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
409 // -------------------------------------------------------------------------
410 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
413 if( this->Actor != NULL ) this->Actor->Delete( );
414 if( this->Mapper != NULL ) this->Mapper->Delete( );
415 if( this->Stripper != NULL ) this->Stripper->Delete( );
416 if( this->Normals != NULL ) this->Normals->Delete( );
419 // -------------------------------------------------------------------------
420 void cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
421 Configure( vtkPolyData* pd )
427 pd->GetScalarRange( range );
429 this->Normals = vtkPolyDataNormals::New( );
430 this->Stripper = vtkStripper::New( );
431 this->Mapper = vtkPolyDataMapper::New( );
432 this->Actor = vtkQuadricLODActor::New( );
435 this->Normals->SetInputData( pd );
436 this->Normals->SetFeatureAngle( 60.0 );
437 this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
438 this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
439 this->Mapper->UseLookupTableScalarRangeOff( );
440 this->Mapper->SetScalarRange(
441 range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
443 this->Actor->SetMapper( this->Mapper );
444 this->Actor->DeferLODConstructionOff( );
447 // -------------------------------------------------------------------------
448 cpPlugins::Interface::BaseMPRWidget::Data::
451 this->Tag = Data::IMAGE;
456 // -------------------------------------------------------------------------
457 cpPlugins::Interface::BaseMPRWidget::Data::
462 // -------------------------------------------------------------------------
463 cpPlugins::Interface::BaseMPRWidget::
464 Data& cpPlugins::Interface::BaseMPRWidget::Data::
465 operator=( const Data& data )
467 this->Tag = data.Tag;
468 this->Source = data.Source;
469 if( this->Tag == Data::IMAGE )
470 this->Image = data.Image;
471 else if( this->Tag == Data::MESH )
472 this->Mesh = data.Mesh;
476 // -------------------------------------------------------------------------
477 cpPlugins::Interface::
478 DataObject* cpPlugins::Interface::BaseMPRWidget::Data::
479 GetSourceDataObject( )
481 return( this->Source );
484 // -------------------------------------------------------------------------
485 const cpPlugins::Interface::
486 DataObject* cpPlugins::Interface::BaseMPRWidget::Data::
487 GetSourceDataObject( ) const
489 return( this->Source );
492 // -------------------------------------------------------------------------
493 void cpPlugins::Interface::BaseMPRWidget::Data::
494 SetSourceDataObject( cpPlugins::Interface::DataObject* dobj )
496 typedef cpPlugins::Interface::Image _TImage;
497 typedef cpPlugins::Interface::Mesh _TMesh;
500 _TImage* image = dynamic_cast< _TImage* >( dobj );
503 this->Tag = Data::IMAGE;
504 this->Image = image->GetVTK< vtkImageData >( );
508 _TMesh* mesh = dynamic_cast< _TMesh* >( dobj );
512 this->Tag = Data::MESH;
513 this->Mesh.Configure( mesh->GetVTK< vtkPolyData >( ) );
518 // -------------------------------------------------------------------------
519 vtkImageData* cpPlugins::Interface::BaseMPRWidget::Data::
522 if( this->Tag == Data::IMAGE )
523 return( this->Image );
528 // -------------------------------------------------------------------------
529 vtkPolyData* cpPlugins::Interface::BaseMPRWidget::Data::
532 if( this->Tag == Data::MESH )
533 return( this->Mesh.Mesh );
538 // -------------------------------------------------------------------------
539 vtkProp* cpPlugins::Interface::BaseMPRWidget::Data::
542 if( this->Tag == Data::MESH )
543 return( this->Mesh.Actor );
548 #endif // cpPlugins_Interface_QT4