+#include "TriangleMeshToBinaryImageFilter.h"
+#include <cpPlugins/Interface/Image.h>
+#include <cpPlugins/Interface/Mesh.h>
+
+#include <itkMesh.h>
+#include <itkTriangleMeshToBinaryImageFilter.h>
+
+// -------------------------------------------------------------------------
+cpPlugins::BasicFilters::TriangleMeshToBinaryImageFilter::
+TriangleMeshToBinaryImageFilter( )
+ : Superclass( )
+{
+ this->_AddInput( "Input" );
+ this->_AddOutput< cpPlugins::Interface::Image >( "Output" );
+
+ this->m_Parameters->ConfigureAsUint( "InsideValue" );
+ this->m_Parameters->ConfigureAsUint( "OutsideValue" );
+ this->m_Parameters->ConfigureAsUint( "MinimumSize" );
+
+ this->m_Parameters->SetUint( "InsideValue", 1 );
+ this->m_Parameters->SetUint( "OutsideValue", 0 );
+ this->m_Parameters->SetUint( "MinimumSize", 100 );
+}
+
+// -------------------------------------------------------------------------
+cpPlugins::BasicFilters::TriangleMeshToBinaryImageFilter::
+~TriangleMeshToBinaryImageFilter( )
+{
+}
+
+// -------------------------------------------------------------------------
+std::string cpPlugins::BasicFilters::TriangleMeshToBinaryImageFilter::
+_GenerateData( )
+{
+ typedef itk::Mesh< float, 3 > _3F;
+ typedef itk::Mesh< double, 3 > _3D;
+
+ auto input = this->GetInputData< cpPlugins::Interface::Mesh >( "Input" );
+ if( input == NULL )
+ return( "TriangleMeshToBinaryImageFilter: No valid input mesh." );
+
+ auto in_3f = input->GetITK< _3F >( );
+ auto in_3d = input->GetITK< _3D >( );
+ if ( in_3f != NULL ) return( this->_GD0( in_3f ) );
+ else if( in_3d != NULL ) return( this->_GD0( in_3d ) );
+ else
+ return( "TriangleMeshToBinaryImageFilter: No valid input itk mesh." );
+}
+
+// -------------------------------------------------------------------------
+template< class M >
+std::string cpPlugins::BasicFilters::TriangleMeshToBinaryImageFilter::
+_GD0( M* mesh )
+{
+ return(
+ this->_RealGD< M, itk::Image< unsigned char, M::PointDimension > >(
+ mesh
+ )
+ );
+}
+
+// -------------------------------------------------------------------------
+template< class M, class I >
+inline std::string cpPlugins::BasicFilters::TriangleMeshToBinaryImageFilter::
+_RealGD( M* mesh )
+{
+ static const unsigned int PAD = 10;
+
+ typedef itk::TriangleMeshToBinaryImageFilter< M, I > _F;
+
+ _F* filter = this->_CreateITK< _F >( );
+
+ auto bb = mesh->GetBoundingBox( );
+ auto minBB = bb->GetMinimum( );
+ auto maxBB = bb->GetMaximum( );
+
+ double lx = double( maxBB[ 0 ] - minBB[ 0 ] );
+ double ly = double( maxBB[ 1 ] - minBB[ 1 ] );
+ double lz = double( maxBB[ 2 ] - minBB[ 2 ] );
+ double lm = ( lx < ly )? lx: ly;
+ lm = ( lm < lz )? lm: lz;
+
+ // Compute spacing
+ double mSpac = lm / double( this->m_Parameters->GetUint( "MinimumSize" ) );
+ typename I::SpacingType spac;
+ spac.Fill( mSpac );
+ filter->SetSpacing( spac );
+
+ // Compute size
+ typename I::SizeType size;
+ size[ 0 ] = ( unsigned long )( std::ceil( lx / mSpac ) );
+ size[ 1 ] = ( unsigned long )( std::ceil( ly / mSpac ) );
+ size[ 2 ] = ( unsigned long )( std::ceil( lz / mSpac ) );
+
+ // ... add some padding pixels
+ size[ 0 ] += PAD;
+ size[ 1 ] += PAD;
+ size[ 2 ] += PAD;
+ filter->SetSize( size );
+
+ // Set origin
+ typename I::PointType origin = minBB;
+ origin[ 0 ] -= double( PAD >> 1 ) * spac[ 0 ];
+ origin[ 1 ] -= double( PAD >> 1 ) * spac[ 1 ];
+ origin[ 2 ] -= double( PAD >> 1 ) * spac[ 2 ];
+ filter->SetOrigin( origin );
+
+ // Remaining values
+ typename I::DirectionType direction;
+ direction.SetIdentity( );
+ filter->SetDirection( direction );
+
+ typename I::IndexType index;
+ index.Fill( 0 );
+ filter->SetIndex( index );
+
+ // Execute
+ filter->SetInput( mesh );
+ filter->Update( );
+
+ // Connect output
+ auto out = this->GetOutputData< cpPlugins::Interface::Image >( "Output" );
+ if( out != NULL )
+ {
+ out->SetITK< I >( filter->GetOutput( ) );
+ return( "" );
+ }
+ else
+ return( "TriangleMeshToBinaryImageFilter: output not correctly created." );
+}
+
+// eof - $RCSfile$