]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Mesh.hxx
16504f095a31d9ea9ee3be828fbbf9c907a73fec
[cpPlugins.git] / lib / cpPlugins / Interface / Mesh.hxx
1 #ifndef __CPPLUGINS__INTERFACE__MESH__HXX__
2 #define __CPPLUGINS__INTERFACE__MESH__HXX__
3
4 #include <map>
5
6 #include <itkMesh.h>
7 #include <itkQuadEdgeMesh.h>
8
9 #include <vtkSmartPointer.h>
10 #include <vtkCellArray.h>
11 #include <vtkPoints.h>
12 #include <vtkPolyData.h>
13
14 // -------------------------------------------------------------------------
15 template< class M >
16 void cpPlugins::Interface::Mesh::
17 _ITK_2_VTK( itk::LightObject* o )
18 {
19   M* mesh = dynamic_cast< M* >( o );
20   if( mesh == NULL )
21     return;
22
23   long numPoints = mesh->GetNumberOfPoints( );
24   if( numPoints == 0 )
25     return;
26
27   vtkSmartPointer< vtkPoints > vpoints =
28     vtkSmartPointer< vtkPoints >::New( );
29   vpoints->SetNumberOfPoints( numPoints );
30   auto points = mesh->GetPoints( );
31
32   // Copy points
33   vtkIdType VTKId = 0;
34   std::map< vtkIdType, long > IndexMap;
35   for( auto i = points->Begin( ); i != points->End( ); ++i, VTKId++ )
36   {
37     IndexMap[ VTKId ] = i->Index( );
38     if( M::PointDimension == 2 )
39       vpoints->SetPoint(
40         VTKId,
41         i->Value( )[ 0 ], i->Value( )[ 1 ], 0
42         );
43     else if( M::PointDimension == 3 )
44       vpoints->SetPoint(
45         VTKId,
46         i->Value( )[ 0 ], i->Value( )[ 1 ], i->Value( )[ 2 ]
47         );
48
49   } // rof
50
51   // Copy cells
52   vtkSmartPointer< vtkCellArray > vcells =
53     vtkSmartPointer< vtkCellArray >::New( );
54   auto cells = mesh->GetCells( );
55   for( auto j = cells->Begin( ); j != cells->End( ); ++j )
56   {
57     auto cell = j->Value( );
58     vcells->InsertNextCell( cell->GetNumberOfPoints( ) );
59     for( auto k = cell->PointIdsBegin( ); k != cell->PointIdsEnd( ); ++k )
60       vcells->InsertCellPoint( IndexMap[ *k ] );
61     
62   } // rof
63
64   // Final assignations
65   vtkSmartPointer< vtkPolyData > vmesh =
66     vtkSmartPointer< vtkPolyData >::New( );
67   vmesh->SetPoints( vpoints );
68   vmesh->SetPolys( vcells );
69   this->m_VTKObject = vmesh;
70   this->Modified( );
71 }
72
73 // -------------------------------------------------------------------------
74 #define cpPlugins_Mesh_Import( N, T, D )                                \
75   cpPlugins_TEMPLATE_IMPORT(                                            \
76     2(class cpPlugins_Interface_EXPORT itk::N< T, D >)                  \
77     )
78
79 // -------------------------------------------------------------------------
80
81 #ifndef cpPlugins_Interface_EXPORTS
82
83 cpPlugins_Mesh_Import( Mesh, float, 2 );
84 cpPlugins_Mesh_Import( Mesh, double, 2 );
85 cpPlugins_Mesh_Import( Mesh, float, 3 );
86 cpPlugins_Mesh_Import( Mesh, double, 3 );
87 cpPlugins_Mesh_Import( QuadEdgeMesh, float, 2 );
88 cpPlugins_Mesh_Import( QuadEdgeMesh, double, 2 );
89 cpPlugins_Mesh_Import( QuadEdgeMesh, float, 3 );
90 cpPlugins_Mesh_Import( QuadEdgeMesh, double, 3 );
91
92 #endif // cpPlugins_Interface_EXPORTS
93
94 #endif // __CPPLUGINS__INTERFACE__MESH__HXX__
95
96 // eof - $RCSfile$