2 #include "ui_ImageMPR.h"
6 // -------------------------------------------------------------------------
7 #define ImageMPR_ConnectAction( ACTION ) \
9 this->m_UI->a##ACTION, SIGNAL( triggered( ) ), \
10 this, SLOT( _a##ACTION( ) ) \
13 // -------------------------------------------------------------------------
15 ImageMPR( QWidget* parent )
16 : QMainWindow( parent ),
17 m_UI( new Ui::ImageMPR ),
18 m_Plugins( new TPlugins )
20 this->m_UI->setupUi( this );
21 this->m_Plugins->SetWidget( this );
24 ImageMPR_ConnectAction( OpenImage );
25 ImageMPR_ConnectAction( OpenDICOMSeries );
26 ImageMPR_ConnectAction( OpenSegmentation );
27 ImageMPR_ConnectAction( OpenPolyData );
28 ImageMPR_ConnectAction( SaveImage );
29 ImageMPR_ConnectAction( SaveSegmentation );
30 ImageMPR_ConnectAction( SavePolyData );
31 ImageMPR_ConnectAction( Undo );
32 ImageMPR_ConnectAction( Redo );
33 ImageMPR_ConnectAction( LoadPlugins );
34 ImageMPR_ConnectAction( ShowPlugins );
36 // Try to load default plugins
38 this->m_Plugins->LoadPlugins("cpPluginsIO.dll");
39 this->m_Plugins->LoadPlugins("cpPluginsBasicFilters.dll");
41 this->m_Plugins->LoadPluginsConfigurationFile("Plugins.cfg");
43 this->m_Plugins->AssociatePluginsToMenu(
44 this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) )
48 // -------------------------------------------------------------------------
53 delete this->m_Plugins;
56 // -------------------------------------------------------------------------
60 // Clear all, since we are loading the main image
61 if( this->m_Objects.size( ) > 0 )
63 this->m_UI->MPR->ClearAll( );
64 this->m_Objects.clear( );
68 // Read and show image, if possible
69 TPlugins::TImage::Pointer image;
70 if( this->m_Plugins->ReadImage( image, true ) )
72 vtkImageData* vimage = image->GetVTK< vtkImageData >( );
74 QMessageBox::critical(
76 QMessageBox::tr( "Error showing image." ),
78 "Image was read, but no valid VTK conversion was found."
82 this->m_UI->MPR->ShowImage( vimage, image->GetName( ) );
84 // Keep a track on a local data tree
85 this->m_Objects[ image->GetName( ) ] =
86 TTreeNode( "", image.GetPointer( ) );
91 // -------------------------------------------------------------------------
95 // Clear all, since we are loading the main image
96 if( this->m_Objects.size( ) > 0 )
98 this->m_UI->MPR->ClearAll( );
99 this->m_Objects.clear( );
103 // Read and show image, if possible
104 TPlugins::TImage::Pointer image;
105 if( this->m_Plugins->ReadDicomSeries( image ) )
107 vtkImageData* vimage = image->GetVTK< vtkImageData >( );
109 QMessageBox::critical(
111 QMessageBox::tr( "Error showing image." ),
113 "Image was read, but no valid VTK conversion was found."
117 this->m_UI->MPR->ShowImage( vimage, image->GetName( ) );
119 // Keep a track on a local data tree
120 this->m_Objects[ image->GetName( ) ] =
121 TTreeNode( "", image.GetPointer( ) );
126 // -------------------------------------------------------------------------
128 _aOpenSegmentation( )
131 if( this->m_ImageLoaded != "" )
132 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
136 // -------------------------------------------------------------------------
142 // -------------------------------------------------------------------------
146 std::string data_name = this->m_UI->MPR->GetSelectedData( );
147 TPlugins::TImage* image = dynamic_cast< TPlugins::TImage* >(
148 this->m_Objects[ data_name ].second.GetPointer( )
152 this->m_Plugins->WriteImage( image, true );
155 // -------------------------------------------------------------------------
157 _aSaveSegmentation( )
161 // -------------------------------------------------------------------------
165 std::string data_name = this->m_UI->MPR->GetSelectedData( );
166 TPlugins::TMesh* mesh = dynamic_cast< TPlugins::TMesh* >(
167 this->m_Objects[ data_name ].second.GetPointer( )
171 this->m_Plugins->WriteMesh( mesh, true );
174 // -------------------------------------------------------------------------
180 // -------------------------------------------------------------------------
186 // -------------------------------------------------------------------------
190 this->m_Plugins->DialogLoadPlugins( );
191 this->m_Plugins->AssociatePluginsToMenu(
192 this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) )
196 // -------------------------------------------------------------------------
202 // -------------------------------------------------------------------------
207 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
210 QMenu* menu = dynamic_cast< QMenu* >( action->parentWidget( ) );
213 std::string filter_cate = menu->title( ).toStdString( );
214 std::string filter_name = action->text( ).toStdString( );
217 TPlugins::TProcessObject::Pointer filter;
218 if( !( this->m_Plugins->CreateFilter( filter, filter_name ) ) )
220 QMessageBox::critical(
222 tr( "Error creating filter" ),
224 std::string( "No valid filter \"" ) +
226 std::string( "\"defined." )
234 if( !( filter->ExecConfigurationDialog( this ) ) )
238 std::string data_name = this->m_UI->MPR->GetSelectedData( );
239 std::vector< std::string > inputs_names = filter->GetInputsNames( );
240 if( inputs_names.size( ) == 1 )
242 TTree::iterator iIt = this->m_Objects.find( data_name );
243 if( iIt == this->m_Objects.end( ) )
245 QMessageBox::critical(
247 tr( "Error configuring filter" ),
248 tr( "No valid input found. Please select a valid input." )
253 filter->SetInput( inputs_names[ 0 ], iIt->second.second );
255 else if( inputs_names.size( ) > 1 )
257 QMessageBox::critical(
259 tr( "Error executing" ),
260 tr( "Filter has multiple inputs: NOT YET IMPLEMENTED!!!" )
268 std::string filter_err = filter->Update( );
270 if( filter_err != "" )
272 QMessageBox::critical(
274 tr( "Error executing" ),
275 tr( filter_err.c_str( ) )
282 std::vector< std::string > outputs_names = filter->GetOutputsNames( );
284 auto oIt = outputs_names.begin( );
285 oIt != outputs_names.end( );
289 std::string out_name = filter_name + "_" + *oIt;
291 TPlugins::TImage* image = filter->GetOutput< TPlugins::TImage >( *oIt );
294 if( filter_cate == "ImageToBinaryImageFilter" )
296 this->m_UI->MPR->ShowImage(
297 image->GetVTK< vtkImageData >( ),
302 else if( filter_cate == "ImageToImageFilter" )
306 // Keep a track on a local data tree and go to next output
307 this->m_Objects[ out_name ] = TTreeNode( data_name, image );
312 TPlugins::TMesh* mesh = filter->GetOutput< TPlugins::TMesh >( *oIt );
317 this->m_UI->MPR->ShowMesh(
318 mesh->GetVTK< vtkPolyData >( ),
324 // Keep a track on a local data tree and go to next output
325 this->m_Objects[ out_name ] = TTreeNode( data_name, mesh );
333 // -------------------------------------------------------------------------
336 _CursorCommand( double* pos, int axis, void* data )
338 Self* app = reinterpret_cast< Self* >( data );
341 if( !( app->m_Flooding ) )
344 cpPlugins::Interface::ProcessObject::Pointer filter =
345 app->m_UI->MPR->CreateFilter(
346 "cpPlugins::BasicFilters::FloodFillImageFilter"
348 if( filter.IsNull( ) )
351 cpPlugins::Interface::Parameters* params = filter->GetParameters( );
352 params->SetPoint( "Seed", 3, pos );
353 params->SetReal( "Window", app->m_UI->MPR->GetWindow( ) );
354 params->SetReal( "Level", app->m_UI->MPR->GetLevel( ) );
355 params->SetUint( "InsideValue", 1 );
356 params->SetUint( "OutsideValue", 0 );
357 filter->SetInput( "Input", app->m_UI->MPR->GetImage( app->m_ImageLoaded ) );
358 app->m_UI->MPR->Block( );
359 std::string err = filter->Update( );
360 cpPlugins::Interface::BaseMPRWindow::TImage::Pointer image = filter->GetOutput< cpPlugins::Interface::BaseMPRWindow::TImage >( "Output" );
361 filter->DisconnectOutputs( );
362 app->m_UI->MPR->AddImage( "Segmentation", image );
363 app->m_UI->MPR->Unblock( );
366 << "CursorCommand ==> "
371 << data << std::endl;
376 #include "MementoState.h"
378 #include <vtkProperty.h>
379 #include <vtkRenderWindow.h>
380 #include <vtkMetaImageReader.h>
382 #include <QFileDialog>
383 #include <QMessageBox>
386 # define PLUGIN_PREFIX ""
387 # define PLUGIN_EXT "dll"
388 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
390 # define PLUGIN_PREFIX "lib"
391 # define PLUGIN_EXT "so"
392 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
395 // -------------------------------------------------------------------------
396 ImageMPR::ImageMPR( QWidget* parent )
397 : QMainWindow( parent ),
398 m_UI( new Ui::ImageMPR ),
399 m_ImageReaderClass( "" ),
400 m_ImageWriterClass( "" ),
401 m_MeshReaderClass( "" ),
402 m_MeshWriterClass( "" ),
403 m_MeshCutterClass( "" ),
408 this->m_UI->setupUi( this );
410 // Create and associate renderers
411 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
412 this->m_MPRObjects->SetRenderWindows(
413 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
414 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
415 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
416 this->m_UI->m_3DVTK->GetRenderWindow( )
421 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
422 this, SLOT( _triggered_actionOpenPlugins( ) )
425 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
426 this, SLOT( _triggered_actionOpenInputImage( ) )
429 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
430 this, SLOT( _triggered_actionOpenSegmentation( ) )
433 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
434 this, SLOT( _triggered_actionOpenInputPolyData( ) )
437 this->m_UI->actionUndo, SIGNAL(triggered()),
438 this, SLOT(_triggered_actionUndo())
441 this->m_UI->actionRedo, SIGNAL(triggered()),
442 this, SLOT(_triggered_actionRedo())
445 // Start: load all disponible plugins
447 std::string( PLUGIN_PREFIX ) +
448 std::string( "cpPluginsIO." ) +
449 std::string( PLUGIN_EXT )
452 std::string( PLUGIN_PREFIX ) +
453 std::string( "cpPluginsBasicFilters." ) +
454 std::string( PLUGIN_EXT )
458 // -------------------------------------------------------------------------
462 // Close all connections
463 this->m_Plugins.UnloadAll( );
469 // -------------------------------------------------------------------------
471 _LoadPlugins( const std::string& filename )
473 QApplication::setOverrideCursor( Qt::WaitCursor );
474 this->setEnabled( false );
476 this->m_ImageReaderClass = "";
477 this->m_ImageWriterClass = "";
478 this->m_MeshReaderClass = "";
479 this->m_MeshWriterClass = "";
480 this->m_MeshCutterClass = "";
481 this->m_UI->MenuImageToImage->clear( );
482 this->m_UI->MenuImageToMesh->clear( );
484 if( !( this->m_Plugins.Load( filename ) ) )
487 typedef TPluginsInterface::TClasses _TClasses;
488 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
489 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
491 TPluginFilter::Pointer o =
492 this->m_Plugins.CreateProcessObject( cIt->first );
493 std::string name = o->GetClassName( );
494 std::string category = o->GetClassCategory( );
495 if( category == "ImageReader" )
496 this->m_ImageReaderClass = name;
497 else if( category == "ImageWriter" )
498 this->m_ImageWriterClass = name;
499 else if( category == "MeshReader" )
500 this->m_MeshReaderClass = name;
501 else if( category == "MeshWriter" )
502 this->m_MeshWriterClass = name;
503 else if( category == "MeshToMeshFilter" )
505 if( name.find_last_of( "Cutter" ) != std::string::npos )
506 this->m_MeshCutterClass = name;
508 else if( category == "ImageToImageFilter" )
511 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
513 action, SIGNAL( triggered( ) ),
514 this, SLOT( _triggered_actionImageToImage( ) )
517 else if( category == "ImageToMeshFilter" )
520 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
522 action, SIGNAL( triggered( ) ),
523 this, SLOT( _triggered_actionImageToMesh( ) )
529 QApplication::restoreOverrideCursor( );
530 this->setEnabled( true );
535 // -------------------------------------------------------------------------
536 std::string ImageMPR::
537 _LoadImage( TPluginImage::Pointer& image )
539 std::string ret = "";
542 // Get a reader from loaded plugins
543 TPluginFilter::Pointer reader =
544 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
545 if( reader.IsNotNull( ) )
547 if( reader->ExecConfigurationDialog( this ) )
550 QApplication::setOverrideCursor( Qt::WaitCursor );
551 this->setEnabled( false );
553 // Execute and get error message, if any
554 ret = reader->Update( );
556 // Assign fresh image, if any
559 image = reader->GetOutput< TPluginImage >( 0 );
560 reader->DisconnectOutputs( );
564 // Unblock application
565 QApplication::restoreOverrideCursor( );
566 this->setEnabled( true );
571 ret = "No suitable reader object found in loaded plugins.";
576 // -------------------------------------------------------------------------
577 std::string ImageMPR::
578 _ConfigureMeshActors( )
580 if( this->m_Mesh.IsNull( ) )
581 return( "Valid mesh not found." );
583 this->m_Mesh->CreateVTKActor( );
584 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
585 if( vtk_actor != NULL )
587 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
588 this->m_MPRObjects->Render( 4 );
592 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
594 std::string err = "";
595 for( unsigned int i = 0; i < 3; ++i )
597 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
598 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
599 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
600 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
601 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
602 std::string lerr = this->m_Cutters[ i ]->Update( );
605 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
606 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
607 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
609 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
611 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
613 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
619 this->m_MPRObjects->RenderAll( );
623 // -------------------------------------------------------------------------
625 _triggered_actionOpenPlugins( )
627 // Show dialog and check if it was accepted
628 QFileDialog dialog( this );
629 dialog.setFileMode( QFileDialog::ExistingFile );
630 dialog.setDirectory( "." );
631 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
632 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
633 if( !( dialog.exec( ) ) )
636 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
637 if( !( _LoadPlugins( fname ) ) )
638 QMessageBox::critical(
640 tr( "Ignoring plugin" ),
645 // -------------------------------------------------------------------------
647 _triggered_actionOpenInputImage( )
650 std::string err = this->_LoadImage( this->m_Image );
653 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
656 this->m_MPRObjects->SetImage( vtk_id );
657 this->m_MPRObjects->ActivateInteractors( );
658 this->m_MPRObjects->ResetCameras( );
659 this->m_MPRObjects->RenderAll( );
661 MementoState(m_state, this->m_Image);
665 QMessageBox::critical(
667 tr( "Error message" ),
668 tr( "Read image does not have a valid VTK converter." )
672 QMessageBox::critical(
674 tr( "Error reading single image" ),
679 // -------------------------------------------------------------------------
681 _triggered_actionOpenSegmentation( )
683 if( this->m_Image.IsNull( ) )
685 QMessageBox::critical(
687 tr( "Error message" ),
688 tr( "Before reading a segmentation, first load a raw image." )
695 std::string err = this->_LoadImage( this->m_Segmentation );
698 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
701 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
702 this->m_MPRObjects->RenderAll( );
705 QMessageBox::critical(
707 tr( "Error message" ),
708 tr( "Read image does not have a valid VTK converter." )
712 QMessageBox::critical(
714 tr( "Error reading single image" ),
719 // -------------------------------------------------------------------------
721 _triggered_actionOpenInputPolyData( )
725 // Get a reader from plugins
726 TPluginFilter::Pointer reader =
727 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
729 if( reader.IsNotNull( ) )
732 if( reader->ExecConfigurationDialog( this ) )
734 // Execute and get error message, if any
735 QApplication::setOverrideCursor( Qt::WaitCursor );
736 this->setEnabled( false );
737 std::string err = reader->Update( );
738 QApplication::restoreOverrideCursor( );
739 this->setEnabled( true );
741 // Assign fresh mesh, if any
744 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
745 reader->DisconnectOutputs( );
746 err = this->_ConfigureMeshActors( );
748 QMessageBox::critical(
750 tr( "Error message" ),
755 QMessageBox::critical(
757 tr( "Error reading mesh" ),
764 QMessageBox::critical(
766 tr( "Error reading single mesh" ),
767 tr( "No suitable mesh reader found in loaded plugins." )
771 // -------------------------------------------------------------------------
773 _triggered_actionImageToImage( )
775 if( this->m_Image.IsNull( ) )
779 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
782 std::string name = action->text( ).toStdString( );
785 TPluginFilter::Pointer filter =
786 this->m_Plugins.CreateProcessObject( name );
787 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
792 QApplication::setOverrideCursor( Qt::WaitCursor );
793 this->setEnabled( false );
794 filter->SetInput( 0, this->m_Image );
795 std::string err = filter->Update( );
796 QApplication::restoreOverrideCursor( );
797 this->setEnabled( true );
802 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
803 result->DisconnectPipeline( );
804 this->m_Image = result;
805 if( this->m_Image.IsNotNull( ) )
806 this->m_MPRObjects->SetImage(
807 this->m_Image->GetVTK< vtkImageData >( )
812 MementoState(this->m_state, this->m_Image);
814 if (this->m_state > this->m_max_state)
816 this->m_max_state = this->m_state;
820 QMessageBox::critical(
822 tr( "Error executing filter" ),
827 // -------------------------------------------------------------------------
829 _triggered_actionImageToMesh( )
831 if( this->m_Image.IsNull( ) )
835 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
838 std::string name = action->text( ).toStdString( );
841 TPluginFilter::Pointer filter =
842 this->m_Plugins.CreateProcessObject( name );
843 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
848 QApplication::setOverrideCursor( Qt::WaitCursor );
849 this->setEnabled( false );
850 filter->SetInput( 0, this->m_Image );
851 std::string err = filter->Update( );
852 QApplication::restoreOverrideCursor( );
853 this->setEnabled( true );
858 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
859 result->DisconnectPipeline( );
860 this->m_Mesh = result;
861 err = this->_ConfigureMeshActors( );
863 QMessageBox::critical(
865 tr( "Error message" ),
870 QMessageBox::critical(
872 tr( "Error executing filter" ),
877 // -------------------------------------------------------------------------
879 _triggered_actionUndo()
881 MementoState memento = MementoState();
886 this->m_MPRObjects->SetImage(
887 memento.getMemento(this->m_state)->GetOutput()
891 QMessageBox::warning(
894 tr("No history to undo")
900 // -------------------------------------------------------------------------
902 _triggered_actionRedo()
904 MementoState memento = MementoState();
905 if (this->m_state + 1 <= m_max_state)
908 this->m_MPRObjects->SetImage(
909 memento.getMemento(this->m_state)->GetOutput()
913 QMessageBox::warning(
916 tr("No history to redo")