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"
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, bool isElemDeleted)
52 if ( !IsComplexSubmesh() && NbElements() ) {
54 if (!isElemDeleted) // alive element has valid ID and can be found
55 return myElements.erase(ME);
57 TElemSet::iterator e = myElements.begin(), eEnd = myElements.end();
58 for ( ; e != eEnd; ++e )
60 myElements.erase( e );
68 //=======================================================================
71 //=======================================================================
72 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
74 if ( !IsComplexSubmesh() )
78 //=======================================================================
79 //function : RemoveNode
81 //=======================================================================
83 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
85 if ( !IsComplexSubmesh() && NbNodes() ) {
87 if (!isNodeDeleted) // alive node has valid ID and can be found
88 return myNodes.erase(N);
90 TElemSet::iterator e = myNodes.begin(), eEnd = myNodes.end();
91 for ( ; e != eEnd; ++e )
101 //=======================================================================
102 //function : NbElements
104 //=======================================================================
105 int SMESHDS_SubMesh::NbElements() const
107 if ( !IsComplexSubmesh() )
108 return myElements.size();
111 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
112 for ( ; it != mySubMeshes.end(); it++ )
113 nbElems += (*it)->NbElements();
118 //=======================================================================
121 //=======================================================================
123 int SMESHDS_SubMesh::NbNodes() const
125 if ( !IsComplexSubmesh() )
126 return myNodes.size();
129 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
130 for ( ; it != mySubMeshes.end(); it++ )
131 nbElems += (*it)->NbNodes();
136 // =====================
137 // class MySetIterator
138 // =====================
140 template<class ELEM, typename TSET> class MySetIterator:
141 public SMDS_SetIterator<ELEM, typename TSET::const_iterator >
143 typedef SMDS_SetIterator<ELEM, typename TSET::const_iterator > TFather;
145 MySetIterator(const TSET& s):TFather(s.begin(),s.end())
150 // =====================
152 // =====================
154 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
157 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
158 : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
162 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
164 myElemIt = getElements(*mySubIt);
167 myMore = myElemIt.get() && myElemIt->more();
174 elem = myElemIt->next();
178 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
179 getElements(const SMESHDS_SubMesh*) const = 0;
183 set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
184 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
187 // =====================
188 // class MyElemIterator
189 // =====================
191 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
194 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
195 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
196 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
197 { return theSubMesh->GetElements(); }
200 // =====================
201 // class MyNodeIterator
202 // =====================
204 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
207 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
208 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
209 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
210 { return theSubMesh->GetNodes(); }
213 //=======================================================================
214 //function : GetElements
216 //=======================================================================
218 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
220 if ( IsComplexSubmesh() )
221 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
223 return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*,TElemSet>(myElements));
226 //=======================================================================
227 //function : GetNodes
229 //=======================================================================
231 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
233 if ( IsComplexSubmesh() )
234 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
236 return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*,TElemSet>(myNodes));
239 //=======================================================================
240 //function : Contains
241 //purpose : check if elem or node is in
242 //=======================================================================
244 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
246 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
247 //if ( IsComplexSubmesh() || !ME )
251 if ( IsComplexSubmesh() )
253 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
254 for ( ; aSubIt != mySubMeshes.end(); aSubIt++ )
255 if ( (*aSubIt)->Contains( ME ))
260 if ( ME->GetType() == SMDSAbs_Node )
261 return ( myNodes.find( ME ) != myNodes.end() );
263 return ( myElements.find( ME ) != myElements.end() );
266 //=======================================================================
267 //function : AddSubMesh
269 //=======================================================================
271 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
273 ASSERT( theSubMesh );
274 mySubMeshes.insert( theSubMesh );
277 //=======================================================================
278 //function : RemoveSubMesh
280 //=======================================================================
282 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
284 return mySubMeshes.erase( theSubMesh );
287 //=======================================================================
288 //function : ContainsSubMesh
290 //=======================================================================
292 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
294 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
297 //=======================================================================
298 //function : GetSubMeshIterator
300 //=======================================================================
302 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
304 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
305 return SMESHDS_SubMeshIteratorPtr
306 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
310 //=======================================================================
312 //purpose : remove the contents
313 //=======================================================================
315 void SMESHDS_SubMesh::Clear()
319 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
320 while ( sub->more() ) {
321 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())