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 bool cpPlugins::Interface::SimpleMPRWidget::
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 // TODO: this->_UpdateTreeItem( name, parent );
98 // -------------------------------------------------------------------------
99 const std::string& cpPlugins::Interface::SimpleMPRWidget::
100 GetMainImage( ) const
102 return( this->m_MainImage );
105 // -------------------------------------------------------------------------
106 bool cpPlugins::Interface::SimpleMPRWidget::
107 SetMainImage( const std::string& name )
109 auto iIt = this->m_Data.find( name );
110 if( iIt != this->m_Data.end( ) )
112 if( iIt->second.Tag == Data::IMAGE )
114 this->m_MainImage = name;
124 // -------------------------------------------------------------------------
125 void cpPlugins::Interface::SimpleMPRWidget::
126 DeleteData( const std::string& name )
128 auto iIt = this->m_Data.find( name );
129 if( iIt != this->m_Data.end( ) )
131 this->m_Data.erase( iIt );
134 std::vector< std::string > to_erase;
135 auto tIt = this->m_Tree.begin( );
136 for( ; tIt != this->m_Tree.end( ); ++tIt )
137 if( tIt->second == name )
138 to_erase.push_back( tIt->first );
141 tIt = this->m_Tree.find( name );
142 if( tIt != this->m_Tree.end( ) )
143 this->m_Tree.erase( tIt );
146 auto dIt = to_erase.begin( );
147 for( ; dIt != to_erase.end( ); ++dIt )
148 this->DeleteData( *dIt );
150 // Delete from tree widget
152 QTreeWidgetItem* item = this->_FindItemInTree( name );
154 this->m_UI->LoadedData->removeItemWidget( item, 0 );
157 // Reset main image, just in case
158 if( this->m_Data.size( ) == 0 )
159 this->m_MainImage = "";
164 // -------------------------------------------------------------------------
165 void cpPlugins::Interface::SimpleMPRWidget::
168 this->m_MPRObjects->Clear( );
169 this->m_Data.clear( );
170 this->m_Tree.clear( );
171 // TODO: this->m_UI->LoadedData->clear( );
172 this->m_MainImage = "";
175 // -------------------------------------------------------------------------
176 void cpPlugins::Interface::SimpleMPRWidget::
178 const std::string& name, const double& r, const double& g, const double& b
182 auto iIt = this->m_Data.find( name );
183 if( iIt == this->m_Data.end( ) )
186 if( iIt->second.Tag == Data::IMAGE )
189 else if( iIt->second.Tag == Data::MESH )
195 // -------------------------------------------------------------------------
196 void cpPlugins::Interface::SimpleMPRWidget::
197 ShowData( const std::string& name )
199 auto iIt = this->m_Data.find( name );
200 if( iIt == this->m_Data.end( ) )
203 if( iIt->second.Tag == Data::IMAGE )
205 if( name == this->m_MainImage )
206 this->m_MPRObjects->SetInputImage( iIt->second.Image );
209 unsigned int i = ( this->m_MPRObjects->GetNumberOfImages( ) - 1 ) % 8;
210 this->m_MPRObjects->AddBinaryImage(
212 Self::cm_Colors[ i ][ 0 ],
213 Self::cm_Colors[ i ][ 1 ],
214 Self::cm_Colors[ i ][ 2 ]
217 this->m_MPRObjects->Show( );
219 else if( iIt->second.Tag == Data::MESH )
223 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
226 ren->AddActor( iIt->second.GetMeshActor( ) );
227 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
232 // -------------------------------------------------------------------------
233 void cpPlugins::Interface::SimpleMPRWidget::
234 HideData( const std::string& name )
238 // -------------------------------------------------------------------------
239 vtkRenderWindowInteractor* cpPlugins::Interface::SimpleMPRWidget::
240 GetInteractor( unsigned int i )
244 if( this->m_VTK[ i ] != NULL )
245 return( this->m_VTK[ i ]->GetInteractor( ) );
253 // -------------------------------------------------------------------------
255 bool cpPlugins::Interface::SimpleMPRWidget::
258 const std::string& name,
259 const std::string& parent
263 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
264 if( new_item == NULL )
267 // Associate new data
268 this->m_Images[ name ] = image;
269 this->m_Tree[ name ] = parent;
271 // Show image and return
272 this->m_MPRObjects->AddImage( image );
276 // -------------------------------------------------------------------------
277 bool cpPlugins::Interface::SimpleMPRWidget::
280 const std::string& name,
281 const std::string& parent,
282 const double& r, const double& g, const double& b
286 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
287 if( new_item == NULL )
290 // Associate new data
291 this->m_Images[ name ] = image;
292 this->m_Tree[ name ] = parent;
294 // Show image and return
295 this->m_MPRObjects->AddImage( image );
299 // -------------------------------------------------------------------------
300 bool cpPlugins::Interface::SimpleMPRWidget::
303 const std::string& name,
304 const std::string& parent
308 QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
309 if( new_item == NULL )
312 // Associate new data
313 PolyDataActor* actor = new PolyDataActor( mesh );
314 this->m_Meshes[ name ] = actor;
315 this->m_Tree[ name ] = parent;
318 this->_Add3DActor( actor->Actor );
322 // -------------------------------------------------------------------------
323 bool cpPlugins::Interface::SimpleMPRWidget::
326 const std::string& name,
327 const std::string& parent,
328 const double& r, const double& g, const double& b
334 // -------------------------------------------------------------------------
335 void cpPlugins::Interface::SimpleMPRWidget::
338 this->m_MPRObjects->ClearAll( );
339 this->m_Images.clear( );
340 this->m_Meshes.clear( );
344 // -------------------------------------------------------------------------
345 std::string cpPlugins::Interface::SimpleMPRWidget::
346 GetSelectedData( ) const
349 QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
351 return( item->text( 0 ).toStdString( ) );
357 // -------------------------------------------------------------------------
359 QTreeWidgetItem* cpPlugins::Interface::SimpleMPRWidget::
360 _FindItemInTree( const std::string& name ) const
362 QList< QTreeWidgetItem* > items =
363 this->m_UI->LoadedData->findItems(
364 name.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
366 if( items.size( ) > 0 )
367 return( items[ 0 ] );
372 // -------------------------------------------------------------------------
373 QTreeWidgetItem* cpPlugins::Interface::SimpleMPRWidget::
374 _UpdateTreeItem( const std::string& name, const std::string& parent )
377 QTreeWidgetItem* new_item = NULL;
380 QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
381 if( parent_item != NULL )
383 QTreeWidgetItem* old_item = this->_FindItemInTree( name );
384 if( old_item == NULL )
387 new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
388 parent_item->setExpanded( true );
396 new_item = new QTreeWidgetItem(
397 ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
399 this->m_UI->LoadedData->addTopLevelItem( new_item );
406 // -------------------------------------------------------------------------
408 void cpPlugins::Interface::SimpleMPRWidget::
409 _Add3DActor( vtkProp3D* prop )
412 this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
415 ren->AddActor( prop );
416 this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
420 // -------------------------------------------------------------------------
421 void cpPlugins::Interface::SimpleMPRWidget::
422 _SyncBottom( int a, int b )
424 this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
427 // -------------------------------------------------------------------------
428 void cpPlugins::Interface::SimpleMPRWidget::
429 _SyncTop( int a, int b )
431 this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
434 // -------------------------------------------------------------------------
435 cpPlugins::Interface::SimpleMPRWidget::PolyDataActor::
445 // -------------------------------------------------------------------------
446 cpPlugins::Interface::SimpleMPRWidget::PolyDataActor::
449 if( this->Actor != NULL ) this->Actor->Delete( );
450 if( this->Mapper != NULL ) this->Mapper->Delete( );
451 if( this->Stripper != NULL ) this->Stripper->Delete( );
452 if( this->Normals != NULL ) this->Normals->Delete( );
455 // -------------------------------------------------------------------------
456 void cpPlugins::Interface::SimpleMPRWidget::PolyDataActor::
457 Configure( vtkPolyData* pd )
463 pd->GetScalarRange( range );
465 this->Normals = vtkPolyDataNormals::New( );
466 this->Stripper = vtkStripper::New( );
467 this->Mapper = vtkPolyDataMapper::New( );
468 this->Actor = vtkQuadricLODActor::New( );
471 this->Normals->SetInputData( pd );
472 this->Normals->SetFeatureAngle( 60.0 );
473 this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
474 this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
475 this->Mapper->UseLookupTableScalarRangeOff( );
476 this->Mapper->SetScalarRange(
477 range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
479 this->Actor->SetMapper( this->Mapper );
480 this->Actor->DeferLODConstructionOff( );
483 // -------------------------------------------------------------------------
484 cpPlugins::Interface::SimpleMPRWidget::Data::
487 this->Tag = Data::IMAGE;
492 // -------------------------------------------------------------------------
493 cpPlugins::Interface::SimpleMPRWidget::Data::
498 // -------------------------------------------------------------------------
499 cpPlugins::Interface::SimpleMPRWidget::
500 Data& cpPlugins::Interface::SimpleMPRWidget::Data::
501 operator=( const Data& data )
503 this->Tag = data.Tag;
504 this->Source = data.Source;
505 if( this->Tag == Data::IMAGE )
506 this->Image = data.Image;
507 else if( this->Tag == Data::MESH )
508 this->Mesh = data.Mesh;
512 // -------------------------------------------------------------------------
513 cpPlugins::Interface::
514 DataObject* cpPlugins::Interface::SimpleMPRWidget::Data::
515 GetSourceDataObject( )
517 return( this->Source );
520 // -------------------------------------------------------------------------
521 const cpPlugins::Interface::
522 DataObject* cpPlugins::Interface::SimpleMPRWidget::Data::
523 GetSourceDataObject( ) const
525 return( this->Source );
528 // -------------------------------------------------------------------------
529 void cpPlugins::Interface::SimpleMPRWidget::Data::
530 SetSourceDataObject( cpPlugins::Interface::DataObject* dobj )
532 typedef cpPlugins::Interface::Image _TImage;
533 typedef cpPlugins::Interface::Mesh _TMesh;
536 _TImage* image = dynamic_cast< _TImage* >( dobj );
539 this->Tag = Data::IMAGE;
540 this->Image = image->GetVTK< vtkImageData >( );
544 _TMesh* mesh = dynamic_cast< _TMesh* >( dobj );
548 this->Tag = Data::MESH;
549 this->Mesh.Configure( mesh->GetVTK< vtkPolyData >( ) );
554 // -------------------------------------------------------------------------
555 vtkImageData* cpPlugins::Interface::SimpleMPRWidget::Data::
558 if( this->Tag == Data::IMAGE )
559 return( this->Image );
564 // -------------------------------------------------------------------------
565 vtkPolyData* cpPlugins::Interface::SimpleMPRWidget::Data::
568 if( this->Tag == Data::MESH )
569 return( this->Mesh.Mesh );
574 // -------------------------------------------------------------------------
575 vtkProp* cpPlugins::Interface::SimpleMPRWidget::Data::
578 if( this->Tag == Data::MESH )
579 return( this->Mesh.Actor );
584 #endif // cpPlugins_Interface_QT4