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