]> Creatis software - cpPlugins.git/blob - plugins/ImageMeshFilters/RasterMeshFilter.cxx
...
[cpPlugins.git] / plugins / ImageMeshFilters / RasterMeshFilter.cxx
1 #include <ImageMeshFilters/RasterMeshFilter.h>
2 #include <cpPlugins/DataObjects/BoundingBox.h>
3 #include <cpPlugins/DataObjects/Mesh.h>
4
5 #include <itkTriangleMeshToBinaryImageFilter.h>
6 #include <cpExtensions/Algorithms/RasterContourFilter.h>
7
8 // -------------------------------------------------------------------------
9 cpPluginsImageMeshFilters::RasterMeshFilter::
10 RasterMeshFilter( )
11   : Superclass( )
12 {
13   typedef cpPlugins::BaseObjects::DataObject _TDataObject;
14   typedef cpPlugins::DataObjects::Image      _TImage;
15   typedef cpPlugins::DataObjects::Mesh       _TMesh;
16
17   this->_ConfigureInput< _TMesh >( "Input", true, false );
18   this->_ConfigureInput< _TDataObject >( "Template", false, false );
19   this->_ConfigureOutput< _TImage >( "Output" );
20
21   this->m_Parameters.ConfigureAsUint( "InsideValue", 1 );
22   this->m_Parameters.ConfigureAsUint( "OutsideValue", 0 );
23   this->m_Parameters.ConfigureAsUint( "MinimumSize", 100 );
24 }
25
26 // -------------------------------------------------------------------------
27 cpPluginsImageMeshFilters::RasterMeshFilter::
28 ~RasterMeshFilter( )
29 {
30 }
31
32 // -------------------------------------------------------------------------
33 void cpPluginsImageMeshFilters::RasterMeshFilter::
34 _GenerateData( )
35 {
36   typedef itk::Mesh< float, 3 >  _3F;
37   typedef itk::Mesh< double, 3 > _3D;
38
39   bool is_2d = false;
40   auto pd = this->GetInputData< vtkPolyData >( "Input" );
41   if( pd != NULL )
42   {
43     double bounds[ 6 ];
44     pd->GetBounds( bounds );
45     is_2d = ( bounds[ 4 ] == bounds[ 5 ] );
46
47   } // fi
48   if( !is_2d )
49   {
50     auto _3f = this->GetInputData< _3F >( "Input" );
51     auto _3d = this->GetInputData< _3D >( "Input" );
52     if     ( _3f != NULL ) this->_GD0_3D( _3f );
53     else if( _3d != NULL ) this->_GD0_3D( _3d );
54     else this->_Error( "No valid input mesh." );
55   }
56   else
57     this->_GD0_2D( pd );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class _TMesh >
62 void cpPluginsImageMeshFilters::RasterMeshFilter::
63 _GD0_2D( _TMesh* mesh )
64 {
65   typedef unsigned char _TPixel;
66   typedef itk::ImageBase< 2 > _TImageBase;
67   typedef itk::Image< _TPixel, 2 > _TImage;
68   typedef cpExtensions::Algorithms::RasterContourFilter< _TImage > _TFilter;
69
70   auto in_im = this->GetInputData< _TImageBase >( "Template" );
71   if( in_im == NULL )
72     this->_Error( "A template is needed for 2D raster (this is temporal)." );
73   _TPixel inside = _TPixel( this->m_Parameters.GetUint( "InsideValue" ) );
74   _TPixel outside = _TPixel( this->m_Parameters.GetUint( "OutsideValue" ) );
75
76   auto filter = this->_CreateITK< _TFilter >( );
77   filter->ClearPoints( );
78   double pnt[ 3 ];
79   for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
80   {
81     mesh->GetPoint( i, pnt );
82     filter->AddPoint( pnt[ 0 ], pnt[ 1 ] );
83
84   } // rof
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$