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 )
79 std::cout << *oIt << std::endl;
82 std::vector< std::string > outputs;
83 std::string err = this->m_Plugins->UpdateActiveFilter( outputs );
86 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
88 TPlugins::TImage* image = this->m_Plugins->GetImage( *oIt );
91 vtkImageData* vimage = image->GetVTK< vtkImageData >( );
94 this->m_UI->MPR->AddImage(
95 vimage, *oIt, this->m_Plugins->GetParent( *oIt )
97 this->m_UI->MPR->ShowData( *oIt );
104 TPlugins::TMesh* mesh = this->m_Plugins->GetMesh( *oIt );
107 this->m_Plugins->BlockWidget( );
108 this->m_UI->MPR->AddMesh(
109 mesh->GetVTK< vtkPolyData >( ),
111 this->m_Plugins->GetParent( *oIt )
113 this->m_UI->MPR->ShowData( *oIt );
114 this->m_Plugins->UnblockWidget( );
122 QMessageBox::critical(
124 tr( "Error executing filter" ),
125 tr( ( std::string( "Error caught: " ) + err ).c_str( ) )
133 // -------------------------------------------------------------------------
135 _AssociatePluginsToMenu( )
137 this->m_UI->MenuFilters->clear( );
139 TPlugins::TStringContainer categories;
140 this->m_Plugins.GetLoadedCategories( categories );
141 for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
143 QMenu* category = this->m_UI->MenuFilters->addMenu( cIt->c_str( ) );
144 const TPlugins::TStringContainer& filters =
145 this->m_Plugins.GetLoadedFilters( *cIt );
146 for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
148 QAction* filter = category->addAction( fIt->c_str( ) );
150 filter, SIGNAL( triggered( ) ),
151 this, SLOT( _execPlugin( ) )
159 // -------------------------------------------------------------------------
160 #define ImageMPR_ReadImage( F ) \
161 this->m_UI->MPR->DeleteAllData( ); \
162 this->m_Plugins.ClearDataObjects( ); \
165 std::string name = this->m_Plugins.Read##F( "" ); \
168 TImage* image = this->m_Plugins.GetData< TImage >( name ); \
169 vtkImageData* vimage = image->GetVTK< vtkImageData >( ); \
170 if( vimage != NULL ) \
172 this->m_UI->MPR->AddImage( vimage, name ); \
173 this->m_UI->MPR->ShowData( name ); \
176 QMessageBox::critical( \
178 QMessageBox::tr( "Error showing image." ), \
179 QMessageBox::tr( "Image read, but no valid VTK conversion found." ) \
182 catch( std::exception& err ) \
184 QMessageBox::critical( \
186 QMessageBox::tr( "Error reading image." ), \
187 QMessageBox::tr( err.what( ) ) \
191 void ImageMPR::_aOpenImage( ) { ImageMPR_ReadImage( Image ) }
192 void ImageMPR::_aOpenDICOMSeries( ) { ImageMPR_ReadImage( DicomSeries ) }
194 // -------------------------------------------------------------------------
196 _aOpenSegmentation( )
199 if( this->m_ImageLoaded != "" )
200 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
204 // -------------------------------------------------------------------------
210 // -------------------------------------------------------------------------
215 std::string data_name = this->m_UI->MPR->GetSelectedData( );
216 this->m_Plugins->WriteImage( data_name );
220 // -------------------------------------------------------------------------
222 _aSaveSegmentation( )
226 // -------------------------------------------------------------------------
232 // -------------------------------------------------------------------------
238 // -------------------------------------------------------------------------
244 // -------------------------------------------------------------------------
248 this->m_Plugins.DialogLoadPlugins( );
249 this->_AssociatePluginsToMenu( );
252 // -------------------------------------------------------------------------
258 // -------------------------------------------------------------------------
263 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
266 std::string filter_name = action->text( ).toStdString( );
269 if( !( this->m_Plugins.ActivateFilter( filter_name ) ) )
273 TPlugins::TStringContainer inputs;
274 this->m_Plugins.GetActiveFilterInputsNames( inputs );
277 if( inputs.size( ) > 1 )
281 else if( inputs.size( ) == 1 )
283 this->m_ActiveFilterMainInput = this->m_UI->MPR->GetSelectedData( );
284 this->m_Plugins.ConnectInputInActiveFilter(
285 this->m_ActiveFilterMainInput, *( inputs.begin( ) )
290 // Configure paramereters
291 auto dlg_res = this->m_Plugins.ConfigureActiveFilter( );
292 if( dlg_res == TPlugins::TProcessObject::DialogResult_Cancel )
294 // Just deactivate filter, since it was canceled
295 this->m_Plugins.DeactivateFilter( );
298 else if( dlg_res == TPlugins::TProcessObject::DialogResult_NoModal )
300 // Execute automatic filter and associate outputs
301 this->UpdateActualFilter( );
302 this->m_Plugins.DeactivateFilter( );
307 // -------------------------------------------------------------------------
310 _CursorCommand( double* pos, int axis, void* data )
312 Self* app = reinterpret_cast< Self* >( data );
315 if( !( app->m_Flooding ) )
318 cpPlugins::Interface::ProcessObject::Pointer filter =
319 app->m_UI->MPR->CreateFilter(
320 "cpPlugins::BasicFilters::FloodFillImageFilter"
322 if( filter.IsNull( ) )
325 cpPlugins::Interface::Parameters* params = filter->GetParameters( );
326 params->SetPoint( "Seed", 3, pos );
327 params->SetReal( "Window", app->m_UI->MPR->GetWindow( ) );
328 params->SetReal( "Level", app->m_UI->MPR->GetLevel( ) );
329 params->SetUint( "InsideValue", 1 );
330 params->SetUint( "OutsideValue", 0 );
331 filter->SetInput( "Input", app->m_UI->MPR->GetImage( app->m_ImageLoaded ) );
332 app->m_UI->MPR->Block( );
333 std::string err = filter->Update( );
334 cpPlugins::Interface::BaseMPRWindow::TImage::Pointer image = filter->GetOutput< cpPlugins::Interface::BaseMPRWindow::TImage >( "Output" );
335 filter->DisconnectOutputs( );
336 app->m_UI->MPR->AddImage( "Segmentation", image );
337 app->m_UI->MPR->Unblock( );
340 << "CursorCommand ==> "
345 << data << std::endl;
350 #include "MementoState.h"
352 #include <vtkProperty.h>
353 #include <vtkRenderWindow.h>
354 #include <vtkMetaImageReader.h>
356 #include <QFileDialog>
357 #include <QMessageBox>
360 # define PLUGIN_PREFIX ""
361 # define PLUGIN_EXT "dll"
362 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
364 # define PLUGIN_PREFIX "lib"
365 # define PLUGIN_EXT "so"
366 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
369 // -------------------------------------------------------------------------
370 ImageMPR::ImageMPR( QWidget* parent )
371 : QMainWindow( parent ),
372 m_UI( new Ui::ImageMPR ),
373 m_ImageReaderClass( "" ),
374 m_ImageWriterClass( "" ),
375 m_MeshReaderClass( "" ),
376 m_MeshWriterClass( "" ),
377 m_MeshCutterClass( "" ),
382 this->m_UI->setupUi( this );
384 // Create and associate renderers
385 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
386 this->m_MPRObjects->SetRenderWindows(
387 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
388 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
389 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
390 this->m_UI->m_3DVTK->GetRenderWindow( )
395 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
396 this, SLOT( _triggered_actionOpenPlugins( ) )
399 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
400 this, SLOT( _triggered_actionOpenInputImage( ) )
403 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
404 this, SLOT( _triggered_actionOpenSegmentation( ) )
407 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
408 this, SLOT( _triggered_actionOpenInputPolyData( ) )
411 this->m_UI->actionUndo, SIGNAL(triggered()),
412 this, SLOT(_triggered_actionUndo())
415 this->m_UI->actionRedo, SIGNAL(triggered()),
416 this, SLOT(_triggered_actionRedo())
419 // Start: load all disponible plugins
421 std::string( PLUGIN_PREFIX ) +
422 std::string( "cpPluginsIO." ) +
423 std::string( PLUGIN_EXT )
426 std::string( PLUGIN_PREFIX ) +
427 std::string( "cpPluginsBasicFilters." ) +
428 std::string( PLUGIN_EXT )
432 // -------------------------------------------------------------------------
436 // Close all connections
437 this->m_Plugins.UnloadAll( );
443 // -------------------------------------------------------------------------
445 _LoadPlugins( const std::string& filename )
447 QApplication::setOverrideCursor( Qt::WaitCursor );
448 this->setEnabled( false );
450 this->m_ImageReaderClass = "";
451 this->m_ImageWriterClass = "";
452 this->m_MeshReaderClass = "";
453 this->m_MeshWriterClass = "";
454 this->m_MeshCutterClass = "";
455 this->m_UI->MenuImageToImage->clear( );
456 this->m_UI->MenuImageToMesh->clear( );
458 if( !( this->m_Plugins.Load( filename ) ) )
461 typedef TPluginsInterface::TClasses _TClasses;
462 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
463 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
465 TPluginFilter::Pointer o =
466 this->m_Plugins.CreateProcessObject( cIt->first );
467 std::string name = o->GetClassName( );
468 std::string category = o->GetClassCategory( );
469 if( category == "ImageReader" )
470 this->m_ImageReaderClass = name;
471 else if( category == "ImageWriter" )
472 this->m_ImageWriterClass = name;
473 else if( category == "MeshReader" )
474 this->m_MeshReaderClass = name;
475 else if( category == "MeshWriter" )
476 this->m_MeshWriterClass = name;
477 else if( category == "MeshToMeshFilter" )
479 if( name.find_last_of( "Cutter" ) != std::string::npos )
480 this->m_MeshCutterClass = name;
482 else if( category == "ImageToImageFilter" )
485 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
487 action, SIGNAL( triggered( ) ),
488 this, SLOT( _triggered_actionImageToImage( ) )
491 else if( category == "ImageToMeshFilter" )
494 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
496 action, SIGNAL( triggered( ) ),
497 this, SLOT( _triggered_actionImageToMesh( ) )
503 QApplication::restoreOverrideCursor( );
504 this->setEnabled( true );
509 // -------------------------------------------------------------------------
510 std::string ImageMPR::
511 _LoadImage( TPluginImage::Pointer& image )
513 std::string ret = "";
516 // Get a reader from loaded plugins
517 TPluginFilter::Pointer reader =
518 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
519 if( reader.IsNotNull( ) )
521 if( reader->ExecConfigurationDialog( this ) )
524 QApplication::setOverrideCursor( Qt::WaitCursor );
525 this->setEnabled( false );
527 // Execute and get error message, if any
528 ret = reader->Update( );
530 // Assign fresh image, if any
533 image = reader->GetOutput< TPluginImage >( 0 );
534 reader->DisconnectOutputs( );
538 // Unblock application
539 QApplication::restoreOverrideCursor( );
540 this->setEnabled( true );
545 ret = "No suitable reader object found in loaded plugins.";
550 // -------------------------------------------------------------------------
551 std::string ImageMPR::
552 _ConfigureMeshActors( )
554 if( this->m_Mesh.IsNull( ) )
555 return( "Valid mesh not found." );
557 this->m_Mesh->CreateVTKActor( );
558 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
559 if( vtk_actor != NULL )
561 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
562 this->m_MPRObjects->Render( 4 );
566 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
568 std::string err = "";
569 for( unsigned int i = 0; i < 3; ++i )
571 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
572 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
573 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
574 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
575 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
576 std::string lerr = this->m_Cutters[ i ]->Update( );
579 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
580 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
581 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
583 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
585 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
587 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
593 this->m_MPRObjects->RenderAll( );
597 // -------------------------------------------------------------------------
599 _triggered_actionOpenPlugins( )
601 // Show dialog and check if it was accepted
602 QFileDialog dialog( this );
603 dialog.setFileMode( QFileDialog::ExistingFile );
604 dialog.setDirectory( "." );
605 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
606 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
607 if( !( dialog.exec( ) ) )
610 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
611 if( !( _LoadPlugins( fname ) ) )
612 QMessageBox::critical(
614 tr( "Ignoring plugin" ),
619 // -------------------------------------------------------------------------
621 _triggered_actionOpenInputImage( )
624 std::string err = this->_LoadImage( this->m_Image );
627 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
630 this->m_MPRObjects->SetImage( vtk_id );
631 this->m_MPRObjects->ActivateInteractors( );
632 this->m_MPRObjects->ResetCameras( );
633 this->m_MPRObjects->RenderAll( );
635 MementoState(m_state, this->m_Image);
639 QMessageBox::critical(
641 tr( "Error message" ),
642 tr( "Read image does not have a valid VTK converter." )
646 QMessageBox::critical(
648 tr( "Error reading single image" ),
653 // -------------------------------------------------------------------------
655 _triggered_actionOpenSegmentation( )
657 if( this->m_Image.IsNull( ) )
659 QMessageBox::critical(
661 tr( "Error message" ),
662 tr( "Before reading a segmentation, first load a raw image." )
669 std::string err = this->_LoadImage( this->m_Segmentation );
672 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
675 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
676 this->m_MPRObjects->RenderAll( );
679 QMessageBox::critical(
681 tr( "Error message" ),
682 tr( "Read image does not have a valid VTK converter." )
686 QMessageBox::critical(
688 tr( "Error reading single image" ),
693 // -------------------------------------------------------------------------
695 _triggered_actionOpenInputPolyData( )
699 // Get a reader from plugins
700 TPluginFilter::Pointer reader =
701 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
703 if( reader.IsNotNull( ) )
706 if( reader->ExecConfigurationDialog( this ) )
708 // Execute and get error message, if any
709 QApplication::setOverrideCursor( Qt::WaitCursor );
710 this->setEnabled( false );
711 std::string err = reader->Update( );
712 QApplication::restoreOverrideCursor( );
713 this->setEnabled( true );
715 // Assign fresh mesh, if any
718 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
719 reader->DisconnectOutputs( );
720 err = this->_ConfigureMeshActors( );
722 QMessageBox::critical(
724 tr( "Error message" ),
729 QMessageBox::critical(
731 tr( "Error reading mesh" ),
738 QMessageBox::critical(
740 tr( "Error reading single mesh" ),
741 tr( "No suitable mesh reader found in loaded plugins." )
745 // -------------------------------------------------------------------------
747 _triggered_actionImageToImage( )
749 if( this->m_Image.IsNull( ) )
753 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
756 std::string name = action->text( ).toStdString( );
759 TPluginFilter::Pointer filter =
760 this->m_Plugins.CreateProcessObject( name );
761 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
766 QApplication::setOverrideCursor( Qt::WaitCursor );
767 this->setEnabled( false );
768 filter->SetInput( 0, this->m_Image );
769 std::string err = filter->Update( );
770 QApplication::restoreOverrideCursor( );
771 this->setEnabled( true );
776 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
777 result->DisconnectPipeline( );
778 this->m_Image = result;
779 if( this->m_Image.IsNotNull( ) )
780 this->m_MPRObjects->SetImage(
781 this->m_Image->GetVTK< vtkImageData >( )
786 MementoState(this->m_state, this->m_Image);
788 if (this->m_state > this->m_max_state)
790 this->m_max_state = this->m_state;
794 QMessageBox::critical(
796 tr( "Error executing filter" ),
801 // -------------------------------------------------------------------------
803 _triggered_actionImageToMesh( )
805 if( this->m_Image.IsNull( ) )
809 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
812 std::string name = action->text( ).toStdString( );
815 TPluginFilter::Pointer filter =
816 this->m_Plugins.CreateProcessObject( name );
817 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
822 QApplication::setOverrideCursor( Qt::WaitCursor );
823 this->setEnabled( false );
824 filter->SetInput( 0, this->m_Image );
825 std::string err = filter->Update( );
826 QApplication::restoreOverrideCursor( );
827 this->setEnabled( true );
832 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
833 result->DisconnectPipeline( );
834 this->m_Mesh = result;
835 err = this->_ConfigureMeshActors( );
837 QMessageBox::critical(
839 tr( "Error message" ),
844 QMessageBox::critical(
846 tr( "Error executing filter" ),
851 // -------------------------------------------------------------------------
853 _triggered_actionUndo()
855 MementoState memento = MementoState();
860 this->m_MPRObjects->SetImage(
861 memento.getMemento(this->m_state)->GetOutput()
865 QMessageBox::warning(
868 tr("No history to undo")
874 // -------------------------------------------------------------------------
876 _triggered_actionRedo()
878 MementoState memento = MementoState();
879 if (this->m_state + 1 <= m_max_state)
882 this->m_MPRObjects->SetImage(
883 memento.getMemento(this->m_state)->GetOutput()
887 QMessageBox::warning(
890 tr("No history to redo")