Salome HOME
Update copyrights
[modules/smesh.git] / src / SMESHDS / SMESHDS_SubMesh.cxx
1 // Copyright (C) 2007-2019  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, or (at your option) any later version.
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
31 #include "SMDS_ElementFactory.hxx"
32 #include "SMDS_IteratorOnIterators.hxx"
33 #include "SMDS_SetIterator.hxx"
34 #include "SMESHDS_Mesh.hxx"
35
36 #include <utilities.h>
37
38 namespace
39 {
40   typedef const SMDS_MeshElement* PElem;
41   typedef const SMDS_MeshNode*    PNode;
42
43   typedef SMDS_SetIterator< PElem, PElem const *,
44                             SMDS::SimpleAccessor< PElem, PElem const * >,
45                             SMDS::NonNullFilter< PElem > >                 EArrayIterator;
46
47   typedef SMDS_SetIterator< PNode, PNode const *,
48                             SMDS::SimpleAccessor< PNode, PNode const * >,
49                             SMDS::NonNullFilter< PNode > >                 NArrayIterator;
50
51   int ind1st( SMDSAbs_ElementType t )
52   {
53     return t == SMDSAbs_Node;
54   }
55
56   //=======================================================================
57   //class : _MyElemIteratorFromNodeIterator
58   //=======================================================================
59   class _MyElemIteratorFromNodeIterator : public SMDS_ElemIterator
60   {
61     SMDS_NodeIteratorPtr myItr;
62   public:
63     _MyElemIteratorFromNodeIterator(SMDS_NodeIteratorPtr nodeItr): myItr( nodeItr ) {}
64     bool more()                    { return myItr->more(); }
65     const SMDS_MeshElement* next() { return myItr->next(); }
66   };
67 }
68
69 //================================================================================
70 /*!
71  * \brief Constructor
72  */
73 //================================================================================
74
75 SMESHDS_SubMesh::SMESHDS_SubMesh(const SMESHDS_Mesh *parent, int index)
76   : SMDS_ElementHolder( parent )
77 {
78   myParent = parent;
79   myIndex = index;
80   myNbElements = 0;
81   myNbNodes = 0;
82   my1stElemNode[0] = my1stElemNode[1] = 0;
83 }
84
85 //================================================================================
86 /*!
87  * \brief Destructor
88  */
89 //================================================================================
90
91 SMESHDS_SubMesh::~SMESHDS_SubMesh()
92 {
93 }
94
95 //=======================================================================
96 //function : AddElement
97 //purpose  :
98 //=======================================================================
99
100 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * elem)
101 {
102   if (!IsComplexSubmesh())
103   {
104     if ( elem->GetType() == SMDSAbs_Node )
105     {
106       AddNode( static_cast< const SMDS_MeshNode* >( elem ));
107       return;
108     }
109     int oldShapeId = elem->GetShapeID();
110     if ( oldShapeId > 0 )
111     {
112       if (oldShapeId != myIndex)
113       {
114         throw SALOME_Exception
115           (LOCALIZED("add element in subshape already belonging to a subshape"));
116       }
117     }
118
119     elem->setShapeID( myIndex );
120     myNbElements++;
121
122     // remember element with smallest ID to optimize iteration on them
123     add( elem );
124   }
125 }
126
127 //=======================================================================
128 //function : RemoveElement
129 //purpose  :
130 //=======================================================================
131
132 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * elem )
133 {
134   if ( myNbElements == 0 || !elem || elem->IsNull() || elem->getshapeId() != myIndex )
135   {
136     return false;
137   }
138   if ( !IsComplexSubmesh() )
139   {
140     elem->setShapeID( 0 );
141     myNbElements--;
142
143     const SMDS_MeshElement* & elem1st = my1stElemNode[ ind1st( elem->GetType() )];
144     if ( elem1st == elem )
145     {
146       if ( myNbElements > 0 )
147       {
148         SMDS_ElemIteratorPtr it = myParent->shapeElementsIterator( myIndex, 1, elem1st );
149         if ( it->more() )
150           elem1st = it->next();
151         else
152           throw SALOME_Exception(LOCALIZED("invalid myNbElements"));
153       }
154       else
155       {
156         elem1st = 0;
157       }
158     }
159     return true;
160   }
161   return false;
162 }
163
164 //=======================================================================
165 //function : AddNode
166 //purpose  :
167 //=======================================================================
168
169 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
170 {
171   if ( !IsComplexSubmesh() )
172   {
173     const int shapeId = N->getshapeId();
174     if ( shapeId > 0 )
175     {
176       if ( shapeId != myIndex )
177         throw SALOME_Exception
178           (LOCALIZED("a node being in sub-mesh is added to another sub-mesh"));
179       return; // already in
180     }
181     N->setShapeID( myIndex );
182     myNbNodes++;
183
184     // remember node with smallest ID to optimize iteration on them
185     add( N );
186   }
187 }
188
189 //=======================================================================
190 //function : RemoveNode
191 //purpose  :
192 //=======================================================================
193
194 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N)
195 {
196   if ( myNbNodes == 0 || !N || N->getshapeId() != myIndex )
197   {
198     return false;
199   }
200   if ( !IsComplexSubmesh() )
201   {
202     N->setShapeID( 0 );
203     myNbNodes--;
204
205     const SMDS_MeshElement* & node1st = my1stElemNode[ ind1st( SMDSAbs_Node )];
206     if ( node1st == N )
207     {
208       if ( myNbNodes > 0 )
209       {
210         SMDS_NodeIteratorPtr it =
211           myParent->shapeNodesIterator( myIndex, 1, static_cast< PNode >( node1st ));
212         if ( it->more() )
213           node1st = it->next();
214         else
215           throw SALOME_Exception(LOCALIZED("invalid myNbNodes"));
216       }
217       else
218       {
219         node1st = 0;
220       }
221     }
222     return true;
223   }
224   return false;
225 }
226
227 //=======================================================================
228 //function : NbElements
229 //purpose  :
230 //=======================================================================
231
232 int SMESHDS_SubMesh::NbElements() const
233 {
234   if ( !IsComplexSubmesh() )
235     return myNbElements;
236
237   int nbElems = 0;
238   TSubMeshSet::const_iterator it = mySubMeshes.begin();
239   for ( ; it != mySubMeshes.end(); it++ )
240     nbElems += (*it)->NbElements();
241
242   return nbElems;
243 }
244
245 //=======================================================================
246 //function : NbNodes
247 //purpose  :
248 //=======================================================================
249
250 int SMESHDS_SubMesh::NbNodes() const
251 {
252   if ( !IsComplexSubmesh() )
253     return myNbNodes;
254
255   int nbElems = 0;
256   TSubMeshSet::const_iterator it = mySubMeshes.begin();
257   for ( ; it != mySubMeshes.end(); it++ )
258     nbElems += (*it)->NbNodes();
259
260   return nbElems;
261 }
262
263 // =====================
264 // class MyIterator
265 // =====================
266
267 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
268 {
269 public:
270   MyIterator (const TSubMeshSet& theSubMeshes)
271     : myMore(false), mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() )
272   {}
273   bool more()
274   {
275     while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
276     {
277       myElemIt = getElements(*mySubIt);
278       mySubIt++;
279     }
280     myMore = myElemIt.get() && myElemIt->more();
281     return myMore;
282   }
283   VALUE next()
284   {
285     VALUE elem = 0;
286     if ( myMore )
287       elem = myElemIt->next();
288     return elem;
289   }
290 protected:
291   virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
292   getElements(const SMESHDS_SubMesh*) const = 0;
293
294 private:
295   bool                                      myMore;
296   TSubMeshSet::const_iterator               mySubIt, mySubEnd;
297   boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
298 };
299
300 // =====================
301 // class MyElemIterator
302 // =====================
303
304 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
305 {
306 public:
307   MyElemIterator (const TSubMeshSet& theSubMeshes)
308     :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
309   SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
310   { return theSubMesh->GetElements(); }
311 };
312
313 // =====================
314 // class MyNodeIterator
315 // =====================
316
317 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
318 {
319 public:
320   MyNodeIterator (const TSubMeshSet& theSubMeshes)
321     :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
322   SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
323   { return theSubMesh->GetNodes(); }
324 };
325
326 //=======================================================================
327 //function : GetElements
328 //purpose  :
329 //=======================================================================
330
331 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
332 {
333   if ( IsComplexSubmesh() )
334     return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
335
336   const SMDS_MeshElement* const * elem1st = & my1stElemNode[ ind1st( SMDSAbs_All )];
337   if ( myNbElements < 2 )
338   {
339     return boost::make_shared< EArrayIterator >( elem1st, elem1st + myNbElements );
340   }
341
342   return myParent->shapeElementsIterator( myIndex, myNbElements, *elem1st );
343 }
344
345 //=======================================================================
346 //function : GetNodes
347 //purpose  :
348 //=======================================================================
349
350 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
351 {
352   if ( IsComplexSubmesh() )
353     return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
354
355   PNode const * node1st =
356     reinterpret_cast< PNode const* >( & my1stElemNode[ ind1st( SMDSAbs_Node )] );
357   if ( myNbNodes < 2 )
358   {
359     return boost::make_shared< NArrayIterator >( node1st, node1st + myNbNodes );
360   }
361
362   return myParent->shapeNodesIterator( myIndex, myNbNodes, *node1st );
363 }
364
365 //=======================================================================
366 //function : Contains
367 //purpose  : check if elem or node is in
368 //=======================================================================
369
370 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
371 {
372   if ( !ME || ME->IsNull() )
373     return false;
374
375   if ( IsComplexSubmesh() )
376   {
377     TSubMeshSet::const_iterator aSubIt = mySubMeshes.begin();
378     for (; aSubIt != mySubMeshes.end(); aSubIt++)
379       if ((*aSubIt)->Contains(ME))
380         return true;
381     return false;
382   }
383   return ME->getshapeId() == myIndex;
384 }
385
386 //=======================================================================
387 //function : IsQuadratic
388 //purpose  : Return true if my 1st element is quadratic
389 //=======================================================================
390
391 bool SMESHDS_SubMesh::IsQuadratic() const
392 {
393   if ( IsComplexSubmesh() )
394   {
395     TSubMeshSet::const_iterator aSubIt = mySubMeshes.begin();
396     for (; aSubIt != mySubMeshes.end(); aSubIt++)
397       if ((*aSubIt)->IsQuadratic())
398         return true;
399     return false;
400   }
401
402   if ( myNbElements == 0 )
403     return false;
404
405   SMDS_ElemIteratorPtr it = GetElements();
406   return it->more() && it->next()->IsQuadratic();
407 }
408
409 //=======================================================================
410 //function : AddSubMesh
411 //purpose  :
412 //=======================================================================
413
414 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
415 {
416   ASSERT( theSubMesh );
417   mySubMeshes.insert( theSubMesh );
418 }
419
420 //=======================================================================
421 //function : RemoveSubMesh
422 //purpose  : 
423 //=======================================================================
424
425 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
426 {
427   return mySubMeshes.erase( theSubMesh );
428 }
429
430 //=======================================================================
431 //function : RemoveAllSubmeshes
432 //purpose  : 
433 //=======================================================================
434
435 void SMESHDS_SubMesh::RemoveAllSubmeshes()
436 {
437   mySubMeshes.clear();
438 }
439
440 //=======================================================================
441 //function : ContainsSubMesh
442 //purpose  :
443 //=======================================================================
444
445 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
446 {
447   return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
448 }
449
450 //=======================================================================
451 //function : GetSubMeshIterator
452 //purpose  :
453 //=======================================================================
454
455 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
456 {
457   typedef SMDS_SetIterator< const SMESHDS_SubMesh*, TSubMeshSet::const_iterator > TIterator;
458   return boost::make_shared< TIterator >( mySubMeshes.begin(), mySubMeshes.end());
459 }
460
461 //=======================================================================
462 //function : Clear
463 //purpose  : remove the contents
464 //=======================================================================
465
466 void SMESHDS_SubMesh::Clear()
467 {
468   if ( myParent && myParent->NbNodes() > 0 )
469   {
470     if ( myNbElements > 0 )
471       for ( SMDS_ElemIteratorPtr it = GetElements(); it->more(); )
472       {
473         const SMDS_MeshElement * elem = it->next();
474         elem->setShapeID( 0 );
475       }
476     if ( myNbNodes > 0 )
477       for ( SMDS_NodeIteratorPtr it = GetNodes(); it->more(); )
478       {
479         const SMDS_MeshNode * elem = it->next();
480         elem->setShapeID( 0 );
481       }
482   }
483
484   myNbElements = 0;
485   myNbNodes = 0;
486   my1stElemNode[0] = my1stElemNode[1] = 0;
487
488   if ( NbSubMeshes() > 0 )
489   {
490     SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
491     while ( sub->more() ) {
492       if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
493         sm->Clear();
494     }
495   }
496 }
497
498 //=======================================================================
499 //function : getElements
500 //purpose  : Return iterator on all elements and nodes during compacting
501 //=======================================================================
502
503 SMDS_ElemIteratorPtr SMESHDS_SubMesh::getElements()
504 {
505   if ( IsComplexSubmesh() ) // return nothing
506     boost::make_shared< EArrayIterator >( & my1stElemNode[0], & my1stElemNode[0] );
507
508   typedef std::vector< SMDS_ElemIteratorPtr > TIterVec;
509   TIterVec iterVec(2);
510   iterVec[0] = GetElements();
511   iterVec[1].reset( new _MyElemIteratorFromNodeIterator( GetNodes() ));
512
513   return boost::make_shared< SMDS_IteratorOnIterators< PElem, TIterVec > >( iterVec );
514 }
515
516 //=======================================================================
517 //function : tmpClear
518 //purpose  : clean up after compacting
519 //=======================================================================
520
521 void SMESHDS_SubMesh::tmpClear()
522 {
523   my1stElemNode[0] = my1stElemNode[1] = 0;
524 }
525
526 //=======================================================================
527 //function : add
528 //purpose  : update my1stElemNode
529 //=======================================================================
530
531 void SMESHDS_SubMesh::add( const SMDS_MeshElement* elem )
532 {
533   const SMDS_MeshElement* & oldElem = my1stElemNode[ ind1st( elem->GetType() )];
534   if ( !oldElem || oldElem->GetID() > elem->GetID() )
535     oldElem = elem;
536 }