#include #include #include #include #include // ------------------------------------------------------------------------- cpPluginsImageMeshFilters::RasterMeshFilter:: RasterMeshFilter( ) : Superclass( ) { typedef cpPlugins::BaseObjects::DataObject _TDataObject; typedef cpPlugins::DataObjects::Image _TImage; typedef cpPlugins::DataObjects::Mesh _TMesh; this->_ConfigureInput< _TMesh >( "Input", true, false ); this->_ConfigureInput< _TDataObject >( "Template", false, false ); this->_ConfigureOutput< _TImage >( "Output" ); this->m_Parameters.ConfigureAsUint( "InsideValue", 1 ); this->m_Parameters.ConfigureAsUint( "OutsideValue", 0 ); this->m_Parameters.ConfigureAsUint( "MinimumSize", 100 ); } // ------------------------------------------------------------------------- cpPluginsImageMeshFilters::RasterMeshFilter:: ~RasterMeshFilter( ) { } // ------------------------------------------------------------------------- void cpPluginsImageMeshFilters::RasterMeshFilter:: _GenerateData( ) { typedef itk::Mesh< float, 3 > _3F; typedef itk::Mesh< double, 3 > _3D; bool is_2d = false; auto pd = this->GetInputData< vtkPolyData >( "Input" ); if( pd != NULL ) { double bounds[ 6 ]; pd->GetBounds( bounds ); is_2d = ( bounds[ 4 ] == bounds[ 5 ] ); } // fi if( !is_2d ) { auto _3f = this->GetInputData< _3F >( "Input" ); auto _3d = this->GetInputData< _3D >( "Input" ); if ( _3f != NULL ) this->_GD0_3D( _3f ); else if( _3d != NULL ) this->_GD0_3D( _3d ); else this->_Error( "No valid input mesh." ); } else this->_GD0_2D( pd ); } // ------------------------------------------------------------------------- template< class _TMesh > void cpPluginsImageMeshFilters::RasterMeshFilter:: _GD0_2D( _TMesh* mesh ) { typedef unsigned char _TPixel; typedef itk::ImageBase< 2 > _TImageBase; typedef itk::Image< _TPixel, 2 > _TImage; typedef cpExtensions::Algorithms::RasterContourFilter< _TImage > _TFilter; auto in_im = this->GetInputData< _TImageBase >( "Template" ); if( in_im == NULL ) this->_Error( "A template is needed for 2D raster (this is temporal)." ); _TPixel inside = _TPixel( this->m_Parameters.GetUint( "InsideValue" ) ); _TPixel outside = _TPixel( this->m_Parameters.GetUint( "OutsideValue" ) ); auto filter = this->_CreateITK< _TFilter >( ); filter->ClearPoints( ); double pnt[ 3 ]; for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i ) { mesh->GetPoint( i, pnt ); filter->AddPoint( pnt[ 0 ], pnt[ 1 ] ); } // rof filter->SetTemplate( in_im ); filter->SetInsideValue( inside ); filter->SetOutsideValue( outside ); filter->Update( ); this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) ); } // ------------------------------------------------------------------------- template< class _TMesh > void cpPluginsImageMeshFilters::RasterMeshFilter:: _GD0_3D( _TMesh* mesh ) { typedef unsigned char _TPixel; typedef cpPlugins::DataObjects::BoundingBox _TBB; typedef itk::ImageBase< _TMesh::PointDimension > _TImageBase; typedef itk::Image< _TPixel, _TMesh::PointDimension > _TImage; typedef itk::TriangleMeshToBinaryImageFilter< _TMesh, _TImage > _TFilter; typedef typename _TImage::PointType _TPoint; static const unsigned int PAD = 10; _TFilter* filter = this->_CreateITK< _TFilter >( ); // Get configuration values typename _TImage::SpacingType spac; typename _TImage::SizeType size; typename _TImage::PointType origin; typename _TImage::DirectionType direction; typename _TImage::IndexType index; auto in_bb = this->GetInput< _TBB >( "Template" ); auto in_im = this->GetInputData< _TImageBase >( "Template" ); if( in_im != NULL ) { spac = in_im->GetSpacing( ); size = in_im->GetLargestPossibleRegion( ).GetSize( ); origin = in_im->GetOrigin( ); direction = in_im->GetDirection( ); index = in_im->GetLargestPossibleRegion( ).GetIndex( ); } else { _TPoint minBB, maxBB; if( in_bb != NULL ) { minBB = in_bb->GetMinimum< _TPoint >( ); maxBB = in_bb->GetMaximum< _TPoint >( ); } else { auto bb = mesh->GetBoundingBox( ); minBB = bb->GetMinimum( ); maxBB = bb->GetMaximum( ); } // fi double lx = double( maxBB[ 0 ] - minBB[ 0 ] ); double ly = double( maxBB[ 1 ] - minBB[ 1 ] ); double lz = double( maxBB[ 2 ] - minBB[ 2 ] ); double lm = ( lx < ly )? lx: ly; lm = ( lm < lz )? lm: lz; // Compute spacing spac.Fill( lm / double( this->m_Parameters.GetUint( "MinimumSize" ) ) ); // Compute size size[ 0 ] = ( unsigned long )( std::ceil( lx / spac[ 0 ] ) ); size[ 1 ] = ( unsigned long )( std::ceil( ly / spac[ 1 ] ) ); size[ 2 ] = ( unsigned long )( std::ceil( lz / spac[ 2 ] ) ); // ... add some padding pixels size[ 0 ] += PAD; size[ 1 ] += PAD; size[ 2 ] += PAD; // Set origin origin = minBB; origin[ 0 ] -= double( PAD >> 1 ) * spac[ 0 ]; origin[ 1 ] -= double( PAD >> 1 ) * spac[ 1 ]; origin[ 2 ] -= double( PAD >> 1 ) * spac[ 2 ]; // Remaining values direction.SetIdentity( ); index.Fill( 0 ); } // fi // Execute filter->SetInput( mesh ); filter->SetSpacing( spac ); filter->SetSize( size ); filter->SetOrigin( origin ); filter->SetDirection( direction ); filter->SetIndex( index ); filter->SetInsideValue( this->m_Parameters.GetUint( "InsideValue" ) ); filter->SetOutsideValue( this->m_Parameters.GetUint( "OutsideValue" ) ); filter->Update( ); this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) ); } // eof - $RCSfile$