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