#ifndef __CPPLUGINS__INTERFACE__MESH__HXX__
#define __CPPLUGINS__INTERFACE__MESH__HXX__
+#include <map>
+
#include <itkMesh.h>
#include <itkQuadEdgeMesh.h>
+#include <vtkSmartPointer.h>
+#include <vtkCellArray.h>
+#include <vtkPoints.h>
+#include <vtkPolyData.h>
+
// -------------------------------------------------------------------------
#define cpPlugins_Mesh_Import( N, T, D ) \
cpPlugins_TEMPLATE_IMPORT( \
// -------------------------------------------------------------------------
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__
--- /dev/null
+#include "TriangleMeshToBinaryImageFilter.h"
+#include <cpPlugins/Interface/Image.h>
+#include <cpPlugins/Interface/Mesh.h>
+
+#include <itkMesh.h>
+#include <itkTriangleMeshToBinaryImageFilter.h>
+
+// -------------------------------------------------------------------------
+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$
--- /dev/null
+#ifndef __CPPLUGINS__PLUGINS__TRIANGLEMESHTOBINARYIMAGEFILTER__H__
+#define __CPPLUGINS__PLUGINS__TRIANGLEMESHTOBINARYIMAGEFILTER__H__
+
+#include <cpPlugins/BasicFilters/cpPluginsBasicFilters_Export.h>
+#include <cpPlugins/Interface/BaseProcessObjects.h>
+
+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$