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