Salome HOME
The selections of elements corrected.
[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
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Utils.h"
32
33 #include "SMESH_Actor.h"
34 #include "SMDS_Mesh.hxx"
35 #include "SMDS_MeshElement.hxx"
36 #include "SMDSAbs_ElementType.hxx"
37
38 // OCCT Includes
39 #include <gp_Vec.hxx>
40 #include <Precision.hxx>
41
42 // VTK Includes
43 #include <vtkCell.h>
44
45 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_Filter, VTKViewer_Filter)
46 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_Filter, VTKViewer_Filter)
47
48 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_PredicateFilter, SMESHGUI_Filter)
49 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_PredicateFilter, SMESHGUI_Filter)
50
51 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_QuadrangleFilter, SMESHGUI_Filter)
52 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_QuadrangleFilter, SMESHGUI_Filter)
53
54 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_TriangleFilter, SMESHGUI_Filter)
55 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_TriangleFilter, SMESHGUI_Filter)
56
57 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_FacesFilter, SMESHGUI_Filter)
58 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_FacesFilter, SMESHGUI_Filter)
59
60 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_VolumesFilter, SMESHGUI_Filter)
61 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_VolumesFilter, SMESHGUI_Filter)
62
63 /*
64   Class       : SMESHGUI_PredicateFilter
65   Description : Selection filter for VTK viewer. This class aggregate object
66                 of SMESH_Predicate class and uses it for verification of criterion
67 */
68
69 //=======================================================================
70 // name    : SMESHGUI_PredicateFilter::SMESHGUI_PredicateFilter
71 // Purpose : Constructor
72 //=======================================================================
73 SMESHGUI_PredicateFilter::SMESHGUI_PredicateFilter()
74 {
75 }
76
77 SMESHGUI_PredicateFilter::~SMESHGUI_PredicateFilter()
78 {
79 }
80
81 //=======================================================================
82 // name    : SMESHGUI_PredicateFilter::IsValid
83 // Purpose : Verify whether entry id satisfies to criterion of the filter
84 //=======================================================================
85 bool SMESHGUI_PredicateFilter::IsValid( const int theCellId ) const
86 {
87   if ( myActor == 0 || myPred->_is_nil() )
88     return false;
89
90   SMESH_Actor* anActor = dynamic_cast<SMESH_Actor*>( myActor );
91   if ( !anActor || anActor->GetObject() == 0 )
92     return false;
93
94   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
95   SMESH::ElementType anElemType = myPred->GetElementType();
96   int aMeshId = anElemType == SMESH::NODE ? anActor->GetNodeObjId( theCellId )
97                                           : anActor->GetElemObjId( theCellId );
98
99   // if type of element != type of predicate return true because
100   // this predicate is not intended for filtering sush elements
101   const SMDS_MeshElement* anElem = anElemType == SMESH::NODE ? aMesh->FindNode( aMeshId )
102                                                              : aMesh->FindElement( aMeshId );
103   if ( anElem != 0 && anElem->GetType() != (SMDSAbs_ElementType)myPred->GetElementType() )
104     return true;
105
106   return myPred->IsSatisfy( aMeshId );
107 }
108
109 //=======================================================================
110 // name    : SMESHGUI_PredicateFilter::IsValid
111 // Purpose : Verify whether entry id satisfies to criterion of the filter
112 //=======================================================================
113 bool SMESHGUI_PredicateFilter::IsObjValid( const int theObjId ) const
114 {
115   if ( myActor == 0 || myPred->_is_nil() )
116     return false;
117
118   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
119   if ( anActor->GetObject() == 0 )
120     return false;
121
122   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
123   SMESH::ElementType anElemType = myPred->GetElementType();
124
125   // if type of element != type of predicate return true because
126   // this predicate is not intended for filtering sush elements
127   const SMDS_MeshElement* anElem = anElemType == SMESH::NODE ? aMesh->FindNode( theObjId )
128                                                              : aMesh->FindElement( theObjId );
129   if ( anElem != 0 && anElem->GetType() != (SMDSAbs_ElementType)myPred->GetElementType() )
130     return true;
131
132   return myPred->IsSatisfy( theObjId );
133 }
134
135 //=======================================================================
136 // name    : SMESHGUI_PredicateFilter::IsNodeFilter
137 // Purpose : Returns true if filter is intended for nodes
138 //=======================================================================
139 bool SMESHGUI_PredicateFilter::IsNodeFilter() const
140 {
141   return GetId() == SMESHGUI_NodeFilter;
142 }
143
144 //=======================================================================
145 // name    : SMESHGUI_PredicateFilter::SetPredicate
146 // Purpose : Set new pridicate to the filter
147 //=======================================================================
148 void SMESHGUI_PredicateFilter::SetPredicate( SMESH::Predicate_ptr thePred )
149 {
150   myPred = SMESH::Predicate::_duplicate( thePred );
151 }
152
153 //=======================================================================
154 // name    : SMESHGUI_PredicateFilter::SetActor
155 // Purpose : Set new actor
156 //=======================================================================
157 void SMESHGUI_PredicateFilter::SetActor( SALOME_Actor* theActor )
158 {
159   if ( myActor == theActor )
160     return;
161   SMESHGUI_Filter::SetActor( theActor );
162
163   if ( myActor != 0 && !myPred->_is_nil() )
164   {
165     SALOME_Actor* sActor = dynamic_cast<SALOME_Actor*>( myActor );
166     Handle(SALOME_InteractiveObject) anIO;
167     if( sActor )
168       anIO = sActor->getIO();
169     if ( !anIO.IsNull() )
170     {
171       SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIO);
172       if(!aMesh->_is_nil())
173         myPred->SetMesh(aMesh);
174     }
175   }
176 }
177
178 //=======================================================================
179 // name    : SMESHGUI_PredicateFilter::SetActor
180 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
181 //           enumeration. All filters must have different ids
182 //=======================================================================
183 int SMESHGUI_PredicateFilter::GetId() const
184 {
185   if      ( myPred->GetElementType() == SMESH::NODE   ) return SMESHGUI_NodeFilter;
186   else if ( myPred->GetElementType() == SMESH::EDGE   ) return SMESHGUI_EdgeFilter;
187   else if ( myPred->GetElementType() == SMESH::FACE   ) return SMESHGUI_FaceFilter;
188   else if ( myPred->GetElementType() == SMESH::VOLUME ) return SMESHGUI_VolumeFilter;
189   else if ( myPred->GetElementType() == SMESH::ALL    ) return SMESHGUI_AllElementsFilter;
190   else                                                  return SMESHGUI_UnknownFilter;
191 }
192
193
194 /*
195   Class       : SMESHGUI_QuadrangleFilter
196   Description : Verify whether selected cell is quadranle
197 */
198
199 //=======================================================================
200 // name    : SMESHGUI_QuadrangleFilter::SMESHGUI_QuadrangleFilter
201 // Purpose : Constructor
202 //=======================================================================
203 SMESHGUI_QuadrangleFilter::SMESHGUI_QuadrangleFilter()
204 : SMESHGUI_Filter()
205 {
206 }
207
208 SMESHGUI_QuadrangleFilter::~SMESHGUI_QuadrangleFilter()
209 {
210 }
211
212 //=======================================================================
213 // name    : SMESHGUI_QuadrangleFilter::IsValid
214 // Purpose : Verify whether selected cell is quadranle
215 //=======================================================================
216 bool SMESHGUI_QuadrangleFilter::IsValid( const int theCellId ) const
217 {
218   if ( myActor == 0 )
219     return false;
220
221   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
222   if ( anActor->GetObject() == 0 )
223     return false;
224
225   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
226   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
227
228   return anElem && anElem->GetType() == SMDSAbs_Face && anElem->NbNodes() == 4;
229 }
230
231 //=======================================================================
232 // name    : SMESHGUI_QuadrangleFilter::IsValid
233 // Purpose : Verify whether selected cell is quadranle
234 //=======================================================================
235 bool SMESHGUI_QuadrangleFilter::IsObjValid( const int theObjId ) const
236 {
237   if ( myActor == 0 )
238     return false;
239
240   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
241   if ( anActor->GetObject() == 0 )
242     return false;
243
244   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
245   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
246
247   return anElem && anElem->GetType() == SMDSAbs_Face && anElem->NbNodes() == 4;
248 }
249
250 //=======================================================================
251 // name    : SMESHGUI_QuadrangleFilter::SetActor
252 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
253 //           enumeration. All filters must have different ids
254 //=======================================================================
255 int SMESHGUI_QuadrangleFilter::GetId() const
256 {
257   return SMESHGUI_QuadFilter;
258 }
259
260 //=======================================================================
261 // name    : SMESHGUI_QuadrangleFilter::IsNodeFilter
262 // Purpose : Returns true if filter is intended for nodes
263 //=======================================================================
264 bool SMESHGUI_QuadrangleFilter::IsNodeFilter() const
265 {
266   return false;
267 }
268
269
270 /*
271   Class       : SMESHGUI_TriangleFilter
272   Description : Verify whether selected cell is triangle
273 */
274
275
276 //=======================================================================
277 // name    : SMESHGUI_TriangleFilter::SMESHGUI_TriangleFilter
278 // Purpose : Constructor
279 //=======================================================================
280 SMESHGUI_TriangleFilter::SMESHGUI_TriangleFilter()
281 : SMESHGUI_Filter()
282 {
283 }
284
285 SMESHGUI_TriangleFilter::~SMESHGUI_TriangleFilter()
286 {
287 }
288
289 //=======================================================================
290 // name    : SMESHGUI_TriangleFilter::IsValid
291 // Purpose : Verify whether selected cell is triangle
292 //=======================================================================
293 bool SMESHGUI_TriangleFilter::IsValid( const int theCellId ) const
294 {
295   if ( myActor == 0 )
296     return false;
297
298   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
299   if ( anActor->GetObject() == 0 )
300     return false;
301
302   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
303   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
304
305   return anElem && anElem->GetType() == SMDSAbs_Face && anElem->NbNodes() == 3;
306 }
307
308 //=======================================================================
309 // name    : SMESHGUI_TriangleFilter::IsValid
310 // Purpose : Verify whether selected cell is triangle
311 //=======================================================================
312 bool SMESHGUI_TriangleFilter::IsObjValid( const int theObjId ) const
313 {
314   if ( myActor == 0 )
315     return false;
316
317   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
318   if ( anActor->GetObject() == 0 )
319     return false;
320
321   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
322   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
323
324   return anElem && anElem->GetType() == SMDSAbs_Face && anElem->NbNodes() == 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 SMESHGUI_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 = ( SMESH_Actor* )myActor;
375   if ( 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 = ( SMESH_Actor* )myActor;
394   if ( 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 SMESHGUI_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 )
449     return false;
450
451   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
452   if ( 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 = ( SMESH_Actor* )myActor;
471   if ( 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 SMESHGUI_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 }