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"
31 #include "utilities.h"
32 #include "SMDS_SetIterator.hxx"
38 SMESHDS_SubMesh::SMESHDS_SubMesh()
43 myUnusedIdElements = 0;
46 //=======================================================================
47 //function : AddElement
49 //=======================================================================
50 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
52 if ( !IsComplexSubmesh() )
54 int idInSubShape = ME->getIdInShape();
55 assert(idInSubShape == -1);
56 SMDS_MeshElement* elem = (SMDS_MeshElement*)(ME);
57 elem->setIdInShape(myElements.size());
58 myElements.push_back(ME);
62 //=======================================================================
63 //function : RemoveElement
65 //=======================================================================
66 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
68 // MESSAGE("--------------------------------------> RemoveElement " << isElemDeleted);
69 // if ( !IsComplexSubmesh() && NbElements() ) {
71 if (!isElemDeleted) // alive element has valid ID and can be found
73 int idInSubShape = ME->getIdInShape();
74 //MESSAGE("SMESHDS_SubMesh::RemoveElement " << idInSubShape << " " << ME->GetID() << " " << myUnusedIdElements);
75 assert(idInSubShape >= 0);
76 assert(idInSubShape < myElements.size());
77 myElements[idInSubShape] = 0; // this vector entry is no more used
84 // TElemSet::iterator e = myElements.begin(), eEnd = myElements.end();
85 // for ( ; e != eEnd; ++e )
87 // myElements.erase( e );
95 //=======================================================================
98 //=======================================================================
99 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
101 if ( !IsComplexSubmesh() )
103 int idInSubShape = N->getIdInShape();
104 assert(idInSubShape == -1);
105 SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
106 node->setIdInShape(myNodes.size());
107 myNodes.push_back(N);
111 //=======================================================================
112 //function : RemoveNode
114 //=======================================================================
116 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
118 // if ( !IsComplexSubmesh() && NbNodes() ) {
120 if (!isNodeDeleted) // alive node has valid ID and can be found
122 int idInSubShape = N->getIdInShape();
123 //MESSAGE("SMESHDS_SubMesh::RemoveNode " << idInSubShape << " " << N->GetID());
124 assert(idInSubShape >= 0);
125 assert(idInSubShape < myNodes.size());
126 myNodes[idInSubShape] = 0; // this vector entry is no more used
132 // TElemSet::iterator e = myNodes.begin(), eEnd = myNodes.end();
133 // for ( ; e != eEnd; ++e )
135 // myNodes.erase( e );
143 //=======================================================================
144 //function : NbElements
146 //=======================================================================
147 int SMESHDS_SubMesh::NbElements() const
149 if ( !IsComplexSubmesh() )
150 return myElements.size() - myUnusedIdElements;
153 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
154 for ( ; it != mySubMeshes.end(); it++ )
155 nbElems += (*it)->NbElements();
160 //=======================================================================
163 //=======================================================================
165 int SMESHDS_SubMesh::NbNodes() const
167 if ( !IsComplexSubmesh() )
168 return myNodes.size() - myUnusedIdNodes;
171 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
172 for ( ; it != mySubMeshes.end(); it++ )
173 nbElems += (*it)->NbNodes();
179 * template class used for iteration on submesh elements. Interface of iterator remains
180 * unchanged after redesign of SMDS to avoid modification everywhere in SMESH.
181 * instances are stored in shared_ptr for automatic destruction.
182 * Container is copied for iteration, because original can be modified
183 * by addition of elements, for instance, and then reallocated (vector)
185 template <class ELEM, typename TSET> class MySetIterator : public SMDS_Iterator<ELEM>
188 typename TSET::const_iterator _it, _end;
192 MySetIterator(const TSET& table)
193 // : _it(table.begin()), _end(table.end())
195 // MESSAGE("table.size()="<< table.size());
197 _it = _table.begin();
199 // for (int i=0; i< _table.size(); i++)
200 // if (_table[i]) { MESSAGE("_table["<< i << "]="<< _table[i]);}
202 // { MESSAGE("_table["<< i << "]=NULL"); }
208 while((_it != _end) && (*_it == 0))
213 // MESSAGE("more _ind=" << _ind);
214 return (_it != _end);
220 // if (e) { MESSAGE("next _ind=" << _ind << " *_it=" << *_it);}
221 // else { MESSAGE("next _ind=" << _ind << " *_it=NULL");}
228 // =====================
230 // =====================
232 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
235 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
236 : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
240 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
242 myElemIt = getElements(*mySubIt);
245 myMore = myElemIt.get() && myElemIt->more();
252 elem = myElemIt->next();
256 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
257 getElements(const SMESHDS_SubMesh*) const = 0;
261 set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
262 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
265 // =====================
266 // class MyElemIterator
267 // =====================
269 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
272 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
273 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
274 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
275 { return theSubMesh->GetElements(); }
278 // =====================
279 // class MyNodeIterator
280 // =====================
282 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
285 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
286 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
287 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
288 { return theSubMesh->GetNodes(); }
291 //=======================================================================
292 //function : GetElements
294 //=======================================================================
296 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
298 if ( IsComplexSubmesh() )
299 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
300 return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*, std::vector<const SMDS_MeshElement*> >(myElements));
303 //=======================================================================
304 //function : GetNodes
306 //=======================================================================
308 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
310 if ( IsComplexSubmesh() )
311 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
313 return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*, std::vector<const SMDS_MeshNode*> >(myNodes));
316 //=======================================================================
317 //function : Contains
318 //purpose : check if elem or node is in
319 //=======================================================================
321 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
323 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
324 //if ( IsComplexSubmesh() || !ME )
328 if ( IsComplexSubmesh() )
330 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
331 for ( ; aSubIt != mySubMeshes.end(); aSubIt++ )
332 if ( (*aSubIt)->Contains( ME ))
337 if ( ME->GetType() == SMDSAbs_Node )
339 int idInShape = ME->getIdInShape();
340 if ((idInShape >= 0) && (idInShape < myNodes.size()))
341 if (myNodes[idInShape] == ME) return true;
345 int idInShape = ME->getIdInShape();
346 if ((idInShape >= 0) && (idInShape < myElements.size()))
347 if (myElements[idInShape] == ME) return true;
352 //=======================================================================
353 //function : AddSubMesh
355 //=======================================================================
357 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
359 ASSERT( theSubMesh );
360 mySubMeshes.insert( theSubMesh );
363 //=======================================================================
364 //function : RemoveSubMesh
366 //=======================================================================
368 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
370 return mySubMeshes.erase( theSubMesh );
373 //=======================================================================
374 //function : ContainsSubMesh
376 //=======================================================================
378 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
380 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
383 //=======================================================================
384 //function : GetSubMeshIterator
386 //=======================================================================
388 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
390 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
391 return SMESHDS_SubMeshIteratorPtr
392 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
396 //=======================================================================
398 //purpose : remove the contents
399 //=======================================================================
401 void SMESHDS_SubMesh::Clear()
405 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
406 while ( sub->more() ) {
407 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
412 int SMESHDS_SubMesh::getSize()
415 int d = NbElements();
416 //cerr << "SMESHDS_SubMesh::NbNodes " << c << endl;
417 //cerr << "SMESHDS_SubMesh::NbElements " << d << endl;
421 void SMESHDS_SubMesh::compactList()
423 // todo : compact vector of nodes and elements