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