Salome HOME
Windows porting
[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   if ( !IsComplexSubmesh() )
42     myElements.insert(ME);
43 }
44
45 //=======================================================================
46 //function : RemoveElement
47 //purpose  : 
48 //=======================================================================
49 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME)
50 {
51   if ( !IsComplexSubmesh() && NbElements() )
52     return myElements.erase(ME);
53   
54   return false;
55 }
56
57 //=======================================================================
58 //function : AddNode
59 //purpose  : 
60 //=======================================================================
61 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
62 {
63   if ( !IsComplexSubmesh() )
64     myNodes.insert(N);
65 }
66
67 //=======================================================================
68 //function : RemoveNode
69 //purpose  : 
70 //=======================================================================
71
72 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N)
73 {
74   if ( !IsComplexSubmesh() && NbNodes() )
75     return myNodes.erase(N);
76
77   return false;
78 }
79
80 //=======================================================================
81 //function : NbElements
82 //purpose  : 
83 //=======================================================================
84 int SMESHDS_SubMesh::NbElements() const
85 {
86   if ( !IsComplexSubmesh() )
87     return myElements.size();
88
89   int nbElems = 0;
90 #ifndef WNT
91   set<const SMESHDS_SubMesh*>::iterator it = mySubMeshes.begin();
92 #else
93   set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
94 #endif
95   for ( ; it != mySubMeshes.end(); it++ )
96     nbElems += (*it)->NbElements();
97
98   return nbElems;
99 }
100
101 //=======================================================================
102 //function : NbNodes
103 //purpose  : 
104 //=======================================================================
105
106 int SMESHDS_SubMesh::NbNodes() const
107 {
108  if ( !IsComplexSubmesh() )
109    return myNodes.size(); 
110
111   int nbElems = 0;
112 #ifndef WNT
113   set<const SMESHDS_SubMesh*>::iterator it = mySubMeshes.begin();
114 #else
115   set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
116 #endif
117   for ( ; it != mySubMeshes.end(); it++ )
118     nbElems += (*it)->NbNodes();
119
120   return nbElems;
121 }
122
123 // =====================
124 // class MySetIterator
125 // =====================
126
127 template<typename T> class MySetIterator:public SMDS_Iterator<const T*>
128 {
129   typedef const set<const T*> TSet;
130   typename TSet::const_iterator myIt;
131   TSet& mySet;
132
133   public:
134         MySetIterator(const set<const T*>& s):mySet(s), myIt(s.begin())
135         {
136         }
137
138         bool more()
139         {
140                 return myIt!=mySet.end();
141         }
142         const T* next()
143         {
144                 const T* t=*myIt;
145                 myIt++;
146                 return t;                       
147         }
148 };
149
150 // =====================
151 // class MyIterator
152 // =====================
153
154 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
155 {
156  public:
157   MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
158     : mySubMeshes( theSubMeshes ), mySubIt( theSubMeshes.begin() ), myMore(false)
159     {}
160   bool more()
161   {
162     while (( !myElemIt.get() || !myElemIt->more() ) &&
163            mySubIt != mySubMeshes.end())
164     {
165       myElemIt = getElements(*mySubIt);
166       mySubIt++;
167     }
168     myMore = myElemIt.get() && myElemIt->more();
169     return myMore;
170   }
171   VALUE next()
172   {
173     VALUE elem = 0;
174     if ( myMore )
175       elem = myElemIt->next();
176     return elem;
177   }
178  protected:
179   virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
180     getElements(const SMESHDS_SubMesh*) const = 0;
181
182  private:
183   bool                                        myMore;
184   const set<const SMESHDS_SubMesh*>&          mySubMeshes;
185   set<const SMESHDS_SubMesh*>::const_iterator mySubIt;
186   boost::shared_ptr< SMDS_Iterator<VALUE> >   myElemIt;
187 };
188
189 // =====================
190 // class MyElemIterator
191 // =====================
192
193 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
194 {
195  public:
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(); }
200 };
201
202 // =====================
203 // class MyNodeIterator
204 // =====================
205
206 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
207 {
208  public:
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(); }
213 };
214   
215 //=======================================================================
216 //function : GetElements
217 //purpose  : 
218 //=======================================================================
219
220 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
221 {
222   if ( IsComplexSubmesh() )
223     return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
224
225   return SMDS_ElemIteratorPtr(new MySetIterator<SMDS_MeshElement>(myElements));
226 }
227
228 //=======================================================================
229 //function : GetNodes
230 //purpose  : 
231 //=======================================================================
232
233 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
234 {
235   if ( IsComplexSubmesh() )
236     return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
237
238   return SMDS_NodeIteratorPtr(new MySetIterator<SMDS_MeshNode>(myNodes));
239 }
240
241 //=======================================================================
242 //function : Contains
243 //purpose  : check if elem or node is in
244 //=======================================================================
245
246 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
247 {
248   // DO NOT TRY TO FIND A REMOVED ELEMENT !!
249   if ( !ME )
250     return false;
251
252   if ( IsComplexSubmesh() )
253   {
254     set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
255     for ( ; aSubIt != mySubMeshes.end(); aSubIt++ )
256       if ( (*aSubIt)->Contains( ME ))
257         return true;
258     return false;
259   }
260
261   if ( ME->GetType() == SMDSAbs_Node )
262   {
263     const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( ME );
264     return ( myNodes.find( n ) != myNodes.end() );
265   }
266
267   return ( myElements.find( ME ) != myElements.end() );
268 }
269
270 //=======================================================================
271 //function : AddSubMesh
272 //purpose  : 
273 //=======================================================================
274
275 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
276 {
277   ASSERT( theSubMesh );
278   mySubMeshes.insert( theSubMesh );
279 }
280
281 //=======================================================================
282 //function : RemoveSubMesh
283 //purpose  : 
284 //=======================================================================
285
286 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
287 {
288   return mySubMeshes.erase( theSubMesh );
289 }
290
291 //=======================================================================
292 //function : ContainsSubMesh
293 //purpose  : 
294 //=======================================================================
295
296 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
297 {
298   return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
299 }