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( "" ),
35 this->m_UI->setupUi( this );
37 // Create and associate renderers
38 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
39 this->m_MPRObjects->SetRenderWindows(
40 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
41 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
42 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
43 this->m_UI->m_3DVTK->GetRenderWindow( )
48 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
49 this, SLOT( _triggered_actionOpenPlugins( ) )
52 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
53 this, SLOT( _triggered_actionOpenInputImage( ) )
56 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
57 this, SLOT( _triggered_actionOpenSegmentation( ) )
60 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
61 this, SLOT( _triggered_actionOpenInputPolyData( ) )
64 this->m_UI->actionUndo, SIGNAL(triggered()),
65 this, SLOT(_triggered_actionUndo())
68 this->m_UI->actionRedo, SIGNAL(triggered()),
69 this, SLOT(_triggered_actionRedo())
72 // Start: load all disponible plugins
74 std::string( PLUGIN_PREFIX ) +
75 std::string( "cpPluginsIO." ) +
76 std::string( PLUGIN_EXT )
79 std::string( PLUGIN_PREFIX ) +
80 std::string( "cpPluginsBasicFilters." ) +
81 std::string( PLUGIN_EXT )
85 // -------------------------------------------------------------------------
89 // Close all connections
90 this->m_Plugins.UnloadAll( );
96 // -------------------------------------------------------------------------
98 _LoadPlugins( const std::string& filename )
100 QApplication::setOverrideCursor( Qt::WaitCursor );
101 this->setEnabled( false );
103 this->m_ImageReaderClass = "";
104 this->m_ImageWriterClass = "";
105 this->m_MeshReaderClass = "";
106 this->m_MeshWriterClass = "";
107 this->m_MeshCutterClass = "";
108 this->m_UI->MenuImageToImage->clear( );
109 this->m_UI->MenuImageToMesh->clear( );
111 if( !( this->m_Plugins.Load( filename ) ) )
114 typedef TPluginsInterface::TClasses _TClasses;
115 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
116 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
118 TPluginFilter::Pointer o =
119 this->m_Plugins.CreateProcessObject( cIt->first );
120 std::string name = o->GetClassName( );
121 std::string category = o->GetClassCategory( );
122 if( category == "ImageReader" )
123 this->m_ImageReaderClass = name;
124 else if( category == "ImageWriter" )
125 this->m_ImageWriterClass = name;
126 else if( category == "MeshReader" )
127 this->m_MeshReaderClass = name;
128 else if( category == "MeshWriter" )
129 this->m_MeshWriterClass = name;
130 else if( category == "MeshToMeshFilter" )
132 if( name.find_last_of( "Cutter" ) != std::string::npos )
133 this->m_MeshCutterClass = name;
135 else if( category == "ImageToImageFilter" )
138 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
140 action, SIGNAL( triggered( ) ),
141 this, SLOT( _triggered_actionImageToImage( ) )
144 else if( category == "ImageToMeshFilter" )
147 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
149 action, SIGNAL( triggered( ) ),
150 this, SLOT( _triggered_actionImageToMesh( ) )
156 QApplication::restoreOverrideCursor( );
157 this->setEnabled( true );
162 // -------------------------------------------------------------------------
163 std::string ImageMPR::
164 _LoadImage( TPluginImage::Pointer& image )
166 std::string ret = "";
169 // Get a reader from loaded plugins
170 TPluginFilter::Pointer reader =
171 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
172 if( reader.IsNotNull( ) )
174 if( reader->ExecConfigurationDialog( this ) )
177 QApplication::setOverrideCursor( Qt::WaitCursor );
178 this->setEnabled( false );
180 // Execute and get error message, if any
181 ret = reader->Update( );
183 // Assign fresh image, if any
186 image = reader->GetOutput< TPluginImage >( 0 );
187 reader->DisconnectOutputs( );
191 // Unblock application
192 QApplication::restoreOverrideCursor( );
193 this->setEnabled( true );
198 ret = "No suitable reader object found in loaded plugins.";
203 // -------------------------------------------------------------------------
204 std::string ImageMPR::
205 _ConfigureMeshActors( )
207 if( this->m_Mesh.IsNull( ) )
208 return( "Valid mesh not found." );
210 this->m_Mesh->CreateVTKActor( );
211 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
212 if( vtk_actor != NULL )
214 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
215 this->m_MPRObjects->Render( 4 );
219 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
221 std::string err = "";
222 for( unsigned int i = 0; i < 3; ++i )
224 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
225 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
226 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
227 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
228 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
229 std::string lerr = this->m_Cutters[ i ]->Update( );
232 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
233 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
234 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
236 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
238 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
240 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
246 this->m_MPRObjects->RenderAll( );
250 // -------------------------------------------------------------------------
252 _triggered_actionOpenPlugins( )
254 // Show dialog and check if it was accepted
255 QFileDialog dialog( this );
256 dialog.setFileMode( QFileDialog::ExistingFile );
257 dialog.setDirectory( "." );
258 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
259 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
260 if( !( dialog.exec( ) ) )
263 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
264 if( !( _LoadPlugins( fname ) ) )
265 QMessageBox::critical(
267 tr( "Ignoring plugin" ),
272 // -------------------------------------------------------------------------
274 _triggered_actionOpenInputImage( )
277 std::string err = this->_LoadImage( this->m_Image );
280 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
283 this->m_MPRObjects->SetImage( vtk_id );
284 this->m_MPRObjects->ActivateInteractors( );
285 this->m_MPRObjects->ResetCameras( );
286 this->m_MPRObjects->RenderAll( );
288 MementoState(m_state, this->m_Image);
292 QMessageBox::critical(
294 tr( "Error message" ),
295 tr( "Read image does not have a valid VTK converter." )
299 QMessageBox::critical(
301 tr( "Error reading single image" ),
306 // -------------------------------------------------------------------------
308 _triggered_actionOpenSegmentation( )
310 if( this->m_Image.IsNull( ) )
312 QMessageBox::critical(
314 tr( "Error message" ),
315 tr( "Before reading a segmentation, first load a raw image." )
322 std::string err = this->_LoadImage( this->m_Segmentation );
325 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
328 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
329 this->m_MPRObjects->RenderAll( );
332 QMessageBox::critical(
334 tr( "Error message" ),
335 tr( "Read image does not have a valid VTK converter." )
339 QMessageBox::critical(
341 tr( "Error reading single image" ),
346 // -------------------------------------------------------------------------
348 _triggered_actionOpenInputPolyData( )
352 // Get a reader from plugins
353 TPluginFilter::Pointer reader =
354 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
356 if( reader.IsNotNull( ) )
359 if( reader->ExecConfigurationDialog( this ) )
361 // Execute and get error message, if any
362 QApplication::setOverrideCursor( Qt::WaitCursor );
363 this->setEnabled( false );
364 std::string err = reader->Update( );
365 QApplication::restoreOverrideCursor( );
366 this->setEnabled( true );
368 // Assign fresh mesh, if any
371 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
372 reader->DisconnectOutputs( );
373 err = this->_ConfigureMeshActors( );
375 QMessageBox::critical(
377 tr( "Error message" ),
382 QMessageBox::critical(
384 tr( "Error reading mesh" ),
391 QMessageBox::critical(
393 tr( "Error reading single mesh" ),
394 tr( "No suitable mesh reader found in loaded plugins." )
398 // -------------------------------------------------------------------------
400 _triggered_actionImageToImage( )
402 if( this->m_Image.IsNull( ) )
406 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
409 std::string name = action->text( ).toStdString( );
412 TPluginFilter::Pointer filter =
413 this->m_Plugins.CreateProcessObject( name );
414 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
419 QApplication::setOverrideCursor( Qt::WaitCursor );
420 this->setEnabled( false );
421 filter->SetInput( 0, this->m_Image );
422 std::string err = filter->Update( );
423 QApplication::restoreOverrideCursor( );
424 this->setEnabled( true );
429 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
430 result->DisconnectPipeline( );
431 this->m_Image = result;
432 if( this->m_Image.IsNotNull( ) )
433 this->m_MPRObjects->SetImage(
434 this->m_Image->GetVTK< vtkImageData >( )
439 MementoState(this->m_state, this->m_Image);
441 if (this->m_state > this->m_max_state)
443 this->m_max_state = this->m_state;
447 QMessageBox::critical(
449 tr( "Error executing filter" ),
454 // -------------------------------------------------------------------------
456 _triggered_actionImageToMesh( )
458 if( this->m_Image.IsNull( ) )
462 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
465 std::string name = action->text( ).toStdString( );
468 TPluginFilter::Pointer filter =
469 this->m_Plugins.CreateProcessObject( name );
470 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
475 QApplication::setOverrideCursor( Qt::WaitCursor );
476 this->setEnabled( false );
477 filter->SetInput( 0, this->m_Image );
478 std::string err = filter->Update( );
479 QApplication::restoreOverrideCursor( );
480 this->setEnabled( true );
485 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
486 result->DisconnectPipeline( );
487 this->m_Mesh = result;
488 err = this->_ConfigureMeshActors( );
490 QMessageBox::critical(
492 tr( "Error message" ),
497 QMessageBox::critical(
499 tr( "Error executing filter" ),
504 // -------------------------------------------------------------------------
506 _triggered_actionUndo()
508 MementoState memento = MementoState();
513 this->m_MPRObjects->SetImage(
514 memento.getMemento(this->m_state)->GetOutput()
518 QMessageBox::warning(
521 tr("No history to undo")
527 // -------------------------------------------------------------------------
529 _triggered_actionRedo()
531 MementoState memento = MementoState();
532 if (this->m_state + 1 <= m_max_state)
535 this->m_MPRObjects->SetImage(
536 memento.getMemento(this->m_state)->GetOutput()
540 QMessageBox::warning(
543 tr("No history to redo")