]> Creatis software - cpPlugins.git/blob - plugins/ITKRasterFilters/RasterImageFilter.cxx
097566c9664cfe660ee45eec87b53796599d4a6b
[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 <vtkPolyData.h>
6 #include <itkTriangleMeshToBinaryImageFilter.h>
7 #include <cpExtensions/Algorithms/RasterContourFilter.h>
8
9 // -------------------------------------------------------------------------
10 cpPluginsITKRasterFilters::RasterImageFilter::
11 RasterImageFilter( )
12   : Superclass( )
13 {
14   typedef cpInstances::DataObjects::Mesh  _TMesh;
15   typedef cpInstances::DataObjects::Image _TImage;
16
17   this->_ConfigureInput< _TMesh >( "Input", true, false );
18   this->_ConfigureInput< _TImage >( "TemplateImage", false, false );
19   this->_ConfigureOutput< _TImage >( "Output" );
20
21   this->m_Parameters.ConfigureAsReal( "InsideValue", 1 );
22   this->m_Parameters.ConfigureAsReal( "OutsideValue", 0 );
23   this->m_Parameters.ConfigureAsScalarTypesChoices( "OutputPixelType" );
24 }
25
26 // -------------------------------------------------------------------------
27 cpPluginsITKRasterFilters::RasterImageFilter::
28 ~RasterImageFilter( )
29 {
30 }
31
32 // -------------------------------------------------------------------------
33 void cpPluginsITKRasterFilters::RasterImageFilter::
34 _GenerateData( )
35 {
36   auto mesh = this->GetInputData< vtkPolyData >( "Input" );
37   double bounds[ 6 ];
38   mesh->GetBounds( bounds );
39   double zdiff = std::fabs( bounds[ 5 ] - bounds[ 4 ] );
40   if( zdiff > 1e-5 )
41   {
42     cpPlugins_Demangle_Mesh_Meshes_1(
43       this->GetInputData( "Input" ), _GD0_3D, 3
44       )
45       this->_Error( "Invalid input image dimension." );
46   }
47   else
48     this->_GD0_2D( mesh );
49 }
50
51 // -------------------------------------------------------------------------
52 template< class _TMesh >
53 void cpPluginsITKRasterFilters::RasterImageFilter::
54 _GD0_2D( _TMesh* mesh )
55 {
56   std::string p_type =
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 );
68 }
69
70 // -------------------------------------------------------------------------
71 template< class _TMesh, class _TPixelType >
72 void cpPluginsITKRasterFilters::RasterImageFilter::
73 _GD1_2D( _TMesh* mesh )
74 {
75   typedef itk::Image< _TPixelType, 2 > _TImage;
76   typedef typename _TImage::Superclass _TBaseImage;
77   typedef cpExtensions::Algorithms::RasterContourFilter< _TImage > _TFilter;
78
79   // Configure filter
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 )
86   {
87     double pnt[ 3 ];
88     mesh->GetPoint( i, pnt );
89     filter->AddPoint( pnt );
90
91   } // rof
92
93   // Execute and connect output
94   filter->Update( );
95   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
96 }
97
98 // -------------------------------------------------------------------------
99 template< class _TMesh >
100 void cpPluginsITKRasterFilters::RasterImageFilter::
101 _GD0_3D( _TMesh* mesh )
102 {
103   std::string p_type =
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 );
115 }
116
117 // -------------------------------------------------------------------------
118 template< class _TMesh, class _TPixelType >
119 void cpPluginsITKRasterFilters::RasterImageFilter::
120 _GD1_3D( _TMesh* mesh )
121 {
122   typedef itk::Image< _TPixelType, 3 > _TImage;
123   typedef typename _TImage::Superclass _TBaseImage;
124   typedef itk::TriangleMeshToBinaryImageFilter< _TMesh, _TImage > _TFilter;
125
126   // Create filter
127   _TFilter* filter = this->_CreateITK< _TFilter >( );
128
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 )
136   {
137   }
138   else
139   {
140     direction = t_image->GetDirection( );
141     origin = t_image->GetOrigin( );
142     size = t_image->GetLargestPossibleRegion( ).GetSize( );
143     spac = t_image->GetSpacing( );
144
145   } // fi
146
147   // Configure filter
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" ) );
154
155   // Execute and connect output
156   filter->Update( );
157   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
158 }
159
160 // eof - $RCSfile$