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 // -------------------------------------------------------------------------
64 if( !( this->m_Plugins->HasActiveFilter( ) ) )
67 std::vector< std::string > outputs;
68 std::string err = this->m_Plugins->UpdateActiveFilter( outputs );
71 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
73 TPlugins::TImage* image = this->m_Plugins->GetImage( *oIt );
76 vtkImageData* vimage = image->GetVTK< vtkImageData >( );
79 this->m_UI->MPR->AddImage(
80 vimage, *oIt, this->m_Plugins->GetParent( *oIt )
82 this->m_UI->MPR->ShowData( *oIt );
89 TPlugins::TMesh* mesh = this->m_Plugins->GetMesh( *oIt );
92 this->m_Plugins->BlockWidget( );
93 this->m_UI->MPR->AddMesh(
94 mesh->GetVTK< vtkPolyData >( ),
96 this->m_Plugins->GetParent( *oIt )
98 this->m_UI->MPR->ShowData( *oIt );
99 this->m_Plugins->UnblockWidget( );
107 QMessageBox::critical(
109 tr( "Error executing filter" ),
110 tr( ( std::string( "Error caught: " ) + err ).c_str( ) )
118 // -------------------------------------------------------------------------
120 _AssociatePluginsToMenu( )
122 this->m_UI->MenuFilters->clear( );
124 TPlugins::TStringContainer categories;
125 this->m_Plugins.GetLoadedCategories( categories );
126 for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
128 QMenu* category = this->m_UI->MenuFilters->addMenu( cIt->c_str( ) );
129 const TPlugins::TStringContainer& filters =
130 this->m_Plugins.GetLoadedFilters( *cIt );
131 for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
133 QAction* filter = category->addAction( fIt->c_str( ) );
135 filter, SIGNAL( triggered( ) ),
136 this, SLOT( _execPlugin( ) )
144 // -------------------------------------------------------------------------
145 #define ImageMPR_ReadImage( F ) \
146 this->m_UI->MPR->DeleteAllData( ); \
147 this->m_Plugins.ClearDataObjects( ); \
150 std::string name = this->m_Plugins.Read##F( "" ); \
153 TImage* image = this->m_Plugins.GetData< TImage >( name ); \
154 vtkImageData* vimage = image->GetVTK< vtkImageData >( ); \
155 if( vimage != NULL ) \
157 this->m_UI->MPR->AddImage( vimage, name ); \
158 this->m_UI->MPR->ShowData( name ); \
161 QMessageBox::critical( \
163 QMessageBox::tr( "Error showing image." ), \
164 QMessageBox::tr( "Image read, but no valid VTK conversion found." ) \
167 catch( std::exception& err ) \
169 QMessageBox::critical( \
171 QMessageBox::tr( "Error reading image." ), \
172 QMessageBox::tr( err.what( ) ) \
176 void ImageMPR::_aOpenImage( ) { ImageMPR_ReadImage( Image ) }
177 void ImageMPR::_aOpenDICOMSeries( ) { ImageMPR_ReadImage( DicomSeries ) }
179 // -------------------------------------------------------------------------
181 _aOpenSegmentation( )
184 if( this->m_ImageLoaded != "" )
185 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
189 // -------------------------------------------------------------------------
195 // -------------------------------------------------------------------------
200 std::string data_name = this->m_UI->MPR->GetSelectedData( );
201 this->m_Plugins->WriteImage( data_name );
205 // -------------------------------------------------------------------------
207 _aSaveSegmentation( )
211 // -------------------------------------------------------------------------
217 // -------------------------------------------------------------------------
223 // -------------------------------------------------------------------------
229 // -------------------------------------------------------------------------
233 this->m_Plugins.DialogLoadPlugins( );
234 this->_AssociatePluginsToMenu( );
237 // -------------------------------------------------------------------------
243 // -------------------------------------------------------------------------
248 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
251 std::string filter_name = action->text( ).toStdString( );
254 if( !( this->m_Plugins.ActivateFilter( filter_name ) ) )
258 TPlugins::TStringContainer inputs, outputs;
259 this->m_Plugins.GetActiveFilterInputsNames( inputs );
260 this->m_Plugins.GetActiveFilterOutputsNames( outputs );
263 if( inputs.size( ) > 1 )
267 else if( inputs.size( ) == 1 )
268 this->m_Plugins.ConnectInputInActiveFilter(
269 this->m_UI->MPR->GetSelectedData( ), *( inputs.begin( ) )
273 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
274 this->m_Plugins.SetOutputNameInActiveFilter(
275 filter_name + "_" + *oIt, *oIt
278 // Configure paramereters
279 auto dlg_res = this->m_Plugins.ConfigureActiveFilter( );
280 if( dlg_res == TPlugins::TProcessObject::DialogResult_Cancel )
282 this->m_Plugins.DeactivateFilter( );
288 QMenu* menu = dynamic_cast< QMenu* >( action->parentWidget( ) );
291 std::string filter_cate = menu->title( ).toStdString( );
294 if( !( this->m_Plugins->ActivateFilter( filter_name ) ) )
298 std::vector< std::string > inputs =
299 this->m_Plugins->GetActiveFilterInputsNames( );
300 if( inputs.size( ) == 1 )
302 std::string data_name = this->m_UI->MPR->GetSelectedData( );
303 this->m_Plugins->ConnectInputInActiveFilter( data_name, inputs[ 0 ] );
305 else if( inputs.size( ) > 1 )
307 QMessageBox::critical(
309 tr( "Error executing" ),
310 tr( "Filter has multiple inputs: NOT YET IMPLEMENTED!!!" )
317 std::vector< std::string > outputs =
318 this->m_Plugins->GetActiveFilterOutputsNames( );
319 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
320 this->m_Plugins->SetOutputNameInActiveFilter(
321 filter_name + "_" + *oIt, *oIt
325 TPlugins::TProcessObject::DialogResult dlg_res =
326 this->m_Plugins->ConfigureActiveFilter( );
327 if( dlg_res == TPlugins::TProcessObject::DialogResult_Cancel )
329 this->m_Plugins->DeactivateFilter( );
334 // Execute filter and associate outputs
335 if( dlg_res == TPlugins::TProcessObject::DialogResult_NoModal )
337 this->UpdateActualFilter( );
338 this->m_Plugins->DeactivateFilter( );
344 // -------------------------------------------------------------------------
347 _CursorCommand( double* pos, int axis, void* data )
349 Self* app = reinterpret_cast< Self* >( data );
352 if( !( app->m_Flooding ) )
355 cpPlugins::Interface::ProcessObject::Pointer filter =
356 app->m_UI->MPR->CreateFilter(
357 "cpPlugins::BasicFilters::FloodFillImageFilter"
359 if( filter.IsNull( ) )
362 cpPlugins::Interface::Parameters* params = filter->GetParameters( );
363 params->SetPoint( "Seed", 3, pos );
364 params->SetReal( "Window", app->m_UI->MPR->GetWindow( ) );
365 params->SetReal( "Level", app->m_UI->MPR->GetLevel( ) );
366 params->SetUint( "InsideValue", 1 );
367 params->SetUint( "OutsideValue", 0 );
368 filter->SetInput( "Input", app->m_UI->MPR->GetImage( app->m_ImageLoaded ) );
369 app->m_UI->MPR->Block( );
370 std::string err = filter->Update( );
371 cpPlugins::Interface::BaseMPRWindow::TImage::Pointer image = filter->GetOutput< cpPlugins::Interface::BaseMPRWindow::TImage >( "Output" );
372 filter->DisconnectOutputs( );
373 app->m_UI->MPR->AddImage( "Segmentation", image );
374 app->m_UI->MPR->Unblock( );
377 << "CursorCommand ==> "
382 << data << std::endl;
387 #include "MementoState.h"
389 #include <vtkProperty.h>
390 #include <vtkRenderWindow.h>
391 #include <vtkMetaImageReader.h>
393 #include <QFileDialog>
394 #include <QMessageBox>
397 # define PLUGIN_PREFIX ""
398 # define PLUGIN_EXT "dll"
399 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
401 # define PLUGIN_PREFIX "lib"
402 # define PLUGIN_EXT "so"
403 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
406 // -------------------------------------------------------------------------
407 ImageMPR::ImageMPR( QWidget* parent )
408 : QMainWindow( parent ),
409 m_UI( new Ui::ImageMPR ),
410 m_ImageReaderClass( "" ),
411 m_ImageWriterClass( "" ),
412 m_MeshReaderClass( "" ),
413 m_MeshWriterClass( "" ),
414 m_MeshCutterClass( "" ),
419 this->m_UI->setupUi( this );
421 // Create and associate renderers
422 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
423 this->m_MPRObjects->SetRenderWindows(
424 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
425 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
426 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
427 this->m_UI->m_3DVTK->GetRenderWindow( )
432 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
433 this, SLOT( _triggered_actionOpenPlugins( ) )
436 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
437 this, SLOT( _triggered_actionOpenInputImage( ) )
440 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
441 this, SLOT( _triggered_actionOpenSegmentation( ) )
444 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
445 this, SLOT( _triggered_actionOpenInputPolyData( ) )
448 this->m_UI->actionUndo, SIGNAL(triggered()),
449 this, SLOT(_triggered_actionUndo())
452 this->m_UI->actionRedo, SIGNAL(triggered()),
453 this, SLOT(_triggered_actionRedo())
456 // Start: load all disponible plugins
458 std::string( PLUGIN_PREFIX ) +
459 std::string( "cpPluginsIO." ) +
460 std::string( PLUGIN_EXT )
463 std::string( PLUGIN_PREFIX ) +
464 std::string( "cpPluginsBasicFilters." ) +
465 std::string( PLUGIN_EXT )
469 // -------------------------------------------------------------------------
473 // Close all connections
474 this->m_Plugins.UnloadAll( );
480 // -------------------------------------------------------------------------
482 _LoadPlugins( const std::string& filename )
484 QApplication::setOverrideCursor( Qt::WaitCursor );
485 this->setEnabled( false );
487 this->m_ImageReaderClass = "";
488 this->m_ImageWriterClass = "";
489 this->m_MeshReaderClass = "";
490 this->m_MeshWriterClass = "";
491 this->m_MeshCutterClass = "";
492 this->m_UI->MenuImageToImage->clear( );
493 this->m_UI->MenuImageToMesh->clear( );
495 if( !( this->m_Plugins.Load( filename ) ) )
498 typedef TPluginsInterface::TClasses _TClasses;
499 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
500 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
502 TPluginFilter::Pointer o =
503 this->m_Plugins.CreateProcessObject( cIt->first );
504 std::string name = o->GetClassName( );
505 std::string category = o->GetClassCategory( );
506 if( category == "ImageReader" )
507 this->m_ImageReaderClass = name;
508 else if( category == "ImageWriter" )
509 this->m_ImageWriterClass = name;
510 else if( category == "MeshReader" )
511 this->m_MeshReaderClass = name;
512 else if( category == "MeshWriter" )
513 this->m_MeshWriterClass = name;
514 else if( category == "MeshToMeshFilter" )
516 if( name.find_last_of( "Cutter" ) != std::string::npos )
517 this->m_MeshCutterClass = name;
519 else if( category == "ImageToImageFilter" )
522 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
524 action, SIGNAL( triggered( ) ),
525 this, SLOT( _triggered_actionImageToImage( ) )
528 else if( category == "ImageToMeshFilter" )
531 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
533 action, SIGNAL( triggered( ) ),
534 this, SLOT( _triggered_actionImageToMesh( ) )
540 QApplication::restoreOverrideCursor( );
541 this->setEnabled( true );
546 // -------------------------------------------------------------------------
547 std::string ImageMPR::
548 _LoadImage( TPluginImage::Pointer& image )
550 std::string ret = "";
553 // Get a reader from loaded plugins
554 TPluginFilter::Pointer reader =
555 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
556 if( reader.IsNotNull( ) )
558 if( reader->ExecConfigurationDialog( this ) )
561 QApplication::setOverrideCursor( Qt::WaitCursor );
562 this->setEnabled( false );
564 // Execute and get error message, if any
565 ret = reader->Update( );
567 // Assign fresh image, if any
570 image = reader->GetOutput< TPluginImage >( 0 );
571 reader->DisconnectOutputs( );
575 // Unblock application
576 QApplication::restoreOverrideCursor( );
577 this->setEnabled( true );
582 ret = "No suitable reader object found in loaded plugins.";
587 // -------------------------------------------------------------------------
588 std::string ImageMPR::
589 _ConfigureMeshActors( )
591 if( this->m_Mesh.IsNull( ) )
592 return( "Valid mesh not found." );
594 this->m_Mesh->CreateVTKActor( );
595 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
596 if( vtk_actor != NULL )
598 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
599 this->m_MPRObjects->Render( 4 );
603 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
605 std::string err = "";
606 for( unsigned int i = 0; i < 3; ++i )
608 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
609 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
610 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
611 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
612 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
613 std::string lerr = this->m_Cutters[ i ]->Update( );
616 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
617 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
618 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
620 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
622 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
624 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
630 this->m_MPRObjects->RenderAll( );
634 // -------------------------------------------------------------------------
636 _triggered_actionOpenPlugins( )
638 // Show dialog and check if it was accepted
639 QFileDialog dialog( this );
640 dialog.setFileMode( QFileDialog::ExistingFile );
641 dialog.setDirectory( "." );
642 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
643 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
644 if( !( dialog.exec( ) ) )
647 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
648 if( !( _LoadPlugins( fname ) ) )
649 QMessageBox::critical(
651 tr( "Ignoring plugin" ),
656 // -------------------------------------------------------------------------
658 _triggered_actionOpenInputImage( )
661 std::string err = this->_LoadImage( this->m_Image );
664 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
667 this->m_MPRObjects->SetImage( vtk_id );
668 this->m_MPRObjects->ActivateInteractors( );
669 this->m_MPRObjects->ResetCameras( );
670 this->m_MPRObjects->RenderAll( );
672 MementoState(m_state, this->m_Image);
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_actionOpenSegmentation( )
694 if( this->m_Image.IsNull( ) )
696 QMessageBox::critical(
698 tr( "Error message" ),
699 tr( "Before reading a segmentation, first load a raw image." )
706 std::string err = this->_LoadImage( this->m_Segmentation );
709 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
712 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
713 this->m_MPRObjects->RenderAll( );
716 QMessageBox::critical(
718 tr( "Error message" ),
719 tr( "Read image does not have a valid VTK converter." )
723 QMessageBox::critical(
725 tr( "Error reading single image" ),
730 // -------------------------------------------------------------------------
732 _triggered_actionOpenInputPolyData( )
736 // Get a reader from plugins
737 TPluginFilter::Pointer reader =
738 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
740 if( reader.IsNotNull( ) )
743 if( reader->ExecConfigurationDialog( this ) )
745 // Execute and get error message, if any
746 QApplication::setOverrideCursor( Qt::WaitCursor );
747 this->setEnabled( false );
748 std::string err = reader->Update( );
749 QApplication::restoreOverrideCursor( );
750 this->setEnabled( true );
752 // Assign fresh mesh, if any
755 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
756 reader->DisconnectOutputs( );
757 err = this->_ConfigureMeshActors( );
759 QMessageBox::critical(
761 tr( "Error message" ),
766 QMessageBox::critical(
768 tr( "Error reading mesh" ),
775 QMessageBox::critical(
777 tr( "Error reading single mesh" ),
778 tr( "No suitable mesh reader found in loaded plugins." )
782 // -------------------------------------------------------------------------
784 _triggered_actionImageToImage( )
786 if( this->m_Image.IsNull( ) )
790 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
793 std::string name = action->text( ).toStdString( );
796 TPluginFilter::Pointer filter =
797 this->m_Plugins.CreateProcessObject( name );
798 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
803 QApplication::setOverrideCursor( Qt::WaitCursor );
804 this->setEnabled( false );
805 filter->SetInput( 0, this->m_Image );
806 std::string err = filter->Update( );
807 QApplication::restoreOverrideCursor( );
808 this->setEnabled( true );
813 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
814 result->DisconnectPipeline( );
815 this->m_Image = result;
816 if( this->m_Image.IsNotNull( ) )
817 this->m_MPRObjects->SetImage(
818 this->m_Image->GetVTK< vtkImageData >( )
823 MementoState(this->m_state, this->m_Image);
825 if (this->m_state > this->m_max_state)
827 this->m_max_state = this->m_state;
831 QMessageBox::critical(
833 tr( "Error executing filter" ),
838 // -------------------------------------------------------------------------
840 _triggered_actionImageToMesh( )
842 if( this->m_Image.IsNull( ) )
846 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
849 std::string name = action->text( ).toStdString( );
852 TPluginFilter::Pointer filter =
853 this->m_Plugins.CreateProcessObject( name );
854 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
859 QApplication::setOverrideCursor( Qt::WaitCursor );
860 this->setEnabled( false );
861 filter->SetInput( 0, this->m_Image );
862 std::string err = filter->Update( );
863 QApplication::restoreOverrideCursor( );
864 this->setEnabled( true );
869 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
870 result->DisconnectPipeline( );
871 this->m_Mesh = result;
872 err = this->_ConfigureMeshActors( );
874 QMessageBox::critical(
876 tr( "Error message" ),
881 QMessageBox::critical(
883 tr( "Error executing filter" ),
888 // -------------------------------------------------------------------------
890 _triggered_actionUndo()
892 MementoState memento = MementoState();
897 this->m_MPRObjects->SetImage(
898 memento.getMemento(this->m_state)->GetOutput()
902 QMessageBox::warning(
905 tr("No history to undo")
911 // -------------------------------------------------------------------------
913 _triggered_actionRedo()
915 MementoState memento = MementoState();
916 if (this->m_state + 1 <= m_max_state)
919 this->m_MPRObjects->SetImage(
920 memento.getMemento(this->m_state)->GetOutput()
924 QMessageBox::warning(
927 tr("No history to redo")