Salome HOME
Update mail address
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 &&
229     ( anElem->NbNodes() == ( anElem->IsQuadratic() ? 8 : 4 ));
230 }
231
232 //=======================================================================
233 // name    : SMESHGUI_QuadrangleFilter::IsValid
234 // Purpose : Verify whether selected cell is quadranle
235 //=======================================================================
236 bool SMESHGUI_QuadrangleFilter::IsObjValid( const int theObjId ) const
237 {
238   if ( myActor == 0 )
239     return false;
240
241   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
242   if ( anActor->GetObject() == 0 )
243     return false;
244
245   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
246   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
247
248   return anElem && anElem->GetType() == SMDSAbs_Face &&
249     ( anElem->NbNodes() == ( anElem->IsQuadratic() ? 8  : 4 ));
250 }
251
252 //=======================================================================
253 // name    : SMESHGUI_QuadrangleFilter::SetActor
254 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
255 //           enumeration. All filters must have different ids
256 //=======================================================================
257 int SMESHGUI_QuadrangleFilter::GetId() const
258 {
259   return SMESHGUI_QuadFilter;
260 }
261
262 //=======================================================================
263 // name    : SMESHGUI_QuadrangleFilter::IsNodeFilter
264 // Purpose : Returns true if filter is intended for nodes
265 //=======================================================================
266 bool SMESHGUI_QuadrangleFilter::IsNodeFilter() const
267 {
268   return false;
269 }
270
271
272 /*
273   Class       : SMESHGUI_TriangleFilter
274   Description : Verify whether selected cell is triangle
275 */
276
277
278 //=======================================================================
279 // name    : SMESHGUI_TriangleFilter::SMESHGUI_TriangleFilter
280 // Purpose : Constructor
281 //=======================================================================
282 SMESHGUI_TriangleFilter::SMESHGUI_TriangleFilter()
283 : SMESHGUI_Filter()
284 {
285 }
286
287 SMESHGUI_TriangleFilter::~SMESHGUI_TriangleFilter()
288 {
289 }
290
291 //=======================================================================
292 // name    : SMESHGUI_TriangleFilter::IsValid
293 // Purpose : Verify whether selected cell is triangle
294 //=======================================================================
295 bool SMESHGUI_TriangleFilter::IsValid( const int theCellId ) const
296 {
297   if ( myActor == 0 )
298     return false;
299
300   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
301   if ( anActor->GetObject() == 0 )
302     return false;
303
304   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
305   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
306
307   return anElem && anElem->GetType() == SMDSAbs_Face &&
308     ( anElem->NbNodes() == ( anElem->IsQuadratic() ? 6  : 3 ));
309 }
310
311 //=======================================================================
312 // name    : SMESHGUI_TriangleFilter::IsValid
313 // Purpose : Verify whether selected cell is triangle
314 //=======================================================================
315 bool SMESHGUI_TriangleFilter::IsObjValid( const int theObjId ) const
316 {
317   if ( myActor == 0 )
318     return false;
319
320   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
321   if ( anActor->GetObject() == 0 )
322     return false;
323
324   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
325   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
326
327   return anElem && anElem->GetType() == SMDSAbs_Face &&
328     ( anElem->NbNodes() == ( anElem->IsQuadratic() ? 6  : 3 ));
329 }
330
331 //=======================================================================
332 // name    : SMESHGUI_TriangleFilter::SetActor
333 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
334 //           enumeration. All filters must have different ids
335 //=======================================================================
336 int SMESHGUI_TriangleFilter::GetId() const
337 {
338   return SMESHGUI_TriaFilter;
339 }
340
341 //=======================================================================
342 // name    : SMESHGUI_TriangleFilter::IsNodeFilter
343 // Purpose : Returns true if filter is intended for nodes
344 //=======================================================================
345 bool SMESHGUI_TriangleFilter::IsNodeFilter() const
346 {
347   return false;
348 }
349
350 /*
351   Class       : SMESHGUI_FacesFilter
352   Description : Verify whether selected cell is any face
353 */
354
355
356 //=======================================================================
357 // name    : SMESHGUI_FacesFilter::SMESHGUI_FacesFilter
358 // Purpose : Constructor
359 //=======================================================================
360 SMESHGUI_FacesFilter::SMESHGUI_FacesFilter()
361 : SMESHGUI_Filter()
362 {
363 }
364
365 SMESHGUI_FacesFilter::~SMESHGUI_FacesFilter()
366 {
367 }
368
369 //=======================================================================
370 // name    : SMESHGUI_FacesFilter::IsValid
371 // Purpose : Verify whether selected cell is face
372 //=======================================================================
373 bool SMESHGUI_FacesFilter::IsValid( const int theCellId ) const
374 {
375   if ( myActor == 0 )
376     return false;
377
378   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
379   if ( anActor->GetObject() == 0 )
380     return false;
381
382   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
383   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
384
385   return anElem && anElem->GetType() == SMDSAbs_Face;
386 }
387
388 //=======================================================================
389 // name    : SMESHGUI_FacesFilter::IsValid
390 // Purpose : Verify whether selected cell is face
391 //=======================================================================
392 bool SMESHGUI_FacesFilter::IsObjValid( const int theObjId ) const
393 {
394   if ( myActor == 0 )
395     return false;
396
397   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
398   if ( anActor->GetObject() == 0 )
399     return false;
400
401   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
402   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
403
404   return anElem && anElem->GetType() == SMDSAbs_Face;
405 }
406
407 //=======================================================================
408 // name    : SMESHGUI_FacesFilter::GetId
409 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
410 //           enumeration. All filters must have different ids
411 //=======================================================================
412 int SMESHGUI_FacesFilter::GetId() const
413 {
414   return SMESHGUI_FaceFilter;
415 }
416
417 //=======================================================================
418 // name    : SMESHGUI_FacesFilter::IsNodeFilter
419 // Purpose : Returns true if filter is intended for nodes
420 //=======================================================================
421 bool SMESHGUI_FacesFilter::IsNodeFilter() const
422 {
423   return false;
424 }
425
426
427 /*
428   Class       : SMESHGUI_VolumesFilter
429   Description : Verify whether selected cell is any volume
430 */
431
432
433 //=======================================================================
434 // name    : SMESHGUI_VolumesFilter::SMESHGUI_VolumesFilter
435 // Purpose : Constructor
436 //=======================================================================
437 SMESHGUI_VolumesFilter::SMESHGUI_VolumesFilter()
438 : SMESHGUI_Filter()
439 {
440 }
441
442 SMESHGUI_VolumesFilter::~SMESHGUI_VolumesFilter()
443 {
444 }
445
446 //=======================================================================
447 // name    : SMESHGUI_VolumesFilter::IsValid
448 // Purpose : Verify whether selected cell is volume
449 //=======================================================================
450 bool SMESHGUI_VolumesFilter::IsValid( const int theCellId ) const
451 {
452   if ( myActor == 0 )
453     return false;
454
455   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
456   if ( anActor->GetObject() == 0 )
457     return false;
458
459   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
460   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
461
462   return anElem && anElem->GetType() == SMDSAbs_Volume;
463 }
464
465 //=======================================================================
466 // name    : SMESHGUI_VolumesFilter::IsValid
467 // Purpose : Verify whether selected cell is volume
468 //=======================================================================
469 bool SMESHGUI_VolumesFilter::IsObjValid( const int theObjId ) const
470 {
471   if ( myActor == 0 )
472     return false;
473
474   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
475   if ( anActor->GetObject() == 0 )
476     return false;
477
478   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
479   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
480
481   return anElem && anElem->GetType() == SMDSAbs_Volume;
482 }
483
484 //=======================================================================
485 // name    : SMESHGUI_VolumesFilter::GetId
486 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
487 //           enumeration. All filters must have different ids
488 //=======================================================================
489 int SMESHGUI_VolumesFilter::GetId() const
490 {
491   return SMESHGUI_VolumeFilter;
492 }
493
494 //=======================================================================
495 // name    : SMESHGUI_VolumesFilter::IsNodeFilter
496 // Purpose : Returns true if filter is intended for nodes
497 //=======================================================================
498 bool SMESHGUI_VolumesFilter::IsNodeFilter() const
499 {
500   return false;
501 }