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 ),
21 this->m_UI->setupUi( this );
22 this->m_Plugins->SetWidget( 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 // Try to load default plugins
38 this->m_Plugins->LoadPluginsConfigurationFile( "Plugins.cfg" );
39 this->m_Plugins->AssociatePluginsToMenu(
40 this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) )
44 // -------------------------------------------------------------------------
49 delete this->m_Plugins;
52 // -------------------------------------------------------------------------
56 if( this->m_MainImage.IsNotNull( ) )
57 this->m_UI->MPR->ClearAll( );
58 if( this->m_Plugins->ReadImage( this->m_MainImage, true ) )
60 vtkImageData* vimage = this->m_MainImage->GetVTK< vtkImageData >( );
62 QMessageBox::critical(
64 QMessageBox::tr( "Error showing image." ),
66 "Image was read, but no valid VTK conversion was found."
70 this->m_UI->MPR->ShowImage( vimage );
75 // -------------------------------------------------------------------------
79 if( this->m_MainImage.IsNotNull( ) )
80 this->m_UI->MPR->ClearAll( );
81 if( this->m_Plugins->ReadDicomSeries( this->m_MainImage ) )
83 vtkImageData* vimage = this->m_MainImage->GetVTK< vtkImageData >( );
85 QMessageBox::critical(
87 QMessageBox::tr( "Error showing image." ),
89 "Image was read, but no valid VTK conversion was found."
93 this->m_UI->MPR->ShowImage( vimage );
98 // -------------------------------------------------------------------------
100 _aOpenSegmentation( )
103 if( this->m_ImageLoaded != "" )
104 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
108 // -------------------------------------------------------------------------
114 // -------------------------------------------------------------------------
118 if( this->m_MainImage.IsNotNull( ) )
119 this->m_Plugins->WriteImage( this->m_MainImage, true );
122 // -------------------------------------------------------------------------
124 _aSaveSegmentation( )
128 // -------------------------------------------------------------------------
134 // -------------------------------------------------------------------------
140 // -------------------------------------------------------------------------
146 // -------------------------------------------------------------------------
150 this->m_Plugins->DialogLoadPlugins( );
151 this->m_Plugins->AssociatePluginsToMenu(
152 this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) )
156 // -------------------------------------------------------------------------
162 // -------------------------------------------------------------------------
167 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
170 QMenu* menu = dynamic_cast< QMenu* >( action->parentWidget( ) );
173 std::string cate = menu->title( ).toStdString( );
174 std::string name = action->text( ).toStdString( );
175 std::string err = "";
177 TPlugins::TProcessObject::Pointer filter;
178 if( this->m_Plugins->CreateFilter( filter, name ) )
180 if( cate == "ImageToMeshFilter" )
182 if( filter->ExecConfigurationDialog( this ) )
184 filter->SetInput( "Input", this->m_MainImage );
186 err = filter->Update( );
188 TPlugins::TMesh::Pointer mesh =
189 filter->GetOutput< TPlugins::TMesh >( "Output" );
190 this->m_Meshes.push_back( mesh );
191 mesh->CreateVTKActor( );
192 vtkActor* actor = mesh->GetVTKActor( );
194 this->m_UI->MPR->Add3DActor( actor );
201 QMessageBox::critical(
203 tr( "Error creating filter" ),
204 tr( "No valid filter defined." )
208 if( name == "cpPlugins::BasicFilters::FloodFillImageFilter" )
210 this->m_Flooding = true;
214 this->m_Flooding = false;
215 this->m_UI->MPR->ExecuteFilter(
216 name, this->m_ImageLoaded, "SegmentedImage"
224 TPluginFilter::Pointer filter =
225 this->m_Plugins.CreateProcessObject( name );
226 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
231 QApplication::setOverrideCursor( Qt::WaitCursor );
232 this->setEnabled( false );
233 filter->SetInput( 0, this->m_Image );
234 std::string err = filter->Update( );
235 QApplication::restoreOverrideCursor( );
236 this->setEnabled( true );
241 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
242 result->DisconnectPipeline( );
243 this->m_Image = result;
244 if( this->m_Image.IsNotNull( ) )
245 this->m_MPRObjects->SetImage(
246 this->m_Image->GetVTK< vtkImageData >( )
248 MementoState(this->m_state, this->m_Image);
250 if (this->m_state > this->m_max_state)
252 this->m_max_state = this->m_state;
256 QMessageBox::critical(
258 tr( "Error executing filter" ),
264 // -------------------------------------------------------------------------
267 _CursorCommand( double* pos, int axis, void* data )
269 Self* app = reinterpret_cast< Self* >( data );
272 if( !( app->m_Flooding ) )
275 cpPlugins::Interface::ProcessObject::Pointer filter =
276 app->m_UI->MPR->CreateFilter(
277 "cpPlugins::BasicFilters::FloodFillImageFilter"
279 if( filter.IsNull( ) )
282 cpPlugins::Interface::Parameters* params = filter->GetParameters( );
283 params->SetPoint( "Seed", 3, pos );
284 params->SetReal( "Window", app->m_UI->MPR->GetWindow( ) );
285 params->SetReal( "Level", app->m_UI->MPR->GetLevel( ) );
286 params->SetUint( "InsideValue", 1 );
287 params->SetUint( "OutsideValue", 0 );
288 filter->SetInput( "Input", app->m_UI->MPR->GetImage( app->m_ImageLoaded ) );
289 app->m_UI->MPR->Block( );
290 std::string err = filter->Update( );
291 cpPlugins::Interface::BaseMPRWindow::TImage::Pointer image = filter->GetOutput< cpPlugins::Interface::BaseMPRWindow::TImage >( "Output" );
292 filter->DisconnectOutputs( );
293 app->m_UI->MPR->AddImage( "Segmentation", image );
294 app->m_UI->MPR->Unblock( );
297 << "CursorCommand ==> "
302 << data << std::endl;
307 #include "MementoState.h"
309 #include <vtkProperty.h>
310 #include <vtkRenderWindow.h>
311 #include <vtkMetaImageReader.h>
313 #include <QFileDialog>
314 #include <QMessageBox>
317 # define PLUGIN_PREFIX ""
318 # define PLUGIN_EXT "dll"
319 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
321 # define PLUGIN_PREFIX "lib"
322 # define PLUGIN_EXT "so"
323 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
326 // -------------------------------------------------------------------------
327 ImageMPR::ImageMPR( QWidget* parent )
328 : QMainWindow( parent ),
329 m_UI( new Ui::ImageMPR ),
330 m_ImageReaderClass( "" ),
331 m_ImageWriterClass( "" ),
332 m_MeshReaderClass( "" ),
333 m_MeshWriterClass( "" ),
334 m_MeshCutterClass( "" ),
339 this->m_UI->setupUi( this );
341 // Create and associate renderers
342 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
343 this->m_MPRObjects->SetRenderWindows(
344 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
345 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
346 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
347 this->m_UI->m_3DVTK->GetRenderWindow( )
352 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
353 this, SLOT( _triggered_actionOpenPlugins( ) )
356 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
357 this, SLOT( _triggered_actionOpenInputImage( ) )
360 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
361 this, SLOT( _triggered_actionOpenSegmentation( ) )
364 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
365 this, SLOT( _triggered_actionOpenInputPolyData( ) )
368 this->m_UI->actionUndo, SIGNAL(triggered()),
369 this, SLOT(_triggered_actionUndo())
372 this->m_UI->actionRedo, SIGNAL(triggered()),
373 this, SLOT(_triggered_actionRedo())
376 // Start: load all disponible plugins
378 std::string( PLUGIN_PREFIX ) +
379 std::string( "cpPluginsIO." ) +
380 std::string( PLUGIN_EXT )
383 std::string( PLUGIN_PREFIX ) +
384 std::string( "cpPluginsBasicFilters." ) +
385 std::string( PLUGIN_EXT )
389 // -------------------------------------------------------------------------
393 // Close all connections
394 this->m_Plugins.UnloadAll( );
400 // -------------------------------------------------------------------------
402 _LoadPlugins( const std::string& filename )
404 QApplication::setOverrideCursor( Qt::WaitCursor );
405 this->setEnabled( false );
407 this->m_ImageReaderClass = "";
408 this->m_ImageWriterClass = "";
409 this->m_MeshReaderClass = "";
410 this->m_MeshWriterClass = "";
411 this->m_MeshCutterClass = "";
412 this->m_UI->MenuImageToImage->clear( );
413 this->m_UI->MenuImageToMesh->clear( );
415 if( !( this->m_Plugins.Load( filename ) ) )
418 typedef TPluginsInterface::TClasses _TClasses;
419 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
420 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
422 TPluginFilter::Pointer o =
423 this->m_Plugins.CreateProcessObject( cIt->first );
424 std::string name = o->GetClassName( );
425 std::string category = o->GetClassCategory( );
426 if( category == "ImageReader" )
427 this->m_ImageReaderClass = name;
428 else if( category == "ImageWriter" )
429 this->m_ImageWriterClass = name;
430 else if( category == "MeshReader" )
431 this->m_MeshReaderClass = name;
432 else if( category == "MeshWriter" )
433 this->m_MeshWriterClass = name;
434 else if( category == "MeshToMeshFilter" )
436 if( name.find_last_of( "Cutter" ) != std::string::npos )
437 this->m_MeshCutterClass = name;
439 else if( category == "ImageToImageFilter" )
442 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
444 action, SIGNAL( triggered( ) ),
445 this, SLOT( _triggered_actionImageToImage( ) )
448 else if( category == "ImageToMeshFilter" )
451 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
453 action, SIGNAL( triggered( ) ),
454 this, SLOT( _triggered_actionImageToMesh( ) )
460 QApplication::restoreOverrideCursor( );
461 this->setEnabled( true );
466 // -------------------------------------------------------------------------
467 std::string ImageMPR::
468 _LoadImage( TPluginImage::Pointer& image )
470 std::string ret = "";
473 // Get a reader from loaded plugins
474 TPluginFilter::Pointer reader =
475 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
476 if( reader.IsNotNull( ) )
478 if( reader->ExecConfigurationDialog( this ) )
481 QApplication::setOverrideCursor( Qt::WaitCursor );
482 this->setEnabled( false );
484 // Execute and get error message, if any
485 ret = reader->Update( );
487 // Assign fresh image, if any
490 image = reader->GetOutput< TPluginImage >( 0 );
491 reader->DisconnectOutputs( );
495 // Unblock application
496 QApplication::restoreOverrideCursor( );
497 this->setEnabled( true );
502 ret = "No suitable reader object found in loaded plugins.";
507 // -------------------------------------------------------------------------
508 std::string ImageMPR::
509 _ConfigureMeshActors( )
511 if( this->m_Mesh.IsNull( ) )
512 return( "Valid mesh not found." );
514 this->m_Mesh->CreateVTKActor( );
515 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
516 if( vtk_actor != NULL )
518 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
519 this->m_MPRObjects->Render( 4 );
523 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
525 std::string err = "";
526 for( unsigned int i = 0; i < 3; ++i )
528 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
529 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
530 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
531 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
532 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
533 std::string lerr = this->m_Cutters[ i ]->Update( );
536 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
537 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
538 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
540 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
542 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
544 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
550 this->m_MPRObjects->RenderAll( );
554 // -------------------------------------------------------------------------
556 _triggered_actionOpenPlugins( )
558 // Show dialog and check if it was accepted
559 QFileDialog dialog( this );
560 dialog.setFileMode( QFileDialog::ExistingFile );
561 dialog.setDirectory( "." );
562 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
563 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
564 if( !( dialog.exec( ) ) )
567 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
568 if( !( _LoadPlugins( fname ) ) )
569 QMessageBox::critical(
571 tr( "Ignoring plugin" ),
576 // -------------------------------------------------------------------------
578 _triggered_actionOpenInputImage( )
581 std::string err = this->_LoadImage( this->m_Image );
584 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
587 this->m_MPRObjects->SetImage( vtk_id );
588 this->m_MPRObjects->ActivateInteractors( );
589 this->m_MPRObjects->ResetCameras( );
590 this->m_MPRObjects->RenderAll( );
592 MementoState(m_state, this->m_Image);
596 QMessageBox::critical(
598 tr( "Error message" ),
599 tr( "Read image does not have a valid VTK converter." )
603 QMessageBox::critical(
605 tr( "Error reading single image" ),
610 // -------------------------------------------------------------------------
612 _triggered_actionOpenSegmentation( )
614 if( this->m_Image.IsNull( ) )
616 QMessageBox::critical(
618 tr( "Error message" ),
619 tr( "Before reading a segmentation, first load a raw image." )
626 std::string err = this->_LoadImage( this->m_Segmentation );
629 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
632 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
633 this->m_MPRObjects->RenderAll( );
636 QMessageBox::critical(
638 tr( "Error message" ),
639 tr( "Read image does not have a valid VTK converter." )
643 QMessageBox::critical(
645 tr( "Error reading single image" ),
650 // -------------------------------------------------------------------------
652 _triggered_actionOpenInputPolyData( )
656 // Get a reader from plugins
657 TPluginFilter::Pointer reader =
658 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
660 if( reader.IsNotNull( ) )
663 if( reader->ExecConfigurationDialog( this ) )
665 // Execute and get error message, if any
666 QApplication::setOverrideCursor( Qt::WaitCursor );
667 this->setEnabled( false );
668 std::string err = reader->Update( );
669 QApplication::restoreOverrideCursor( );
670 this->setEnabled( true );
672 // Assign fresh mesh, if any
675 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
676 reader->DisconnectOutputs( );
677 err = this->_ConfigureMeshActors( );
679 QMessageBox::critical(
681 tr( "Error message" ),
686 QMessageBox::critical(
688 tr( "Error reading mesh" ),
695 QMessageBox::critical(
697 tr( "Error reading single mesh" ),
698 tr( "No suitable mesh reader found in loaded plugins." )
702 // -------------------------------------------------------------------------
704 _triggered_actionImageToImage( )
706 if( this->m_Image.IsNull( ) )
710 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
713 std::string name = action->text( ).toStdString( );
716 TPluginFilter::Pointer filter =
717 this->m_Plugins.CreateProcessObject( name );
718 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
723 QApplication::setOverrideCursor( Qt::WaitCursor );
724 this->setEnabled( false );
725 filter->SetInput( 0, this->m_Image );
726 std::string err = filter->Update( );
727 QApplication::restoreOverrideCursor( );
728 this->setEnabled( true );
733 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
734 result->DisconnectPipeline( );
735 this->m_Image = result;
736 if( this->m_Image.IsNotNull( ) )
737 this->m_MPRObjects->SetImage(
738 this->m_Image->GetVTK< vtkImageData >( )
743 MementoState(this->m_state, this->m_Image);
745 if (this->m_state > this->m_max_state)
747 this->m_max_state = this->m_state;
751 QMessageBox::critical(
753 tr( "Error executing filter" ),
758 // -------------------------------------------------------------------------
760 _triggered_actionImageToMesh( )
762 if( this->m_Image.IsNull( ) )
766 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
769 std::string name = action->text( ).toStdString( );
772 TPluginFilter::Pointer filter =
773 this->m_Plugins.CreateProcessObject( name );
774 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
779 QApplication::setOverrideCursor( Qt::WaitCursor );
780 this->setEnabled( false );
781 filter->SetInput( 0, this->m_Image );
782 std::string err = filter->Update( );
783 QApplication::restoreOverrideCursor( );
784 this->setEnabled( true );
789 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
790 result->DisconnectPipeline( );
791 this->m_Mesh = result;
792 err = this->_ConfigureMeshActors( );
794 QMessageBox::critical(
796 tr( "Error message" ),
801 QMessageBox::critical(
803 tr( "Error executing filter" ),
808 // -------------------------------------------------------------------------
810 _triggered_actionUndo()
812 MementoState memento = MementoState();
817 this->m_MPRObjects->SetImage(
818 memento.getMemento(this->m_state)->GetOutput()
822 QMessageBox::warning(
825 tr("No history to undo")
831 // -------------------------------------------------------------------------
833 _triggered_actionRedo()
835 MementoState memento = MementoState();
836 if (this->m_state + 1 <= m_max_state)
839 this->m_MPRObjects->SetImage(
840 memento.getMemento(this->m_state)->GetOutput()
844 QMessageBox::warning(
847 tr("No history to redo")