Salome HOME
0020213: RadialPrism_3D_4 failed on V5_1 64 bits
[modules/smesh.git] / src / SMESHDS / SMESHDS_SubMesh.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SMESH SMESHDS : management of mesh data and SMESH document
23 //  File   : SMESH_SubMesh.cxx
24 //  Author : Yves FRICAUD, OCC
25 //  Module : SMESH
26 //  $Header: 
27 //
28 #include "SMESHDS_SubMesh.hxx"
29
30 #include "utilities.h"
31 #include "SMDS_SetIterator.hxx"
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, bool isElemDeleted)
50 {
51   if ( !IsComplexSubmesh() && NbElements() ) {
52
53     if (!isElemDeleted) // alive element has valid ID and can be found
54       return myElements.erase(ME);
55
56     TElemSet::iterator e = myElements.begin(), eEnd = myElements.end();
57     for ( ; e != eEnd; ++e )
58       if ( ME == *e ) {
59         myElements.erase( e );
60         return true;
61       }
62   }
63   
64   return false;
65 }
66
67 //=======================================================================
68 //function : AddNode
69 //purpose  : 
70 //=======================================================================
71 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
72 {
73   if ( !IsComplexSubmesh() )
74     myNodes.insert(N);
75 }
76
77 //=======================================================================
78 //function : RemoveNode
79 //purpose  : 
80 //=======================================================================
81
82 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
83 {
84   if ( !IsComplexSubmesh() && NbNodes() ) {
85
86     if (!isNodeDeleted) // alive node has valid ID and can be found
87       return myNodes.erase(N);
88
89     TElemSet::iterator e = myNodes.begin(), eEnd = myNodes.end();
90     for ( ; e != eEnd; ++e )
91       if ( N == *e ) {
92         myNodes.erase( e );
93         return true;
94       }
95   }
96
97   return false;
98 }
99
100 //=======================================================================
101 //function : NbElements
102 //purpose  : 
103 //=======================================================================
104 int SMESHDS_SubMesh::NbElements() const
105 {
106   if ( !IsComplexSubmesh() )
107     return myElements.size();
108
109   int nbElems = 0;
110   set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
111   for ( ; it != mySubMeshes.end(); it++ )
112     nbElems += (*it)->NbElements();
113
114   return nbElems;
115 }
116
117 //=======================================================================
118 //function : NbNodes
119 //purpose  : 
120 //=======================================================================
121
122 int SMESHDS_SubMesh::NbNodes() const
123 {
124  if ( !IsComplexSubmesh() )
125    return myNodes.size(); 
126
127   int nbElems = 0;
128   set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
129   for ( ; it != mySubMeshes.end(); it++ )
130     nbElems += (*it)->NbNodes();
131
132   return nbElems;
133 }
134
135 // =====================
136 // class MySetIterator
137 // =====================
138
139 template<class ELEM, typename TSET> class MySetIterator:
140   public SMDS_SetIterator<ELEM, typename TSET::const_iterator >
141 {
142   typedef SMDS_SetIterator<ELEM, typename TSET::const_iterator > TFather;
143   public:
144         MySetIterator(const TSET& s):TFather(s.begin(),s.end())
145         {
146         }
147 };
148
149 // =====================
150 // class MyIterator
151 // =====================
152
153 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
154 {
155  public:
156   MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
157     : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
158     {}
159   bool more()
160   {
161     while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
162     {
163       myElemIt = getElements(*mySubIt);
164       mySubIt++;
165     }
166     myMore = myElemIt.get() && myElemIt->more();
167     return myMore;
168   }
169   VALUE next()
170   {
171     VALUE elem = 0;
172     if ( myMore )
173       elem = myElemIt->next();
174     return elem;
175   }
176  protected:
177   virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
178     getElements(const SMESHDS_SubMesh*) const = 0;
179
180  private:
181   bool                                        myMore;
182   set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
183   boost::shared_ptr< SMDS_Iterator<VALUE> >   myElemIt;
184 };
185
186 // =====================
187 // class MyElemIterator
188 // =====================
189
190 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
191 {
192  public:
193   MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
194     :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
195   SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
196   { return theSubMesh->GetElements(); }
197 };
198
199 // =====================
200 // class MyNodeIterator
201 // =====================
202
203 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
204 {
205  public:
206   MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
207     :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
208   SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
209   { return theSubMesh->GetNodes(); }
210 };
211   
212 //=======================================================================
213 //function : GetElements
214 //purpose  : 
215 //=======================================================================
216
217 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
218 {
219   if ( IsComplexSubmesh() )
220     return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
221
222   return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*,TElemSet>(myElements));
223 }
224
225 //=======================================================================
226 //function : GetNodes
227 //purpose  : 
228 //=======================================================================
229
230 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
231 {
232   if ( IsComplexSubmesh() )
233     return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
234
235   return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*,TElemSet>(myNodes));
236 }
237
238 //=======================================================================
239 //function : Contains
240 //purpose  : check if elem or node is in
241 //=======================================================================
242
243 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
244 {
245   // DO NOT TRY TO FIND A REMOVED ELEMENT !!
246   if ( IsComplexSubmesh() || !ME )
247     return false;
248
249 //   if ( IsComplexSubmesh() )
250 //   {
251 //     set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
252 //     for ( ; aSubIt != mySubMeshes.end(); aSubIt++ )
253 //       if ( (*aSubIt)->Contains( ME ))
254 //         return true;
255 //     return false;
256 //   }
257
258   if ( ME->GetType() == SMDSAbs_Node )
259     return ( myNodes.find( ME ) != myNodes.end() );
260
261   return ( myElements.find( ME ) != myElements.end() );
262 }
263
264 //=======================================================================
265 //function : AddSubMesh
266 //purpose  : 
267 //=======================================================================
268
269 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
270 {
271   ASSERT( theSubMesh );
272   mySubMeshes.insert( theSubMesh );
273 }
274
275 //=======================================================================
276 //function : RemoveSubMesh
277 //purpose  : 
278 //=======================================================================
279
280 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
281 {
282   return mySubMeshes.erase( theSubMesh );
283 }
284
285 //=======================================================================
286 //function : ContainsSubMesh
287 //purpose  : 
288 //=======================================================================
289
290 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
291 {
292   return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
293 }
294
295 //=======================================================================
296 //function : GetSubMeshIterator
297 //purpose  : 
298 //=======================================================================
299
300 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
301 {
302   typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
303   return SMESHDS_SubMeshIteratorPtr
304     ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
305                                                                  mySubMeshes.end()));
306 }
307
308 //=======================================================================
309 //function : Clear
310 //purpose  : remove the contents
311 //=======================================================================
312
313 void SMESHDS_SubMesh::Clear()
314 {
315   myElements.clear();
316   myNodes.clear();
317   SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
318   while ( sub->more() ) {
319     if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
320       sm->Clear();
321   }
322 }