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