]> Creatis software - cpPlugins.git/blob - plugins/ImageMeshFilters/RasterMeshFilter.cxx
Windows compilation 3/3
[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->ClearPoints( );
85   double pnt[ 3 ];
86   for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
87   {
88     mesh->GetPoint( i, pnt );
89     filter->AddPoint( pnt[ 0 ], pnt[ 1 ] );
90
91   } // rof
92   filter->SetTemplate( in_im );
93   filter->SetInsideValue( inside );
94   filter->SetOutsideValue( outside );
95   filter->Update( );
96   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
97 }
98
99 // -------------------------------------------------------------------------
100 template< class _TMesh >
101 void cpPluginsImageMeshFilters::RasterMeshFilter::
102 _GD0_3D( _TMesh* mesh )
103 {
104   typedef unsigned char _TPixel;
105   typedef cpPlugins::DataObjects::BoundingBox _TBB;
106   typedef itk::ImageBase< _TMesh::PointDimension > _TImageBase;
107   typedef itk::Image< _TPixel, _TMesh::PointDimension > _TImage;
108   typedef itk::TriangleMeshToBinaryImageFilter< _TMesh, _TImage > _TFilter;
109   typedef typename _TImage::PointType _TPoint;
110
111   static const unsigned int PAD = 10;
112
113   _TFilter* filter = this->_CreateITK< _TFilter >( );
114
115   // Get configuration values
116   typename _TImage::SpacingType spac;
117   typename _TImage::SizeType size;
118   typename _TImage::PointType origin;
119   typename _TImage::DirectionType direction;
120   typename _TImage::IndexType index;
121
122   auto in_bb = this->GetInput< _TBB >( "Template" );
123   auto in_im = this->GetInputData< _TImageBase >( "Template" );
124   if( in_im != NULL )
125   {
126     spac = in_im->GetSpacing( );
127     size = in_im->GetLargestPossibleRegion( ).GetSize( );
128     origin = in_im->GetOrigin( );
129     direction = in_im->GetDirection( );
130     index = in_im->GetLargestPossibleRegion( ).GetIndex( );
131   }
132   else
133   {
134     _TPoint minBB, maxBB;
135     if( in_bb != NULL )
136     {
137       minBB = in_bb->GetMinimum< _TPoint >( );
138       maxBB = in_bb->GetMaximum< _TPoint >( );
139     }
140     else
141     {
142       auto bb = mesh->GetBoundingBox( );
143       minBB = bb->GetMinimum( );
144       maxBB = bb->GetMaximum( );
145
146     } // fi
147
148     double lx = double( maxBB[ 0 ] - minBB[ 0 ] );
149     double ly = double( maxBB[ 1 ] - minBB[ 1 ] );
150     double lz = double( maxBB[ 2 ] - minBB[ 2 ] );
151     double lm = ( lx < ly )? lx: ly;
152     lm = ( lm < lz )? lm: lz;
153
154     // Compute spacing
155     spac.Fill( lm / double( this->m_Parameters.GetUint( "MinimumSize" ) ) );
156
157     // Compute size
158     size[ 0 ] = ( unsigned long )( std::ceil( lx / spac[ 0 ] ) );
159     size[ 1 ] = ( unsigned long )( std::ceil( ly / spac[ 1 ] ) );
160     size[ 2 ] = ( unsigned long )( std::ceil( lz / spac[ 2 ] ) );
161
162     // ... add some padding pixels
163     size[ 0 ] += PAD;
164     size[ 1 ] += PAD;
165     size[ 2 ] += PAD;
166
167     // Set origin
168     origin = minBB;
169     origin[ 0 ] -= double( PAD >> 1 ) * spac[ 0 ];
170     origin[ 1 ] -= double( PAD >> 1 ) * spac[ 1 ];
171     origin[ 2 ] -= double( PAD >> 1 ) * spac[ 2 ];
172
173     // Remaining values
174     direction.SetIdentity( );
175     index.Fill( 0 );
176
177   } // fi
178
179   // Execute
180   filter->SetInput( mesh );
181   filter->SetSpacing( spac );
182   filter->SetSize( size );
183   filter->SetOrigin( origin );
184   filter->SetDirection( direction );
185   filter->SetIndex( index );
186   filter->SetInsideValue( this->m_Parameters.GetUint( "InsideValue" ) );
187   filter->SetOutsideValue( this->m_Parameters.GetUint( "OutsideValue" ) );
188   filter->Update( );
189   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
190 }
191
192 // eof - $RCSfile$