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