1 // SMESH SMESHDS : management of mesh data and SMESH document
3 // Copyright (C) 2003 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
24 // File : SMESH_SubMesh.cxx
25 // Author : Yves FRICAUD, OCC
29 #include "SMESHDS_SubMesh.hxx"
31 #include "utilities.h"
32 #include "SMDS_SetIterator.hxx"
36 //=======================================================================
37 //function : AddElement
39 //=======================================================================
40 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
42 if ( !IsComplexSubmesh() )
43 myElements.insert(ME);
46 //=======================================================================
47 //function : RemoveElement
49 //=======================================================================
50 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME)
52 if ( !IsComplexSubmesh() && NbElements() )
53 return myElements.erase(ME);
58 //=======================================================================
61 //=======================================================================
62 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
64 if ( !IsComplexSubmesh() )
68 //=======================================================================
69 //function : RemoveNode
71 //=======================================================================
73 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N)
75 if ( !IsComplexSubmesh() && NbNodes() )
76 return myNodes.erase(N);
81 //=======================================================================
82 //function : NbElements
84 //=======================================================================
85 int SMESHDS_SubMesh::NbElements() const
87 if ( !IsComplexSubmesh() )
88 return myElements.size();
92 set<const SMESHDS_SubMesh*>::iterator it = mySubMeshes.begin();
94 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
96 for ( ; it != mySubMeshes.end(); it++ )
97 nbElems += (*it)->NbElements();
102 //=======================================================================
105 //=======================================================================
107 int SMESHDS_SubMesh::NbNodes() const
109 if ( !IsComplexSubmesh() )
110 return myNodes.size();
114 set<const SMESHDS_SubMesh*>::iterator it = mySubMeshes.begin();
116 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
118 for ( ; it != mySubMeshes.end(); it++ )
119 nbElems += (*it)->NbNodes();
124 // =====================
125 // class MySetIterator
126 // =====================
128 template<typename T> class MySetIterator:public SMDS_Iterator<const T*>
130 typedef const set<const T*> TSet;
131 typename TSet::const_iterator myIt;
135 MySetIterator(const set<const T*>& s):mySet(s), myIt(s.begin())
141 return myIt!=mySet.end();
151 // =====================
153 // =====================
155 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
158 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
159 : mySubMeshes( theSubMeshes ), mySubIt( theSubMeshes.begin() ), myMore(false)
163 while (( !myElemIt.get() || !myElemIt->more() ) &&
164 mySubIt != mySubMeshes.end())
166 myElemIt = getElements(*mySubIt);
169 myMore = myElemIt.get() && myElemIt->more();
176 elem = myElemIt->next();
180 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
181 getElements(const SMESHDS_SubMesh*) const = 0;
185 const set<const SMESHDS_SubMesh*>& mySubMeshes;
186 set<const SMESHDS_SubMesh*>::const_iterator mySubIt;
187 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
190 // =====================
191 // class MyElemIterator
192 // =====================
194 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
197 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
198 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
199 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
200 { return theSubMesh->GetElements(); }
203 // =====================
204 // class MyNodeIterator
205 // =====================
207 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
210 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
211 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
212 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
213 { return theSubMesh->GetNodes(); }
216 //=======================================================================
217 //function : GetElements
219 //=======================================================================
221 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
223 if ( IsComplexSubmesh() )
224 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
226 return SMDS_ElemIteratorPtr(new MySetIterator<SMDS_MeshElement>(myElements));
229 //=======================================================================
230 //function : GetNodes
232 //=======================================================================
234 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
236 if ( IsComplexSubmesh() )
237 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
239 return SMDS_NodeIteratorPtr(new MySetIterator<SMDS_MeshNode>(myNodes));
242 //=======================================================================
243 //function : Contains
244 //purpose : check if elem or node is in
245 //=======================================================================
247 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
249 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
253 if ( IsComplexSubmesh() )
255 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
256 for ( ; aSubIt != mySubMeshes.end(); aSubIt++ )
257 if ( (*aSubIt)->Contains( ME ))
262 if ( ME->GetType() == SMDSAbs_Node )
264 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( ME );
265 return ( myNodes.find( n ) != myNodes.end() );
268 return ( myElements.find( ME ) != myElements.end() );
271 //=======================================================================
272 //function : AddSubMesh
274 //=======================================================================
276 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
278 ASSERT( theSubMesh );
279 mySubMeshes.insert( theSubMesh );
282 //=======================================================================
283 //function : RemoveSubMesh
285 //=======================================================================
287 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
289 return mySubMeshes.erase( theSubMesh );
292 //=======================================================================
293 //function : ContainsSubMesh
295 //=======================================================================
297 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
299 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
302 //=======================================================================
303 //function : GetSubMeshIterator
305 //=======================================================================
307 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
309 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
310 return SMESHDS_SubMeshIteratorPtr
311 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
315 //=======================================================================
317 //purpose : remove the contents
318 //=======================================================================
320 void SMESHDS_SubMesh::Clear()
324 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
325 while ( sub->more() ) {
326 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())