]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/BasicFilters/MarchingCubes.cxx
ce6aa32fba7b3ab1372ffde0e1a19320482de8c4
[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->_AddInput( "Input" );
15   this->_AddOutput< cpPlugins::Interface::Mesh >( "Output" );
16
17   this->m_Parameters->ConfigureAsRealList( "Thresholds" );
18 }
19
20 // -------------------------------------------------------------------------
21 cpPlugins::BasicFilters::MarchingCubes::
22 ~MarchingCubes( )
23 {
24 }
25
26 // -------------------------------------------------------------------------
27 std::string cpPlugins::BasicFilters::MarchingCubes::
28 _GenerateData( )
29 {
30   // Get input
31   cpPlugins::Interface::Image* image =
32     this->GetInput< cpPlugins::Interface::Image >( "Input" );
33   if( image == NULL )
34     return( "MarchingCubes: Input data is not a valid image." );
35   vtkImageData* vtk_image = image->GetVTK< vtkImageData >( );
36   if( vtk_image == NULL )
37     return( "MarchingCubes: Input does not have a valid VTK conversion." );
38
39   std::vector< double > values =
40     this->m_Parameters->GetRealList( "Thresholds" );
41   vtkPolyData* pd = NULL;
42   if( vtk_image->GetDataDimension( ) == 2 )
43   {
44     vtkMarchingSquares* ms = this->_CreateVTK< vtkMarchingSquares >( );
45     ms->SetInputData( vtk_image );
46     for( unsigned int i = 0; i < values.size( ); ++i )
47       ms->SetValue( i, values[ i ] );
48     ms->Update( );
49     pd = ms->GetOutput( );
50   }
51   else if( vtk_image->GetDataDimension( ) == 3 )
52   {
53     vtkMarchingCubes* mc = this->_CreateVTK< vtkMarchingCubes >( );
54     mc->ComputeNormalsOff( );
55     mc->SetInputData( vtk_image );
56     for( unsigned int i = 0; i < values.size( ); ++i )
57       mc->SetValue( i, values[ i ] );
58     mc->Update( );
59     pd = mc->GetOutput( );
60   }
61   else
62     return( "MarchingCubes: Input data does not have a valid dimension." );
63
64   // Execute filter
65   cpPlugins::Interface::Mesh* out =
66     this->GetOutput< cpPlugins::Interface::Mesh >( "Output" );
67   out->SetVTK( pd );
68   return( "" );
69 }
70
71 // eof - $RCSfile$