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