]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/Mesh.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface / Mesh.cxx
index 3bcb5afaa043e06f650bd07746055cad3578b101..d56d1929aebeb81719640b70f20d80460975c775 100644 (file)
@@ -1,69 +1,97 @@
 #include <cpPlugins/Interface/Mesh.h>
 
-#include <vtkMapper.h>
+#include <itkMesh.h>
+#include <itkLineCell.h>
+#include <itkTriangleCell.h>
+#include <itkPolygonCell.h>
 
-#include <cpPlugins/Extensions/QuadEdgeMesh.h>
-#include <cpPlugins/Extensions/OpenGLMeshMapper.h>
+#include <vtkPolyData.h>
 
 // -------------------------------------------------------------------------
-cpPlugins::Interface::Mesh::
-Mesh( )
-  : Superclass( ),
-    m_Mapper( NULL )
+void cpPlugins::Interface::Mesh::
+SetVTK( vtkObject* object )
 {
-}
+  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;
 
-// -------------------------------------------------------------------------
-cpPlugins::Interface::Mesh::
-~Mesh( )
-{
-  if( this->m_Mapper != NULL ) this->m_Mapper->Delete( );
-}
+  vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( object );
+  if( mesh == NULL )
+  {
+    this->m_ITKObject = NULL;
+    this->Modified( );
+    return;
 
-// -------------------------------------------------------------------------
-std::string cpPlugins::Interface::Mesh::
-GetClassName( ) const
-{
-  return( "cpPlugins::Interface::Mesh" );
-}
+  } // fi
 
-// -------------------------------------------------------------------------
-void cpPlugins::Interface::Mesh::
-SetDataObject( itk::DataObject* dobj )
-{
-  this->Superclass::SetDataObject( dobj );
-  
-  // WARNING: Only 2 and 3 dimensions at this moment
-  typedef cpPlugins::Extensions::QuadEdgeMesh< float, 2 >  _TF2;
-  typedef cpPlugins::Extensions::QuadEdgeMesh< double, 2 > _TD2;
-  typedef cpPlugins::Extensions::QuadEdgeMesh< float, 3 >  _TF3;
-  typedef cpPlugins::Extensions::QuadEdgeMesh< double, 3 > _TD3;
+  if( this->m_VTKObject.GetPointer( ) != mesh )
+  {
+    this->m_VTKObject = mesh;
 
-  if     ( dynamic_cast< _TF2* >( dobj ) ) this->_Map< _TF2 >( );
-  else if( dynamic_cast< _TD2* >( dobj ) ) this->_Map< _TD2 >( );
-  else if( dynamic_cast< _TF3* >( dobj ) ) this->_Map< _TF3 >( );
-  else if( dynamic_cast< _TD3* >( dobj ) ) this->_Map< _TD3 >( );
-}
+    // 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
+    for( long i = 0; i < mesh->GetNumberOfCells( ); ++i )
+    {
+      auto cell = mesh->GetCell( i );
+      long nPoints = cell->GetNumberOfPoints( );
+      _TCellAutoPointer icell;
+      if( nPoints == 2 )
+      {
+        icell.TakeOwnership( new _TLine );
+        icell->SetPointId( 0, cell->GetPointId( 0 ) );
+        icell->SetPointId( 1, cell->GetPointId( 1 ) );
+      }
+      else if( nPoints == 3 )
+      {
+        icell.TakeOwnership( new _TTriangle );
+        icell->SetPointId( 0, cell->GetPointId( 0 ) );
+        icell->SetPointId( 1, cell->GetPointId( 1 ) );
+        icell->SetPointId( 2, cell->GetPointId( 2 ) );
+      }
+      else if( nPoints > 3 )
+      {
+        _TPolygon* polygon = new _TPolygon( );
+        for( long j = 0; j < nPoints; ++j )
+          polygon->AddPointId( cell->GetPointId( j ) );
+        icell.TakeOwnership( polygon );
+
+      } // fi
+      imesh->SetCell( imesh->GetNumberOfCells( ), icell );
+
+    } // rof
+
+    this->m_ITKObject = imesh;
+    this->Modified( );
 
+  } // fi
+}
 // -------------------------------------------------------------------------
-vtkMapper* cpPlugins::Interface::Mesh::
-GetVTKMapper( ) const
+cpPlugins::Interface::Mesh::
+Mesh( )
+  : Superclass( )
 {
-  return( this->m_Mapper );
 }
 
 // -------------------------------------------------------------------------
-template< class M >
-void cpPlugins::Interface::Mesh::
-_Map( )
+cpPlugins::Interface::Mesh::
+~Mesh( )
 {
-  typedef cpPlugins::Extensions::OpenGLMeshMapper< M > _TMapper;
-
-  if( this->m_Mapper != NULL ) this->m_Mapper->Delete( );
-  M* mesh = dynamic_cast< M* >( this->Superclass::GetDataObject( ) );
-  _TMapper* mapper = _TMapper::New( );
-  mapper->SetInputData( mesh );
-  this->m_Mapper = mapper;
 }
 
 // eof - $RCSfile$