Salome HOME
Integration of PAL/SALOME V2.1.0c from OCC
[modules/smesh.git] / src / SMESHDS / SMESHDS_SubMesh.cxx
1 //  SMESH SMESHDS : management of mesh data and SMESH document
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SMESH_SubMesh.cxx
25 //  Author : Yves FRICAUD, OCC
26 //  Module : SMESH
27 //  $Header: 
28
29 #include "SMESHDS_SubMesh.hxx"
30
31 #include "utilities.h"
32
33 using namespace std;
34
35 //=======================================================================
36 //function : AddElement
37 //purpose  : 
38 //=======================================================================
39 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
40 {
41         myElements.insert(ME);
42 }
43
44 //=======================================================================
45 //function : RemoveElement
46 //purpose  : 
47 //=======================================================================
48 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME)
49 {
50   if ( NbElements() )
51     return myElements.erase(ME);
52   
53   return false;
54 }
55
56 //=======================================================================
57 //function : AddNode
58 //purpose  : 
59 //=======================================================================
60 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
61 {
62         myNodes.insert(N);
63 }
64
65 //=======================================================================
66 //function : RemoveNode
67 //purpose  : 
68 //=======================================================================
69
70 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N)
71 {
72   if ( NbNodes() )
73     return myNodes.erase(N);
74
75   return false;
76 }
77
78 //=======================================================================
79 //function : NbElements
80 //purpose  : 
81 //=======================================================================
82 int SMESHDS_SubMesh::NbElements() const
83 {
84   if ( mySubMeshes.empty() )
85     return myElements.size();
86
87   int nbElems = 0;
88   set<const SMESHDS_SubMesh*>::iterator it = mySubMeshes.begin();
89   for ( ; it != mySubMeshes.end(); it++ )
90     nbElems += (*it)->NbElements();
91
92   return nbElems;
93 }
94
95 //=======================================================================
96 //function : NbNodes
97 //purpose  : 
98 //=======================================================================
99
100 int SMESHDS_SubMesh::NbNodes() const
101 {
102  if ( mySubMeshes.empty() )
103    return myNodes.size(); 
104
105   int nbElems = 0;
106   set<const SMESHDS_SubMesh*>::iterator it = mySubMeshes.begin();
107   for ( ; it != mySubMeshes.end(); it++ )
108     nbElems += (*it)->NbNodes();
109
110   return nbElems;
111 }
112
113 // =====================
114 // class MySetIterator
115 // =====================
116
117 template<typename T> class MySetIterator:public SMDS_Iterator<const T*>
118 {
119   typedef const set<const T*> TSet;
120   typename TSet::const_iterator myIt;
121   TSet& mySet;
122
123   public:
124         MySetIterator(const set<const T*>& s):mySet(s), myIt(s.begin())
125         {
126         }
127
128         bool more()
129         {
130                 return myIt!=mySet.end();
131         }
132         const T* next()
133         {
134                 const T* t=*myIt;
135                 myIt++;
136                 return t;                       
137         }
138 };
139
140 // =====================
141 // class MyIterator
142 // =====================
143
144 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
145 {
146  public:
147   MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
148     : mySubMeshes( theSubMeshes ), mySubIt( theSubMeshes.begin() ) {}
149   bool more()
150   {
151     while (( !myElemIt.get() || !myElemIt->more() ) &&
152            mySubIt != mySubMeshes.end())
153     {
154       myElemIt = getElements(*mySubIt);
155       mySubIt++;
156     }
157     return myElemIt.get() && myElemIt->more();
158   }
159   VALUE next()
160   {
161     if ( more() )
162       return myElemIt->next();
163     return 0;
164   }
165  protected:
166   virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
167     getElements(const SMESHDS_SubMesh*) const = 0;
168
169  private:
170   const set<const SMESHDS_SubMesh*>&          mySubMeshes;
171   set<const SMESHDS_SubMesh*>::const_iterator mySubIt;
172   boost::shared_ptr< SMDS_Iterator<VALUE> >   myElemIt;
173 };
174
175 // =====================
176 // class MyElemIterator
177 // =====================
178
179 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
180 {
181  public:
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(); }
186 };
187
188 // =====================
189 // class MyNodeIterator
190 // =====================
191
192 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
193 {
194  public:
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(); }
199 };
200   
201 //=======================================================================
202 //function : GetElements
203 //purpose  : 
204 //=======================================================================
205
206 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
207 {
208   if ( mySubMeshes.empty() )
209     return SMDS_ElemIteratorPtr(new MySetIterator<SMDS_MeshElement>(myElements));
210
211   return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
212 }
213
214 //=======================================================================
215 //function : GetNodes
216 //purpose  : 
217 //=======================================================================
218
219 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
220 {
221   if ( mySubMeshes.empty() )
222     return SMDS_NodeIteratorPtr(new MySetIterator<SMDS_MeshNode>(myNodes));
223
224   return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
225 }
226
227 //=======================================================================
228 //function : Contains
229 //purpose  : check if elem or node is in
230 //=======================================================================
231
232 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME)
233 {
234   if ( !ME )
235     return false;
236
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() );
241   }
242
243   return ( myElements.find( ME ) != myElements.end() );
244 }
245
246 //=======================================================================
247 //function : AddSubMesh
248 //purpose  : 
249 //=======================================================================
250
251 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
252 {
253   ASSERT( theSubMesh );
254   mySubMeshes.insert( theSubMesh );
255 }
256
257 //=======================================================================
258 //function : RemoveSubMesh
259 //purpose  : 
260 //=======================================================================
261
262 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
263 {
264   return mySubMeshes.erase( theSubMesh );
265 }
266
267 //=======================================================================
268 //function : ContainsSubMesh
269 //purpose  : 
270 //=======================================================================
271
272 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
273 {
274   return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
275 }