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->ShowData( name ); \
178 catch( std::exception& err ) \
180 QMessageBox::critical( \
182 QMessageBox::tr( "Error reading image." ), \
183 QMessageBox::tr( err.what( ) ) \
187 void ImageMPR::_aOpenImage( ) { ImageMPR_ReadImage( Image ) }
188 void ImageMPR::_aOpenDICOMSeries( ) { ImageMPR_ReadImage( DicomSeries ) }
190 // -------------------------------------------------------------------------
192 _aOpenSegmentation( )
195 if( this->m_ImageLoaded != "" )
196 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
200 // -------------------------------------------------------------------------
206 // -------------------------------------------------------------------------
211 std::string data_name = this->m_UI->MPR->GetSelectedData( );
212 this->m_Plugins->WriteImage( data_name );
216 // -------------------------------------------------------------------------
218 _aSaveSegmentation( )
222 // -------------------------------------------------------------------------
228 // -------------------------------------------------------------------------
234 // -------------------------------------------------------------------------
240 // -------------------------------------------------------------------------
244 this->m_Plugins.DialogLoadPlugins( );
245 this->_AssociatePluginsToMenu( );
248 // -------------------------------------------------------------------------
254 // -------------------------------------------------------------------------
259 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
262 std::string filter_name = action->text( ).toStdString( );
265 if( !( this->m_Plugins.ActivateFilter( filter_name ) ) )
269 TPlugins::TStringContainer inputs;
270 this->m_Plugins.GetActiveFilterInputsNames( inputs );
273 if( inputs.size( ) > 1 )
277 else if( inputs.size( ) == 1 )
279 this->m_ActiveFilterMainInput = this->m_UI->MPR->GetSelectedData( );
280 this->m_Plugins.ConnectInputInActiveFilter(
281 this->m_ActiveFilterMainInput, *( inputs.begin( ) )
286 // Configure paramereters
287 auto dlg_res = this->m_Plugins.ConfigureActiveFilter( );
288 if( dlg_res == TPlugins::TProcessObject::DialogResult_Cancel )
290 // Just deactivate filter, since it was canceled
291 this->m_Plugins.DeactivateFilter( );
294 else if( dlg_res == TPlugins::TProcessObject::DialogResult_NoModal )
296 // Execute automatic filter and associate outputs
297 this->UpdateActualFilter( );
298 this->m_Plugins.DeactivateFilter( );
303 // -------------------------------------------------------------------------
306 _CursorCommand( double* pos, int axis, void* data )
308 Self* app = reinterpret_cast< Self* >( data );
311 if( !( app->m_Flooding ) )
314 cpPlugins::Interface::ProcessObject::Pointer filter =
315 app->m_UI->MPR->CreateFilter(
316 "cpPlugins::BasicFilters::FloodFillImageFilter"
318 if( filter.IsNull( ) )
321 cpPlugins::Interface::Parameters* params = filter->GetParameters( );
322 params->SetPoint( "Seed", 3, pos );
323 params->SetReal( "Window", app->m_UI->MPR->GetWindow( ) );
324 params->SetReal( "Level", app->m_UI->MPR->GetLevel( ) );
325 params->SetUint( "InsideValue", 1 );
326 params->SetUint( "OutsideValue", 0 );
327 filter->SetInput( "Input", app->m_UI->MPR->GetImage( app->m_ImageLoaded ) );
328 app->m_UI->MPR->Block( );
329 std::string err = filter->Update( );
330 cpPlugins::Interface::BaseMPRWindow::TImage::Pointer image = filter->GetOutput< cpPlugins::Interface::BaseMPRWindow::TImage >( "Output" );
331 filter->DisconnectOutputs( );
332 app->m_UI->MPR->AddImage( "Segmentation", image );
333 app->m_UI->MPR->Unblock( );
336 << "CursorCommand ==> "
341 << data << std::endl;
346 #include "MementoState.h"
348 #include <vtkProperty.h>
349 #include <vtkRenderWindow.h>
350 #include <vtkMetaImageReader.h>
352 #include <QFileDialog>
353 #include <QMessageBox>
356 # define PLUGIN_PREFIX ""
357 # define PLUGIN_EXT "dll"
358 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
360 # define PLUGIN_PREFIX "lib"
361 # define PLUGIN_EXT "so"
362 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
365 // -------------------------------------------------------------------------
366 ImageMPR::ImageMPR( QWidget* parent )
367 : QMainWindow( parent ),
368 m_UI( new Ui::ImageMPR ),
369 m_ImageReaderClass( "" ),
370 m_ImageWriterClass( "" ),
371 m_MeshReaderClass( "" ),
372 m_MeshWriterClass( "" ),
373 m_MeshCutterClass( "" ),
378 this->m_UI->setupUi( this );
380 // Create and associate renderers
381 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
382 this->m_MPRObjects->SetRenderWindows(
383 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
384 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
385 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
386 this->m_UI->m_3DVTK->GetRenderWindow( )
391 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
392 this, SLOT( _triggered_actionOpenPlugins( ) )
395 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
396 this, SLOT( _triggered_actionOpenInputImage( ) )
399 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
400 this, SLOT( _triggered_actionOpenSegmentation( ) )
403 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
404 this, SLOT( _triggered_actionOpenInputPolyData( ) )
407 this->m_UI->actionUndo, SIGNAL(triggered()),
408 this, SLOT(_triggered_actionUndo())
411 this->m_UI->actionRedo, SIGNAL(triggered()),
412 this, SLOT(_triggered_actionRedo())
415 // Start: load all disponible plugins
417 std::string( PLUGIN_PREFIX ) +
418 std::string( "cpPluginsIO." ) +
419 std::string( PLUGIN_EXT )
422 std::string( PLUGIN_PREFIX ) +
423 std::string( "cpPluginsBasicFilters." ) +
424 std::string( PLUGIN_EXT )
428 // -------------------------------------------------------------------------
432 // Close all connections
433 this->m_Plugins.UnloadAll( );
439 // -------------------------------------------------------------------------
441 _LoadPlugins( const std::string& filename )
443 QApplication::setOverrideCursor( Qt::WaitCursor );
444 this->setEnabled( false );
446 this->m_ImageReaderClass = "";
447 this->m_ImageWriterClass = "";
448 this->m_MeshReaderClass = "";
449 this->m_MeshWriterClass = "";
450 this->m_MeshCutterClass = "";
451 this->m_UI->MenuImageToImage->clear( );
452 this->m_UI->MenuImageToMesh->clear( );
454 if( !( this->m_Plugins.Load( filename ) ) )
457 typedef TPluginsInterface::TClasses _TClasses;
458 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
459 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
461 TPluginFilter::Pointer o =
462 this->m_Plugins.CreateProcessObject( cIt->first );
463 std::string name = o->GetClassName( );
464 std::string category = o->GetClassCategory( );
465 if( category == "ImageReader" )
466 this->m_ImageReaderClass = name;
467 else if( category == "ImageWriter" )
468 this->m_ImageWriterClass = name;
469 else if( category == "MeshReader" )
470 this->m_MeshReaderClass = name;
471 else if( category == "MeshWriter" )
472 this->m_MeshWriterClass = name;
473 else if( category == "MeshToMeshFilter" )
475 if( name.find_last_of( "Cutter" ) != std::string::npos )
476 this->m_MeshCutterClass = name;
478 else if( category == "ImageToImageFilter" )
481 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
483 action, SIGNAL( triggered( ) ),
484 this, SLOT( _triggered_actionImageToImage( ) )
487 else if( category == "ImageToMeshFilter" )
490 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
492 action, SIGNAL( triggered( ) ),
493 this, SLOT( _triggered_actionImageToMesh( ) )
499 QApplication::restoreOverrideCursor( );
500 this->setEnabled( true );
505 // -------------------------------------------------------------------------
506 std::string ImageMPR::
507 _LoadImage( TPluginImage::Pointer& image )
509 std::string ret = "";
512 // Get a reader from loaded plugins
513 TPluginFilter::Pointer reader =
514 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
515 if( reader.IsNotNull( ) )
517 if( reader->ExecConfigurationDialog( this ) )
520 QApplication::setOverrideCursor( Qt::WaitCursor );
521 this->setEnabled( false );
523 // Execute and get error message, if any
524 ret = reader->Update( );
526 // Assign fresh image, if any
529 image = reader->GetOutput< TPluginImage >( 0 );
530 reader->DisconnectOutputs( );
534 // Unblock application
535 QApplication::restoreOverrideCursor( );
536 this->setEnabled( true );
541 ret = "No suitable reader object found in loaded plugins.";
546 // -------------------------------------------------------------------------
547 std::string ImageMPR::
548 _ConfigureMeshActors( )
550 if( this->m_Mesh.IsNull( ) )
551 return( "Valid mesh not found." );
553 this->m_Mesh->CreateVTKActor( );
554 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
555 if( vtk_actor != NULL )
557 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
558 this->m_MPRObjects->Render( 4 );
562 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
564 std::string err = "";
565 for( unsigned int i = 0; i < 3; ++i )
567 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
568 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
569 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
570 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
571 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
572 std::string lerr = this->m_Cutters[ i ]->Update( );
575 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
576 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
577 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
579 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
581 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
583 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
589 this->m_MPRObjects->RenderAll( );
593 // -------------------------------------------------------------------------
595 _triggered_actionOpenPlugins( )
597 // Show dialog and check if it was accepted
598 QFileDialog dialog( this );
599 dialog.setFileMode( QFileDialog::ExistingFile );
600 dialog.setDirectory( "." );
601 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
602 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
603 if( !( dialog.exec( ) ) )
606 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
607 if( !( _LoadPlugins( fname ) ) )
608 QMessageBox::critical(
610 tr( "Ignoring plugin" ),
615 // -------------------------------------------------------------------------
617 _triggered_actionOpenInputImage( )
620 std::string err = this->_LoadImage( this->m_Image );
623 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
626 this->m_MPRObjects->SetImage( vtk_id );
627 this->m_MPRObjects->ActivateInteractors( );
628 this->m_MPRObjects->ResetCameras( );
629 this->m_MPRObjects->RenderAll( );
631 MementoState(m_state, this->m_Image);
635 QMessageBox::critical(
637 tr( "Error message" ),
638 tr( "Read image does not have a valid VTK converter." )
642 QMessageBox::critical(
644 tr( "Error reading single image" ),
649 // -------------------------------------------------------------------------
651 _triggered_actionOpenSegmentation( )
653 if( this->m_Image.IsNull( ) )
655 QMessageBox::critical(
657 tr( "Error message" ),
658 tr( "Before reading a segmentation, first load a raw image." )
665 std::string err = this->_LoadImage( this->m_Segmentation );
668 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
671 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
672 this->m_MPRObjects->RenderAll( );
675 QMessageBox::critical(
677 tr( "Error message" ),
678 tr( "Read image does not have a valid VTK converter." )
682 QMessageBox::critical(
684 tr( "Error reading single image" ),
689 // -------------------------------------------------------------------------
691 _triggered_actionOpenInputPolyData( )
695 // Get a reader from plugins
696 TPluginFilter::Pointer reader =
697 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
699 if( reader.IsNotNull( ) )
702 if( reader->ExecConfigurationDialog( this ) )
704 // Execute and get error message, if any
705 QApplication::setOverrideCursor( Qt::WaitCursor );
706 this->setEnabled( false );
707 std::string err = reader->Update( );
708 QApplication::restoreOverrideCursor( );
709 this->setEnabled( true );
711 // Assign fresh mesh, if any
714 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
715 reader->DisconnectOutputs( );
716 err = this->_ConfigureMeshActors( );
718 QMessageBox::critical(
720 tr( "Error message" ),
725 QMessageBox::critical(
727 tr( "Error reading mesh" ),
734 QMessageBox::critical(
736 tr( "Error reading single mesh" ),
737 tr( "No suitable mesh reader found in loaded plugins." )
741 // -------------------------------------------------------------------------
743 _triggered_actionImageToImage( )
745 if( this->m_Image.IsNull( ) )
749 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
752 std::string name = action->text( ).toStdString( );
755 TPluginFilter::Pointer filter =
756 this->m_Plugins.CreateProcessObject( name );
757 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
762 QApplication::setOverrideCursor( Qt::WaitCursor );
763 this->setEnabled( false );
764 filter->SetInput( 0, this->m_Image );
765 std::string err = filter->Update( );
766 QApplication::restoreOverrideCursor( );
767 this->setEnabled( true );
772 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
773 result->DisconnectPipeline( );
774 this->m_Image = result;
775 if( this->m_Image.IsNotNull( ) )
776 this->m_MPRObjects->SetImage(
777 this->m_Image->GetVTK< vtkImageData >( )
782 MementoState(this->m_state, this->m_Image);
784 if (this->m_state > this->m_max_state)
786 this->m_max_state = this->m_state;
790 QMessageBox::critical(
792 tr( "Error executing filter" ),
797 // -------------------------------------------------------------------------
799 _triggered_actionImageToMesh( )
801 if( this->m_Image.IsNull( ) )
805 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
808 std::string name = action->text( ).toStdString( );
811 TPluginFilter::Pointer filter =
812 this->m_Plugins.CreateProcessObject( name );
813 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
818 QApplication::setOverrideCursor( Qt::WaitCursor );
819 this->setEnabled( false );
820 filter->SetInput( 0, this->m_Image );
821 std::string err = filter->Update( );
822 QApplication::restoreOverrideCursor( );
823 this->setEnabled( true );
828 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
829 result->DisconnectPipeline( );
830 this->m_Mesh = result;
831 err = this->_ConfigureMeshActors( );
833 QMessageBox::critical(
835 tr( "Error message" ),
840 QMessageBox::critical(
842 tr( "Error executing filter" ),
847 // -------------------------------------------------------------------------
849 _triggered_actionUndo()
851 MementoState memento = MementoState();
856 this->m_MPRObjects->SetImage(
857 memento.getMemento(this->m_state)->GetOutput()
861 QMessageBox::warning(
864 tr("No history to undo")
870 // -------------------------------------------------------------------------
872 _triggered_actionRedo()
874 MementoState memento = MementoState();
875 if (this->m_state + 1 <= m_max_state)
878 this->m_MPRObjects->SetImage(
879 memento.getMemento(this->m_state)->GetOutput()
883 QMessageBox::warning(
886 tr("No history to redo")