]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/MarchingCubes.cxx
bc56f9cb4f9fd38e7df6d7d16a0e4a29225e200e
[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* input =
40     dynamic_cast< cpPlugins::Interface::Image* >(
41       this->m_Inputs[ 0 ].GetPointer( )
42       );
43   if( input == NULL )
44     return( "MarchingCubes: Input data is not a valid image." );
45   vtkImageData* vtk_input =
46     dynamic_cast< vtkImageData* >( input->GetVTKDataObject( ) );
47   if( vtk_input == NULL )
48     return( "MarchingCubes: Input does not have a valid conversion to VTK." );
49
50   if( this->m_Algorithm != NULL )
51     this->m_Algorithm->Delete( );
52
53   std::vector< double > values;
54   this->m_Parameters.GetValueAsRealList( values, "Thresholds" );
55   if( vtk_input->GetDataDimension( ) == 2 )
56   {
57     vtkMarchingSquares* ms = vtkMarchingSquares::New( );
58     ms->SetInputData( vtk_input );
59     for( unsigned int i = 0; i < values.size( ); ++i )
60       ms->SetValue( i, values[ i ] );
61     this->m_Algorithm = ms;
62   }
63   else if( vtk_input->GetDataDimension( ) == 3 )
64   {
65     vtkMarchingCubes* mc = vtkMarchingCubes::New( );
66     mc->SetInputData( vtk_input );
67     for( unsigned int i = 0; i < values.size( ); ++i )
68       mc->SetValue( i, values[ i ] );
69     this->m_Algorithm = mc;
70   }
71   else
72     return( "MarchingCubes: Input data does not have a valid dimension." );
73
74   // Execute filter
75   this->m_Algorithm->Update( );
76   this->m_Outputs[ 0 ]->SetVTKDataObject( this->m_Algorithm->GetOutput( ) );
77   return( "" );
78 }
79
80 // eof - $RCSfile$