2 #include "MementoState.h"
3 #include "ui_ImageMPR.h"
5 #include <vtkProperty.h>
6 #include <vtkRenderWindow.h>
7 #include <vtkMetaImageReader.h>
10 #include <QMessageBox>
13 # define PLUGIN_PREFIX ""
14 # define PLUGIN_EXT "dll"
15 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
17 # define PLUGIN_PREFIX "lib"
18 # define PLUGIN_EXT "so"
19 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
22 // -------------------------------------------------------------------------
23 ImageMPR::ImageMPR( QWidget* parent )
24 : QMainWindow( parent ),
25 m_UI( new Ui::ImageMPR ),
26 m_ImageReaderClass( "" ),
27 m_ImageWriterClass( "" ),
28 m_MeshReaderClass( "" ),
29 m_MeshWriterClass( "" ),
30 m_MeshCutterClass( "" ),
34 this->m_UI->setupUi( this );
36 // Create and associate renderers
37 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
38 this->m_MPRObjects->SetRenderWindows(
39 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
40 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
41 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
42 this->m_UI->m_3DVTK->GetRenderWindow( )
47 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
48 this, SLOT( _triggered_actionOpenPlugins( ) )
51 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
52 this, SLOT( _triggered_actionOpenInputImage( ) )
55 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
56 this, SLOT( _triggered_actionOpenSegmentation( ) )
59 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
60 this, SLOT( _triggered_actionOpenInputPolyData( ) )
63 this->m_UI->actionUndo, SIGNAL(triggered()),
64 this, SLOT(_triggered_actionUndo())
67 this->m_UI->actionRedo, SIGNAL(triggered()),
68 this, SLOT(_triggered_actionRedo())
71 // Start: load all disponible plugins
73 std::string( PLUGIN_PREFIX ) +
74 std::string( "cpPluginsIO." ) +
75 std::string( PLUGIN_EXT )
78 std::string( PLUGIN_PREFIX ) +
79 std::string( "cpPluginsBasicFilters." ) +
80 std::string( PLUGIN_EXT )
84 // -------------------------------------------------------------------------
88 // Close all connections
89 this->m_Plugins.UnloadAll( );
95 // -------------------------------------------------------------------------
97 _LoadPlugins( const std::string& filename )
99 QApplication::setOverrideCursor( Qt::WaitCursor );
100 this->setEnabled( false );
102 this->m_ImageReaderClass = "";
103 this->m_ImageWriterClass = "";
104 this->m_MeshReaderClass = "";
105 this->m_MeshWriterClass = "";
106 this->m_MeshCutterClass = "";
107 this->m_UI->MenuImageToImage->clear( );
108 this->m_UI->MenuImageToMesh->clear( );
110 if( !( this->m_Plugins.Load( filename ) ) )
113 typedef TPluginsInterface::TClasses _TClasses;
114 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
115 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
117 TPluginFilter::Pointer o =
118 this->m_Plugins.CreateProcessObject( cIt->first );
119 std::string name = o->GetClassName( );
120 std::string category = o->GetClassCategory( );
121 if( category == "ImageReader" )
122 this->m_ImageReaderClass = name;
123 else if( category == "ImageWriter" )
124 this->m_ImageWriterClass = name;
125 else if( category == "MeshReader" )
126 this->m_MeshReaderClass = name;
127 else if( category == "MeshWriter" )
128 this->m_MeshWriterClass = name;
129 else if( category == "MeshToMeshFilter" )
131 if( name.find_last_of( "Cutter" ) != std::string::npos )
132 this->m_MeshCutterClass = name;
134 else if( category == "ImageToImageFilter" )
137 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
139 action, SIGNAL( triggered( ) ),
140 this, SLOT( _triggered_actionImageToImage( ) )
143 else if( category == "ImageToMeshFilter" )
146 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
148 action, SIGNAL( triggered( ) ),
149 this, SLOT( _triggered_actionImageToMesh( ) )
155 QApplication::restoreOverrideCursor( );
156 this->setEnabled( true );
161 // -------------------------------------------------------------------------
162 std::string ImageMPR::
163 _LoadImage( TPluginImage::Pointer& image )
165 std::string ret = "";
168 // Get a reader from loaded plugins
169 TPluginFilter::Pointer reader =
170 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
171 if( reader.IsNotNull( ) )
173 if( reader->ExecConfigurationDialog( this ) )
176 QApplication::setOverrideCursor( Qt::WaitCursor );
177 this->setEnabled( false );
179 // Execute and get error message, if any
180 ret = reader->Update( );
182 // Assign fresh image, if any
185 image = reader->GetOutput< TPluginImage >( 0 );
186 reader->DisconnectOutputs( );
190 // Unblock application
191 QApplication::restoreOverrideCursor( );
192 this->setEnabled( true );
197 ret = "No suitable reader object found in loaded plugins.";
202 // -------------------------------------------------------------------------
203 std::string ImageMPR::
204 _ConfigureMeshActors( )
206 if( this->m_Mesh.IsNull( ) )
207 return( "Valid mesh not found." );
209 this->m_Mesh->CreateVTKActor( );
210 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
211 if( vtk_actor != NULL )
213 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
214 this->m_MPRObjects->Render( 4 );
218 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
220 std::string err = "";
221 for( unsigned int i = 0; i < 3; ++i )
223 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
224 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
225 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
226 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
227 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
228 std::string lerr = this->m_Cutters[ i ]->Update( );
231 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
232 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
233 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
235 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
237 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
239 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
245 this->m_MPRObjects->RenderAll( );
249 // -------------------------------------------------------------------------
251 _triggered_actionOpenPlugins( )
253 // Show dialog and check if it was accepted
254 QFileDialog dialog( this );
255 dialog.setFileMode( QFileDialog::ExistingFile );
256 dialog.setDirectory( "." );
257 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
258 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
259 if( !( dialog.exec( ) ) )
262 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
263 if( !( _LoadPlugins( fname ) ) )
264 QMessageBox::critical(
266 tr( "Ignoring plugin" ),
271 // -------------------------------------------------------------------------
273 _triggered_actionOpenInputImage( )
276 std::string err = this->_LoadImage( this->m_Image );
279 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
282 this->m_MPRObjects->SetImage( vtk_id );
283 this->m_MPRObjects->ActivateInteractors( );
284 this->m_MPRObjects->ResetCameras( );
285 this->m_MPRObjects->RenderAll( );
287 MementoState(m_state, this->m_Image);
290 QMessageBox::critical(
292 tr( "Error message" ),
293 tr( "Read image does not have a valid VTK converter." )
297 QMessageBox::critical(
299 tr( "Error reading single image" ),
304 // -------------------------------------------------------------------------
306 _triggered_actionOpenSegmentation( )
308 if( this->m_Image.IsNull( ) )
310 QMessageBox::critical(
312 tr( "Error message" ),
313 tr( "Before reading a segmentation, first load a raw image." )
320 std::string err = this->_LoadImage( this->m_Segmentation );
323 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
326 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
327 this->m_MPRObjects->RenderAll( );
330 QMessageBox::critical(
332 tr( "Error message" ),
333 tr( "Read image does not have a valid VTK converter." )
337 QMessageBox::critical(
339 tr( "Error reading single image" ),
344 // -------------------------------------------------------------------------
346 _triggered_actionOpenInputPolyData( )
350 // Get a reader from plugins
351 TPluginFilter::Pointer reader =
352 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
354 if( reader.IsNotNull( ) )
357 if( reader->ExecConfigurationDialog( this ) )
359 // Execute and get error message, if any
360 QApplication::setOverrideCursor( Qt::WaitCursor );
361 this->setEnabled( false );
362 std::string err = reader->Update( );
363 QApplication::restoreOverrideCursor( );
364 this->setEnabled( true );
366 // Assign fresh mesh, if any
369 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
370 reader->DisconnectOutputs( );
371 err = this->_ConfigureMeshActors( );
373 QMessageBox::critical(
375 tr( "Error message" ),
380 QMessageBox::critical(
382 tr( "Error reading mesh" ),
389 QMessageBox::critical(
391 tr( "Error reading single mesh" ),
392 tr( "No suitable mesh reader found in loaded plugins." )
396 // -------------------------------------------------------------------------
398 _triggered_actionImageToImage( )
400 if( this->m_Image.IsNull( ) )
404 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
407 std::string name = action->text( ).toStdString( );
410 TPluginFilter::Pointer filter =
411 this->m_Plugins.CreateProcessObject( name );
412 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
417 QApplication::setOverrideCursor( Qt::WaitCursor );
418 this->setEnabled( false );
419 filter->SetInput( 0, this->m_Image );
420 std::string err = filter->Update( );
421 QApplication::restoreOverrideCursor( );
422 this->setEnabled( true );
427 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
428 result->DisconnectPipeline( );
429 this->m_Image = result;
430 if( this->m_Image.IsNotNull( ) )
431 this->m_MPRObjects->SetImage(
432 this->m_Image->GetVTK< vtkImageData >( )
436 MementoState(m_state, this->m_Image);
439 QMessageBox::critical(
441 tr( "Error executing filter" ),
446 // -------------------------------------------------------------------------
448 _triggered_actionImageToMesh( )
450 if( this->m_Image.IsNull( ) )
454 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
457 std::string name = action->text( ).toStdString( );
460 TPluginFilter::Pointer filter =
461 this->m_Plugins.CreateProcessObject( name );
462 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
467 QApplication::setOverrideCursor( Qt::WaitCursor );
468 this->setEnabled( false );
469 filter->SetInput( 0, this->m_Image );
470 std::string err = filter->Update( );
471 QApplication::restoreOverrideCursor( );
472 this->setEnabled( true );
477 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
478 result->DisconnectPipeline( );
479 this->m_Mesh = result;
480 err = this->_ConfigureMeshActors( );
482 QMessageBox::critical(
484 tr( "Error message" ),
489 QMessageBox::critical(
491 tr( "Error executing filter" ),
496 // -------------------------------------------------------------------------
498 _triggered_actionUndo()
500 MementoState memento = MementoState();
505 this->m_MPRObjects->SetImage(
506 memento.getMemento(this->m_state)->GetOutput()
510 QMessageBox::critical(
513 tr("No history to undo")
519 // -------------------------------------------------------------------------
521 _triggered_actionRedo()
523 MementoState memento = MementoState();
527 this->m_MPRObjects->SetImage(
528 memento.getMemento(this->m_state)->GetOutput()
533 QMessageBox::critical(
536 tr("No history to redo")