Salome HOME
Update copyright
[modules/smesh.git] / src / SMDS / SMDS_VtkCellIterator.cxx
1 // Copyright (C) 2010-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SMDS_VtkCellIterator.hxx"
21 #include "utilities.h"
22
23 SMDS_VtkCellIterator::SMDS_VtkCellIterator(SMDS_Mesh* mesh, int vtkCellId, SMDSAbs_EntityType aType) :
24   _mesh(mesh), _cellId(vtkCellId), _index(0), _type(aType)
25 {
26   //MESSAGE("SMDS_VtkCellIterator " << _type);
27   _vtkIdList = vtkIdList::New();
28   vtkUnstructuredGrid* grid = _mesh->getGrid();
29   grid->GetCellPoints(_cellId, _vtkIdList);
30   _nbNodes = _vtkIdList->GetNumberOfIds();
31   switch (_type)
32   {
33     case SMDSEntity_Tetra:
34       {
35         this->exchange(1, 2);
36         break;
37       }
38     case SMDSEntity_Pyramid:
39       {
40         this->exchange(1, 3);
41         break;
42       }
43     case SMDSEntity_Penta:
44       {
45         //this->exchange(1, 2);
46         //this->exchange(4, 5);
47         break;
48       }
49     case SMDSEntity_Hexa:
50       {
51         this->exchange(1, 3);
52         this->exchange(5, 7);
53         break;
54       }
55     case SMDSEntity_Quad_Tetra:
56       {
57         this->exchange(1, 2);
58         this->exchange(4, 6);
59         this->exchange(8, 9);
60         break;
61       }
62     case SMDSEntity_Quad_Pyramid:
63       {
64         this->exchange(1, 3);
65         this->exchange(5, 8);
66         this->exchange(6, 7);
67         this->exchange(10, 12);
68         break;
69       }
70     case SMDSEntity_Quad_Penta:
71       {
72         //this->exchange(1, 2);
73         //this->exchange(4, 5);
74         //this->exchange(6, 8);
75         //this->exchange(9, 11);
76         //this->exchange(13, 14);
77         break;
78       }
79     case SMDSEntity_Quad_Hexa:
80       {
81         MESSAGE("SMDS_VtkCellIterator Quad_Hexa");
82         this->exchange(1, 3);
83         this->exchange(5, 7);
84         this->exchange(8, 11);
85         this->exchange(9, 10);
86         this->exchange(12, 15);
87         this->exchange(13, 14);
88         this->exchange(17, 19);
89         break;
90       }
91     case SMDSEntity_Polyhedra:
92       MESSAGE("SMDS_VtkCellIterator Polyhedra (iterate on actual nodes)");
93       break;
94     default:
95       break;
96   }
97 }
98
99 SMDS_VtkCellIterator::~SMDS_VtkCellIterator()
100 {
101   _vtkIdList->Delete();
102 }
103
104 bool SMDS_VtkCellIterator::more()
105 {
106   return (_index < _nbNodes);
107 }
108
109 const SMDS_MeshElement* SMDS_VtkCellIterator::next()
110 {
111   vtkIdType id = _vtkIdList->GetId(_index++);
112   return _mesh->FindNodeVtk(id);
113 }
114
115 SMDS_VtkCellIteratorToUNV::SMDS_VtkCellIteratorToUNV(SMDS_Mesh* mesh, int vtkCellId, SMDSAbs_EntityType aType) :
116   SMDS_VtkCellIterator()
117 {
118   _mesh = mesh;
119   _cellId = vtkCellId;
120   _index = 0;
121   _type = aType;
122   //MESSAGE("SMDS_VtkCellInterlacedIterator (UNV)" << _type);
123
124   _vtkIdList = vtkIdList::New();
125   vtkIdType* pts;
126   vtkUnstructuredGrid* grid = _mesh->getGrid();
127   grid->GetCellPoints((vtkIdType)_cellId, (vtkIdType&)_nbNodes, pts);
128   _vtkIdList->SetNumberOfIds(_nbNodes);
129   int *ids = 0;
130   switch (_type)
131   {
132     case SMDSEntity_Quad_Edge:
133       {
134         static int id[] = { 0, 2, 1 };
135         ids = id;
136         break;
137       }
138     case SMDSEntity_Quad_Triangle:
139       {
140         static int id[] = { 0, 3, 1, 4, 2, 5 };
141         ids = id;
142         break;
143       }
144     case SMDSEntity_Quad_Quadrangle:
145       {
146         static int id[] = { 0, 4, 1, 5, 2, 6, 3, 7 };
147         ids = id;
148         break;
149       }
150     case SMDSEntity_Quad_Tetra:
151       {
152         static int id[] = { 0, 4, 1, 5, 2, 6, 7, 8, 9, 3 };
153         ids = id;
154         break;
155       }
156     case SMDSEntity_Quad_Pyramid:
157       {
158         static int id[] = { 0, 5, 1, 6, 2, 7, 3, 8, 9, 10, 11, 12, 4 };
159         ids = id;
160         break;
161       }
162     case SMDSEntity_Penta:
163       {
164         static int id[] = { 0, 2, 1, 3, 5, 4 };
165         ids = id;
166         break;
167       }
168     case SMDSEntity_Quad_Penta:
169       {
170         static int id[] = { 0, 8, 2, 7, 1, 6, 12, 14, 13, 3, 11, 5, 10, 4, 9 };
171         ids = id;
172         break;
173       }
174     case SMDSEntity_Quad_Hexa:
175       {
176         static int id[] = { 0, 8, 1, 9, 2, 10, 3, 11, 16, 17, 18, 19, 4, 12, 5, 13, 6, 14, 7, 15 };
177         ids = id;
178         break;
179       }
180     case SMDSEntity_Polygon:
181     case SMDSEntity_Quad_Polygon:
182     case SMDSEntity_Polyhedra:
183     case SMDSEntity_Quad_Polyhedra:
184     default:
185       {
186         static int id[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
187                             25, 26, 27, 28, 29 };
188         ids = id;
189         break;
190       }
191   }
192   //MESSAGE("_nbNodes " << _nbNodes);
193   for (int i = 0; i < _nbNodes; i++)
194     _vtkIdList->SetId(i, pts[ids[i]]);
195 }
196
197 SMDS_VtkCellIteratorToUNV::~SMDS_VtkCellIteratorToUNV()
198 {
199 }
200
201 SMDS_VtkCellIteratorPolyH::SMDS_VtkCellIteratorPolyH(SMDS_Mesh* mesh, int vtkCellId, SMDSAbs_EntityType aType) :
202   SMDS_VtkCellIterator()
203 {
204   _mesh = mesh;
205   _cellId = vtkCellId;
206   _index = 0;
207   _type = aType;
208   //MESSAGE("SMDS_VtkCellIteratorPolyH " << _type);
209   _vtkIdList = vtkIdList::New();
210   vtkUnstructuredGrid* grid = _mesh->getGrid();
211   grid->GetCellPoints(_cellId, _vtkIdList);
212   _nbNodes = _vtkIdList->GetNumberOfIds();
213   switch (_type)
214   {
215     case SMDSEntity_Polyhedra:
216       {
217         //MESSAGE("SMDS_VtkCellIterator Polyhedra");
218         vtkIdType nFaces = 0;
219         vtkIdType* ptIds = 0;
220         grid->GetFaceStream(_cellId, nFaces, ptIds);
221         int id = 0;
222         _nbNodesInFaces = 0;
223         for (int i = 0; i < nFaces; i++)
224           {
225             int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
226             _nbNodesInFaces += nodesInFace;
227             id += (nodesInFace + 1);
228           }
229         _vtkIdList->SetNumberOfIds(_nbNodesInFaces);
230         id = 0;
231         int n = 0;
232         for (int i = 0; i < nFaces; i++)
233           {
234             int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
235             for (int k = 1; k <= nodesInFace; k++)
236               _vtkIdList->SetId(n++, ptIds[id + k]);
237             id += (nodesInFace + 1);
238           }
239         break;
240       }
241     default:
242       assert(0);
243   }
244 }
245
246 SMDS_VtkCellIteratorPolyH::~SMDS_VtkCellIteratorPolyH()
247 {
248 }
249
250 bool SMDS_VtkCellIteratorPolyH::more()
251 {
252   return (_index < _nbNodesInFaces);
253 }