Salome HOME
e783854816ff339f827357f3c6bb2a4b05a5d04e
[modules/smesh.git] / src / SMESHDS / SMESHDS_GroupOnFilter.cxx
1 // Copyright (C) 2007-2013  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 "SMDS_SetIterator.hxx"
29 #include "SMESHDS_Mesh.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 );
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() );
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, myTotalNb;
142     vector< const SMDS_MeshElement*>& myFoundElems;
143     bool &                            myFoundElemsOK;
144
145     TIterator( const SMESH_PredicatePtr&         filter,
146                SMDS_ElemIteratorPtr&             elems,
147                size_t                            nbToFind,
148                size_t                            totalNb,
149                vector< const SMDS_MeshElement*>& foundElems,
150                bool &                            foundElemsOK):
151       myPredicate( filter ),
152       myElemIt( elems ),
153       myNextElem( 0 ),
154       myNbToFind( nbToFind ),
155       myNbFound( 0 ),
156       myTotalNb( totalNb ),
157       myFoundElems( foundElems ),
158       myFoundElemsOK( foundElemsOK )
159     {
160       myFoundElemsOK = false;
161       next();
162     }
163     ~TIterator()
164     {
165       if ( !myFoundElemsOK )
166         clearVector( myFoundElems );
167     }
168     virtual bool more()
169     {
170       return myNextElem;
171     }
172     virtual const SMDS_MeshElement* next()
173     {
174       const SMDS_MeshElement* res = myNextElem;
175       myNbFound += bool( res );
176       myNextElem = 0;
177       if ( myNbFound < myNbToFind )
178       {
179         while ( myElemIt->more() && !myNextElem )
180         {
181           myNextElem = myElemIt->next();
182           if ( !myPredicate->IsSatisfy( myNextElem->GetID() ))
183             myNextElem = 0;
184         }
185         if ( myNextElem )
186           myFoundElems.push_back( myNextElem );
187         else
188           keepOrClearElemVec();
189       }
190       else
191       {
192         keepOrClearElemVec();
193       }
194       return res;
195     }
196     void keepOrClearElemVec()
197     {
198       if ( myNbFound == myTotalNb )
199       {
200         myFoundElemsOK = false; // all elems are OK, no need to keep them
201       }
202       else
203       {
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 )
208         {
209           myFoundElemsOK = true; // < 1 MB - do not clear
210         }
211         else
212         {
213           int freeRamMB = SMDS_Mesh::CheckMemory( /*doNotRaise=*/true );
214           if ( freeRamMB < 0 )
215             myFoundElemsOK = true; // hope it's OK
216           else
217             myFoundElemsOK = ( freeRamMB * aMB > 10 * vecMemSize );
218         }
219       }
220       if ( !myFoundElemsOK )
221         clearVector( myFoundElems );
222     }
223   };
224
225   struct TEmptyIterator : public SMDS_ElemIterator
226   {
227     virtual bool more()                    { return false; }
228     virtual const SMDS_MeshElement* next() { return 0; }
229   };
230 }
231
232 //================================================================================
233 /*!
234  * \brief Return iterator on all elements
235  */
236 //================================================================================
237
238 SMDS_ElemIteratorPtr SMESHDS_GroupOnFilter::GetElements() const
239 {
240   size_t nbToFind = std::numeric_limits<size_t>::max();
241   size_t totalNb  = GetMesh()->GetMeshInfo().NbElements( GetType() );
242
243   SMDS_ElemIteratorPtr elemIt; // iterator on all elements to initialize TIterator
244   if ( myPredicate )
245   {
246     myPredicate->SetMesh( GetMesh() ); // hope myPredicate updates self here if necessary
247
248     elemIt = GetMesh()->elementsIterator( GetType() );
249     if ( IsUpToDate() )
250     {
251       if ( myElementsOK )
252         return SMDS_ElemIteratorPtr( new SMDS_ElementVectorIterator( myElements.begin(),
253                                                                      myElements.end() ));
254       nbToFind = Extent();
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
259     }
260   }
261   else
262   {
263     elemIt = SMDS_ElemIteratorPtr( new TEmptyIterator );
264   }
265
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 ));
270 }
271
272 //================================================================================
273 /*!
274  * \brief Return info on sub-types of elements
275  */
276 //================================================================================
277
278 std::vector< int > SMESHDS_GroupOnFilter::GetMeshInfo() const
279 {
280   update();
281   return myMeshInfo;
282 }
283
284 //================================================================================
285 /*!
286  * \brief Fill ids of elements. And return their number.
287  *       \a ids must be pre-allocated using nb of elements of type == GetType()
288  */
289 //================================================================================
290
291 int SMESHDS_GroupOnFilter::getElementIds( void* ids, size_t idSize ) const
292 {
293   SMESHDS_GroupOnFilter* me = const_cast<SMESHDS_GroupOnFilter*>( this );
294
295   char* curID = (char*) ids;
296   SMDS_ElemIteratorPtr elIt = GetElements();
297   if ( elIt->more() )
298   {
299     if ( IsUpToDate() )
300     {
301       for ( ; elIt->more(); curID += idSize )
302         (*(int*) curID) = elIt->next()->GetID();
303     }
304     else
305     {
306       me->setChanged();
307
308       // find out nb of elements to skip w/o check before the 1st OK element
309       const SMDS_MeshElement* firstOkElem = me->setNbElemToSkip( elIt );
310
311       me->myMeshInfo.assign( SMDSEntity_Last, 0 );
312       me->myMeshInfo[ firstOkElem->GetEntityType() ]++;
313
314       (*(int*) curID) = firstOkElem->GetID();
315       for ( curID += idSize; elIt->more(); curID += idSize )
316       {
317         const SMDS_MeshElement* e = elIt->next();
318         (*(int*) curID) = e->GetID();
319         me->myMeshInfo[ e->GetEntityType() ]++;
320       }
321     }
322   }
323   me->setChanged( false );
324
325   return ( curID - (char*)ids ) / idSize;
326 }
327
328 //================================================================================
329 /*!
330  * \brief Return a value allowing to find out if a group has changed or not
331  */
332 //================================================================================
333
334 int SMESHDS_GroupOnFilter::GetTic() const
335 {
336   return GetMesh()->GetMTime() * myPredicateTic;
337 }
338
339 //================================================================================
340 /*!
341  * \brief Return false if update() is needed
342  */
343 //================================================================================
344
345 bool SMESHDS_GroupOnFilter::IsUpToDate() const
346 {
347   return !( myMeshModifTime < GetMesh()->GetMTime() );
348 }
349
350 //================================================================================
351 /*!
352  * \brief Updates myElements if necessary
353  */
354 //================================================================================
355
356 void SMESHDS_GroupOnFilter::update() const
357 {
358   SMESHDS_GroupOnFilter* me = const_cast<SMESHDS_GroupOnFilter*>( this );
359   if ( !IsUpToDate() )
360   {
361     me->setChanged();
362     SMDS_ElemIteratorPtr elIt = GetElements();
363     if ( elIt->more() ) {
364       // find out nb of elements to skip w/o check before the 1st OK element
365       const SMDS_MeshElement* e = me->setNbElemToSkip( elIt );
366       ++me->myMeshInfo[ e->GetEntityType() ];
367       while ( elIt->more() )
368         ++me->myMeshInfo[ elIt->next()->GetEntityType() ];
369     }
370     me->setChanged( false );
371   }
372 }
373
374 //================================================================================
375 /*!
376  * \brief Sets myMeshModifTime and clear fields according to modification state
377  */
378 //================================================================================
379
380 void SMESHDS_GroupOnFilter::setChanged(bool changed)
381 {
382   myMeshModifTime = GetMesh()->GetMTime();
383   if ( changed && myMeshModifTime != 0 )
384     --myMeshModifTime;
385   if ( changed ) {
386     clearVector( myElements );
387     myElementsOK = false;
388     myNbElemToSkip = 0;
389     myMeshInfo.assign( SMDSEntity_Last, 0 );
390   }
391 }
392
393 //================================================================================
394 /*!
395  * \brief Sets myNbElemToSkip
396  *  \param okElemIt - iterator on OK elements
397  *  \retval const SMDS_MeshElement* - the first OK element
398  */
399 //================================================================================
400
401 const SMDS_MeshElement*
402 SMESHDS_GroupOnFilter::setNbElemToSkip( SMDS_ElemIteratorPtr& okElemIt )
403 {
404   // find out nb of elements to skip w/o check before the 1st OK element
405   const SMDS_MeshElement* firstOkElem = okElemIt->next();
406   if ( myNbElemToSkip == 0 )
407   {
408     SMDS_ElemIteratorPtr elemIt = GetMesh()->elementsIterator( GetType() );
409     myNbElemToSkip = 0;
410     while ( elemIt->next() != firstOkElem )
411       ++myNbElemToSkip;
412   }
413   return firstOkElem;
414 }