2 #include "ui_ImageMPR.h"
4 // -------------------------------------------------------------------------
5 #define ImageMPR_ConnectAction( ACTION ) \
7 this->m_UI->a##ACTION, SIGNAL( triggered( ) ), \
8 this, SLOT( _a##ACTION( ) ) \
11 // -------------------------------------------------------------------------
13 ImageMPR( QWidget* parent )
14 : QMainWindow( parent ),
15 m_UI( new Ui::ImageMPR ),
16 m_ImageLoaded( false )
18 this->m_UI->setupUi( this );
21 ImageMPR_ConnectAction( OpenImage );
22 ImageMPR_ConnectAction( OpenSegmentation );
23 ImageMPR_ConnectAction( OpenPolyData );
24 ImageMPR_ConnectAction( SaveImage );
25 ImageMPR_ConnectAction( SaveSegmentation );
26 ImageMPR_ConnectAction( SavePolyData );
27 ImageMPR_ConnectAction( Undo );
28 ImageMPR_ConnectAction( Redo );
29 ImageMPR_ConnectAction( LoadPlugins );
30 ImageMPR_ConnectAction( ShowPlugins );
32 // Try to load default plugins
33 this->m_UI->MPR->LoadPlugins( );
34 this->m_UI->MPR->AssociatePluginsToMenu(
35 this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) )
39 // -------------------------------------------------------------------------
46 // -------------------------------------------------------------------------
50 if( this->m_ImageLoaded )
51 this->m_UI->MPR->ClearAll( );
52 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
55 // -------------------------------------------------------------------------
59 if( this->m_ImageLoaded )
60 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
63 // -------------------------------------------------------------------------
69 // -------------------------------------------------------------------------
75 // -------------------------------------------------------------------------
81 // -------------------------------------------------------------------------
87 // -------------------------------------------------------------------------
93 // -------------------------------------------------------------------------
99 // -------------------------------------------------------------------------
103 this->m_UI->MPR->DialogLoadPlugins( );
104 this->m_UI->MPR->AssociatePluginsToMenu(
105 this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) )
109 // -------------------------------------------------------------------------
115 // -------------------------------------------------------------------------
120 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
123 std::string name = action->text( ).toStdString( );
124 this->m_UI->MPR->ExecuteFilter( name, 0 );
128 TPluginFilter::Pointer filter =
129 this->m_Plugins.CreateProcessObject( name );
130 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
135 QApplication::setOverrideCursor( Qt::WaitCursor );
136 this->setEnabled( false );
137 filter->SetInput( 0, this->m_Image );
138 std::string err = filter->Update( );
139 QApplication::restoreOverrideCursor( );
140 this->setEnabled( true );
145 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
146 result->DisconnectPipeline( );
147 this->m_Image = result;
148 if( this->m_Image.IsNotNull( ) )
149 this->m_MPRObjects->SetImage(
150 this->m_Image->GetVTK< vtkImageData >( )
152 MementoState(this->m_state, this->m_Image);
154 if (this->m_state > this->m_max_state)
156 this->m_max_state = this->m_state;
160 QMessageBox::critical(
162 tr( "Error executing filter" ),
169 #include "MementoState.h"
171 #include <vtkProperty.h>
172 #include <vtkRenderWindow.h>
173 #include <vtkMetaImageReader.h>
175 #include <QFileDialog>
176 #include <QMessageBox>
179 # define PLUGIN_PREFIX ""
180 # define PLUGIN_EXT "dll"
181 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
183 # define PLUGIN_PREFIX "lib"
184 # define PLUGIN_EXT "so"
185 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
188 // -------------------------------------------------------------------------
189 ImageMPR::ImageMPR( QWidget* parent )
190 : QMainWindow( parent ),
191 m_UI( new Ui::ImageMPR ),
192 m_ImageReaderClass( "" ),
193 m_ImageWriterClass( "" ),
194 m_MeshReaderClass( "" ),
195 m_MeshWriterClass( "" ),
196 m_MeshCutterClass( "" ),
201 this->m_UI->setupUi( this );
203 // Create and associate renderers
204 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
205 this->m_MPRObjects->SetRenderWindows(
206 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
207 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
208 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
209 this->m_UI->m_3DVTK->GetRenderWindow( )
214 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
215 this, SLOT( _triggered_actionOpenPlugins( ) )
218 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
219 this, SLOT( _triggered_actionOpenInputImage( ) )
222 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
223 this, SLOT( _triggered_actionOpenSegmentation( ) )
226 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
227 this, SLOT( _triggered_actionOpenInputPolyData( ) )
230 this->m_UI->actionUndo, SIGNAL(triggered()),
231 this, SLOT(_triggered_actionUndo())
234 this->m_UI->actionRedo, SIGNAL(triggered()),
235 this, SLOT(_triggered_actionRedo())
238 // Start: load all disponible plugins
240 std::string( PLUGIN_PREFIX ) +
241 std::string( "cpPluginsIO." ) +
242 std::string( PLUGIN_EXT )
245 std::string( PLUGIN_PREFIX ) +
246 std::string( "cpPluginsBasicFilters." ) +
247 std::string( PLUGIN_EXT )
251 // -------------------------------------------------------------------------
255 // Close all connections
256 this->m_Plugins.UnloadAll( );
262 // -------------------------------------------------------------------------
264 _LoadPlugins( const std::string& filename )
266 QApplication::setOverrideCursor( Qt::WaitCursor );
267 this->setEnabled( false );
269 this->m_ImageReaderClass = "";
270 this->m_ImageWriterClass = "";
271 this->m_MeshReaderClass = "";
272 this->m_MeshWriterClass = "";
273 this->m_MeshCutterClass = "";
274 this->m_UI->MenuImageToImage->clear( );
275 this->m_UI->MenuImageToMesh->clear( );
277 if( !( this->m_Plugins.Load( filename ) ) )
280 typedef TPluginsInterface::TClasses _TClasses;
281 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
282 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
284 TPluginFilter::Pointer o =
285 this->m_Plugins.CreateProcessObject( cIt->first );
286 std::string name = o->GetClassName( );
287 std::string category = o->GetClassCategory( );
288 if( category == "ImageReader" )
289 this->m_ImageReaderClass = name;
290 else if( category == "ImageWriter" )
291 this->m_ImageWriterClass = name;
292 else if( category == "MeshReader" )
293 this->m_MeshReaderClass = name;
294 else if( category == "MeshWriter" )
295 this->m_MeshWriterClass = name;
296 else if( category == "MeshToMeshFilter" )
298 if( name.find_last_of( "Cutter" ) != std::string::npos )
299 this->m_MeshCutterClass = name;
301 else if( category == "ImageToImageFilter" )
304 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
306 action, SIGNAL( triggered( ) ),
307 this, SLOT( _triggered_actionImageToImage( ) )
310 else if( category == "ImageToMeshFilter" )
313 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
315 action, SIGNAL( triggered( ) ),
316 this, SLOT( _triggered_actionImageToMesh( ) )
322 QApplication::restoreOverrideCursor( );
323 this->setEnabled( true );
328 // -------------------------------------------------------------------------
329 std::string ImageMPR::
330 _LoadImage( TPluginImage::Pointer& image )
332 std::string ret = "";
335 // Get a reader from loaded plugins
336 TPluginFilter::Pointer reader =
337 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
338 if( reader.IsNotNull( ) )
340 if( reader->ExecConfigurationDialog( this ) )
343 QApplication::setOverrideCursor( Qt::WaitCursor );
344 this->setEnabled( false );
346 // Execute and get error message, if any
347 ret = reader->Update( );
349 // Assign fresh image, if any
352 image = reader->GetOutput< TPluginImage >( 0 );
353 reader->DisconnectOutputs( );
357 // Unblock application
358 QApplication::restoreOverrideCursor( );
359 this->setEnabled( true );
364 ret = "No suitable reader object found in loaded plugins.";
369 // -------------------------------------------------------------------------
370 std::string ImageMPR::
371 _ConfigureMeshActors( )
373 if( this->m_Mesh.IsNull( ) )
374 return( "Valid mesh not found." );
376 this->m_Mesh->CreateVTKActor( );
377 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
378 if( vtk_actor != NULL )
380 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
381 this->m_MPRObjects->Render( 4 );
385 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
387 std::string err = "";
388 for( unsigned int i = 0; i < 3; ++i )
390 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
391 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
392 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
393 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
394 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
395 std::string lerr = this->m_Cutters[ i ]->Update( );
398 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
399 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
400 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
402 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
404 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
406 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
412 this->m_MPRObjects->RenderAll( );
416 // -------------------------------------------------------------------------
418 _triggered_actionOpenPlugins( )
420 // Show dialog and check if it was accepted
421 QFileDialog dialog( this );
422 dialog.setFileMode( QFileDialog::ExistingFile );
423 dialog.setDirectory( "." );
424 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
425 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
426 if( !( dialog.exec( ) ) )
429 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
430 if( !( _LoadPlugins( fname ) ) )
431 QMessageBox::critical(
433 tr( "Ignoring plugin" ),
438 // -------------------------------------------------------------------------
440 _triggered_actionOpenInputImage( )
443 std::string err = this->_LoadImage( this->m_Image );
446 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
449 this->m_MPRObjects->SetImage( vtk_id );
450 this->m_MPRObjects->ActivateInteractors( );
451 this->m_MPRObjects->ResetCameras( );
452 this->m_MPRObjects->RenderAll( );
454 MementoState(m_state, this->m_Image);
458 QMessageBox::critical(
460 tr( "Error message" ),
461 tr( "Read image does not have a valid VTK converter." )
465 QMessageBox::critical(
467 tr( "Error reading single image" ),
472 // -------------------------------------------------------------------------
474 _triggered_actionOpenSegmentation( )
476 if( this->m_Image.IsNull( ) )
478 QMessageBox::critical(
480 tr( "Error message" ),
481 tr( "Before reading a segmentation, first load a raw image." )
488 std::string err = this->_LoadImage( this->m_Segmentation );
491 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
494 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
495 this->m_MPRObjects->RenderAll( );
498 QMessageBox::critical(
500 tr( "Error message" ),
501 tr( "Read image does not have a valid VTK converter." )
505 QMessageBox::critical(
507 tr( "Error reading single image" ),
512 // -------------------------------------------------------------------------
514 _triggered_actionOpenInputPolyData( )
518 // Get a reader from plugins
519 TPluginFilter::Pointer reader =
520 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
522 if( reader.IsNotNull( ) )
525 if( reader->ExecConfigurationDialog( this ) )
527 // Execute and get error message, if any
528 QApplication::setOverrideCursor( Qt::WaitCursor );
529 this->setEnabled( false );
530 std::string err = reader->Update( );
531 QApplication::restoreOverrideCursor( );
532 this->setEnabled( true );
534 // Assign fresh mesh, if any
537 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
538 reader->DisconnectOutputs( );
539 err = this->_ConfigureMeshActors( );
541 QMessageBox::critical(
543 tr( "Error message" ),
548 QMessageBox::critical(
550 tr( "Error reading mesh" ),
557 QMessageBox::critical(
559 tr( "Error reading single mesh" ),
560 tr( "No suitable mesh reader found in loaded plugins." )
564 // -------------------------------------------------------------------------
566 _triggered_actionImageToImage( )
568 if( this->m_Image.IsNull( ) )
572 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
575 std::string name = action->text( ).toStdString( );
578 TPluginFilter::Pointer filter =
579 this->m_Plugins.CreateProcessObject( name );
580 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
585 QApplication::setOverrideCursor( Qt::WaitCursor );
586 this->setEnabled( false );
587 filter->SetInput( 0, this->m_Image );
588 std::string err = filter->Update( );
589 QApplication::restoreOverrideCursor( );
590 this->setEnabled( true );
595 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
596 result->DisconnectPipeline( );
597 this->m_Image = result;
598 if( this->m_Image.IsNotNull( ) )
599 this->m_MPRObjects->SetImage(
600 this->m_Image->GetVTK< vtkImageData >( )
605 MementoState(this->m_state, this->m_Image);
607 if (this->m_state > this->m_max_state)
609 this->m_max_state = this->m_state;
613 QMessageBox::critical(
615 tr( "Error executing filter" ),
620 // -------------------------------------------------------------------------
622 _triggered_actionImageToMesh( )
624 if( this->m_Image.IsNull( ) )
628 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
631 std::string name = action->text( ).toStdString( );
634 TPluginFilter::Pointer filter =
635 this->m_Plugins.CreateProcessObject( name );
636 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
641 QApplication::setOverrideCursor( Qt::WaitCursor );
642 this->setEnabled( false );
643 filter->SetInput( 0, this->m_Image );
644 std::string err = filter->Update( );
645 QApplication::restoreOverrideCursor( );
646 this->setEnabled( true );
651 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
652 result->DisconnectPipeline( );
653 this->m_Mesh = result;
654 err = this->_ConfigureMeshActors( );
656 QMessageBox::critical(
658 tr( "Error message" ),
663 QMessageBox::critical(
665 tr( "Error executing filter" ),
670 // -------------------------------------------------------------------------
672 _triggered_actionUndo()
674 MementoState memento = MementoState();
679 this->m_MPRObjects->SetImage(
680 memento.getMemento(this->m_state)->GetOutput()
684 QMessageBox::warning(
687 tr("No history to undo")
693 // -------------------------------------------------------------------------
695 _triggered_actionRedo()
697 MementoState memento = MementoState();
698 if (this->m_state + 1 <= m_max_state)
701 this->m_MPRObjects->SetImage(
702 memento.getMemento(this->m_state)->GetOutput()
706 QMessageBox::warning(
709 tr("No history to redo")