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