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