1 #include <cpPluginsImageMeshFilters/TriangleMeshToBinaryImageFilter.h>
2 #include <cpPlugins/BoundingBox.h>
3 #include <cpPlugins/Image.h>
4 #include <cpPlugins/Mesh.h>
6 #include <cpPlugins_Instances/ImageMeshFilters.h>
8 // -------------------------------------------------------------------------
9 cpPluginsImageMeshFilters::TriangleMeshToBinaryImageFilter::
10 TriangleMeshToBinaryImageFilter( )
13 this->_AddInput( "Input" );
14 this->_AddInput( "BoundingBox", false );
15 this->_AddOutput< cpPlugins::Image >( "Output" );
17 this->m_Parameters.ConfigureAsUint( "InsideValue" );
18 this->m_Parameters.ConfigureAsUint( "OutsideValue" );
19 this->m_Parameters.ConfigureAsUint( "MinimumSize" );
21 this->m_Parameters.SetUint( "InsideValue", 1 );
22 this->m_Parameters.SetUint( "OutsideValue", 0 );
23 this->m_Parameters.SetUint( "MinimumSize", 100 );
26 // -------------------------------------------------------------------------
27 cpPluginsImageMeshFilters::TriangleMeshToBinaryImageFilter::
28 ~TriangleMeshToBinaryImageFilter( )
32 // -------------------------------------------------------------------------
33 std::string cpPluginsImageMeshFilters::TriangleMeshToBinaryImageFilter::
36 typedef itk::Mesh< float, 3 > _3F;
37 typedef itk::Mesh< double, 3 > _3D;
39 auto _3f = this->GetInputData( "Input" )->GetITK< _3F >( );
40 auto _3d = this->GetInputData( "Input" )->GetITK< _3D >( );
41 if ( _3f != NULL ) return( this->_GD0( _3f ) );
42 else if( _3d != NULL ) return( this->_GD0( _3d ) );
44 return( "TriangleMeshToBinaryImageFilter: No valid input itk mesh." );
47 // -------------------------------------------------------------------------
48 template< class _TMesh >
49 std::string cpPluginsImageMeshFilters::TriangleMeshToBinaryImageFilter::
52 return( this->_GD1< _TMesh, unsigned char >( mesh ) );
55 // -------------------------------------------------------------------------
56 template< class _TMesh, class _TPixel >
57 std::string cpPluginsImageMeshFilters::TriangleMeshToBinaryImageFilter::
60 typedef cpPlugins::BoundingBox _TBB;
61 typedef itk::Image< _TPixel, _TMesh::PointDimension > _TImage;
62 typedef itk::TriangleMeshToBinaryImageFilter< _TMesh, _TImage > _TFilter;
63 typedef typename _TImage::PointType _TPoint;
65 static const unsigned int PAD = 10;
67 _TFilter* filter = this->_CreateITK< _TFilter >( );
69 auto in_bb = dynamic_cast< _TBB* >( this->GetInputData( "BoundingBox" ) );
73 auto bb = mesh->GetBoundingBox( );
74 minBB = bb->GetMinimum( );
75 maxBB = bb->GetMaximum( );
79 minBB = in_bb->GetMinimum< _TPoint >( );
80 maxBB = in_bb->GetMaximum< _TPoint >( );
84 double lx = double( maxBB[ 0 ] - minBB[ 0 ] );
85 double ly = double( maxBB[ 1 ] - minBB[ 1 ] );
86 double lz = double( maxBB[ 2 ] - minBB[ 2 ] );
87 double lm = ( lx < ly )? lx: ly;
88 lm = ( lm < lz )? lm: lz;
91 double mSpac = lm / double( this->m_Parameters.GetUint( "MinimumSize" ) );
92 typename _TImage::SpacingType spac;
94 filter->SetSpacing( spac );
97 typename _TImage::SizeType size;
98 size[ 0 ] = ( unsigned long )( std::ceil( lx / mSpac ) );
99 size[ 1 ] = ( unsigned long )( std::ceil( ly / mSpac ) );
100 size[ 2 ] = ( unsigned long )( std::ceil( lz / mSpac ) );
102 // ... add some padding pixels
106 filter->SetSize( size );
109 typename _TImage::PointType origin = minBB;
110 origin[ 0 ] -= double( PAD >> 1 ) * spac[ 0 ];
111 origin[ 1 ] -= double( PAD >> 1 ) * spac[ 1 ];
112 origin[ 2 ] -= double( PAD >> 1 ) * spac[ 2 ];
113 filter->SetOrigin( origin );
116 typename _TImage::DirectionType direction;
117 direction.SetIdentity( );
118 filter->SetDirection( direction );
120 typename _TImage::IndexType index;
122 filter->SetIndex( index );
125 filter->SetInput( mesh );
126 filter->SetInsideValue( this->m_Parameters.GetUint( "InsideValue" ) );
127 filter->SetOutsideValue( this->m_Parameters.GetUint( "OutsideValue" ) );
131 auto out = this->GetOutputData( "Output" );
132 out->SetITK( filter->GetOutput( ) );