Salome HOME
0020743: EDF 1271 SMESH : Create a mesh from a group / export groups
[modules/smesh.git] / src / SMDS / SMDS_MeshInfo.hxx
1 // Copyright (C) 2007-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 // File      : SMDS_MeshInfo.hxx
21 // Created   : Mon Sep 24 18:32:41 2007
22 // Author    : Edward AGAPOV (eap)
23 //
24 #ifndef SMDS_MeshInfo_HeaderFile
25 #define SMDS_MeshInfo_HeaderFile
26
27 using namespace std;
28
29 #include "SMESH_SMDS.hxx"
30
31 #include "SMDS_MeshElement.hxx"
32
33 class SMDS_EXPORT SMDS_MeshInfo
34 {
35 public:
36
37   inline SMDS_MeshInfo();
38   inline void Clear();
39
40   int NbNodes() const { return myNbNodes; }
41   inline int NbElements(SMDSAbs_ElementType type=SMDSAbs_All) const;
42   inline int NbEntities(SMDSAbs_EntityType  type) const;
43
44   int Nb0DElements() const { return myNb0DElements; }
45   inline int NbEdges      (SMDSAbs_ElementOrder order = ORDER_ANY) const;
46   inline int NbFaces      (SMDSAbs_ElementOrder order = ORDER_ANY) const;
47   inline int NbTriangles  (SMDSAbs_ElementOrder order = ORDER_ANY) const;
48   inline int NbQuadrangles(SMDSAbs_ElementOrder order = ORDER_ANY) const;
49   int NbPolygons() const { return myNbPolygons; }
50
51   inline int NbVolumes (SMDSAbs_ElementOrder order = ORDER_ANY) const;
52   inline int NbTetras  (SMDSAbs_ElementOrder order = ORDER_ANY) const;
53   inline int NbHexas   (SMDSAbs_ElementOrder order = ORDER_ANY) const;
54   inline int NbPyramids(SMDSAbs_ElementOrder order = ORDER_ANY) const;
55   inline int NbPrisms  (SMDSAbs_ElementOrder order = ORDER_ANY) const;
56   int NbPolyhedrons() const { return myNbPolyhedrons; }
57
58 protected:
59   inline void addWithPoly(const SMDS_MeshElement* el);
60
61 private:
62   friend class SMDS_Mesh;
63
64   // methods to count NOT POLY elements
65   inline void remove(const SMDS_MeshElement* el);
66   inline void add   (const SMDS_MeshElement* el);
67   inline int  index(SMDSAbs_ElementType type, int nbNodes) const;
68   // methods to remove elements of ANY kind
69   inline void RemoveEdge(const SMDS_MeshElement* el);
70   inline void RemoveFace(const SMDS_MeshElement* el);
71   inline void RemoveVolume(const SMDS_MeshElement* el);
72
73   int myNbNodes;
74
75   int myNb0DElements;
76   int myNbEdges      , myNbQuadEdges      ;
77   int myNbTriangles  , myNbQuadTriangles  ;
78   int myNbQuadrangles, myNbQuadQuadrangles;
79   int myNbPolygons;
80
81   int myNbTetras  , myNbQuadTetras  ;
82   int myNbHexas   , myNbQuadHexas   ;
83   int myNbPyramids, myNbQuadPyramids;
84   int myNbPrisms  , myNbQuadPrisms  ;
85   int myNbPolyhedrons;
86
87   std::vector<int*> myNb; // pointers to myNb... fields
88   std::vector<int>  myShift; // shift to get an index in myNb by elem->NbNodes()
89 };
90
91 inline SMDS_MeshInfo::SMDS_MeshInfo():
92   myNbNodes(0),
93   myNb0DElements(0),
94   myNbEdges      (0), myNbQuadEdges      (0),
95   myNbTriangles  (0), myNbQuadTriangles  (0),
96   myNbQuadrangles(0), myNbQuadQuadrangles(0),
97   myNbPolygons(0),
98   myNbTetras  (0), myNbQuadTetras  (0),
99   myNbHexas   (0), myNbQuadHexas   (0),
100   myNbPyramids(0), myNbQuadPyramids(0),
101   myNbPrisms  (0), myNbQuadPrisms  (0),
102   myNbPolyhedrons(0)
103 {
104   // Number of nodes in standard element types
105   // n   v  f  e  0  n
106   // o   o  a  d  d  o
107   // d   l  c  g     d
108   // e      e  e     e
109   // s
110   // -----------------
111   // 0         *
112   // 1            .  *
113   // 2         *
114   // 3      .     *
115   // 4   *  .  .
116   // 5   *
117   // 6   *  .
118   // 7
119   // 8   *  .
120   // 9
121   // 10  *
122   // 11     *
123   // 12     *
124   // 13  *
125   // 14     *
126   // 15  *
127   // 16     *
128   // 17
129   // 18
130   // 19
131   // 20  *
132   //
133   // So to have a unique index for each type basing on nb of nodes, we use a shift:
134   myShift.resize(SMDSAbs_NbElementTypes, 0);
135
136   myShift[ SMDSAbs_Face      ] = +8; // 3->11, 4->12, 6->14, 8->16
137   myShift[ SMDSAbs_Edge      ] = -2; // 2->0, 4->2
138   myShift[ SMDSAbs_0DElement ] = +2; // 1->3
139
140   myNb.resize( index( SMDSAbs_Volume,20 ) + 1, NULL);
141
142   myNb[ index( SMDSAbs_Node,1 )] = & myNbNodes;
143
144   myNb[ index( SMDSAbs_0DElement,1 )] = & myNb0DElements;
145
146   myNb[ index( SMDSAbs_Edge,2 )] = & myNbEdges;
147   myNb[ index( SMDSAbs_Edge,4 )] = & myNbQuadEdges;
148
149   myNb[ index( SMDSAbs_Face,3 )] = & myNbTriangles;
150   myNb[ index( SMDSAbs_Face,4 )] = & myNbQuadrangles;
151   myNb[ index( SMDSAbs_Face,6 )] = & myNbQuadTriangles;
152   myNb[ index( SMDSAbs_Face,8 )] = & myNbQuadQuadrangles;
153
154   myNb[ index( SMDSAbs_Volume, 4)]  = & myNbTetras;
155   myNb[ index( SMDSAbs_Volume, 5)]  = & myNbPyramids;
156   myNb[ index( SMDSAbs_Volume, 6)]  = & myNbPrisms;
157   myNb[ index( SMDSAbs_Volume, 8)]  = & myNbHexas;
158   myNb[ index( SMDSAbs_Volume, 10)] = & myNbQuadTetras;  
159   myNb[ index( SMDSAbs_Volume, 13)] = & myNbQuadPyramids;
160   myNb[ index( SMDSAbs_Volume, 15)] = & myNbQuadPrisms;  
161   myNb[ index( SMDSAbs_Volume, 20)] = & myNbQuadHexas;   
162 }
163
164 inline void // Clear
165 SMDS_MeshInfo::Clear()
166 { for ( int i=0; i<myNb.size(); ++i ) if ( myNb[i] ) (*myNb[i])=0;
167   myNbPolygons=myNbPolyhedrons=0;
168 }
169
170 inline int // index
171 SMDS_MeshInfo::index(SMDSAbs_ElementType type, int nbNodes) const
172 { return nbNodes + myShift[ type ]; }
173
174 inline void // remove
175 SMDS_MeshInfo::remove(const SMDS_MeshElement* el)
176 { --(*myNb[ index(el->GetType(), el->NbNodes()) ]); }
177
178 inline void // add
179 SMDS_MeshInfo::add(const SMDS_MeshElement* el)
180 { ++(*myNb[ index(el->GetType(), el->NbNodes()) ]); }
181
182 inline void // addWithPoly
183 SMDS_MeshInfo::addWithPoly(const SMDS_MeshElement* el)
184 {
185   if ( el->IsPoly() )
186     ++( el->GetType()==SMDSAbs_Face ? myNbPolygons : myNbPolyhedrons );
187   else
188     add(el);
189 }
190 inline void // RemoveEdge
191 SMDS_MeshInfo::RemoveEdge(const SMDS_MeshElement* el)
192 { if ( el->IsQuadratic() ) --myNbQuadEdges; else --myNbEdges; }
193
194 inline void // RemoveFace
195 SMDS_MeshInfo::RemoveFace(const SMDS_MeshElement* el)
196 { if ( el->IsPoly() ) --myNbPolygons; else remove( el ); }
197
198 inline void // RemoveVolume
199 SMDS_MeshInfo::RemoveVolume(const SMDS_MeshElement* el)
200 { if ( el->IsPoly() ) --myNbPolyhedrons; else remove( el ); }
201
202 inline int // NbEdges
203 SMDS_MeshInfo::NbEdges      (SMDSAbs_ElementOrder order) const
204 { return order == ORDER_ANY ? myNbEdges+myNbQuadEdges : order == ORDER_LINEAR ? myNbEdges : myNbQuadEdges; }
205
206 inline int // NbFaces
207 SMDS_MeshInfo::NbFaces      (SMDSAbs_ElementOrder order) const
208 { return NbTriangles(order)+NbQuadrangles(order)+(order == ORDER_QUADRATIC ? 0 : myNbPolygons); }
209
210 inline int // NbTriangles
211 SMDS_MeshInfo::NbTriangles  (SMDSAbs_ElementOrder order) const
212 { return order == ORDER_ANY ? myNbTriangles+myNbQuadTriangles : order == ORDER_LINEAR ? myNbTriangles : myNbQuadTriangles; }
213
214 inline int // NbQuadrangles
215 SMDS_MeshInfo::NbQuadrangles(SMDSAbs_ElementOrder order) const
216 { return order == ORDER_ANY ? myNbQuadrangles+myNbQuadQuadrangles : order == ORDER_LINEAR ? myNbQuadrangles : myNbQuadQuadrangles; }
217
218 inline int // NbVolumes
219 SMDS_MeshInfo::NbVolumes (SMDSAbs_ElementOrder order) const
220 { return NbTetras(order) + NbHexas(order) + NbPyramids(order) + NbPrisms(order) + (order == ORDER_QUADRATIC ? 0 : myNbPolyhedrons); }
221
222 inline int // NbTetras
223 SMDS_MeshInfo::NbTetras  (SMDSAbs_ElementOrder order) const
224 { return order == ORDER_ANY ? myNbTetras+myNbQuadTetras : order == ORDER_LINEAR ? myNbTetras : myNbQuadTetras; }
225
226 inline int // NbHexas
227 SMDS_MeshInfo::NbHexas   (SMDSAbs_ElementOrder order) const
228 { return order == ORDER_ANY ? myNbHexas+myNbQuadHexas : order == ORDER_LINEAR ? myNbHexas : myNbQuadHexas; }
229
230 inline int // NbPyramids
231 SMDS_MeshInfo::NbPyramids(SMDSAbs_ElementOrder order) const
232 { return order == ORDER_ANY ? myNbPyramids+myNbQuadPyramids : order == ORDER_LINEAR ? myNbPyramids : myNbQuadPyramids; }
233
234 inline int // NbPrisms
235 SMDS_MeshInfo::NbPrisms  (SMDSAbs_ElementOrder order) const
236 { return order == ORDER_ANY ? myNbPrisms+myNbQuadPrisms : order == ORDER_LINEAR ? myNbPrisms : myNbQuadPrisms; }
237
238 inline int // NbElements
239 SMDS_MeshInfo::NbElements(SMDSAbs_ElementType type) const
240
241   int nb = 0;
242   switch (type) {
243   case SMDSAbs_All:
244     for ( int i=1+index( SMDSAbs_Node,1 ); i<myNb.size(); ++i ) if ( myNb[i] ) nb += *myNb[i];
245     nb += myNbPolygons + myNbPolyhedrons;
246     break;
247   case SMDSAbs_Volume:
248     nb = myNbTetras+ myNbPyramids+ myNbPrisms+ myNbHexas+
249       myNbQuadTetras+ myNbQuadPyramids+ myNbQuadPrisms+ myNbQuadHexas+myNbPolyhedrons;
250     break;
251   case SMDSAbs_Face:
252     nb = myNbTriangles+ myNbQuadrangles+ myNbQuadTriangles+ myNbQuadQuadrangles + myNbPolygons;
253     break;
254   case SMDSAbs_Edge:
255     nb = myNbEdges + myNbQuadEdges;
256     break;
257   case SMDSAbs_0DElement:
258     nb = myNb0DElements;
259     break;
260   case SMDSAbs_Node:
261     nb = myNbNodes;
262     break;
263   default:;
264   }
265   return nb;
266 }
267
268 int // NbEntities
269 SMDS_MeshInfo::NbEntities(SMDSAbs_EntityType  type) const
270 {
271   switch (type) {
272   case SMDSEntity_Node:
273     return myNbNodes;
274     break;
275   case SMDSEntity_0D:
276     return myNb0DElements;
277     break;
278   case SMDSEntity_Edge:
279     return myNbEdges;
280     break;
281   case SMDSEntity_Quad_Edge:
282     return myNbQuadEdges;
283     break;
284   case SMDSEntity_Triangle:
285     return myNbTriangles;
286     break;
287   case SMDSEntity_Quad_Triangle:
288     return myNbQuadTriangles;
289     break;
290   case SMDSEntity_Quadrangle:
291     return myNbQuadrangles;
292     break;
293   case SMDSEntity_Quad_Quadrangle:
294     return myNbQuadQuadrangles;
295     break;
296   case SMDSEntity_Polygon:
297     return myNbPolygons;
298     break;
299   case SMDSEntity_Tetra:
300     return myNbTetras;
301     break;
302   case SMDSEntity_Quad_Tetra:
303     return myNbQuadTetras;
304     break;
305   case SMDSEntity_Pyramid:
306     return myNbPyramids;
307     break;
308   case SMDSEntity_Quad_Pyramid:
309     return myNbQuadPyramids;
310     break;
311   case SMDSEntity_Hexa:
312     return myNbHexas;
313     break;
314   case SMDSEntity_Quad_Hexa:
315     return myNbQuadHexas;
316     break;
317   case SMDSEntity_Penta:
318     return myNbPrisms;
319     break;
320   case SMDSEntity_Quad_Penta:
321     return myNbQuadPrisms;
322     break;
323   case SMDSEntity_Polyhedra:
324     return myNbPolyhedrons;
325     break;
326   case SMDSEntity_Quad_Polygon:
327   case SMDSEntity_Quad_Polyhedra:
328   default:
329   break;
330   }
331   return 0;
332 }
333
334 #endif