]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Mesh.hxx
...
[cpPlugins.git] / lib / cpPlugins / Mesh.hxx
1 #ifndef __CPPLUGINS__MESH__HXX__
2 #define __CPPLUGINS__MESH__HXX__
3
4 #include <map>
5 #include <vtkSmartPointer.h>
6 #include <vtkCellArray.h>
7 #include <vtkPoints.h>
8 #include <vtkPolyData.h>
9
10 // -------------------------------------------------------------------------
11 template< class M >
12 bool cpPlugins::Mesh::
13 _ITK_2_VTK( itk::LightObject* o )
14 {
15   M* mesh = dynamic_cast< M* >( o );
16   if( mesh == NULL )
17     return( false );
18
19   long numPoints = mesh->GetNumberOfPoints( );
20   if( numPoints == 0 )
21     return( false );
22
23   vtkSmartPointer< vtkPoints > vpoints =
24     vtkSmartPointer< vtkPoints >::New( );
25   vpoints->SetNumberOfPoints( numPoints );
26   auto points = mesh->GetPoints( );
27
28   // Copy points
29   vtkIdType VTKId = 0;
30   std::map< vtkIdType, long > IndexMap;
31   for( auto i = points->Begin( ); i != points->End( ); ++i, VTKId++ )
32   {
33     IndexMap[ VTKId ] = i->Index( );
34     if( M::PointDimension == 2 )
35       vpoints->SetPoint(
36         VTKId,
37         i->Value( )[ 0 ], i->Value( )[ 1 ], 0
38         );
39     else if( M::PointDimension == 3 )
40       vpoints->SetPoint(
41         VTKId,
42         i->Value( )[ 0 ], i->Value( )[ 1 ], i->Value( )[ 2 ]
43         );
44
45   } // rof
46
47   // Copy cells
48   vtkSmartPointer< vtkCellArray > vcells =
49     vtkSmartPointer< vtkCellArray >::New( );
50   auto cells = mesh->GetCells( );
51   for( auto j = cells->Begin( ); j != cells->End( ); ++j )
52   {
53     auto cell = j->Value( );
54     vcells->InsertNextCell( cell->GetNumberOfPoints( ) );
55     for( auto k = cell->PointIdsBegin( ); k != cell->PointIdsEnd( ); ++k )
56       vcells->InsertCellPoint( IndexMap[ *k ] );
57     
58   } // rof
59
60   // Final assignations
61   vtkSmartPointer< vtkPolyData > vmesh =
62     vtkSmartPointer< vtkPolyData >::New( );
63   vmesh->SetPoints( vpoints );
64   vmesh->SetPolys( vcells );
65   this->m_VTKObject = vmesh;
66   this->Modified( );
67   return( true );
68 }
69
70 #endif // __CPPLUGINS__MESH__HXX__
71
72 // eof - $RCSfile$