2 #include "ui_ImageMPR.h"
4 #include <vtkProperty.h>
5 #include <vtkRenderWindow.h>
11 # define PLUGIN_PREFIX ""
12 # define PLUGIN_EXT "dll"
13 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
15 # define PLUGIN_PREFIX "lib"
16 # define PLUGIN_EXT "so"
17 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
20 // -------------------------------------------------------------------------
21 ImageMPR::ImageMPR( QWidget* parent )
22 : QMainWindow( parent ),
23 m_UI( new Ui::ImageMPR ),
24 m_ImageReaderClass( "" ),
25 m_ImageWriterClass( "" ),
26 m_MeshReaderClass( "" ),
27 m_MeshWriterClass( "" ),
28 m_MeshCutterClass( "" ),
31 this->m_UI->setupUi( this );
33 // Create and associate renderers
34 this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
35 this->m_MPRObjects->SetRenderWindows(
36 this->m_UI->m_XPlaneVTK->GetRenderWindow( ),
37 this->m_UI->m_YPlaneVTK->GetRenderWindow( ),
38 this->m_UI->m_ZPlaneVTK->GetRenderWindow( ),
39 this->m_UI->m_3DVTK->GetRenderWindow( )
44 this->m_UI->actionOpenPlugins, SIGNAL( triggered( ) ),
45 this, SLOT( _triggered_actionOpenPlugins( ) )
48 this->m_UI->actionOpenInputImage, SIGNAL( triggered( ) ),
49 this, SLOT( _triggered_actionOpenInputImage( ) )
52 this->m_UI->actionOpenSegmentation, SIGNAL( triggered( ) ),
53 this, SLOT( _triggered_actionOpenSegmentation( ) )
56 this->m_UI->actionOpenInputPolyData, SIGNAL( triggered( ) ),
57 this, SLOT( _triggered_actionOpenInputPolyData( ) )
60 // Start: load all disponible plugins
62 std::string( PLUGIN_PREFIX ) +
63 std::string( "cpPluginsIO." ) +
64 std::string( PLUGIN_EXT )
67 std::string( PLUGIN_PREFIX ) +
68 std::string( "cpPluginsBasicFilters." ) +
69 std::string( PLUGIN_EXT )
73 // -------------------------------------------------------------------------
77 // Close all connections
78 this->m_Plugins.UnloadAll( );
84 // -------------------------------------------------------------------------
86 _LoadPlugins( const std::string& filename )
88 QApplication::setOverrideCursor( Qt::WaitCursor );
89 this->setEnabled( false );
91 this->m_ImageReaderClass = "";
92 this->m_ImageWriterClass = "";
93 this->m_MeshReaderClass = "";
94 this->m_MeshWriterClass = "";
95 this->m_MeshCutterClass = "";
96 this->m_UI->MenuImageToImage->clear( );
97 this->m_UI->MenuImageToMesh->clear( );
99 if( !( this->m_Plugins.Load( filename ) ) )
102 typedef TPluginsInterface::TClasses _TClasses;
103 _TClasses::const_iterator cIt = this->m_Plugins.GetClasses( ).begin( );
104 for( ; cIt != this->m_Plugins.GetClasses( ).end( ); ++cIt )
106 TPluginFilter::Pointer o =
107 this->m_Plugins.CreateProcessObject( cIt->first );
108 std::string name = o->GetClassName( );
109 std::string category = o->GetClassCategory( );
110 if( category == "ImageReader" )
111 this->m_ImageReaderClass = name;
112 else if( category == "ImageWriter" )
113 this->m_ImageWriterClass = name;
114 else if( category == "MeshReader" )
115 this->m_MeshReaderClass = name;
116 else if( category == "MeshWriter" )
117 this->m_MeshWriterClass = name;
118 else if( category == "MeshToMeshFilter" )
120 if( name.find_last_of( "Cutter" ) != std::string::npos )
121 this->m_MeshCutterClass = name;
123 else if( category == "ImageToImageFilter" )
126 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
128 action, SIGNAL( triggered( ) ),
129 this, SLOT( _triggered_actionImageToImage( ) )
132 else if( category == "ImageToMeshFilter" )
135 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
137 action, SIGNAL( triggered( ) ),
138 this, SLOT( _triggered_actionImageToMesh( ) )
144 QApplication::restoreOverrideCursor( );
145 this->setEnabled( true );
150 // -------------------------------------------------------------------------
151 std::string ImageMPR::
152 _LoadImage( TPluginImage::Pointer& image )
154 std::string ret = "";
157 // Get a reader from loaded plugins
158 TPluginFilter::Pointer reader =
159 this->m_Plugins.CreateProcessObject( this->m_ImageReaderClass );
160 if( reader.IsNotNull( ) )
162 if( reader->ExecConfigurationDialog( this ) )
165 QApplication::setOverrideCursor( Qt::WaitCursor );
166 this->setEnabled( false );
168 // Execute and get error message, if any
169 ret = reader->Update( );
171 // Assign fresh image, if any
174 image = reader->GetOutput< TPluginImage >( 0 );
175 reader->DisconnectOutputs( );
179 // Unblock application
180 QApplication::restoreOverrideCursor( );
181 this->setEnabled( true );
186 ret = "No suitable reader object found in loaded plugins.";
191 // -------------------------------------------------------------------------
192 std::string ImageMPR::
193 _ConfigureMeshActors( )
195 if( this->m_Mesh.IsNull( ) )
196 return( "Valid mesh not found." );
198 this->m_Mesh->CreateVTKActor( );
199 vtkActor* vtk_actor = this->m_Mesh->GetVTKActor( );
200 if( vtk_actor != NULL )
202 this->m_MPRObjects->Get3DRenderer( )->AddActor( vtk_actor );
203 this->m_MPRObjects->Render( 4 );
207 TMPRObjects::TMPRActors* mprActors = this->m_MPRObjects->GetMPRActors( );
209 std::string err = "";
210 for( unsigned int i = 0; i < 3; ++i )
212 this->m_Cutters[ i ] = this->m_Plugins.CreateProcessObject( this->m_MeshCutterClass );
213 this->m_Planes[ i ] = TPluginImplicitFunction::New( );
214 this->m_Planes[ i ]->SetFunction( mprActors->GetSliceActors( i )->GetPlaneFunction( ) );
215 this->m_Cutters[ i ]->SetInput( 0, this->m_Mesh );
216 this->m_Cutters[ i ]->SetInput( 1, this->m_Planes[ i ] );
217 std::string lerr = this->m_Cutters[ i ]->Update( );
220 this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->CreateVTKActor( );
221 vtkActor* actor = this->m_Cutters[ i ]->GetOutput< TPluginMesh >( 0 )->GetVTKActor( );
222 mprActors->GetSliceActors( i )->AddActor( this->m_Cutters[ i ]->GetVTK< vtkAlgorithm >( ), actor );
224 this->m_MPRObjects->GetXRenderer( )->AddActor( actor );
226 this->m_MPRObjects->GetYRenderer( )->AddActor( actor );
228 this->m_MPRObjects->GetZRenderer( )->AddActor( actor );
234 this->m_MPRObjects->RenderAll( );
238 // -------------------------------------------------------------------------
240 _triggered_actionOpenPlugins( )
242 // Show dialog and check if it was accepted
243 QFileDialog dialog( this );
244 dialog.setFileMode( QFileDialog::ExistingFile );
245 dialog.setDirectory( "." );
246 dialog.setNameFilter( tr( PLUGIN_REGEX ) );
247 dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
248 if( !( dialog.exec( ) ) )
251 std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
252 if( !( _LoadPlugins( fname ) ) )
253 QMessageBox::critical(
255 tr( "Ignoring plugin" ),
260 // -------------------------------------------------------------------------
262 _triggered_actionOpenInputImage( )
265 std::string err = this->_LoadImage( this->m_Image );
268 vtkImageData* vtk_id = this->m_Image->GetVTK< vtkImageData >( );
271 this->m_MPRObjects->SetImage( vtk_id );
272 this->m_MPRObjects->ActivateInteractors( );
273 this->m_MPRObjects->ResetCameras( );
274 this->m_MPRObjects->RenderAll( );
277 QMessageBox::critical(
279 tr( "Error message" ),
280 tr( "Read image does not have a valid VTK converter." )
284 QMessageBox::critical(
286 tr( "Error reading single image" ),
291 // -------------------------------------------------------------------------
293 _triggered_actionOpenSegmentation( )
295 if( this->m_Image.IsNull( ) )
297 QMessageBox::critical(
299 tr( "Error message" ),
300 tr( "Before reading a segmentation, first load a raw image." )
307 std::string err = this->_LoadImage( this->m_Segmentation );
310 vtkImageData* vtk_id = this->m_Segmentation->GetVTK< vtkImageData >( );
313 this->m_MPRObjects->AddAuxiliaryImage( vtk_id );
314 this->m_MPRObjects->RenderAll( );
317 QMessageBox::critical(
319 tr( "Error message" ),
320 tr( "Read image does not have a valid VTK converter." )
324 QMessageBox::critical(
326 tr( "Error reading single image" ),
331 // -------------------------------------------------------------------------
333 _triggered_actionOpenInputPolyData( )
337 // Get a reader from plugins
338 TPluginFilter::Pointer reader =
339 this->m_Plugins.CreateProcessObject( this->m_MeshReaderClass );
341 if( reader.IsNotNull( ) )
344 if( reader->ExecConfigurationDialog( this ) )
346 // Execute and get error message, if any
347 QApplication::setOverrideCursor( Qt::WaitCursor );
348 this->setEnabled( false );
349 std::string err = reader->Update( );
350 QApplication::restoreOverrideCursor( );
351 this->setEnabled( true );
353 // Assign fresh mesh, if any
356 this->m_Mesh = reader->GetOutput< TPluginMesh >( 0 );
357 reader->DisconnectOutputs( );
358 err = this->_ConfigureMeshActors( );
360 QMessageBox::critical(
362 tr( "Error message" ),
367 QMessageBox::critical(
369 tr( "Error reading mesh" ),
376 QMessageBox::critical(
378 tr( "Error reading single mesh" ),
379 tr( "No suitable mesh reader found in loaded plugins." )
383 // -------------------------------------------------------------------------
385 _triggered_actionImageToImage( )
387 if( this->m_Image.IsNull( ) )
391 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
394 std::string name = action->text( ).toStdString( );
397 TPluginFilter::Pointer filter =
398 this->m_Plugins.CreateProcessObject( name );
399 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
404 QApplication::setOverrideCursor( Qt::WaitCursor );
405 this->setEnabled( false );
406 filter->SetInput( 0, this->m_Image );
407 std::string err = filter->Update( );
408 QApplication::restoreOverrideCursor( );
409 this->setEnabled( true );
414 TPluginImage* result = filter->GetOutput< TPluginImage >( 0 );
415 result->DisconnectPipeline( );
416 this->m_Image = result;
417 if( this->m_Image.IsNotNull( ) )
418 this->m_MPRObjects->SetImage(
419 this->m_Image->GetVTK< vtkImageData >( )
423 QMessageBox::critical(
425 tr( "Error executing filter" ),
430 // -------------------------------------------------------------------------
432 _triggered_actionImageToMesh( )
434 if( this->m_Image.IsNull( ) )
438 QAction* action = dynamic_cast< QAction* >( this->sender( ) );
441 std::string name = action->text( ).toStdString( );
444 TPluginFilter::Pointer filter =
445 this->m_Plugins.CreateProcessObject( name );
446 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
451 QApplication::setOverrideCursor( Qt::WaitCursor );
452 this->setEnabled( false );
453 filter->SetInput( 0, this->m_Image );
454 std::string err = filter->Update( );
455 QApplication::restoreOverrideCursor( );
456 this->setEnabled( true );
461 TPluginMesh* result = filter->GetOutput< TPluginMesh >( 0 );
462 result->DisconnectPipeline( );
463 this->m_Mesh = result;
464 err = this->_ConfigureMeshActors( );
466 QMessageBox::critical(
468 tr( "Error message" ),
473 QMessageBox::critical(
475 tr( "Error executing filter" ),