1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // SMESH SMESHDS : management of mesh data and SMESH document
23 // File : SMESH_SubMesh.cxx
24 // Author : Yves FRICAUD, OCC
28 #include "SMESHDS_SubMesh.hxx"
30 #include "utilities.h"
31 #include "SMDS_SetIterator.hxx"
37 SMESHDS_SubMesh::SMESHDS_SubMesh()
42 myUnusedIdElements = 0;
45 //=======================================================================
46 //function : AddElement
48 //=======================================================================
49 void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
51 if ( !IsComplexSubmesh() )
53 int idInSubShape = ME->getIdInShape();
54 assert(idInSubShape == -1);
55 SMDS_MeshElement* elem = (SMDS_MeshElement*)(ME);
56 elem->setIdInShape(myElements.size());
57 myElements.push_back(ME);
61 //=======================================================================
62 //function : RemoveElement
64 //=======================================================================
65 bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDeleted)
67 // if ( !IsComplexSubmesh() && NbElements() ) {
69 if (!isElemDeleted) // alive element has valid ID and can be found
71 int idInSubShape = ME->getIdInShape();
72 assert(idInSubShape >= 0);
73 assert(idInSubShape < myElements.size());
74 myElements[idInSubShape] = 0; // this vector entry is no more used
81 // TElemSet::iterator e = myElements.begin(), eEnd = myElements.end();
82 // for ( ; e != eEnd; ++e )
84 // myElements.erase( e );
92 //=======================================================================
95 //=======================================================================
96 void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
98 if ( !IsComplexSubmesh() )
100 int idInSubShape = N->getIdInShape();
101 assert(idInSubShape == -1);
102 SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
103 node->setIdInShape(myNodes.size());
104 myNodes.push_back(N);
108 //=======================================================================
109 //function : RemoveNode
111 //=======================================================================
113 bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
115 // if ( !IsComplexSubmesh() && NbNodes() ) {
117 if (!isNodeDeleted) // alive node has valid ID and can be found
119 int idInSubShape = N->getIdInShape();
120 assert(idInSubShape >= 0);
121 assert(idInSubShape < myNodes.size());
122 myNodes[idInSubShape] = 0; // this vector entry is no more used
128 // TElemSet::iterator e = myNodes.begin(), eEnd = myNodes.end();
129 // for ( ; e != eEnd; ++e )
131 // myNodes.erase( e );
139 //=======================================================================
140 //function : NbElements
142 //=======================================================================
143 int SMESHDS_SubMesh::NbElements() const
145 if ( !IsComplexSubmesh() )
146 return myElements.size() - myUnusedIdElements;
149 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
150 for ( ; it != mySubMeshes.end(); it++ )
151 nbElems += (*it)->NbElements();
156 //=======================================================================
159 //=======================================================================
161 int SMESHDS_SubMesh::NbNodes() const
163 if ( !IsComplexSubmesh() )
164 return myNodes.size() - myUnusedIdNodes;
167 set<const SMESHDS_SubMesh*>::const_iterator it = mySubMeshes.begin();
168 for ( ; it != mySubMeshes.end(); it++ )
169 nbElems += (*it)->NbNodes();
174 // =====================
175 // class MySetIterator
176 // =====================
178 template<class ELEM, typename TSET> class MySetIterator:
179 public SMDS_SetIterator<ELEM, typename TSET::const_iterator >
181 typedef SMDS_SetIterator<ELEM, typename TSET::const_iterator > TFather;
183 MySetIterator(const TSET& s):TFather(s.begin(),s.end())
188 // =====================
190 // =====================
192 template<typename VALUE> class MyIterator : public SMDS_Iterator<VALUE>
195 MyIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
196 : mySubIt( theSubMeshes.begin() ), mySubEnd( theSubMeshes.end() ), myMore(false)
200 while (( !myElemIt.get() || !myElemIt->more() ) && mySubIt != mySubEnd)
202 myElemIt = getElements(*mySubIt);
205 myMore = myElemIt.get() && myElemIt->more();
212 elem = myElemIt->next();
216 virtual boost::shared_ptr< SMDS_Iterator<VALUE> >
217 getElements(const SMESHDS_SubMesh*) const = 0;
221 set<const SMESHDS_SubMesh*>::const_iterator mySubIt, mySubEnd;
222 boost::shared_ptr< SMDS_Iterator<VALUE> > myElemIt;
225 // =====================
226 // class MyElemIterator
227 // =====================
229 class MyElemIterator: public MyIterator<const SMDS_MeshElement*>
232 MyElemIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
233 :MyIterator<const SMDS_MeshElement*>( theSubMeshes ) {}
234 SMDS_ElemIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
235 { return theSubMesh->GetElements(); }
238 // =====================
239 // class MyNodeIterator
240 // =====================
242 class MyNodeIterator: public MyIterator<const SMDS_MeshNode*>
245 MyNodeIterator (const set<const SMESHDS_SubMesh*>& theSubMeshes)
246 :MyIterator<const SMDS_MeshNode*>( theSubMeshes ) {}
247 SMDS_NodeIteratorPtr getElements(const SMESHDS_SubMesh* theSubMesh) const
248 { return theSubMesh->GetNodes(); }
251 //=======================================================================
252 //function : GetElements
254 //=======================================================================
256 SMDS_ElemIteratorPtr SMESHDS_SubMesh::GetElements() const
258 if ( IsComplexSubmesh() )
259 return SMDS_ElemIteratorPtr( new MyElemIterator( mySubMeshes ));
261 return SMDS_ElemIteratorPtr(new MySetIterator<const SMDS_MeshElement*,TElemSet>(myElements));
264 //=======================================================================
265 //function : GetNodes
267 //=======================================================================
269 SMDS_NodeIteratorPtr SMESHDS_SubMesh::GetNodes() const
271 if ( IsComplexSubmesh() )
272 return SMDS_NodeIteratorPtr( new MyNodeIterator( mySubMeshes ));
274 return SMDS_NodeIteratorPtr(new MySetIterator<const SMDS_MeshNode*,TElemSet>(myNodes));
277 //=======================================================================
278 //function : Contains
279 //purpose : check if elem or node is in
280 //=======================================================================
282 bool SMESHDS_SubMesh::Contains(const SMDS_MeshElement * ME) const
284 // DO NOT TRY TO FIND A REMOVED ELEMENT !!
285 //if ( IsComplexSubmesh() || !ME )
289 if ( IsComplexSubmesh() )
291 set<const SMESHDS_SubMesh*>::const_iterator aSubIt = mySubMeshes.begin();
292 for ( ; aSubIt != mySubMeshes.end(); aSubIt++ )
293 if ( (*aSubIt)->Contains( ME ))
298 if ( ME->GetType() == SMDSAbs_Node )
300 int idInShape = ME->getIdInShape();
301 if ((idInShape >= 0) && (idInShape < myNodes.size()))
302 if (myNodes[idInShape] == ME) return true;
306 int idInShape = ME->getIdInShape();
307 if ((idInShape >= 0) && (idInShape < myElements.size()))
308 if (myElements[idInShape] == ME) return true;
313 //=======================================================================
314 //function : AddSubMesh
316 //=======================================================================
318 void SMESHDS_SubMesh::AddSubMesh( const SMESHDS_SubMesh* theSubMesh )
320 ASSERT( theSubMesh );
321 mySubMeshes.insert( theSubMesh );
324 //=======================================================================
325 //function : RemoveSubMesh
327 //=======================================================================
329 bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
331 return mySubMeshes.erase( theSubMesh );
334 //=======================================================================
335 //function : ContainsSubMesh
337 //=======================================================================
339 bool SMESHDS_SubMesh::ContainsSubMesh( const SMESHDS_SubMesh* theSubMesh ) const
341 return mySubMeshes.find( theSubMesh ) != mySubMeshes.end();
344 //=======================================================================
345 //function : GetSubMeshIterator
347 //=======================================================================
349 SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
351 typedef set<const SMESHDS_SubMesh*>::const_iterator TIterator;
352 return SMESHDS_SubMeshIteratorPtr
353 ( new SMDS_SetIterator< const SMESHDS_SubMesh*, TIterator >( mySubMeshes.begin(),
357 //=======================================================================
359 //purpose : remove the contents
360 //=======================================================================
362 void SMESHDS_SubMesh::Clear()
366 SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();
367 while ( sub->more() ) {
368 if ( SMESHDS_SubMesh* sm = (SMESHDS_SubMesh*) sub->next())
373 int SMESHDS_SubMesh::getSize()
376 int d = NbElements();
377 cerr << "SMESHDS_SubMesh::NbNodes " << c << endl;
378 cerr << "SMESHDS_SubMesh::NbElements " << d << endl;