]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/DataObjects/Mesh.cxx
...
[cpPlugins.git] / lib / cpPlugins / DataObjects / Mesh.cxx
index c40bb021baa4c3a592424a9a8b0bda63b215379e..4a955520adc2dcb50772f990302335c231973489 100644 (file)
 #include <cpPlugins/DataObjects/Mesh.h>
+#include <cpPlugins/DataObjects/Mesh_Demanglers.h>
+
+#include <itkMesh.h>
+#include <itkLineCell.h>
+#include <itkTriangleCell.h>
+#include <itkPolygonCell.h>
 #include <vtkPolyData.h>
 
 // -------------------------------------------------------------------------
 void cpPlugins::DataObjects::Mesh::
 SetITK( itk::LightObject* o )
 {
-  /* TODO
-     this->Superclass::SetITK( o );
-     bool s = false;
-     cpPlugins_Demangle_Meshes_Dims( o, _ITK_2_VTK );
-  */
+  this->Superclass::SetITK( o );
+  cpPlugins_Demangle_Mesh_Meshes( o, _ITK_2_VTK, 2, );
+  cpPlugins_Demangle_Mesh_Meshes( o, _ITK_2_VTK, 3, );
+  {
+    this->m_VTK = NULL;
+
+  } // fi
+  this->Modified( );
 }
 
 // -------------------------------------------------------------------------
 void cpPlugins::DataObjects::Mesh::
 SetVTK( vtkObjectBase* o )
 {
-  /* TODO
-     typedef itk::Mesh< double, 3 >      _TMesh;
-     typedef _TMesh::CellType            _TCell;
-     typedef _TCell::CellAutoPointer     _TCellAutoPointer;
-     typedef itk::LineCell< _TCell >     _TLine;
-     typedef itk::TriangleCell< _TCell > _TTriangle;
-     typedef itk::PolygonCell< _TCell >  _TPolygon;
-
-     vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
-     if( mesh == NULL )
-     {
-     this->m_ITK = NULL;
-     this->Modified( );
-     return;
-
-     } // fi
-
-     if( this->m_VTK.GetPointer( ) != mesh )
-     {
-     this->m_VTK = mesh;
-
-     // Copy points
-     _TMesh::Pointer imesh = _TMesh::New( );
-     double point[ 3 ];
-     for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
-     {
-     mesh->GetPoint( i, point );
-     _TMesh::PointType ipoint;
-     ipoint[ 0 ] = point[ 0 ];
-     ipoint[ 1 ] = point[ 1 ];
-     ipoint[ 2 ] = point[ 2 ];
-     imesh->SetPoint( i, ipoint );
-
-     } // rof
-
-     // Copy cells
-     vtkCellArray* arrays[ 4 ];
-     arrays[ 0 ] = mesh->GetLines( );
-     arrays[ 1 ] = mesh->GetPolys( );
-     arrays[ 2 ] = NULL; // TODO: mesh->GetStrips( );
-     arrays[ 3 ] = mesh->GetVerts( );
-
-     for( unsigned int c = 0; c < 4; c++ )
-     {
-     if( arrays[ c ] != NULL )
-     {
-     vtkSmartPointer< vtkIdList > ids =
-     vtkSmartPointer< vtkIdList >::New( );
-     arrays[ c ]->InitTraversal( );
-     while( arrays[ c ]->GetNextCell( ids ) == 1 )
-     {
-     long nPoints = ids->GetNumberOfIds( );
-     _TCellAutoPointer icell;
-     if( nPoints == 2 )
-     {
-     icell.TakeOwnership( new _TLine );
-     icell->SetPointId( 0, ids->GetId( 0 ) );
-     icell->SetPointId( 1, ids->GetId( 1 ) );
-     }
-     else if( nPoints == 3 )
-     {
-     icell.TakeOwnership( new _TTriangle );
-     icell->SetPointId( 0, ids->GetId( 0 ) );
-     icell->SetPointId( 1, ids->GetId( 1 ) );
-     icell->SetPointId( 2, ids->GetId( 2 ) );
-     }
-     else if( nPoints > 3 )
-     {
-     _TPolygon* polygon = new _TPolygon( );
-     for( long j = 0; j < nPoints; ++j )
-     polygon->AddPointId( ids->GetId( j ) );
-     icell.TakeOwnership( polygon );
-
-     } // fi
-     imesh->SetCell( imesh->GetNumberOfCells( ), icell );
-
-     } // elihw
-
-     } // fi
-
-     } // rof
-     this->m_ITK = imesh;
-     this->Modified( );
-
-     } // fi
-  */
+  typedef itk::Mesh< double, 3 >      _TMesh;
+  typedef _TMesh::CellType            _TCell;
+  typedef _TCell::CellAutoPointer     _TCellAutoPointer;
+  typedef itk::LineCell< _TCell >     _TLine;
+  typedef itk::TriangleCell< _TCell > _TTriangle;
+  typedef itk::PolygonCell< _TCell >  _TPolygon;
+
+  vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
+  if( mesh == NULL )
+  {
+    this->m_ITK = NULL;
+    this->Modified( );
+    return;
+
+  } // fi
+
+  if( this->m_VTK.GetPointer( ) != mesh )
+  {
+    this->m_VTK = mesh;
+
+    // Copy points
+    _TMesh::Pointer imesh = _TMesh::New( );
+    double point[ 3 ];
+    for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
+    {
+      mesh->GetPoint( i, point );
+      _TMesh::PointType ipoint;
+      ipoint[ 0 ] = point[ 0 ];
+      ipoint[ 1 ] = point[ 1 ];
+      ipoint[ 2 ] = point[ 2 ];
+      imesh->SetPoint( i, ipoint );
+
+    } // rof
+
+    // Copy cells
+    vtkCellArray* arrays[ 4 ];
+    arrays[ 0 ] = mesh->GetLines( );
+    arrays[ 1 ] = mesh->GetPolys( );
+    arrays[ 2 ] = NULL; // TODO: mesh->GetStrips( );
+    arrays[ 3 ] = mesh->GetVerts( );
+
+    for( unsigned int c = 0; c < 4; c++ )
+    {
+      if( arrays[ c ] != NULL )
+      {
+        vtkSmartPointer< vtkIdList > ids =
+          vtkSmartPointer< vtkIdList >::New( );
+        arrays[ c ]->InitTraversal( );
+        while( arrays[ c ]->GetNextCell( ids ) == 1 )
+        {
+          long nPoints = ids->GetNumberOfIds( );
+          _TCellAutoPointer icell;
+          if( nPoints == 2 )
+          {
+            icell.TakeOwnership( new _TLine );
+            icell->SetPointId( 0, ids->GetId( 0 ) );
+            icell->SetPointId( 1, ids->GetId( 1 ) );
+          }
+          else if( nPoints == 3 )
+          {
+            icell.TakeOwnership( new _TTriangle );
+            icell->SetPointId( 0, ids->GetId( 0 ) );
+            icell->SetPointId( 1, ids->GetId( 1 ) );
+            icell->SetPointId( 2, ids->GetId( 2 ) );
+          }
+          else if( nPoints > 3 )
+          {
+            _TPolygon* polygon = new _TPolygon( );
+            for( long j = 0; j < nPoints; ++j )
+              polygon->AddPointId( ids->GetId( j ) );
+            icell.TakeOwnership( polygon );
+
+          } // fi
+          imesh->SetCell( imesh->GetNumberOfCells( ), icell );
+
+        } // elihw
+
+      } // fi
+
+    } // rof
+    this->m_ITK = imesh;
+    this->Modified( );
+
+  } // fi
 }
 
 // -------------------------------------------------------------------------
@@ -117,4 +124,59 @@ cpPlugins::DataObjects::Mesh::
 {
 }
 
+// -------------------------------------------------------------------------
+template< class _TMesh >
+void cpPlugins::DataObjects::Mesh::
+_ITK_2_VTK( _TMesh* 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( _TMesh::PointDimension == 2 )
+      vpoints->SetPoint(
+        VTKId,
+        i->Value( )[ 0 ], i->Value( )[ 1 ], 0
+        );
+    else if( _TMesh::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_VTK = vmesh;
+  this->Modified( );
+}
+
 // eof - $RCSfile$