]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/MarchingCubes.cxx
...
[cpPlugins.git] / lib / cpPlugins / Plugins / MarchingCubes.cxx
1 #include <cpPlugins/Plugins/MarchingCubes.h>
2 #include <cpPlugins/Interface/Image.h>
3 #include <cpPlugins/Interface/Mesh.h>
4
5 #include <vtkImageData.h>
6 #include <vtkMarchingCubes.h>
7 #include <vtkMarchingSquares.h>
8
9 // -------------------------------------------------------------------------
10 cpPlugins::Plugins::MarchingCubes::
11 MarchingCubes( )
12   : Superclass( ),
13     m_Algorithm( NULL )
14 {
15   this->m_ClassName = "cpPlugins::MarchingCubes";
16   this->m_ClassCategory = "ImageToMeshFilter";
17
18   this->SetNumberOfInputs( 1 );
19   this->SetNumberOfOutputs( 1 );
20   this->_MakeOutput< cpPlugins::Interface::Mesh >( 0 );
21
22   using namespace cpPlugins::Interface;
23   this->m_DefaultParameters.Configure( Parameters::RealList, "Thresholds" );
24   this->m_Parameters = this->m_DefaultParameters;
25 }
26
27 // -------------------------------------------------------------------------
28 cpPlugins::Plugins::MarchingCubes::
29 ~MarchingCubes( )
30 {
31   if( this->m_Algorithm != NULL )
32     this->m_Algorithm->Delete( );
33 }
34
35 // -------------------------------------------------------------------------
36 std::string cpPlugins::Plugins::MarchingCubes::
37 _GenerateData( )
38 {
39   // Get input
40   cpPlugins::Interface::Image* image =
41     this->_Input< cpPlugins::Interface::Image >( 0 );
42   if( image == NULL )
43     return( "MarchingCubes: Input data is not a valid image." );
44   vtkImageData* vtk_image = image->GetVTKImageData( );
45   if( vtk_image == NULL )
46     return( "MarchingCubes: Input does not have a valid VTK conversion." );
47
48   if( this->m_Algorithm != NULL )
49     this->m_Algorithm->Delete( );
50
51   std::vector< double > values;
52   this->m_Parameters.GetValueAsRealList( values, "Thresholds" );
53   if( vtk_image->GetDataDimension( ) == 2 )
54   {
55     vtkMarchingSquares* ms = vtkMarchingSquares::New( );
56     ms->SetInputData( vtk_image );
57     for( unsigned int i = 0; i < values.size( ); ++i )
58       ms->SetValue( i, values[ i ] );
59     this->m_Algorithm = ms;
60   }
61   else if( vtk_image->GetDataDimension( ) == 3 )
62   {
63     vtkMarchingCubes* mc = vtkMarchingCubes::New( );
64     mc->SetInputData( vtk_image );
65     for( unsigned int i = 0; i < values.size( ); ++i )
66       mc->SetValue( i, values[ i ] );
67     this->m_Algorithm = mc;
68   }
69   else
70     return( "MarchingCubes: Input data does not have a valid dimension." );
71
72   // Execute filter
73   this->m_Algorithm->Update( );
74   cpPlugins::Interface::Mesh* out =
75     this->_Output< cpPlugins::Interface::Mesh >( 0 );
76   out->SetVTKMesh( this->m_Algorithm->GetOutput( ) );
77   return( "" );
78 }
79
80 // eof - $RCSfile$