]> Creatis software - cpPlugins.git/blob - plugins/ImageMeshFilters/TriangleMeshToBinaryImageFilter.cxx
b5dc0d4017e44ca0159811aa738c0e39d1913ea7
[cpPlugins.git] / plugins / ImageMeshFilters / TriangleMeshToBinaryImageFilter.cxx
1 #include <plugins/ImageMeshFilters/TriangleMeshToBinaryImageFilter.h>
2 #include <cpPlugins/BoundingBox.h>
3 #include <cpPlugins/Image.h>
4 #include <cpPlugins/Mesh.h>
5
6 #include <cpPlugins_Instances_ImageMeshFilters.h>
7
8 // -------------------------------------------------------------------------
9 cpPluginsImageMeshFilters::TriangleMeshToBinaryImageFilter::
10 TriangleMeshToBinaryImageFilter( )
11   : Superclass( )
12 {
13   this->_AddInput( "Input" );
14   this->_AddInput( "BoundingBox", false );
15   this->_AddOutput< cpPlugins::Image >( "Output" );
16
17   this->m_Parameters.ConfigureAsUint( "InsideValue" );
18   this->m_Parameters.ConfigureAsUint( "OutsideValue" );
19   this->m_Parameters.ConfigureAsUint( "MinimumSize" );
20
21   this->m_Parameters.SetUint( "InsideValue", 1 );
22   this->m_Parameters.SetUint( "OutsideValue", 0 );
23   this->m_Parameters.SetUint( "MinimumSize", 100 );
24 }
25
26 // -------------------------------------------------------------------------
27 cpPluginsImageMeshFilters::TriangleMeshToBinaryImageFilter::
28 ~TriangleMeshToBinaryImageFilter( )
29 {
30 }
31
32 // -------------------------------------------------------------------------
33 void cpPluginsImageMeshFilters::TriangleMeshToBinaryImageFilter::
34 _GenerateData( )
35 {
36   typedef itk::Mesh< float, 3 >  _3F;
37   typedef itk::Mesh< double, 3 > _3D;
38
39   auto _3f = this->GetInputData< _3F >( "Input" );
40   auto _3d = this->GetInputData< _3D >( "Input" );
41   if     ( _3f != NULL ) this->_GD0( _3f );
42   else if( _3d != NULL ) this->_GD0( _3d );
43   else this->_Error( "No valid input mesh." );
44 }
45
46 // -------------------------------------------------------------------------
47 template< class _TMesh >
48 void cpPluginsImageMeshFilters::TriangleMeshToBinaryImageFilter::
49 _GD0( _TMesh* mesh )
50 {
51   this->_GD1< _TMesh, unsigned char >( mesh );
52 }
53
54 // -------------------------------------------------------------------------
55 template< class _TMesh, class _TPixel >
56 void cpPluginsImageMeshFilters::TriangleMeshToBinaryImageFilter::
57 _GD1( _TMesh* mesh )
58 {
59   typedef cpPlugins::BoundingBox _TBB;
60   typedef itk::Image< _TPixel, _TMesh::PointDimension > _TImage;
61   typedef itk::TriangleMeshToBinaryImageFilter< _TMesh, _TImage > _TFilter;
62   typedef typename _TImage::PointType _TPoint;
63
64   static const unsigned int PAD = 10;
65
66   _TFilter* filter = this->_CreateITK< _TFilter >( );
67
68   auto in_bb = dynamic_cast< _TBB* >( this->GetInput( "BoundingBox" ) );
69   _TPoint minBB, maxBB;
70   if( in_bb == NULL )
71   {
72     auto bb = mesh->GetBoundingBox( );
73     minBB = bb->GetMinimum( );
74     maxBB = bb->GetMaximum( );
75   }
76   else
77   {
78     minBB = in_bb->GetMinimum< _TPoint >( );
79     maxBB = in_bb->GetMaximum< _TPoint >( );
80
81   } // fi
82
83   double lx = double( maxBB[ 0 ] - minBB[ 0 ] );
84   double ly = double( maxBB[ 1 ] - minBB[ 1 ] );
85   double lz = double( maxBB[ 2 ] - minBB[ 2 ] );
86   double lm = ( lx < ly )? lx: ly;
87   lm = ( lm < lz )? lm: lz;
88
89   // Compute spacing
90   double mSpac = lm / double( this->m_Parameters.GetUint( "MinimumSize" ) );
91   typename _TImage::SpacingType spac;
92   spac.Fill( mSpac );
93   filter->SetSpacing( spac );
94
95   // Compute size
96   typename _TImage::SizeType size;
97   size[ 0 ] = ( unsigned long )( std::ceil( lx / mSpac ) );
98   size[ 1 ] = ( unsigned long )( std::ceil( ly / mSpac ) );
99   size[ 2 ] = ( unsigned long )( std::ceil( lz / mSpac ) );
100
101   // ... add some padding pixels
102   size[ 0 ] += PAD;
103   size[ 1 ] += PAD;
104   size[ 2 ] += PAD;
105   filter->SetSize( size );
106
107   // Set origin
108   typename _TImage::PointType origin = minBB;
109   origin[ 0 ] -= double( PAD >> 1 ) * spac[ 0 ];
110   origin[ 1 ] -= double( PAD >> 1 ) * spac[ 1 ];
111   origin[ 2 ] -= double( PAD >> 1 ) * spac[ 2 ];
112   filter->SetOrigin( origin );
113
114   // Remaining values
115   typename _TImage::DirectionType direction;
116   direction.SetIdentity( );
117   filter->SetDirection( direction );
118
119   typename _TImage::IndexType index;
120   index.Fill( 0 );
121   filter->SetIndex( index );
122
123   // Execute
124   filter->SetInput( mesh );
125   filter->SetInsideValue( this->m_Parameters.GetUint( "InsideValue" ) );
126   filter->SetOutsideValue( this->m_Parameters.GetUint( "OutsideValue" ) );
127   filter->Update( );
128
129   // Connect output
130   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
131 }
132
133 // eof - $RCSfile$