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"
35 //=======================================================================
36 //function : AddElement
38 //=======================================================================
39 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
41 if ( !IsComplexSubmesh() )
42 myElements.insert(ME);
45 //=======================================================================
46 //function : RemoveElement
48 //=======================================================================
49 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
51 if ( !IsComplexSubmesh() && NbElements() ) {
53 if (!isElemDeleted) // alive element has valid ID and can be found
54 return myElements.erase(ME);
56 TElemSet::iterator e = myElements.begin(), eEnd = myElements.end();
57 for ( ; e != eEnd; ++e )
59 myElements.erase( e );
67 //=======================================================================
70 //=======================================================================
71 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
73 if ( !IsComplexSubmesh() )
77 //=======================================================================
78 //function : RemoveNode
80 //=======================================================================
82 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
84 if ( !IsComplexSubmesh() && NbNodes() ) {
86 if (!isNodeDeleted) // alive node has valid ID and can be found
87 return myNodes.erase(N);
89 TElemSet::iterator e = myNodes.begin(), eEnd = myNodes.end();
90 for ( ; e != eEnd; ++e )
100 //=======================================================================
101 //function : NbElements
103 //=======================================================================
104 int SMESHDS_SubMesh::NbElements() const
106 if ( !IsComplexSubmesh() )
107 return myElements.size();
110 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
111 for ( ; it != mySubMeshes.end(); it++ )
112 nbElems += (*it)->NbElements();
117 //=======================================================================
120 //=======================================================================
122 int SMESHDS_SubMesh::NbNodes() const
124 if ( !IsComplexSubmesh() )
125 return myNodes.size();
128 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
129 for ( ; it != mySubMeshes.end(); it++ )
130 nbElems += (*it)->NbNodes();
135 // =====================
136 // class MySetIterator
137 // =====================
139 template<class ELEM, typename TSET> class MySetIterator:
140 public SMDS_SetIterator<ELEM, typename TSET::const_iterator >
142 typedef SMDS_SetIterator<ELEM, typename TSET::const_iterator > TFather;
144 MySetIterator(const TSET& s):TFather(s.begin(),s.end())
149 // =====================
151 // =====================
153 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
156 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
157 : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
161 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
163 myElemIt = getElements(*mySubIt);
166 myMore = myElemIt.get() && myElemIt->more();
173 elem = myElemIt->next();
177 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
178 getElements(const SMESHDS_SubMesh*) const = 0;
182 set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
183 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
186 // =====================
187 // class MyElemIterator
188 // =====================
190 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
193 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
194 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
195 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
196 { return theSubMesh->GetElements(); }
199 // =====================
200 // class MyNodeIterator
201 // =====================
203 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
206 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
207 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
208 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
209 { return theSubMesh->GetNodes(); }
212 //=======================================================================
213 //function : GetElements
215 //=======================================================================
217 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
219 if ( IsComplexSubmesh() )
220 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
222 return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*,TElemSet>(myElements));
225 //=======================================================================
226 //function : GetNodes
228 //=======================================================================
230 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
232 if ( IsComplexSubmesh() )
233 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
235 return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*,TElemSet>(myNodes));
238 //=======================================================================
239 //function : Contains
240 //purpose : check if elem or node is in
241 //=======================================================================
243 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
245 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
249 if ( IsComplexSubmesh() )
251 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
252 for ( ; aSubIt != mySubMeshes.end(); aSubIt++ )
253 if ( (*aSubIt)->Contains( ME ))
258 if ( ME->GetType() == SMDSAbs_Node )
259 return ( myNodes.find( ME ) != myNodes.end() );
261 return ( myElements.find( ME ) != myElements.end() );
264 //=======================================================================
265 //function : AddSubMesh
267 //=======================================================================
269 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
271 ASSERT( theSubMesh );
272 mySubMeshes.insert( theSubMesh );
275 //=======================================================================
276 //function : RemoveSubMesh
278 //=======================================================================
280 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
282 return mySubMeshes.erase( theSubMesh );
285 //=======================================================================
286 //function : ContainsSubMesh
288 //=======================================================================
290 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
292 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
295 //=======================================================================
296 //function : GetSubMeshIterator
298 //=======================================================================
300 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
302 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
303 return SMESHDS_SubMeshIteratorPtr
304 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
308 //=======================================================================
310 //purpose : remove the contents
311 //=======================================================================
313 void SMESHDS_SubMesh::Clear()
317 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
318 while ( sub->more() ) {
319 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())