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