Salome HOME
#18963 Minimize compiler warnings
[modules/smesh.git] / src / SMESH / SMESH_HypoFilter.cxx
1 // Copyright (C) 2007-2020  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 //  SMESH SMESH : implementation of SMESH idl descriptions
24 //  File   : SMESH_HypoFilter.cxx
25 //  Module : SMESH
26 //
27 #include "SMESH_HypoFilter.hxx"
28
29 #include "SMESHDS_Mesh.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_Hypothesis.hxx"
32 #include "SMESH_Mesh.hxx"
33 #include "SMESH_MesherHelper.hxx"
34 #include "SMESH_subMesh.hxx"
35
36 #include <TopExp_Explorer.hxx>
37
38 using namespace std;
39
40
41 //=======================================================================
42 //function : NamePredicate::Value
43 //purpose  : 
44 //=======================================================================
45
46 bool SMESH_HypoFilter::NamePredicate::IsOk (const SMESH_Hypothesis* aHyp,
47                                             const TopoDS_Shape&     /*aShape*/ ) const
48 {
49   return ( _name == aHyp->GetName() );
50 }
51
52 //=======================================================================
53 //function : TypePredicate::Value
54 //purpose  : 
55 //=======================================================================
56
57 int SMESH_HypoFilter::TypePredicate::Value( const SMESH_Hypothesis* aHyp ) const
58 {
59   return aHyp->GetType();
60 }
61
62 //=======================================================================
63 //function : DimPredicate::Value
64 //purpose  : 
65 //=======================================================================
66
67 int SMESH_HypoFilter::DimPredicate::Value( const SMESH_Hypothesis* aHyp ) const
68 {
69   return aHyp->GetDim();
70 }
71
72 //=======================================================================
73 //function : ApplicablePredicate::IsOk
74 //purpose  : 
75 //=======================================================================
76
77 bool SMESH_HypoFilter::ApplicablePredicate::IsOk(const SMESH_Hypothesis* aHyp,
78                                                  const TopoDS_Shape&     /*aShape*/) const
79 {
80   return SMESH_subMesh::IsApplicableHypothesis( aHyp, (TopAbs_ShapeEnum)_shapeType );
81 }
82
83 //=======================================================================
84 //function : IsAuxiliaryPredicate::IsOk
85 //purpose  : 
86 //=======================================================================
87
88 bool SMESH_HypoFilter::IsAuxiliaryPredicate::IsOk(const SMESH_Hypothesis* aHyp,
89                                                   const TopoDS_Shape&     /*aShape*/) const
90 {
91   return aHyp->IsAuxiliary();
92 }
93
94 //=======================================================================
95 //function : ApplicablePredicate::ApplicablePredicate
96 //purpose  : 
97 //=======================================================================
98
99 SMESH_HypoFilter::ApplicablePredicate::ApplicablePredicate( const TopoDS_Shape& theShape )
100 {
101   _shapeType = ( theShape.IsNull() ? TopAbs_SHAPE : theShape.ShapeType());
102 }
103
104 //=======================================================================
105 //function : InstancePredicate::IsOk
106 //purpose  : 
107 //=======================================================================
108
109 bool SMESH_HypoFilter::InstancePredicate::IsOk(const SMESH_Hypothesis* aHyp,
110                                                const TopoDS_Shape&     /*aShape*/) const
111 {
112   return _hypo == aHyp;
113 }
114
115 //=======================================================================
116 //function : IsAssignedToPredicate::IsOk
117 //purpose  : 
118 //=======================================================================
119
120 bool SMESH_HypoFilter::IsAssignedToPredicate::IsOk(const SMESH_Hypothesis* /*aHyp*/,
121                                                    const TopoDS_Shape&     aShape) const
122 {
123   return ( !_mainShape.IsNull() && !aShape.IsNull() && _mainShape.IsSame( aShape ));
124 }
125
126 //================================================================================
127 /*!
128  * \brief Finds shapes preferable over _shape due to sub-mesh order
129  */
130 //================================================================================
131
132 void SMESH_HypoFilter::IsMoreLocalThanPredicate::findPreferable()
133 {
134   const int shapeID = _mesh.GetMeshDS()->ShapeToIndex( _shape );
135   const TListOfListOfInt& listOfShapeIDList = _mesh.GetMeshOrder();
136   TListOfListOfInt::const_iterator  listsIt = listOfShapeIDList.begin();
137   for ( ; listsIt != listOfShapeIDList.end(); ++listsIt )
138   {
139     const TListOfInt& idList = *listsIt;
140     TListOfInt::const_iterator idIt =
141       std::find( idList.begin(), idList.end(), shapeID );
142     if ( idIt != idList.end() && *idIt != idList.front() )
143     {
144       for ( --idIt; true; --idIt )
145       {
146         const TopoDS_Shape& shape = _mesh.GetMeshDS()->IndexToShape( *idIt );
147         if ( !shape.IsNull())
148           _preferableShapes.Add( shape );
149
150         if ( idIt == idList.begin() )
151           break;
152       }
153     }
154   }
155 }
156
157 //=======================================================================
158 //function : IsMoreLocalThanPredicate::IsOk
159 //purpose  : Check if aShape is more local than this->_shape
160 //=======================================================================
161
162 bool SMESH_HypoFilter::IsMoreLocalThanPredicate::IsOk(const SMESH_Hypothesis* aHyp,
163                                                       const TopoDS_Shape&     aShape) const
164 {
165   if ( aShape.IsSame( _mesh.GetShapeToMesh() ) ||  // aHyp is global
166        aShape.IsSame( _shape ))
167     return false;
168
169   if ( SMESH_MesherHelper::IsSubShape( aShape, /*mainShape=*/_shape ))
170     return true;
171
172   if ( aShape.ShapeType() == TopAbs_COMPOUND &&
173        !SMESH_MesherHelper::IsSubShape( _shape, /*mainShape=*/aShape)) // issue 0020963
174   {
175     // [bos#22320] compound of FACEs is MORE local than compound of SOLIDs
176     TopAbs_ShapeEnum givenType = SMESH_MesherHelper::GetGroupType( _shape );
177     TopAbs_ShapeEnum   hypType = SMESH_MesherHelper::GetGroupType( aShape );
178     if ( SMESH_Gen::GetShapeDim( givenType ) > SMESH_Gen::GetShapeDim( hypType ))
179     {
180       for ( int type = TopAbs_SOLID; type < TopAbs_SHAPE; ++type )
181         if ( aHyp->GetDim() == SMESH_Gen::GetShapeDim( TopAbs_ShapeEnum( type )))
182           for ( TopExp_Explorer exp( aShape, TopAbs_ShapeEnum( type )); exp.More(); exp.Next())
183             if ( SMESH_MesherHelper::IsSubShape( exp.Current(), /*mainShape=*/_shape ))
184               return true;
185     }
186   }
187
188   // take forced sub-mesh priority into account
189   if ( _preferableShapes.Contains( aShape ))
190     return true; // issue 21559, Mesh_6
191
192   return false;
193 }
194
195 //=======================================================================
196 //function : SMESH_HypoFilter
197 //purpose  :
198 //=======================================================================
199
200 SMESH_HypoFilter::SMESH_HypoFilter()
201   : myNbPredicates(0)
202 {
203 }
204
205 //=======================================================================
206 //function : SMESH_HypoFilter
207 //purpose  : 
208 //=======================================================================
209
210 SMESH_HypoFilter::SMESH_HypoFilter( SMESH_HypoPredicate* aPredicate, bool notNegate )
211   : myNbPredicates(0)
212 {
213   add( notNegate ? AND : AND_NOT, aPredicate );
214 }
215
216 //=======================================================================
217 //function : And
218 //purpose  : 
219 //=======================================================================
220
221 SMESH_HypoFilter & SMESH_HypoFilter::And( SMESH_HypoPredicate* aPredicate )
222 {
223   add( AND, aPredicate );
224   return *this;
225 }
226
227 //=======================================================================
228 //function : AndNot
229 //purpose  : 
230 //=======================================================================
231
232 SMESH_HypoFilter & SMESH_HypoFilter::AndNot( SMESH_HypoPredicate* aPredicate )
233 {
234   add( AND_NOT, aPredicate );
235   return *this;
236 }
237
238 //=======================================================================
239 //function : Or
240 //purpose  : 
241 //=======================================================================
242
243 SMESH_HypoFilter & SMESH_HypoFilter::Or( SMESH_HypoPredicate* aPredicate )
244 {
245   add( OR, aPredicate );
246   return *this;
247 }
248
249 //=======================================================================
250 //function : OrNot
251 //purpose  : Return predicates
252 //=======================================================================
253
254 SMESH_HypoFilter & SMESH_HypoFilter::OrNot( SMESH_HypoPredicate* aPredicate )
255 {
256   add( OR_NOT, aPredicate );
257   return *this;
258 }
259
260 //=======================================================================
261 //function : Is
262 //purpose  : 
263 //=======================================================================
264
265 SMESH_HypoPredicate* SMESH_HypoFilter::Is(const SMESH_Hypothesis* theHypo)
266 {
267   return new InstancePredicate( theHypo );
268 }
269
270 //=======================================================================
271 //function : IsAlgo
272 //purpose  : 
273 //=======================================================================
274
275 SMESH_HypoPredicate* SMESH_HypoFilter::IsAlgo()
276 {
277   return new TypePredicate( MORE, SMESHDS_Hypothesis::PARAM_ALGO );
278 }
279
280 //=======================================================================
281 //function : IsAuxiliary
282 //purpose  : 
283 //=======================================================================
284
285 SMESH_HypoPredicate* SMESH_HypoFilter::IsAuxiliary()
286 {
287   return new IsAuxiliaryPredicate();
288 }
289
290
291 //=======================================================================
292 //function : IsGlobal
293 //purpose  : 
294 //=======================================================================
295
296  SMESH_HypoPredicate* SMESH_HypoFilter::IsGlobal(const TopoDS_Shape& theMainShape)
297 {
298   return new IsAssignedToPredicate( theMainShape );
299 }
300
301 //=======================================================================
302 //function : IsAssignedTo
303 //purpose  : 
304 //=======================================================================
305
306  SMESH_HypoPredicate* SMESH_HypoFilter::IsAssignedTo(const TopoDS_Shape& theShape)
307 {
308   return new IsAssignedToPredicate( theShape );
309 }
310
311 //=======================================================================
312 //function : HasName
313 //purpose  : 
314 //=======================================================================
315
316 SMESH_HypoPredicate* SMESH_HypoFilter::HasName(const string & theName)
317 {
318   return new NamePredicate( theName );
319 }
320
321 //=======================================================================
322 //function : HasDim
323 //purpose  : 
324 //=======================================================================
325
326 SMESH_HypoPredicate* SMESH_HypoFilter::HasDim(const int theDim)
327 {
328   return new DimPredicate( EQUAL, theDim );
329 }
330
331 //=======================================================================
332 //function : IsApplicableTo
333 //purpose  : 
334 //=======================================================================
335
336 SMESH_HypoPredicate* SMESH_HypoFilter::IsApplicableTo(const TopoDS_Shape& theShape)
337 {
338   return new ApplicablePredicate( theShape );
339 }
340
341 //=======================================================================
342 //function : IsMoreLocalThan
343 //purpose  : 
344 //=======================================================================
345
346 SMESH_HypoPredicate* SMESH_HypoFilter::IsMoreLocalThan(const TopoDS_Shape& theShape,
347                                                        const SMESH_Mesh&   theMesh)
348 {
349   return new IsMoreLocalThanPredicate( theShape, theMesh );
350 }
351
352 //=======================================================================
353 //function : HasType
354 //purpose  : 
355 //=======================================================================
356
357 SMESH_HypoPredicate* SMESH_HypoFilter::HasType(const int theHypType)
358 {
359   return new TypePredicate( EQUAL, theHypType );
360 }
361
362 //=======================================================================
363 //function : IsOk
364 //purpose  : 
365 //=======================================================================
366
367 bool SMESH_HypoFilter::IsOk (const SMESH_Hypothesis* aHyp,
368                              const TopoDS_Shape&     aShape) const
369 {
370   if ( IsEmpty() )
371     return true;
372
373   bool ok = ( myPredicates[0]->_logical_op <= AND_NOT );
374   for ( int i = 0; i < myNbPredicates; ++i )
375   {
376     bool ok2 = myPredicates[i]->IsOk( aHyp, aShape );
377     switch ( myPredicates[i]->_logical_op ) {
378     case AND:     ok = ok && ok2; break;
379     case AND_NOT: ok = ok && !ok2; break;
380     case OR:      ok = ok || ok2; break;
381     case OR_NOT:  ok = ok || !ok2; break;
382     default:;
383     }
384   }
385   return ok;
386 }
387
388 //=======================================================================
389 //function : Init
390 //purpose  : 
391 //=======================================================================
392
393 SMESH_HypoFilter & SMESH_HypoFilter::Init  ( SMESH_HypoPredicate* aPredicate, bool notNegate )
394 {
395   SMESH_HypoPredicate** pred = &myPredicates[0];
396   SMESH_HypoPredicate** end  = &myPredicates[myNbPredicates];
397   for ( ; pred != end; ++pred )
398     delete *pred;
399   myNbPredicates = 0;
400
401   add( notNegate ? AND : AND_NOT, aPredicate );
402   return *this;
403 }
404
405
406 //=======================================================================
407 //function : IsOk
408 //purpose  : 
409 //=======================================================================
410
411 SMESH_HypoFilter::~SMESH_HypoFilter()
412 {
413   SMESH_HypoPredicate** pred = &myPredicates[0];
414   SMESH_HypoPredicate** end  = &myPredicates[myNbPredicates];
415   for ( ; pred != end; ++pred )
416     delete *pred;
417   myNbPredicates = 0;
418 }
419
420
421