]> Creatis software - cpPlugins.git/blob - plugins/ITKRasterFilters/RasterImageFilter.cxx
...
[cpPlugins.git] / plugins / ITKRasterFilters / RasterImageFilter.cxx
1 #include <ITKRasterFilters/RasterImageFilter.h>
2 #include <cpInstances/DataObjects/Image.h>
3 #include <cpInstances/DataObjects/Mesh.h>
4
5 #include <itkTriangleMeshToBinaryImageFilter.h>
6 #include <cpExtensions/Algorithms/RasterContourFilter.h>
7
8 // -------------------------------------------------------------------------
9 cpPluginsITKRasterFilters::RasterImageFilter::
10 RasterImageFilter( )
11   : Superclass( )
12 {
13   typedef cpInstances::DataObjects::Mesh  _TMesh;
14   typedef cpInstances::DataObjects::Image _TImage;
15
16   this->_ConfigureInput< _TMesh >( "Input", true, false );
17   this->_ConfigureInput< _TImage >( "TemplateImage", false, false );
18   this->_ConfigureOutput< _TImage >( "Output" );
19
20   this->m_Parameters.ConfigureAsReal( "InsideValue", 1 );
21   this->m_Parameters.ConfigureAsReal( "OutsideValue", 0 );
22   this->m_Parameters.ConfigureAsScalarTypesChoices( "OutputPixelType" );
23 }
24
25 // -------------------------------------------------------------------------
26 cpPluginsITKRasterFilters::RasterImageFilter::
27 ~RasterImageFilter( )
28 {
29 }
30
31 // -------------------------------------------------------------------------
32 void cpPluginsITKRasterFilters::RasterImageFilter::
33 _GenerateData( )
34 {
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." );
39 }
40
41 // -------------------------------------------------------------------------
42 template< class _TMesh >
43 void cpPluginsITKRasterFilters::RasterImageFilter::
44 _GD0_2D( _TMesh* mesh )
45 {
46   std::string p_type =
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 );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class _TMesh, class _TPixelType >
62 void cpPluginsITKRasterFilters::RasterImageFilter::
63 _GD1_2D( _TMesh* mesh )
64 {
65   typedef itk::Image< _TPixelType, 2 > _TImage;
66   typedef typename _TImage::Superclass _TBaseImage;
67   typedef cpExtensions::Algorithms::RasterContourFilter< _TImage > _TFilter;
68
69   // Configure filter
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 ) );
77
78   // Execute and connect output
79   filter->Update( );
80   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
81 }
82
83 // -------------------------------------------------------------------------
84 template< class _TMesh >
85 void cpPluginsITKRasterFilters::RasterImageFilter::
86 _GD0_3D( _TMesh* mesh )
87 {
88   std::string p_type =
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 );
100 }
101
102 // -------------------------------------------------------------------------
103 template< class _TMesh, class _TPixelType >
104 void cpPluginsITKRasterFilters::RasterImageFilter::
105 _GD1_3D( _TMesh* mesh )
106 {
107   typedef itk::Image< _TPixelType, 3 > _TImage;
108   typedef typename _TImage::Superclass _TBaseImage;
109   typedef itk::TriangleMeshToBinaryImageFilter< _TMesh, _TImage > _TFilter;
110
111   // Create filter
112   _TFilter* filter = this->_CreateITK< _TFilter >( );
113
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 )
121   {
122   }
123   else
124   {
125     direction = t_image->GetDirection( );
126     origin = t_image->GetOrigin( );
127     size = t_image->GetLargestPossibleRegion( ).GetSize( );
128     spac = t_image->GetSpacing( );
129
130   } // fi
131
132   // Configure filter
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" ) );
139
140   // Execute and connect output
141   filter->Update( );
142   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
143 }
144
145 // eof - $RCSfile$