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 this->m_UI->MPR->AddData( image, name, "" ); \
170 this->m_UI->MPR->ShowData( name ); \
172 catch( std::exception& err ) \
174 QMessageBox::critical( \
176 QMessageBox::tr( "Error reading image." ), \
177 QMessageBox::tr( err.what( ) ) \
181 void ImageMPR::_aOpenImage( ) { ImageMPR_ReadImage( Image ) }
182 void ImageMPR::_aOpenDICOMSeries( ) { ImageMPR_ReadImage( DicomSeries ) }
184 // -------------------------------------------------------------------------
186 _aOpenSegmentation( )
189 if( this->m_ImageLoaded != "" )
190 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
194 // -------------------------------------------------------------------------
200 // -------------------------------------------------------------------------
205 std::string data_name = this->m_UI->MPR->GetSelectedData( );
206 this->m_Plugins->WriteImage( data_name );
210 // -------------------------------------------------------------------------
212 _aSaveSegmentation( )
216 // -------------------------------------------------------------------------
222 // -------------------------------------------------------------------------
228 // -------------------------------------------------------------------------
234 // -------------------------------------------------------------------------
238 this->m_Plugins.DialogLoadPlugins( );
239 this->_AssociatePluginsToMenu( );
242 // -------------------------------------------------------------------------
248 // -------------------------------------------------------------------------
253 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
256 std::string filter_name = action->text( ).toStdString( );
259 if( !( this->m_Plugins.ActivateFilter( filter_name ) ) )
263 TPlugins::TStringContainer inputs;
264 this->m_Plugins.GetActiveFilterInputsNames( inputs );
267 if( inputs.size( ) > 1 )
271 else if( inputs.size( ) == 1 )
273 this->m_ActiveFilterMainInput = this->m_UI->MPR->GetSelectedData( );
274 this->m_Plugins.ConnectInputInActiveFilter(
275 this->m_ActiveFilterMainInput, *( inputs.begin( ) )
280 // Configure paramereters
281 auto dlg_res = this->m_Plugins.ConfigureActiveFilter( );
282 if( dlg_res == TPlugins::TProcessObject::DialogResult_Cancel )
284 // Just deactivate filter, since it was canceled
285 this->m_Plugins.DeactivateFilter( );
288 else if( dlg_res == TPlugins::TProcessObject::DialogResult_NoModal )
290 // Execute automatic filter and associate outputs
291 this->UpdateActualFilter( );
292 this->m_Plugins.DeactivateFilter( );
297 // -------------------------------------------------------------------------
300 _CursorCommand( double* pos, int axis, void* data )
302 Self* app = reinterpret_cast< Self* >( data );
305 if( !( app->m_Flooding ) )
308 cpPlugins::Interface::ProcessObject::Pointer filter =
309 app->m_UI->MPR->CreateFilter(
310 "cpPlugins::BasicFilters::FloodFillImageFilter"
312 if( filter.IsNull( ) )
315 cpPlugins::Interface::Parameters* params = filter->GetParameters( );
316 params->SetPoint( "Seed", 3, pos );
317 params->SetReal( "Window", app->m_UI->MPR->GetWindow( ) );
318 params->SetReal( "Level", app->m_UI->MPR->GetLevel( ) );
319 params->SetUint( "InsideValue", 1 );
320 params->SetUint( "OutsideValue", 0 );
321 filter->SetInput( "Input", app->m_UI->MPR->GetImage( app->m_ImageLoaded ) );
322 app->m_UI->MPR->Block( );
323 std::string err = filter->Update( );
324 cpPlugins::Interface::BaseMPRWindow::TImage::Pointer image = filter->GetOutput< cpPlugins::Interface::BaseMPRWindow::TImage >( "Output" );
325 filter->DisconnectOutputs( );
326 app->m_UI->MPR->AddImage( "Segmentation", image );
327 app->m_UI->MPR->Unblock( );
330 << "CursorCommand ==> "
335 << data << std::endl;
340 #include "MementoState.h"
342 #include <vtkProperty.h>
343 #include <vtkRenderWindow.h>
344 #include <vtkMetaImageReader.h>
346 #include <QFileDialog>
347 #include <QMessageBox>
350 # define PLUGIN_PREFIX ""
351 # define PLUGIN_EXT "dll"
352 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
354 # define PLUGIN_PREFIX "lib"
355 # define PLUGIN_EXT "so"
356 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
359 // -------------------------------------------------------------------------
360 ImageMPR::ImageMPR( QWidget* parent )
361 : QMainWindow( parent ),
362 m_UI( new Ui::ImageMPR ),
363 m_ImageReaderClass( "" ),
364 m_ImageWriterClass( "" ),
365 m_MeshReaderClass( "" ),
366 m_MeshWriterClass( "" ),
367 m_MeshCutterClass( "" ),
372 this->m_UI->setupUi( this );
374 // Create and associate renderers
375 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
376 this->m_MPRObjects->SetRenderWindows(
377 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
378 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
379 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
380 this->m_UI->m_3DVTK->GetRenderWindow( )
385 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
386 this, SLOT( _triggered_actionOpenPlugins( ) )
389 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
390 this, SLOT( _triggered_actionOpenInputImage( ) )
393 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
394 this, SLOT( _triggered_actionOpenSegmentation( ) )
397 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
398 this, SLOT( _triggered_actionOpenInputPolyData( ) )
401 this->m_UI->actionUndo, SIGNAL(triggered()),
402 this, SLOT(_triggered_actionUndo())
405 this->m_UI->actionRedo, SIGNAL(triggered()),
406 this, SLOT(_triggered_actionRedo())
409 // Start: load all disponible plugins
411 std::string( PLUGIN_PREFIX ) +
412 std::string( "cpPluginsIO." ) +
413 std::string( PLUGIN_EXT )
416 std::string( PLUGIN_PREFIX ) +
417 std::string( "cpPluginsBasicFilters." ) +
418 std::string( PLUGIN_EXT )
422 // -------------------------------------------------------------------------
426 // Close all connections
427 this->m_Plugins.UnloadAll( );
433 // -------------------------------------------------------------------------
435 _LoadPlugins( const std::string& filename )
437 QApplication::setOverrideCursor( Qt::WaitCursor );
438 this->setEnabled( false );
440 this->m_ImageReaderClass = "";
441 this->m_ImageWriterClass = "";
442 this->m_MeshReaderClass = "";
443 this->m_MeshWriterClass = "";
444 this->m_MeshCutterClass = "";
445 this->m_UI->MenuImageToImage->clear( );
446 this->m_UI->MenuImageToMesh->clear( );
448 if( !( this->m_Plugins.Load( filename ) ) )
451 typedef TPluginsInterface::TClasses _TClasses;
452 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
453 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
455 TPluginFilter::Pointer o =
456 this->m_Plugins.CreateProcessObject( cIt->first );
457 std::string name = o->GetClassName( );
458 std::string category = o->GetClassCategory( );
459 if( category == "ImageReader" )
460 this->m_ImageReaderClass = name;
461 else if( category == "ImageWriter" )
462 this->m_ImageWriterClass = name;
463 else if( category == "MeshReader" )
464 this->m_MeshReaderClass = name;
465 else if( category == "MeshWriter" )
466 this->m_MeshWriterClass = name;
467 else if( category == "MeshToMeshFilter" )
469 if( name.find_last_of( "Cutter" ) != std::string::npos )
470 this->m_MeshCutterClass = name;
472 else if( category == "ImageToImageFilter" )
475 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
477 action, SIGNAL( triggered( ) ),
478 this, SLOT( _triggered_actionImageToImage( ) )
481 else if( category == "ImageToMeshFilter" )
484 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
486 action, SIGNAL( triggered( ) ),
487 this, SLOT( _triggered_actionImageToMesh( ) )
493 QApplication::restoreOverrideCursor( );
494 this->setEnabled( true );
499 // -------------------------------------------------------------------------
500 std::string ImageMPR::
501 _LoadImage( TPluginImage::Pointer& image )
503 std::string ret = "";
506 // Get a reader from loaded plugins
507 TPluginFilter::Pointer reader =
508 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
509 if( reader.IsNotNull( ) )
511 if( reader->ExecConfigurationDialog( this ) )
514 QApplication::setOverrideCursor( Qt::WaitCursor );
515 this->setEnabled( false );
517 // Execute and get error message, if any
518 ret = reader->Update( );
520 // Assign fresh image, if any
523 image = reader->GetOutput< TPluginImage >( 0 );
524 reader->DisconnectOutputs( );
528 // Unblock application
529 QApplication::restoreOverrideCursor( );
530 this->setEnabled( true );
535 ret = "No suitable reader object found in loaded plugins.";
540 // -------------------------------------------------------------------------
541 std::string ImageMPR::
542 _ConfigureMeshActors( )
544 if( this->m_Mesh.IsNull( ) )
545 return( "Valid mesh not found." );
547 this->m_Mesh->CreateVTKActor( );
548 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
549 if( vtk_actor != NULL )
551 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
552 this->m_MPRObjects->Render( 4 );
556 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
558 std::string err = "";
559 for( unsigned int i = 0; i < 3; ++i )
561 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
562 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
563 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
564 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
565 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
566 std::string lerr = this->m_Cutters[ i ]->Update( );
569 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
570 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
571 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
573 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
575 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
577 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
583 this->m_MPRObjects->RenderAll( );
587 // -------------------------------------------------------------------------
589 _triggered_actionOpenPlugins( )
591 // Show dialog and check if it was accepted
592 QFileDialog dialog( this );
593 dialog.setFileMode( QFileDialog::ExistingFile );
594 dialog.setDirectory( "." );
595 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
596 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
597 if( !( dialog.exec( ) ) )
600 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
601 if( !( _LoadPlugins( fname ) ) )
602 QMessageBox::critical(
604 tr( "Ignoring plugin" ),
609 // -------------------------------------------------------------------------
611 _triggered_actionOpenInputImage( )
614 std::string err = this->_LoadImage( this->m_Image );
617 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
620 this->m_MPRObjects->SetImage( vtk_id );
621 this->m_MPRObjects->ActivateInteractors( );
622 this->m_MPRObjects->ResetCameras( );
623 this->m_MPRObjects->RenderAll( );
625 MementoState(m_state, this->m_Image);
629 QMessageBox::critical(
631 tr( "Error message" ),
632 tr( "Read image does not have a valid VTK converter." )
636 QMessageBox::critical(
638 tr( "Error reading single image" ),
643 // -------------------------------------------------------------------------
645 _triggered_actionOpenSegmentation( )
647 if( this->m_Image.IsNull( ) )
649 QMessageBox::critical(
651 tr( "Error message" ),
652 tr( "Before reading a segmentation, first load a raw image." )
659 std::string err = this->_LoadImage( this->m_Segmentation );
662 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
665 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
666 this->m_MPRObjects->RenderAll( );
669 QMessageBox::critical(
671 tr( "Error message" ),
672 tr( "Read image does not have a valid VTK converter." )
676 QMessageBox::critical(
678 tr( "Error reading single image" ),
683 // -------------------------------------------------------------------------
685 _triggered_actionOpenInputPolyData( )
689 // Get a reader from plugins
690 TPluginFilter::Pointer reader =
691 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
693 if( reader.IsNotNull( ) )
696 if( reader->ExecConfigurationDialog( this ) )
698 // Execute and get error message, if any
699 QApplication::setOverrideCursor( Qt::WaitCursor );
700 this->setEnabled( false );
701 std::string err = reader->Update( );
702 QApplication::restoreOverrideCursor( );
703 this->setEnabled( true );
705 // Assign fresh mesh, if any
708 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
709 reader->DisconnectOutputs( );
710 err = this->_ConfigureMeshActors( );
712 QMessageBox::critical(
714 tr( "Error message" ),
719 QMessageBox::critical(
721 tr( "Error reading mesh" ),
728 QMessageBox::critical(
730 tr( "Error reading single mesh" ),
731 tr( "No suitable mesh reader found in loaded plugins." )
735 // -------------------------------------------------------------------------
737 _triggered_actionImageToImage( )
739 if( this->m_Image.IsNull( ) )
743 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
746 std::string name = action->text( ).toStdString( );
749 TPluginFilter::Pointer filter =
750 this->m_Plugins.CreateProcessObject( name );
751 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
756 QApplication::setOverrideCursor( Qt::WaitCursor );
757 this->setEnabled( false );
758 filter->SetInput( 0, this->m_Image );
759 std::string err = filter->Update( );
760 QApplication::restoreOverrideCursor( );
761 this->setEnabled( true );
766 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
767 result->DisconnectPipeline( );
768 this->m_Image = result;
769 if( this->m_Image.IsNotNull( ) )
770 this->m_MPRObjects->SetImage(
771 this->m_Image->GetVTK< vtkImageData >( )
776 MementoState(this->m_state, this->m_Image);
778 if (this->m_state > this->m_max_state)
780 this->m_max_state = this->m_state;
784 QMessageBox::critical(
786 tr( "Error executing filter" ),
791 // -------------------------------------------------------------------------
793 _triggered_actionImageToMesh( )
795 if( this->m_Image.IsNull( ) )
799 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
802 std::string name = action->text( ).toStdString( );
805 TPluginFilter::Pointer filter =
806 this->m_Plugins.CreateProcessObject( name );
807 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
812 QApplication::setOverrideCursor( Qt::WaitCursor );
813 this->setEnabled( false );
814 filter->SetInput( 0, this->m_Image );
815 std::string err = filter->Update( );
816 QApplication::restoreOverrideCursor( );
817 this->setEnabled( true );
822 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
823 result->DisconnectPipeline( );
824 this->m_Mesh = result;
825 err = this->_ConfigureMeshActors( );
827 QMessageBox::critical(
829 tr( "Error message" ),
834 QMessageBox::critical(
836 tr( "Error executing filter" ),
841 // -------------------------------------------------------------------------
843 _triggered_actionUndo()
845 MementoState memento = MementoState();
850 this->m_MPRObjects->SetImage(
851 memento.getMemento(this->m_state)->GetOutput()
855 QMessageBox::warning(
858 tr("No history to undo")
864 // -------------------------------------------------------------------------
866 _triggered_actionRedo()
868 MementoState memento = MementoState();
869 if (this->m_state + 1 <= m_max_state)
872 this->m_MPRObjects->SetImage(
873 memento.getMemento(this->m_state)->GetOutput()
877 QMessageBox::warning(
880 tr("No history to redo")