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