Salome HOME
Merge branch V7_3_1_BR
[modules/smesh.git] / src / SMDS / SMDS_VtkCellIterator.cxx
1 // Copyright (C) 2010-2014  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, or (at your option) any later version.
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   vtkUnstructuredGrid* grid = _mesh->getGrid();
27   _vtkIdList = vtkIdList::New();
28   const std::vector<int>& interlace = SMDS_MeshCell::fromVtkOrder( aType );
29   if ( interlace.empty() )
30   {
31     grid->GetCellPoints(_cellId, _vtkIdList);
32     _nbNodes = _vtkIdList->GetNumberOfIds();
33   }
34   else
35   {
36     vtkIdType npts, *pts;
37     grid->GetCellPoints( _cellId, npts, pts );
38     _vtkIdList->SetNumberOfIds( _nbNodes = npts );
39     for (int i = 0; i < _nbNodes; i++)
40       _vtkIdList->SetId(i, pts[interlace[i]]);
41   }
42 }
43
44 SMDS_VtkCellIterator::~SMDS_VtkCellIterator()
45 {
46   _vtkIdList->Delete();
47 }
48
49 bool SMDS_VtkCellIterator::more()
50 {
51   return (_index < _nbNodes);
52 }
53
54 const SMDS_MeshElement* SMDS_VtkCellIterator::next()
55 {
56   vtkIdType id = _vtkIdList->GetId(_index++);
57   return _mesh->FindNodeVtk(id);
58 }
59
60 SMDS_VtkCellIteratorToUNV::SMDS_VtkCellIteratorToUNV(SMDS_Mesh* mesh, int vtkCellId, SMDSAbs_EntityType aType) :
61   SMDS_VtkCellIterator()
62 {
63   _mesh = mesh;
64   _cellId = vtkCellId;
65   _index = 0;
66   _type = aType;
67   //MESSAGE("SMDS_VtkCellInterlacedIterator (UNV)" << _type);
68
69   _vtkIdList = vtkIdList::New();
70   vtkIdType* pts;
71   vtkUnstructuredGrid* grid = _mesh->getGrid();
72   grid->GetCellPoints((vtkIdType)_cellId, (vtkIdType&)_nbNodes, pts);
73   _vtkIdList->SetNumberOfIds(_nbNodes);
74   int *ids = 0;
75   switch (_type)
76   {
77     case SMDSEntity_Quad_Edge:
78       {
79         static int id[] = { 0, 2, 1 };
80         ids = id;
81         break;
82       }
83     case SMDSEntity_Quad_Triangle:
84     case SMDSEntity_BiQuad_Triangle:
85       {
86         static int id[] = { 0, 3, 1, 4, 2, 5 };
87         ids = id;
88         _nbNodes = 6;
89         break;
90       }
91     case SMDSEntity_Quad_Quadrangle:
92     case SMDSEntity_BiQuad_Quadrangle:
93       {
94         static int id[] = { 0, 4, 1, 5, 2, 6, 3, 7 };
95         ids = id;
96         _nbNodes = 8;
97         break;
98       }
99     case SMDSEntity_Quad_Tetra:
100       {
101         static int id[] = { 0, 4, 1, 5, 2, 6, 7, 8, 9, 3 };
102         ids = id;
103         break;
104       }
105     case SMDSEntity_Quad_Pyramid:
106       {
107         static int id[] = { 0, 5, 1, 6, 2, 7, 3, 8, 9, 10, 11, 12, 4 };
108         ids = id;
109         break;
110       }
111     case SMDSEntity_Penta:
112       {
113         static int id[] = { 0, 2, 1, 3, 5, 4 };
114         ids = id;
115         break;
116       }
117     case SMDSEntity_Quad_Penta:
118       {
119         static int id[] = { 0, 8, 2, 7, 1, 6, 12, 14, 13, 3, 11, 5, 10, 4, 9 };
120         ids = id;
121         break;
122       }
123     case SMDSEntity_Quad_Hexa:
124     case SMDSEntity_TriQuad_Hexa:
125       {
126         static int id[] = { 0, 8, 1, 9, 2, 10, 3, 11, 16, 17, 18, 19, 4, 12, 5, 13, 6, 14, 7, 15 };
127         ids = id;
128         _nbNodes = 20;
129         break;
130       }
131     case SMDSEntity_Polygon:
132     case SMDSEntity_Quad_Polygon:
133     case SMDSEntity_Polyhedra:
134     case SMDSEntity_Quad_Polyhedra:
135     default:
136       {
137         // 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,
138         //                     25, 26, 27, 28, 29 };
139         // ids = id;
140         // break;
141       }
142   }
143   if ( ids )
144     for (int i = 0; i < _nbNodes; i++)
145       _vtkIdList->SetId(i, pts[ids[i]]);
146   else
147     for (int i = 0; i < _nbNodes; i++)
148       _vtkIdList->SetId(i, pts[i]);
149 }
150
151 bool SMDS_VtkCellIteratorToUNV::more()
152 {
153   return SMDS_VtkCellIterator::more();
154 }
155
156 const SMDS_MeshNode* SMDS_VtkCellIteratorToUNV::next()
157 {
158   return static_cast< const SMDS_MeshNode* >( SMDS_VtkCellIterator::next() );
159 }
160
161 SMDS_VtkCellIteratorToUNV::~SMDS_VtkCellIteratorToUNV()
162 {
163 }
164
165 SMDS_VtkCellIteratorPolyH::SMDS_VtkCellIteratorPolyH(SMDS_Mesh* mesh, int vtkCellId, SMDSAbs_EntityType aType) :
166   SMDS_VtkCellIterator()
167 {
168   _mesh = mesh;
169   _cellId = vtkCellId;
170   _index = 0;
171   _type = aType;
172   //MESSAGE("SMDS_VtkCellIteratorPolyH " << _type);
173   _vtkIdList = vtkIdList::New();
174   vtkUnstructuredGrid* grid = _mesh->getGrid();
175   grid->GetCellPoints(_cellId, _vtkIdList);
176   _nbNodes = _vtkIdList->GetNumberOfIds();
177   switch (_type)
178   {
179     case SMDSEntity_Polyhedra:
180       {
181         //MESSAGE("SMDS_VtkCellIterator Polyhedra");
182         vtkIdType nFaces = 0;
183         vtkIdType* ptIds = 0;
184         grid->GetFaceStream(_cellId, nFaces, ptIds);
185         int id = 0;
186         _nbNodesInFaces = 0;
187         for (int i = 0; i < nFaces; i++)
188           {
189             int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
190             _nbNodesInFaces += nodesInFace;
191             id += (nodesInFace + 1);
192           }
193         _vtkIdList->SetNumberOfIds(_nbNodesInFaces);
194         id = 0;
195         int n = 0;
196         for (int i = 0; i < nFaces; i++)
197           {
198             int nodesInFace = ptIds[id]; // nodeIds in ptIds[id+1 .. id+nodesInFace]
199             for (int k = 1; k <= nodesInFace; k++)
200               _vtkIdList->SetId(n++, ptIds[id + k]);
201             id += (nodesInFace + 1);
202           }
203         break;
204       }
205     default:
206       assert(0);
207   }
208 }
209
210 SMDS_VtkCellIteratorPolyH::~SMDS_VtkCellIteratorPolyH()
211 {
212 }
213
214 bool SMDS_VtkCellIteratorPolyH::more()
215 {
216   return (_index < _nbNodesInFaces);
217 }