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();
90 set<const SMESHDS_SubMesh*>::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*>::iterator it = mySubMeshes.begin();
109 for ( ; it != mySubMeshes.end(); it++ )
110 nbElems += (*it)->NbNodes();
115 // =====================
116 // class MySetIterator
117 // =====================
119 template<typename T> class MySetIterator:public SMDS_Iterator<const T*>
121 typedef const set<const T*> TSet;
122 typename TSet::const_iterator myIt;
126 MySetIterator(const set<const T*>& s):mySet(s), myIt(s.begin())
132 return myIt!=mySet.end();
142 // =====================
144 // =====================
146 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
149 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
150 : mySubMeshes( theSubMeshes ), mySubIt( theSubMeshes.begin() ), myMore(false)
154 while (( !myElemIt.get() || !myElemIt->more() ) &&
155 mySubIt != mySubMeshes.end())
157 myElemIt = getElements(*mySubIt);
160 myMore = myElemIt.get() && myElemIt->more();
167 elem = myElemIt->next();
171 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
172 getElements(const SMESHDS_SubMesh*) const = 0;
176 const set<const SMESHDS_SubMesh*>& mySubMeshes;
177 set<const SMESHDS_SubMesh*>::const_iterator mySubIt;
178 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
181 // =====================
182 // class MyElemIterator
183 // =====================
185 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
188 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
189 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
190 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
191 { return theSubMesh->GetElements(); }
194 // =====================
195 // class MyNodeIterator
196 // =====================
198 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
201 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
202 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
203 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
204 { return theSubMesh->GetNodes(); }
207 //=======================================================================
208 //function : GetElements
210 //=======================================================================
212 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
214 if ( IsComplexSubmesh() )
215 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
217 return SMDS_ElemIteratorPtr(new MySetIterator<SMDS_MeshElement>(myElements));
220 //=======================================================================
221 //function : GetNodes
223 //=======================================================================
225 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
227 if ( IsComplexSubmesh() )
228 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
230 return SMDS_NodeIteratorPtr(new MySetIterator<SMDS_MeshNode>(myNodes));
233 //=======================================================================
234 //function : Contains
235 //purpose : check if elem or node is in
236 //=======================================================================
238 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
240 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
244 if ( IsComplexSubmesh() )
246 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
247 for ( ; aSubIt != mySubMeshes.end(); aSubIt++ )
248 if ( (*aSubIt)->Contains( ME ))
253 if ( ME->GetType() == SMDSAbs_Node )
255 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( ME );
256 return ( myNodes.find( n ) != myNodes.end() );
259 return ( myElements.find( ME ) != myElements.end() );
262 //=======================================================================
263 //function : AddSubMesh
265 //=======================================================================
267 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
269 ASSERT( theSubMesh );
270 mySubMeshes.insert( theSubMesh );
273 //=======================================================================
274 //function : RemoveSubMesh
276 //=======================================================================
278 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
280 return mySubMeshes.erase( theSubMesh );
283 //=======================================================================
284 //function : ContainsSubMesh
286 //=======================================================================
288 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
290 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();