Salome HOME
Update copyright information
[modules/smesh.git] / src / SMESH / SMESH_HypoFilter.cxx
1 // Copyright (C) 2007-2012  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 //
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 //function : IsMoreLocalThanPredicate::IsOk
126 //purpose  : 
127 //=======================================================================
128
129 bool SMESH_HypoFilter::IsMoreLocalThanPredicate::IsOk(const SMESH_Hypothesis* aHyp,
130                                                       const TopoDS_Shape&     aShape) const
131 {
132   if ( aShape.IsSame( _shapeToMesh ))
133     return false; // aHyp is global
134
135   if ( SMESH_MesherHelper::IsSubShape( aShape, /*mainShape=*/_shape ))
136     return true;
137
138   if ( aShape.ShapeType() == TopAbs_COMPOUND && 
139        !SMESH_MesherHelper::IsSubShape( _shape, /*mainShape=*/aShape)) // issue 0020963
140   {
141     for ( int type = TopAbs_SOLID; type < TopAbs_SHAPE; ++type )
142       if ( aHyp->GetDim() == SMESH_Gen::GetShapeDim( TopAbs_ShapeEnum( type )))
143         for ( TopExp_Explorer exp( aShape, TopAbs_ShapeEnum( type )); exp.More(); exp.Next())
144           if ( SMESH_MesherHelper::IsSubShape( exp.Current(), /*mainShape=*/_shape ))
145             return true;
146   }
147   return false;
148 }
149
150 //=======================================================================
151 //function : SMESH_HypoFilter
152 //purpose  : 
153 //=======================================================================
154
155 SMESH_HypoFilter::SMESH_HypoFilter()
156 {
157 }
158
159 //=======================================================================
160 //function : SMESH_HypoFilter
161 //purpose  : 
162 //=======================================================================
163
164 SMESH_HypoFilter::SMESH_HypoFilter( SMESH_HypoPredicate* aPredicate, bool notNagate )
165 {
166   add( notNagate ? AND : AND_NOT, aPredicate );
167 }
168
169 //=======================================================================
170 //function : And
171 //purpose  : 
172 //=======================================================================
173
174 SMESH_HypoFilter & SMESH_HypoFilter::And( SMESH_HypoPredicate* aPredicate )
175 {
176   add( AND, aPredicate );
177   return *this;
178 }
179
180 //=======================================================================
181 //function : AndNot
182 //purpose  : 
183 //=======================================================================
184
185 SMESH_HypoFilter & SMESH_HypoFilter::AndNot( SMESH_HypoPredicate* aPredicate )
186 {
187   add( AND_NOT, aPredicate );
188   return *this;
189 }
190
191 //=======================================================================
192 //function : Or
193 //purpose  : 
194 //=======================================================================
195
196 SMESH_HypoFilter & SMESH_HypoFilter::Or( SMESH_HypoPredicate* aPredicate )
197 {
198   add( OR, aPredicate );
199   return *this;
200 }
201
202 //=======================================================================
203 //function : OrNot
204 //purpose  : Return predicates
205 //=======================================================================
206
207 SMESH_HypoFilter & SMESH_HypoFilter::OrNot( SMESH_HypoPredicate* aPredicate )
208 {
209   add( OR_NOT, aPredicate );
210   return *this;
211 }
212
213 //=======================================================================
214 //function : Is
215 //purpose  : 
216 //=======================================================================
217
218 SMESH_HypoPredicate* SMESH_HypoFilter::Is(const SMESH_Hypothesis* theHypo)
219 {
220   return new InstancePredicate( theHypo );
221 }
222
223 //=======================================================================
224 //function : IsAlgo
225 //purpose  : 
226 //=======================================================================
227
228 SMESH_HypoPredicate* SMESH_HypoFilter::IsAlgo()
229 {
230   return new TypePredicate( MORE, SMESHDS_Hypothesis::PARAM_ALGO );
231 }
232
233 //=======================================================================
234 //function : IsAuxiliary
235 //purpose  : 
236 //=======================================================================
237
238 SMESH_HypoPredicate* SMESH_HypoFilter::IsAuxiliary()
239 {
240   return new IsAuxiliaryPredicate();
241 }
242
243
244 //=======================================================================
245 //function : IsGlobal
246 //purpose  : 
247 //=======================================================================
248
249  SMESH_HypoPredicate* SMESH_HypoFilter::IsGlobal(const TopoDS_Shape& theMainShape)
250 {
251   return new IsAssignedToPredicate( theMainShape );
252 }
253
254 //=======================================================================
255 //function : IsAssignedTo
256 //purpose  : 
257 //=======================================================================
258
259  SMESH_HypoPredicate* SMESH_HypoFilter::IsAssignedTo(const TopoDS_Shape& theShape)
260 {
261   return new IsAssignedToPredicate( theShape );
262 }
263
264 //=======================================================================
265 //function : HasName
266 //purpose  : 
267 //=======================================================================
268
269 SMESH_HypoPredicate* SMESH_HypoFilter::HasName(const string & theName)
270 {
271   return new NamePredicate( theName );
272 }
273
274 //=======================================================================
275 //function : HasDim
276 //purpose  : 
277 //=======================================================================
278
279 SMESH_HypoPredicate* SMESH_HypoFilter::HasDim(const int theDim)
280 {
281   return new DimPredicate( EQUAL, theDim );
282 }
283
284 //=======================================================================
285 //function : IsApplicableTo
286 //purpose  : 
287 //=======================================================================
288
289 SMESH_HypoPredicate* SMESH_HypoFilter::IsApplicableTo(const TopoDS_Shape& theShape)
290 {
291   return new ApplicablePredicate( theShape );
292 }
293
294 //=======================================================================
295 //function : IsMoreLocalThan
296 //purpose  : 
297 //=======================================================================
298
299 SMESH_HypoPredicate* SMESH_HypoFilter::IsMoreLocalThan(const TopoDS_Shape& theShape,
300                                                        const TopoDS_Shape& theShapeToMesh)
301 {
302   return new IsMoreLocalThanPredicate( theShape, theShapeToMesh);
303 }
304
305 //=======================================================================
306 //function : HasType
307 //purpose  : 
308 //=======================================================================
309
310 SMESH_HypoPredicate* SMESH_HypoFilter::HasType(const int theHypType)
311 {
312   return new TypePredicate( EQUAL, theHypType );
313 }
314
315 //=======================================================================
316 //function : IsOk
317 //purpose  : 
318 //=======================================================================
319
320 bool SMESH_HypoFilter::IsOk (const SMESH_Hypothesis* aHyp,
321                              const TopoDS_Shape&     aShape) const
322 {
323   if ( myPredicates.empty() )
324     return true;
325
326   bool ok = ( myPredicates.front()->_logical_op <= AND_NOT );
327   list<SMESH_HypoPredicate*>::const_iterator pred = myPredicates.begin();
328   for ( ; pred != myPredicates.end(); ++pred )
329   {
330     bool ok2 = (*pred)->IsOk( aHyp, aShape );
331     switch ( (*pred)->_logical_op ) {
332     case AND:     ok = ok && ok2; break;
333     case AND_NOT: ok = ok && !ok2; break;
334     case OR:      ok = ok || ok2; break;
335     case OR_NOT:  ok = ok || !ok2; break;
336     default:;
337     }
338   }
339   return ok;
340 }
341
342 //=======================================================================
343 //function : Init
344 //purpose  : 
345 //=======================================================================
346
347 SMESH_HypoFilter & SMESH_HypoFilter::Init  ( SMESH_HypoPredicate* aPredicate, bool notNagate )
348 {
349   list<SMESH_HypoPredicate*>::const_iterator pred = myPredicates.begin();
350   for ( ; pred != myPredicates.end(); ++pred )
351     delete *pred;
352   myPredicates.clear();
353
354   add( notNagate ? AND : AND_NOT, aPredicate );
355   return *this;
356 }
357
358
359 //=======================================================================
360 //function : IsOk
361 //purpose  : 
362 //=======================================================================
363
364 SMESH_HypoFilter::~SMESH_HypoFilter()
365 {
366   Init(0);
367 }
368
369
370