1 // Copyright (C) 2007-2010 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.
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"
39 SMESHDS_SubMesh::SMESHDS_SubMesh(SMESHDS_Mesh *parent, int index)
46 myUnusedIdElements = 0;
49 //=======================================================================
50 //function : AddElement
52 //=======================================================================
53 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
55 if (!IsComplexSubmesh())
57 //MESSAGE("in " << myIndex << " AddElement "<< ME->GetID());
58 int idInSubShape = ME->getIdInShape();
59 if (idInSubShape != -1)
61 MESSAGE("add element in subshape already belonging to a subshape "
62 << ME->GetID() << " " << ME->getIdInShape() << " " << ME->getshapeId());
63 throw SALOME_Exception(LOCALIZED("add element in subshape already belonging to a subshape"));
65 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
66 elem->setShapeId(myIndex);
67 elem->setIdInShape(myElements.size());
68 myElements.push_back(ME);
72 //=======================================================================
73 //function : RemoveElement
75 //=======================================================================
76 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
80 MESSAGE("-----------------> Remove Null Element " << isElemDeleted);
83 //MESSAGE("-----------------> RemoveElement "<< ME->GetID() << " " << isElemDeleted);
84 if (!IsComplexSubmesh())
86 // if (!isElemDeleted) // alive element has valid ID and can be found
88 int idInSubShape = ME->getIdInShape();
89 //MESSAGE("in "<< myIndex << " RemoveElement " << ME->GetID() << " " << idInSubShape << " " << myUnusedIdElements);
90 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
92 elem->setIdInShape(-1);
93 if ((idInSubShape >= 0) && (idInSubShape < myElements.size()))
95 myElements[idInSubShape] = 0; // this vector entry is no more used
102 MESSAGE("Try to remove an element from a complex submesh ");
106 //=======================================================================
109 //=======================================================================
110 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
112 if ( !IsComplexSubmesh() )
114 int idInSubShape = N->getIdInShape();
115 int shapeId = N->getshapeId();
116 if ((shapeId > 0) && (idInSubShape >= 0))
118 MESSAGE("========== AddNode already belonging to other subShape " << N->GetID());
119 // OK for vertex nodes
120 //this->getParent()->UnSetNodeOnShape(N);
122 SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
123 node->setShapeId(myIndex);
124 node->setIdInShape(myNodes.size());
125 myNodes.push_back(N);
126 //MESSAGE("in "<< myIndex << " AddNode " << node->GetID());
128 //MESSAGE("try to add node in a complex submesh " << N->GetID());
131 //=======================================================================
132 //function : RemoveNode
134 //=======================================================================
136 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
138 if (!IsComplexSubmesh())
140 // if (!isNodeDeleted) // alive node has valid ID and can be found
142 int idInSubShape = N->getIdInShape();
143 //int shapeId = N->getshapeId();
144 //MESSAGE("in "<< myIndex << " RemoveNode " << shapeId << " " << idInSubShape << " " << N->GetID());
145 SMDS_MeshNode* node = (SMDS_MeshNode*) (N);
147 node->setIdInShape(-1);
148 if ((idInSubShape >= 0) && (idInSubShape < myNodes.size()))
150 myNodes[idInSubShape] = 0; // this vector entry is no more used
157 MESSAGE("Try to remove a node from a complex submesh");
161 //=======================================================================
162 //function : NbElements
164 //=======================================================================
165 int SMESHDS_SubMesh::NbElements() const
167 //MESSAGE(this << " NbElements " << IsComplexSubmesh() << " " << myElements.size() - myUnusedIdElements);
168 if ( !IsComplexSubmesh() )
169 return myElements.size() - myUnusedIdElements;
172 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
173 for ( ; it != mySubMeshes.end(); it++ )
174 nbElems += (*it)->NbElements();
179 //=======================================================================
182 //=======================================================================
184 int SMESHDS_SubMesh::NbNodes() const
186 //MESSAGE(this << " NbNodes " << IsComplexSubmesh() << " " << myNodes.size() - myUnusedIdNodes);
187 if ( !IsComplexSubmesh() )
188 return myNodes.size() - myUnusedIdNodes;
191 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
192 for ( ; it != mySubMeshes.end(); it++ )
193 nbElems += (*it)->NbNodes();
199 * template class used for iteration on submesh elements. Interface of iterator remains
200 * unchanged after redesign of SMDS to avoid modification everywhere in SMESH.
201 * instances are stored in shared_ptr for automatic destruction.
202 * Container is copied for iteration, because original can be modified
203 * by addition of elements, for instance, and then reallocated (vector)
205 template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
208 typename TSET::const_iterator _it, _end;
211 MySetIterator(const TSET& table)
214 _it = _table.begin();
216 while ((_it != _end) && (*_it == 0))
222 while ((_it != _end) && (*_it == 0))
224 return (_it != _end);
235 // =====================
237 // =====================
239 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
242 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
243 : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
247 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
249 myElemIt = getElements(*mySubIt);
252 myMore = myElemIt.get() && myElemIt->more();
259 elem = myElemIt->next();
263 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
264 getElements(const SMESHDS_SubMesh*) const = 0;
268 set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
269 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
272 // =====================
273 // class MyElemIterator
274 // =====================
276 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
279 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
280 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
281 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
282 { return theSubMesh->GetElements(); }
285 // =====================
286 // class MyNodeIterator
287 // =====================
289 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
292 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
293 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
294 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
295 { return theSubMesh->GetNodes(); }
298 //=======================================================================
299 //function : GetElements
301 //=======================================================================
303 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
305 if ( IsComplexSubmesh() )
306 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
307 return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> >(myElements));
310 //=======================================================================
311 //function : GetNodes
313 //=======================================================================
315 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
317 if ( IsComplexSubmesh() )
318 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
320 return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> >(myNodes));
323 //=======================================================================
324 //function : Contains
325 //purpose : check if elem or node is in
326 //=======================================================================
328 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
330 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
331 //if ( IsComplexSubmesh() || !ME )
335 if (IsComplexSubmesh())
337 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
338 for (; aSubIt != mySubMeshes.end(); aSubIt++)
339 if ((*aSubIt)->Contains(ME))
344 if (ME->GetType() == SMDSAbs_Node)
346 int idInShape = ME->getIdInShape();
347 if ((idInShape >= 0) && (idInShape < myNodes.size()))
348 if (myNodes[idInShape] == ME)
353 int idInShape = ME->getIdInShape();
354 if ((idInShape >= 0) && (idInShape < myElements.size()))
355 if (myElements[idInShape] == ME)
361 //=======================================================================
362 //function : AddSubMesh
364 //=======================================================================
366 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
368 ASSERT( theSubMesh );
369 mySubMeshes.insert( theSubMesh );
372 //=======================================================================
373 //function : RemoveSubMesh
375 //=======================================================================
377 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
379 return mySubMeshes.erase( theSubMesh );
382 //=======================================================================
383 //function : ContainsSubMesh
385 //=======================================================================
387 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
389 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
392 //=======================================================================
393 //function : GetSubMeshIterator
395 //=======================================================================
397 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
399 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
400 return SMESHDS_SubMeshIteratorPtr
401 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
405 //=======================================================================
407 //purpose : remove the contents
408 //=======================================================================
410 void SMESHDS_SubMesh::Clear()
415 myUnusedIdElements = 0;
416 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
417 while ( sub->more() ) {
418 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
423 int SMESHDS_SubMesh::getSize()
426 int d = NbElements();
427 //cerr << "SMESHDS_SubMesh::NbNodes " << c << endl;
428 //cerr << "SMESHDS_SubMesh::NbElements " << d << endl;
432 void SMESHDS_SubMesh::compactList()
434 //MESSAGE("compactList old: nodes " << myNodes.size() << " elements " << myElements.size());
440 std::vector<const SMDS_MeshElement*> newElems;
442 for (int i = 0; i < myElements.size(); i++)
445 SMDS_MeshElement* elem = (SMDS_MeshElement*)myElements[i];
446 elem->setIdInShape(newElems.size());
447 newElems.push_back(elem);
448 //a << elem->GetID() << " ";
449 //b << elem->GetID() << " ";
453 myElements.swap(newElems);
454 myUnusedIdElements = 0;
455 //MESSAGE("in " << myIndex << " oldElems " << a.str());
456 //MESSAGE("in " << myIndex << " newElems " << b.str());
458 std::vector<const SMDS_MeshNode*> newNodes;
460 for (int i = 0; i < myNodes.size(); i++)
463 SMDS_MeshNode* node = (SMDS_MeshNode*)myNodes[i];
464 node->setIdInShape(newNodes.size());
465 newNodes.push_back(node);
466 //c << node->GetID() << " ";
467 //d << node->GetID() << " ";
471 myNodes.swap(newNodes);
473 //MESSAGE("in " << myIndex << " oldNodes " << c.str());
474 //MESSAGE("in " << myIndex << " newNodes " << d.str());
475 //MESSAGE("compactList new: nodes " << myNodes.size() << " elements " << myElements.size());