1 // Copyright (C) 2007-2011 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"
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 //=======================================================================
70 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
72 if (!IsComplexSubmesh())
74 //MESSAGE("in " << myIndex << " AddElement "<< ME->GetID());
75 int oldShapeId = ME->getshapeId();
78 if (oldShapeId != myIndex)
80 MESSAGE("add element in subshape already belonging to another subshape "
81 << ME->GetID() << " " << oldShapeId << " " << myIndex);
82 throw SALOME_Exception(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 >= myElements.size())
94 MESSAGE("out of bounds " << idInSubShape << " " << myElements.size());
95 throw SALOME_Exception(LOCALIZED("out of bounds"));
97 if (ME != myElements[idInSubShape])
99 MESSAGE("not the same element");
100 throw SALOME_Exception(LOCALIZED("not the same element"));
102 MESSAGE("already done, OK, nothing to do");
108 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
109 elem->setShapeId(myIndex);
110 elem->setIdInShape(myElements.size());
111 myElements.push_back(ME);
115 //=======================================================================
116 //function : RemoveElement
118 //=======================================================================
119 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
123 MESSAGE("-----------------> Remove Null Element " << isElemDeleted);
126 //MESSAGE("-----------------> RemoveElement "<< ME->GetID() << " " << isElemDeleted);
127 if (!IsComplexSubmesh())
129 if ( ME->getshapeId() != myIndex )
131 int idInSubShape = ME->getIdInShape();
132 //MESSAGE("in "<< myIndex << " RemoveElement " << ME->GetID() << " " << idInSubShape << " " << myUnusedIdElements);
133 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
135 elem->setIdInShape(-1);
136 if ((idInSubShape >= 0) && (idInSubShape < myElements.size()))
138 myElements[idInSubShape] = 0; // this vector entry is no more used
139 myUnusedIdElements++;
145 MESSAGE("Try to remove an element from a complex submesh ");
149 //=======================================================================
152 //=======================================================================
153 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
155 if ( !IsComplexSubmesh() )
157 int idInSubShape = N->getIdInShape();
158 int shapeId = N->getshapeId();
159 if ((shapeId > 0) && (idInSubShape >= 0))
161 MESSAGE("========== AddNode already belonging to other subShape " << N->GetID());
162 // OK for vertex nodes
163 //this->getParent()->UnSetNodeOnShape(N);
165 SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
166 node->setShapeId(myIndex);
167 node->setIdInShape(myNodes.size());
168 myNodes.push_back(N);
169 //MESSAGE("in "<< myIndex << " AddNode " << node->GetID());
171 //MESSAGE("try to add node in a complex submesh " << N->GetID());
174 //=======================================================================
175 //function : RemoveNode
177 //=======================================================================
179 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
181 if (!IsComplexSubmesh())
183 // if (!isNodeDeleted) // alive node has valid ID and can be found
185 if ( N->getshapeId() != myIndex )
187 int idInSubShape = N->getIdInShape();
188 //int shapeId = N->getshapeId();
189 //MESSAGE("in "<< myIndex << " RemoveNode " << shapeId << " " << idInSubShape << " " << N->GetID());
190 SMDS_MeshNode* node = (SMDS_MeshNode*) (N);
192 node->setIdInShape(-1);
193 if ((idInSubShape >= 0) && (idInSubShape < myNodes.size()))
195 myNodes[idInSubShape] = 0; // this vector entry is no more used
202 MESSAGE("Try to remove a node from a complex submesh");
206 //=======================================================================
207 //function : NbElements
209 //=======================================================================
210 int SMESHDS_SubMesh::NbElements() const
212 //MESSAGE(this << " NbElements " << IsComplexSubmesh() << " " << myElements.size() - myUnusedIdElements);
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 //MESSAGE(this << " NbNodes " << IsComplexSubmesh() << " " << myNodes.size() - myUnusedIdNodes);
232 if ( !IsComplexSubmesh() )
233 return myNodes.size() - myUnusedIdNodes;
236 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
237 for ( ; it != mySubMeshes.end(); it++ )
238 nbElems += (*it)->NbNodes();
244 * template class used for iteration on submesh elements. Interface of iterator remains
245 * unchanged after redesign of SMDS to avoid modification everywhere in SMESH.
246 * instances are stored in shared_ptr for automatic destruction.
247 * Container is copied for iteration, because original can be modified
248 * by addition of elements, for instance, and then reallocated (vector)
250 template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
253 typename TSET::const_iterator _it, _end;
256 MySetIterator(const TSET& table)
259 _it = _table.begin();
261 while ((_it != _end) && (*_it == 0))
267 while ((_it != _end) && (*_it == 0))
269 return (_it != _end);
280 // =====================
282 // =====================
284 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
287 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
288 : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
292 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
294 myElemIt = getElements(*mySubIt);
297 myMore = myElemIt.get() && myElemIt->more();
304 elem = myElemIt->next();
308 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
309 getElements(const SMESHDS_SubMesh*) const = 0;
313 set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
314 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
317 // =====================
318 // class MyElemIterator
319 // =====================
321 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
324 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
325 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
326 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
327 { return theSubMesh->GetElements(); }
330 // =====================
331 // class MyNodeIterator
332 // =====================
334 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
337 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
338 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
339 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
340 { return theSubMesh->GetNodes(); }
343 //=======================================================================
344 //function : GetElements
346 //=======================================================================
348 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
350 if ( IsComplexSubmesh() )
351 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
352 return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> >(myElements));
355 //=======================================================================
356 //function : GetNodes
358 //=======================================================================
360 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
362 if ( IsComplexSubmesh() )
363 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
365 return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> >(myNodes));
368 //=======================================================================
369 //function : Contains
370 //purpose : check if elem or node is in
371 //=======================================================================
373 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
375 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
376 //if ( IsComplexSubmesh() || !ME )
380 if (IsComplexSubmesh())
382 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
383 for (; aSubIt != mySubMeshes.end(); aSubIt++)
384 if ((*aSubIt)->Contains(ME))
389 if (ME->GetType() == SMDSAbs_Node)
391 int idInShape = ME->getIdInShape();
392 if ((idInShape >= 0) && (idInShape < myNodes.size()))
393 if (myNodes[idInShape] == ME)
398 int idInShape = ME->getIdInShape();
399 if ((idInShape >= 0) && (idInShape < myElements.size()))
400 if (myElements[idInShape] == ME)
406 //=======================================================================
407 //function : AddSubMesh
409 //=======================================================================
411 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
413 ASSERT( theSubMesh );
414 mySubMeshes.insert( theSubMesh );
417 //=======================================================================
418 //function : RemoveSubMesh
420 //=======================================================================
422 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
424 return mySubMeshes.erase( theSubMesh );
427 //=======================================================================
428 //function : RemoveAllSubmeshes
430 //=======================================================================
432 void SMESHDS_SubMesh::RemoveAllSubmeshes()
437 //=======================================================================
438 //function : ContainsSubMesh
440 //=======================================================================
442 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
444 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
447 //=======================================================================
448 //function : GetSubMeshIterator
450 //=======================================================================
452 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
454 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
455 return SMESHDS_SubMeshIteratorPtr
456 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
460 //=======================================================================
462 //purpose : remove the contents
463 //=======================================================================
465 void SMESHDS_SubMesh::Clear()
470 myUnusedIdElements = 0;
471 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
472 while ( sub->more() ) {
473 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
478 int SMESHDS_SubMesh::getSize()
481 int d = NbElements();
482 //cerr << "SMESHDS_SubMesh::NbNodes " << c << endl;
483 //cerr << "SMESHDS_SubMesh::NbElements " << d << endl;
487 void SMESHDS_SubMesh::compactList()
489 //MESSAGE("compactList old: nodes " << myNodes.size() << " elements " << myElements.size());
495 std::vector<const SMDS_MeshElement*> newElems;
497 for (int i = 0; i < myElements.size(); i++)
500 SMDS_MeshElement* elem = (SMDS_MeshElement*)myElements[i];
501 elem->setIdInShape(newElems.size());
502 newElems.push_back(elem);
503 //a << elem->GetID() << " ";
504 //b << elem->GetID() << " ";
508 myElements.swap(newElems);
509 myUnusedIdElements = 0;
510 //MESSAGE("in " << myIndex << " oldElems " << a.str());
511 //MESSAGE("in " << myIndex << " newElems " << b.str());
513 std::vector<const SMDS_MeshNode*> newNodes;
515 for (int i = 0; i < myNodes.size(); i++)
518 SMDS_MeshNode* node = (SMDS_MeshNode*)myNodes[i];
519 node->setIdInShape(newNodes.size());
520 newNodes.push_back(node);
521 //c << node->GetID() << " ";
522 //d << node->GetID() << " ";
526 myNodes.swap(newNodes);
528 //MESSAGE("in " << myIndex << " oldNodes " << c.str());
529 //MESSAGE("in " << myIndex << " newNodes " << d.str());
530 //MESSAGE("compactList new: nodes " << myNodes.size() << " elements " << myElements.size());