]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/DataObjects/Mesh.cxx
...
[cpPlugins.git] / lib / cpPlugins / DataObjects / Mesh.cxx
1 #include <cpPlugins/DataObjects/Mesh.h>
2 #include <cpPlugins/DataObjects/Mesh_Demanglers.h>
3
4 #include <itkMesh.h>
5 #include <itkLineCell.h>
6 #include <itkTriangleCell.h>
7 #include <itkPolygonCell.h>
8 #include <vtkPolyData.h>
9
10 // -------------------------------------------------------------------------
11 void cpPlugins::DataObjects::Mesh::
12 SetITK( itk::LightObject* o )
13 {
14   this->Superclass::SetITK( o );
15   cpPlugins_Demangle_Mesh_Meshes_1( o, _ITK_2_VTK, 2 )
16     cpPlugins_Demangle_Mesh_Meshes_1( o, _ITK_2_VTK, 3 )
17   {
18     this->m_VTK = NULL;
19
20   } // fi
21   this->Modified( );
22 }
23
24 // -------------------------------------------------------------------------
25 void cpPlugins::DataObjects::Mesh::
26 SetVTK( vtkObjectBase* o )
27 {
28   typedef itk::Mesh< double, 3 >      _TMesh;
29   typedef _TMesh::CellType            _TCell;
30   typedef _TCell::CellAutoPointer     _TCellAutoPointer;
31   typedef itk::LineCell< _TCell >     _TLine;
32   typedef itk::TriangleCell< _TCell > _TTriangle;
33   typedef itk::PolygonCell< _TCell >  _TPolygon;
34
35   vtkPolyData* mesh = dynamic_cast< vtkPolyData* >( o );
36   if( mesh == NULL )
37   {
38     this->m_ITK = NULL;
39     this->Modified( );
40     return;
41
42   } // fi
43
44   if( this->m_VTK.GetPointer( ) != mesh )
45   {
46     this->m_VTK = mesh;
47
48     // Copy points
49     _TMesh::Pointer imesh = _TMesh::New( );
50     double point[ 3 ];
51     for( long i = 0; i < mesh->GetNumberOfPoints( ); ++i )
52     {
53       mesh->GetPoint( i, point );
54       _TMesh::PointType ipoint;
55       ipoint[ 0 ] = point[ 0 ];
56       ipoint[ 1 ] = point[ 1 ];
57       ipoint[ 2 ] = point[ 2 ];
58       imesh->SetPoint( i, ipoint );
59
60     } // rof
61
62     // Copy cells
63     vtkCellArray* arrays[ 4 ];
64     arrays[ 0 ] = mesh->GetLines( );
65     arrays[ 1 ] = mesh->GetPolys( );
66     arrays[ 2 ] = NULL; // TODO: mesh->GetStrips( );
67     arrays[ 3 ] = mesh->GetVerts( );
68
69     for( unsigned int c = 0; c < 4; c++ )
70     {
71       if( arrays[ c ] != NULL )
72       {
73         vtkSmartPointer< vtkIdList > ids =
74           vtkSmartPointer< vtkIdList >::New( );
75         arrays[ c ]->InitTraversal( );
76         while( arrays[ c ]->GetNextCell( ids ) == 1 )
77         {
78           long nPoints = ids->GetNumberOfIds( );
79           _TCellAutoPointer icell;
80           if( nPoints == 2 )
81           {
82             icell.TakeOwnership( new _TLine );
83             icell->SetPointId( 0, ids->GetId( 0 ) );
84             icell->SetPointId( 1, ids->GetId( 1 ) );
85           }
86           else if( nPoints == 3 )
87           {
88             icell.TakeOwnership( new _TTriangle );
89             icell->SetPointId( 0, ids->GetId( 0 ) );
90             icell->SetPointId( 1, ids->GetId( 1 ) );
91             icell->SetPointId( 2, ids->GetId( 2 ) );
92           }
93           else if( nPoints > 3 )
94           {
95             _TPolygon* polygon = new _TPolygon( );
96             for( long j = 0; j < nPoints; ++j )
97               polygon->AddPointId( ids->GetId( j ) );
98             icell.TakeOwnership( polygon );
99
100           } // fi
101           imesh->SetCell( imesh->GetNumberOfCells( ), icell );
102
103         } // elihw
104
105       } // fi
106
107     } // rof
108     this->m_ITK = imesh;
109     this->Modified( );
110
111   } // fi
112 }
113
114 // -------------------------------------------------------------------------
115 cpPlugins::DataObjects::Mesh::
116 Mesh( )
117   : Superclass( )
118 {
119 }
120
121 // -------------------------------------------------------------------------
122 cpPlugins::DataObjects::Mesh::
123 ~Mesh( )
124 {
125 }
126
127 // -------------------------------------------------------------------------
128 template< class _TMesh >
129 void cpPlugins::DataObjects::Mesh::
130 _ITK_2_VTK( _TMesh* mesh )
131 {
132   long numPoints = mesh->GetNumberOfPoints( );
133   if( numPoints == 0 )
134     return;
135
136   vtkSmartPointer< vtkPoints > vpoints =
137     vtkSmartPointer< vtkPoints >::New( );
138   vpoints->SetNumberOfPoints( numPoints );
139   auto points = mesh->GetPoints( );
140
141   // Copy points
142   vtkIdType VTKId = 0;
143   std::map< vtkIdType, long > IndexMap;
144   for( auto i = points->Begin( ); i != points->End( ); ++i, VTKId++ )
145   {
146     IndexMap[ VTKId ] = i->Index( );
147     if( _TMesh::PointDimension == 2 )
148       vpoints->SetPoint(
149         VTKId,
150         i->Value( )[ 0 ], i->Value( )[ 1 ], 0
151         );
152     else if( _TMesh::PointDimension == 3 )
153       vpoints->SetPoint(
154         VTKId,
155         i->Value( )[ 0 ], i->Value( )[ 1 ], i->Value( )[ 2 ]
156         );
157
158   } // rof
159
160   // Copy cells
161   vtkSmartPointer< vtkCellArray > vcells =
162     vtkSmartPointer< vtkCellArray >::New( );
163   auto cells = mesh->GetCells( );
164   for( auto j = cells->Begin( ); j != cells->End( ); ++j )
165   {
166     auto cell = j->Value( );
167     vcells->InsertNextCell( cell->GetNumberOfPoints( ) );
168     for( auto k = cell->PointIdsBegin( ); k != cell->PointIdsEnd( ); ++k )
169       vcells->InsertCellPoint( IndexMap[ *k ] );
170
171   } // rof
172
173   // Final assignations
174   vtkSmartPointer< vtkPolyData > vmesh =
175     vtkSmartPointer< vtkPolyData >::New( );
176   vmesh->SetPoints( vpoints );
177   vmesh->SetPolys( vcells );
178   this->m_VTK = vmesh;
179   this->Modified( );
180 }
181
182 // eof - $RCSfile$