1 // Copyright (C) 2007-2015 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"
30 #include "SMESHDS_Mesh.hxx"
32 #include "utilities.h"
33 #include "SMDS_SetIterator.hxx"
40 //================================================================================
44 //================================================================================
46 SMESHDS_SubMesh::SMESHDS_SubMesh(SMESHDS_Mesh *parent, int index)
51 myUnusedIdElements = 0;
54 //================================================================================
58 //================================================================================
60 SMESHDS_SubMesh::~SMESHDS_SubMesh()
64 //=======================================================================
65 //function : AddElement
67 //=======================================================================
69 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
71 if (!IsComplexSubmesh())
73 if ( ME->GetType() == SMDSAbs_Node )
75 AddNode( static_cast< const SMDS_MeshNode* >( ME ));
78 int oldShapeId = ME->getshapeId();
81 if (oldShapeId != myIndex)
83 throw SALOME_Exception
84 (LOCALIZED("add element in subshape already belonging to a subshape"));
86 int idInSubShape = ME->getIdInShape();
87 if (idInSubShape >= 0)
89 MESSAGE("add element in subshape already belonging to that subshape "
90 << ME->GetID() << " " << oldShapeId << " " << idInSubShape);
91 // check if ok: do nothing if ok
92 if (idInSubShape >= (int)myElements.size())
94 throw SALOME_Exception(LOCALIZED("out of bounds"));
96 if (ME != myElements[idInSubShape])
98 throw SALOME_Exception(LOCALIZED("not the same element"));
104 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
105 elem->setShapeId(myIndex);
106 elem->setIdInShape(myElements.size());
107 myElements.push_back(ME);
111 //=======================================================================
112 //function : RemoveElement
114 //=======================================================================
116 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
122 if (!IsComplexSubmesh())
124 if ( ME->getshapeId() != myIndex ) // elem not in a pool can loose it's data already
127 for ( size_t i = 0; i < myElements.size(); ++i )
128 if ( myElements[i] == ME )
131 ++myUnusedIdElements;
136 int idInSubShape = ME->getIdInShape();
137 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
139 elem->setIdInShape(-1);
140 if ((idInSubShape >= 0) && (idInSubShape < (int) myElements.size()))
142 myElements[idInSubShape] = 0; // this vector entry is no more used
143 if ( ++myUnusedIdElements == (int) myElements.size() )
145 clearVector( myElements );
146 myUnusedIdElements = 0;
155 //=======================================================================
158 //=======================================================================
160 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
162 if ( !IsComplexSubmesh() )
164 const int idInSubShape = N->getIdInShape();
165 const int shapeId = N->getshapeId();
166 if ((shapeId > 0) && (idInSubShape >= 0))
168 if ( shapeId != myIndex )
169 throw SALOME_Exception
170 (LOCALIZED("a node being in sub-mesh is added to another sub-mesh"));
171 if ( idInSubShape >= (int)myNodes.size() || myNodes[ idInSubShape ] != N )
172 throw SALOME_Exception
173 (LOCALIZED("a node with wrong idInSubShape is re-added to the same sub-mesh"));
174 return; // already in
176 SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
177 node->setShapeId(myIndex);
178 node->setIdInShape(myNodes.size());
179 myNodes.push_back(N);
183 //=======================================================================
184 //function : RemoveNode
186 //=======================================================================
188 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
190 if (!IsComplexSubmesh())
192 if ( N->getshapeId() != myIndex )
195 for ( size_t i = 0; i < myNodes.size(); ++i )
196 if ( myNodes[i] == N )
204 int idInSubShape = N->getIdInShape();
205 SMDS_MeshNode* node = (SMDS_MeshNode*) (N);
207 node->setIdInShape(-1);
208 if ((idInSubShape >= 0) && (idInSubShape < (int) myNodes.size()))
210 myNodes[idInSubShape] = 0; // this vector entry is no more used
211 if ( ++myUnusedIdNodes == (int) myNodes.size() )
213 clearVector( myNodes );
223 //=======================================================================
224 //function : NbElements
226 //=======================================================================
228 int SMESHDS_SubMesh::NbElements() const
230 if ( !IsComplexSubmesh() )
231 return myElements.size() - myUnusedIdElements;
234 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
235 for ( ; it != mySubMeshes.end(); it++ )
236 nbElems += (*it)->NbElements();
241 //=======================================================================
244 //=======================================================================
246 int SMESHDS_SubMesh::NbNodes() const
248 if ( !IsComplexSubmesh() )
249 return myNodes.size() - myUnusedIdNodes;
252 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
253 for ( ; it != mySubMeshes.end(); it++ )
254 nbElems += (*it)->NbNodes();
260 * template class used for iteration on submesh elements. Interface of iterator remains
261 * unchanged after redesign of SMDS to avoid modification everywhere in SMESH.
262 * instances are stored in shared_ptr for automatic destruction.
263 * Container is copied for iteration, because original can be modified
264 * by addition of elements, for instance, and then reallocated (vector)
266 template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
269 typename TSET::const_iterator _it, _end;
272 MySetIterator(const TSET& table)
275 _it = _table.begin();
277 while ((_it != _end) && (*_it == 0))
283 while ((_it != _end) && (*_it == 0))
285 return (_it != _end);
296 // =====================
298 // =====================
300 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
303 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
304 : myMore(false), mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() )
308 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
310 myElemIt = getElements(*mySubIt);
313 myMore = myElemIt.get() && myElemIt->more();
320 elem = myElemIt->next();
324 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
325 getElements(const SMESHDS_SubMesh*) const = 0;
329 set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
330 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
333 // =====================
334 // class MyElemIterator
335 // =====================
337 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
340 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
341 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
342 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
343 { return theSubMesh->GetElements(); }
346 // =====================
347 // class MyNodeIterator
348 // =====================
350 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
353 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
354 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
355 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
356 { return theSubMesh->GetNodes(); }
359 //=======================================================================
360 //function : GetElements
362 //=======================================================================
364 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
366 if ( IsComplexSubmesh() )
367 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
368 return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> >(myElements));
371 //=======================================================================
372 //function : GetNodes
374 //=======================================================================
376 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
378 if ( IsComplexSubmesh() )
379 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
381 return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> >(myNodes));
384 //=======================================================================
385 //function : Contains
386 //purpose : check if elem or node is in
387 //=======================================================================
389 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
391 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
392 //if ( IsComplexSubmesh() || !ME )
396 if ( IsComplexSubmesh() )
398 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
399 for (; aSubIt != mySubMeshes.end(); aSubIt++)
400 if ((*aSubIt)->Contains(ME))
405 if (ME->GetType() == SMDSAbs_Node)
407 int idInShape = ME->getIdInShape();
408 if ((idInShape >= 0) && (idInShape < (int) myNodes.size()))
409 if (myNodes[idInShape] == ME)
414 int idInShape = ME->getIdInShape();
415 if ((idInShape >= 0) && (idInShape < (int) myElements.size()))
416 if (myElements[idInShape] == ME)
422 //=======================================================================
423 //function : IsQuadratic
424 //purpose : Return true if my 1st element is quadratic
425 //=======================================================================
427 bool SMESHDS_SubMesh::IsQuadratic() const
429 if ( IsComplexSubmesh() )
431 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
432 for (; aSubIt != mySubMeshes.end(); aSubIt++)
433 if ((*aSubIt)->IsQuadratic())
438 for ( size_t i = 0; i < myElements.size(); ++i )
440 return myElements[i]->IsQuadratic();
445 //=======================================================================
446 //function : AddSubMesh
448 //=======================================================================
450 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
452 ASSERT( theSubMesh );
453 mySubMeshes.insert( theSubMesh );
456 //=======================================================================
457 //function : RemoveSubMesh
459 //=======================================================================
461 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
463 return mySubMeshes.erase( theSubMesh );
466 //=======================================================================
467 //function : RemoveAllSubmeshes
469 //=======================================================================
471 void SMESHDS_SubMesh::RemoveAllSubmeshes()
476 //=======================================================================
477 //function : ContainsSubMesh
479 //=======================================================================
481 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
483 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
486 //=======================================================================
487 //function : GetSubMeshIterator
489 //=======================================================================
491 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
493 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
494 return SMESHDS_SubMeshIteratorPtr
495 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
499 //=======================================================================
501 //purpose : remove the contents
502 //=======================================================================
504 void SMESHDS_SubMesh::Clear()
506 clearVector( myElements );
507 clearVector( myNodes );
509 myUnusedIdElements = 0;
510 if ( NbSubMeshes() > 0 )
512 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
513 while ( sub->more() ) {
514 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
520 int SMESHDS_SubMesh::getSize()
523 int d = NbElements();
527 void SMESHDS_SubMesh::compactList()
529 if ( myUnusedIdElements > 0 )
531 std::vector<const SMDS_MeshElement*> newElems;
532 newElems.reserve( myElements.size() - myUnusedIdElements );
533 for (size_t i = 0; i < myElements.size(); i++)
536 SMDS_MeshElement* elem = (SMDS_MeshElement*)myElements[i];
537 elem->setIdInShape(newElems.size());
538 newElems.push_back(elem);
540 myElements.swap(newElems);
541 myUnusedIdElements = 0;
544 if ( myUnusedIdNodes > 0 )
546 std::vector<const SMDS_MeshNode*> newNodes;
547 newNodes.reserve( myNodes.size() - myUnusedIdNodes );
548 for (size_t i = 0; i < myNodes.size(); i++)
551 SMDS_MeshNode* node = (SMDS_MeshNode*)myNodes[i];
552 node->setIdInShape(newNodes.size());
553 newNodes.push_back(node);
555 myNodes.swap(newNodes);
560 //=======================================================================
561 //function : GetElement
562 //purpose : Return an element by its IdInShape
563 //=======================================================================
565 const SMDS_MeshElement* SMESHDS_SubMesh::GetElement( size_t idInShape ) const
567 return ( !IsComplexSubmesh() && idInShape < myElements.size() ) ? myElements[idInShape] : 0;
570 //=======================================================================
571 //function : GetElement
572 //purpose : Return a node by its IdInShape
573 //=======================================================================
575 const SMDS_MeshNode* SMESHDS_SubMesh::GetNode( size_t idInShape ) const
577 return ( !IsComplexSubmesh() && idInShape < myNodes.size() ) ? myNodes[idInShape] : 0;