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
79 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
80 std::cout << *oIt << std::endl;
83 std::vector< std::string > outputs;
84 std::string err = this->m_Plugins->UpdateActiveFilter( outputs );
87 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
89 TPlugins::TImage* image = this->m_Plugins->GetImage( *oIt );
92 vtkImageData* vimage = image->GetVTK< vtkImageData >( );
95 this->m_UI->MPR->AddImage(
96 vimage, *oIt, this->m_Plugins->GetParent( *oIt )
98 this->m_UI->MPR->ShowData( *oIt );
105 TPlugins::TMesh* mesh = this->m_Plugins->GetMesh( *oIt );
108 this->m_Plugins->BlockWidget( );
109 this->m_UI->MPR->AddMesh(
110 mesh->GetVTK< vtkPolyData >( ),
112 this->m_Plugins->GetParent( *oIt )
114 this->m_UI->MPR->ShowData( *oIt );
115 this->m_Plugins->UnblockWidget( );
123 QMessageBox::critical(
125 tr( "Error executing filter" ),
126 tr( ( std::string( "Error caught: " ) + err ).c_str( ) )
134 // -------------------------------------------------------------------------
136 _AssociatePluginsToMenu( )
138 this->m_UI->MenuFilters->clear( );
140 TPlugins::TStringContainer categories;
141 this->m_Plugins.GetLoadedCategories( categories );
142 for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
144 QMenu* category = this->m_UI->MenuFilters->addMenu( cIt->c_str( ) );
145 const TPlugins::TStringContainer& filters =
146 this->m_Plugins.GetLoadedFilters( *cIt );
147 for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
149 QAction* filter = category->addAction( fIt->c_str( ) );
151 filter, SIGNAL( triggered( ) ),
152 this, SLOT( _execPlugin( ) )
160 // -------------------------------------------------------------------------
161 #define ImageMPR_ReadImage( F ) \
162 this->m_UI->MPR->DeleteAllData( ); \
163 this->m_Plugins.ClearDataObjects( ); \
166 std::string name = this->m_Plugins.Read##F( "" ); \
169 TImage* image = this->m_Plugins.GetData< TImage >( name ); \
170 this->m_UI->MPR->AddData( image, name, "" ); \
171 this->m_UI->MPR->ShowData( name ); \
173 catch( std::exception& err ) \
175 QMessageBox::critical( \
177 QMessageBox::tr( "Error reading image." ), \
178 QMessageBox::tr( err.what( ) ) \
182 void ImageMPR::_aOpenImage( ) { ImageMPR_ReadImage( Image ) }
183 void ImageMPR::_aOpenDICOMSeries( ) { ImageMPR_ReadImage( DicomSeries ) }
185 // -------------------------------------------------------------------------
187 _aOpenSegmentation( )
190 if( this->m_ImageLoaded != "" )
191 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
195 // -------------------------------------------------------------------------
201 // -------------------------------------------------------------------------
206 std::string data_name = this->m_UI->MPR->GetSelectedData( );
207 this->m_Plugins->WriteImage( data_name );
211 // -------------------------------------------------------------------------
213 _aSaveSegmentation( )
217 // -------------------------------------------------------------------------
223 // -------------------------------------------------------------------------
229 // -------------------------------------------------------------------------
235 // -------------------------------------------------------------------------
239 this->m_Plugins.DialogLoadPlugins( );
240 this->_AssociatePluginsToMenu( );
243 // -------------------------------------------------------------------------
249 // -------------------------------------------------------------------------
254 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
257 std::string filter_name = action->text( ).toStdString( );
260 if( !( this->m_Plugins.ActivateFilter( filter_name ) ) )
264 TPlugins::TStringContainer inputs;
265 this->m_Plugins.GetActiveFilterInputsNames( inputs );
268 if( inputs.size( ) > 1 )
272 else if( inputs.size( ) == 1 )
274 this->m_ActiveFilterMainInput = this->m_UI->MPR->GetSelectedData( );
275 this->m_Plugins.ConnectInputInActiveFilter(
276 this->m_ActiveFilterMainInput, *( inputs.begin( ) )
281 // Configure paramereters
282 auto dlg_res = this->m_Plugins.ConfigureActiveFilter( );
283 if( dlg_res == TPlugins::TProcessObject::DialogResult_Cancel )
285 // Just deactivate filter, since it was canceled
286 this->m_Plugins.DeactivateFilter( );
289 else if( dlg_res == TPlugins::TProcessObject::DialogResult_NoModal )
291 // Execute automatic filter and associate outputs
292 this->UpdateActualFilter( );
293 this->m_Plugins.DeactivateFilter( );
298 // -------------------------------------------------------------------------
301 _CursorCommand( double* pos, int axis, void* data )
303 Self* app = reinterpret_cast< Self* >( data );
306 if( !( app->m_Flooding ) )
309 cpPlugins::Interface::ProcessObject::Pointer filter =
310 app->m_UI->MPR->CreateFilter(
311 "cpPlugins::BasicFilters::FloodFillImageFilter"
313 if( filter.IsNull( ) )
316 cpPlugins::Interface::Parameters* params = filter->GetParameters( );
317 params->SetPoint( "Seed", 3, pos );
318 params->SetReal( "Window", app->m_UI->MPR->GetWindow( ) );
319 params->SetReal( "Level", app->m_UI->MPR->GetLevel( ) );
320 params->SetUint( "InsideValue", 1 );
321 params->SetUint( "OutsideValue", 0 );
322 filter->SetInput( "Input", app->m_UI->MPR->GetImage( app->m_ImageLoaded ) );
323 app->m_UI->MPR->Block( );
324 std::string err = filter->Update( );
325 cpPlugins::Interface::BaseMPRWindow::TImage::Pointer image = filter->GetOutput< cpPlugins::Interface::BaseMPRWindow::TImage >( "Output" );
326 filter->DisconnectOutputs( );
327 app->m_UI->MPR->AddImage( "Segmentation", image );
328 app->m_UI->MPR->Unblock( );
331 << "CursorCommand ==> "
336 << data << std::endl;
341 #include "MementoState.h"
343 #include <vtkProperty.h>
344 #include <vtkRenderWindow.h>
345 #include <vtkMetaImageReader.h>
347 #include <QFileDialog>
348 #include <QMessageBox>
351 # define PLUGIN_PREFIX ""
352 # define PLUGIN_EXT "dll"
353 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
355 # define PLUGIN_PREFIX "lib"
356 # define PLUGIN_EXT "so"
357 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
360 // -------------------------------------------------------------------------
361 ImageMPR::ImageMPR( QWidget* parent )
362 : QMainWindow( parent ),
363 m_UI( new Ui::ImageMPR ),
364 m_ImageReaderClass( "" ),
365 m_ImageWriterClass( "" ),
366 m_MeshReaderClass( "" ),
367 m_MeshWriterClass( "" ),
368 m_MeshCutterClass( "" ),
373 this->m_UI->setupUi( this );
375 // Create and associate renderers
376 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
377 this->m_MPRObjects->SetRenderWindows(
378 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
379 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
380 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
381 this->m_UI->m_3DVTK->GetRenderWindow( )
386 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
387 this, SLOT( _triggered_actionOpenPlugins( ) )
390 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
391 this, SLOT( _triggered_actionOpenInputImage( ) )
394 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
395 this, SLOT( _triggered_actionOpenSegmentation( ) )
398 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
399 this, SLOT( _triggered_actionOpenInputPolyData( ) )
402 this->m_UI->actionUndo, SIGNAL(triggered()),
403 this, SLOT(_triggered_actionUndo())
406 this->m_UI->actionRedo, SIGNAL(triggered()),
407 this, SLOT(_triggered_actionRedo())
410 // Start: load all disponible plugins
412 std::string( PLUGIN_PREFIX ) +
413 std::string( "cpPluginsIO." ) +
414 std::string( PLUGIN_EXT )
417 std::string( PLUGIN_PREFIX ) +
418 std::string( "cpPluginsBasicFilters." ) +
419 std::string( PLUGIN_EXT )
423 // -------------------------------------------------------------------------
427 // Close all connections
428 this->m_Plugins.UnloadAll( );
434 // -------------------------------------------------------------------------
436 _LoadPlugins( const std::string& filename )
438 QApplication::setOverrideCursor( Qt::WaitCursor );
439 this->setEnabled( false );
441 this->m_ImageReaderClass = "";
442 this->m_ImageWriterClass = "";
443 this->m_MeshReaderClass = "";
444 this->m_MeshWriterClass = "";
445 this->m_MeshCutterClass = "";
446 this->m_UI->MenuImageToImage->clear( );
447 this->m_UI->MenuImageToMesh->clear( );
449 if( !( this->m_Plugins.Load( filename ) ) )
452 typedef TPluginsInterface::TClasses _TClasses;
453 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
454 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
456 TPluginFilter::Pointer o =
457 this->m_Plugins.CreateProcessObject( cIt->first );
458 std::string name = o->GetClassName( );
459 std::string category = o->GetClassCategory( );
460 if( category == "ImageReader" )
461 this->m_ImageReaderClass = name;
462 else if( category == "ImageWriter" )
463 this->m_ImageWriterClass = name;
464 else if( category == "MeshReader" )
465 this->m_MeshReaderClass = name;
466 else if( category == "MeshWriter" )
467 this->m_MeshWriterClass = name;
468 else if( category == "MeshToMeshFilter" )
470 if( name.find_last_of( "Cutter" ) != std::string::npos )
471 this->m_MeshCutterClass = name;
473 else if( category == "ImageToImageFilter" )
476 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
478 action, SIGNAL( triggered( ) ),
479 this, SLOT( _triggered_actionImageToImage( ) )
482 else if( category == "ImageToMeshFilter" )
485 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
487 action, SIGNAL( triggered( ) ),
488 this, SLOT( _triggered_actionImageToMesh( ) )
494 QApplication::restoreOverrideCursor( );
495 this->setEnabled( true );
500 // -------------------------------------------------------------------------
501 std::string ImageMPR::
502 _LoadImage( TPluginImage::Pointer& image )
504 std::string ret = "";
507 // Get a reader from loaded plugins
508 TPluginFilter::Pointer reader =
509 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
510 if( reader.IsNotNull( ) )
512 if( reader->ExecConfigurationDialog( this ) )
515 QApplication::setOverrideCursor( Qt::WaitCursor );
516 this->setEnabled( false );
518 // Execute and get error message, if any
519 ret = reader->Update( );
521 // Assign fresh image, if any
524 image = reader->GetOutput< TPluginImage >( 0 );
525 reader->DisconnectOutputs( );
529 // Unblock application
530 QApplication::restoreOverrideCursor( );
531 this->setEnabled( true );
536 ret = "No suitable reader object found in loaded plugins.";
541 // -------------------------------------------------------------------------
542 std::string ImageMPR::
543 _ConfigureMeshActors( )
545 if( this->m_Mesh.IsNull( ) )
546 return( "Valid mesh not found." );
548 this->m_Mesh->CreateVTKActor( );
549 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
550 if( vtk_actor != NULL )
552 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
553 this->m_MPRObjects->Render( 4 );
557 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
559 std::string err = "";
560 for( unsigned int i = 0; i < 3; ++i )
562 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
563 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
564 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
565 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
566 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
567 std::string lerr = this->m_Cutters[ i ]->Update( );
570 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
571 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
572 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
574 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
576 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
578 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
584 this->m_MPRObjects->RenderAll( );
588 // -------------------------------------------------------------------------
590 _triggered_actionOpenPlugins( )
592 // Show dialog and check if it was accepted
593 QFileDialog dialog( this );
594 dialog.setFileMode( QFileDialog::ExistingFile );
595 dialog.setDirectory( "." );
596 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
597 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
598 if( !( dialog.exec( ) ) )
601 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
602 if( !( _LoadPlugins( fname ) ) )
603 QMessageBox::critical(
605 tr( "Ignoring plugin" ),
610 // -------------------------------------------------------------------------
612 _triggered_actionOpenInputImage( )
615 std::string err = this->_LoadImage( this->m_Image );
618 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
621 this->m_MPRObjects->SetImage( vtk_id );
622 this->m_MPRObjects->ActivateInteractors( );
623 this->m_MPRObjects->ResetCameras( );
624 this->m_MPRObjects->RenderAll( );
626 MementoState(m_state, this->m_Image);
630 QMessageBox::critical(
632 tr( "Error message" ),
633 tr( "Read image does not have a valid VTK converter." )
637 QMessageBox::critical(
639 tr( "Error reading single image" ),
644 // -------------------------------------------------------------------------
646 _triggered_actionOpenSegmentation( )
648 if( this->m_Image.IsNull( ) )
650 QMessageBox::critical(
652 tr( "Error message" ),
653 tr( "Before reading a segmentation, first load a raw image." )
660 std::string err = this->_LoadImage( this->m_Segmentation );
663 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
666 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
667 this->m_MPRObjects->RenderAll( );
670 QMessageBox::critical(
672 tr( "Error message" ),
673 tr( "Read image does not have a valid VTK converter." )
677 QMessageBox::critical(
679 tr( "Error reading single image" ),
684 // -------------------------------------------------------------------------
686 _triggered_actionOpenInputPolyData( )
690 // Get a reader from plugins
691 TPluginFilter::Pointer reader =
692 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
694 if( reader.IsNotNull( ) )
697 if( reader->ExecConfigurationDialog( this ) )
699 // Execute and get error message, if any
700 QApplication::setOverrideCursor( Qt::WaitCursor );
701 this->setEnabled( false );
702 std::string err = reader->Update( );
703 QApplication::restoreOverrideCursor( );
704 this->setEnabled( true );
706 // Assign fresh mesh, if any
709 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
710 reader->DisconnectOutputs( );
711 err = this->_ConfigureMeshActors( );
713 QMessageBox::critical(
715 tr( "Error message" ),
720 QMessageBox::critical(
722 tr( "Error reading mesh" ),
729 QMessageBox::critical(
731 tr( "Error reading single mesh" ),
732 tr( "No suitable mesh reader found in loaded plugins." )
736 // -------------------------------------------------------------------------
738 _triggered_actionImageToImage( )
740 if( this->m_Image.IsNull( ) )
744 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
747 std::string name = action->text( ).toStdString( );
750 TPluginFilter::Pointer filter =
751 this->m_Plugins.CreateProcessObject( name );
752 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
757 QApplication::setOverrideCursor( Qt::WaitCursor );
758 this->setEnabled( false );
759 filter->SetInput( 0, this->m_Image );
760 std::string err = filter->Update( );
761 QApplication::restoreOverrideCursor( );
762 this->setEnabled( true );
767 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
768 result->DisconnectPipeline( );
769 this->m_Image = result;
770 if( this->m_Image.IsNotNull( ) )
771 this->m_MPRObjects->SetImage(
772 this->m_Image->GetVTK< vtkImageData >( )
777 MementoState(this->m_state, this->m_Image);
779 if (this->m_state > this->m_max_state)
781 this->m_max_state = this->m_state;
785 QMessageBox::critical(
787 tr( "Error executing filter" ),
792 // -------------------------------------------------------------------------
794 _triggered_actionImageToMesh( )
796 if( this->m_Image.IsNull( ) )
800 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
803 std::string name = action->text( ).toStdString( );
806 TPluginFilter::Pointer filter =
807 this->m_Plugins.CreateProcessObject( name );
808 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
813 QApplication::setOverrideCursor( Qt::WaitCursor );
814 this->setEnabled( false );
815 filter->SetInput( 0, this->m_Image );
816 std::string err = filter->Update( );
817 QApplication::restoreOverrideCursor( );
818 this->setEnabled( true );
823 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
824 result->DisconnectPipeline( );
825 this->m_Mesh = result;
826 err = this->_ConfigureMeshActors( );
828 QMessageBox::critical(
830 tr( "Error message" ),
835 QMessageBox::critical(
837 tr( "Error executing filter" ),
842 // -------------------------------------------------------------------------
844 _triggered_actionUndo()
846 MementoState memento = MementoState();
851 this->m_MPRObjects->SetImage(
852 memento.getMemento(this->m_state)->GetOutput()
856 QMessageBox::warning(
859 tr("No history to undo")
865 // -------------------------------------------------------------------------
867 _triggered_actionRedo()
869 MementoState memento = MementoState();
870 if (this->m_state + 1 <= m_max_state)
873 this->m_MPRObjects->SetImage(
874 memento.getMemento(this->m_state)->GetOutput()
878 QMessageBox::warning(
881 tr("No history to redo")