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)
51 if ( !IsComplexSubmesh() && NbElements() )
52 return myElements.erase(ME);
57 //=======================================================================
60 //=======================================================================
61 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
63 if ( !IsComplexSubmesh() )
67 //=======================================================================
68 //function : RemoveNode
70 //=======================================================================
72 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N)
74 if ( !IsComplexSubmesh() && NbNodes() )
75 return myNodes.erase(N);
80 //=======================================================================
81 //function : NbElements
83 //=======================================================================
84 int SMESHDS_SubMesh::NbElements() const
86 if ( !IsComplexSubmesh() )
87 return myElements.size();
90 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
91 for ( ; it != mySubMeshes.end(); it++ )
92 nbElems += (*it)->NbElements();
97 //=======================================================================
100 //=======================================================================
102 int SMESHDS_SubMesh::NbNodes() const
104 if ( !IsComplexSubmesh() )
105 return myNodes.size();
108 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
109 for ( ; it != mySubMeshes.end(); it++ )
110 nbElems += (*it)->NbNodes();
115 // =====================
116 // class MySetIterator
117 // =====================
119 template<typename TSet> class MySetIterator: public SMDS_SetIterator<typename TSet::key_type,
120 typename TSet::const_iterator >
122 typedef SMDS_SetIterator<typename TSet::key_type, typename TSet::const_iterator > TFather;
124 MySetIterator(const TSet& s):TFather(s.begin(),s.end())
129 // =====================
131 // =====================
133 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
136 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
137 : mySubMeshes( theSubMeshes ), mySubIt( theSubMeshes.begin() ), myMore(false)
141 while (( !myElemIt.get() || !myElemIt->more() ) &&
142 mySubIt != mySubMeshes.end())
144 myElemIt = getElements(*mySubIt);
147 myMore = myElemIt.get() && myElemIt->more();
154 elem = myElemIt->next();
158 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
159 getElements(const SMESHDS_SubMesh*) const = 0;
163 const set<const SMESHDS_SubMesh*>& mySubMeshes;
164 set<const SMESHDS_SubMesh*>::const_iterator mySubIt;
165 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
168 // =====================
169 // class MyElemIterator
170 // =====================
172 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
175 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
176 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
177 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
178 { return theSubMesh->GetElements(); }
181 // =====================
182 // class MyNodeIterator
183 // =====================
185 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
188 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
189 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
190 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
191 { return theSubMesh->GetNodes(); }
194 //=======================================================================
195 //function : GetElements
197 //=======================================================================
199 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
201 if ( IsComplexSubmesh() )
202 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
204 return SMDS_ElemIteratorPtr(new MySetIterator<TElemSet>(myElements));
207 //=======================================================================
208 //function : GetNodes
210 //=======================================================================
212 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
214 if ( IsComplexSubmesh() )
215 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
217 return SMDS_NodeIteratorPtr(new MySetIterator<TNodeSet>(myNodes));
220 //=======================================================================
221 //function : Contains
222 //purpose : check if elem or node is in
223 //=======================================================================
225 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
227 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
231 if ( IsComplexSubmesh() )
233 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
234 for ( ; aSubIt != mySubMeshes.end(); aSubIt++ )
235 if ( (*aSubIt)->Contains( ME ))
240 if ( ME->GetType() == SMDSAbs_Node )
242 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( ME );
243 return ( myNodes.find( n ) != myNodes.end() );
246 return ( myElements.find( ME ) != myElements.end() );
249 //=======================================================================
250 //function : AddSubMesh
252 //=======================================================================
254 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
256 ASSERT( theSubMesh );
257 mySubMeshes.insert( theSubMesh );
260 //=======================================================================
261 //function : RemoveSubMesh
263 //=======================================================================
265 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
267 return mySubMeshes.erase( theSubMesh );
270 //=======================================================================
271 //function : ContainsSubMesh
273 //=======================================================================
275 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
277 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
280 //=======================================================================
281 //function : GetSubMeshIterator
283 //=======================================================================
285 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
287 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
288 return SMESHDS_SubMeshIteratorPtr
289 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
293 //=======================================================================
295 //purpose : remove the contents
296 //=======================================================================
298 void SMESHDS_SubMesh::Clear()
302 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
303 while ( sub->more() ) {
304 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())