1 #include <ITKRasterFilters/RasterImageFilter.h>
2 #include <cpInstances/DataObjects/Image.h>
3 #include <cpInstances/DataObjects/Mesh.h>
5 #include <itkTriangleMeshToBinaryImageFilter.h>
6 #include <cpExtensions/Algorithms/RasterContourFilter.h>
8 // -------------------------------------------------------------------------
9 cpPluginsITKRasterFilters::RasterImageFilter::
13 typedef cpInstances::DataObjects::Mesh _TMesh;
14 typedef cpInstances::DataObjects::Image _TImage;
16 this->_ConfigureInput< _TMesh >( "Input", true, false );
17 this->_ConfigureInput< _TImage >( "TemplateImage", false, false );
18 this->_ConfigureOutput< _TImage >( "Output" );
20 this->m_Parameters.ConfigureAsReal( "InsideValue", 1 );
21 this->m_Parameters.ConfigureAsReal( "OutsideValue", 0 );
22 this->m_Parameters.ConfigureAsScalarTypesChoices( "OutputPixelType" );
25 // -------------------------------------------------------------------------
26 cpPluginsITKRasterFilters::RasterImageFilter::
31 // -------------------------------------------------------------------------
32 void cpPluginsITKRasterFilters::RasterImageFilter::
35 auto o = this->GetInputData( "Input" );
36 cpPlugins_Demangle_Mesh_Meshes_1( o, _GD0_2D, 2 )
37 cpPlugins_Demangle_Mesh_Meshes_1( o, _GD0_3D, 3 )
38 this->_Error( "Invalid input image dimension." );
41 // -------------------------------------------------------------------------
42 template< class _TMesh >
43 void cpPluginsITKRasterFilters::RasterImageFilter::
44 _GD0_2D( _TMesh* mesh )
47 this->m_Parameters.GetSelectedChoice( "OutputPixelType" );
48 if( p_type == "char" ) this->_GD1_2D< _TMesh, char >( mesh );
49 else if( p_type == "short" ) this->_GD1_2D< _TMesh, short >( mesh );
50 else if( p_type == "int" ) this->_GD1_2D< _TMesh, int >( mesh );
51 else if( p_type == "long" ) this->_GD1_2D< _TMesh, long >( mesh );
52 else if( p_type == "uchar" ) this->_GD1_2D< _TMesh, unsigned char >( mesh );
53 else if( p_type == "ushort" ) this->_GD1_2D< _TMesh, unsigned short >( mesh );
54 else if( p_type == "uint" ) this->_GD1_2D< _TMesh, unsigned int >( mesh );
55 else if( p_type == "ulong" ) this->_GD1_2D< _TMesh, unsigned long >( mesh );
56 else if( p_type == "float" ) this->_GD1_2D< _TMesh, float >( mesh );
57 else if( p_type == "double" ) this->_GD1_2D< _TMesh, double >( mesh );
60 // -------------------------------------------------------------------------
61 template< class _TMesh, class _TPixelType >
62 void cpPluginsITKRasterFilters::RasterImageFilter::
63 _GD1_2D( _TMesh* mesh )
65 typedef itk::Image< _TPixelType, 2 > _TImage;
66 typedef typename _TImage::Superclass _TBaseImage;
67 typedef cpExtensions::Algorithms::RasterContourFilter< _TImage > _TFilter;
70 _TFilter* filter = this->_CreateITK< _TFilter >( );
71 filter->SetInsideValue( this->m_Parameters.GetReal( "InsideValue" ) );
72 filter->SetOutsideValue( this->m_Parameters.GetReal( "OutsideValue" ) );
73 filter->SetTemplate( this->GetInputData< _TBaseImage >( "TemplateImage" ) );
74 filter->ClearPoints( );
75 for( unsigned long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
76 filter->AddPoint( mesh->GetPoint( i ) );
78 // Execute and connect output
80 this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
83 // -------------------------------------------------------------------------
84 template< class _TMesh >
85 void cpPluginsITKRasterFilters::RasterImageFilter::
86 _GD0_3D( _TMesh* mesh )
89 this->m_Parameters.GetSelectedChoice( "OutputPixelType" );
90 if( p_type == "char" ) this->_GD1_3D< _TMesh, char >( mesh );
91 else if( p_type == "short" ) this->_GD1_3D< _TMesh, short >( mesh );
92 else if( p_type == "int" ) this->_GD1_3D< _TMesh, int >( mesh );
93 else if( p_type == "long" ) this->_GD1_3D< _TMesh, long >( mesh );
94 else if( p_type == "uchar" ) this->_GD1_3D< _TMesh, unsigned char >( mesh );
95 else if( p_type == "ushort" ) this->_GD1_3D< _TMesh, unsigned short >( mesh );
96 else if( p_type == "uint" ) this->_GD1_3D< _TMesh, unsigned int >( mesh );
97 else if( p_type == "ulong" ) this->_GD1_3D< _TMesh, unsigned long >( mesh );
98 else if( p_type == "float" ) this->_GD1_3D< _TMesh, float >( mesh );
99 else if( p_type == "double" ) this->_GD1_3D< _TMesh, double >( mesh );
102 // -------------------------------------------------------------------------
103 template< class _TMesh, class _TPixelType >
104 void cpPluginsITKRasterFilters::RasterImageFilter::
105 _GD1_3D( _TMesh* mesh )
107 typedef itk::Image< _TPixelType, 3 > _TImage;
108 typedef typename _TImage::Superclass _TBaseImage;
109 typedef itk::TriangleMeshToBinaryImageFilter< _TMesh, _TImage > _TFilter;
112 _TFilter* filter = this->_CreateITK< _TFilter >( );
114 // Compute bounding box
115 typename _TImage::DirectionType direction;
116 typename _TImage::PointType origin;
117 typename _TImage::SizeType size;
118 typename _TImage::SpacingType spac;
119 auto t_image = this->GetInputData< _TBaseImage >( "TemplateImage" );
120 if( t_image == NULL )
125 direction = t_image->GetDirection( );
126 origin = t_image->GetOrigin( );
127 size = t_image->GetLargestPossibleRegion( ).GetSize( );
128 spac = t_image->GetSpacing( );
133 filter->SetDirection( direction );
134 filter->SetOrigin( origin );
135 filter->SetSize( size );
136 filter->SetSpacing( spac );
137 filter->SetInsideValue( this->m_Parameters.GetReal( "InsideValue" ) );
138 filter->SetOutsideValue( this->m_Parameters.GetReal( "OutsideValue" ) );
140 // Execute and connect output
142 this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );