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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SMESH_SubMesh.cxx
25 // Author : Yves FRICAUD, OCC
29 #include "SMESHDS_SubMesh.hxx"
31 #include "utilities.h"
35 //=======================================================================
36 //function : AddElement
38 //=======================================================================
39 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
41 myElements.insert(ME);
44 //=======================================================================
45 //function : RemoveElement
47 //=======================================================================
48 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME)
51 return myElements.erase(ME);
56 //=======================================================================
59 //=======================================================================
60 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
65 //=======================================================================
66 //function : RemoveNode
68 //=======================================================================
70 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N)
73 return myNodes.erase(N);
78 //=======================================================================
79 //function : NbElements
81 //=======================================================================
82 int SMESHDS_SubMesh::NbElements() const
84 if ( mySubMeshes.empty() )
85 return myElements.size();
88 set<const SMESHDS_SubMesh*>::iterator it = mySubMeshes.begin();
89 for ( ; it != mySubMeshes.end(); it++ )
90 nbElems += (*it)->NbElements();
95 //=======================================================================
98 //=======================================================================
100 int SMESHDS_SubMesh::NbNodes() const
102 if ( mySubMeshes.empty() )
103 return myNodes.size();
106 set<const SMESHDS_SubMesh*>::iterator it = mySubMeshes.begin();
107 for ( ; it != mySubMeshes.end(); it++ )
108 nbElems += (*it)->NbNodes();
113 // =====================
114 // class MySetIterator
115 // =====================
117 template<typename T> class MySetIterator:public SMDS_Iterator<const T*>
119 typedef const set<const T*> TSet;
120 typename TSet::const_iterator myIt;
124 MySetIterator(const set<const T*>& s):mySet(s), myIt(s.begin())
130 return myIt!=mySet.end();
140 // =====================
142 // =====================
144 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
147 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
148 : mySubMeshes( theSubMeshes ), mySubIt( theSubMeshes.begin() ) {}
151 while (( !myElemIt.get() || !myElemIt->more() ) &&
152 mySubIt != mySubMeshes.end())
154 myElemIt = getElements(*mySubIt);
157 return myElemIt.get() && myElemIt->more();
162 return myElemIt->next();
166 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
167 getElements(const SMESHDS_SubMesh*) const = 0;
170 const set<const SMESHDS_SubMesh*>& mySubMeshes;
171 set<const SMESHDS_SubMesh*>::const_iterator mySubIt;
172 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
175 // =====================
176 // class MyElemIterator
177 // =====================
179 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
182 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
183 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
184 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
185 { return theSubMesh->GetElements(); }
188 // =====================
189 // class MyNodeIterator
190 // =====================
192 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
195 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
196 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
197 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
198 { return theSubMesh->GetNodes(); }
201 //=======================================================================
202 //function : GetElements
204 //=======================================================================
206 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
208 if ( mySubMeshes.empty() )
209 return SMDS_ElemIteratorPtr(new MySetIterator<SMDS_MeshElement>(myElements));
211 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
214 //=======================================================================
215 //function : GetNodes
217 //=======================================================================
219 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
221 if ( mySubMeshes.empty() )
222 return SMDS_NodeIteratorPtr(new MySetIterator<SMDS_MeshNode>(myNodes));
224 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
227 //=======================================================================
228 //function : Contains
229 //purpose : check if elem or node is in
230 //=======================================================================
232 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME)
237 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
238 if ( ME->GetType() == SMDSAbs_Node ) {
239 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( ME );
240 return ( myNodes.find( n ) != myNodes.end() );
243 return ( myElements.find( ME ) != myElements.end() );
246 //=======================================================================
247 //function : AddSubMesh
249 //=======================================================================
251 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
253 ASSERT( theSubMesh );
254 mySubMeshes.insert( theSubMesh );
257 //=======================================================================
258 //function : RemoveSubMesh
260 //=======================================================================
262 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
264 return mySubMeshes.erase( theSubMesh );
267 //=======================================================================
268 //function : ContainsSubMesh
270 //=======================================================================
272 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
274 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();