Salome HOME
Copyright update: 2016
[modules/smesh.git] / src / SMESHDS / SMESHDS_GroupOnFilter.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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   if ( !IsUpToDate() )
296     me->setChanged();
297     
298   char* curID = (char*) ids;
299   SMDS_ElemIteratorPtr elIt = GetElements();
300   if ( elIt->more() )
301   {
302     if ( IsUpToDate() )
303     {
304       for ( ; elIt->more(); curID += idSize )
305         (*(int*) curID) = elIt->next()->GetID();
306     }
307     else
308     {
309       // find out nb of elements to skip w/o check before the 1st OK element
310       const SMDS_MeshElement* firstOkElem = me->setNbElemToSkip( elIt );
311
312       me->myMeshInfo.assign( SMDSEntity_Last, 0 );
313       me->myMeshInfo[ firstOkElem->GetEntityType() ]++;
314
315       (*(int*) curID) = firstOkElem->GetID();
316       for ( curID += idSize; elIt->more(); curID += idSize )
317       {
318         const SMDS_MeshElement* e = elIt->next();
319         (*(int*) curID) = e->GetID();
320         me->myMeshInfo[ e->GetEntityType() ]++;
321       }
322     }
323   }
324   me->setChanged( false );
325
326   return ( curID - (char*)ids ) / idSize;
327 }
328
329 //================================================================================
330 /*!
331  * \brief Return a value allowing to find out if a group has changed or not
332  */
333 //================================================================================
334
335 int SMESHDS_GroupOnFilter::GetTic() const
336 {
337   return GetMesh()->GetMTime() * myPredicateTic;
338 }
339
340 //================================================================================
341 /*!
342  * \brief Return false if update() is needed
343  */
344 //================================================================================
345
346 bool SMESHDS_GroupOnFilter::IsUpToDate() const
347 {
348   return !( myMeshModifTime < GetMesh()->GetMTime() );
349 }
350
351 //================================================================================
352 /*!
353  * \brief Updates myElements if necessary
354  */
355 //================================================================================
356
357 void SMESHDS_GroupOnFilter::update() const
358 {
359   SMESHDS_GroupOnFilter* me = const_cast<SMESHDS_GroupOnFilter*>( this );
360   if ( !IsUpToDate() )
361   {
362     me->setChanged();
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() ];
370     }
371     me->setChanged( false );
372   }
373 }
374
375 //================================================================================
376 /*!
377  * \brief Sets myMeshModifTime and clear fields according to modification state
378  */
379 //================================================================================
380
381 void SMESHDS_GroupOnFilter::setChanged(bool changed)
382 {
383   myMeshModifTime = GetMesh()->GetMTime();
384   if ( changed && myMeshModifTime != 0 )
385     --myMeshModifTime;
386   if ( changed ) {
387     clearVector( myElements );
388     myElementsOK = false;
389     myNbElemToSkip = 0;
390     myMeshInfo.assign( SMDSEntity_Last, 0 );
391   }
392 }
393
394 //================================================================================
395 /*!
396  * \brief Sets myNbElemToSkip
397  *  \param okElemIt - iterator on OK elements
398  *  \retval const SMDS_MeshElement* - the first OK element
399  */
400 //================================================================================
401
402 const SMDS_MeshElement*
403 SMESHDS_GroupOnFilter::setNbElemToSkip( SMDS_ElemIteratorPtr& okElemIt )
404 {
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 )
408   {
409     SMDS_ElemIteratorPtr elemIt = GetMesh()->elementsIterator( GetType() );
410     myNbElemToSkip = 0;
411     while ( elemIt->next() != firstOkElem )
412       ++myNbElemToSkip;
413   }
414   return firstOkElem;
415 }