Salome HOME
PAL6938
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Filter.cxx
1 //  SMESHGUI_PredicateFilter : Filters for VTK viewer
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : SMESHGUI_Filter.cxx
25 //  Author : Sergey LITONIN
26 //  Module : SMESH
27
28 #include "SMESHGUI_Filter.h"
29 #include "SMESHGUI_Utils.h"
30 #include "SMDS_Mesh.hxx"
31 #include "SMDS_MeshElement.hxx"
32 #include "SMDSAbs_ElementType.hxx"
33
34 #include <vtkCell.h>
35
36 #include <gp_Vec.hxx>
37 #include <Precision.hxx>
38 #include "SMESH_Actor.h"
39 #include "SMESHGUI.h"
40
41 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_Filter, VTKViewer_Filter)
42 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_Filter, VTKViewer_Filter)
43
44 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_PredicateFilter, SMESHGUI_Filter)
45 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_PredicateFilter, SMESHGUI_Filter)
46
47 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_QuadrangleFilter, SMESHGUI_Filter)
48 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_QuadrangleFilter, SMESHGUI_Filter)
49
50 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_TriangleFilter, SMESHGUI_Filter)
51 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_TriangleFilter, SMESHGUI_Filter)
52
53 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_FacesFilter, SMESHGUI_Filter)
54 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_FacesFilter, SMESHGUI_Filter)
55
56 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_VolumesFilter, SMESHGUI_Filter)
57 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_VolumesFilter, SMESHGUI_Filter)
58
59 /*
60   Class       : SMESHGUI_PredicateFilter
61   Description : Selection filter for VTK viewer. This class aggregate object
62                 of SMESH_Predicate class and uses it for verification of criterion
63 */
64
65 //=======================================================================
66 // name    : SMESHGUI_PredicateFilter::SMESHGUI_PredicateFilter
67 // Purpose : Constructor
68 //=======================================================================
69 SMESHGUI_PredicateFilter::SMESHGUI_PredicateFilter()
70 {
71 }
72
73 SMESHGUI_PredicateFilter::~SMESHGUI_PredicateFilter()
74 {
75 }
76
77 //=======================================================================
78 // name    : SMESHGUI_PredicateFilter::IsValid
79 // Purpose : Verify whether entry id satisfies to criterion of the filter
80 //=======================================================================
81 bool SMESHGUI_PredicateFilter::IsValid( const int theCellId ) const
82 {
83   if ( myActor == 0 || myPred->_is_nil() )
84     return false;
85
86   SMESH_Actor* anActor = dynamic_cast<SMESH_Actor*>( myActor );
87   if ( !anActor || anActor->GetObject() == 0 )
88     return false;
89   
90   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
91   SMESH::ElementType anElemType = myPred->GetElementType();
92   int aMeshId = anElemType == SMESH::NODE ? anActor->GetNodeObjId( theCellId )
93                                           : anActor->GetElemObjId( theCellId );
94
95   // if type of element != type of predicate return true because
96   // this predicate is not intended for filtering sush elements
97   const SMDS_MeshElement* anElem = anElemType == SMESH::NODE ? aMesh->FindNode( aMeshId )
98                                                              : aMesh->FindElement( aMeshId );
99   if ( anElem != 0 && anElem->GetType() != (SMDSAbs_ElementType)myPred->GetElementType() )
100     return true;
101   
102   return myPred->IsSatisfy( aMeshId );
103 }
104
105 //=======================================================================
106 // name    : SMESHGUI_PredicateFilter::IsValid
107 // Purpose : Verify whether entry id satisfies to criterion of the filter
108 //=======================================================================
109 bool SMESHGUI_PredicateFilter::IsObjValid( const int theObjId ) const
110 {
111   if ( myActor == 0 || myPred->_is_nil() )
112     return false;
113
114   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
115   if ( anActor->GetObject() == 0 )
116     return false;
117   
118   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
119   SMESH::ElementType anElemType = myPred->GetElementType();
120   
121   // if type of element != type of predicate return true because
122   // this predicate is not intended for filtering sush elements
123   const SMDS_MeshElement* anElem = anElemType == SMESH::NODE ? aMesh->FindNode( theObjId )
124                                                              : aMesh->FindElement( theObjId );
125   if ( anElem != 0 && anElem->GetType() != (SMDSAbs_ElementType)myPred->GetElementType() )
126     return true;
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() == SMESHGUI_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     Handle(SALOME_InteractiveObject) anIO = myActor->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 SMESHGUI_NodeFilter;
179   else if ( myPred->GetElementType() == SMESH::EDGE   ) return SMESHGUI_EdgeFilter;
180   else if ( myPred->GetElementType() == SMESH::FACE   ) return SMESHGUI_FaceFilter;
181   else if ( myPred->GetElementType() == SMESH::VOLUME ) return SMESHGUI_VolumeFilter;
182   else if ( myPred->GetElementType() == SMESH::ALL    ) return SMESHGUI_AllElementsFilter;
183   else                                                  return SMESHGUI_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 = ( SMESH_Actor* )myActor;
215   if ( 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 && anElem->NbNodes() == 4;
222 }
223
224 //=======================================================================
225 // name    : SMESHGUI_QuadrangleFilter::IsValid
226 // Purpose : Verify whether selected cell is quadranle
227 //=======================================================================
228 bool SMESHGUI_QuadrangleFilter::IsObjValid( const int theObjId ) const
229 {
230   if ( myActor == 0 )
231     return false;
232
233   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
234   if ( anActor->GetObject() == 0 )
235     return false;
236   
237   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
238   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
239
240   return anElem && anElem->GetType() == SMDSAbs_Face && anElem->NbNodes() == 4;
241 }
242
243 //=======================================================================
244 // name    : SMESHGUI_QuadrangleFilter::SetActor
245 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
246 //           enumeration. All filters must have different ids
247 //=======================================================================
248 int SMESHGUI_QuadrangleFilter::GetId() const
249 {
250   return SMESHGUI_QuadFilter;
251 }
252
253 //=======================================================================
254 // name    : SMESHGUI_QuadrangleFilter::IsNodeFilter
255 // Purpose : Returns true if filter is intended for nodes
256 //=======================================================================
257 bool SMESHGUI_QuadrangleFilter::IsNodeFilter() const
258 {
259   return false;
260 }
261
262
263 /*
264   Class       : SMESHGUI_TriangleFilter
265   Description : Verify whether selected cell is triangle
266 */
267
268
269 //=======================================================================
270 // name    : SMESHGUI_TriangleFilter::SMESHGUI_TriangleFilter
271 // Purpose : Constructor
272 //=======================================================================
273 SMESHGUI_TriangleFilter::SMESHGUI_TriangleFilter()
274 : SMESHGUI_Filter()
275 {
276 }
277
278 SMESHGUI_TriangleFilter::~SMESHGUI_TriangleFilter()
279 {
280 }
281
282 //=======================================================================
283 // name    : SMESHGUI_TriangleFilter::IsValid
284 // Purpose : Verify whether selected cell is triangle
285 //=======================================================================
286 bool SMESHGUI_TriangleFilter::IsValid( const int theCellId ) const
287 {
288   if ( myActor == 0 )
289     return false;
290
291   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
292   if ( anActor->GetObject() == 0 )
293     return false;
294   
295   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
296   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
297
298   return anElem && anElem->GetType() == SMDSAbs_Face && anElem->NbNodes() == 3;
299 }
300
301 //=======================================================================
302 // name    : SMESHGUI_TriangleFilter::IsValid
303 // Purpose : Verify whether selected cell is triangle
304 //=======================================================================
305 bool SMESHGUI_TriangleFilter::IsObjValid( const int theObjId ) const
306 {
307   if ( myActor == 0 )
308     return false;
309
310   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
311   if ( anActor->GetObject() == 0 )
312     return false;
313   
314   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
315   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
316
317   return anElem && anElem->GetType() == SMDSAbs_Face && anElem->NbNodes() == 3;
318 }
319
320 //=======================================================================
321 // name    : SMESHGUI_TriangleFilter::SetActor
322 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
323 //           enumeration. All filters must have different ids
324 //=======================================================================
325 int SMESHGUI_TriangleFilter::GetId() const
326 {
327   return SMESHGUI_TriaFilter;
328 }
329
330 //=======================================================================
331 // name    : SMESHGUI_TriangleFilter::IsNodeFilter
332 // Purpose : Returns true if filter is intended for nodes
333 //=======================================================================
334 bool SMESHGUI_TriangleFilter::IsNodeFilter() const
335 {
336   return false;
337 }
338
339 /*
340   Class       : SMESHGUI_FacesFilter
341   Description : Verify whether selected cell is any face
342 */
343
344
345 //=======================================================================
346 // name    : SMESHGUI_FacesFilter::SMESHGUI_FacesFilter
347 // Purpose : Constructor
348 //=======================================================================
349 SMESHGUI_FacesFilter::SMESHGUI_FacesFilter()
350 : SMESHGUI_Filter()
351 {
352 }
353
354 SMESHGUI_FacesFilter::~SMESHGUI_FacesFilter()
355 {
356 }
357
358 //=======================================================================
359 // name    : SMESHGUI_FacesFilter::IsValid
360 // Purpose : Verify whether selected cell is face
361 //=======================================================================
362 bool SMESHGUI_FacesFilter::IsValid( const int theCellId ) const
363 {
364   if ( myActor == 0 )
365     return false;
366
367   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
368   if ( anActor->GetObject() == 0 )
369     return false;
370   
371   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
372   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
373
374   return anElem && anElem->GetType() == SMDSAbs_Face;
375 }
376
377 //=======================================================================
378 // name    : SMESHGUI_FacesFilter::IsValid
379 // Purpose : Verify whether selected cell is face
380 //=======================================================================
381 bool SMESHGUI_FacesFilter::IsObjValid( const int theObjId ) const
382 {
383   if ( myActor == 0 )
384     return false;
385
386   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
387   if ( anActor->GetObject() == 0 )
388     return false;
389   
390   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
391   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
392
393   return anElem && anElem->GetType() == SMDSAbs_Face;
394 }
395
396 //=======================================================================
397 // name    : SMESHGUI_FacesFilter::GetId
398 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
399 //           enumeration. All filters must have different ids
400 //=======================================================================
401 int SMESHGUI_FacesFilter::GetId() const
402 {
403   return SMESHGUI_FaceFilter;
404 }
405
406 //=======================================================================
407 // name    : SMESHGUI_FacesFilter::IsNodeFilter
408 // Purpose : Returns true if filter is intended for nodes
409 //=======================================================================
410 bool SMESHGUI_FacesFilter::IsNodeFilter() const
411 {
412   return false;
413 }
414
415
416 /*
417   Class       : SMESHGUI_VolumesFilter
418   Description : Verify whether selected cell is any volume
419 */
420
421
422 //=======================================================================
423 // name    : SMESHGUI_VolumesFilter::SMESHGUI_VolumesFilter
424 // Purpose : Constructor
425 //=======================================================================
426 SMESHGUI_VolumesFilter::SMESHGUI_VolumesFilter()
427 : SMESHGUI_Filter()
428 {
429 }
430
431 SMESHGUI_VolumesFilter::~SMESHGUI_VolumesFilter()
432 {
433 }
434
435 //=======================================================================
436 // name    : SMESHGUI_VolumesFilter::IsValid
437 // Purpose : Verify whether selected cell is volume
438 //=======================================================================
439 bool SMESHGUI_VolumesFilter::IsValid( const int theCellId ) const
440 {
441   if ( myActor == 0 )
442     return false;
443
444   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
445   if ( anActor->GetObject() == 0 )
446     return false;
447   
448   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
449   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
450
451   return anElem && anElem->GetType() == SMDSAbs_Volume;
452 }
453
454 //=======================================================================
455 // name    : SMESHGUI_VolumesFilter::IsValid
456 // Purpose : Verify whether selected cell is volume
457 //=======================================================================
458 bool SMESHGUI_VolumesFilter::IsObjValid( const int theObjId ) const
459 {
460   if ( myActor == 0 )
461     return false;
462
463   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
464   if ( anActor->GetObject() == 0 )
465     return false;
466   
467   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
468   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
469
470   return anElem && anElem->GetType() == SMDSAbs_Volume;
471 }
472
473 //=======================================================================
474 // name    : SMESHGUI_VolumesFilter::GetId
475 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
476 //           enumeration. All filters must have different ids
477 //=======================================================================
478 int SMESHGUI_VolumesFilter::GetId() const
479 {
480   return SMESHGUI_VolumeFilter;
481 }
482
483 //=======================================================================
484 // name    : SMESHGUI_VolumesFilter::IsNodeFilter
485 // Purpose : Returns true if filter is intended for nodes
486 //=======================================================================
487 bool SMESHGUI_VolumesFilter::IsNodeFilter() const
488 {
489   return false;
490 }
491
492
493
494
495
496
497
498
499
500
501