2 #include "ui_ImageMPR.h"
4 // -------------------------------------------------------------------------
5 #define ImageMPR_ConnectAction( ACTION ) \
7 this->m_UI->a##ACTION, SIGNAL( triggered( ) ), \
8 this, SLOT( _a##ACTION( ) ) \
11 // -------------------------------------------------------------------------
13 ImageMPR( QWidget* parent )
14 : QMainWindow( parent ),
15 m_UI( new Ui::ImageMPR ),
19 this->m_UI->setupUi( this );
22 ImageMPR_ConnectAction( OpenImage );
23 ImageMPR_ConnectAction( OpenSegmentation );
24 ImageMPR_ConnectAction( OpenPolyData );
25 ImageMPR_ConnectAction( SaveImage );
26 ImageMPR_ConnectAction( SaveSegmentation );
27 ImageMPR_ConnectAction( SavePolyData );
28 ImageMPR_ConnectAction( Undo );
29 ImageMPR_ConnectAction( Redo );
30 ImageMPR_ConnectAction( LoadPlugins );
31 ImageMPR_ConnectAction( ShowPlugins );
33 // Try to load default plugins
34 this->m_UI->MPR->LoadPlugins( );
35 this->m_UI->MPR->AssociatePluginsToMenu(
36 this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) )
40 // -------------------------------------------------------------------------
47 // -------------------------------------------------------------------------
51 if( this->m_ImageLoaded != "" )
52 this->m_UI->MPR->ClearAll( );
53 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
56 // -------------------------------------------------------------------------
60 if( this->m_ImageLoaded != "" )
61 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
64 // -------------------------------------------------------------------------
70 // -------------------------------------------------------------------------
76 // -------------------------------------------------------------------------
82 // -------------------------------------------------------------------------
88 // -------------------------------------------------------------------------
94 // -------------------------------------------------------------------------
100 // -------------------------------------------------------------------------
104 this->m_UI->MPR->DialogLoadPlugins( );
105 this->m_UI->MPR->AssociatePluginsToMenu(
106 this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) )
110 // -------------------------------------------------------------------------
116 // -------------------------------------------------------------------------
121 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
124 std::string name = action->text( ).toStdString( );
126 if( name == "cpPlugins::BasicFilters::FloodFillImageFilter" )
128 this->m_Flooding = true;
132 this->m_Flooding = false;
133 this->m_UI->MPR->ExecuteFilter(
134 name, this->m_ImageLoaded, "SegmentedImage"
141 TPluginFilter::Pointer filter =
142 this->m_Plugins.CreateProcessObject( name );
143 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
148 QApplication::setOverrideCursor( Qt::WaitCursor );
149 this->setEnabled( false );
150 filter->SetInput( 0, this->m_Image );
151 std::string err = filter->Update( );
152 QApplication::restoreOverrideCursor( );
153 this->setEnabled( true );
158 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
159 result->DisconnectPipeline( );
160 this->m_Image = result;
161 if( this->m_Image.IsNotNull( ) )
162 this->m_MPRObjects->SetImage(
163 this->m_Image->GetVTK< vtkImageData >( )
165 MementoState(this->m_state, this->m_Image);
167 if (this->m_state > this->m_max_state)
169 this->m_max_state = this->m_state;
173 QMessageBox::critical(
175 tr( "Error executing filter" ),
181 // -------------------------------------------------------------------------
184 _CursorCommand( double* pos, int axis, void* data )
186 Self* app = reinterpret_cast< Self* >( data );
189 if( !( app->m_Flooding ) )
192 cpPlugins::Interface::ProcessObject::Pointer filter =
193 app->m_UI->MPR->CreateFilter(
194 "cpPlugins::BasicFilters::FloodFillImageFilter"
196 if( filter.IsNull( ) )
199 cpPlugins::Interface::Parameters* params = filter->GetParameters( );
200 params->SetPoint( "Seed", 3, pos );
201 params->SetReal( "Window", app->m_UI->MPR->GetWindow( ) );
202 params->SetReal( "Level", app->m_UI->MPR->GetLevel( ) );
203 params->SetUint( "InsideValue", 1 );
204 params->SetUint( "OutsideValue", 0 );
205 filter->SetInput( "Input", app->m_UI->MPR->GetImage( app->m_ImageLoaded ) );
206 app->m_UI->MPR->Block( );
207 std::string err = filter->Update( );
208 cpPlugins::Interface::BaseMPRWindow::TImage::Pointer image = filter->GetOutput< cpPlugins::Interface::BaseMPRWindow::TImage >( "Output" );
209 filter->DisconnectOutputs( );
210 app->m_UI->MPR->AddImage( "Segmentation", image );
211 app->m_UI->MPR->Unblock( );
214 << "CursorCommand ==> "
219 << data << std::endl;
224 #include "MementoState.h"
226 #include <vtkProperty.h>
227 #include <vtkRenderWindow.h>
228 #include <vtkMetaImageReader.h>
230 #include <QFileDialog>
231 #include <QMessageBox>
234 # define PLUGIN_PREFIX ""
235 # define PLUGIN_EXT "dll"
236 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
238 # define PLUGIN_PREFIX "lib"
239 # define PLUGIN_EXT "so"
240 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
243 // -------------------------------------------------------------------------
244 ImageMPR::ImageMPR( QWidget* parent )
245 : QMainWindow( parent ),
246 m_UI( new Ui::ImageMPR ),
247 m_ImageReaderClass( "" ),
248 m_ImageWriterClass( "" ),
249 m_MeshReaderClass( "" ),
250 m_MeshWriterClass( "" ),
251 m_MeshCutterClass( "" ),
256 this->m_UI->setupUi( this );
258 // Create and associate renderers
259 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
260 this->m_MPRObjects->SetRenderWindows(
261 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
262 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
263 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
264 this->m_UI->m_3DVTK->GetRenderWindow( )
269 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
270 this, SLOT( _triggered_actionOpenPlugins( ) )
273 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
274 this, SLOT( _triggered_actionOpenInputImage( ) )
277 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
278 this, SLOT( _triggered_actionOpenSegmentation( ) )
281 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
282 this, SLOT( _triggered_actionOpenInputPolyData( ) )
285 this->m_UI->actionUndo, SIGNAL(triggered()),
286 this, SLOT(_triggered_actionUndo())
289 this->m_UI->actionRedo, SIGNAL(triggered()),
290 this, SLOT(_triggered_actionRedo())
293 // Start: load all disponible plugins
295 std::string( PLUGIN_PREFIX ) +
296 std::string( "cpPluginsIO." ) +
297 std::string( PLUGIN_EXT )
300 std::string( PLUGIN_PREFIX ) +
301 std::string( "cpPluginsBasicFilters." ) +
302 std::string( PLUGIN_EXT )
306 // -------------------------------------------------------------------------
310 // Close all connections
311 this->m_Plugins.UnloadAll( );
317 // -------------------------------------------------------------------------
319 _LoadPlugins( const std::string& filename )
321 QApplication::setOverrideCursor( Qt::WaitCursor );
322 this->setEnabled( false );
324 this->m_ImageReaderClass = "";
325 this->m_ImageWriterClass = "";
326 this->m_MeshReaderClass = "";
327 this->m_MeshWriterClass = "";
328 this->m_MeshCutterClass = "";
329 this->m_UI->MenuImageToImage->clear( );
330 this->m_UI->MenuImageToMesh->clear( );
332 if( !( this->m_Plugins.Load( filename ) ) )
335 typedef TPluginsInterface::TClasses _TClasses;
336 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
337 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
339 TPluginFilter::Pointer o =
340 this->m_Plugins.CreateProcessObject( cIt->first );
341 std::string name = o->GetClassName( );
342 std::string category = o->GetClassCategory( );
343 if( category == "ImageReader" )
344 this->m_ImageReaderClass = name;
345 else if( category == "ImageWriter" )
346 this->m_ImageWriterClass = name;
347 else if( category == "MeshReader" )
348 this->m_MeshReaderClass = name;
349 else if( category == "MeshWriter" )
350 this->m_MeshWriterClass = name;
351 else if( category == "MeshToMeshFilter" )
353 if( name.find_last_of( "Cutter" ) != std::string::npos )
354 this->m_MeshCutterClass = name;
356 else if( category == "ImageToImageFilter" )
359 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
361 action, SIGNAL( triggered( ) ),
362 this, SLOT( _triggered_actionImageToImage( ) )
365 else if( category == "ImageToMeshFilter" )
368 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
370 action, SIGNAL( triggered( ) ),
371 this, SLOT( _triggered_actionImageToMesh( ) )
377 QApplication::restoreOverrideCursor( );
378 this->setEnabled( true );
383 // -------------------------------------------------------------------------
384 std::string ImageMPR::
385 _LoadImage( TPluginImage::Pointer& image )
387 std::string ret = "";
390 // Get a reader from loaded plugins
391 TPluginFilter::Pointer reader =
392 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
393 if( reader.IsNotNull( ) )
395 if( reader->ExecConfigurationDialog( this ) )
398 QApplication::setOverrideCursor( Qt::WaitCursor );
399 this->setEnabled( false );
401 // Execute and get error message, if any
402 ret = reader->Update( );
404 // Assign fresh image, if any
407 image = reader->GetOutput< TPluginImage >( 0 );
408 reader->DisconnectOutputs( );
412 // Unblock application
413 QApplication::restoreOverrideCursor( );
414 this->setEnabled( true );
419 ret = "No suitable reader object found in loaded plugins.";
424 // -------------------------------------------------------------------------
425 std::string ImageMPR::
426 _ConfigureMeshActors( )
428 if( this->m_Mesh.IsNull( ) )
429 return( "Valid mesh not found." );
431 this->m_Mesh->CreateVTKActor( );
432 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
433 if( vtk_actor != NULL )
435 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
436 this->m_MPRObjects->Render( 4 );
440 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
442 std::string err = "";
443 for( unsigned int i = 0; i < 3; ++i )
445 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
446 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
447 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
448 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
449 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
450 std::string lerr = this->m_Cutters[ i ]->Update( );
453 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
454 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
455 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
457 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
459 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
461 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
467 this->m_MPRObjects->RenderAll( );
471 // -------------------------------------------------------------------------
473 _triggered_actionOpenPlugins( )
475 // Show dialog and check if it was accepted
476 QFileDialog dialog( this );
477 dialog.setFileMode( QFileDialog::ExistingFile );
478 dialog.setDirectory( "." );
479 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
480 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
481 if( !( dialog.exec( ) ) )
484 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
485 if( !( _LoadPlugins( fname ) ) )
486 QMessageBox::critical(
488 tr( "Ignoring plugin" ),
493 // -------------------------------------------------------------------------
495 _triggered_actionOpenInputImage( )
498 std::string err = this->_LoadImage( this->m_Image );
501 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
504 this->m_MPRObjects->SetImage( vtk_id );
505 this->m_MPRObjects->ActivateInteractors( );
506 this->m_MPRObjects->ResetCameras( );
507 this->m_MPRObjects->RenderAll( );
509 MementoState(m_state, this->m_Image);
513 QMessageBox::critical(
515 tr( "Error message" ),
516 tr( "Read image does not have a valid VTK converter." )
520 QMessageBox::critical(
522 tr( "Error reading single image" ),
527 // -------------------------------------------------------------------------
529 _triggered_actionOpenSegmentation( )
531 if( this->m_Image.IsNull( ) )
533 QMessageBox::critical(
535 tr( "Error message" ),
536 tr( "Before reading a segmentation, first load a raw image." )
543 std::string err = this->_LoadImage( this->m_Segmentation );
546 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
549 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
550 this->m_MPRObjects->RenderAll( );
553 QMessageBox::critical(
555 tr( "Error message" ),
556 tr( "Read image does not have a valid VTK converter." )
560 QMessageBox::critical(
562 tr( "Error reading single image" ),
567 // -------------------------------------------------------------------------
569 _triggered_actionOpenInputPolyData( )
573 // Get a reader from plugins
574 TPluginFilter::Pointer reader =
575 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
577 if( reader.IsNotNull( ) )
580 if( reader->ExecConfigurationDialog( this ) )
582 // Execute and get error message, if any
583 QApplication::setOverrideCursor( Qt::WaitCursor );
584 this->setEnabled( false );
585 std::string err = reader->Update( );
586 QApplication::restoreOverrideCursor( );
587 this->setEnabled( true );
589 // Assign fresh mesh, if any
592 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
593 reader->DisconnectOutputs( );
594 err = this->_ConfigureMeshActors( );
596 QMessageBox::critical(
598 tr( "Error message" ),
603 QMessageBox::critical(
605 tr( "Error reading mesh" ),
612 QMessageBox::critical(
614 tr( "Error reading single mesh" ),
615 tr( "No suitable mesh reader found in loaded plugins." )
619 // -------------------------------------------------------------------------
621 _triggered_actionImageToImage( )
623 if( this->m_Image.IsNull( ) )
627 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
630 std::string name = action->text( ).toStdString( );
633 TPluginFilter::Pointer filter =
634 this->m_Plugins.CreateProcessObject( name );
635 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
640 QApplication::setOverrideCursor( Qt::WaitCursor );
641 this->setEnabled( false );
642 filter->SetInput( 0, this->m_Image );
643 std::string err = filter->Update( );
644 QApplication::restoreOverrideCursor( );
645 this->setEnabled( true );
650 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
651 result->DisconnectPipeline( );
652 this->m_Image = result;
653 if( this->m_Image.IsNotNull( ) )
654 this->m_MPRObjects->SetImage(
655 this->m_Image->GetVTK< vtkImageData >( )
660 MementoState(this->m_state, this->m_Image);
662 if (this->m_state > this->m_max_state)
664 this->m_max_state = this->m_state;
668 QMessageBox::critical(
670 tr( "Error executing filter" ),
675 // -------------------------------------------------------------------------
677 _triggered_actionImageToMesh( )
679 if( this->m_Image.IsNull( ) )
683 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
686 std::string name = action->text( ).toStdString( );
689 TPluginFilter::Pointer filter =
690 this->m_Plugins.CreateProcessObject( name );
691 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
696 QApplication::setOverrideCursor( Qt::WaitCursor );
697 this->setEnabled( false );
698 filter->SetInput( 0, this->m_Image );
699 std::string err = filter->Update( );
700 QApplication::restoreOverrideCursor( );
701 this->setEnabled( true );
706 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
707 result->DisconnectPipeline( );
708 this->m_Mesh = result;
709 err = this->_ConfigureMeshActors( );
711 QMessageBox::critical(
713 tr( "Error message" ),
718 QMessageBox::critical(
720 tr( "Error executing filter" ),
725 // -------------------------------------------------------------------------
727 _triggered_actionUndo()
729 MementoState memento = MementoState();
734 this->m_MPRObjects->SetImage(
735 memento.getMemento(this->m_state)->GetOutput()
739 QMessageBox::warning(
742 tr("No history to undo")
748 // -------------------------------------------------------------------------
750 _triggered_actionRedo()
752 MementoState memento = MementoState();
753 if (this->m_state + 1 <= m_max_state)
756 this->m_MPRObjects->SetImage(
757 memento.getMemento(this->m_state)->GetOutput()
761 QMessageBox::warning(
764 tr("No history to redo")