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)
120 MESSAGE("-----------------> Remove Null Element " << isElemDeleted);
123 if (!IsComplexSubmesh())
125 if ( ME->getshapeId() != myIndex )
127 int idInSubShape = ME->getIdInShape();
128 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
130 elem->setIdInShape(-1);
131 if ((idInSubShape >= 0) && (idInSubShape < (int) myElements.size()))
133 myElements[idInSubShape] = 0; // this vector entry is no more used
134 if ( ++myUnusedIdElements == (int) myElements.size() )
136 clearVector( myElements );
137 myUnusedIdElements = 0;
143 MESSAGE("Try to remove an element from a complex submesh ");
147 //=======================================================================
150 //=======================================================================
152 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
154 if ( !IsComplexSubmesh() )
156 const int idInSubShape = N->getIdInShape();
157 const int shapeId = N->getshapeId();
158 if ((shapeId > 0) && (idInSubShape >= 0))
160 if ( shapeId != myIndex )
161 throw SALOME_Exception
162 (LOCALIZED("a node being in sub-mesh is added to another sub-mesh"));
163 if ( idInSubShape >= (int)myNodes.size() || myNodes[ idInSubShape ] != N )
164 throw SALOME_Exception
165 (LOCALIZED("a node with wrong idInSubShape is re-added to the same sub-mesh"));
166 return; // already in
168 SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
169 node->setShapeId(myIndex);
170 node->setIdInShape(myNodes.size());
171 myNodes.push_back(N);
175 //=======================================================================
176 //function : RemoveNode
178 //=======================================================================
180 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
182 if (!IsComplexSubmesh())
184 if ( N->getshapeId() != myIndex )
186 int idInSubShape = N->getIdInShape();
187 SMDS_MeshNode* node = (SMDS_MeshNode*) (N);
189 node->setIdInShape(-1);
190 if ((idInSubShape >= 0) && (idInSubShape < (int) myNodes.size()))
192 myNodes[idInSubShape] = 0; // this vector entry is no more used
193 if ( ++myUnusedIdNodes == (int) myNodes.size() )
195 clearVector( myNodes );
202 MESSAGE("Try to remove a node from a complex submesh");
206 //=======================================================================
207 //function : NbElements
209 //=======================================================================
211 int SMESHDS_SubMesh::NbElements() const
213 if ( !IsComplexSubmesh() )
214 return myElements.size() - myUnusedIdElements;
217 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
218 for ( ; it != mySubMeshes.end(); it++ )
219 nbElems += (*it)->NbElements();
224 //=======================================================================
227 //=======================================================================
229 int SMESHDS_SubMesh::NbNodes() const
231 if ( !IsComplexSubmesh() )
232 return myNodes.size() - myUnusedIdNodes;
235 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
236 for ( ; it != mySubMeshes.end(); it++ )
237 nbElems += (*it)->NbNodes();
243 * template class used for iteration on submesh elements. Interface of iterator remains
244 * unchanged after redesign of SMDS to avoid modification everywhere in SMESH.
245 * instances are stored in shared_ptr for automatic destruction.
246 * Container is copied for iteration, because original can be modified
247 * by addition of elements, for instance, and then reallocated (vector)
249 template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
252 typename TSET::const_iterator _it, _end;
255 MySetIterator(const TSET& table)
258 _it = _table.begin();
260 while ((_it != _end) && (*_it == 0))
266 while ((_it != _end) && (*_it == 0))
268 return (_it != _end);
279 // =====================
281 // =====================
283 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
286 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
287 : myMore(false), mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() )
291 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
293 myElemIt = getElements(*mySubIt);
296 myMore = myElemIt.get() && myElemIt->more();
303 elem = myElemIt->next();
307 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
308 getElements(const SMESHDS_SubMesh*) const = 0;
312 set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
313 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
316 // =====================
317 // class MyElemIterator
318 // =====================
320 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
323 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
324 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
325 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
326 { return theSubMesh->GetElements(); }
329 // =====================
330 // class MyNodeIterator
331 // =====================
333 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
336 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
337 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
338 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
339 { return theSubMesh->GetNodes(); }
342 //=======================================================================
343 //function : GetElements
345 //=======================================================================
347 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
349 if ( IsComplexSubmesh() )
350 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
351 return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> >(myElements));
354 //=======================================================================
355 //function : GetNodes
357 //=======================================================================
359 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
361 if ( IsComplexSubmesh() )
362 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
364 return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> >(myNodes));
367 //=======================================================================
368 //function : Contains
369 //purpose : check if elem or node is in
370 //=======================================================================
372 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
374 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
375 //if ( IsComplexSubmesh() || !ME )
379 if ( IsComplexSubmesh() )
381 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
382 for (; aSubIt != mySubMeshes.end(); aSubIt++)
383 if ((*aSubIt)->Contains(ME))
388 if (ME->GetType() == SMDSAbs_Node)
390 int idInShape = ME->getIdInShape();
391 if ((idInShape >= 0) && (idInShape < (int) myNodes.size()))
392 if (myNodes[idInShape] == ME)
397 int idInShape = ME->getIdInShape();
398 if ((idInShape >= 0) && (idInShape < (int) myElements.size()))
399 if (myElements[idInShape] == ME)
405 //=======================================================================
406 //function : IsQuadratic
407 //purpose : Return true if my 1st element is quadratic
408 //=======================================================================
410 bool SMESHDS_SubMesh::IsQuadratic() const
412 if ( IsComplexSubmesh() )
414 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
415 for (; aSubIt != mySubMeshes.end(); aSubIt++)
416 if ((*aSubIt)->IsQuadratic())
421 for ( size_t i = 0; i < myElements.size(); ++i )
423 return myElements[i]->IsQuadratic();
428 //=======================================================================
429 //function : AddSubMesh
431 //=======================================================================
433 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
435 ASSERT( theSubMesh );
436 mySubMeshes.insert( theSubMesh );
439 //=======================================================================
440 //function : RemoveSubMesh
442 //=======================================================================
444 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
446 return mySubMeshes.erase( theSubMesh );
449 //=======================================================================
450 //function : RemoveAllSubmeshes
452 //=======================================================================
454 void SMESHDS_SubMesh::RemoveAllSubmeshes()
459 //=======================================================================
460 //function : ContainsSubMesh
462 //=======================================================================
464 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
466 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
469 //=======================================================================
470 //function : GetSubMeshIterator
472 //=======================================================================
474 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
476 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
477 return SMESHDS_SubMeshIteratorPtr
478 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
482 //=======================================================================
484 //purpose : remove the contents
485 //=======================================================================
487 void SMESHDS_SubMesh::Clear()
489 clearVector( myElements );
490 clearVector( myNodes );
492 myUnusedIdElements = 0;
493 if ( NbSubMeshes() > 0 )
495 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
496 while ( sub->more() ) {
497 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
503 int SMESHDS_SubMesh::getSize()
506 int d = NbElements();
510 void SMESHDS_SubMesh::compactList()
512 if ( myUnusedIdElements > 0 )
514 std::vector<const SMDS_MeshElement*> newElems;
515 newElems.reserve( myElements.size() - myUnusedIdElements );
516 for (size_t i = 0; i < myElements.size(); i++)
519 SMDS_MeshElement* elem = (SMDS_MeshElement*)myElements[i];
520 elem->setIdInShape(newElems.size());
521 newElems.push_back(elem);
523 myElements.swap(newElems);
524 myUnusedIdElements = 0;
527 if ( myUnusedIdNodes > 0 )
529 std::vector<const SMDS_MeshNode*> newNodes;
530 newNodes.reserve( myNodes.size() - myUnusedIdNodes );
531 for (size_t i = 0; i < myNodes.size(); i++)
534 SMDS_MeshNode* node = (SMDS_MeshNode*)myNodes[i];
535 node->setIdInShape(newNodes.size());
536 newNodes.push_back(node);
538 myNodes.swap(newNodes);
543 //=======================================================================
544 //function : GetElement
545 //purpose : Return an element by its IdInShape
546 //=======================================================================
548 const SMDS_MeshElement* SMESHDS_SubMesh::GetElement( size_t idInShape ) const
550 return ( !IsComplexSubmesh() && idInShape < myElements.size() ) ? myElements[idInShape] : 0;
553 //=======================================================================
554 //function : GetElement
555 //purpose : Return a node by its IdInShape
556 //=======================================================================
558 const SMDS_MeshNode* SMESHDS_SubMesh::GetNode( size_t idInShape ) const
560 return ( !IsComplexSubmesh() && idInShape < myNodes.size() ) ? myNodes[idInShape] : 0;