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 );
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
43 this->m_Plugins->LoadPlugins( "cpPluginsIO.dll" );
44 this->m_Plugins->LoadPlugins( "cpPluginsBasicFilters.dll" );
46 this->m_Plugins->LoadPluginsConfigurationFile( "Plugins.cfg" );
48 this->m_Plugins->AssociatePluginsToMenu(
49 this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) )
53 // -------------------------------------------------------------------------
58 delete this->m_Plugins;
61 // -------------------------------------------------------------------------
65 if( !( this->m_Plugins->HasActiveFilter( ) ) )
68 std::vector< std::string > outputs;
69 std::string err = this->m_Plugins->UpdateActiveFilter( outputs );
72 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
74 TPlugins::TImage* image = this->m_Plugins->GetImage( *oIt );
77 vtkImageData* vimage = image->GetVTK< vtkImageData >( );
80 this->m_UI->MPR->AddImage(
81 vimage, *oIt, this->m_Plugins->GetParent( *oIt )
83 this->m_UI->MPR->ShowData( *oIt );
90 TPlugins::TMesh* mesh = this->m_Plugins->GetMesh( *oIt );
93 this->m_Plugins->BlockWidget( );
94 this->m_UI->MPR->AddMesh(
95 mesh->GetVTK< vtkPolyData >( ),
97 this->m_Plugins->GetParent( *oIt )
99 this->m_UI->MPR->ShowData( *oIt );
100 this->m_Plugins->UnblockWidget( );
108 QMessageBox::critical(
110 tr( "Error executing filter" ),
111 tr( ( std::string( "Error caught: " ) + err ).c_str( ) )
118 // -------------------------------------------------------------------------
122 std::string name = this->m_Plugins->ReadImage( "" );
125 // Since we are opening an image, clear view
126 this->m_UI->MPR->DeleteAllData( );
128 // Prepare visualization
129 TPlugins::TImage* image = this->m_Plugins->GetImage( name );
130 vtkImageData* vimage = image->GetVTK< vtkImageData >( );
133 this->m_UI->MPR->AddImage( vimage, name );
134 this->m_UI->MPR->ShowData( name );
137 QMessageBox::critical(
139 QMessageBox::tr( "Error showing image." ),
141 "Image was read, but no valid VTK conversion was found."
148 // -------------------------------------------------------------------------
152 std::string name = this->m_Plugins->ReadDicomSeries( "" );
155 // Since we are opening an image, clear view
156 this->m_UI->MPR->DeleteAllData( );
158 // Prepare visualization
159 TPlugins::TImage* image = this->m_Plugins->GetImage( name );
160 vtkImageData* vimage = image->GetVTK< vtkImageData >( );
163 this->m_UI->MPR->AddImage( vimage, name );
164 this->m_UI->MPR->ShowData( name );
167 QMessageBox::critical(
169 QMessageBox::tr( "Error showing image." ),
171 "Image was read, but no valid VTK conversion was found."
178 // -------------------------------------------------------------------------
180 _aOpenSegmentation( )
183 if( this->m_ImageLoaded != "" )
184 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
188 // -------------------------------------------------------------------------
194 // -------------------------------------------------------------------------
198 std::string data_name = this->m_UI->MPR->GetSelectedData( );
199 this->m_Plugins->WriteImage( data_name );
202 // -------------------------------------------------------------------------
204 _aSaveSegmentation( )
208 // -------------------------------------------------------------------------
214 // -------------------------------------------------------------------------
220 // -------------------------------------------------------------------------
226 // -------------------------------------------------------------------------
230 this->m_Plugins->DialogLoadPlugins( );
231 this->m_Plugins->AssociatePluginsToMenu(
232 this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) )
236 // -------------------------------------------------------------------------
242 // -------------------------------------------------------------------------
246 // Get filter's name and category
247 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
250 QMenu* menu = dynamic_cast< QMenu* >( action->parentWidget( ) );
253 std::string filter_cate = menu->title( ).toStdString( );
254 std::string filter_name = action->text( ).toStdString( );
257 if( !( this->m_Plugins->ActivateFilter( filter_name ) ) )
261 std::vector< std::string > inputs =
262 this->m_Plugins->GetActiveFilterInputsNames( );
263 if( inputs.size( ) == 1 )
265 std::string data_name = this->m_UI->MPR->GetSelectedData( );
266 this->m_Plugins->ConnectInputInActiveFilter( data_name, inputs[ 0 ] );
268 else if( inputs.size( ) > 1 )
270 QMessageBox::critical(
272 tr( "Error executing" ),
273 tr( "Filter has multiple inputs: NOT YET IMPLEMENTED!!!" )
280 std::vector< std::string > outputs =
281 this->m_Plugins->GetActiveFilterOutputsNames( );
282 for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
283 this->m_Plugins->SetOutputNameInActiveFilter(
284 filter_name + "_" + *oIt, *oIt
288 TPlugins::TProcessObject::DialogResult dlg_res =
289 this->m_Plugins->ConfigureActiveFilter( );
290 if( dlg_res == TPlugins::TProcessObject::DialogResult_Cancel )
292 this->m_Plugins->DeactivateFilter( );
297 // Execute filter and associate outputs
298 if( dlg_res == TPlugins::TProcessObject::DialogResult_NoModal )
300 this->UpdateActualFilter( );
301 this->m_Plugins->DeactivateFilter( );
306 // -------------------------------------------------------------------------
309 _CursorCommand( double* pos, int axis, void* data )
311 Self* app = reinterpret_cast< Self* >( data );
314 if( !( app->m_Flooding ) )
317 cpPlugins::Interface::ProcessObject::Pointer filter =
318 app->m_UI->MPR->CreateFilter(
319 "cpPlugins::BasicFilters::FloodFillImageFilter"
321 if( filter.IsNull( ) )
324 cpPlugins::Interface::Parameters* params = filter->GetParameters( );
325 params->SetPoint( "Seed", 3, pos );
326 params->SetReal( "Window", app->m_UI->MPR->GetWindow( ) );
327 params->SetReal( "Level", app->m_UI->MPR->GetLevel( ) );
328 params->SetUint( "InsideValue", 1 );
329 params->SetUint( "OutsideValue", 0 );
330 filter->SetInput( "Input", app->m_UI->MPR->GetImage( app->m_ImageLoaded ) );
331 app->m_UI->MPR->Block( );
332 std::string err = filter->Update( );
333 cpPlugins::Interface::BaseMPRWindow::TImage::Pointer image = filter->GetOutput< cpPlugins::Interface::BaseMPRWindow::TImage >( "Output" );
334 filter->DisconnectOutputs( );
335 app->m_UI->MPR->AddImage( "Segmentation", image );
336 app->m_UI->MPR->Unblock( );
339 << "CursorCommand ==> "
344 << data << std::endl;
349 #include "MementoState.h"
351 #include <vtkProperty.h>
352 #include <vtkRenderWindow.h>
353 #include <vtkMetaImageReader.h>
355 #include <QFileDialog>
356 #include <QMessageBox>
359 # define PLUGIN_PREFIX ""
360 # define PLUGIN_EXT "dll"
361 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
363 # define PLUGIN_PREFIX "lib"
364 # define PLUGIN_EXT "so"
365 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
368 // -------------------------------------------------------------------------
369 ImageMPR::ImageMPR( QWidget* parent )
370 : QMainWindow( parent ),
371 m_UI( new Ui::ImageMPR ),
372 m_ImageReaderClass( "" ),
373 m_ImageWriterClass( "" ),
374 m_MeshReaderClass( "" ),
375 m_MeshWriterClass( "" ),
376 m_MeshCutterClass( "" ),
381 this->m_UI->setupUi( this );
383 // Create and associate renderers
384 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
385 this->m_MPRObjects->SetRenderWindows(
386 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
387 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
388 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
389 this->m_UI->m_3DVTK->GetRenderWindow( )
394 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
395 this, SLOT( _triggered_actionOpenPlugins( ) )
398 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
399 this, SLOT( _triggered_actionOpenInputImage( ) )
402 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
403 this, SLOT( _triggered_actionOpenSegmentation( ) )
406 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
407 this, SLOT( _triggered_actionOpenInputPolyData( ) )
410 this->m_UI->actionUndo, SIGNAL(triggered()),
411 this, SLOT(_triggered_actionUndo())
414 this->m_UI->actionRedo, SIGNAL(triggered()),
415 this, SLOT(_triggered_actionRedo())
418 // Start: load all disponible plugins
420 std::string( PLUGIN_PREFIX ) +
421 std::string( "cpPluginsIO." ) +
422 std::string( PLUGIN_EXT )
425 std::string( PLUGIN_PREFIX ) +
426 std::string( "cpPluginsBasicFilters." ) +
427 std::string( PLUGIN_EXT )
431 // -------------------------------------------------------------------------
435 // Close all connections
436 this->m_Plugins.UnloadAll( );
442 // -------------------------------------------------------------------------
444 _LoadPlugins( const std::string& filename )
446 QApplication::setOverrideCursor( Qt::WaitCursor );
447 this->setEnabled( false );
449 this->m_ImageReaderClass = "";
450 this->m_ImageWriterClass = "";
451 this->m_MeshReaderClass = "";
452 this->m_MeshWriterClass = "";
453 this->m_MeshCutterClass = "";
454 this->m_UI->MenuImageToImage->clear( );
455 this->m_UI->MenuImageToMesh->clear( );
457 if( !( this->m_Plugins.Load( filename ) ) )
460 typedef TPluginsInterface::TClasses _TClasses;
461 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
462 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
464 TPluginFilter::Pointer o =
465 this->m_Plugins.CreateProcessObject( cIt->first );
466 std::string name = o->GetClassName( );
467 std::string category = o->GetClassCategory( );
468 if( category == "ImageReader" )
469 this->m_ImageReaderClass = name;
470 else if( category == "ImageWriter" )
471 this->m_ImageWriterClass = name;
472 else if( category == "MeshReader" )
473 this->m_MeshReaderClass = name;
474 else if( category == "MeshWriter" )
475 this->m_MeshWriterClass = name;
476 else if( category == "MeshToMeshFilter" )
478 if( name.find_last_of( "Cutter" ) != std::string::npos )
479 this->m_MeshCutterClass = name;
481 else if( category == "ImageToImageFilter" )
484 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
486 action, SIGNAL( triggered( ) ),
487 this, SLOT( _triggered_actionImageToImage( ) )
490 else if( category == "ImageToMeshFilter" )
493 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
495 action, SIGNAL( triggered( ) ),
496 this, SLOT( _triggered_actionImageToMesh( ) )
502 QApplication::restoreOverrideCursor( );
503 this->setEnabled( true );
508 // -------------------------------------------------------------------------
509 std::string ImageMPR::
510 _LoadImage( TPluginImage::Pointer& image )
512 std::string ret = "";
515 // Get a reader from loaded plugins
516 TPluginFilter::Pointer reader =
517 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
518 if( reader.IsNotNull( ) )
520 if( reader->ExecConfigurationDialog( this ) )
523 QApplication::setOverrideCursor( Qt::WaitCursor );
524 this->setEnabled( false );
526 // Execute and get error message, if any
527 ret = reader->Update( );
529 // Assign fresh image, if any
532 image = reader->GetOutput< TPluginImage >( 0 );
533 reader->DisconnectOutputs( );
537 // Unblock application
538 QApplication::restoreOverrideCursor( );
539 this->setEnabled( true );
544 ret = "No suitable reader object found in loaded plugins.";
549 // -------------------------------------------------------------------------
550 std::string ImageMPR::
551 _ConfigureMeshActors( )
553 if( this->m_Mesh.IsNull( ) )
554 return( "Valid mesh not found." );
556 this->m_Mesh->CreateVTKActor( );
557 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
558 if( vtk_actor != NULL )
560 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
561 this->m_MPRObjects->Render( 4 );
565 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
567 std::string err = "";
568 for( unsigned int i = 0; i < 3; ++i )
570 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
571 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
572 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
573 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
574 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
575 std::string lerr = this->m_Cutters[ i ]->Update( );
578 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
579 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
580 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
582 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
584 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
586 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
592 this->m_MPRObjects->RenderAll( );
596 // -------------------------------------------------------------------------
598 _triggered_actionOpenPlugins( )
600 // Show dialog and check if it was accepted
601 QFileDialog dialog( this );
602 dialog.setFileMode( QFileDialog::ExistingFile );
603 dialog.setDirectory( "." );
604 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
605 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
606 if( !( dialog.exec( ) ) )
609 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
610 if( !( _LoadPlugins( fname ) ) )
611 QMessageBox::critical(
613 tr( "Ignoring plugin" ),
618 // -------------------------------------------------------------------------
620 _triggered_actionOpenInputImage( )
623 std::string err = this->_LoadImage( this->m_Image );
626 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
629 this->m_MPRObjects->SetImage( vtk_id );
630 this->m_MPRObjects->ActivateInteractors( );
631 this->m_MPRObjects->ResetCameras( );
632 this->m_MPRObjects->RenderAll( );
634 MementoState(m_state, this->m_Image);
638 QMessageBox::critical(
640 tr( "Error message" ),
641 tr( "Read image does not have a valid VTK converter." )
645 QMessageBox::critical(
647 tr( "Error reading single image" ),
652 // -------------------------------------------------------------------------
654 _triggered_actionOpenSegmentation( )
656 if( this->m_Image.IsNull( ) )
658 QMessageBox::critical(
660 tr( "Error message" ),
661 tr( "Before reading a segmentation, first load a raw image." )
668 std::string err = this->_LoadImage( this->m_Segmentation );
671 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
674 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
675 this->m_MPRObjects->RenderAll( );
678 QMessageBox::critical(
680 tr( "Error message" ),
681 tr( "Read image does not have a valid VTK converter." )
685 QMessageBox::critical(
687 tr( "Error reading single image" ),
692 // -------------------------------------------------------------------------
694 _triggered_actionOpenInputPolyData( )
698 // Get a reader from plugins
699 TPluginFilter::Pointer reader =
700 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
702 if( reader.IsNotNull( ) )
705 if( reader->ExecConfigurationDialog( this ) )
707 // Execute and get error message, if any
708 QApplication::setOverrideCursor( Qt::WaitCursor );
709 this->setEnabled( false );
710 std::string err = reader->Update( );
711 QApplication::restoreOverrideCursor( );
712 this->setEnabled( true );
714 // Assign fresh mesh, if any
717 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
718 reader->DisconnectOutputs( );
719 err = this->_ConfigureMeshActors( );
721 QMessageBox::critical(
723 tr( "Error message" ),
728 QMessageBox::critical(
730 tr( "Error reading mesh" ),
737 QMessageBox::critical(
739 tr( "Error reading single mesh" ),
740 tr( "No suitable mesh reader found in loaded plugins." )
744 // -------------------------------------------------------------------------
746 _triggered_actionImageToImage( )
748 if( this->m_Image.IsNull( ) )
752 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
755 std::string name = action->text( ).toStdString( );
758 TPluginFilter::Pointer filter =
759 this->m_Plugins.CreateProcessObject( name );
760 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
765 QApplication::setOverrideCursor( Qt::WaitCursor );
766 this->setEnabled( false );
767 filter->SetInput( 0, this->m_Image );
768 std::string err = filter->Update( );
769 QApplication::restoreOverrideCursor( );
770 this->setEnabled( true );
775 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
776 result->DisconnectPipeline( );
777 this->m_Image = result;
778 if( this->m_Image.IsNotNull( ) )
779 this->m_MPRObjects->SetImage(
780 this->m_Image->GetVTK< vtkImageData >( )
785 MementoState(this->m_state, this->m_Image);
787 if (this->m_state > this->m_max_state)
789 this->m_max_state = this->m_state;
793 QMessageBox::critical(
795 tr( "Error executing filter" ),
800 // -------------------------------------------------------------------------
802 _triggered_actionImageToMesh( )
804 if( this->m_Image.IsNull( ) )
808 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
811 std::string name = action->text( ).toStdString( );
814 TPluginFilter::Pointer filter =
815 this->m_Plugins.CreateProcessObject( name );
816 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
821 QApplication::setOverrideCursor( Qt::WaitCursor );
822 this->setEnabled( false );
823 filter->SetInput( 0, this->m_Image );
824 std::string err = filter->Update( );
825 QApplication::restoreOverrideCursor( );
826 this->setEnabled( true );
831 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
832 result->DisconnectPipeline( );
833 this->m_Mesh = result;
834 err = this->_ConfigureMeshActors( );
836 QMessageBox::critical(
838 tr( "Error message" ),
843 QMessageBox::critical(
845 tr( "Error executing filter" ),
850 // -------------------------------------------------------------------------
852 _triggered_actionUndo()
854 MementoState memento = MementoState();
859 this->m_MPRObjects->SetImage(
860 memento.getMemento(this->m_state)->GetOutput()
864 QMessageBox::warning(
867 tr("No history to undo")
873 // -------------------------------------------------------------------------
875 _triggered_actionRedo()
877 MementoState memento = MementoState();
878 if (this->m_state + 1 <= m_max_state)
881 this->m_MPRObjects->SetImage(
882 memento.getMemento(this->m_state)->GetOutput()
886 QMessageBox::warning(
889 tr("No history to redo")