]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/BasicFilters/MarchingCubes.cxx
388b75e293ef3f4f202d4351fcdf8614cde72877
[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   auto image = this->GetInputData< cpPlugins::Interface::Image >( "Input" );
32   vtkImageData* vtk_image = image->GetVTK< vtkImageData >( );
33   if( vtk_image == NULL )
34     return( "MarchingCubes: Input does not have a valid VTK conversion." );
35
36   std::vector< double > values =
37     this->m_Parameters->GetRealList( "Thresholds" );
38   vtkPolyData* pd = NULL;
39   if( vtk_image->GetDataDimension( ) == 2 )
40   {
41     vtkMarchingSquares* ms = this->_CreateVTK< vtkMarchingSquares >( );
42     ms->SetInputData( vtk_image );
43     for( unsigned int i = 0; i < values.size( ); ++i )
44       ms->SetValue( i, values[ i ] );
45     ms->Update( );
46     pd = ms->GetOutput( );
47   }
48   else if( vtk_image->GetDataDimension( ) == 3 )
49   {
50     vtkMarchingCubes* mc = this->_CreateVTK< vtkMarchingCubes >( );
51     mc->ComputeNormalsOff( );
52     mc->SetInputData( vtk_image );
53     for( unsigned int i = 0; i < values.size( ); ++i )
54       mc->SetValue( i, values[ i ] );
55     mc->Update( );
56     pd = mc->GetOutput( );
57   }
58   else
59     return( "MarchingCubes: Input data does not have a valid dimension." );
60
61   // Execute filter
62   auto out = this->GetOutputData< cpPlugins::Interface::Mesh >( "Output" );
63   out->SetVTK( pd );
64   return( "" );
65 }
66
67 // eof - $RCSfile$