Salome HOME
Optimize compactList()
[modules/smesh.git] / src / SMESHDS / SMESHDS_SubMesh.cxx
1 // Copyright (C) 2007-2013  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
23 //  SMESH SMESHDS : management of mesh data and SMESH document
24 //  File   : SMESH_SubMesh.cxx
25 //  Author : Yves FRICAUD, OCC
26 //  Module : SMESH
27 //  $Header: 
28 //
29 #include "SMESHDS_SubMesh.hxx"
30 #include "SMESHDS_Mesh.hxx"
31
32 #include "utilities.h"
33 #include "SMDS_SetIterator.hxx"
34 #include <iostream>
35 #include <cassert>
36
37 using namespace std;
38
39
40 //================================================================================
41 /*!
42  * \brief Constructor
43  */
44 //================================================================================
45
46 SMESHDS_SubMesh::SMESHDS_SubMesh(SMESHDS_Mesh *parent, int index)
47 {
48   myParent = parent;
49   myElements.clear();
50   myNodes.clear();
51   myIndex = index;
52   myUnusedIdNodes = 0;
53   myUnusedIdElements = 0;
54 }
55
56 //================================================================================
57 /*!
58  * \brief Destructor
59  */
60 //================================================================================
61
62 SMESHDS_SubMesh::~SMESHDS_SubMesh()
63 {
64 }
65
66 //=======================================================================
67 //function : AddElement
68 //purpose  : 
69 //=======================================================================
70
71 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
72 {
73   if (!IsComplexSubmesh())
74     {
75       if ( ME->GetType() == SMDSAbs_Node )
76       {
77         AddNode( static_cast< const SMDS_MeshNode* >( ME ));
78         return;
79       }
80       int oldShapeId = ME->getshapeId();
81       if ( oldShapeId > 0 )
82         {
83           if (oldShapeId != myIndex)
84             {
85               MESSAGE("add element in subshape already belonging to another subshape "
86                 << ME->GetID() << " " << oldShapeId << " " << myIndex);
87               throw SALOME_Exception(LOCALIZED("add element in subshape already belonging to a subshape"));
88             }
89           else
90             {
91               int idInSubShape = ME->getIdInShape();
92               if (idInSubShape >= 0)
93                 {
94                   MESSAGE("add element in subshape already belonging to that subshape "
95                       << ME->GetID() << " " << oldShapeId << " " << idInSubShape);
96                   // check if ok: do nothing if ok
97                   if (idInSubShape >= myElements.size())
98                     {
99                       MESSAGE("out of bounds " << idInSubShape << " " << myElements.size());
100                       throw SALOME_Exception(LOCALIZED("out of bounds"));
101                     }
102                   if (ME != myElements[idInSubShape])
103                     {
104                       MESSAGE("not the same element");
105                       throw SALOME_Exception(LOCALIZED("not the same element"));
106                     }
107                   MESSAGE("already done, OK, nothing to do");
108                   return;
109                 }
110             }
111         }
112
113       SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
114       elem->setShapeId(myIndex);
115       elem->setIdInShape(myElements.size());
116       myElements.push_back(ME);
117     }
118 }
119
120 //=======================================================================
121 //function : RemoveElement
122 //purpose  : 
123 //=======================================================================
124
125 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
126 {
127   if (!ME)
128   {
129     MESSAGE("-----------------> Remove Null Element " << isElemDeleted);
130     return false;
131   }
132   if (!IsComplexSubmesh())
133   {
134     if ( ME->getshapeId() != myIndex )
135       return false;
136     int idInSubShape = ME->getIdInShape();
137     SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
138     elem->setShapeId(0);
139     elem->setIdInShape(-1);
140     if ((idInSubShape >= 0) && (idInSubShape < myElements.size()))
141     {
142       myElements[idInSubShape] = 0; // this vector entry is no more used
143       myUnusedIdElements++;
144       return true;
145     }
146     return false;
147   }
148   MESSAGE("Try to remove an element from a complex submesh ");
149   return false;
150 }
151
152 //=======================================================================
153 //function : AddNode
154 //purpose  : 
155 //=======================================================================
156
157 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
158 {
159   if ( !IsComplexSubmesh() )
160   {
161     const int idInSubShape = N->getIdInShape();
162     const int shapeId      = N->getshapeId();
163     if ((shapeId > 0) && (idInSubShape >= 0))
164     {
165       if ( shapeId != myIndex )
166         throw SALOME_Exception
167           (LOCALIZED("a node being in sub-mesh is added to another sub-mesh"));
168       if ( idInSubShape >= myNodes.size() || myNodes[ idInSubShape ] != N )
169         throw SALOME_Exception
170           (LOCALIZED("a node with wrong idInSubShape is re-added to the same sub-mesh"));
171       return; // already in
172     }
173     SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
174     node->setShapeId(myIndex);
175     node->setIdInShape(myNodes.size());
176     myNodes.push_back(N);
177   }
178 }
179
180 //=======================================================================
181 //function : RemoveNode
182 //purpose  : 
183 //=======================================================================
184
185 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
186 {
187   if (!IsComplexSubmesh())
188   {
189     if ( N->getshapeId() != myIndex )
190       return false;
191     int idInSubShape = N->getIdInShape();
192     SMDS_MeshNode* node = (SMDS_MeshNode*) (N);
193     node->setShapeId(0);
194     node->setIdInShape(-1);
195     if ((idInSubShape >= 0) && (idInSubShape < myNodes.size()))
196     {
197       myNodes[idInSubShape] = 0; // this vector entry is no more used
198       myUnusedIdNodes++;
199       return true;
200     }
201     return false;
202   }
203   MESSAGE("Try to remove a node from a complex submesh");
204   return false;
205 }
206
207 //=======================================================================
208 //function : NbElements
209 //purpose  : 
210 //=======================================================================
211
212 int SMESHDS_SubMesh::NbElements() const
213 {
214   if ( !IsComplexSubmesh() )
215     return myElements.size() - myUnusedIdElements;
216
217   int nbElems = 0;
218   set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
219   for ( ; it != mySubMeshes.end(); it++ )
220     nbElems += (*it)->NbElements();
221
222   return nbElems;
223 }
224
225 //=======================================================================
226 //function : NbNodes
227 //purpose  : 
228 //=======================================================================
229
230 int SMESHDS_SubMesh::NbNodes() const
231 {
232   if ( !IsComplexSubmesh() )
233     return myNodes.size() - myUnusedIdNodes;
234
235   int nbElems = 0;
236   set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
237   for ( ; it != mySubMeshes.end(); it++ )
238     nbElems += (*it)->NbNodes();
239
240   return nbElems;
241 }
242
243 /*!
244  * template class used for iteration on submesh elements. Interface of iterator remains
245  * unchanged after redesign of SMDS to avoid modification everywhere in SMESH.
246  * instances are stored in shared_ptr for automatic destruction.
247  * Container is copied for iteration, because original can be modified
248  * by addition of elements, for instance, and then reallocated (vector)
249  */
250 template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
251 {
252 protected:
253   typename TSET::const_iterator _it, _end;
254   TSET _table;
255 public:
256   MySetIterator(const TSET& table)
257   {
258     _table = table;
259     _it = _table.begin();
260     _end = _table.end();
261     while ((_it != _end) && (*_it == 0))
262       _it++;
263   }
264
265   virtual bool more()
266   {
267     while ((_it != _end) && (*_it == 0))
268       _it++;
269     return (_it != _end);
270   }
271
272   virtual ELEM next()
273   {
274     ELEM e = *_it;
275     _it++;
276     return e;
277   }
278 };
279
280 // =====================
281 // class MyIterator
282 // =====================
283
284 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
285 {
286  public:
287   MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
288     : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
289     {}
290   bool more()
291   {
292     while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
293     {
294       myElemIt = getElements(*mySubIt);
295       mySubIt++;
296     }
297     myMore = myElemIt.get() && myElemIt->more();
298     return myMore;
299   }
300   VALUE next()
301   {
302     VALUE elem = 0;
303     if ( myMore )
304       elem = myElemIt->next();
305     return elem;
306   }
307  protected:
308   virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
309     getElements(const SMESHDS_SubMesh*) const = 0;
310
311  private:
312   bool                                        myMore;
313   set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
314   boost::shared_ptr< SMDS_Iterator<VALUE> >   myElemIt;
315 };
316
317 // =====================
318 // class MyElemIterator
319 // =====================
320
321 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
322 {
323  public:
324   MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
325     :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
326   SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
327   { return theSubMesh->GetElements(); }
328 };
329
330 // =====================
331 // class MyNodeIterator
332 // =====================
333
334 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
335 {
336  public:
337   MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
338     :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
339   SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
340   { return theSubMesh->GetNodes(); }
341 };
342   
343 //=======================================================================
344 //function : GetElements
345 //purpose  : 
346 //=======================================================================
347
348 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
349 {
350   if ( IsComplexSubmesh() )
351     return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
352   return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> >(myElements));
353 }
354
355 //=======================================================================
356 //function : GetNodes
357 //purpose  : 
358 //=======================================================================
359
360 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
361 {
362   if ( IsComplexSubmesh() )
363     return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
364
365   return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> >(myNodes));
366 }
367
368 //=======================================================================
369 //function : Contains
370 //purpose  : check if elem or node is in
371 //=======================================================================
372
373 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
374 {
375   // DO NOT TRY TO FIND A REMOVED ELEMENT !!
376   //if ( IsComplexSubmesh() || !ME )
377   if (!ME)
378     return false;
379
380   if (IsComplexSubmesh())
381     {
382       set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
383       for (; aSubIt != mySubMeshes.end(); aSubIt++)
384         if ((*aSubIt)->Contains(ME))
385           return true;
386       return false;
387     }
388
389   if (ME->GetType() == SMDSAbs_Node)
390     {
391       int idInShape = ME->getIdInShape();
392       if ((idInShape >= 0) && (idInShape < myNodes.size()))
393         if (myNodes[idInShape] == ME)
394           return true;
395     }
396   else
397     {
398       int idInShape = ME->getIdInShape();
399       if ((idInShape >= 0) && (idInShape < myElements.size()))
400         if (myElements[idInShape] == ME)
401           return true;
402     }
403   return false;
404 }
405
406 //=======================================================================
407 //function : AddSubMesh
408 //purpose  : 
409 //=======================================================================
410
411 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
412 {
413   ASSERT( theSubMesh );
414   mySubMeshes.insert( theSubMesh );
415 }
416
417 //=======================================================================
418 //function : RemoveSubMesh
419 //purpose  : 
420 //=======================================================================
421
422 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
423 {
424   return mySubMeshes.erase( theSubMesh );
425 }
426
427 //=======================================================================
428 //function : RemoveAllSubmeshes
429 //purpose  : 
430 //=======================================================================
431
432 void SMESHDS_SubMesh::RemoveAllSubmeshes()
433 {
434   mySubMeshes.clear();
435 }
436
437 //=======================================================================
438 //function : ContainsSubMesh
439 //purpose  : 
440 //=======================================================================
441
442 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
443 {
444   return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
445 }
446
447 //=======================================================================
448 //function : GetSubMeshIterator
449 //purpose  : 
450 //=======================================================================
451
452 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
453 {
454   typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
455   return SMESHDS_SubMeshIteratorPtr
456     ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
457                                                                  mySubMeshes.end()));
458 }
459
460 //=======================================================================
461 //function : Clear
462 //purpose  : remove the contents
463 //=======================================================================
464
465 void SMESHDS_SubMesh::Clear()
466 {
467   clearVector( myElements );
468   clearVector( myNodes );
469   myUnusedIdNodes = 0;
470   myUnusedIdElements = 0;
471   if ( NbSubMeshes() > 0 )
472   {
473     SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
474     while ( sub->more() ) {
475       if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
476         sm->Clear();
477     }
478   }
479 }
480
481 int SMESHDS_SubMesh::getSize()
482 {
483   int c = NbNodes();
484   int d = NbElements();
485   return c+d;
486 }
487
488 void SMESHDS_SubMesh::compactList()
489 {
490   if ( myUnusedIdElements > 0 )
491   {
492     std::vector<const SMDS_MeshElement*> newElems;
493     newElems.reserve( myElements.size() - myUnusedIdElements );
494     for (size_t i = 0; i < myElements.size(); i++)
495       if (myElements[i])
496       {
497         SMDS_MeshElement* elem = (SMDS_MeshElement*)myElements[i];
498         elem->setIdInShape(newElems.size());
499         newElems.push_back(elem);
500       }
501     myElements.swap(newElems);
502     myUnusedIdElements = 0;
503   }
504
505   if ( myUnusedIdNodes > 0 )
506   {
507     std::vector<const SMDS_MeshNode*> newNodes;
508     newNodes.reserve( myNodes.size() - myUnusedIdNodes );
509     for (size_t i = 0; i < myNodes.size(); i++)
510       if (myNodes[i])
511       {
512         SMDS_MeshNode* node = (SMDS_MeshNode*)myNodes[i];
513         node->setIdInShape(newNodes.size());
514         newNodes.push_back(node);
515       }
516     myNodes.swap(newNodes);
517     myUnusedIdNodes = 0;
518   }
519 }