Salome HOME
Merge from V6_main 11/02/2013
[modules/smesh.git] / src / SMESHDS / SMESHDS_GroupOnFilter.cxx
1 // Copyright (C) 2007-2012  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
23 //  File   : SMESHDS_GroupOnFilter.cxx
24 //  Module : SMESH
25 //
26 #include "SMESHDS_GroupOnFilter.hxx"
27
28 #include "SMESHDS_Mesh.hxx"
29 #include "SMDS_SetIterator.hxx"
30
31 #include <numeric>
32 #include <limits>
33
34 using namespace std;
35
36 //=============================================================================
37 /*!
38  * Creates a group based on thePredicate
39  */
40 //=============================================================================
41
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 ),
48     myMeshModifTime(0),
49     myPredicateTic(0),
50     myNbElemToSkip(0)
51 {
52   SetPredicate( thePredicate );
53 }
54
55 //================================================================================
56 /*!
57  * \brief Sets a new predicate
58  */
59 //================================================================================
60
61 void SMESHDS_GroupOnFilter::SetPredicate( const SMESH_PredicatePtr& thePredicate )
62 {
63   myPredicate = thePredicate;
64   ++myPredicateTic;
65   setChanged();
66   if ( myPredicate )
67     myPredicate->SetMesh( GetMesh() );
68 }
69
70 //================================================================================
71 /*!
72  * \brief Returns nb of elements
73  */
74 //================================================================================
75
76 int SMESHDS_GroupOnFilter::Extent() const
77 {
78   update();
79   return std::accumulate( myMeshInfo.begin(), myMeshInfo.end(), 0 );
80 }
81
82 //================================================================================
83 /*!
84  * \brief Checks emptyness
85  */
86 //================================================================================
87
88 bool SMESHDS_GroupOnFilter::IsEmpty()
89 {
90   if ( IsUpToDate() )
91   {
92     return ( Extent() == 0 );
93   }
94   else // not up-to-date
95   {
96     setChanged();
97     SMDS_ElemIteratorPtr okElemIt = GetElements();
98     if ( !okElemIt->more() )
99     {
100       // no satisfying elements
101       setChanged( false );
102     }
103     else
104     {
105       return false;
106     }
107   }
108   return true;
109 }
110
111 //================================================================================
112 /*!
113  * \brief Checks if the element belongs to the group
114  */
115 //================================================================================
116
117 bool SMESHDS_GroupOnFilter::Contains (const int theID)
118 {
119   return myPredicate ? myPredicate->IsSatisfy( theID ) : false;
120 }
121
122 //================================================================================
123 /*!
124  * \brief Checks if the element belongs to the group
125  */
126 //================================================================================
127
128 bool SMESHDS_GroupOnFilter::Contains (const SMDS_MeshElement* elem)
129 {
130   return myPredicate ? myPredicate->IsSatisfy( elem->GetID() ) : false;
131 }
132
133 //================================================================================
134 namespace // Iterator
135 {
136   struct TIterator : public SMDS_ElemIterator
137   {
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,
144                size_t                    nbToFind):
145       myPredicate( filter ),
146       myElemIt( elems ),
147       myNextElem( 0 ),
148       myNbToFind( nbToFind ),
149       myNbFound( 0 )
150     {
151       next();
152     }
153     virtual bool more()
154     {
155       return myNextElem;
156     }
157     virtual const SMDS_MeshElement* next()
158     {
159       const SMDS_MeshElement* res = myNextElem;
160       myNbFound += bool( res );
161       myNextElem = 0;
162       if ( myNbFound < myNbToFind )
163         while ( myElemIt->more() && !myNextElem )
164         {
165           myNextElem = myElemIt->next();
166           if ( !myPredicate->IsSatisfy( myNextElem->GetID() ))
167             myNextElem = 0;
168         }
169       return res;
170     }
171   };
172
173   struct TEmptyIterator : public SMDS_ElemIterator
174   {
175     virtual bool more()                    { return false; }
176     virtual const SMDS_MeshElement* next() { return 0; }
177   };
178 }
179
180 //================================================================================
181 /*!
182  * \brief Return iterator on all elements
183  */
184 //================================================================================
185
186 SMDS_ElemIteratorPtr SMESHDS_GroupOnFilter::GetElements() const
187 {
188   size_t nbToFind = std::numeric_limits<size_t>::max();
189
190   SMDS_ElemIteratorPtr elemIt;
191   if ( myPredicate )
192   {
193     myPredicate->SetMesh( GetMesh() ); // hope myPredicate updates self here if necessary
194
195     elemIt = GetMesh()->elementsIterator( GetType() );
196     if ( IsUpToDate() )
197     {
198       nbToFind = Extent();
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
203     }
204   }
205   else
206   {
207     elemIt = SMDS_ElemIteratorPtr( new TEmptyIterator );
208   }
209   return SMDS_ElemIteratorPtr ( new TIterator( myPredicate, elemIt, nbToFind ));
210 }
211
212 //================================================================================
213 /*!
214  * \brief Return info on sub-types of elements
215  */
216 //================================================================================
217
218 std::vector< int > SMESHDS_GroupOnFilter::GetMeshInfo() const
219 {
220   update();
221   return myMeshInfo;
222 }
223
224 //================================================================================
225 /*!
226  * \brief Fill ids of elements. And return their number.
227  *       \a ids must be pre-allocated using nb of elements of type == GetType()
228  */
229 //================================================================================
230
231 int SMESHDS_GroupOnFilter::getElementIds( void* ids, size_t idSize ) const
232 {
233   SMESHDS_GroupOnFilter* me = const_cast<SMESHDS_GroupOnFilter*>( this );
234
235   char* curID = (char*) ids;
236   SMDS_ElemIteratorPtr elIt = GetElements();
237   if ( elIt->more() )
238   {
239     if ( IsUpToDate() )
240     {
241       for ( ; elIt->more(); curID += idSize )
242         (*(int*) curID) = elIt->next()->GetID();
243     }
244     else
245     {
246       me->setChanged();
247
248       // find out nb of elements to skip w/o check before the 1st OK element
249       const SMDS_MeshElement* firstOkElem = me->setNbElemToSkip( elIt );
250
251       me->myMeshInfo.assign( SMDSEntity_Last, 0 );
252       me->myMeshInfo[ firstOkElem->GetEntityType() ]++;
253
254       (*(int*) curID) = firstOkElem->GetID();
255       for ( curID += idSize; elIt->more(); curID += idSize )
256       {
257         const SMDS_MeshElement* e = elIt->next();
258         (*(int*) curID) = e->GetID();
259         me->myMeshInfo[ e->GetEntityType() ]++;
260       }
261     }
262   }
263   me->setChanged( false );
264
265   return ( curID - (char*)ids ) / idSize;
266 }
267
268 //================================================================================
269 /*!
270  * \brief Return a value allowing to find out if a group has changed or not
271  */
272 //================================================================================
273
274 int SMESHDS_GroupOnFilter::GetTic() const
275 {
276   return GetMesh()->GetMTime() * myPredicateTic;
277 }
278
279 //================================================================================
280 /*!
281  * \brief Return false if update() is needed
282  */
283 //================================================================================
284
285 bool SMESHDS_GroupOnFilter::IsUpToDate() const
286 {
287   return !( myMeshModifTime < GetMesh()->GetMTime() );
288 }
289
290 //================================================================================
291 /*!
292  * \brief Updates myElements if necessary
293  */
294 //================================================================================
295
296 void SMESHDS_GroupOnFilter::update() const
297 {
298   SMESHDS_GroupOnFilter* me = const_cast<SMESHDS_GroupOnFilter*>( this );
299   if ( !IsUpToDate() )
300   {
301     me->setChanged();
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() ];
309     }
310     me->setChanged( false );
311   }
312 }
313
314 //================================================================================
315 /*!
316  * \brief Sets myMeshModifTime and clear fields according to modification state
317  */
318 //================================================================================
319
320 void SMESHDS_GroupOnFilter::setChanged(bool changed)
321 {
322   myMeshModifTime = GetMesh()->GetMTime();
323   if ( changed && myMeshModifTime != 0 )
324     --myMeshModifTime;
325   if ( changed ) {
326     myNbElemToSkip = 0;
327     myMeshInfo.assign( SMDSEntity_Last, 0 );
328   }
329 }
330
331 //================================================================================
332 /*!
333  * \brief Sets myNbElemToSkip
334  *  \param okElemIt - iterator on OK elements
335  *  \retval const SMDS_MeshElement* - the first OK element
336  */
337 //================================================================================
338
339 const SMDS_MeshElement*
340 SMESHDS_GroupOnFilter::setNbElemToSkip( SMDS_ElemIteratorPtr& okElemIt )
341 {
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 )
345   {
346     SMDS_ElemIteratorPtr elemIt = GetMesh()->elementsIterator( GetType() );
347     myNbElemToSkip = 0;
348     while ( elemIt->next() != firstOkElem )
349       ++myNbElemToSkip;
350   }
351   return firstOkElem;
352 }