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 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();
91 set<const SMESHDS_SubMesh*>::iterator it = mySubMeshes.begin();
93 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
95 for ( ; it != mySubMeshes.end(); it++ )
96 nbElems += (*it)->NbElements();
101 //=======================================================================
104 //=======================================================================
106 int SMESHDS_SubMesh::NbNodes() const
108 if ( !IsComplexSubmesh() )
109 return myNodes.size();
113 set<const SMESHDS_SubMesh*>::iterator it = mySubMeshes.begin();
115 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
117 for ( ; it != mySubMeshes.end(); it++ )
118 nbElems += (*it)->NbNodes();
123 // =====================
124 // class MySetIterator
125 // =====================
127 template<typename T> class MySetIterator:public SMDS_Iterator<const T*>
129 typedef const set<const T*> TSet;
130 typename TSet::const_iterator myIt;
134 MySetIterator(const set<const T*>& s):mySet(s), myIt(s.begin())
140 return myIt!=mySet.end();
150 // =====================
152 // =====================
154 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
157 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
158 : mySubMeshes( theSubMeshes ), mySubIt( theSubMeshes.begin() ), myMore(false)
162 while (( !myElemIt.get() || !myElemIt->more() ) &&
163 mySubIt != mySubMeshes.end())
165 myElemIt = getElements(*mySubIt);
168 myMore = myElemIt.get() && myElemIt->more();
175 elem = myElemIt->next();
179 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
180 getElements(const SMESHDS_SubMesh*) const = 0;
184 const set<const SMESHDS_SubMesh*>& mySubMeshes;
185 set<const SMESHDS_SubMesh*>::const_iterator mySubIt;
186 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
189 // =====================
190 // class MyElemIterator
191 // =====================
193 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
196 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
197 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
198 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
199 { return theSubMesh->GetElements(); }
202 // =====================
203 // class MyNodeIterator
204 // =====================
206 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
209 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
210 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
211 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
212 { return theSubMesh->GetNodes(); }
215 //=======================================================================
216 //function : GetElements
218 //=======================================================================
220 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
222 if ( IsComplexSubmesh() )
223 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
225 return SMDS_ElemIteratorPtr(new MySetIterator<SMDS_MeshElement>(myElements));
228 //=======================================================================
229 //function : GetNodes
231 //=======================================================================
233 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
235 if ( IsComplexSubmesh() )
236 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
238 return SMDS_NodeIteratorPtr(new MySetIterator<SMDS_MeshNode>(myNodes));
241 //=======================================================================
242 //function : Contains
243 //purpose : check if elem or node is in
244 //=======================================================================
246 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
248 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
252 if ( IsComplexSubmesh() )
254 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
255 for ( ; aSubIt != mySubMeshes.end(); aSubIt++ )
256 if ( (*aSubIt)->Contains( ME ))
261 if ( ME->GetType() == SMDSAbs_Node )
263 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( ME );
264 return ( myNodes.find( n ) != myNodes.end() );
267 return ( myElements.find( ME ) != myElements.end() );
270 //=======================================================================
271 //function : AddSubMesh
273 //=======================================================================
275 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
277 ASSERT( theSubMesh );
278 mySubMeshes.insert( theSubMesh );
281 //=======================================================================
282 //function : RemoveSubMesh
284 //=======================================================================
286 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
288 return mySubMeshes.erase( theSubMesh );
291 //=======================================================================
292 //function : ContainsSubMesh
294 //=======================================================================
296 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
298 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();