From 706ab4caa20c5027cbbe5e6ace379529f6f99c64 Mon Sep 17 00:00:00 2001 From: Leonardo Florez-Valencia Date: Tue, 2 Feb 2016 12:06:40 -0500 Subject: [PATCH] ... --- lib/cpPlugins/Interface/Mesh.h | 2 +- lib/cpPlugins/Interface/Mesh.hxx | 64 ++++++++- .../TriangleMeshToBinaryImageFilter.cxx | 132 ++++++++++++++++++ .../TriangleMeshToBinaryImageFilter.h | 57 ++++++++ 4 files changed, 251 insertions(+), 4 deletions(-) create mode 100644 lib/cpPlugins/Plugins/BasicFilters/TriangleMeshToBinaryImageFilter.cxx create mode 100644 lib/cpPlugins/Plugins/BasicFilters/TriangleMeshToBinaryImageFilter.h diff --git a/lib/cpPlugins/Interface/Mesh.h b/lib/cpPlugins/Interface/Mesh.h index 10752ba..8754b0e 100644 --- a/lib/cpPlugins/Interface/Mesh.h +++ b/lib/cpPlugins/Interface/Mesh.h @@ -28,7 +28,7 @@ namespace cpPlugins public: template< class M > - inline void SetITK( itk::Object* object ); + inline void SetITK( M* mesh ); virtual void SetVTK( vtkObject* mesh ); diff --git a/lib/cpPlugins/Interface/Mesh.hxx b/lib/cpPlugins/Interface/Mesh.hxx index 6de611e..5c5e562 100644 --- a/lib/cpPlugins/Interface/Mesh.hxx +++ b/lib/cpPlugins/Interface/Mesh.hxx @@ -1,9 +1,16 @@ #ifndef __CPPLUGINS__INTERFACE__MESH__HXX__ #define __CPPLUGINS__INTERFACE__MESH__HXX__ +#include + #include #include +#include +#include +#include +#include + // ------------------------------------------------------------------------- #define cpPlugins_Mesh_Import( N, T, D ) \ cpPlugins_TEMPLATE_IMPORT( \ @@ -28,10 +35,61 @@ cpPlugins_Mesh_Import( QuadEdgeMesh, double, 3 ); // ------------------------------------------------------------------------- template< class M > void cpPlugins::Interface::Mesh:: -SetITK( itk::Object* object ) +SetITK( M* mesh ) { - std::cerr << "SetITKMesh: TODO" << std::endl; - std::exit( 1 ); + if( this->m_ITKObject.GetPointer( ) == mesh ) + return; + + this->m_ITKObject = mesh; + + long numPoints = mesh->GetNumberOfPoints( ); + if( numPoints == 0 ) + return; + + vtkSmartPointer< vtkPoints > vpoints = + vtkSmartPointer< vtkPoints >::New( ); + vpoints->SetNumberOfPoints( numPoints ); + auto points = mesh->GetPoints( ); + + // Copy points + vtkIdType VTKId = 0; + std::map< vtkIdType, long > IndexMap; + for( auto i = points->Begin( ); i != points->End( ); ++i, VTKId++ ) + { + IndexMap[ VTKId ] = i->Index( ); + if( M::PointDimension == 2 ) + vpoints->SetPoint( + VTKId, + i->Value( )[ 0 ], i->Value( )[ 1 ], 0 + ); + else if( M::PointDimension == 3 ) + vpoints->SetPoint( + VTKId, + i->Value( )[ 0 ], i->Value( )[ 1 ], i->Value( )[ 2 ] + ); + + } // rof + + // Copy cells + vtkSmartPointer< vtkCellArray > vcells = + vtkSmartPointer< vtkCellArray >::New( ); + auto cells = mesh->GetCells( ); + for( auto j = cells->Begin( ); j != cells->End( ); ++j ) + { + auto cell = j->Value( ); + vcells->InsertNextCell( cell->GetNumberOfPoints( ) ); + for( auto k = cell->PointIdsBegin( ); k != cell->PointIdsEnd( ); ++k ) + vcells->InsertCellPoint( IndexMap[ *k ] ); + + } // rof + + // Final assignations + vtkSmartPointer< vtkPolyData > vmesh = + vtkSmartPointer< vtkPolyData >::New( ); + vmesh->SetPoints( vpoints ); + vmesh->SetPolys( vcells ); + this->m_VTKObject = vmesh; + this->Modified( ); } #endif // __CPPLUGINS__INTERFACE__MESH__HXX__ diff --git a/lib/cpPlugins/Plugins/BasicFilters/TriangleMeshToBinaryImageFilter.cxx b/lib/cpPlugins/Plugins/BasicFilters/TriangleMeshToBinaryImageFilter.cxx new file mode 100644 index 0000000..8ede24e --- /dev/null +++ b/lib/cpPlugins/Plugins/BasicFilters/TriangleMeshToBinaryImageFilter.cxx @@ -0,0 +1,132 @@ +#include "TriangleMeshToBinaryImageFilter.h" +#include +#include + +#include +#include + +// ------------------------------------------------------------------------- +cpPlugins::BasicFilters::TriangleMeshToBinaryImageFilter:: +TriangleMeshToBinaryImageFilter( ) + : Superclass( ) +{ + this->_AddInput( "Input" ); + this->_AddOutput< cpPlugins::Interface::Image >( "Output" ); + + this->m_Parameters->ConfigureAsUint( "InsideValue" ); + this->m_Parameters->ConfigureAsUint( "OutsideValue" ); + this->m_Parameters->ConfigureAsUint( "MinimumSize" ); + + this->m_Parameters->SetUint( "InsideValue", 1 ); + this->m_Parameters->SetUint( "OutsideValue", 0 ); + this->m_Parameters->SetUint( "MinimumSize", 100 ); +} + +// ------------------------------------------------------------------------- +cpPlugins::BasicFilters::TriangleMeshToBinaryImageFilter:: +~TriangleMeshToBinaryImageFilter( ) +{ +} + +// ------------------------------------------------------------------------- +std::string cpPlugins::BasicFilters::TriangleMeshToBinaryImageFilter:: +_GenerateData( ) +{ + typedef itk::Mesh< float, 3 > _3F; + typedef itk::Mesh< double, 3 > _3D; + + auto input = this->GetInputData< cpPlugins::Interface::Mesh >( "Input" ); + if( input == NULL ) + return( "TriangleMeshToBinaryImageFilter: No valid input mesh." ); + + auto in_3f = input->GetITK< _3F >( ); + auto in_3d = input->GetITK< _3D >( ); + if ( in_3f != NULL ) return( this->_GD0( in_3f ) ); + else if( in_3d != NULL ) return( this->_GD0( in_3d ) ); + else + return( "TriangleMeshToBinaryImageFilter: No valid input itk mesh." ); +} + +// ------------------------------------------------------------------------- +template< class M > +std::string cpPlugins::BasicFilters::TriangleMeshToBinaryImageFilter:: +_GD0( M* mesh ) +{ + return( + this->_RealGD< M, itk::Image< unsigned char, M::PointDimension > >( + mesh + ) + ); +} + +// ------------------------------------------------------------------------- +template< class M, class I > +inline std::string cpPlugins::BasicFilters::TriangleMeshToBinaryImageFilter:: +_RealGD( M* mesh ) +{ + static const unsigned int PAD = 10; + + typedef itk::TriangleMeshToBinaryImageFilter< M, I > _F; + + _F* filter = this->_CreateITK< _F >( ); + + auto bb = mesh->GetBoundingBox( ); + auto minBB = bb->GetMinimum( ); + auto maxBB = bb->GetMaximum( ); + + 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 + double mSpac = lm / double( this->m_Parameters->GetUint( "MinimumSize" ) ); + typename I::SpacingType spac; + spac.Fill( mSpac ); + filter->SetSpacing( spac ); + + // Compute size + typename I::SizeType size; + size[ 0 ] = ( unsigned long )( std::ceil( lx / mSpac ) ); + size[ 1 ] = ( unsigned long )( std::ceil( ly / mSpac ) ); + size[ 2 ] = ( unsigned long )( std::ceil( lz / mSpac ) ); + + // ... add some padding pixels + size[ 0 ] += PAD; + size[ 1 ] += PAD; + size[ 2 ] += PAD; + filter->SetSize( size ); + + // Set origin + typename I::PointType origin = minBB; + origin[ 0 ] -= double( PAD >> 1 ) * spac[ 0 ]; + origin[ 1 ] -= double( PAD >> 1 ) * spac[ 1 ]; + origin[ 2 ] -= double( PAD >> 1 ) * spac[ 2 ]; + filter->SetOrigin( origin ); + + // Remaining values + typename I::DirectionType direction; + direction.SetIdentity( ); + filter->SetDirection( direction ); + + typename I::IndexType index; + index.Fill( 0 ); + filter->SetIndex( index ); + + // Execute + filter->SetInput( mesh ); + filter->Update( ); + + // Connect output + auto out = this->GetOutputData< cpPlugins::Interface::Image >( "Output" ); + if( out != NULL ) + { + out->SetITK< I >( filter->GetOutput( ) ); + return( "" ); + } + else + return( "TriangleMeshToBinaryImageFilter: output not correctly created." ); +} + +// eof - $RCSfile$ diff --git a/lib/cpPlugins/Plugins/BasicFilters/TriangleMeshToBinaryImageFilter.h b/lib/cpPlugins/Plugins/BasicFilters/TriangleMeshToBinaryImageFilter.h new file mode 100644 index 0000000..4214867 --- /dev/null +++ b/lib/cpPlugins/Plugins/BasicFilters/TriangleMeshToBinaryImageFilter.h @@ -0,0 +1,57 @@ +#ifndef __CPPLUGINS__PLUGINS__TRIANGLEMESHTOBINARYIMAGEFILTER__H__ +#define __CPPLUGINS__PLUGINS__TRIANGLEMESHTOBINARYIMAGEFILTER__H__ + +#include +#include + +namespace cpPlugins +{ + namespace BasicFilters + { + /** + */ + class cpPluginsBasicFilters_EXPORT TriangleMeshToBinaryImageFilter + : public cpPlugins::Interface::MeshToImageFilter + { + public: + typedef TriangleMeshToBinaryImageFilter Self; + typedef cpPlugins::Interface::MeshToImageFilter Superclass; + typedef itk::SmartPointer< Self > Pointer; + typedef itk::SmartPointer< const Self > ConstPointer; + + public: + itkNewMacro( Self ); + itkTypeMacro( + TriangleMeshToBinaryImageFilter, + cpPluginsInterfaceMeshToImageFilter + ); + cpPlugins_Id_Macro( + cpPlugins::BasicFilters::TriangleMeshToBinaryImageFilter, + MeshToImageFilter + ); + + protected: + TriangleMeshToBinaryImageFilter( ); + virtual ~TriangleMeshToBinaryImageFilter( ); + + virtual std::string _GenerateData( ); + + template< class M > + inline std::string _GD0( M* mesh ); + + template< class M, class I > + inline std::string _RealGD( M* mesh ); + + private: + // Purposely not implemented + TriangleMeshToBinaryImageFilter( const Self& ); + Self& operator=( const Self& ); + }; + + } // ecapseman + +} // ecapseman + +#endif // __CPPLUGINS__PLUGINS__TRIANGLEMESHTOBINARYIMAGEFILTER__H__ + +// eof - $RCSfile$ -- 2.45.2