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