Salome HOME
SMESHDS_Mesh structures
[modules/smesh.git] / src / SMESHDS / SMESHDS_SubMesh.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH SMESHDS : management of mesh data and SMESH document
23 //  File   : SMESH_SubMesh.cxx
24 //  Author : Yves FRICAUD, OCC
25 //  Module : SMESH
26 //  $Header: 
27 //
28 #include "SMESHDS_SubMesh.hxx"
29
30 #include "utilities.h"
31 #include "SMDS_SetIterator.hxx"
32 #include <iostream>
33 #include <cassert>
34
35 using namespace std;
36
37 SMESHDS_SubMesh::SMESHDS_SubMesh()
38 {
39   myElements.clear();
40   myNodes.clear();
41   myUnusedIdNodes = 0;
42   myUnusedIdElements = 0;
43 }
44
45 //=======================================================================
46 //function : AddElement
47 //purpose  : 
48 //=======================================================================
49 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
50 {
51   if ( !IsComplexSubmesh() )
52     {
53       int idInSubShape = ME->getIdInShape();
54       assert(idInSubShape == -1);
55       SMDS_MeshElement* elem = (SMDS_MeshElement*)(ME);
56       elem->setIdInShape(myElements.size());
57       myElements.push_back(ME);
58     }
59 }
60
61 //=======================================================================
62 //function : RemoveElement
63 //purpose  : 
64 //=======================================================================
65 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
66 {
67 //  if ( !IsComplexSubmesh() && NbElements() ) {
68
69     if (!isElemDeleted) // alive element has valid ID and can be found
70       {
71         int idInSubShape = ME->getIdInShape();
72         assert(idInSubShape >= 0);
73         assert(idInSubShape < myElements.size());
74         myElements[idInSubShape] = 0; // this vector entry is no more used
75         myUnusedIdElements++;
76         return true;
77       }
78
79     
80 // --- strange ?
81 //     TElemSet::iterator e = myElements.begin(), eEnd = myElements.end();
82 //     for ( ; e != eEnd; ++e )
83 //       if ( ME == *e ) {
84 //         myElements.erase( e );
85 //         return true;
86 //       }
87 //   }
88   
89   return false;
90 }
91
92 //=======================================================================
93 //function : AddNode
94 //purpose  : 
95 //=======================================================================
96 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
97 {
98   if ( !IsComplexSubmesh() )
99     {
100       int idInSubShape = N->getIdInShape();
101       assert(idInSubShape == -1);
102       SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
103       node->setIdInShape(myNodes.size());
104       myNodes.push_back(N);
105     }
106 }
107
108 //=======================================================================
109 //function : RemoveNode
110 //purpose  : 
111 //=======================================================================
112
113 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
114 {
115 //   if ( !IsComplexSubmesh() && NbNodes() ) {
116
117     if (!isNodeDeleted) // alive node has valid ID and can be found
118       {
119         int idInSubShape = N->getIdInShape();
120         assert(idInSubShape >= 0);
121         assert(idInSubShape < myNodes.size());
122         myNodes[idInSubShape] = 0; // this vector entry is no more used
123         myUnusedIdNodes++;
124         return true;
125       }
126
127 // --- strange ?
128 //     TElemSet::iterator e = myNodes.begin(), eEnd = myNodes.end();
129 //     for ( ; e != eEnd; ++e )
130 //       if ( N == *e ) {
131 //         myNodes.erase( e );
132 //         return true;
133 //       }
134 //   }
135
136   return false;
137 }
138
139 //=======================================================================
140 //function : NbElements
141 //purpose  : 
142 //=======================================================================
143 int SMESHDS_SubMesh::NbElements() const
144 {
145   if ( !IsComplexSubmesh() )
146     return myElements.size() - myUnusedIdElements;
147
148   int nbElems = 0;
149   set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
150   for ( ; it != mySubMeshes.end(); it++ )
151     nbElems += (*it)->NbElements();
152
153   return nbElems;
154 }
155
156 //=======================================================================
157 //function : NbNodes
158 //purpose  : 
159 //=======================================================================
160
161 int SMESHDS_SubMesh::NbNodes() const
162 {
163  if ( !IsComplexSubmesh() )
164    return myNodes.size() - myUnusedIdNodes;
165
166   int nbElems = 0;
167   set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
168   for ( ; it != mySubMeshes.end(); it++ )
169     nbElems += (*it)->NbNodes();
170
171   return nbElems;
172 }
173
174 // =====================
175 // class MySetIterator
176 // =====================
177
178 template<class ELEM, typename TSET> class MySetIterator:
179   public SMDS_SetIterator<ELEM, typename TSET::const_iterator >
180 {
181   typedef SMDS_SetIterator<ELEM, typename TSET::const_iterator > TFather;
182   public:
183         MySetIterator(const TSET& s):TFather(s.begin(),s.end())
184         {
185         }
186 };
187
188 // =====================
189 // class MyIterator
190 // =====================
191
192 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
193 {
194  public:
195   MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
196     : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
197     {}
198   bool more()
199   {
200     while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
201     {
202       myElemIt = getElements(*mySubIt);
203       mySubIt++;
204     }
205     myMore = myElemIt.get() && myElemIt->more();
206     return myMore;
207   }
208   VALUE next()
209   {
210     VALUE elem = 0;
211     if ( myMore )
212       elem = myElemIt->next();
213     return elem;
214   }
215  protected:
216   virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
217     getElements(const SMESHDS_SubMesh*) const = 0;
218
219  private:
220   bool                                        myMore;
221   set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
222   boost::shared_ptr< SMDS_Iterator<VALUE> >   myElemIt;
223 };
224
225 // =====================
226 // class MyElemIterator
227 // =====================
228
229 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
230 {
231  public:
232   MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
233     :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
234   SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
235   { return theSubMesh->GetElements(); }
236 };
237
238 // =====================
239 // class MyNodeIterator
240 // =====================
241
242 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
243 {
244  public:
245   MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
246     :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
247   SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
248   { return theSubMesh->GetNodes(); }
249 };
250   
251 //=======================================================================
252 //function : GetElements
253 //purpose  : 
254 //=======================================================================
255
256 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
257 {
258   if ( IsComplexSubmesh() )
259     return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
260
261   return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*,TElemSet>(myElements));
262 }
263
264 //=======================================================================
265 //function : GetNodes
266 //purpose  : 
267 //=======================================================================
268
269 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
270 {
271   if ( IsComplexSubmesh() )
272     return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
273
274   return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*,TElemSet>(myNodes));
275 }
276
277 //=======================================================================
278 //function : Contains
279 //purpose  : check if elem or node is in
280 //=======================================================================
281
282 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
283 {
284   // DO NOT TRY TO FIND A REMOVED ELEMENT !!
285   //if ( IsComplexSubmesh() || !ME )
286   if (!ME )
287     return false;
288
289   if ( IsComplexSubmesh() )
290   {
291     set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
292     for ( ; aSubIt != mySubMeshes.end(); aSubIt++ )
293       if ( (*aSubIt)->Contains( ME ))
294         return true;
295     return false;
296   }
297
298   if ( ME->GetType() == SMDSAbs_Node )
299     {
300       int idInShape = ME->getIdInShape();
301       if ((idInShape >= 0) && (idInShape < myNodes.size()))
302         if (myNodes[idInShape] == ME) return true;
303     }
304   else
305     {
306       int idInShape = ME->getIdInShape();
307       if ((idInShape >= 0) && (idInShape < myElements.size()))
308         if (myElements[idInShape] == ME) return true;
309     }
310     return false;
311 }
312
313 //=======================================================================
314 //function : AddSubMesh
315 //purpose  : 
316 //=======================================================================
317
318 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
319 {
320   ASSERT( theSubMesh );
321   mySubMeshes.insert( theSubMesh );
322 }
323
324 //=======================================================================
325 //function : RemoveSubMesh
326 //purpose  : 
327 //=======================================================================
328
329 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
330 {
331   return mySubMeshes.erase( theSubMesh );
332 }
333
334 //=======================================================================
335 //function : ContainsSubMesh
336 //purpose  : 
337 //=======================================================================
338
339 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
340 {
341   return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
342 }
343
344 //=======================================================================
345 //function : GetSubMeshIterator
346 //purpose  : 
347 //=======================================================================
348
349 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
350 {
351   typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
352   return SMESHDS_SubMeshIteratorPtr
353     ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
354                                                                  mySubMeshes.end()));
355 }
356
357 //=======================================================================
358 //function : Clear
359 //purpose  : remove the contents
360 //=======================================================================
361
362 void SMESHDS_SubMesh::Clear()
363 {
364   myElements.clear();
365   myNodes.clear();
366   SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
367   while ( sub->more() ) {
368     if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
369       sm->Clear();
370   }
371 }
372
373 int SMESHDS_SubMesh::getSize()
374 {
375   int c = NbNodes();
376   int d = NbElements();
377   cerr << "SMESHDS_SubMesh::NbNodes " << c << endl;
378   cerr << "SMESHDS_SubMesh::NbElements " << d << endl;
379   return c+d;
380 }
381