]> Creatis software - cpPlugins.git/blob - plugins/ImageMeshFilters/RasterMeshFilter.cxx
4124cb1232db6b88ab851a75bedcbc062460f401
[cpPlugins.git] / plugins / ImageMeshFilters / RasterMeshFilter.cxx
1 #include <plugins/ImageMeshFilters/RasterMeshFilter.h>
2 #include <cpPlugins/DataObjects/BoundingBox.h>
3 #include <cpPlugins/DataObjects/Mesh.h>
4 #include <cpPlugins_ImageIterators.h>
5
6 #include <itkTriangleMeshToBinaryImageFilter.h>
7 #include <cpExtensions/Algorithms/RasterContourFilter.h>
8 #include <itkTriangleMeshToBinaryImageFilter.hxx>
9 #include <cpExtensions/Algorithms/RasterContourFilter.hxx>
10
11 // -------------------------------------------------------------------------
12 cpPluginsImageMeshFilters::RasterMeshFilter::
13 RasterMeshFilter( )
14   : Superclass( )
15 {
16   typedef cpPlugins::BaseObjects::DataObject _TDataObject;
17   typedef cpPlugins::DataObjects::Image      _TImage;
18   typedef cpPlugins::DataObjects::Mesh       _TMesh;
19
20   this->_ConfigureInput< _TMesh >( "Input", true, false );
21   this->_ConfigureInput< _TDataObject >( "Template", false, false );
22   this->_ConfigureOutput< _TImage >( "Output" );
23
24   this->m_Parameters.ConfigureAsUint( "InsideValue" );
25   this->m_Parameters.ConfigureAsUint( "OutsideValue" );
26   this->m_Parameters.ConfigureAsUint( "MinimumSize" );
27
28   this->m_Parameters.SetUint( "InsideValue", 1 );
29   this->m_Parameters.SetUint( "OutsideValue", 0 );
30   this->m_Parameters.SetUint( "MinimumSize", 100 );
31 }
32
33 // -------------------------------------------------------------------------
34 cpPluginsImageMeshFilters::RasterMeshFilter::
35 ~RasterMeshFilter( )
36 {
37 }
38
39 // -------------------------------------------------------------------------
40 void cpPluginsImageMeshFilters::RasterMeshFilter::
41 _GenerateData( )
42 {
43   typedef itk::Mesh< float, 3 >  _3F;
44   typedef itk::Mesh< double, 3 > _3D;
45
46   bool is_2d = false;
47   auto pd = this->GetInputData< vtkPolyData >( "Input" );
48   if( pd != NULL )
49   {
50     double bounds[ 6 ];
51     pd->GetBounds( bounds );
52     is_2d = ( bounds[ 4 ] == bounds[ 5 ] );
53
54   } // fi
55   if( !is_2d )
56   {
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." );
62   }
63   else
64     this->_GD0_2D( pd );
65 }
66
67 // -------------------------------------------------------------------------
68 template< class _TMesh >
69 void cpPluginsImageMeshFilters::RasterMeshFilter::
70 _GD0_2D( _TMesh* mesh )
71 {
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;
76
77   auto in_im = this->GetInputData< _TImageBase >( "Template" );
78   if( in_im == NULL )
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" ) );
82
83   auto filter = this->_CreateITK< _TFilter >( );
84   filter->AddPoints( mesh->GetPoints( ) );
85   filter->SetTemplate( in_im );
86   filter->SetInsideValue( inside );
87   filter->SetOutsideValue( outside );
88   filter->Update( );
89   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
90 }
91
92 // -------------------------------------------------------------------------
93 template< class _TMesh >
94 void cpPluginsImageMeshFilters::RasterMeshFilter::
95 _GD0_3D( _TMesh* mesh )
96 {
97   typedef unsigned char _TPixel;
98   typedef cpPlugins::DataObjects::BoundingBox _TBB;
99   typedef itk::ImageBase< _TMesh::PointDimension > _TImageBase;
100   typedef itk::Image< _TPixel, _TMesh::PointDimension > _TImage;
101   typedef itk::TriangleMeshToBinaryImageFilter< _TMesh, _TImage > _TFilter;
102   typedef typename _TImage::PointType _TPoint;
103
104   static const unsigned int PAD = 10;
105
106   _TFilter* filter = this->_CreateITK< _TFilter >( );
107
108   // Get configuration values
109   typename _TImage::SpacingType spac;
110   typename _TImage::SizeType size;
111   typename _TImage::PointType origin;
112   typename _TImage::DirectionType direction;
113   typename _TImage::IndexType index;
114
115   auto in_bb = this->GetInput< _TBB >( "Template" );
116   auto in_im = this->GetInputData< _TImageBase >( "Template" );
117   if( in_im != NULL )
118   {
119     spac = in_im->GetSpacing( );
120     size = in_im->GetLargestPossibleRegion( ).GetSize( );
121     origin = in_im->GetOrigin( );
122     direction = in_im->GetDirection( );
123     index = in_im->GetLargestPossibleRegion( ).GetIndex( );
124   }
125   else
126   {
127     _TPoint minBB, maxBB;
128     if( in_bb != NULL )
129     {
130       minBB = in_bb->GetMinimum< _TPoint >( );
131       maxBB = in_bb->GetMaximum< _TPoint >( );
132     }
133     else
134     {
135       auto bb = mesh->GetBoundingBox( );
136       minBB = bb->GetMinimum( );
137       maxBB = bb->GetMaximum( );
138
139     } // fi
140
141     double lx = double( maxBB[ 0 ] - minBB[ 0 ] );
142     double ly = double( maxBB[ 1 ] - minBB[ 1 ] );
143     double lz = double( maxBB[ 2 ] - minBB[ 2 ] );
144     double lm = ( lx < ly )? lx: ly;
145     lm = ( lm < lz )? lm: lz;
146
147     // Compute spacing
148     spac.Fill( lm / double( this->m_Parameters.GetUint( "MinimumSize" ) ) );
149
150     // Compute size
151     size[ 0 ] = ( unsigned long )( std::ceil( lx / spac[ 0 ] ) );
152     size[ 1 ] = ( unsigned long )( std::ceil( ly / spac[ 1 ] ) );
153     size[ 2 ] = ( unsigned long )( std::ceil( lz / spac[ 2 ] ) );
154
155     // ... add some padding pixels
156     size[ 0 ] += PAD;
157     size[ 1 ] += PAD;
158     size[ 2 ] += PAD;
159
160     // Set origin
161     origin = minBB;
162     origin[ 0 ] -= double( PAD >> 1 ) * spac[ 0 ];
163     origin[ 1 ] -= double( PAD >> 1 ) * spac[ 1 ];
164     origin[ 2 ] -= double( PAD >> 1 ) * spac[ 2 ];
165
166     // Remaining values
167     direction.SetIdentity( );
168     index.Fill( 0 );
169
170   } // fi
171
172   // Execute
173   filter->SetInput( mesh );
174   filter->SetSpacing( spac );
175   filter->SetSize( size );
176   filter->SetOrigin( origin );
177   filter->SetDirection( direction );
178   filter->SetIndex( index );
179   filter->SetInsideValue( this->m_Parameters.GetUint( "InsideValue" ) );
180   filter->SetOutsideValue( this->m_Parameters.GetUint( "OutsideValue" ) );
181   filter->Update( );
182   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
183 }
184
185 // eof - $RCSfile$