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 );
21 // Associate callbacks
22 this->m_UI->MPR->AddCursorCommand( Self::_CursorCommand, this );
25 ImageMPR_ConnectAction( OpenImage );
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
37 this->m_UI->MPR->LoadPlugins( );
38 this->m_UI->MPR->AssociatePluginsToMenu(
39 this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) )
43 // -------------------------------------------------------------------------
50 // -------------------------------------------------------------------------
54 if( this->m_ImageLoaded != "" )
55 this->m_UI->MPR->ClearAll( );
56 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
59 // -------------------------------------------------------------------------
63 if( this->m_ImageLoaded != "" )
64 this->m_ImageLoaded = this->m_UI->MPR->LoadImage( );
67 // -------------------------------------------------------------------------
73 // -------------------------------------------------------------------------
79 // -------------------------------------------------------------------------
85 // -------------------------------------------------------------------------
91 // -------------------------------------------------------------------------
97 // -------------------------------------------------------------------------
103 // -------------------------------------------------------------------------
107 this->m_UI->MPR->DialogLoadPlugins( );
108 this->m_UI->MPR->AssociatePluginsToMenu(
109 this->m_UI->MenuFilters, this, SLOT( _execPlugin( ) )
113 // -------------------------------------------------------------------------
119 // -------------------------------------------------------------------------
124 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
127 std::string name = action->text( ).toStdString( );
129 if( name == "cpPlugins::BasicFilters::FloodFillImageFilter" )
131 this->m_Flooding = true;
135 this->m_Flooding = false;
136 this->m_UI->MPR->ExecuteFilter(
137 name, this->m_ImageLoaded, "SegmentedImage"
144 TPluginFilter::Pointer filter =
145 this->m_Plugins.CreateProcessObject( name );
146 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
151 QApplication::setOverrideCursor( Qt::WaitCursor );
152 this->setEnabled( false );
153 filter->SetInput( 0, this->m_Image );
154 std::string err = filter->Update( );
155 QApplication::restoreOverrideCursor( );
156 this->setEnabled( true );
161 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
162 result->DisconnectPipeline( );
163 this->m_Image = result;
164 if( this->m_Image.IsNotNull( ) )
165 this->m_MPRObjects->SetImage(
166 this->m_Image->GetVTK< vtkImageData >( )
168 MementoState(this->m_state, this->m_Image);
170 if (this->m_state > this->m_max_state)
172 this->m_max_state = this->m_state;
176 QMessageBox::critical(
178 tr( "Error executing filter" ),
184 // -------------------------------------------------------------------------
186 _CursorCommand( double* pos, int axis, void* data )
188 Self* app = reinterpret_cast< Self* >( data );
191 if( !( app->m_Flooding ) )
194 cpPlugins::Interface::ProcessObject::Pointer filter =
195 app->m_UI->MPR->CreateFilter(
196 "cpPlugins::BasicFilters::FloodFillImageFilter"
198 if( filter.IsNull( ) )
201 cpPlugins::Interface::Parameters* params = filter->GetParameters( );
202 params->SetPoint( "Seed", 3, pos );
203 params->SetReal( "Window", app->m_UI->MPR->GetWindow( ) );
204 params->SetReal( "Level", app->m_UI->MPR->GetLevel( ) );
205 params->SetUint( "InsideValue", 1 );
206 params->SetUint( "OutsideValue", 0 );
207 filter->SetInput( "Input", app->m_UI->MPR->GetImage( app->m_ImageLoaded ) );
208 app->m_UI->MPR->Block( );
209 std::string err = filter->Update( );
210 cpPlugins::Interface::BaseMPRWindow::TImage::Pointer image = filter->GetOutput< cpPlugins::Interface::BaseMPRWindow::TImage >( "Output" );
211 filter->DisconnectOutputs( );
212 app->m_UI->MPR->AddImage( "Segmentation", image );
213 app->m_UI->MPR->Unblock( );
219 << "CursorCommand ==> "
224 << data << std::endl;
229 #include "MementoState.h"
231 #include <vtkProperty.h>
232 #include <vtkRenderWindow.h>
233 #include <vtkMetaImageReader.h>
235 #include <QFileDialog>
236 #include <QMessageBox>
239 # define PLUGIN_PREFIX ""
240 # define PLUGIN_EXT "dll"
241 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
243 # define PLUGIN_PREFIX "lib"
244 # define PLUGIN_EXT "so"
245 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
248 // -------------------------------------------------------------------------
249 ImageMPR::ImageMPR( QWidget* parent )
250 : QMainWindow( parent ),
251 m_UI( new Ui::ImageMPR ),
252 m_ImageReaderClass( "" ),
253 m_ImageWriterClass( "" ),
254 m_MeshReaderClass( "" ),
255 m_MeshWriterClass( "" ),
256 m_MeshCutterClass( "" ),
261 this->m_UI->setupUi( this );
263 // Create and associate renderers
264 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
265 this->m_MPRObjects->SetRenderWindows(
266 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
267 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
268 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
269 this->m_UI->m_3DVTK->GetRenderWindow( )
274 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
275 this, SLOT( _triggered_actionOpenPlugins( ) )
278 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
279 this, SLOT( _triggered_actionOpenInputImage( ) )
282 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
283 this, SLOT( _triggered_actionOpenSegmentation( ) )
286 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
287 this, SLOT( _triggered_actionOpenInputPolyData( ) )
290 this->m_UI->actionUndo, SIGNAL(triggered()),
291 this, SLOT(_triggered_actionUndo())
294 this->m_UI->actionRedo, SIGNAL(triggered()),
295 this, SLOT(_triggered_actionRedo())
298 // Start: load all disponible plugins
300 std::string( PLUGIN_PREFIX ) +
301 std::string( "cpPluginsIO." ) +
302 std::string( PLUGIN_EXT )
305 std::string( PLUGIN_PREFIX ) +
306 std::string( "cpPluginsBasicFilters." ) +
307 std::string( PLUGIN_EXT )
311 // -------------------------------------------------------------------------
315 // Close all connections
316 this->m_Plugins.UnloadAll( );
322 // -------------------------------------------------------------------------
324 _LoadPlugins( const std::string& filename )
326 QApplication::setOverrideCursor( Qt::WaitCursor );
327 this->setEnabled( false );
329 this->m_ImageReaderClass = "";
330 this->m_ImageWriterClass = "";
331 this->m_MeshReaderClass = "";
332 this->m_MeshWriterClass = "";
333 this->m_MeshCutterClass = "";
334 this->m_UI->MenuImageToImage->clear( );
335 this->m_UI->MenuImageToMesh->clear( );
337 if( !( this->m_Plugins.Load( filename ) ) )
340 typedef TPluginsInterface::TClasses _TClasses;
341 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
342 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
344 TPluginFilter::Pointer o =
345 this->m_Plugins.CreateProcessObject( cIt->first );
346 std::string name = o->GetClassName( );
347 std::string category = o->GetClassCategory( );
348 if( category == "ImageReader" )
349 this->m_ImageReaderClass = name;
350 else if( category == "ImageWriter" )
351 this->m_ImageWriterClass = name;
352 else if( category == "MeshReader" )
353 this->m_MeshReaderClass = name;
354 else if( category == "MeshWriter" )
355 this->m_MeshWriterClass = name;
356 else if( category == "MeshToMeshFilter" )
358 if( name.find_last_of( "Cutter" ) != std::string::npos )
359 this->m_MeshCutterClass = name;
361 else if( category == "ImageToImageFilter" )
364 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
366 action, SIGNAL( triggered( ) ),
367 this, SLOT( _triggered_actionImageToImage( ) )
370 else if( category == "ImageToMeshFilter" )
373 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
375 action, SIGNAL( triggered( ) ),
376 this, SLOT( _triggered_actionImageToMesh( ) )
382 QApplication::restoreOverrideCursor( );
383 this->setEnabled( true );
388 // -------------------------------------------------------------------------
389 std::string ImageMPR::
390 _LoadImage( TPluginImage::Pointer& image )
392 std::string ret = "";
395 // Get a reader from loaded plugins
396 TPluginFilter::Pointer reader =
397 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
398 if( reader.IsNotNull( ) )
400 if( reader->ExecConfigurationDialog( this ) )
403 QApplication::setOverrideCursor( Qt::WaitCursor );
404 this->setEnabled( false );
406 // Execute and get error message, if any
407 ret = reader->Update( );
409 // Assign fresh image, if any
412 image = reader->GetOutput< TPluginImage >( 0 );
413 reader->DisconnectOutputs( );
417 // Unblock application
418 QApplication::restoreOverrideCursor( );
419 this->setEnabled( true );
424 ret = "No suitable reader object found in loaded plugins.";
429 // -------------------------------------------------------------------------
430 std::string ImageMPR::
431 _ConfigureMeshActors( )
433 if( this->m_Mesh.IsNull( ) )
434 return( "Valid mesh not found." );
436 this->m_Mesh->CreateVTKActor( );
437 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
438 if( vtk_actor != NULL )
440 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
441 this->m_MPRObjects->Render( 4 );
445 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
447 std::string err = "";
448 for( unsigned int i = 0; i < 3; ++i )
450 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
451 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
452 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
453 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
454 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
455 std::string lerr = this->m_Cutters[ i ]->Update( );
458 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
459 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
460 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
462 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
464 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
466 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
472 this->m_MPRObjects->RenderAll( );
476 // -------------------------------------------------------------------------
478 _triggered_actionOpenPlugins( )
480 // Show dialog and check if it was accepted
481 QFileDialog dialog( this );
482 dialog.setFileMode( QFileDialog::ExistingFile );
483 dialog.setDirectory( "." );
484 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
485 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
486 if( !( dialog.exec( ) ) )
489 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
490 if( !( _LoadPlugins( fname ) ) )
491 QMessageBox::critical(
493 tr( "Ignoring plugin" ),
498 // -------------------------------------------------------------------------
500 _triggered_actionOpenInputImage( )
503 std::string err = this->_LoadImage( this->m_Image );
506 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
509 this->m_MPRObjects->SetImage( vtk_id );
510 this->m_MPRObjects->ActivateInteractors( );
511 this->m_MPRObjects->ResetCameras( );
512 this->m_MPRObjects->RenderAll( );
514 MementoState(m_state, this->m_Image);
518 QMessageBox::critical(
520 tr( "Error message" ),
521 tr( "Read image does not have a valid VTK converter." )
525 QMessageBox::critical(
527 tr( "Error reading single image" ),
532 // -------------------------------------------------------------------------
534 _triggered_actionOpenSegmentation( )
536 if( this->m_Image.IsNull( ) )
538 QMessageBox::critical(
540 tr( "Error message" ),
541 tr( "Before reading a segmentation, first load a raw image." )
548 std::string err = this->_LoadImage( this->m_Segmentation );
551 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
554 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
555 this->m_MPRObjects->RenderAll( );
558 QMessageBox::critical(
560 tr( "Error message" ),
561 tr( "Read image does not have a valid VTK converter." )
565 QMessageBox::critical(
567 tr( "Error reading single image" ),
572 // -------------------------------------------------------------------------
574 _triggered_actionOpenInputPolyData( )
578 // Get a reader from plugins
579 TPluginFilter::Pointer reader =
580 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
582 if( reader.IsNotNull( ) )
585 if( reader->ExecConfigurationDialog( this ) )
587 // Execute and get error message, if any
588 QApplication::setOverrideCursor( Qt::WaitCursor );
589 this->setEnabled( false );
590 std::string err = reader->Update( );
591 QApplication::restoreOverrideCursor( );
592 this->setEnabled( true );
594 // Assign fresh mesh, if any
597 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
598 reader->DisconnectOutputs( );
599 err = this->_ConfigureMeshActors( );
601 QMessageBox::critical(
603 tr( "Error message" ),
608 QMessageBox::critical(
610 tr( "Error reading mesh" ),
617 QMessageBox::critical(
619 tr( "Error reading single mesh" ),
620 tr( "No suitable mesh reader found in loaded plugins." )
624 // -------------------------------------------------------------------------
626 _triggered_actionImageToImage( )
628 if( this->m_Image.IsNull( ) )
632 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
635 std::string name = action->text( ).toStdString( );
638 TPluginFilter::Pointer filter =
639 this->m_Plugins.CreateProcessObject( name );
640 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
645 QApplication::setOverrideCursor( Qt::WaitCursor );
646 this->setEnabled( false );
647 filter->SetInput( 0, this->m_Image );
648 std::string err = filter->Update( );
649 QApplication::restoreOverrideCursor( );
650 this->setEnabled( true );
655 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
656 result->DisconnectPipeline( );
657 this->m_Image = result;
658 if( this->m_Image.IsNotNull( ) )
659 this->m_MPRObjects->SetImage(
660 this->m_Image->GetVTK< vtkImageData >( )
665 MementoState(this->m_state, this->m_Image);
667 if (this->m_state > this->m_max_state)
669 this->m_max_state = this->m_state;
673 QMessageBox::critical(
675 tr( "Error executing filter" ),
680 // -------------------------------------------------------------------------
682 _triggered_actionImageToMesh( )
684 if( this->m_Image.IsNull( ) )
688 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
691 std::string name = action->text( ).toStdString( );
694 TPluginFilter::Pointer filter =
695 this->m_Plugins.CreateProcessObject( name );
696 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
701 QApplication::setOverrideCursor( Qt::WaitCursor );
702 this->setEnabled( false );
703 filter->SetInput( 0, this->m_Image );
704 std::string err = filter->Update( );
705 QApplication::restoreOverrideCursor( );
706 this->setEnabled( true );
711 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
712 result->DisconnectPipeline( );
713 this->m_Mesh = result;
714 err = this->_ConfigureMeshActors( );
716 QMessageBox::critical(
718 tr( "Error message" ),
723 QMessageBox::critical(
725 tr( "Error executing filter" ),
730 // -------------------------------------------------------------------------
732 _triggered_actionUndo()
734 MementoState memento = MementoState();
739 this->m_MPRObjects->SetImage(
740 memento.getMemento(this->m_state)->GetOutput()
744 QMessageBox::warning(
747 tr("No history to undo")
753 // -------------------------------------------------------------------------
755 _triggered_actionRedo()
757 MementoState memento = MementoState();
758 if (this->m_state + 1 <= m_max_state)
761 this->m_MPRObjects->SetImage(
762 memento.getMemento(this->m_state)->GetOutput()
766 QMessageBox::warning(
769 tr("No history to redo")