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>
10 #include <vtkRendererCollection.h>
12 double cpPlugins::Interface::SimpleMPRWidget::
25 // -------------------------------------------------------------------------
26 cpPlugins::Interface::SimpleMPRWidget::
27 SimpleMPRWidget( QWidget* parent )
29 m_UI( new Ui::SimpleMPRWidget ),
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::SimpleMPRWidget::
66 // -------------------------------------------------------------------------
67 unsigned int cpPlugins::Interface::SimpleMPRWidget::
68 GetNumberOfData( ) const
70 return( this->m_Data.size( ) );
73 // -------------------------------------------------------------------------
74 bool cpPlugins::Interface::SimpleMPRWidget::
76 cpPlugins::Interface::DataObject* data, const std::string& name,
77 const std::string& parent
83 auto iIt = this->m_Data.find( name );
84 if( iIt == this->m_Data.end( ) )
88 auto pIt = this->m_Data.find( parent );
89 if( pIt == this->m_Data.end( ) )
95 this->m_Data[ name ].SetSourceDataObject( data );
98 // TODO: this->_UpdateTreeItem( name, parent );
105 // -------------------------------------------------------------------------
106 const std::string& cpPlugins::Interface::SimpleMPRWidget::
107 GetMainImage( ) const
109 return( this->m_MainImage );
112 // -------------------------------------------------------------------------
113 bool cpPlugins::Interface::SimpleMPRWidget::
114 SetMainImage( const std::string& name )
116 auto iIt = this->m_Data.find( name );
117 if( iIt != this->m_Data.end( ) )
119 if( iIt->second.Tag == Data::IMAGE )
121 this->m_MainImage = name;
131 // -------------------------------------------------------------------------
132 void cpPlugins::Interface::SimpleMPRWidget::
133 DeleteData( const std::string& name )
135 auto iIt = this->m_Data.find( name );
136 if( iIt != this->m_Data.end( ) )
138 this->m_Data.erase( iIt );
141 std::vector< std::string > to_erase;
142 auto tIt = this->m_Tree.begin( );
143 for( ; tIt != this->m_Tree.end( ); ++tIt )
144 if( tIt->second == name )
145 to_erase.push_back( tIt->first );
148 tIt = this->m_Tree.find( name );
149 if( tIt != this->m_Tree.end( ) )
150 this->m_Tree.erase( tIt );
153 auto dIt = to_erase.begin( );
154 for( ; dIt != to_erase.end( ); ++dIt )
155 this->DeleteData( *dIt );
157 // Delete from tree widget
159 QTreeWidgetItem* item = this->_FindItemInTree( name );
161 this->m_UI->LoadedData->removeItemWidget( item, 0 );
164 // Reset main image, just in case
165 if( this->m_Data.size( ) == 0 )
166 this->m_MainImage = "";
171 // -------------------------------------------------------------------------
172 void cpPlugins::Interface::SimpleMPRWidget::
175 this->m_MPRObjects->Clear( );
176 this->m_Data.clear( );
177 this->m_Tree.clear( );
178 // TODO: this->m_UI->LoadedData->clear( );
179 this->m_MainImage = "";
182 // -------------------------------------------------------------------------
183 void cpPlugins::Interface::SimpleMPRWidget::
185 const std::string& name, const double& r, const double& g, const double& b
189 auto iIt = this->m_Data.find( name );
190 if( iIt == this->m_Data.end( ) )
193 if( iIt->second.Tag == Data::IMAGE )
196 else if( iIt->second.Tag == Data::MESH )
202 // -------------------------------------------------------------------------
203 void cpPlugins::Interface::SimpleMPRWidget::
204 ShowData( const std::string& name )
206 auto iIt = this->m_Data.find( name );
207 if( iIt == this->m_Data.end( ) )
210 if( iIt->second.Tag == Data::IMAGE )
212 if( name == this->m_MainImage )
213 this->m_MPRObjects->SetInputImage( iIt->second.Image );
216 unsigned int i = ( this->m_MPRObjects->GetNumberOfImages( ) - 1 ) % 8;
217 this->m_MPRObjects->AddBinaryImage(
219 Self::cm_Colors[ i ][ 0 ],
220 Self::cm_Colors[ i ][ 1 ],
221 Self::cm_Colors[ i ][ 2 ]
224 this->m_MPRObjects->Show( );
226 else if( iIt->second.Tag == Data::MESH )
230 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
233 ren->AddActor( iIt->second.GetMeshActor( ) );
234 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
239 // -------------------------------------------------------------------------
240 void cpPlugins::Interface::SimpleMPRWidget::
241 HideData( const std::string& name )
245 // -------------------------------------------------------------------------
246 vtkRenderWindowInteractor* cpPlugins::Interface::SimpleMPRWidget::
247 GetInteractor( unsigned int i )
251 if( this->m_VTK[ i ] != NULL )
252 return( this->m_VTK[ i ]->GetInteractor( ) );
260 // -------------------------------------------------------------------------
262 bool cpPlugins::Interface::SimpleMPRWidget::
265 const std::string& name,
266 const std::string& parent
270 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
271 if( new_item == NULL )
274 // Associate new data
275 this->m_Images[ name ] = image;
276 this->m_Tree[ name ] = parent;
278 // Show image and return
279 this->m_MPRObjects->AddImage( image );
283 // -------------------------------------------------------------------------
284 bool cpPlugins::Interface::SimpleMPRWidget::
287 const std::string& name,
288 const std::string& parent,
289 const double& r, const double& g, const double& b
293 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
294 if( new_item == NULL )
297 // Associate new data
298 this->m_Images[ name ] = image;
299 this->m_Tree[ name ] = parent;
301 // Show image and return
302 this->m_MPRObjects->AddImage( image );
306 // -------------------------------------------------------------------------
307 bool cpPlugins::Interface::SimpleMPRWidget::
310 const std::string& name,
311 const std::string& parent
315 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
316 if( new_item == NULL )
319 // Associate new data
320 PolyDataActor* actor = new PolyDataActor( mesh );
321 this->m_Meshes[ name ] = actor;
322 this->m_Tree[ name ] = parent;
325 this->_Add3DActor( actor->Actor );
329 // -------------------------------------------------------------------------
330 bool cpPlugins::Interface::SimpleMPRWidget::
333 const std::string& name,
334 const std::string& parent,
335 const double& r, const double& g, const double& b
341 // -------------------------------------------------------------------------
342 void cpPlugins::Interface::SimpleMPRWidget::
345 this->m_MPRObjects->ClearAll( );
346 this->m_Images.clear( );
347 this->m_Meshes.clear( );
351 // -------------------------------------------------------------------------
352 std::string cpPlugins::Interface::SimpleMPRWidget::
353 GetSelectedData( ) const
356 QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
358 return( item->text( 0 ).toStdString( ) );
364 // -------------------------------------------------------------------------
366 QTreeWidgetItem* cpPlugins::Interface::SimpleMPRWidget::
367 _FindItemInTree( const std::string& name ) const
369 QList< QTreeWidgetItem* > items =
370 this->m_UI->LoadedData->findItems(
371 name.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
373 if( items.size( ) > 0 )
374 return( items[ 0 ] );
379 // -------------------------------------------------------------------------
380 QTreeWidgetItem* cpPlugins::Interface::SimpleMPRWidget::
381 _UpdateTreeItem( const std::string& name, const std::string& parent )
384 QTreeWidgetItem* new_item = NULL;
387 QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
388 if( parent_item != NULL )
390 QTreeWidgetItem* old_item = this->_FindItemInTree( name );
391 if( old_item == NULL )
394 new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
395 parent_item->setExpanded( true );
403 new_item = new QTreeWidgetItem(
404 ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
406 this->m_UI->LoadedData->addTopLevelItem( new_item );
413 // -------------------------------------------------------------------------
415 void cpPlugins::Interface::SimpleMPRWidget::
416 _Add3DActor( vtkProp3D* prop )
419 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
422 ren->AddActor( prop );
423 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
427 // -------------------------------------------------------------------------
428 void cpPlugins::Interface::SimpleMPRWidget::
429 _SyncBottom( int a, int b )
431 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
434 // -------------------------------------------------------------------------
435 void cpPlugins::Interface::SimpleMPRWidget::
436 _SyncTop( int a, int b )
438 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
441 // -------------------------------------------------------------------------
442 cpPlugins::Interface::SimpleMPRWidget::PolyDataActor::
452 // -------------------------------------------------------------------------
453 cpPlugins::Interface::SimpleMPRWidget::PolyDataActor::
456 if( this->Actor != NULL ) this->Actor->Delete( );
457 if( this->Mapper != NULL ) this->Mapper->Delete( );
458 if( this->Stripper != NULL ) this->Stripper->Delete( );
459 if( this->Normals != NULL ) this->Normals->Delete( );
462 // -------------------------------------------------------------------------
463 void cpPlugins::Interface::SimpleMPRWidget::PolyDataActor::
464 Configure( vtkPolyData* pd )
470 pd->GetScalarRange( range );
472 this->Normals = vtkPolyDataNormals::New( );
473 this->Stripper = vtkStripper::New( );
474 this->Mapper = vtkPolyDataMapper::New( );
475 this->Actor = vtkQuadricLODActor::New( );
478 this->Normals->SetInputData( pd );
479 this->Normals->SetFeatureAngle( 60.0 );
480 this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
481 this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
482 this->Mapper->UseLookupTableScalarRangeOff( );
483 this->Mapper->SetScalarRange(
484 range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
486 this->Actor->SetMapper( this->Mapper );
487 this->Actor->DeferLODConstructionOff( );
490 // -------------------------------------------------------------------------
491 cpPlugins::Interface::SimpleMPRWidget::Data::
494 this->Tag = Data::IMAGE;
499 // -------------------------------------------------------------------------
500 cpPlugins::Interface::SimpleMPRWidget::Data::
505 // -------------------------------------------------------------------------
506 cpPlugins::Interface::SimpleMPRWidget::
507 Data& cpPlugins::Interface::SimpleMPRWidget::Data::
508 operator=( const Data& data )
510 this->Tag = data.Tag;
511 this->Source = data.Source;
512 if( this->Tag == Data::IMAGE )
513 this->Image = data.Image;
514 else if( this->Tag == Data::MESH )
515 this->Mesh = data.Mesh;
519 // -------------------------------------------------------------------------
520 cpPlugins::Interface::
521 DataObject* cpPlugins::Interface::SimpleMPRWidget::Data::
522 GetSourceDataObject( )
524 return( this->Source );
527 // -------------------------------------------------------------------------
528 const cpPlugins::Interface::
529 DataObject* cpPlugins::Interface::SimpleMPRWidget::Data::
530 GetSourceDataObject( ) const
532 return( this->Source );
535 // -------------------------------------------------------------------------
536 void cpPlugins::Interface::SimpleMPRWidget::Data::
537 SetSourceDataObject( cpPlugins::Interface::DataObject* dobj )
539 typedef cpPlugins::Interface::Image _TImage;
540 typedef cpPlugins::Interface::Mesh _TMesh;
543 _TImage* image = dynamic_cast< _TImage* >( dobj );
546 this->Tag = Data::IMAGE;
547 this->Image = image->GetVTK< vtkImageData >( );
551 _TMesh* mesh = dynamic_cast< _TMesh* >( dobj );
555 this->Tag = Data::MESH;
556 this->Mesh.Configure( mesh->GetVTK< vtkPolyData >( ) );
561 // -------------------------------------------------------------------------
562 vtkImageData* cpPlugins::Interface::SimpleMPRWidget::Data::
565 if( this->Tag == Data::IMAGE )
566 return( this->Image );
571 // -------------------------------------------------------------------------
572 vtkPolyData* cpPlugins::Interface::SimpleMPRWidget::Data::
575 if( this->Tag == Data::MESH )
576 return( this->Mesh.Mesh );
581 // -------------------------------------------------------------------------
582 vtkProp* cpPlugins::Interface::SimpleMPRWidget::Data::
585 if( this->Tag == Data::MESH )
586 return( this->Mesh.Actor );
591 #endif // cpPlugins_Interface_QT4