1 // Copyright (C) 2007-2008 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
22 // SMESH SMESHDS : management of mesh data and SMESH document
23 // File : SMESH_SubMesh.cxx
24 // Author : Yves FRICAUD, OCC
28 #include "SMESHDS_SubMesh.hxx"
30 #include "utilities.h"
31 #include "SMDS_SetIterator.hxx"
37 SMESHDS_SubMesh::SMESHDS_SubMesh()
42 myUnusedIdElements = 0;
45 //=======================================================================
46 //function : AddElement
48 //=======================================================================
49 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
51 if ( !IsComplexSubmesh() )
53 int idInSubShape = ME->getIdInShape();
54 assert(idInSubShape == -1);
55 SMDS_MeshElement* elem = (SMDS_MeshElement*)(ME);
56 elem->setIdInShape(myElements.size());
57 myElements.push_back(ME);
61 //=======================================================================
62 //function : RemoveElement
64 //=======================================================================
65 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
67 // MESSAGE("--------------------------------------> RemoveElement " << isElemDeleted);
68 // if ( !IsComplexSubmesh() && NbElements() ) {
70 if (!isElemDeleted) // alive element has valid ID and can be found
72 int idInSubShape = ME->getIdInShape();
73 //MESSAGE("SMESHDS_SubMesh::RemoveElement " << idInSubShape << " " << ME->GetID() << " " << myUnusedIdElements);
74 assert(idInSubShape >= 0);
75 assert(idInSubShape < myElements.size());
76 myElements[idInSubShape] = 0; // this vector entry is no more used
83 // TElemSet::iterator e = myElements.begin(), eEnd = myElements.end();
84 // for ( ; e != eEnd; ++e )
86 // myElements.erase( e );
94 //=======================================================================
97 //=======================================================================
98 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
100 if ( !IsComplexSubmesh() )
102 int idInSubShape = N->getIdInShape();
103 assert(idInSubShape == -1);
104 SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
105 node->setIdInShape(myNodes.size());
106 myNodes.push_back(N);
110 //=======================================================================
111 //function : RemoveNode
113 //=======================================================================
115 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
117 // if ( !IsComplexSubmesh() && NbNodes() ) {
119 if (!isNodeDeleted) // alive node has valid ID and can be found
121 int idInSubShape = N->getIdInShape();
122 //MESSAGE("SMESHDS_SubMesh::RemoveNode " << idInSubShape << " " << N->GetID());
123 assert(idInSubShape >= 0);
124 assert(idInSubShape < myNodes.size());
125 myNodes[idInSubShape] = 0; // this vector entry is no more used
131 // TElemSet::iterator e = myNodes.begin(), eEnd = myNodes.end();
132 // for ( ; e != eEnd; ++e )
134 // myNodes.erase( e );
142 //=======================================================================
143 //function : NbElements
145 //=======================================================================
146 int SMESHDS_SubMesh::NbElements() const
148 if ( !IsComplexSubmesh() )
149 return myElements.size() - myUnusedIdElements;
152 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
153 for ( ; it != mySubMeshes.end(); it++ )
154 nbElems += (*it)->NbElements();
159 //=======================================================================
162 //=======================================================================
164 int SMESHDS_SubMesh::NbNodes() const
166 if ( !IsComplexSubmesh() )
167 return myNodes.size() - myUnusedIdNodes;
170 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
171 for ( ; it != mySubMeshes.end(); it++ )
172 nbElems += (*it)->NbNodes();
178 * template class used for iteration on submesh elements. Interface of iterator remains
179 * unchanged after redesign of SMDS to avoid modification everywhere in SMESH.
180 * instances are stored in shared_ptr for automatic destruction.
181 * Container is copied for iteration, because original can be modified
182 * by addition of elements, for instance, and then reallocated (vector)
184 template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
187 typename TSET::const_iterator _it, _end;
191 MySetIterator(const TSET& table)
192 // : _it(table.begin()), _end(table.end())
194 // MESSAGE("table.size()="<< table.size());
196 _it = _table.begin();
198 // for (int i=0; i< _table.size(); i++)
199 // if (_table[i]) { MESSAGE("_table["<< i << "]="<< _table[i]);}
201 // { MESSAGE("_table["<< i << "]=NULL"); }
207 while((_it != _end) && (*_it == 0))
212 // MESSAGE("more _ind=" << _ind);
213 return (_it != _end);
219 // if (e) { MESSAGE("next _ind=" << _ind << " *_it=" << *_it);}
220 // else { MESSAGE("next _ind=" << _ind << " *_it=NULL");}
227 // =====================
229 // =====================
231 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
234 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
235 : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
239 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
241 myElemIt = getElements(*mySubIt);
244 myMore = myElemIt.get() && myElemIt->more();
251 elem = myElemIt->next();
255 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
256 getElements(const SMESHDS_SubMesh*) const = 0;
260 set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
261 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
264 // =====================
265 // class MyElemIterator
266 // =====================
268 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
271 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
272 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
273 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
274 { return theSubMesh->GetElements(); }
277 // =====================
278 // class MyNodeIterator
279 // =====================
281 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
284 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
285 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
286 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
287 { return theSubMesh->GetNodes(); }
290 //=======================================================================
291 //function : GetElements
293 //=======================================================================
295 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
297 if ( IsComplexSubmesh() )
298 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
299 return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> >(myElements));
302 //=======================================================================
303 //function : GetNodes
305 //=======================================================================
307 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
309 if ( IsComplexSubmesh() )
310 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
312 return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> >(myNodes));
315 //=======================================================================
316 //function : Contains
317 //purpose : check if elem or node is in
318 //=======================================================================
320 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
322 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
323 //if ( IsComplexSubmesh() || !ME )
327 if ( IsComplexSubmesh() )
329 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
330 for ( ; aSubIt != mySubMeshes.end(); aSubIt++ )
331 if ( (*aSubIt)->Contains( ME ))
336 if ( ME->GetType() == SMDSAbs_Node )
338 int idInShape = ME->getIdInShape();
339 if ((idInShape >= 0) && (idInShape < myNodes.size()))
340 if (myNodes[idInShape] == ME) return true;
344 int idInShape = ME->getIdInShape();
345 if ((idInShape >= 0) && (idInShape < myElements.size()))
346 if (myElements[idInShape] == ME) return true;
351 //=======================================================================
352 //function : AddSubMesh
354 //=======================================================================
356 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
358 ASSERT( theSubMesh );
359 mySubMeshes.insert( theSubMesh );
362 //=======================================================================
363 //function : RemoveSubMesh
365 //=======================================================================
367 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
369 return mySubMeshes.erase( theSubMesh );
372 //=======================================================================
373 //function : ContainsSubMesh
375 //=======================================================================
377 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
379 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
382 //=======================================================================
383 //function : GetSubMeshIterator
385 //=======================================================================
387 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
389 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
390 return SMESHDS_SubMeshIteratorPtr
391 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
395 //=======================================================================
397 //purpose : remove the contents
398 //=======================================================================
400 void SMESHDS_SubMesh::Clear()
404 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
405 while ( sub->more() ) {
406 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
411 int SMESHDS_SubMesh::getSize()
414 int d = NbElements();
415 //cerr << "SMESHDS_SubMesh::NbNodes " << c << endl;
416 //cerr << "SMESHDS_SubMesh::NbElements " << d << endl;
420 void SMESHDS_SubMesh::compactList()
422 // todo : compact vector of nodes and elements