]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/BasicFilters/MarchingCubes.cxx
cca186c79f44bc03ebeefc8733a4d247f2203bd5
[cpPlugins.git] / lib / cpPlugins / Plugins / BasicFilters / MarchingCubes.cxx
1 #include "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::BasicFilters::MarchingCubes::
11 MarchingCubes( )
12   : Superclass( )
13 {
14   this->m_ClassName = "cpPlugins::BasicFilters::MarchingCubes";
15   this->m_ClassCategory = "ImageToMeshFilter";
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::BasicFilters::MarchingCubes::
28 ~MarchingCubes( )
29 {
30 }
31
32 // -------------------------------------------------------------------------
33 std::string cpPlugins::BasicFilters::MarchingCubes::
34 _GenerateData( )
35 {
36   // Get input
37   cpPlugins::Interface::Image* image =
38     this->GetInput< cpPlugins::Interface::Image >( 0 );
39   if( image == NULL )
40     return( "MarchingCubes: Input data is not a valid image." );
41   vtkImageData* vtk_image = image->GetVTK< vtkImageData >( );
42   if( vtk_image == NULL )
43     return( "MarchingCubes: Input does not have a valid VTK conversion." );
44
45   std::vector< double > values;
46   this->m_Parameters.GetValueAsRealList( values, "Thresholds" );
47   vtkPolyData* pd = NULL;
48   if( vtk_image->GetDataDimension( ) == 2 )
49   {
50     vtkMarchingSquares* ms = this->_CreateVTK< vtkMarchingSquares >( );
51     ms->SetInputData( vtk_image );
52     for( unsigned int i = 0; i < values.size( ); ++i )
53       ms->SetValue( i, values[ i ] );
54     ms->Update( );
55     pd = ms->GetOutput( );
56   }
57   else if( vtk_image->GetDataDimension( ) == 3 )
58   {
59     vtkMarchingCubes* mc = this->_CreateVTK< vtkMarchingCubes >( );
60     mc->ComputeNormalsOff( );
61     mc->SetInputData( vtk_image );
62     for( unsigned int i = 0; i < values.size( ); ++i )
63       mc->SetValue( i, values[ i ] );
64     mc->Update( );
65     pd = mc->GetOutput( );
66   }
67   else
68     return( "MarchingCubes: Input data does not have a valid dimension." );
69
70   // Execute filter
71   cpPlugins::Interface::Mesh* out =
72     this->GetOutput< cpPlugins::Interface::Mesh >( 0 );
73   out->SetVTK( pd );
74   return( "" );
75 }
76
77 // eof - $RCSfile$