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 "SMESHDS_Mesh.hxx"
29 #include "SMDS_SetIterator.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 ) : false;
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() ) : false;
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;
142 TIterator( const SMESH_PredicatePtr& filter,
143 SMDS_ElemIteratorPtr& elems,
145 myPredicate( filter ),
148 myNbToFind( nbToFind ),
157 virtual const SMDS_MeshElement* next()
159 const SMDS_MeshElement* res = myNextElem;
160 myNbFound += bool( res );
162 if ( myNbFound < myNbToFind )
163 while ( myElemIt->more() && !myNextElem )
165 myNextElem = myElemIt->next();
166 if ( !myPredicate->IsSatisfy( myNextElem->GetID() ))
173 struct TEmptyIterator : public SMDS_ElemIterator
175 virtual bool more() { return false; }
176 virtual const SMDS_MeshElement* next() { return 0; }
180 //================================================================================
182 * \brief Return iterator on all elements
184 //================================================================================
186 SMDS_ElemIteratorPtr SMESHDS_GroupOnFilter::GetElements() const
188 size_t nbToFind = std::numeric_limits<size_t>::max();
190 SMDS_ElemIteratorPtr elemIt;
193 myPredicate->SetMesh( GetMesh() ); // hope myPredicate updates self here if necessary
195 elemIt = GetMesh()->elementsIterator( GetType() );
199 if ( nbToFind == GetMesh()->GetMeshInfo().NbElements( GetType() ))
200 return elemIt; // all elements are OK
201 for ( size_t i = 0; i < myNbElemToSkip; ++i )
202 elemIt->next(); // skip w/o check
207 elemIt = SMDS_ElemIteratorPtr( new TEmptyIterator );
209 return SMDS_ElemIteratorPtr ( new TIterator( myPredicate, elemIt, nbToFind ));
212 //================================================================================
214 * \brief Return info on sub-types of elements
216 //================================================================================
218 std::vector< int > SMESHDS_GroupOnFilter::GetMeshInfo() const
224 //================================================================================
226 * \brief Fill ids of elements. And return their number.
227 * \a ids must be pre-allocated using nb of elements of type == GetType()
229 //================================================================================
231 int SMESHDS_GroupOnFilter::getElementIds( void* ids, size_t idSize ) const
233 SMESHDS_GroupOnFilter* me = const_cast<SMESHDS_GroupOnFilter*>( this );
235 char* curID = (char*) ids;
236 SMDS_ElemIteratorPtr elIt = GetElements();
241 for ( ; elIt->more(); curID += idSize )
242 (*(int*) curID) = elIt->next()->GetID();
248 // find out nb of elements to skip w/o check before the 1st OK element
249 const SMDS_MeshElement* firstOkElem = me->setNbElemToSkip( elIt );
251 me->myMeshInfo.assign( SMDSEntity_Last, 0 );
252 me->myMeshInfo[ firstOkElem->GetEntityType() ]++;
254 (*(int*) curID) = firstOkElem->GetID();
255 for ( curID += idSize; elIt->more(); curID += idSize )
257 const SMDS_MeshElement* e = elIt->next();
258 (*(int*) curID) = e->GetID();
259 me->myMeshInfo[ e->GetEntityType() ]++;
263 me->setChanged( false );
265 return ( curID - (char*)ids ) / idSize;
268 //================================================================================
270 * \brief Return a value allowing to find out if a group has changed or not
272 //================================================================================
274 int SMESHDS_GroupOnFilter::GetTic() const
276 return GetMesh()->GetMTime() * myPredicateTic;
279 //================================================================================
281 * \brief Return false if update() is needed
283 //================================================================================
285 bool SMESHDS_GroupOnFilter::IsUpToDate() const
287 return !( myMeshModifTime < GetMesh()->GetMTime() );
290 //================================================================================
292 * \brief Updates myElements if necessary
294 //================================================================================
296 void SMESHDS_GroupOnFilter::update() const
298 SMESHDS_GroupOnFilter* me = const_cast<SMESHDS_GroupOnFilter*>( this );
302 SMDS_ElemIteratorPtr elIt = GetElements();
303 if ( elIt->more() ) {
304 // find out nb of elements to skip w/o check before the 1st OK element
305 const SMDS_MeshElement* e = me->setNbElemToSkip( elIt );
306 ++me->myMeshInfo[ e->GetEntityType() ];
307 while ( elIt->more() )
308 ++me->myMeshInfo[ elIt->next()->GetEntityType() ];
310 me->setChanged( false );
314 //================================================================================
316 * \brief Sets myMeshModifTime and clear fields according to modification state
318 //================================================================================
320 void SMESHDS_GroupOnFilter::setChanged(bool changed)
322 myMeshModifTime = GetMesh()->GetMTime();
323 if ( changed && myMeshModifTime != 0 )
327 myMeshInfo.assign( SMDSEntity_Last, 0 );
331 //================================================================================
333 * \brief Sets myNbElemToSkip
334 * \param okElemIt - iterator on OK elements
335 * \retval const SMDS_MeshElement* - the first OK element
337 //================================================================================
339 const SMDS_MeshElement*
340 SMESHDS_GroupOnFilter::setNbElemToSkip( SMDS_ElemIteratorPtr& okElemIt )
342 // find out nb of elements to skip w/o check before the 1st OK element
343 const SMDS_MeshElement* firstOkElem = okElemIt->next();
344 if ( myNbElemToSkip == 0 )
346 SMDS_ElemIteratorPtr elemIt = GetMesh()->elementsIterator( GetType() );
348 while ( elemIt->next() != firstOkElem )