1 #include <plugins/ImageMeshFilters/RasterMeshFilter.h>
2 #include <cpPlugins/DataObjects/BoundingBox.h>
3 #include <cpPlugins/DataObjects/Mesh.h>
4 #include <cpPlugins_ImageIterators.h>
6 #include <itkTriangleMeshToBinaryImageFilter.h>
7 #include <cpExtensions/Algorithms/RasterContourFilter.h>
8 #include <itkTriangleMeshToBinaryImageFilter.hxx>
9 #include <cpExtensions/Algorithms/RasterContourFilter.hxx>
11 // -------------------------------------------------------------------------
12 cpPluginsImageMeshFilters::RasterMeshFilter::
16 typedef cpPlugins::BaseObjects::DataObject _TDataObject;
17 typedef cpPlugins::DataObjects::Image _TImage;
18 typedef cpPlugins::DataObjects::Mesh _TMesh;
20 this->_ConfigureInput< _TMesh >( "Input", true, false );
21 this->_ConfigureInput< _TDataObject >( "Template", false, false );
22 this->_ConfigureOutput< _TImage >( "Output" );
24 this->m_Parameters.ConfigureAsUint( "InsideValue" );
25 this->m_Parameters.ConfigureAsUint( "OutsideValue" );
26 this->m_Parameters.ConfigureAsUint( "MinimumSize" );
28 this->m_Parameters.SetUint( "InsideValue", 1 );
29 this->m_Parameters.SetUint( "OutsideValue", 0 );
30 this->m_Parameters.SetUint( "MinimumSize", 100 );
33 // -------------------------------------------------------------------------
34 cpPluginsImageMeshFilters::RasterMeshFilter::
39 // -------------------------------------------------------------------------
40 void cpPluginsImageMeshFilters::RasterMeshFilter::
43 typedef itk::Mesh< float, 3 > _3F;
44 typedef itk::Mesh< double, 3 > _3D;
47 auto pd = this->GetInputData< vtkPolyData >( "Input" );
51 pd->GetBounds( bounds );
52 is_2d = ( bounds[ 4 ] == bounds[ 5 ] );
57 auto _3f = this->GetInputData< _3F >( "Input" );
58 auto _3d = this->GetInputData< _3D >( "Input" );
59 if ( _3f != NULL ) this->_GD0_3D( _3f );
60 else if( _3d != NULL ) this->_GD0_3D( _3d );
61 else this->_Error( "No valid input mesh." );
67 // -------------------------------------------------------------------------
68 template< class _TMesh >
69 void cpPluginsImageMeshFilters::RasterMeshFilter::
70 _GD0_2D( _TMesh* mesh )
72 typedef unsigned char _TPixel;
73 typedef itk::ImageBase< 2 > _TImageBase;
74 typedef itk::Image< _TPixel, 2 > _TImage;
75 typedef cpExtensions::Algorithms::RasterContourFilter< _TImage > _TFilter;
77 auto in_im = this->GetInputData< _TImageBase >( "Template" );
79 this->_Error( "A template is needed for 2D raster (this is temporal)." );
80 _TPixel inside = _TPixel( this->m_Parameters.GetUint( "InsideValue" ) );
81 _TPixel outside = _TPixel( this->m_Parameters.GetUint( "OutsideValue" ) );
83 auto filter = this->_CreateITK< _TFilter >( );
84 filter->AddPoints( mesh->GetPoints( ) );
85 filter->SetTemplate( in_im );
86 filter->SetInsideValue( inside );
87 filter->SetOutsideValue( outside );
89 this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
92 // -------------------------------------------------------------------------
93 template< class _TMesh >
94 void cpPluginsImageMeshFilters::RasterMeshFilter::
95 _GD0_3D( _TMesh* mesh )
97 typedef unsigned char _TPixel;
98 typedef cpPlugins::DataObjects::BoundingBox _TBB;
99 typedef itk::ImageBase< _TMesh::PointDimension > _TImageBase;
100 typedef itk::Image< _TPixel, _TMesh::PointDimension > _TImage;
101 typedef itk::TriangleMeshToBinaryImageFilter< _TMesh, _TImage > _TFilter;
102 typedef typename _TImage::PointType _TPoint;
104 static const unsigned int PAD = 10;
106 _TFilter* filter = this->_CreateITK< _TFilter >( );
108 // Get configuration values
109 typename _TImage::SpacingType spac;
110 typename _TImage::SizeType size;
111 typename _TImage::PointType origin;
112 typename _TImage::DirectionType direction;
113 typename _TImage::IndexType index;
115 auto in_bb = this->GetInput< _TBB >( "Template" );
116 auto in_im = this->GetInputData< _TImageBase >( "Template" );
119 spac = in_im->GetSpacing( );
120 size = in_im->GetLargestPossibleRegion( ).GetSize( );
121 origin = in_im->GetOrigin( );
122 direction = in_im->GetDirection( );
123 index = in_im->GetLargestPossibleRegion( ).GetIndex( );
127 _TPoint minBB, maxBB;
130 minBB = in_bb->GetMinimum< _TPoint >( );
131 maxBB = in_bb->GetMaximum< _TPoint >( );
135 auto bb = mesh->GetBoundingBox( );
136 minBB = bb->GetMinimum( );
137 maxBB = bb->GetMaximum( );
141 double lx = double( maxBB[ 0 ] - minBB[ 0 ] );
142 double ly = double( maxBB[ 1 ] - minBB[ 1 ] );
143 double lz = double( maxBB[ 2 ] - minBB[ 2 ] );
144 double lm = ( lx < ly )? lx: ly;
145 lm = ( lm < lz )? lm: lz;
148 spac.Fill( lm / double( this->m_Parameters.GetUint( "MinimumSize" ) ) );
151 size[ 0 ] = ( unsigned long )( std::ceil( lx / spac[ 0 ] ) );
152 size[ 1 ] = ( unsigned long )( std::ceil( ly / spac[ 1 ] ) );
153 size[ 2 ] = ( unsigned long )( std::ceil( lz / spac[ 2 ] ) );
155 // ... add some padding pixels
162 origin[ 0 ] -= double( PAD >> 1 ) * spac[ 0 ];
163 origin[ 1 ] -= double( PAD >> 1 ) * spac[ 1 ];
164 origin[ 2 ] -= double( PAD >> 1 ) * spac[ 2 ];
167 direction.SetIdentity( );
173 filter->SetInput( mesh );
174 filter->SetSpacing( spac );
175 filter->SetSize( size );
176 filter->SetOrigin( origin );
177 filter->SetDirection( direction );
178 filter->SetIndex( index );
179 filter->SetInsideValue( this->m_Parameters.GetUint( "InsideValue" ) );
180 filter->SetOutsideValue( this->m_Parameters.GetUint( "OutsideValue" ) );
182 this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );