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