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 oldShapeId = ME->getshapeId();
61 if (oldShapeId != myIndex)
63 MESSAGE("add element in subshape already belonging to another subshape "
64 << ME->GetID() << " " << oldShapeId << " " << myIndex);
65 throw SALOME_Exception(LOCALIZED("add element in subshape already belonging to a subshape"));
69 int idInSubShape = ME->getIdInShape();
70 if (idInSubShape >= 0)
72 MESSAGE("add element in subshape already belonging to that subshape "
73 << ME->GetID() << " " << oldShapeId << " " << idInSubShape);
74 // check if ok: do nothing if ok
75 if (idInSubShape >= myElements.size())
77 MESSAGE("out of bounds " << idInSubShape << " " << myElements.size());
78 throw SALOME_Exception(LOCALIZED("out of bounds"));
80 if (ME != myElements[idInSubShape])
82 MESSAGE("not the same element");
83 throw SALOME_Exception(LOCALIZED("not the same element"));
85 MESSAGE("already done, OK, nothing to do");
91 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
92 elem->setShapeId(myIndex);
93 elem->setIdInShape(myElements.size());
94 myElements.push_back(ME);
98 //=======================================================================
99 //function : RemoveElement
101 //=======================================================================
102 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
106 MESSAGE("-----------------> Remove Null Element " << isElemDeleted);
109 //MESSAGE("-----------------> RemoveElement "<< ME->GetID() << " " << isElemDeleted);
110 if (!IsComplexSubmesh())
112 // if (!isElemDeleted) // alive element has valid ID and can be found
114 int idInSubShape = ME->getIdInShape();
115 //MESSAGE("in "<< myIndex << " RemoveElement " << ME->GetID() << " " << idInSubShape << " " << myUnusedIdElements);
116 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
118 elem->setIdInShape(-1);
119 if ((idInSubShape >= 0) && (idInSubShape < myElements.size()))
121 myElements[idInSubShape] = 0; // this vector entry is no more used
122 myUnusedIdElements++;
128 MESSAGE("Try to remove an element from a complex submesh ");
132 //=======================================================================
135 //=======================================================================
136 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
138 if ( !IsComplexSubmesh() )
140 int idInSubShape = N->getIdInShape();
141 int shapeId = N->getshapeId();
142 if ((shapeId > 0) && (idInSubShape >= 0))
144 MESSAGE("========== AddNode already belonging to other subShape " << N->GetID());
145 // OK for vertex nodes
146 //this->getParent()->UnSetNodeOnShape(N);
148 SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
149 node->setShapeId(myIndex);
150 node->setIdInShape(myNodes.size());
151 myNodes.push_back(N);
152 //MESSAGE("in "<< myIndex << " AddNode " << node->GetID());
154 //MESSAGE("try to add node in a complex submesh " << N->GetID());
157 //=======================================================================
158 //function : RemoveNode
160 //=======================================================================
162 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
164 if (!IsComplexSubmesh())
166 // if (!isNodeDeleted) // alive node has valid ID and can be found
168 int idInSubShape = N->getIdInShape();
169 //int shapeId = N->getshapeId();
170 //MESSAGE("in "<< myIndex << " RemoveNode " << shapeId << " " << idInSubShape << " " << N->GetID());
171 SMDS_MeshNode* node = (SMDS_MeshNode*) (N);
173 node->setIdInShape(-1);
174 if ((idInSubShape >= 0) && (idInSubShape < myNodes.size()))
176 myNodes[idInSubShape] = 0; // this vector entry is no more used
183 MESSAGE("Try to remove a node from a complex submesh");
187 //=======================================================================
188 //function : NbElements
190 //=======================================================================
191 int SMESHDS_SubMesh::NbElements() const
193 //MESSAGE(this << " NbElements " << IsComplexSubmesh() << " " << myElements.size() - myUnusedIdElements);
194 if ( !IsComplexSubmesh() )
195 return myElements.size() - myUnusedIdElements;
198 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
199 for ( ; it != mySubMeshes.end(); it++ )
200 nbElems += (*it)->NbElements();
205 //=======================================================================
208 //=======================================================================
210 int SMESHDS_SubMesh::NbNodes() const
212 //MESSAGE(this << " NbNodes " << IsComplexSubmesh() << " " << myNodes.size() - myUnusedIdNodes);
213 if ( !IsComplexSubmesh() )
214 return myNodes.size() - myUnusedIdNodes;
217 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
218 for ( ; it != mySubMeshes.end(); it++ )
219 nbElems += (*it)->NbNodes();
225 * template class used for iteration on submesh elements. Interface of iterator remains
226 * unchanged after redesign of SMDS to avoid modification everywhere in SMESH.
227 * instances are stored in shared_ptr for automatic destruction.
228 * Container is copied for iteration, because original can be modified
229 * by addition of elements, for instance, and then reallocated (vector)
231 template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
234 typename TSET::const_iterator _it, _end;
237 MySetIterator(const TSET& table)
240 _it = _table.begin();
242 while ((_it != _end) && (*_it == 0))
248 while ((_it != _end) && (*_it == 0))
250 return (_it != _end);
261 // =====================
263 // =====================
265 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
268 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
269 : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
273 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
275 myElemIt = getElements(*mySubIt);
278 myMore = myElemIt.get() && myElemIt->more();
285 elem = myElemIt->next();
289 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
290 getElements(const SMESHDS_SubMesh*) const = 0;
294 set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
295 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
298 // =====================
299 // class MyElemIterator
300 // =====================
302 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
305 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
306 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
307 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
308 { return theSubMesh->GetElements(); }
311 // =====================
312 // class MyNodeIterator
313 // =====================
315 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
318 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
319 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
320 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
321 { return theSubMesh->GetNodes(); }
324 //=======================================================================
325 //function : GetElements
327 //=======================================================================
329 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
331 if ( IsComplexSubmesh() )
332 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
333 return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> >(myElements));
336 //=======================================================================
337 //function : GetNodes
339 //=======================================================================
341 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
343 if ( IsComplexSubmesh() )
344 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
346 return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> >(myNodes));
349 //=======================================================================
350 //function : Contains
351 //purpose : check if elem or node is in
352 //=======================================================================
354 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
356 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
357 //if ( IsComplexSubmesh() || !ME )
361 if (IsComplexSubmesh())
363 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
364 for (; aSubIt != mySubMeshes.end(); aSubIt++)
365 if ((*aSubIt)->Contains(ME))
370 if (ME->GetType() == SMDSAbs_Node)
372 int idInShape = ME->getIdInShape();
373 if ((idInShape >= 0) && (idInShape < myNodes.size()))
374 if (myNodes[idInShape] == ME)
379 int idInShape = ME->getIdInShape();
380 if ((idInShape >= 0) && (idInShape < myElements.size()))
381 if (myElements[idInShape] == ME)
387 //=======================================================================
388 //function : AddSubMesh
390 //=======================================================================
392 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
394 ASSERT( theSubMesh );
395 mySubMeshes.insert( theSubMesh );
398 //=======================================================================
399 //function : RemoveSubMesh
401 //=======================================================================
403 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
405 return mySubMeshes.erase( theSubMesh );
408 //=======================================================================
409 //function : ContainsSubMesh
411 //=======================================================================
413 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
415 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
418 //=======================================================================
419 //function : GetSubMeshIterator
421 //=======================================================================
423 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
425 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
426 return SMESHDS_SubMeshIteratorPtr
427 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
431 //=======================================================================
433 //purpose : remove the contents
434 //=======================================================================
436 void SMESHDS_SubMesh::Clear()
441 myUnusedIdElements = 0;
442 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
443 while ( sub->more() ) {
444 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
449 int SMESHDS_SubMesh::getSize()
452 int d = NbElements();
453 //cerr << "SMESHDS_SubMesh::NbNodes " << c << endl;
454 //cerr << "SMESHDS_SubMesh::NbElements " << d << endl;
458 void SMESHDS_SubMesh::compactList()
460 //MESSAGE("compactList old: nodes " << myNodes.size() << " elements " << myElements.size());
466 std::vector<const SMDS_MeshElement*> newElems;
468 for (int i = 0; i < myElements.size(); i++)
471 SMDS_MeshElement* elem = (SMDS_MeshElement*)myElements[i];
472 elem->setIdInShape(newElems.size());
473 newElems.push_back(elem);
474 //a << elem->GetID() << " ";
475 //b << elem->GetID() << " ";
479 myElements.swap(newElems);
480 myUnusedIdElements = 0;
481 //MESSAGE("in " << myIndex << " oldElems " << a.str());
482 //MESSAGE("in " << myIndex << " newElems " << b.str());
484 std::vector<const SMDS_MeshNode*> newNodes;
486 for (int i = 0; i < myNodes.size(); i++)
489 SMDS_MeshNode* node = (SMDS_MeshNode*)myNodes[i];
490 node->setIdInShape(newNodes.size());
491 newNodes.push_back(node);
492 //c << node->GetID() << " ";
493 //d << node->GetID() << " ";
497 myNodes.swap(newNodes);
499 //MESSAGE("in " << myIndex << " oldNodes " << c.str());
500 //MESSAGE("in " << myIndex << " newNodes " << d.str());
501 //MESSAGE("compactList new: nodes " << myNodes.size() << " elements " << myElements.size());