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 bool cpPlugins::Interface::BaseMPRWidget::
100 SetMainImage( const std::string& name )
102 auto iIt = this->m_Data.find( name );
103 if( iIt != this->m_Data.end( ) )
105 if( iIt->second.Tag == Data::IMAGE )
107 this->m_MainImage = name;
117 // -------------------------------------------------------------------------
118 void cpPlugins::Interface::BaseMPRWidget::
119 DeleteData( const std::string& name )
121 auto iIt = this->m_Data.find( name );
122 if( iIt != this->m_Data.end( ) )
124 this->m_Data.erase( iIt );
127 std::vector< std::string > to_erase;
128 auto tIt = this->m_Tree.begin( );
129 for( ; tIt != this->m_Tree.end( ); ++tIt )
130 if( tIt->second == name )
131 to_erase.push_back( tIt->first );
134 tIt = this->m_Tree.find( name );
135 if( tIt != this->m_Tree.end( ) )
136 this->m_Tree.erase( tIt );
139 auto dIt = to_erase.begin( );
140 for( ; dIt != to_erase.end( ); ++dIt )
141 this->DeleteData( *dIt );
143 // Delete from tree widget
144 QTreeWidgetItem* item = this->_FindItemInTree( name );
146 this->m_UI->LoadedData->removeItemWidget( item, 0 );
148 // Reset main image, just in case
149 if( this->m_Data.size( ) == 0 )
150 this->m_MainImage = "";
155 // -------------------------------------------------------------------------
156 void cpPlugins::Interface::BaseMPRWidget::
159 this->m_MPRObjects->Clear( );
160 this->m_Data.clear( );
161 this->m_Tree.clear( );
162 this->m_UI->LoadedData->clear( );
163 this->m_MainImage = "";
166 // -------------------------------------------------------------------------
167 void cpPlugins::Interface::BaseMPRWidget::
169 const std::string& name, const double& r, const double& g, const double& b
173 auto iIt = this->m_Data.find( name );
174 if( iIt == this->m_Data.end( ) )
177 if( iIt->second.Tag == Data::IMAGE )
180 else if( iIt->second.Tag == Data::MESH )
186 // -------------------------------------------------------------------------
187 void cpPlugins::Interface::BaseMPRWidget::
188 ShowData( const std::string& name )
190 auto iIt = this->m_Data.find( name );
191 if( iIt == this->m_Data.end( ) )
194 if( iIt->second.Tag == Data::IMAGE )
196 if( name == this->m_MainImage )
197 this->m_MPRObjects->SetInputImage( iIt->second.Image );
200 unsigned int i = ( this->m_MPRObjects->GetNumberOfImages( ) - 1 ) % 8;
201 this->m_MPRObjects->AddBinaryImage(
203 Self::cm_Colors[ i ][ 0 ],
204 Self::cm_Colors[ i ][ 1 ],
205 Self::cm_Colors[ i ][ 2 ]
208 this->m_MPRObjects->Show( );
210 else if( iIt->second.Tag == Data::MESH )
214 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
217 ren->AddActor( iIt->second.GetMeshActor( ) );
218 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
223 // -------------------------------------------------------------------------
224 void cpPlugins::Interface::BaseMPRWidget::
225 HideData( const std::string& name )
229 // -------------------------------------------------------------------------
230 vtkRenderWindowInteractor* cpPlugins::Interface::BaseMPRWidget::
231 GetInteractor( unsigned int i )
235 if( this->m_VTK[ i ] != NULL )
236 return( this->m_VTK[ i ]->GetInteractor( ) );
244 // -------------------------------------------------------------------------
246 bool cpPlugins::Interface::BaseMPRWidget::
249 const std::string& name,
250 const std::string& parent
254 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
255 if( new_item == NULL )
258 // Associate new data
259 this->m_Images[ name ] = image;
260 this->m_Tree[ name ] = parent;
262 // Show image and return
263 this->m_MPRObjects->AddImage( image );
267 // -------------------------------------------------------------------------
268 bool cpPlugins::Interface::BaseMPRWidget::
271 const std::string& name,
272 const std::string& parent,
273 const double& r, const double& g, const double& b
277 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
278 if( new_item == NULL )
281 // Associate new data
282 this->m_Images[ name ] = image;
283 this->m_Tree[ name ] = parent;
285 // Show image and return
286 this->m_MPRObjects->AddImage( image );
290 // -------------------------------------------------------------------------
291 bool cpPlugins::Interface::BaseMPRWidget::
294 const std::string& name,
295 const std::string& parent
299 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
300 if( new_item == NULL )
303 // Associate new data
304 PolyDataActor* actor = new PolyDataActor( mesh );
305 this->m_Meshes[ name ] = actor;
306 this->m_Tree[ name ] = parent;
309 this->_Add3DActor( actor->Actor );
313 // -------------------------------------------------------------------------
314 bool cpPlugins::Interface::BaseMPRWidget::
317 const std::string& name,
318 const std::string& parent,
319 const double& r, const double& g, const double& b
325 // -------------------------------------------------------------------------
326 void cpPlugins::Interface::BaseMPRWidget::
329 this->m_MPRObjects->ClearAll( );
330 this->m_Images.clear( );
331 this->m_Meshes.clear( );
335 // -------------------------------------------------------------------------
336 std::string cpPlugins::Interface::BaseMPRWidget::
337 GetSelectedData( ) const
339 QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
341 return( item->text( 0 ).toStdString( ) );
346 // -------------------------------------------------------------------------
347 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
348 _FindItemInTree( const std::string& name ) const
350 QList< QTreeWidgetItem* > items =
351 this->m_UI->LoadedData->findItems(
352 name.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
354 if( items.size( ) > 0 )
355 return( items[ 0 ] );
360 // -------------------------------------------------------------------------
361 QTreeWidgetItem* cpPlugins::Interface::BaseMPRWidget::
362 _UpdateTreeItem( const std::string& name, const std::string& parent )
365 QTreeWidgetItem* new_item = NULL;
368 QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
369 if( parent_item != NULL )
371 QTreeWidgetItem* old_item = this->_FindItemInTree( name );
372 if( old_item == NULL )
375 new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
376 parent_item->setExpanded( true );
384 new_item = new QTreeWidgetItem(
385 ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
387 this->m_UI->LoadedData->addTopLevelItem( new_item );
393 // -------------------------------------------------------------------------
395 void cpPlugins::Interface::BaseMPRWidget::
396 _Add3DActor( vtkProp3D* prop )
399 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
402 ren->AddActor( prop );
403 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
407 // -------------------------------------------------------------------------
408 void cpPlugins::Interface::BaseMPRWidget::
409 _SyncBottom( int a, int b )
411 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
414 // -------------------------------------------------------------------------
415 void cpPlugins::Interface::BaseMPRWidget::
416 _SyncTop( int a, int b )
418 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
421 // -------------------------------------------------------------------------
422 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
432 // -------------------------------------------------------------------------
433 cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
436 if( this->Actor != NULL ) this->Actor->Delete( );
437 if( this->Mapper != NULL ) this->Mapper->Delete( );
438 if( this->Stripper != NULL ) this->Stripper->Delete( );
439 if( this->Normals != NULL ) this->Normals->Delete( );
442 // -------------------------------------------------------------------------
443 void cpPlugins::Interface::BaseMPRWidget::PolyDataActor::
444 Configure( vtkPolyData* pd )
450 pd->GetScalarRange( range );
452 this->Normals = vtkPolyDataNormals::New( );
453 this->Stripper = vtkStripper::New( );
454 this->Mapper = vtkPolyDataMapper::New( );
455 this->Actor = vtkQuadricLODActor::New( );
458 this->Normals->SetInputData( pd );
459 this->Normals->SetFeatureAngle( 60.0 );
460 this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
461 this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
462 this->Mapper->UseLookupTableScalarRangeOff( );
463 this->Mapper->SetScalarRange(
464 range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
466 this->Actor->SetMapper( this->Mapper );
467 this->Actor->DeferLODConstructionOff( );
470 // -------------------------------------------------------------------------
471 cpPlugins::Interface::BaseMPRWidget::Data::
474 this->Tag = Data::IMAGE;
479 // -------------------------------------------------------------------------
480 cpPlugins::Interface::BaseMPRWidget::Data::
485 // -------------------------------------------------------------------------
486 cpPlugins::Interface::BaseMPRWidget::
487 Data& cpPlugins::Interface::BaseMPRWidget::Data::
488 operator=( const Data& data )
490 this->Tag = data.Tag;
491 this->Source = data.Source;
492 if( this->Tag == Data::IMAGE )
493 this->Image = data.Image;
494 else if( this->Tag == Data::MESH )
495 this->Mesh = data.Mesh;
499 // -------------------------------------------------------------------------
500 cpPlugins::Interface::
501 DataObject* cpPlugins::Interface::BaseMPRWidget::Data::
502 GetSourceDataObject( )
504 return( this->Source );
507 // -------------------------------------------------------------------------
508 const cpPlugins::Interface::
509 DataObject* cpPlugins::Interface::BaseMPRWidget::Data::
510 GetSourceDataObject( ) const
512 return( this->Source );
515 // -------------------------------------------------------------------------
516 void cpPlugins::Interface::BaseMPRWidget::Data::
517 SetSourceDataObject( cpPlugins::Interface::DataObject* dobj )
519 typedef cpPlugins::Interface::Image _TImage;
520 typedef cpPlugins::Interface::Mesh _TMesh;
523 _TImage* image = dynamic_cast< _TImage* >( dobj );
526 this->Tag = Data::IMAGE;
527 this->Image = image->GetVTK< vtkImageData >( );
531 _TMesh* mesh = dynamic_cast< _TMesh* >( dobj );
535 this->Tag = Data::MESH;
536 this->Mesh.Configure( mesh->GetVTK< vtkPolyData >( ) );
541 // -------------------------------------------------------------------------
542 vtkImageData* cpPlugins::Interface::BaseMPRWidget::Data::
545 if( this->Tag == Data::IMAGE )
546 return( this->Image );
551 // -------------------------------------------------------------------------
552 vtkPolyData* cpPlugins::Interface::BaseMPRWidget::Data::
555 if( this->Tag == Data::MESH )
556 return( this->Mesh.Mesh );
561 // -------------------------------------------------------------------------
562 vtkProp* cpPlugins::Interface::BaseMPRWidget::Data::
565 if( this->Tag == Data::MESH )
566 return( this->Mesh.Actor );
571 #endif // cpPlugins_Interface_QT4