1 // Copyright (C) 2007-2016 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(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 ( !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 )
152 //=======================================================================
155 //=======================================================================
157 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
159 if ( !IsComplexSubmesh() )
161 const int shapeId = N->getshapeId();
164 if ( shapeId != myIndex )
165 throw SALOME_Exception
166 (LOCALIZED("a node being in sub-mesh is added to another sub-mesh"));
167 return; // already in
169 N->setShapeID( myIndex );
172 // remember node with smallest ID to optimize iteration on them
177 //=======================================================================
178 //function : RemoveNode
180 //=======================================================================
182 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N)
184 if ( !N || N->getshapeId() != myIndex )
188 if ( !IsComplexSubmesh() )
193 const SMDS_MeshElement* & node1st = my1stElemNode[ ind1st( SMDSAbs_Node )];
202 //=======================================================================
203 //function : NbElements
205 //=======================================================================
207 int SMESHDS_SubMesh::NbElements() const
209 if ( !IsComplexSubmesh() )
213 TSubMeshSet::const_iterator it = mySubMeshes.begin();
214 for ( ; it != mySubMeshes.end(); it++ )
215 nbElems += (*it)->NbElements();
220 //=======================================================================
223 //=======================================================================
225 int SMESHDS_SubMesh::NbNodes() const
227 if ( !IsComplexSubmesh() )
231 TSubMeshSet::const_iterator it = mySubMeshes.begin();
232 for ( ; it != mySubMeshes.end(); it++ )
233 nbElems += (*it)->NbNodes();
238 // =====================
240 // =====================
242 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
245 MyIterator (const TSubMeshSet& theSubMeshes)
246 : myMore(false), mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() )
250 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
252 myElemIt = getElements(*mySubIt);
255 myMore = myElemIt.get() && myElemIt->more();
262 elem = myElemIt->next();
266 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
267 getElements(const SMESHDS_SubMesh*) const = 0;
271 TSubMeshSet::const_iterator mySubIt, mySubEnd;
272 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
275 // =====================
276 // class MyElemIterator
277 // =====================
279 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
282 MyElemIterator (const TSubMeshSet& theSubMeshes)
283 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
284 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
285 { return theSubMesh->GetElements(); }
288 // =====================
289 // class MyNodeIterator
290 // =====================
292 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
295 MyNodeIterator (const TSubMeshSet& theSubMeshes)
296 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
297 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
298 { return theSubMesh->GetNodes(); }
301 //=======================================================================
302 //function : GetElements
304 //=======================================================================
306 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
308 if ( IsComplexSubmesh() )
309 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
311 const SMDS_MeshElement* const * elem1st = & my1stElemNode[ ind1st( SMDSAbs_All )];
312 if ( myNbElements == 1 )
314 return boost::make_shared< EArrayIterator >( elem1st, elem1st+1 );
317 return myParent->shapeElementsIterator( myIndex, myNbElements, *elem1st );
320 //=======================================================================
321 //function : GetNodes
323 //=======================================================================
325 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
327 if ( IsComplexSubmesh() )
328 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
330 PNode const * node1st =
331 reinterpret_cast< PNode const* >( & my1stElemNode[ ind1st( SMDSAbs_Node )] );
332 if ( myNbNodes == 1 )
334 return boost::make_shared< NArrayIterator >( node1st, node1st+1 );
337 return myParent->shapeNodesIterator( myIndex, myNbNodes, *node1st );
340 //=======================================================================
341 //function : Contains
342 //purpose : check if elem or node is in
343 //=======================================================================
345 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
347 if ( !ME || ME->IsNull() )
350 if ( IsComplexSubmesh() )
352 TSubMeshSet::const_iterator aSubIt = mySubMeshes.begin();
353 for (; aSubIt != mySubMeshes.end(); aSubIt++)
354 if ((*aSubIt)->Contains(ME))
358 return ME->getshapeId() == myIndex;
361 //=======================================================================
362 //function : IsQuadratic
363 //purpose : Return true if my 1st element is quadratic
364 //=======================================================================
366 bool SMESHDS_SubMesh::IsQuadratic() const
368 if ( IsComplexSubmesh() )
370 TSubMeshSet::const_iterator aSubIt = mySubMeshes.begin();
371 for (; aSubIt != mySubMeshes.end(); aSubIt++)
372 if ((*aSubIt)->IsQuadratic())
377 if ( myNbElements == 0 )
380 SMDS_ElemIteratorPtr it = GetElements();
381 return it->more() && it->next()->IsQuadratic();
384 //=======================================================================
385 //function : AddSubMesh
387 //=======================================================================
389 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
391 ASSERT( theSubMesh );
392 mySubMeshes.insert( theSubMesh );
395 //=======================================================================
396 //function : RemoveSubMesh
398 //=======================================================================
400 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
402 return mySubMeshes.erase( theSubMesh );
405 //=======================================================================
406 //function : RemoveAllSubmeshes
408 //=======================================================================
410 void SMESHDS_SubMesh::RemoveAllSubmeshes()
415 //=======================================================================
416 //function : ContainsSubMesh
418 //=======================================================================
420 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
422 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
425 //=======================================================================
426 //function : GetSubMeshIterator
428 //=======================================================================
430 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
432 typedef SMDS_SetIterator< const SMESHDS_SubMesh*, TSubMeshSet::const_iterator > TIterator;
433 return boost::make_shared< TIterator >( mySubMeshes.begin(), mySubMeshes.end());
436 //=======================================================================
438 //purpose : remove the contents
439 //=======================================================================
441 void SMESHDS_SubMesh::Clear()
443 if ( myParent && myParent->NbNodes() > 0 )
445 if ( myNbElements > 0 )
446 for ( SMDS_ElemIteratorPtr it = GetElements(); it->more(); )
448 const SMDS_MeshElement * elem = it->next();
449 elem->setShapeID( 0 );
452 for ( SMDS_NodeIteratorPtr it = GetNodes(); it->more(); )
454 const SMDS_MeshNode * elem = it->next();
455 elem->setShapeID( 0 );
461 my1stElemNode[0] = my1stElemNode[1] = 0;
462 if ( NbSubMeshes() > 0 )
464 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
465 while ( sub->more() ) {
466 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
472 //=======================================================================
473 //function : getElements
474 //purpose : Return iterator on all elements and nodes during compacting
475 //=======================================================================
477 SMDS_ElemIteratorPtr SMESHDS_SubMesh::getElements()
479 if ( IsComplexSubmesh() ) // return nothing
480 boost::make_shared< EArrayIterator >( & my1stElemNode[0], & my1stElemNode[0] );
482 typedef std::vector< SMDS_ElemIteratorPtr > TIterVec;
484 iterVec[0] = GetElements();
485 iterVec[1].reset( new _MyElemIteratorFromNodeIterator( GetNodes() ));
487 return boost::make_shared< SMDS_IteratorOnIterators< PElem, TIterVec > >( iterVec );
490 //=======================================================================
491 //function : tmpClear
492 //purpose : clean up after compacting
493 //=======================================================================
495 void SMESHDS_SubMesh::tmpClear()
497 my1stElemNode[0] = my1stElemNode[1] = 0;
500 //=======================================================================
502 //purpose : update my1stElemNode
503 //=======================================================================
505 void SMESHDS_SubMesh::add( const SMDS_MeshElement* elem )
507 const SMDS_MeshElement* & oldElem = my1stElemNode[ ind1st( elem->GetType() )];
508 if ( !oldElem || oldElem->GetID() > elem->GetID() )