Salome HOME
typo-fix by Kunda
[modules/smesh.git] / src / SMESH / SMESH_HypoFilter.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 //  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  : 
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     for ( int type = TopAbs_SOLID; type < TopAbs_SHAPE; ++type )
176       if ( aHyp->GetDim() == SMESH_Gen::GetShapeDim( TopAbs_ShapeEnum( type )))
177         for ( TopExp_Explorer exp( aShape, TopAbs_ShapeEnum( type )); exp.More(); exp.Next())
178           if ( SMESH_MesherHelper::IsSubShape( exp.Current(), /*mainShape=*/_shape ))
179             return true;
180   }
181
182   if ( _preferableShapes.Contains( aShape ))
183     return true; // issue 21559, Mesh_6
184
185   return false;
186 }
187
188 //=======================================================================
189 //function : SMESH_HypoFilter
190 //purpose  : 
191 //=======================================================================
192
193 SMESH_HypoFilter::SMESH_HypoFilter()
194   : myNbPredicates(0)
195 {
196 }
197
198 //=======================================================================
199 //function : SMESH_HypoFilter
200 //purpose  : 
201 //=======================================================================
202
203 SMESH_HypoFilter::SMESH_HypoFilter( SMESH_HypoPredicate* aPredicate, bool notNegate )
204   : myNbPredicates(0)
205 {
206   add( notNegate ? AND : AND_NOT, aPredicate );
207 }
208
209 //=======================================================================
210 //function : And
211 //purpose  : 
212 //=======================================================================
213
214 SMESH_HypoFilter & SMESH_HypoFilter::And( SMESH_HypoPredicate* aPredicate )
215 {
216   add( AND, aPredicate );
217   return *this;
218 }
219
220 //=======================================================================
221 //function : AndNot
222 //purpose  : 
223 //=======================================================================
224
225 SMESH_HypoFilter & SMESH_HypoFilter::AndNot( SMESH_HypoPredicate* aPredicate )
226 {
227   add( AND_NOT, aPredicate );
228   return *this;
229 }
230
231 //=======================================================================
232 //function : Or
233 //purpose  : 
234 //=======================================================================
235
236 SMESH_HypoFilter & SMESH_HypoFilter::Or( SMESH_HypoPredicate* aPredicate )
237 {
238   add( OR, aPredicate );
239   return *this;
240 }
241
242 //=======================================================================
243 //function : OrNot
244 //purpose  : Return predicates
245 //=======================================================================
246
247 SMESH_HypoFilter & SMESH_HypoFilter::OrNot( SMESH_HypoPredicate* aPredicate )
248 {
249   add( OR_NOT, aPredicate );
250   return *this;
251 }
252
253 //=======================================================================
254 //function : Is
255 //purpose  : 
256 //=======================================================================
257
258 SMESH_HypoPredicate* SMESH_HypoFilter::Is(const SMESH_Hypothesis* theHypo)
259 {
260   return new InstancePredicate( theHypo );
261 }
262
263 //=======================================================================
264 //function : IsAlgo
265 //purpose  : 
266 //=======================================================================
267
268 SMESH_HypoPredicate* SMESH_HypoFilter::IsAlgo()
269 {
270   return new TypePredicate( MORE, SMESHDS_Hypothesis::PARAM_ALGO );
271 }
272
273 //=======================================================================
274 //function : IsAuxiliary
275 //purpose  : 
276 //=======================================================================
277
278 SMESH_HypoPredicate* SMESH_HypoFilter::IsAuxiliary()
279 {
280   return new IsAuxiliaryPredicate();
281 }
282
283
284 //=======================================================================
285 //function : IsGlobal
286 //purpose  : 
287 //=======================================================================
288
289  SMESH_HypoPredicate* SMESH_HypoFilter::IsGlobal(const TopoDS_Shape& theMainShape)
290 {
291   return new IsAssignedToPredicate( theMainShape );
292 }
293
294 //=======================================================================
295 //function : IsAssignedTo
296 //purpose  : 
297 //=======================================================================
298
299  SMESH_HypoPredicate* SMESH_HypoFilter::IsAssignedTo(const TopoDS_Shape& theShape)
300 {
301   return new IsAssignedToPredicate( theShape );
302 }
303
304 //=======================================================================
305 //function : HasName
306 //purpose  : 
307 //=======================================================================
308
309 SMESH_HypoPredicate* SMESH_HypoFilter::HasName(const string & theName)
310 {
311   return new NamePredicate( theName );
312 }
313
314 //=======================================================================
315 //function : HasDim
316 //purpose  : 
317 //=======================================================================
318
319 SMESH_HypoPredicate* SMESH_HypoFilter::HasDim(const int theDim)
320 {
321   return new DimPredicate( EQUAL, theDim );
322 }
323
324 //=======================================================================
325 //function : IsApplicableTo
326 //purpose  : 
327 //=======================================================================
328
329 SMESH_HypoPredicate* SMESH_HypoFilter::IsApplicableTo(const TopoDS_Shape& theShape)
330 {
331   return new ApplicablePredicate( theShape );
332 }
333
334 //=======================================================================
335 //function : IsMoreLocalThan
336 //purpose  : 
337 //=======================================================================
338
339 SMESH_HypoPredicate* SMESH_HypoFilter::IsMoreLocalThan(const TopoDS_Shape& theShape,
340                                                        const SMESH_Mesh&   theMesh)
341 {
342   return new IsMoreLocalThanPredicate( theShape, theMesh );
343 }
344
345 //=======================================================================
346 //function : HasType
347 //purpose  : 
348 //=======================================================================
349
350 SMESH_HypoPredicate* SMESH_HypoFilter::HasType(const int theHypType)
351 {
352   return new TypePredicate( EQUAL, theHypType );
353 }
354
355 //=======================================================================
356 //function : IsOk
357 //purpose  : 
358 //=======================================================================
359
360 bool SMESH_HypoFilter::IsOk (const SMESH_Hypothesis* aHyp,
361                              const TopoDS_Shape&     aShape) const
362 {
363   if ( IsEmpty() )
364     return true;
365
366   bool ok = ( myPredicates[0]->_logical_op <= AND_NOT );
367   for ( int i = 0; i < myNbPredicates; ++i )
368   {
369     bool ok2 = myPredicates[i]->IsOk( aHyp, aShape );
370     switch ( myPredicates[i]->_logical_op ) {
371     case AND:     ok = ok && ok2; break;
372     case AND_NOT: ok = ok && !ok2; break;
373     case OR:      ok = ok || ok2; break;
374     case OR_NOT:  ok = ok || !ok2; break;
375     default:;
376     }
377   }
378   return ok;
379 }
380
381 //=======================================================================
382 //function : Init
383 //purpose  : 
384 //=======================================================================
385
386 SMESH_HypoFilter & SMESH_HypoFilter::Init  ( SMESH_HypoPredicate* aPredicate, bool notNegate )
387 {
388   SMESH_HypoPredicate** pred = &myPredicates[0];
389   SMESH_HypoPredicate** end  = &myPredicates[myNbPredicates];
390   for ( ; pred != end; ++pred )
391     delete *pred;
392   myNbPredicates = 0;
393
394   add( notNegate ? AND : AND_NOT, aPredicate );
395   return *this;
396 }
397
398
399 //=======================================================================
400 //function : IsOk
401 //purpose  : 
402 //=======================================================================
403
404 SMESH_HypoFilter::~SMESH_HypoFilter()
405 {
406   SMESH_HypoPredicate** pred = &myPredicates[0];
407   SMESH_HypoPredicate** end  = &myPredicates[myNbPredicates];
408   for ( ; pred != end; ++pred )
409     delete *pred;
410   myNbPredicates = 0;
411 }
412
413
414