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->ClearPoints( );
86 for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
88 mesh->GetPoint( i, pnt );
89 filter->AddPoint( pnt[ 0 ], pnt[ 1 ] );
92 filter->SetTemplate( in_im );
93 filter->SetInsideValue( inside );
94 filter->SetOutsideValue( outside );
96 this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
99 // -------------------------------------------------------------------------
100 template< class _TMesh >
101 void cpPluginsImageMeshFilters::RasterMeshFilter::
102 _GD0_3D( _TMesh* mesh )
104 typedef unsigned char _TPixel;
105 typedef cpPlugins::DataObjects::BoundingBox _TBB;
106 typedef itk::ImageBase< _TMesh::PointDimension > _TImageBase;
107 typedef itk::Image< _TPixel, _TMesh::PointDimension > _TImage;
108 typedef itk::TriangleMeshToBinaryImageFilter< _TMesh, _TImage > _TFilter;
109 typedef typename _TImage::PointType _TPoint;
111 static const unsigned int PAD = 10;
113 _TFilter* filter = this->_CreateITK< _TFilter >( );
115 // Get configuration values
116 typename _TImage::SpacingType spac;
117 typename _TImage::SizeType size;
118 typename _TImage::PointType origin;
119 typename _TImage::DirectionType direction;
120 typename _TImage::IndexType index;
122 auto in_bb = this->GetInput< _TBB >( "Template" );
123 auto in_im = this->GetInputData< _TImageBase >( "Template" );
126 spac = in_im->GetSpacing( );
127 size = in_im->GetLargestPossibleRegion( ).GetSize( );
128 origin = in_im->GetOrigin( );
129 direction = in_im->GetDirection( );
130 index = in_im->GetLargestPossibleRegion( ).GetIndex( );
134 _TPoint minBB, maxBB;
137 minBB = in_bb->GetMinimum< _TPoint >( );
138 maxBB = in_bb->GetMaximum< _TPoint >( );
142 auto bb = mesh->GetBoundingBox( );
143 minBB = bb->GetMinimum( );
144 maxBB = bb->GetMaximum( );
148 double lx = double( maxBB[ 0 ] - minBB[ 0 ] );
149 double ly = double( maxBB[ 1 ] - minBB[ 1 ] );
150 double lz = double( maxBB[ 2 ] - minBB[ 2 ] );
151 double lm = ( lx < ly )? lx: ly;
152 lm = ( lm < lz )? lm: lz;
155 spac.Fill( lm / double( this->m_Parameters.GetUint( "MinimumSize" ) ) );
158 size[ 0 ] = ( unsigned long )( std::ceil( lx / spac[ 0 ] ) );
159 size[ 1 ] = ( unsigned long )( std::ceil( ly / spac[ 1 ] ) );
160 size[ 2 ] = ( unsigned long )( std::ceil( lz / spac[ 2 ] ) );
162 // ... add some padding pixels
169 origin[ 0 ] -= double( PAD >> 1 ) * spac[ 0 ];
170 origin[ 1 ] -= double( PAD >> 1 ) * spac[ 1 ];
171 origin[ 2 ] -= double( PAD >> 1 ) * spac[ 2 ];
174 direction.SetIdentity( );
180 filter->SetInput( mesh );
181 filter->SetSpacing( spac );
182 filter->SetSize( size );
183 filter->SetOrigin( origin );
184 filter->SetDirection( direction );
185 filter->SetIndex( index );
186 filter->SetInsideValue( this->m_Parameters.GetUint( "InsideValue" ) );
187 filter->SetOutsideValue( this->m_Parameters.GetUint( "OutsideValue" ) );
189 this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );