Salome HOME
Merge from V6_5_BR 05/06/2012
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Filter.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 // SMESHGUI_Filter : Filters for VTK viewer
24 // File   : SMESHGUI_Filter.cxx
25 // Author : Sergey LITONIN, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_Filter.h"
29
30 #include "SMESHGUI_Utils.h"
31
32 #include <SMESH_Actor.h>
33 #include <SMDS_Mesh.hxx>
34 #include <SMDSAbs_ElementType.hxx>
35
36 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_Filter, VTKViewer_Filter)
37 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_Filter, VTKViewer_Filter)
38
39 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_PredicateFilter, SMESHGUI_Filter)
40 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_PredicateFilter, SMESHGUI_Filter)
41
42 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_QuadrangleFilter, SMESHGUI_Filter)
43 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_QuadrangleFilter, SMESHGUI_Filter)
44
45 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_TriangleFilter, SMESHGUI_Filter)
46 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_TriangleFilter, SMESHGUI_Filter)
47
48 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_FacesFilter, SMESHGUI_Filter)
49 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_FacesFilter, SMESHGUI_Filter)
50
51 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_VolumesFilter, SMESHGUI_Filter)
52 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_VolumesFilter, SMESHGUI_Filter)
53
54 /*
55   Class       : SMESHGUI_PredicateFilter
56   Description : Selection filter for VTK viewer. This class aggregate object
57                 of SMESH_Predicate class and uses it for verification of criterion
58 */
59
60 //=======================================================================
61 // name    : SMESHGUI_PredicateFilter::SMESHGUI_PredicateFilter
62 // Purpose : Constructor
63 //=======================================================================
64 SMESHGUI_PredicateFilter::SMESHGUI_PredicateFilter()
65 {
66 }
67
68 SMESHGUI_PredicateFilter::~SMESHGUI_PredicateFilter()
69 {
70 }
71
72 //=======================================================================
73 // name    : SMESHGUI_PredicateFilter::IsValid
74 // Purpose : Verify whether entry id satisfies to criterion of the filter
75 //=======================================================================
76 bool SMESHGUI_PredicateFilter::IsValid( const int theCellId ) const
77 {
78   if ( myActor == 0 || myPred->_is_nil() )
79     return false;
80
81   SMESH_Actor* anActor = dynamic_cast<SMESH_Actor*>( myActor );
82   if ( !anActor || anActor->GetObject() == 0 )
83     return false;
84
85   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
86   SMDSAbs_ElementType anElemType = (SMDSAbs_ElementType)myPred->GetElementType();
87   int aMeshId = anElemType == SMDSAbs_Node ? anActor->GetNodeObjId( theCellId )
88                                            : anActor->GetElemObjId( theCellId );
89
90   // if type of element != type of predicate return true because
91   // this predicate is not intended for filtering sush elements
92   const SMDS_MeshElement* anElem = anElemType == SMDSAbs_Node ? aMesh->FindNode( aMeshId )
93                                                               : aMesh->FindElement( aMeshId );
94   // here we guess that predicate element type can not be All in case of node selection
95   if ( !anElem || (anElemType != SMDSAbs_All && anElem->GetType() != anElemType) )
96     return false;
97   
98   return myPred->IsSatisfy( aMeshId );
99 }
100
101 //=======================================================================
102 // name    : SMESHGUI_PredicateFilter::IsValid
103 // Purpose : Verify whether entry id satisfies to criterion of the filter
104 //=======================================================================
105 bool SMESHGUI_PredicateFilter::IsObjValid( const int theObjId ) const
106 {
107   if ( myActor == 0 || myPred->_is_nil() )
108     return false;
109
110   SMESH_Actor* anActor = dynamic_cast< SMESH_Actor* >( myActor );
111   if ( !anActor || anActor->GetObject() == 0 )
112     return false;
113
114   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
115   SMDSAbs_ElementType anElemType = (SMDSAbs_ElementType)myPred->GetElementType();
116
117   // if type of element != type of predicate return true because
118   // this predicate is not intended for filtering sush elements
119   const SMDS_MeshElement* anElem = anElemType == SMDSAbs_Node ? aMesh->FindNode( theObjId )
120                                                               : aMesh->FindElement( theObjId );
121   // here we guess that predicate element type can not be All in case of node selection
122   if ( !anElem || (anElemType != SMDSAbs_All && anElem->GetType() != anElemType) )
123     return false;
124
125   return myPred->IsSatisfy( theObjId );
126 }
127
128 //=======================================================================
129 // name    : SMESHGUI_PredicateFilter::IsNodeFilter
130 // Purpose : Returns true if filter is intended for nodes
131 //=======================================================================
132 bool SMESHGUI_PredicateFilter::IsNodeFilter() const
133 {
134   return GetId() == SMESH::NodeFilter;
135 }
136
137 //=======================================================================
138 // name    : SMESHGUI_PredicateFilter::SetPredicate
139 // Purpose : Set new pridicate to the filter
140 //=======================================================================
141 void SMESHGUI_PredicateFilter::SetPredicate( SMESH::Predicate_ptr thePred )
142 {
143   myPred = SMESH::Predicate::_duplicate( thePred );
144 }
145
146 //=======================================================================
147 // name    : SMESHGUI_PredicateFilter::SetActor
148 // Purpose : Set new actor
149 //=======================================================================
150 void SMESHGUI_PredicateFilter::SetActor( SALOME_Actor* theActor )
151 {
152   if ( myActor == theActor )
153     return;
154   SMESHGUI_Filter::SetActor( theActor );
155
156   if ( myActor != 0 && !myPred->_is_nil() )
157   {
158     SALOME_Actor* sActor = dynamic_cast<SALOME_Actor*>( myActor );
159     Handle(SALOME_InteractiveObject) anIO;
160     if( sActor )
161       anIO = sActor->getIO();
162     if ( !anIO.IsNull() )
163     {
164       SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIO);
165       if(!aMesh->_is_nil())
166         myPred->SetMesh(aMesh);
167     }
168   }
169 }
170
171 //=======================================================================
172 // name    : SMESHGUI_PredicateFilter::SetActor
173 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
174 //           enumeration. All filters must have different ids
175 //=======================================================================
176 int SMESHGUI_PredicateFilter::GetId() const
177 {
178   if      ( myPred->GetElementType() == SMESH::NODE   ) return SMESH::NodeFilter;
179   else if ( myPred->GetElementType() == SMESH::EDGE   ) return SMESH::EdgeFilter;
180   else if ( myPred->GetElementType() == SMESH::FACE   ) return SMESH::FaceFilter;
181   else if ( myPred->GetElementType() == SMESH::VOLUME ) return SMESH::VolumeFilter;
182   else if ( myPred->GetElementType() == SMESH::ALL    ) return SMESH::AllElementsFilter;
183   else                                                  return SMESH::UnknownFilter;
184 }
185
186
187 /*
188   Class       : SMESHGUI_QuadrangleFilter
189   Description : Verify whether selected cell is quadranle
190 */
191
192 //=======================================================================
193 // name    : SMESHGUI_QuadrangleFilter::SMESHGUI_QuadrangleFilter
194 // Purpose : Constructor
195 //=======================================================================
196 SMESHGUI_QuadrangleFilter::SMESHGUI_QuadrangleFilter()
197 : SMESHGUI_Filter()
198 {
199 }
200
201 SMESHGUI_QuadrangleFilter::~SMESHGUI_QuadrangleFilter()
202 {
203 }
204
205 //=======================================================================
206 // name    : SMESHGUI_QuadrangleFilter::IsValid
207 // Purpose : Verify whether selected cell is quadranle
208 //=======================================================================
209 bool SMESHGUI_QuadrangleFilter::IsValid( const int theCellId ) const
210 {
211   if ( myActor == 0 )
212     return false;
213
214   SMESH_Actor* anActor = dynamic_cast< SMESH_Actor* >( myActor );
215   if ( !anActor || anActor->GetObject() == 0 )
216     return false;
217
218   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
219   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
220
221   return anElem && anElem->GetType() == SMDSAbs_Face &&
222     ( anElem->NbNodes() == ( anElem->IsQuadratic() ? 8 : 4 ));
223 }
224
225 //=======================================================================
226 // name    : SMESHGUI_QuadrangleFilter::IsValid
227 // Purpose : Verify whether selected cell is quadranle
228 //=======================================================================
229 bool SMESHGUI_QuadrangleFilter::IsObjValid( const int theObjId ) const
230 {
231   if ( myActor == 0 )
232     return false;
233
234   SMESH_Actor* anActor = dynamic_cast< SMESH_Actor* >( myActor );
235   if ( !anActor || anActor->GetObject() == 0 )
236     return false;
237
238   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
239   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
240
241   return anElem && anElem->GetType() == SMDSAbs_Face &&
242     ( anElem->NbNodes() == ( anElem->IsQuadratic() ? 8  : 4 ));
243 }
244
245 //=======================================================================
246 // name    : SMESHGUI_QuadrangleFilter::SetActor
247 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
248 //           enumeration. All filters must have different ids
249 //=======================================================================
250 int SMESHGUI_QuadrangleFilter::GetId() const
251 {
252   return SMESH::QuadFilter;
253 }
254
255 //=======================================================================
256 // name    : SMESHGUI_QuadrangleFilter::IsNodeFilter
257 // Purpose : Returns true if filter is intended for nodes
258 //=======================================================================
259 bool SMESHGUI_QuadrangleFilter::IsNodeFilter() const
260 {
261   return false;
262 }
263
264
265 /*
266   Class       : SMESHGUI_TriangleFilter
267   Description : Verify whether selected cell is triangle
268 */
269
270
271 //=======================================================================
272 // name    : SMESHGUI_TriangleFilter::SMESHGUI_TriangleFilter
273 // Purpose : Constructor
274 //=======================================================================
275 SMESHGUI_TriangleFilter::SMESHGUI_TriangleFilter()
276 : SMESHGUI_Filter()
277 {
278 }
279
280 SMESHGUI_TriangleFilter::~SMESHGUI_TriangleFilter()
281 {
282 }
283
284 //=======================================================================
285 // name    : SMESHGUI_TriangleFilter::IsValid
286 // Purpose : Verify whether selected cell is triangle
287 //=======================================================================
288 bool SMESHGUI_TriangleFilter::IsValid( const int theCellId ) const
289 {
290   if ( myActor == 0 )
291     return false;
292
293   SMESH_Actor* anActor = dynamic_cast< SMESH_Actor* >( myActor );
294   if ( !anActor || anActor->GetObject() == 0 )
295     return false;
296
297   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
298   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
299
300   return anElem && anElem->GetType() == SMDSAbs_Face &&
301     ( anElem->NbNodes() == ( anElem->IsQuadratic() ? 6  : 3 ));
302 }
303
304 //=======================================================================
305 // name    : SMESHGUI_TriangleFilter::IsValid
306 // Purpose : Verify whether selected cell is triangle
307 //=======================================================================
308 bool SMESHGUI_TriangleFilter::IsObjValid( const int theObjId ) const
309 {
310   if ( myActor == 0 )
311     return false;
312
313   SMESH_Actor* anActor = dynamic_cast< SMESH_Actor* >( myActor );
314   if ( !anActor || anActor->GetObject() == 0 )
315     return false;
316
317   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
318   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
319
320   return anElem && anElem->GetType() == SMDSAbs_Face &&
321     ( anElem->NbNodes() == ( anElem->IsQuadratic() ? 6  : 3 ));
322 }
323
324 //=======================================================================
325 // name    : SMESHGUI_TriangleFilter::SetActor
326 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
327 //           enumeration. All filters must have different ids
328 //=======================================================================
329 int SMESHGUI_TriangleFilter::GetId() const
330 {
331   return SMESH::TriaFilter;
332 }
333
334 //=======================================================================
335 // name    : SMESHGUI_TriangleFilter::IsNodeFilter
336 // Purpose : Returns true if filter is intended for nodes
337 //=======================================================================
338 bool SMESHGUI_TriangleFilter::IsNodeFilter() const
339 {
340   return false;
341 }
342
343 /*
344   Class       : SMESHGUI_FacesFilter
345   Description : Verify whether selected cell is any face
346 */
347
348
349 //=======================================================================
350 // name    : SMESHGUI_FacesFilter::SMESHGUI_FacesFilter
351 // Purpose : Constructor
352 //=======================================================================
353 SMESHGUI_FacesFilter::SMESHGUI_FacesFilter()
354 : SMESHGUI_Filter()
355 {
356 }
357
358 SMESHGUI_FacesFilter::~SMESHGUI_FacesFilter()
359 {
360 }
361
362 //=======================================================================
363 // name    : SMESHGUI_FacesFilter::IsValid
364 // Purpose : Verify whether selected cell is face
365 //=======================================================================
366 bool SMESHGUI_FacesFilter::IsValid( const int theCellId ) const
367 {
368   if ( myActor == 0 )
369     return false;
370
371   SMESH_Actor* anActor = dynamic_cast< SMESH_Actor* >( myActor );
372   if ( !anActor || anActor->GetObject() == 0 )
373     return false;
374
375   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
376   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
377
378   return anElem && anElem->GetType() == SMDSAbs_Face;
379 }
380
381 //=======================================================================
382 // name    : SMESHGUI_FacesFilter::IsValid
383 // Purpose : Verify whether selected cell is face
384 //=======================================================================
385 bool SMESHGUI_FacesFilter::IsObjValid( const int theObjId ) const
386 {
387   if ( myActor == 0 )
388     return false;
389
390   SMESH_Actor* anActor = dynamic_cast< SMESH_Actor* >( myActor );
391   if ( !anActor || anActor->GetObject() == 0 )
392     return false;
393
394   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
395   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
396
397   return anElem && anElem->GetType() == SMDSAbs_Face;
398 }
399
400 //=======================================================================
401 // name    : SMESHGUI_FacesFilter::GetId
402 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
403 //           enumeration. All filters must have different ids
404 //=======================================================================
405 int SMESHGUI_FacesFilter::GetId() const
406 {
407   return SMESH::FaceFilter;
408 }
409
410 //=======================================================================
411 // name    : SMESHGUI_FacesFilter::IsNodeFilter
412 // Purpose : Returns true if filter is intended for nodes
413 //=======================================================================
414 bool SMESHGUI_FacesFilter::IsNodeFilter() const
415 {
416   return false;
417 }
418
419
420 /*
421   Class       : SMESHGUI_VolumesFilter
422   Description : Verify whether selected cell is any volume
423 */
424
425
426 //=======================================================================
427 // name    : SMESHGUI_VolumesFilter::SMESHGUI_VolumesFilter
428 // Purpose : Constructor
429 //=======================================================================
430 SMESHGUI_VolumesFilter::SMESHGUI_VolumesFilter()
431 : SMESHGUI_Filter()
432 {
433 }
434
435 SMESHGUI_VolumesFilter::~SMESHGUI_VolumesFilter()
436 {
437 }
438
439 //=======================================================================
440 // name    : SMESHGUI_VolumesFilter::IsValid
441 // Purpose : Verify whether selected cell is volume
442 //=======================================================================
443 bool SMESHGUI_VolumesFilter::IsValid( const int theCellId ) const
444 {
445   if ( myActor == 0 || theCellId < 1 )
446     return false;
447
448   SMESH_Actor* anActor = dynamic_cast< SMESH_Actor* >( myActor );
449   if ( !anActor || anActor->GetObject() == 0 )
450     return false;
451
452   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
453   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
454
455   return anElem && anElem->GetType() == SMDSAbs_Volume;
456 }
457
458 //=======================================================================
459 // name    : SMESHGUI_VolumesFilter::IsValid
460 // Purpose : Verify whether selected cell is volume
461 //=======================================================================
462 bool SMESHGUI_VolumesFilter::IsObjValid( const int theObjId ) const
463 {
464   if ( myActor == 0 )
465     return false;
466
467   SMESH_Actor* anActor = dynamic_cast< SMESH_Actor* >( myActor );
468   if ( !anActor || anActor->GetObject() == 0 )
469     return false;
470
471   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
472   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
473
474   return anElem && anElem->GetType() == SMDSAbs_Volume;
475 }
476
477 //=======================================================================
478 // name    : SMESHGUI_VolumesFilter::GetId
479 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
480 //           enumeration. All filters must have different ids
481 //=======================================================================
482 int SMESHGUI_VolumesFilter::GetId() const
483 {
484   return SMESH::VolumeFilter;
485 }
486
487 //=======================================================================
488 // name    : SMESHGUI_VolumesFilter::IsNodeFilter
489 // Purpose : Returns true if filter is intended for nodes
490 //=======================================================================
491 bool SMESHGUI_VolumesFilter::IsNodeFilter() const
492 {
493   return false;
494 }