1 // Copyright (C) 2007-2019 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"));
119 elem->setShapeID( myIndex );
122 // remember element with smallest ID to optimize iteration on them
127 //=======================================================================
128 //function : RemoveElement
130 //=======================================================================
132 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * elem )
134 if ( myNbElements == 0 || !elem || elem->IsNull() || elem->getshapeId() != myIndex )
138 if ( !IsComplexSubmesh() )
140 elem->setShapeID( 0 );
143 const SMDS_MeshElement* & elem1st = my1stElemNode[ ind1st( elem->GetType() )];
144 if ( elem1st == elem )
146 if ( myNbElements > 0 )
148 SMDS_ElemIteratorPtr it = myParent->shapeElementsIterator( myIndex, 1, elem1st );
150 elem1st = it->next();
152 throw SALOME_Exception(LOCALIZED("invalid myNbElements"));
164 //=======================================================================
167 //=======================================================================
169 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
171 if ( !IsComplexSubmesh() )
173 const int shapeId = N->getshapeId();
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
181 N->setShapeID( myIndex );
184 // remember node with smallest ID to optimize iteration on them
189 //=======================================================================
190 //function : RemoveNode
192 //=======================================================================
194 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N)
196 if ( myNbNodes == 0 || !N || N->getshapeId() != myIndex )
200 if ( !IsComplexSubmesh() )
205 const SMDS_MeshElement* & node1st = my1stElemNode[ ind1st( SMDSAbs_Node )];
210 SMDS_NodeIteratorPtr it =
211 myParent->shapeNodesIterator( myIndex, 1, static_cast< PNode >( node1st ));
213 node1st = it->next();
215 throw SALOME_Exception(LOCALIZED("invalid myNbNodes"));
227 //=======================================================================
228 //function : NbElements
230 //=======================================================================
232 int SMESHDS_SubMesh::NbElements() const
234 if ( !IsComplexSubmesh() )
238 TSubMeshSet::const_iterator it = mySubMeshes.begin();
239 for ( ; it != mySubMeshes.end(); it++ )
240 nbElems += (*it)->NbElements();
245 //=======================================================================
248 //=======================================================================
250 int SMESHDS_SubMesh::NbNodes() const
252 if ( !IsComplexSubmesh() )
256 TSubMeshSet::const_iterator it = mySubMeshes.begin();
257 for ( ; it != mySubMeshes.end(); it++ )
258 nbElems += (*it)->NbNodes();
263 // =====================
265 // =====================
267 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
270 MyIterator (const TSubMeshSet& theSubMeshes)
271 : myMore(false), mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() )
275 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
277 myElemIt = getElements(*mySubIt);
280 myMore = myElemIt.get() && myElemIt->more();
287 elem = myElemIt->next();
291 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
292 getElements(const SMESHDS_SubMesh*) const = 0;
296 TSubMeshSet::const_iterator mySubIt, mySubEnd;
297 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
300 // =====================
301 // class MyElemIterator
302 // =====================
304 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
307 MyElemIterator (const TSubMeshSet& theSubMeshes)
308 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
309 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
310 { return theSubMesh->GetElements(); }
313 // =====================
314 // class MyNodeIterator
315 // =====================
317 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
320 MyNodeIterator (const TSubMeshSet& theSubMeshes)
321 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
322 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
323 { return theSubMesh->GetNodes(); }
326 //=======================================================================
327 //function : GetElements
329 //=======================================================================
331 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
333 if ( IsComplexSubmesh() )
334 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
336 const SMDS_MeshElement* const * elem1st = & my1stElemNode[ ind1st( SMDSAbs_All )];
337 if ( myNbElements < 2 )
339 return boost::make_shared< EArrayIterator >( elem1st, elem1st + myNbElements );
342 return myParent->shapeElementsIterator( myIndex, myNbElements, *elem1st );
345 //=======================================================================
346 //function : GetNodes
348 //=======================================================================
350 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
352 if ( IsComplexSubmesh() )
353 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
355 PNode const * node1st =
356 reinterpret_cast< PNode const* >( & my1stElemNode[ ind1st( SMDSAbs_Node )] );
359 return boost::make_shared< NArrayIterator >( node1st, node1st + myNbNodes );
362 return myParent->shapeNodesIterator( myIndex, myNbNodes, *node1st );
365 //=======================================================================
366 //function : Contains
367 //purpose : check if elem or node is in
368 //=======================================================================
370 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
372 if ( !ME || ME->IsNull() )
375 if ( IsComplexSubmesh() )
377 TSubMeshSet::const_iterator aSubIt = mySubMeshes.begin();
378 for (; aSubIt != mySubMeshes.end(); aSubIt++)
379 if ((*aSubIt)->Contains(ME))
383 return ME->getshapeId() == myIndex;
386 //=======================================================================
387 //function : IsQuadratic
388 //purpose : Return true if my 1st element is quadratic
389 //=======================================================================
391 bool SMESHDS_SubMesh::IsQuadratic() const
393 if ( IsComplexSubmesh() )
395 TSubMeshSet::const_iterator aSubIt = mySubMeshes.begin();
396 for (; aSubIt != mySubMeshes.end(); aSubIt++)
397 if ((*aSubIt)->IsQuadratic())
402 if ( myNbElements == 0 )
405 SMDS_ElemIteratorPtr it = GetElements();
406 return it->more() && it->next()->IsQuadratic();
409 //=======================================================================
410 //function : AddSubMesh
412 //=======================================================================
414 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
416 ASSERT( theSubMesh );
417 mySubMeshes.insert( theSubMesh );
420 //=======================================================================
421 //function : RemoveSubMesh
423 //=======================================================================
425 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
427 return mySubMeshes.erase( theSubMesh );
430 //=======================================================================
431 //function : RemoveAllSubmeshes
433 //=======================================================================
435 void SMESHDS_SubMesh::RemoveAllSubmeshes()
440 //=======================================================================
441 //function : ContainsSubMesh
443 //=======================================================================
445 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
447 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
450 //=======================================================================
451 //function : GetSubMeshIterator
453 //=======================================================================
455 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
457 typedef SMDS_SetIterator< const SMESHDS_SubMesh*, TSubMeshSet::const_iterator > TIterator;
458 return boost::make_shared< TIterator >( mySubMeshes.begin(), mySubMeshes.end());
461 //=======================================================================
463 //purpose : remove the contents
464 //=======================================================================
466 void SMESHDS_SubMesh::Clear()
468 if ( myParent && myParent->NbNodes() > 0 )
470 if ( myNbElements > 0 )
471 for ( SMDS_ElemIteratorPtr it = GetElements(); it->more(); )
473 const SMDS_MeshElement * elem = it->next();
474 elem->setShapeID( 0 );
477 for ( SMDS_NodeIteratorPtr it = GetNodes(); it->more(); )
479 const SMDS_MeshNode * elem = it->next();
480 elem->setShapeID( 0 );
486 my1stElemNode[0] = my1stElemNode[1] = 0;
488 if ( NbSubMeshes() > 0 )
490 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
491 while ( sub->more() ) {
492 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
498 //=======================================================================
499 //function : getElements
500 //purpose : Return iterator on all elements and nodes during compacting
501 //=======================================================================
503 SMDS_ElemIteratorPtr SMESHDS_SubMesh::getElements()
505 if ( IsComplexSubmesh() ) // return nothing
506 boost::make_shared< EArrayIterator >( & my1stElemNode[0], & my1stElemNode[0] );
508 typedef std::vector< SMDS_ElemIteratorPtr > TIterVec;
510 iterVec[0] = GetElements();
511 iterVec[1].reset( new _MyElemIteratorFromNodeIterator( GetNodes() ));
513 return boost::make_shared< SMDS_IteratorOnIterators< PElem, TIterVec > >( iterVec );
516 //=======================================================================
517 //function : tmpClear
518 //purpose : clean up after compacting
519 //=======================================================================
521 void SMESHDS_SubMesh::tmpClear()
523 my1stElemNode[0] = my1stElemNode[1] = 0;
526 //=======================================================================
528 //purpose : update my1stElemNode
529 //=======================================================================
531 void SMESHDS_SubMesh::add( const SMDS_MeshElement* elem )
533 const SMDS_MeshElement* & oldElem = my1stElemNode[ ind1st( elem->GetType() )];
534 if ( !oldElem || oldElem->GetID() > elem->GetID() )