]> Creatis software - cpPlugins.git/commitdiff
...
authorLeonardo Florez-Valencia <florez-l@javeriana.edu.co>
Tue, 2 Feb 2016 17:06:40 +0000 (12:06 -0500)
committerLeonardo Florez-Valencia <florez-l@javeriana.edu.co>
Tue, 2 Feb 2016 17:06:40 +0000 (12:06 -0500)
lib/cpPlugins/Interface/Mesh.h
lib/cpPlugins/Interface/Mesh.hxx
lib/cpPlugins/Plugins/BasicFilters/TriangleMeshToBinaryImageFilter.cxx [new file with mode: 0644]
lib/cpPlugins/Plugins/BasicFilters/TriangleMeshToBinaryImageFilter.h [new file with mode: 0644]

index 10752baeb0be3d171b5495957173da3715d644b8..8754b0e9931bd7ac6fbdbce1d7ac839b6cb033b6 100644 (file)
@@ -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 );
 
index 6de611e2faa1fab2b533972521e16a9bc4344ff8..5c5e5626c5b8690d3a502267b69a377f8ee3862d 100644 (file)
@@ -1,9 +1,16 @@
 #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(                                            \
@@ -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 (file)
index 0000000..8ede24e
--- /dev/null
@@ -0,0 +1,132 @@
+#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$
diff --git a/lib/cpPlugins/Plugins/BasicFilters/TriangleMeshToBinaryImageFilter.h b/lib/cpPlugins/Plugins/BasicFilters/TriangleMeshToBinaryImageFilter.h
new file mode 100644 (file)
index 0000000..4214867
--- /dev/null
@@ -0,0 +1,57 @@
+#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$