1 // Copyright (C) 2007-2013 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
23 // File : SMESHDS_GroupOnFilter.cxx
26 #include "SMESHDS_GroupOnFilter.hxx"
28 #include "SMDS_SetIterator.hxx"
29 #include "SMESHDS_Mesh.hxx"
36 //=============================================================================
38 * Creates a group based on thePredicate
40 //=============================================================================
42 SMESHDS_GroupOnFilter::SMESHDS_GroupOnFilter (const int theID,
43 const SMESHDS_Mesh* theMesh,
44 const SMDSAbs_ElementType theType,
45 const SMESH_PredicatePtr& thePredicate)
46 : SMESHDS_GroupBase(theID,theMesh,theType),
47 myMeshInfo( SMDSEntity_Last, 0 ),
52 SetPredicate( thePredicate );
55 //================================================================================
57 * \brief Sets a new predicate
59 //================================================================================
61 void SMESHDS_GroupOnFilter::SetPredicate( const SMESH_PredicatePtr& thePredicate )
63 myPredicate = thePredicate;
67 myPredicate->SetMesh( GetMesh() );
70 //================================================================================
72 * \brief Returns nb of elements
74 //================================================================================
76 int SMESHDS_GroupOnFilter::Extent() const
79 return std::accumulate( myMeshInfo.begin(), myMeshInfo.end(), 0 );
82 //================================================================================
84 * \brief Checks emptyness
86 //================================================================================
88 bool SMESHDS_GroupOnFilter::IsEmpty()
92 return ( Extent() == 0 );
94 else // not up-to-date
97 SMDS_ElemIteratorPtr okElemIt = GetElements();
98 if ( !okElemIt->more() )
100 // no satisfying elements
111 //================================================================================
113 * \brief Checks if the element belongs to the group
115 //================================================================================
117 bool SMESHDS_GroupOnFilter::Contains (const int theID)
119 return myPredicate && myPredicate->IsSatisfy( theID );
122 //================================================================================
124 * \brief Checks if the element belongs to the group
126 //================================================================================
128 bool SMESHDS_GroupOnFilter::Contains (const SMDS_MeshElement* elem)
130 return myPredicate && myPredicate->IsSatisfy( elem->GetID() );
133 //================================================================================
134 namespace // Iterator
136 struct TIterator : public SMDS_ElemIterator
138 SMESH_PredicatePtr myPredicate;
139 SMDS_ElemIteratorPtr myElemIt;
140 const SMDS_MeshElement* myNextElem;
141 size_t myNbToFind, myNbFound, myTotalNb;
142 vector< const SMDS_MeshElement*>& myFoundElems;
143 bool & myFoundElemsOK;
145 TIterator( const SMESH_PredicatePtr& filter,
146 SMDS_ElemIteratorPtr& elems,
149 vector< const SMDS_MeshElement*>& foundElems,
150 bool & foundElemsOK):
151 myPredicate( filter ),
154 myNbToFind( nbToFind ),
156 myTotalNb( totalNb ),
157 myFoundElems( foundElems ),
158 myFoundElemsOK( foundElemsOK )
160 myFoundElemsOK = false;
165 if ( !myFoundElemsOK )
166 clearVector( myFoundElems );
172 virtual const SMDS_MeshElement* next()
174 const SMDS_MeshElement* res = myNextElem;
175 myNbFound += bool( res );
177 if ( myNbFound < myNbToFind )
179 while ( myElemIt->more() && !myNextElem )
181 myNextElem = myElemIt->next();
182 if ( !myPredicate->IsSatisfy( myNextElem->GetID() ))
186 myFoundElems.push_back( myNextElem );
188 keepOrClearElemVec();
192 keepOrClearElemVec();
196 void keepOrClearElemVec()
198 if ( myNbFound == myTotalNb )
200 myFoundElemsOK = false; // all elems are OK, no need to keep them
204 // nb of bytes used for myFoundElems
205 size_t vecMemSize = myFoundElems.size() * sizeof( SMDS_MeshElement* ) / sizeof(char);
206 size_t aMB = 1024 * 1024;
207 if ( vecMemSize < aMB )
209 myFoundElemsOK = true; // < 1 MB - do not clear
213 int freeRamMB = SMDS_Mesh::CheckMemory( /*doNotRaise=*/true );
215 myFoundElemsOK = true; // hope it's OK
217 myFoundElemsOK = ( freeRamMB * aMB > 10 * vecMemSize );
220 if ( !myFoundElemsOK )
221 clearVector( myFoundElems );
225 struct TEmptyIterator : public SMDS_ElemIterator
227 virtual bool more() { return false; }
228 virtual const SMDS_MeshElement* next() { return 0; }
232 //================================================================================
234 * \brief Return iterator on all elements
236 //================================================================================
238 SMDS_ElemIteratorPtr SMESHDS_GroupOnFilter::GetElements() const
240 size_t nbToFind = std::numeric_limits<size_t>::max();
241 size_t totalNb = GetMesh()->GetMeshInfo().NbElements( GetType() );
243 SMDS_ElemIteratorPtr elemIt; // iterator on all elements to initialize TIterator
246 myPredicate->SetMesh( GetMesh() ); // hope myPredicate updates self here if necessary
248 elemIt = GetMesh()->elementsIterator( GetType() );
252 return SMDS_ElemIteratorPtr( new SMDS_ElementVectorIterator( myElements.begin(),
255 if ( nbToFind == totalNb )
256 return elemIt; // all elements are OK
257 for ( size_t i = 0; i < myNbElemToSkip; ++i )
258 elemIt->next(); // skip w/o check
263 elemIt = SMDS_ElemIteratorPtr( new TEmptyIterator );
266 // the iterator fills myElements if all elements are checked
267 SMESHDS_GroupOnFilter* me = const_cast<SMESHDS_GroupOnFilter*>( this );
268 return SMDS_ElemIteratorPtr
269 ( new TIterator( myPredicate, elemIt, nbToFind, totalNb, me->myElements, me->myElementsOK ));
272 //================================================================================
274 * \brief Return info on sub-types of elements
276 //================================================================================
278 std::vector< int > SMESHDS_GroupOnFilter::GetMeshInfo() const
284 //================================================================================
286 * \brief Fill ids of elements. And return their number.
287 * \a ids must be pre-allocated using nb of elements of type == GetType()
289 //================================================================================
291 int SMESHDS_GroupOnFilter::getElementIds( void* ids, size_t idSize ) const
293 SMESHDS_GroupOnFilter* me = const_cast<SMESHDS_GroupOnFilter*>( this );
298 char* curID = (char*) ids;
299 SMDS_ElemIteratorPtr elIt = GetElements();
304 for ( ; elIt->more(); curID += idSize )
305 (*(int*) curID) = elIt->next()->GetID();
309 // find out nb of elements to skip w/o check before the 1st OK element
310 const SMDS_MeshElement* firstOkElem = me->setNbElemToSkip( elIt );
312 me->myMeshInfo.assign( SMDSEntity_Last, 0 );
313 me->myMeshInfo[ firstOkElem->GetEntityType() ]++;
315 (*(int*) curID) = firstOkElem->GetID();
316 for ( curID += idSize; elIt->more(); curID += idSize )
318 const SMDS_MeshElement* e = elIt->next();
319 (*(int*) curID) = e->GetID();
320 me->myMeshInfo[ e->GetEntityType() ]++;
324 me->setChanged( false );
326 return ( curID - (char*)ids ) / idSize;
329 //================================================================================
331 * \brief Return a value allowing to find out if a group has changed or not
333 //================================================================================
335 int SMESHDS_GroupOnFilter::GetTic() const
337 return GetMesh()->GetMTime() * myPredicateTic;
340 //================================================================================
342 * \brief Return false if update() is needed
344 //================================================================================
346 bool SMESHDS_GroupOnFilter::IsUpToDate() const
348 return !( myMeshModifTime < GetMesh()->GetMTime() );
351 //================================================================================
353 * \brief Updates myElements if necessary
355 //================================================================================
357 void SMESHDS_GroupOnFilter::update() const
359 SMESHDS_GroupOnFilter* me = const_cast<SMESHDS_GroupOnFilter*>( this );
363 SMDS_ElemIteratorPtr elIt = GetElements();
364 if ( elIt->more() ) {
365 // find out nb of elements to skip w/o check before the 1st OK element
366 const SMDS_MeshElement* e = me->setNbElemToSkip( elIt );
367 ++me->myMeshInfo[ e->GetEntityType() ];
368 while ( elIt->more() )
369 ++me->myMeshInfo[ elIt->next()->GetEntityType() ];
371 me->setChanged( false );
375 //================================================================================
377 * \brief Sets myMeshModifTime and clear fields according to modification state
379 //================================================================================
381 void SMESHDS_GroupOnFilter::setChanged(bool changed)
383 myMeshModifTime = GetMesh()->GetMTime();
384 if ( changed && myMeshModifTime != 0 )
387 clearVector( myElements );
388 myElementsOK = false;
390 myMeshInfo.assign( SMDSEntity_Last, 0 );
394 //================================================================================
396 * \brief Sets myNbElemToSkip
397 * \param okElemIt - iterator on OK elements
398 * \retval const SMDS_MeshElement* - the first OK element
400 //================================================================================
402 const SMDS_MeshElement*
403 SMESHDS_GroupOnFilter::setNbElemToSkip( SMDS_ElemIteratorPtr& okElemIt )
405 // find out nb of elements to skip w/o check before the 1st OK element
406 const SMDS_MeshElement* firstOkElem = okElemIt->next();
407 if ( myNbElemToSkip == 0 )
409 SMDS_ElemIteratorPtr elemIt = GetMesh()->elementsIterator( GetType() );
411 while ( elemIt->next() != firstOkElem )