1 // Copyright (C) 2007-2014 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)
53 myUnusedIdElements = 0;
56 //================================================================================
60 //================================================================================
62 SMESHDS_SubMesh::~SMESHDS_SubMesh()
66 //=======================================================================
67 //function : AddElement
69 //=======================================================================
71 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
73 if (!IsComplexSubmesh())
75 if ( ME->GetType() == SMDSAbs_Node )
77 AddNode( static_cast< const SMDS_MeshNode* >( ME ));
80 int oldShapeId = ME->getshapeId();
83 if (oldShapeId != myIndex)
85 MESSAGE("add element in subshape already belonging to another subshape "
86 << ME->GetID() << " " << oldShapeId << " " << myIndex);
87 throw SALOME_Exception(LOCALIZED("add element in subshape already belonging to a subshape"));
91 int idInSubShape = ME->getIdInShape();
92 if (idInSubShape >= 0)
94 MESSAGE("add element in subshape already belonging to that subshape "
95 << ME->GetID() << " " << oldShapeId << " " << idInSubShape);
96 // check if ok: do nothing if ok
97 if (idInSubShape >= myElements.size())
99 MESSAGE("out of bounds " << idInSubShape << " " << myElements.size());
100 throw SALOME_Exception(LOCALIZED("out of bounds"));
102 if (ME != myElements[idInSubShape])
104 MESSAGE("not the same element");
105 throw SALOME_Exception(LOCALIZED("not the same element"));
107 MESSAGE("already done, OK, nothing to do");
113 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
114 elem->setShapeId(myIndex);
115 elem->setIdInShape(myElements.size());
116 myElements.push_back(ME);
120 //=======================================================================
121 //function : RemoveElement
123 //=======================================================================
125 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
129 MESSAGE("-----------------> Remove Null Element " << isElemDeleted);
132 if (!IsComplexSubmesh())
134 if ( ME->getshapeId() != myIndex )
136 int idInSubShape = ME->getIdInShape();
137 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
139 elem->setIdInShape(-1);
140 if ((idInSubShape >= 0) && (idInSubShape < 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;
152 MESSAGE("Try to remove an element from a complex submesh ");
156 //=======================================================================
159 //=======================================================================
161 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
163 if ( !IsComplexSubmesh() )
165 const int idInSubShape = N->getIdInShape();
166 const int shapeId = N->getshapeId();
167 if ((shapeId > 0) && (idInSubShape >= 0))
169 if ( shapeId != myIndex )
170 throw SALOME_Exception
171 (LOCALIZED("a node being in sub-mesh is added to another sub-mesh"));
172 if ( idInSubShape >= myNodes.size() || myNodes[ idInSubShape ] != N )
173 throw SALOME_Exception
174 (LOCALIZED("a node with wrong idInSubShape is re-added to the same sub-mesh"));
175 return; // already in
177 SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
178 node->setShapeId(myIndex);
179 node->setIdInShape(myNodes.size());
180 myNodes.push_back(N);
184 //=======================================================================
185 //function : RemoveNode
187 //=======================================================================
189 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
191 if (!IsComplexSubmesh())
193 if ( N->getshapeId() != myIndex )
195 int idInSubShape = N->getIdInShape();
196 SMDS_MeshNode* node = (SMDS_MeshNode*) (N);
198 node->setIdInShape(-1);
199 if ((idInSubShape >= 0) && (idInSubShape < myNodes.size()))
201 myNodes[idInSubShape] = 0; // this vector entry is no more used
202 if ( ++myUnusedIdNodes == (int) myNodes.size() )
204 clearVector( myNodes );
211 MESSAGE("Try to remove a node from a complex submesh");
215 //=======================================================================
216 //function : NbElements
218 //=======================================================================
220 int SMESHDS_SubMesh::NbElements() const
222 if ( !IsComplexSubmesh() )
223 return myElements.size() - myUnusedIdElements;
226 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
227 for ( ; it != mySubMeshes.end(); it++ )
228 nbElems += (*it)->NbElements();
233 //=======================================================================
236 //=======================================================================
238 int SMESHDS_SubMesh::NbNodes() const
240 if ( !IsComplexSubmesh() )
241 return myNodes.size() - myUnusedIdNodes;
244 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
245 for ( ; it != mySubMeshes.end(); it++ )
246 nbElems += (*it)->NbNodes();
252 * template class used for iteration on submesh elements. Interface of iterator remains
253 * unchanged after redesign of SMDS to avoid modification everywhere in SMESH.
254 * instances are stored in shared_ptr for automatic destruction.
255 * Container is copied for iteration, because original can be modified
256 * by addition of elements, for instance, and then reallocated (vector)
258 template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
261 typename TSET::const_iterator _it, _end;
264 MySetIterator(const TSET& table)
267 _it = _table.begin();
269 while ((_it != _end) && (*_it == 0))
275 while ((_it != _end) && (*_it == 0))
277 return (_it != _end);
288 // =====================
290 // =====================
292 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
295 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
296 : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
300 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
302 myElemIt = getElements(*mySubIt);
305 myMore = myElemIt.get() && myElemIt->more();
312 elem = myElemIt->next();
316 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
317 getElements(const SMESHDS_SubMesh*) const = 0;
321 set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
322 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
325 // =====================
326 // class MyElemIterator
327 // =====================
329 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
332 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
333 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
334 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
335 { return theSubMesh->GetElements(); }
338 // =====================
339 // class MyNodeIterator
340 // =====================
342 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
345 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
346 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
347 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
348 { return theSubMesh->GetNodes(); }
351 //=======================================================================
352 //function : GetElements
354 //=======================================================================
356 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
358 if ( IsComplexSubmesh() )
359 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
360 return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> >(myElements));
363 //=======================================================================
364 //function : GetNodes
366 //=======================================================================
368 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
370 if ( IsComplexSubmesh() )
371 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
373 return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> >(myNodes));
376 //=======================================================================
377 //function : Contains
378 //purpose : check if elem or node is in
379 //=======================================================================
381 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
383 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
384 //if ( IsComplexSubmesh() || !ME )
388 if (IsComplexSubmesh())
390 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
391 for (; aSubIt != mySubMeshes.end(); aSubIt++)
392 if ((*aSubIt)->Contains(ME))
397 if (ME->GetType() == SMDSAbs_Node)
399 int idInShape = ME->getIdInShape();
400 if ((idInShape >= 0) && (idInShape < myNodes.size()))
401 if (myNodes[idInShape] == ME)
406 int idInShape = ME->getIdInShape();
407 if ((idInShape >= 0) && (idInShape < myElements.size()))
408 if (myElements[idInShape] == ME)
414 //=======================================================================
415 //function : AddSubMesh
417 //=======================================================================
419 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
421 ASSERT( theSubMesh );
422 mySubMeshes.insert( theSubMesh );
425 //=======================================================================
426 //function : RemoveSubMesh
428 //=======================================================================
430 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
432 return mySubMeshes.erase( theSubMesh );
435 //=======================================================================
436 //function : RemoveAllSubmeshes
438 //=======================================================================
440 void SMESHDS_SubMesh::RemoveAllSubmeshes()
445 //=======================================================================
446 //function : ContainsSubMesh
448 //=======================================================================
450 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
452 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
455 //=======================================================================
456 //function : GetSubMeshIterator
458 //=======================================================================
460 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
462 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
463 return SMESHDS_SubMeshIteratorPtr
464 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
468 //=======================================================================
470 //purpose : remove the contents
471 //=======================================================================
473 void SMESHDS_SubMesh::Clear()
475 clearVector( myElements );
476 clearVector( myNodes );
478 myUnusedIdElements = 0;
479 if ( NbSubMeshes() > 0 )
481 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
482 while ( sub->more() ) {
483 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
489 int SMESHDS_SubMesh::getSize()
492 int d = NbElements();
496 void SMESHDS_SubMesh::compactList()
498 if ( myUnusedIdElements > 0 )
500 std::vector<const SMDS_MeshElement*> newElems;
501 newElems.reserve( myElements.size() - myUnusedIdElements );
502 for (size_t i = 0; i < myElements.size(); i++)
505 SMDS_MeshElement* elem = (SMDS_MeshElement*)myElements[i];
506 elem->setIdInShape(newElems.size());
507 newElems.push_back(elem);
509 myElements.swap(newElems);
510 myUnusedIdElements = 0;
513 if ( myUnusedIdNodes > 0 )
515 std::vector<const SMDS_MeshNode*> newNodes;
516 newNodes.reserve( myNodes.size() - myUnusedIdNodes );
517 for (size_t i = 0; i < myNodes.size(); i++)
520 SMDS_MeshNode* node = (SMDS_MeshNode*)myNodes[i];
521 node->setIdInShape(newNodes.size());
522 newNodes.push_back(node);
524 myNodes.swap(newNodes);