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 MESSAGE("add element in subshape already belonging to that subshape "
71 << ME->GetID() << " " << oldShapeId << " " << idInSubShape);
72 // check if ok: do nothing if ok
73 if ((idInSubShape == -1) || (idInSubShape >= myElements.size()))
75 MESSAGE("out of bounds");
76 throw SALOME_Exception(LOCALIZED("out of bounds"));
78 if (ME != myElements[idInSubShape])
80 MESSAGE("not the same element");
81 throw SALOME_Exception(LOCALIZED("not the same element"));
86 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
87 elem->setShapeId(myIndex);
88 elem->setIdInShape(myElements.size());
89 myElements.push_back(ME);
93 //=======================================================================
94 //function : RemoveElement
96 //=======================================================================
97 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
101 MESSAGE("-----------------> Remove Null Element " << isElemDeleted);
104 //MESSAGE("-----------------> RemoveElement "<< ME->GetID() << " " << isElemDeleted);
105 if (!IsComplexSubmesh())
107 // if (!isElemDeleted) // alive element has valid ID and can be found
109 int idInSubShape = ME->getIdInShape();
110 //MESSAGE("in "<< myIndex << " RemoveElement " << ME->GetID() << " " << idInSubShape << " " << myUnusedIdElements);
111 SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
113 elem->setIdInShape(-1);
114 if ((idInSubShape >= 0) && (idInSubShape < myElements.size()))
116 myElements[idInSubShape] = 0; // this vector entry is no more used
117 myUnusedIdElements++;
123 MESSAGE("Try to remove an element from a complex submesh ");
127 //=======================================================================
130 //=======================================================================
131 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
133 if ( !IsComplexSubmesh() )
135 int idInSubShape = N->getIdInShape();
136 int shapeId = N->getshapeId();
137 if ((shapeId > 0) && (idInSubShape >= 0))
139 MESSAGE("========== AddNode already belonging to other subShape " << N->GetID());
140 // OK for vertex nodes
141 //this->getParent()->UnSetNodeOnShape(N);
143 SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
144 node->setShapeId(myIndex);
145 node->setIdInShape(myNodes.size());
146 myNodes.push_back(N);
147 //MESSAGE("in "<< myIndex << " AddNode " << node->GetID());
149 //MESSAGE("try to add node in a complex submesh " << N->GetID());
152 //=======================================================================
153 //function : RemoveNode
155 //=======================================================================
157 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
159 if (!IsComplexSubmesh())
161 // if (!isNodeDeleted) // alive node has valid ID and can be found
163 int idInSubShape = N->getIdInShape();
164 //int shapeId = N->getshapeId();
165 //MESSAGE("in "<< myIndex << " RemoveNode " << shapeId << " " << idInSubShape << " " << N->GetID());
166 SMDS_MeshNode* node = (SMDS_MeshNode*) (N);
168 node->setIdInShape(-1);
169 if ((idInSubShape >= 0) && (idInSubShape < myNodes.size()))
171 myNodes[idInSubShape] = 0; // this vector entry is no more used
178 MESSAGE("Try to remove a node from a complex submesh");
182 //=======================================================================
183 //function : NbElements
185 //=======================================================================
186 int SMESHDS_SubMesh::NbElements() const
188 //MESSAGE(this << " NbElements " << IsComplexSubmesh() << " " << myElements.size() - myUnusedIdElements);
189 if ( !IsComplexSubmesh() )
190 return myElements.size() - myUnusedIdElements;
193 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
194 for ( ; it != mySubMeshes.end(); it++ )
195 nbElems += (*it)->NbElements();
200 //=======================================================================
203 //=======================================================================
205 int SMESHDS_SubMesh::NbNodes() const
207 //MESSAGE(this << " NbNodes " << IsComplexSubmesh() << " " << myNodes.size() - myUnusedIdNodes);
208 if ( !IsComplexSubmesh() )
209 return myNodes.size() - myUnusedIdNodes;
212 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
213 for ( ; it != mySubMeshes.end(); it++ )
214 nbElems += (*it)->NbNodes();
220 * template class used for iteration on submesh elements. Interface of iterator remains
221 * unchanged after redesign of SMDS to avoid modification everywhere in SMESH.
222 * instances are stored in shared_ptr for automatic destruction.
223 * Container is copied for iteration, because original can be modified
224 * by addition of elements, for instance, and then reallocated (vector)
226 template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
229 typename TSET::const_iterator _it, _end;
232 MySetIterator(const TSET& table)
235 _it = _table.begin();
237 while ((_it != _end) && (*_it == 0))
243 while ((_it != _end) && (*_it == 0))
245 return (_it != _end);
256 // =====================
258 // =====================
260 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
263 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
264 : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
268 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
270 myElemIt = getElements(*mySubIt);
273 myMore = myElemIt.get() && myElemIt->more();
280 elem = myElemIt->next();
284 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
285 getElements(const SMESHDS_SubMesh*) const = 0;
289 set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
290 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
293 // =====================
294 // class MyElemIterator
295 // =====================
297 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
300 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
301 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
302 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
303 { return theSubMesh->GetElements(); }
306 // =====================
307 // class MyNodeIterator
308 // =====================
310 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
313 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
314 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
315 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
316 { return theSubMesh->GetNodes(); }
319 //=======================================================================
320 //function : GetElements
322 //=======================================================================
324 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
326 if ( IsComplexSubmesh() )
327 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
328 return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> >(myElements));
331 //=======================================================================
332 //function : GetNodes
334 //=======================================================================
336 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
338 if ( IsComplexSubmesh() )
339 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
341 return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> >(myNodes));
344 //=======================================================================
345 //function : Contains
346 //purpose : check if elem or node is in
347 //=======================================================================
349 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
351 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
352 //if ( IsComplexSubmesh() || !ME )
356 if (IsComplexSubmesh())
358 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
359 for (; aSubIt != mySubMeshes.end(); aSubIt++)
360 if ((*aSubIt)->Contains(ME))
365 if (ME->GetType() == SMDSAbs_Node)
367 int idInShape = ME->getIdInShape();
368 if ((idInShape >= 0) && (idInShape < myNodes.size()))
369 if (myNodes[idInShape] == ME)
374 int idInShape = ME->getIdInShape();
375 if ((idInShape >= 0) && (idInShape < myElements.size()))
376 if (myElements[idInShape] == ME)
382 //=======================================================================
383 //function : AddSubMesh
385 //=======================================================================
387 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
389 ASSERT( theSubMesh );
390 mySubMeshes.insert( theSubMesh );
393 //=======================================================================
394 //function : RemoveSubMesh
396 //=======================================================================
398 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
400 return mySubMeshes.erase( theSubMesh );
403 //=======================================================================
404 //function : ContainsSubMesh
406 //=======================================================================
408 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
410 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
413 //=======================================================================
414 //function : GetSubMeshIterator
416 //=======================================================================
418 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
420 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
421 return SMESHDS_SubMeshIteratorPtr
422 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
426 //=======================================================================
428 //purpose : remove the contents
429 //=======================================================================
431 void SMESHDS_SubMesh::Clear()
436 myUnusedIdElements = 0;
437 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
438 while ( sub->more() ) {
439 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
444 int SMESHDS_SubMesh::getSize()
447 int d = NbElements();
448 //cerr << "SMESHDS_SubMesh::NbNodes " << c << endl;
449 //cerr << "SMESHDS_SubMesh::NbElements " << d << endl;
453 void SMESHDS_SubMesh::compactList()
455 //MESSAGE("compactList old: nodes " << myNodes.size() << " elements " << myElements.size());
461 std::vector<const SMDS_MeshElement*> newElems;
463 for (int i = 0; i < myElements.size(); i++)
466 SMDS_MeshElement* elem = (SMDS_MeshElement*)myElements[i];
467 elem->setIdInShape(newElems.size());
468 newElems.push_back(elem);
469 //a << elem->GetID() << " ";
470 //b << elem->GetID() << " ";
474 myElements.swap(newElems);
475 myUnusedIdElements = 0;
476 //MESSAGE("in " << myIndex << " oldElems " << a.str());
477 //MESSAGE("in " << myIndex << " newElems " << b.str());
479 std::vector<const SMDS_MeshNode*> newNodes;
481 for (int i = 0; i < myNodes.size(); i++)
484 SMDS_MeshNode* node = (SMDS_MeshNode*)myNodes[i];
485 node->setIdInShape(newNodes.size());
486 newNodes.push_back(node);
487 //c << node->GetID() << " ";
488 //d << node->GetID() << " ";
492 myNodes.swap(newNodes);
494 //MESSAGE("in " << myIndex << " oldNodes " << c.str());
495 //MESSAGE("in " << myIndex << " newNodes " << d.str());
496 //MESSAGE("compactList new: nodes " << myNodes.size() << " elements " << myElements.size());