1 #include <cpPlugins/Interface/SimpleMPRWidget.h>
3 #ifdef cpPlugins_Interface_QT4
5 #include <cpPlugins/Interface/ui_SimpleMPRWidget.h>
6 #include <cpPlugins/Interface/DataObject.h>
7 #include <cpPlugins/Interface/Image.h>
8 #include <cpPlugins/Interface/Mesh.h>
9 // TODO: #include <QTreeWidgetItem>
11 #include <vtkProperty.h>
12 #include <vtkRendererCollection.h>
14 double cpPlugins::Interface::SimpleMPRWidget::
27 // -------------------------------------------------------------------------
28 cpPlugins::Interface::SimpleMPRWidget::
29 SimpleMPRWidget( QWidget* parent )
31 m_UI( new Ui::SimpleMPRWidget ),
34 this->m_UI->setupUi( this );
36 // Configure VTK widgets
37 this->m_VTK[ 0 ] = this->m_UI->VTK01;
38 this->m_VTK[ 1 ] = this->m_UI->VTK00;
39 this->m_VTK[ 2 ] = this->m_UI->VTK10;
40 this->m_VTK[ 3 ] = this->m_UI->VTK11;
42 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
43 this->m_MPRObjects->SetRenderWindows(
44 this->m_VTK[ 0 ]->GetRenderWindow( ),
45 this->m_VTK[ 1 ]->GetRenderWindow( ),
46 this->m_VTK[ 2 ]->GetRenderWindow( ),
47 this->m_VTK[ 3 ]->GetRenderWindow( )
52 this->m_UI->TopSplitter, SIGNAL( splitterMoved( int, int ) ),
53 this, SLOT( _SyncBottom( int, int ) )
56 this->m_UI->BottomSplitter, SIGNAL( splitterMoved( int, int ) ),
57 this, SLOT( _SyncTop( int, int ) )
61 // -------------------------------------------------------------------------
62 cpPlugins::Interface::SimpleMPRWidget::
68 // -------------------------------------------------------------------------
69 unsigned int cpPlugins::Interface::SimpleMPRWidget::
70 GetNumberOfData( ) const
72 return( this->m_Data.size( ) );
75 // -------------------------------------------------------------------------
76 bool cpPlugins::Interface::SimpleMPRWidget::
78 cpPlugins::Interface::DataObject* data, const std::string& name,
79 const std::string& parent
85 auto iIt = this->m_Data.find( name );
86 if( iIt == this->m_Data.end( ) )
90 auto pIt = this->m_Data.find( parent );
91 if( pIt == this->m_Data.end( ) )
97 this->m_Data[ name ].SetSourceDataObject( data );
100 auto iIt = this->m_Data.find( name );
101 if( iIt != this->m_Data.end( ) )
104 dynamic_cast< vtkActor* >( iIt->second.GetMeshActor( ) );
107 unsigned int idx = this->m_Data.size( ) % 8;
108 actor->GetProperty( )->SetColor(
109 Self::cm_Colors[ idx ][ 0 ],
110 Self::cm_Colors[ idx ][ 1 ],
111 Self::cm_Colors[ idx ][ 2 ]
119 // TODO: this->_UpdateTreeItem( name, parent );
126 // -------------------------------------------------------------------------
127 const std::string& cpPlugins::Interface::SimpleMPRWidget::
128 GetMainImage( ) const
130 return( this->m_MainImage );
133 // -------------------------------------------------------------------------
134 bool cpPlugins::Interface::SimpleMPRWidget::
135 SetMainImage( const std::string& name )
137 auto iIt = this->m_Data.find( name );
138 if( iIt != this->m_Data.end( ) )
140 if( iIt->second.Tag == Data::IMAGE )
142 this->m_MainImage = name;
152 // -------------------------------------------------------------------------
153 void cpPlugins::Interface::SimpleMPRWidget::
154 DeleteData( const std::string& name )
156 auto iIt = this->m_Data.find( name );
157 if( iIt != this->m_Data.end( ) )
159 this->m_Data.erase( iIt );
162 std::vector< std::string > to_erase;
163 auto tIt = this->m_Tree.begin( );
164 for( ; tIt != this->m_Tree.end( ); ++tIt )
165 if( tIt->second == name )
166 to_erase.push_back( tIt->first );
169 tIt = this->m_Tree.find( name );
170 if( tIt != this->m_Tree.end( ) )
171 this->m_Tree.erase( tIt );
174 auto dIt = to_erase.begin( );
175 for( ; dIt != to_erase.end( ); ++dIt )
176 this->DeleteData( *dIt );
178 // Delete from tree widget
180 QTreeWidgetItem* item = this->_FindItemInTree( name );
182 this->m_UI->LoadedData->removeItemWidget( item, 0 );
185 // Reset main image, just in case
186 if( this->m_Data.size( ) == 0 )
187 this->m_MainImage = "";
192 // -------------------------------------------------------------------------
193 void cpPlugins::Interface::SimpleMPRWidget::
196 this->m_MPRObjects->Clear( );
197 this->m_Data.clear( );
198 this->m_Tree.clear( );
199 // TODO: this->m_UI->LoadedData->clear( );
200 this->m_MainImage = "";
203 // -------------------------------------------------------------------------
204 void cpPlugins::Interface::SimpleMPRWidget::
206 const std::string& name, const double& r, const double& g, const double& b
210 auto iIt = this->m_Data.find( name );
211 if( iIt == this->m_Data.end( ) )
214 if( iIt->second.Tag == Data::IMAGE )
217 else if( iIt->second.Tag == Data::MESH )
223 // -------------------------------------------------------------------------
224 void cpPlugins::Interface::SimpleMPRWidget::
225 ShowData( const std::string& name )
227 auto iIt = this->m_Data.find( name );
228 if( iIt == this->m_Data.end( ) )
231 if( iIt->second.Tag == Data::IMAGE )
233 if( name == this->m_MainImage )
234 this->m_MPRObjects->SetInputImage( iIt->second.Image );
237 unsigned int i = ( this->m_MPRObjects->GetNumberOfImages( ) - 1 ) % 8;
238 this->m_MPRObjects->AddBinaryImage(
240 Self::cm_Colors[ i ][ 0 ],
241 Self::cm_Colors[ i ][ 1 ],
242 Self::cm_Colors[ i ][ 2 ]
245 this->m_MPRObjects->Show( );
247 else if( iIt->second.Tag == Data::MESH )
250 this->m_VTK[ 3 ]->GetRenderWindow( )->
251 GetRenderers( )->GetFirstRenderer( );
254 ren->AddActor( iIt->second.GetMeshActor( ) );
255 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
260 // -------------------------------------------------------------------------
261 void cpPlugins::Interface::SimpleMPRWidget::
262 HideData( const std::string& name )
266 // -------------------------------------------------------------------------
267 vtkRenderWindowInteractor* cpPlugins::Interface::SimpleMPRWidget::
268 GetInteractor( unsigned int i )
272 if( this->m_VTK[ i ] != NULL )
273 return( this->m_VTK[ i ]->GetInteractor( ) );
281 // -------------------------------------------------------------------------
283 bool cpPlugins::Interface::SimpleMPRWidget::
286 const std::string& name,
287 const std::string& parent
291 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
292 if( new_item == NULL )
295 // Associate new data
296 this->m_Images[ name ] = image;
297 this->m_Tree[ name ] = parent;
299 // Show image and return
300 this->m_MPRObjects->AddImage( image );
304 // -------------------------------------------------------------------------
305 bool cpPlugins::Interface::SimpleMPRWidget::
308 const std::string& name,
309 const std::string& parent,
310 const double& r, const double& g, const double& b
314 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
315 if( new_item == NULL )
318 // Associate new data
319 this->m_Images[ name ] = image;
320 this->m_Tree[ name ] = parent;
322 // Show image and return
323 this->m_MPRObjects->AddImage( image );
327 // -------------------------------------------------------------------------
328 bool cpPlugins::Interface::SimpleMPRWidget::
331 const std::string& name,
332 const std::string& parent
336 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
337 if( new_item == NULL )
340 // Associate new data
341 PolyDataActor* actor = new PolyDataActor( mesh );
342 this->m_Meshes[ name ] = actor;
343 this->m_Tree[ name ] = parent;
346 this->_Add3DActor( actor->Actor );
350 // -------------------------------------------------------------------------
351 bool cpPlugins::Interface::SimpleMPRWidget::
354 const std::string& name,
355 const std::string& parent,
356 const double& r, const double& g, const double& b
362 // -------------------------------------------------------------------------
363 void cpPlugins::Interface::SimpleMPRWidget::
366 this->m_MPRObjects->ClearAll( );
367 this->m_Images.clear( );
368 this->m_Meshes.clear( );
372 // -------------------------------------------------------------------------
373 std::string cpPlugins::Interface::SimpleMPRWidget::
374 GetSelectedData( ) const
377 QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
379 return( item->text( 0 ).toStdString( ) );
385 // -------------------------------------------------------------------------
387 QTreeWidgetItem* cpPlugins::Interface::SimpleMPRWidget::
388 _FindItemInTree( const std::string& name ) const
390 QList< QTreeWidgetItem* > items =
391 this->m_UI->LoadedData->findItems(
392 name.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
394 if( items.size( ) > 0 )
395 return( items[ 0 ] );
400 // -------------------------------------------------------------------------
401 QTreeWidgetItem* cpPlugins::Interface::SimpleMPRWidget::
402 _UpdateTreeItem( const std::string& name, const std::string& parent )
405 QTreeWidgetItem* new_item = NULL;
408 QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
409 if( parent_item != NULL )
411 QTreeWidgetItem* old_item = this->_FindItemInTree( name );
412 if( old_item == NULL )
415 new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
416 parent_item->setExpanded( true );
424 new_item = new QTreeWidgetItem(
425 ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
427 this->m_UI->LoadedData->addTopLevelItem( new_item );
434 // -------------------------------------------------------------------------
436 void cpPlugins::Interface::SimpleMPRWidget::
437 _Add3DActor( vtkProp3D* prop )
440 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
443 ren->AddActor( prop );
444 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
448 // -------------------------------------------------------------------------
449 void cpPlugins::Interface::SimpleMPRWidget::
450 _SyncBottom( int a, int b )
452 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
455 // -------------------------------------------------------------------------
456 void cpPlugins::Interface::SimpleMPRWidget::
457 _SyncTop( int a, int b )
459 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
462 // -------------------------------------------------------------------------
463 cpPlugins::Interface::SimpleMPRWidget::PolyDataActor::
473 // -------------------------------------------------------------------------
474 cpPlugins::Interface::SimpleMPRWidget::PolyDataActor::
477 if( this->Actor != NULL ) this->Actor->Delete( );
478 if( this->Mapper != NULL ) this->Mapper->Delete( );
479 if( this->Stripper != NULL ) this->Stripper->Delete( );
480 if( this->Normals != NULL ) this->Normals->Delete( );
483 // -------------------------------------------------------------------------
484 void cpPlugins::Interface::SimpleMPRWidget::PolyDataActor::
485 Configure( vtkPolyData* pd )
491 pd->GetScalarRange( range );
493 this->Normals = vtkPolyDataNormals::New( );
494 this->Stripper = vtkStripper::New( );
495 this->Mapper = vtkPolyDataMapper::New( );
496 this->Actor = vtkQuadricLODActor::New( );
499 this->Normals->SetInputData( pd );
500 this->Normals->SetFeatureAngle( 60.0 );
501 this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
502 this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
503 this->Mapper->UseLookupTableScalarRangeOff( );
504 this->Mapper->SetScalarRange(
505 range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
507 this->Actor->SetMapper( this->Mapper );
508 this->Actor->DeferLODConstructionOff( );
511 // -------------------------------------------------------------------------
512 cpPlugins::Interface::SimpleMPRWidget::Data::
515 this->Tag = Data::IMAGE;
520 // -------------------------------------------------------------------------
521 cpPlugins::Interface::SimpleMPRWidget::Data::
526 // -------------------------------------------------------------------------
527 cpPlugins::Interface::SimpleMPRWidget::
528 Data& cpPlugins::Interface::SimpleMPRWidget::Data::
529 operator=( const Data& data )
531 this->Tag = data.Tag;
532 this->Source = data.Source;
533 if( this->Tag == Data::IMAGE )
534 this->Image = data.Image;
535 else if( this->Tag == Data::MESH )
536 this->Mesh = data.Mesh;
540 // -------------------------------------------------------------------------
541 cpPlugins::Interface::
542 DataObject* cpPlugins::Interface::SimpleMPRWidget::Data::
543 GetSourceDataObject( )
545 return( this->Source );
548 // -------------------------------------------------------------------------
549 const cpPlugins::Interface::
550 DataObject* cpPlugins::Interface::SimpleMPRWidget::Data::
551 GetSourceDataObject( ) const
553 return( this->Source );
556 // -------------------------------------------------------------------------
557 void cpPlugins::Interface::SimpleMPRWidget::Data::
558 SetSourceDataObject( cpPlugins::Interface::DataObject* dobj )
560 typedef cpPlugins::Interface::Image _TImage;
561 typedef cpPlugins::Interface::Mesh _TMesh;
564 _TImage* image = dynamic_cast< _TImage* >( dobj );
567 this->Tag = Data::IMAGE;
568 this->Image = image->GetVTK< vtkImageData >( );
572 _TMesh* mesh = dynamic_cast< _TMesh* >( dobj );
576 this->Tag = Data::MESH;
577 this->Mesh.Configure( mesh->GetVTK< vtkPolyData >( ) );
582 // -------------------------------------------------------------------------
583 vtkImageData* cpPlugins::Interface::SimpleMPRWidget::Data::
586 if( this->Tag == Data::IMAGE )
587 return( this->Image );
592 // -------------------------------------------------------------------------
593 vtkPolyData* cpPlugins::Interface::SimpleMPRWidget::Data::
596 if( this->Tag == Data::MESH )
597 return( this->Mesh.Mesh );
602 // -------------------------------------------------------------------------
603 vtkProp* cpPlugins::Interface::SimpleMPRWidget::Data::
606 if( this->Tag == Data::MESH )
607 return( this->Mesh.Actor );
612 #endif // cpPlugins_Interface_QT4