1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESHDS : management of mesh data and SMESH document
24 // File : SMESH_SubMesh.cxx
25 // Author : Yves FRICAUD, OCC
29 #include "SMESHDS_SubMesh.hxx"
31 #include "SMDS_ElementFactory.hxx"
32 #include "SMDS_IteratorOnIterators.hxx"
33 #include "SMDS_SetIterator.hxx"
34 #include "SMESHDS_Mesh.hxx"
36 #include <utilities.h>
40 typedef const SMDS_MeshElement* PElem;
41 typedef const SMDS_MeshNode* PNode;
43 typedef SMDS_SetIterator< PElem, PElem const *,
44 SMDS::SimpleAccessor< PElem, PElem const * >,
45 SMDS::NonNullFilter< PElem > > EArrayIterator;
47 typedef SMDS_SetIterator< PNode, PNode const *,
48 SMDS::SimpleAccessor< PNode, PNode const * >,
49 SMDS::NonNullFilter< PNode > > NArrayIterator;
51 int ind1st( SMDSAbs_ElementType t )
53 return t == SMDSAbs_Node;
56 //=======================================================================
57 //class : _MyElemIteratorFromNodeIterator
58 //=======================================================================
59 class _MyElemIteratorFromNodeIterator : public SMDS_ElemIterator
61 SMDS_NodeIteratorPtr myItr;
63 _MyElemIteratorFromNodeIterator(SMDS_NodeIteratorPtr nodeItr): myItr( nodeItr ) {}
64 bool more() { return myItr->more(); }
65 const SMDS_MeshElement* next() { return myItr->next(); }
69 //================================================================================
73 //================================================================================
75 SMESHDS_SubMesh::SMESHDS_SubMesh(const SMESHDS_Mesh *parent, int index)
76 : SMDS_ElementHolder( parent )
82 my1stElemNode[0] = my1stElemNode[1] = 0;
85 //================================================================================
89 //================================================================================
91 SMESHDS_SubMesh::~SMESHDS_SubMesh()
95 //=======================================================================
96 //function : AddElement
98 //=======================================================================
100 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * elem)
102 if (!IsComplexSubmesh())
104 if ( elem->GetType() == SMDSAbs_Node )
106 AddNode( static_cast< const SMDS_MeshNode* >( elem ));
109 int oldShapeId = elem->GetShapeID();
110 if ( oldShapeId > 0 )
112 if (oldShapeId != myIndex)
114 throw SALOME_Exception
115 (LOCALIZED("add element in subshape already belonging to a subshape"));
123 elem->setShapeID( myIndex );
125 // remember element with smallest ID to optimize iteration on them
130 //=======================================================================
131 //function : RemoveElement
133 //=======================================================================
135 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * elem )
137 if ( myNbElements == 0 || !elem || elem->IsNull() || elem->getshapeId() != myIndex )
141 if ( !IsComplexSubmesh() )
143 elem->setShapeID( 0 );
146 const SMDS_MeshElement* & elem1st = my1stElemNode[ ind1st( elem->GetType() )];
147 if ( elem1st == elem )
149 if ( myNbElements > 0 )
151 SMDS_ElemIteratorPtr it = myParent->shapeElementsIterator( myIndex, 1, elem1st );
153 elem1st = it->next();
155 throw SALOME_Exception(LOCALIZED("invalid myNbElements"));
167 //=======================================================================
170 //=======================================================================
172 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
174 if ( !IsComplexSubmesh() )
176 const int shapeId = N->getshapeId();
179 if ( shapeId != myIndex )
180 throw SALOME_Exception
181 (LOCALIZED("a node being in sub-mesh is added to another sub-mesh"));
182 return; // already in
188 N->setShapeID( myIndex );
190 // remember node with smallest ID to optimize iteration on them
195 //=======================================================================
196 //function : RemoveNode
198 //=======================================================================
200 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N)
202 if ( myNbNodes == 0 || !N || N->getshapeId() != myIndex )
206 if ( !IsComplexSubmesh() )
211 const SMDS_MeshElement* & node1st = my1stElemNode[ ind1st( SMDSAbs_Node )];
216 SMDS_NodeIteratorPtr it =
217 myParent->shapeNodesIterator( myIndex, 1, static_cast< PNode >( node1st ));
219 node1st = it->next();
221 throw SALOME_Exception(LOCALIZED("invalid myNbNodes"));
233 //=======================================================================
234 //function : NbElements
236 //=======================================================================
238 int SMESHDS_SubMesh::NbElements() const
240 if ( !IsComplexSubmesh() )
244 TSubMeshSet::const_iterator it = mySubMeshes.begin();
245 for ( ; it != mySubMeshes.end(); it++ )
246 nbElems += (*it)->NbElements();
251 //=======================================================================
254 //=======================================================================
256 int SMESHDS_SubMesh::NbNodes() const
258 if ( !IsComplexSubmesh() )
262 TSubMeshSet::const_iterator it = mySubMeshes.begin();
263 for ( ; it != mySubMeshes.end(); it++ )
264 nbElems += (*it)->NbNodes();
269 // =====================
271 // =====================
273 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
276 MyIterator (const TSubMeshSet& theSubMeshes)
277 : myMore(false), mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() )
281 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
283 myElemIt = getElements(*mySubIt);
286 myMore = myElemIt.get() && myElemIt->more();
293 elem = myElemIt->next();
297 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
298 getElements(const SMESHDS_SubMesh*) const = 0;
302 TSubMeshSet::const_iterator mySubIt, mySubEnd;
303 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
306 // =====================
307 // class MyElemIterator
308 // =====================
310 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
313 MyElemIterator (const TSubMeshSet& theSubMeshes)
314 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
315 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
316 { return theSubMesh->GetElements(); }
319 // =====================
320 // class MyNodeIterator
321 // =====================
323 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
326 MyNodeIterator (const TSubMeshSet& theSubMeshes)
327 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
328 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
329 { return theSubMesh->GetNodes(); }
332 //=======================================================================
333 //function : GetElements
335 //=======================================================================
337 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
339 if ( IsComplexSubmesh() )
340 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
342 const SMDS_MeshElement* const * elem1st = & my1stElemNode[ ind1st( SMDSAbs_All )];
343 if ( myNbElements < 2 )
345 return boost::make_shared< EArrayIterator >( elem1st, elem1st + myNbElements );
348 return myParent->shapeElementsIterator( myIndex, myNbElements, *elem1st );
351 //=======================================================================
352 //function : GetNodes
354 //=======================================================================
356 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
358 if ( IsComplexSubmesh() )
359 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
361 PNode const * node1st =
362 reinterpret_cast< PNode const* >( & my1stElemNode[ ind1st( SMDSAbs_Node )] );
365 return boost::make_shared< NArrayIterator >( node1st, node1st + myNbNodes );
368 return myParent->shapeNodesIterator( myIndex, myNbNodes, *node1st );
371 //=======================================================================
372 //function : Contains
373 //purpose : check if elem or node is in
374 //=======================================================================
376 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
378 if ( !ME || ME->IsNull() )
381 if ( IsComplexSubmesh() )
383 TSubMeshSet::const_iterator aSubIt = mySubMeshes.begin();
384 for (; aSubIt != mySubMeshes.end(); aSubIt++)
385 if ((*aSubIt)->Contains(ME))
389 return ME->getshapeId() == myIndex;
392 //=======================================================================
393 //function : IsQuadratic
394 //purpose : Return true if my 1st element is quadratic
395 //=======================================================================
397 bool SMESHDS_SubMesh::IsQuadratic() const
399 if ( IsComplexSubmesh() )
401 TSubMeshSet::const_iterator aSubIt = mySubMeshes.begin();
402 for (; aSubIt != mySubMeshes.end(); aSubIt++)
403 if ((*aSubIt)->IsQuadratic())
408 if ( myNbElements == 0 )
411 SMDS_ElemIteratorPtr it = GetElements();
412 return it->more() && it->next()->IsQuadratic();
415 //=======================================================================
416 //function : AddSubMesh
418 //=======================================================================
420 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
422 ASSERT( theSubMesh );
423 mySubMeshes.insert( theSubMesh );
426 //=======================================================================
427 //function : RemoveSubMesh
429 //=======================================================================
431 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
433 return mySubMeshes.erase( theSubMesh );
436 //=======================================================================
437 //function : RemoveAllSubmeshes
439 //=======================================================================
441 void SMESHDS_SubMesh::RemoveAllSubmeshes()
446 //=======================================================================
447 //function : ContainsSubMesh
449 //=======================================================================
451 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
453 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
456 //=======================================================================
457 //function : GetSubMeshIterator
459 //=======================================================================
461 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
463 typedef SMDS_SetIterator< const SMESHDS_SubMesh*, TSubMeshSet::const_iterator > TIterator;
464 return boost::make_shared< TIterator >( mySubMeshes.begin(), mySubMeshes.end());
467 //=======================================================================
469 //purpose : remove the contents
470 //=======================================================================
472 void SMESHDS_SubMesh::Clear()
474 if ( myParent && myParent->NbNodes() > 0 )
476 if ( myNbElements > 0 )
477 for ( SMDS_ElemIteratorPtr it = GetElements(); it->more(); )
479 const SMDS_MeshElement * elem = it->next();
480 elem->setShapeID( 0 );
483 for ( SMDS_NodeIteratorPtr it = GetNodes(); it->more(); )
485 const SMDS_MeshNode * elem = it->next();
486 elem->setShapeID( 0 );
492 my1stElemNode[0] = my1stElemNode[1] = 0;
494 if ( NbSubMeshes() > 0 )
496 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
497 while ( sub->more() ) {
498 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
504 //=======================================================================
505 //function : getElements
506 //purpose : Return iterator on all elements and nodes during compacting
507 //=======================================================================
509 SMDS_ElemIteratorPtr SMESHDS_SubMesh::getElements()
511 if ( IsComplexSubmesh() ) // return nothing
512 boost::make_shared< EArrayIterator >( & my1stElemNode[0], & my1stElemNode[0] );
514 typedef std::vector< SMDS_ElemIteratorPtr > TIterVec;
516 iterVec[0] = GetElements();
517 iterVec[1].reset( new _MyElemIteratorFromNodeIterator( GetNodes() ));
519 return boost::make_shared< SMDS_IteratorOnIterators< PElem, TIterVec > >( iterVec );
522 //=======================================================================
523 //function : tmpClear
524 //purpose : clean up after compacting
525 //=======================================================================
527 void SMESHDS_SubMesh::tmpClear()
529 my1stElemNode[0] = my1stElemNode[1] = 0;
532 //=======================================================================
534 //purpose : update my1stElemNode
535 //=======================================================================
537 void SMESHDS_SubMesh::add( const SMDS_MeshElement* elem )
539 const SMDS_MeshElement* & oldElem = my1stElemNode[ ind1st( elem->GetType() )];
540 if ( !oldElem || oldElem->GetID() > elem->GetID() )