2 #include "ui_ImageMPR.h"
7 // -------------------------------------------------------------------------
8 #define ImageMPR_ConnectAction( ACTION ) \
10 this->m_UI->a##ACTION, SIGNAL( triggered( ) ), \
11 this, SLOT( _a##ACTION( ) ) \
14 // -------------------------------------------------------------------------
16 ImageMPR( QWidget* parent )
17 : QMainWindow( parent ),
18 m_UI( new Ui::ImageMPR )
20 this->m_UI->setupUi( this );
21 this->m_Plugins.SetWidget( this );
22 this->m_Plugins.SetApplication( this );
25 ImageMPR_ConnectAction( OpenImage );
26 ImageMPR_ConnectAction( OpenDICOMSeries );
27 ImageMPR_ConnectAction( OpenSegmentation );
28 ImageMPR_ConnectAction( OpenPolyData );
29 ImageMPR_ConnectAction( SaveImage );
30 ImageMPR_ConnectAction( SaveSegmentation );
31 ImageMPR_ConnectAction( SavePolyData );
32 ImageMPR_ConnectAction( Undo );
33 ImageMPR_ConnectAction( Redo );
34 ImageMPR_ConnectAction( LoadPlugins );
35 ImageMPR_ConnectAction( ShowPlugins );
37 // Associate model with view
38 for( unsigned int i = 0; i < 4; ++i )
39 this->m_Plugins.AddInteractor( this->m_UI->MPR->GetInteractor( i ) );
41 // Try to load default plugins
42 QStringList args = QApplication::arguments( );
43 QFileInfo info( args.at( 0 ) );
44 this->m_Plugins.LoadPluginsPath(
45 info.absolutePath( ).toStdString( ), true
48 // Put loaded plugins into menu
49 this->_AssociatePluginsToMenu( );
52 // -------------------------------------------------------------------------
59 // -------------------------------------------------------------------------
63 if( !( this->m_Plugins.HasActiveFilter( ) ) )
67 TPlugins::TStringContainer outputs;
70 this->m_Plugins.UpdateActiveFilter(
71 outputs, this->m_ActiveFilterMainInput
78 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
80 std::string parent = this->m_Plugins.GetParent( *oIt );
81 TDataObject* dobj = this->m_Plugins.GetData< TDataObject >( *oIt );
82 this->m_UI->MPR->AddData( dobj, *oIt, parent );
83 this->m_UI->MPR->ShowData( *oIt );
88 std::vector< std::string > outputs;
89 std::string err = this->m_Plugins->UpdateActiveFilter( outputs );
92 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
94 TPlugins::TImage* image = this->m_Plugins->GetImage( *oIt );
97 vtkImageData* vimage = image->GetVTK< vtkImageData >( );
100 this->m_UI->MPR->AddImage(
101 vimage, *oIt, this->m_Plugins->GetParent( *oIt )
103 this->m_UI->MPR->ShowData( *oIt );
110 TPlugins::TMesh* mesh = this->m_Plugins->GetMesh( *oIt );
113 this->m_Plugins->BlockWidget( );
114 this->m_UI->MPR->AddMesh(
115 mesh->GetVTK< vtkPolyData >( ),
117 this->m_Plugins->GetParent( *oIt )
119 this->m_UI->MPR->ShowData( *oIt );
120 this->m_Plugins->UnblockWidget( );
128 QMessageBox::critical(
130 tr( "Error executing filter" ),
131 tr( ( std::string( "Error caught: " ) + err ).c_str( ) )
139 // -------------------------------------------------------------------------
141 _AssociatePluginsToMenu( )
143 this->m_UI->MenuFilters->clear( );
145 TPlugins::TStringContainer categories;
146 this->m_Plugins.GetLoadedCategories( categories );
147 for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
149 QMenu* category = this->m_UI->MenuFilters->addMenu( cIt->c_str( ) );
150 const TPlugins::TStringContainer& filters =
151 this->m_Plugins.GetLoadedFilters( *cIt );
152 for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
154 QAction* filter = category->addAction( fIt->c_str( ) );
156 filter, SIGNAL( triggered( ) ),
157 this, SLOT( _execPlugin( ) )
165 // -------------------------------------------------------------------------
166 #define ImageMPR_ReadImage( F ) \
167 this->m_UI->MPR->DeleteAllData( ); \
168 this->m_Plugins.ClearDataObjects( ); \
171 std::string name = this->m_Plugins.Read##F( "" ); \
174 TImage* image = this->m_Plugins.GetData< TImage >( name ); \
175 this->m_UI->MPR->AddData( image, name, "" ); \
176 this->m_UI->MPR->SetMainImage( name ); \
177 this->m_UI->MPR->ShowData( name ); \
179 catch( std::exception& err ) \
181 QMessageBox::critical( \
183 QMessageBox::tr( "Error reading image." ), \
184 QMessageBox::tr( err.what( ) ) \
188 void ImageMPR::_aOpenImage( ) { ImageMPR_ReadImage( Image ) }
189 void ImageMPR::_aOpenDICOMSeries( ) { ImageMPR_ReadImage( DicomSeries ) }
191 // -------------------------------------------------------------------------
193 _aOpenSegmentation( )
196 if( this->m_ImageLoaded != "" )
197 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
201 // -------------------------------------------------------------------------
207 // -------------------------------------------------------------------------
212 std::string data_name = this->m_UI->MPR->GetSelectedData( );
213 this->m_Plugins->WriteImage( data_name );
217 // -------------------------------------------------------------------------
219 _aSaveSegmentation( )
223 // -------------------------------------------------------------------------
229 // -------------------------------------------------------------------------
235 // -------------------------------------------------------------------------
241 // -------------------------------------------------------------------------
245 this->m_Plugins.DialogLoadPlugins( );
246 this->_AssociatePluginsToMenu( );
249 // -------------------------------------------------------------------------
255 // -------------------------------------------------------------------------
260 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
263 std::string filter_name = action->text( ).toStdString( );
266 if( !( this->m_Plugins.ActivateFilter( filter_name ) ) )
270 TPlugins::TStringContainer inputs;
271 this->m_Plugins.GetActiveFilterInputsNames( inputs );
274 if( inputs.size( ) > 1 )
278 else if( inputs.size( ) == 1 )
280 this->m_ActiveFilterMainInput = this->m_UI->MPR->GetSelectedData( );
281 this->m_Plugins.ConnectInputInActiveFilter(
282 this->m_ActiveFilterMainInput, *( inputs.begin( ) )
287 // Configure paramereters
288 auto dlg_res = this->m_Plugins.ConfigureActiveFilter( );
289 if( dlg_res == TPlugins::TProcessObject::DialogResult_Cancel )
291 // Just deactivate filter, since it was canceled
292 this->m_Plugins.DeactivateFilter( );
295 else if( dlg_res == TPlugins::TProcessObject::DialogResult_NoModal )
297 // Execute automatic filter and associate outputs
298 this->UpdateActualFilter( );
299 this->m_Plugins.DeactivateFilter( );
304 // -------------------------------------------------------------------------
307 _CursorCommand( double* pos, int axis, void* data )
309 Self* app = reinterpret_cast< Self* >( data );
312 if( !( app->m_Flooding ) )
315 cpPlugins::Interface::ProcessObject::Pointer filter =
316 app->m_UI->MPR->CreateFilter(
317 "cpPlugins::BasicFilters::FloodFillImageFilter"
319 if( filter.IsNull( ) )
322 cpPlugins::Interface::Parameters* params = filter->GetParameters( );
323 params->SetPoint( "Seed", 3, pos );
324 params->SetReal( "Window", app->m_UI->MPR->GetWindow( ) );
325 params->SetReal( "Level", app->m_UI->MPR->GetLevel( ) );
326 params->SetUint( "InsideValue", 1 );
327 params->SetUint( "OutsideValue", 0 );
328 filter->SetInput( "Input", app->m_UI->MPR->GetImage( app->m_ImageLoaded ) );
329 app->m_UI->MPR->Block( );
330 std::string err = filter->Update( );
331 cpPlugins::Interface::BaseMPRWindow::TImage::Pointer image = filter->GetOutput< cpPlugins::Interface::BaseMPRWindow::TImage >( "Output" );
332 filter->DisconnectOutputs( );
333 app->m_UI->MPR->AddImage( "Segmentation", image );
334 app->m_UI->MPR->Unblock( );
337 << "CursorCommand ==> "
342 << data << std::endl;
347 #include "MementoState.h"
349 #include <vtkProperty.h>
350 #include <vtkRenderWindow.h>
351 #include <vtkMetaImageReader.h>
353 #include <QFileDialog>
354 #include <QMessageBox>
357 # define PLUGIN_PREFIX ""
358 # define PLUGIN_EXT "dll"
359 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
361 # define PLUGIN_PREFIX "lib"
362 # define PLUGIN_EXT "so"
363 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
366 // -------------------------------------------------------------------------
367 ImageMPR::ImageMPR( QWidget* parent )
368 : QMainWindow( parent ),
369 m_UI( new Ui::ImageMPR ),
370 m_ImageReaderClass( "" ),
371 m_ImageWriterClass( "" ),
372 m_MeshReaderClass( "" ),
373 m_MeshWriterClass( "" ),
374 m_MeshCutterClass( "" ),
379 this->m_UI->setupUi( this );
381 // Create and associate renderers
382 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
383 this->m_MPRObjects->SetRenderWindows(
384 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
385 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
386 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
387 this->m_UI->m_3DVTK->GetRenderWindow( )
392 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
393 this, SLOT( _triggered_actionOpenPlugins( ) )
396 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
397 this, SLOT( _triggered_actionOpenInputImage( ) )
400 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
401 this, SLOT( _triggered_actionOpenSegmentation( ) )
404 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
405 this, SLOT( _triggered_actionOpenInputPolyData( ) )
408 this->m_UI->actionUndo, SIGNAL(triggered()),
409 this, SLOT(_triggered_actionUndo())
412 this->m_UI->actionRedo, SIGNAL(triggered()),
413 this, SLOT(_triggered_actionRedo())
416 // Start: load all disponible plugins
418 std::string( PLUGIN_PREFIX ) +
419 std::string( "cpPluginsIO." ) +
420 std::string( PLUGIN_EXT )
423 std::string( PLUGIN_PREFIX ) +
424 std::string( "cpPluginsBasicFilters." ) +
425 std::string( PLUGIN_EXT )
429 // -------------------------------------------------------------------------
433 // Close all connections
434 this->m_Plugins.UnloadAll( );
440 // -------------------------------------------------------------------------
442 _LoadPlugins( const std::string& filename )
444 QApplication::setOverrideCursor( Qt::WaitCursor );
445 this->setEnabled( false );
447 this->m_ImageReaderClass = "";
448 this->m_ImageWriterClass = "";
449 this->m_MeshReaderClass = "";
450 this->m_MeshWriterClass = "";
451 this->m_MeshCutterClass = "";
452 this->m_UI->MenuImageToImage->clear( );
453 this->m_UI->MenuImageToMesh->clear( );
455 if( !( this->m_Plugins.Load( filename ) ) )
458 typedef TPluginsInterface::TClasses _TClasses;
459 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
460 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
462 TPluginFilter::Pointer o =
463 this->m_Plugins.CreateProcessObject( cIt->first );
464 std::string name = o->GetClassName( );
465 std::string category = o->GetClassCategory( );
466 if( category == "ImageReader" )
467 this->m_ImageReaderClass = name;
468 else if( category == "ImageWriter" )
469 this->m_ImageWriterClass = name;
470 else if( category == "MeshReader" )
471 this->m_MeshReaderClass = name;
472 else if( category == "MeshWriter" )
473 this->m_MeshWriterClass = name;
474 else if( category == "MeshToMeshFilter" )
476 if( name.find_last_of( "Cutter" ) != std::string::npos )
477 this->m_MeshCutterClass = name;
479 else if( category == "ImageToImageFilter" )
482 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
484 action, SIGNAL( triggered( ) ),
485 this, SLOT( _triggered_actionImageToImage( ) )
488 else if( category == "ImageToMeshFilter" )
491 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
493 action, SIGNAL( triggered( ) ),
494 this, SLOT( _triggered_actionImageToMesh( ) )
500 QApplication::restoreOverrideCursor( );
501 this->setEnabled( true );
506 // -------------------------------------------------------------------------
507 std::string ImageMPR::
508 _LoadImage( TPluginImage::Pointer& image )
510 std::string ret = "";
513 // Get a reader from loaded plugins
514 TPluginFilter::Pointer reader =
515 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
516 if( reader.IsNotNull( ) )
518 if( reader->ExecConfigurationDialog( this ) )
521 QApplication::setOverrideCursor( Qt::WaitCursor );
522 this->setEnabled( false );
524 // Execute and get error message, if any
525 ret = reader->Update( );
527 // Assign fresh image, if any
530 image = reader->GetOutput< TPluginImage >( 0 );
531 reader->DisconnectOutputs( );
535 // Unblock application
536 QApplication::restoreOverrideCursor( );
537 this->setEnabled( true );
542 ret = "No suitable reader object found in loaded plugins.";
547 // -------------------------------------------------------------------------
548 std::string ImageMPR::
549 _ConfigureMeshActors( )
551 if( this->m_Mesh.IsNull( ) )
552 return( "Valid mesh not found." );
554 this->m_Mesh->CreateVTKActor( );
555 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
556 if( vtk_actor != NULL )
558 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
559 this->m_MPRObjects->Render( 4 );
563 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
565 std::string err = "";
566 for( unsigned int i = 0; i < 3; ++i )
568 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
569 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
570 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
571 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
572 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
573 std::string lerr = this->m_Cutters[ i ]->Update( );
576 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
577 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
578 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
580 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
582 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
584 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
590 this->m_MPRObjects->RenderAll( );
594 // -------------------------------------------------------------------------
596 _triggered_actionOpenPlugins( )
598 // Show dialog and check if it was accepted
599 QFileDialog dialog( this );
600 dialog.setFileMode( QFileDialog::ExistingFile );
601 dialog.setDirectory( "." );
602 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
603 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
604 if( !( dialog.exec( ) ) )
607 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
608 if( !( _LoadPlugins( fname ) ) )
609 QMessageBox::critical(
611 tr( "Ignoring plugin" ),
616 // -------------------------------------------------------------------------
618 _triggered_actionOpenInputImage( )
621 std::string err = this->_LoadImage( this->m_Image );
624 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
627 this->m_MPRObjects->SetImage( vtk_id );
628 this->m_MPRObjects->ActivateInteractors( );
629 this->m_MPRObjects->ResetCameras( );
630 this->m_MPRObjects->RenderAll( );
632 MementoState(m_state, this->m_Image);
636 QMessageBox::critical(
638 tr( "Error message" ),
639 tr( "Read image does not have a valid VTK converter." )
643 QMessageBox::critical(
645 tr( "Error reading single image" ),
650 // -------------------------------------------------------------------------
652 _triggered_actionOpenSegmentation( )
654 if( this->m_Image.IsNull( ) )
656 QMessageBox::critical(
658 tr( "Error message" ),
659 tr( "Before reading a segmentation, first load a raw image." )
666 std::string err = this->_LoadImage( this->m_Segmentation );
669 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
672 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
673 this->m_MPRObjects->RenderAll( );
676 QMessageBox::critical(
678 tr( "Error message" ),
679 tr( "Read image does not have a valid VTK converter." )
683 QMessageBox::critical(
685 tr( "Error reading single image" ),
690 // -------------------------------------------------------------------------
692 _triggered_actionOpenInputPolyData( )
696 // Get a reader from plugins
697 TPluginFilter::Pointer reader =
698 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
700 if( reader.IsNotNull( ) )
703 if( reader->ExecConfigurationDialog( this ) )
705 // Execute and get error message, if any
706 QApplication::setOverrideCursor( Qt::WaitCursor );
707 this->setEnabled( false );
708 std::string err = reader->Update( );
709 QApplication::restoreOverrideCursor( );
710 this->setEnabled( true );
712 // Assign fresh mesh, if any
715 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
716 reader->DisconnectOutputs( );
717 err = this->_ConfigureMeshActors( );
719 QMessageBox::critical(
721 tr( "Error message" ),
726 QMessageBox::critical(
728 tr( "Error reading mesh" ),
735 QMessageBox::critical(
737 tr( "Error reading single mesh" ),
738 tr( "No suitable mesh reader found in loaded plugins." )
742 // -------------------------------------------------------------------------
744 _triggered_actionImageToImage( )
746 if( this->m_Image.IsNull( ) )
750 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
753 std::string name = action->text( ).toStdString( );
756 TPluginFilter::Pointer filter =
757 this->m_Plugins.CreateProcessObject( name );
758 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
763 QApplication::setOverrideCursor( Qt::WaitCursor );
764 this->setEnabled( false );
765 filter->SetInput( 0, this->m_Image );
766 std::string err = filter->Update( );
767 QApplication::restoreOverrideCursor( );
768 this->setEnabled( true );
773 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
774 result->DisconnectPipeline( );
775 this->m_Image = result;
776 if( this->m_Image.IsNotNull( ) )
777 this->m_MPRObjects->SetImage(
778 this->m_Image->GetVTK< vtkImageData >( )
783 MementoState(this->m_state, this->m_Image);
785 if (this->m_state > this->m_max_state)
787 this->m_max_state = this->m_state;
791 QMessageBox::critical(
793 tr( "Error executing filter" ),
798 // -------------------------------------------------------------------------
800 _triggered_actionImageToMesh( )
802 if( this->m_Image.IsNull( ) )
806 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
809 std::string name = action->text( ).toStdString( );
812 TPluginFilter::Pointer filter =
813 this->m_Plugins.CreateProcessObject( name );
814 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
819 QApplication::setOverrideCursor( Qt::WaitCursor );
820 this->setEnabled( false );
821 filter->SetInput( 0, this->m_Image );
822 std::string err = filter->Update( );
823 QApplication::restoreOverrideCursor( );
824 this->setEnabled( true );
829 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
830 result->DisconnectPipeline( );
831 this->m_Mesh = result;
832 err = this->_ConfigureMeshActors( );
834 QMessageBox::critical(
836 tr( "Error message" ),
841 QMessageBox::critical(
843 tr( "Error executing filter" ),
848 // -------------------------------------------------------------------------
850 _triggered_actionUndo()
852 MementoState memento = MementoState();
857 this->m_MPRObjects->SetImage(
858 memento.getMemento(this->m_state)->GetOutput()
862 QMessageBox::warning(
865 tr("No history to undo")
871 // -------------------------------------------------------------------------
873 _triggered_actionRedo()
875 MementoState memento = MementoState();
876 if (this->m_state + 1 <= m_max_state)
879 this->m_MPRObjects->SetImage(
880 memento.getMemento(this->m_state)->GetOutput()
884 QMessageBox::warning(
887 tr("No history to redo")