1 #include <ITKRasterFilters/RasterImageFilter.h>
2 #include <cpInstances/DataObjects/Image.h>
3 #include <cpInstances/DataObjects/Mesh.h>
5 #include <vtkPolyData.h>
6 #include <itkTriangleMeshToBinaryImageFilter.h>
7 #include <cpExtensions/Algorithms/RasterContourFilter.h>
9 // -------------------------------------------------------------------------
10 cpPluginsITKRasterFilters::RasterImageFilter::
14 typedef cpInstances::DataObjects::Mesh _TMesh;
15 typedef cpInstances::DataObjects::Image _TImage;
17 this->_ConfigureInput< _TMesh >( "Input", true, false );
18 this->_ConfigureInput< _TImage >( "TemplateImage", false, false );
19 this->_ConfigureOutput< _TImage >( "Output" );
21 this->m_Parameters.ConfigureAsReal( "InsideValue", 1 );
22 this->m_Parameters.ConfigureAsReal( "OutsideValue", 0 );
23 this->m_Parameters.ConfigureAsScalarTypesChoices( "OutputPixelType" );
26 // -------------------------------------------------------------------------
27 cpPluginsITKRasterFilters::RasterImageFilter::
32 // -------------------------------------------------------------------------
33 void cpPluginsITKRasterFilters::RasterImageFilter::
36 auto mesh = this->GetInputData< vtkPolyData >( "Input" );
38 mesh->GetBounds( bounds );
39 double zdiff = std::fabs( bounds[ 5 ] - bounds[ 4 ] );
42 cpPlugins_Demangle_Mesh_Meshes_1(
43 this->GetInputData( "Input" ), _GD0_3D, 3
45 this->_Error( "Invalid input image dimension." );
48 this->_GD0_2D( mesh );
51 // -------------------------------------------------------------------------
52 template< class _TMesh >
53 void cpPluginsITKRasterFilters::RasterImageFilter::
54 _GD0_2D( _TMesh* mesh )
57 this->m_Parameters.GetSelectedChoice( "OutputPixelType" );
58 if( p_type == "char" ) this->_GD1_2D< _TMesh, char >( mesh );
59 else if( p_type == "short" ) this->_GD1_2D< _TMesh, short >( mesh );
60 else if( p_type == "int" ) this->_GD1_2D< _TMesh, int >( mesh );
61 else if( p_type == "long" ) this->_GD1_2D< _TMesh, long >( mesh );
62 else if( p_type == "uchar" ) this->_GD1_2D< _TMesh, unsigned char >( mesh );
63 else if( p_type == "ushort" ) this->_GD1_2D< _TMesh, unsigned short >( mesh );
64 else if( p_type == "uint" ) this->_GD1_2D< _TMesh, unsigned int >( mesh );
65 else if( p_type == "ulong" ) this->_GD1_2D< _TMesh, unsigned long >( mesh );
66 else if( p_type == "float" ) this->_GD1_2D< _TMesh, float >( mesh );
67 else if( p_type == "double" ) this->_GD1_2D< _TMesh, double >( mesh );
70 // -------------------------------------------------------------------------
71 template< class _TMesh, class _TPixelType >
72 void cpPluginsITKRasterFilters::RasterImageFilter::
73 _GD1_2D( _TMesh* mesh )
75 typedef itk::Image< _TPixelType, 2 > _TImage;
76 typedef typename _TImage::Superclass _TBaseImage;
77 typedef cpExtensions::Algorithms::RasterContourFilter< _TImage > _TFilter;
80 _TFilter* filter = this->_CreateITK< _TFilter >( );
81 filter->SetInsideValue( this->m_Parameters.GetReal( "InsideValue" ) );
82 filter->SetOutsideValue( this->m_Parameters.GetReal( "OutsideValue" ) );
83 filter->SetTemplate( this->GetInputData< _TBaseImage >( "TemplateImage" ) );
84 filter->ClearPoints( );
85 for( unsigned long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
88 mesh->GetPoint( i, pnt );
89 filter->AddPoint( pnt );
93 // Execute and connect output
95 this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
98 // -------------------------------------------------------------------------
99 template< class _TMesh >
100 void cpPluginsITKRasterFilters::RasterImageFilter::
101 _GD0_3D( _TMesh* mesh )
104 this->m_Parameters.GetSelectedChoice( "OutputPixelType" );
105 if( p_type == "char" ) this->_GD1_3D< _TMesh, char >( mesh );
106 else if( p_type == "short" ) this->_GD1_3D< _TMesh, short >( mesh );
107 else if( p_type == "int" ) this->_GD1_3D< _TMesh, int >( mesh );
108 else if( p_type == "long" ) this->_GD1_3D< _TMesh, long >( mesh );
109 else if( p_type == "uchar" ) this->_GD1_3D< _TMesh, unsigned char >( mesh );
110 else if( p_type == "ushort" ) this->_GD1_3D< _TMesh, unsigned short >( mesh );
111 else if( p_type == "uint" ) this->_GD1_3D< _TMesh, unsigned int >( mesh );
112 else if( p_type == "ulong" ) this->_GD1_3D< _TMesh, unsigned long >( mesh );
113 else if( p_type == "float" ) this->_GD1_3D< _TMesh, float >( mesh );
114 else if( p_type == "double" ) this->_GD1_3D< _TMesh, double >( mesh );
117 // -------------------------------------------------------------------------
118 template< class _TMesh, class _TPixelType >
119 void cpPluginsITKRasterFilters::RasterImageFilter::
120 _GD1_3D( _TMesh* mesh )
122 typedef itk::Image< _TPixelType, 3 > _TImage;
123 typedef typename _TImage::Superclass _TBaseImage;
124 typedef itk::TriangleMeshToBinaryImageFilter< _TMesh, _TImage > _TFilter;
127 _TFilter* filter = this->_CreateITK< _TFilter >( );
129 // Compute bounding box
130 typename _TImage::DirectionType direction;
131 typename _TImage::PointType origin;
132 typename _TImage::SizeType size;
133 typename _TImage::SpacingType spac;
134 auto t_image = this->GetInputData< _TBaseImage >( "TemplateImage" );
135 if( t_image == NULL )
140 direction = t_image->GetDirection( );
141 origin = t_image->GetOrigin( );
142 size = t_image->GetLargestPossibleRegion( ).GetSize( );
143 spac = t_image->GetSpacing( );
148 filter->SetDirection( direction );
149 filter->SetOrigin( origin );
150 filter->SetSize( size );
151 filter->SetSpacing( spac );
152 filter->SetInsideValue( this->m_Parameters.GetReal( "InsideValue" ) );
153 filter->SetOutsideValue( this->m_Parameters.GetReal( "OutsideValue" ) );
155 // Execute and connect output
157 this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );