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 vtkRenderWindowInteractor* cpExtensions::QT::SimpleMPRWidget::
304 GetInteractor( unsigned int i )
308 if( this->m_VTK[ i ] != NULL )
309 return( this->m_VTK[ i ]->GetInteractor( ) );
317 // -------------------------------------------------------------------------
319 bool cpExtensions::QT::SimpleMPRWidget::
322 const std::string& name,
323 const std::string& parent
327 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
328 if( new_item == NULL )
331 // Associate new data
332 this->m_Images[ name ] = image;
333 this->m_Tree[ name ] = parent;
335 // Show image and return
336 this->m_MPRObjects->AddImage( image );
340 // -------------------------------------------------------------------------
341 bool cpExtensions::QT::SimpleMPRWidget::
344 const std::string& name,
345 const std::string& parent,
346 const double& r, const double& g, const double& b
350 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
351 if( new_item == NULL )
354 // Associate new data
355 this->m_Images[ name ] = image;
356 this->m_Tree[ name ] = parent;
358 // Show image and return
359 this->m_MPRObjects->AddImage( image );
363 // -------------------------------------------------------------------------
364 bool cpExtensions::QT::SimpleMPRWidget::
367 const std::string& name,
368 const std::string& parent
372 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
373 if( new_item == NULL )
376 // Associate new data
377 PolyDataActor* actor = new PolyDataActor( mesh );
378 this->m_Meshes[ name ] = actor;
379 this->m_Tree[ name ] = parent;
382 this->_Add3DActor( actor->Actor );
386 // -------------------------------------------------------------------------
387 bool cpExtensions::QT::SimpleMPRWidget::
390 const std::string& name,
391 const std::string& parent,
392 const double& r, const double& g, const double& b
398 // -------------------------------------------------------------------------
399 void cpExtensions::QT::SimpleMPRWidget::
402 this->m_MPRObjects->ClearAll( );
403 this->m_Images.clear( );
404 this->m_Meshes.clear( );
408 // -------------------------------------------------------------------------
409 std::string cpExtensions::QT::SimpleMPRWidget::
410 GetSelectedData( ) const
413 QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
415 return( item->text( 0 ).toStdString( ) );
421 // -------------------------------------------------------------------------
423 QTreeWidgetItem* cpExtensions::QT::SimpleMPRWidget::
424 _FindItemInTree( const std::string& name ) const
426 QList< QTreeWidgetItem* > items =
427 this->m_UI->LoadedData->findItems(
428 name.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
430 if( items.size( ) > 0 )
431 return( items[ 0 ] );
436 // -------------------------------------------------------------------------
437 QTreeWidgetItem* cpExtensions::QT::SimpleMPRWidget::
438 _UpdateTreeItem( const std::string& name, const std::string& parent )
441 QTreeWidgetItem* new_item = NULL;
444 QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
445 if( parent_item != NULL )
447 QTreeWidgetItem* old_item = this->_FindItemInTree( name );
448 if( old_item == NULL )
451 new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
452 parent_item->setExpanded( true );
460 new_item = new QTreeWidgetItem(
461 ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
463 this->m_UI->LoadedData->addTopLevelItem( new_item );
470 // -------------------------------------------------------------------------
472 void cpExtensions::QT::SimpleMPRWidget::
473 _Add3DActor( vtkProp3D* prop )
476 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
479 ren->AddActor( prop );
480 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
484 // -------------------------------------------------------------------------
485 void cpExtensions::QT::SimpleMPRWidget::
486 _SyncBottom( int a, int b )
488 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
491 // -------------------------------------------------------------------------
492 void cpExtensions::QT::SimpleMPRWidget::
493 _SyncTop( int a, int b )
495 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
498 // -------------------------------------------------------------------------
499 cpExtensions::QT::SimpleMPRWidget::PolyDataActor::
509 // -------------------------------------------------------------------------
510 cpExtensions::QT::SimpleMPRWidget::PolyDataActor::
513 if( this->Actor != NULL ) this->Actor->Delete( );
514 if( this->Mapper != NULL ) this->Mapper->Delete( );
515 if( this->Stripper != NULL ) this->Stripper->Delete( );
516 if( this->Normals != NULL ) this->Normals->Delete( );
519 // -------------------------------------------------------------------------
520 void cpExtensions::QT::SimpleMPRWidget::PolyDataActor::
521 Configure( vtkPolyData* pd )
527 pd->GetScalarRange( range );
529 this->Normals = vtkPolyDataNormals::New( );
530 this->Stripper = vtkStripper::New( );
531 this->Mapper = vtkPolyDataMapper::New( );
532 this->Actor = vtkQuadricLODActor::New( );
535 this->Normals->SetInputData( pd );
536 this->Normals->SetFeatureAngle( 60.0 );
537 this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
538 this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
539 this->Mapper->UseLookupTableScalarRangeOff( );
540 this->Mapper->SetScalarRange(
541 range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
543 this->Actor->SetMapper( this->Mapper );
544 this->Actor->GetProperty( )->SetPointSize( 10 );
545 this->Actor->DeferLODConstructionOff( );
548 // -------------------------------------------------------------------------
549 cpExtensions::QT::SimpleMPRWidget::Data::
552 this->Tag = Data::IMAGE;
556 // -------------------------------------------------------------------------
557 cpExtensions::QT::SimpleMPRWidget::Data::
562 // -------------------------------------------------------------------------
563 cpExtensions::QT::SimpleMPRWidget::
564 Data& cpExtensions::QT::SimpleMPRWidget::Data::
565 operator=( const Data& data )
567 this->Tag = data.Tag;
568 if( this->Tag == Data::IMAGE )
569 this->Image = data.Image;
570 else if( this->Tag == Data::MESH )
571 this->Mesh = data.Mesh;
575 // -------------------------------------------------------------------------
576 void cpExtensions::QT::SimpleMPRWidget::Data::
577 SetImageData( vtkImageData* data )
579 this->Tag = Data::IMAGE;
583 // -------------------------------------------------------------------------
584 void cpExtensions::QT::SimpleMPRWidget::Data::
585 SetPolyData( vtkPolyData* data )
587 this->Tag = Data::MESH;
588 this->Mesh.Configure( data );
591 // -------------------------------------------------------------------------
592 vtkImageData* cpExtensions::QT::SimpleMPRWidget::Data::
595 if( this->Tag == Data::IMAGE )
596 return( this->Image );
601 // -------------------------------------------------------------------------
602 vtkPolyData* cpExtensions::QT::SimpleMPRWidget::Data::
605 if( this->Tag == Data::MESH )
606 return( this->Mesh.Mesh );
611 // -------------------------------------------------------------------------
612 vtkProp* cpExtensions::QT::SimpleMPRWidget::Data::
615 if( this->Tag == Data::MESH )
616 return( this->Mesh.Actor );
621 #endif // cpExtensions_QT4