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