Salome HOME
IPAL21120 SIGSEGV on Meshing attached Compound with Automatic Hexadralization
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_Filter.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  SMESHGUI_PredicateFilter : Filters for VTK viewer
23 //  File   : SMESHGUI_Filter.cxx
24 //  Author : Sergey LITONIN
25 //  Module : SMESH
26 //
27 #include "SMESHGUI_Filter.h"
28
29 #include "SMESHGUI.h"
30 #include "SMESHGUI_Utils.h"
31
32 #include "SMESH_Actor.h"
33 #include "SMDS_Mesh.hxx"
34 #include "SMDS_MeshElement.hxx"
35 #include "SMDSAbs_ElementType.hxx"
36
37 // OCCT Includes
38 #include <gp_Vec.hxx>
39 #include <Precision.hxx>
40
41 // VTK Includes
42 #include <vtkCell.h>
43
44 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_Filter, VTKViewer_Filter)
45 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_Filter, VTKViewer_Filter)
46
47 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_PredicateFilter, SMESHGUI_Filter)
48 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_PredicateFilter, SMESHGUI_Filter)
49
50 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_QuadrangleFilter, SMESHGUI_Filter)
51 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_QuadrangleFilter, SMESHGUI_Filter)
52
53 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_TriangleFilter, SMESHGUI_Filter)
54 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_TriangleFilter, SMESHGUI_Filter)
55
56 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_FacesFilter, SMESHGUI_Filter)
57 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_FacesFilter, SMESHGUI_Filter)
58
59 IMPLEMENT_STANDARD_HANDLE(SMESHGUI_VolumesFilter, SMESHGUI_Filter)
60 IMPLEMENT_STANDARD_RTTIEXT(SMESHGUI_VolumesFilter, SMESHGUI_Filter)
61
62 /*
63   Class       : SMESHGUI_PredicateFilter
64   Description : Selection filter for VTK viewer. This class aggregate object
65                 of SMESH_Predicate class and uses it for verification of criterion
66 */
67
68 //=======================================================================
69 // name    : SMESHGUI_PredicateFilter::SMESHGUI_PredicateFilter
70 // Purpose : Constructor
71 //=======================================================================
72 SMESHGUI_PredicateFilter::SMESHGUI_PredicateFilter()
73 {
74 }
75
76 SMESHGUI_PredicateFilter::~SMESHGUI_PredicateFilter()
77 {
78 }
79
80 //=======================================================================
81 // name    : SMESHGUI_PredicateFilter::IsValid
82 // Purpose : Verify whether entry id satisfies to criterion of the filter
83 //=======================================================================
84 bool SMESHGUI_PredicateFilter::IsValid( const int theCellId ) const
85 {
86   if ( myActor == 0 || myPred->_is_nil() )
87     return false;
88
89   SMESH_Actor* anActor = dynamic_cast<SMESH_Actor*>( myActor );
90   if ( !anActor || anActor->GetObject() == 0 )
91     return false;
92
93   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
94   SMESH::ElementType anElemType = myPred->GetElementType();
95   int aMeshId = anElemType == SMESH::NODE ? anActor->GetNodeObjId( theCellId )
96                                           : anActor->GetElemObjId( theCellId );
97
98   // if type of element != type of predicate return true because
99   // this predicate is not intended for filtering sush elements
100   const SMDS_MeshElement* anElem = anElemType == SMESH::NODE ? aMesh->FindNode( aMeshId )
101                                                              : aMesh->FindElement( aMeshId );
102   if ( anElem != 0 && anElem->GetType() != (SMDSAbs_ElementType)myPred->GetElementType() )
103     return true;
104
105   return myPred->IsSatisfy( aMeshId );
106 }
107
108 //=======================================================================
109 // name    : SMESHGUI_PredicateFilter::IsValid
110 // Purpose : Verify whether entry id satisfies to criterion of the filter
111 //=======================================================================
112 bool SMESHGUI_PredicateFilter::IsObjValid( const int theObjId ) const
113 {
114   if ( myActor == 0 || myPred->_is_nil() )
115     return false;
116
117   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
118   if ( anActor->GetObject() == 0 )
119     return false;
120
121   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
122   SMESH::ElementType anElemType = myPred->GetElementType();
123
124   // if type of element != type of predicate return true because
125   // this predicate is not intended for filtering sush elements
126   const SMDS_MeshElement* anElem = anElemType == SMESH::NODE ? aMesh->FindNode( theObjId )
127                                                              : aMesh->FindElement( theObjId );
128   if ( anElem != 0 && anElem->GetType() != (SMDSAbs_ElementType)myPred->GetElementType() )
129     return true;
130
131   return myPred->IsSatisfy( theObjId );
132 }
133
134 //=======================================================================
135 // name    : SMESHGUI_PredicateFilter::IsNodeFilter
136 // Purpose : Returns true if filter is intended for nodes
137 //=======================================================================
138 bool SMESHGUI_PredicateFilter::IsNodeFilter() const
139 {
140   return GetId() == SMESHGUI_NodeFilter;
141 }
142
143 //=======================================================================
144 // name    : SMESHGUI_PredicateFilter::SetPredicate
145 // Purpose : Set new pridicate to the filter
146 //=======================================================================
147 void SMESHGUI_PredicateFilter::SetPredicate( SMESH::Predicate_ptr thePred )
148 {
149   myPred = SMESH::Predicate::_duplicate( thePred );
150 }
151
152 //=======================================================================
153 // name    : SMESHGUI_PredicateFilter::SetActor
154 // Purpose : Set new actor
155 //=======================================================================
156 void SMESHGUI_PredicateFilter::SetActor( SALOME_Actor* theActor )
157 {
158   if ( myActor == theActor )
159     return;
160   SMESHGUI_Filter::SetActor( theActor );
161
162   if ( myActor != 0 && !myPred->_is_nil() )
163   {
164     SALOME_Actor* sActor = dynamic_cast<SALOME_Actor*>( myActor );
165     Handle(SALOME_InteractiveObject) anIO;
166     if( sActor )
167       anIO = sActor->getIO();
168     if ( !anIO.IsNull() )
169     {
170       SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIO);
171       if(!aMesh->_is_nil())
172         myPred->SetMesh(aMesh);
173     }
174   }
175 }
176
177 //=======================================================================
178 // name    : SMESHGUI_PredicateFilter::SetActor
179 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
180 //           enumeration. All filters must have different ids
181 //=======================================================================
182 int SMESHGUI_PredicateFilter::GetId() const
183 {
184   if      ( myPred->GetElementType() == SMESH::NODE   ) return SMESHGUI_NodeFilter;
185   else if ( myPred->GetElementType() == SMESH::EDGE   ) return SMESHGUI_EdgeFilter;
186   else if ( myPred->GetElementType() == SMESH::FACE   ) return SMESHGUI_FaceFilter;
187   else if ( myPred->GetElementType() == SMESH::VOLUME ) return SMESHGUI_VolumeFilter;
188   else if ( myPred->GetElementType() == SMESH::ALL    ) return SMESHGUI_AllElementsFilter;
189   else                                                  return SMESHGUI_UnknownFilter;
190 }
191
192
193 /*
194   Class       : SMESHGUI_QuadrangleFilter
195   Description : Verify whether selected cell is quadranle
196 */
197
198 //=======================================================================
199 // name    : SMESHGUI_QuadrangleFilter::SMESHGUI_QuadrangleFilter
200 // Purpose : Constructor
201 //=======================================================================
202 SMESHGUI_QuadrangleFilter::SMESHGUI_QuadrangleFilter()
203 : SMESHGUI_Filter()
204 {
205 }
206
207 SMESHGUI_QuadrangleFilter::~SMESHGUI_QuadrangleFilter()
208 {
209 }
210
211 //=======================================================================
212 // name    : SMESHGUI_QuadrangleFilter::IsValid
213 // Purpose : Verify whether selected cell is quadranle
214 //=======================================================================
215 bool SMESHGUI_QuadrangleFilter::IsValid( const int theCellId ) const
216 {
217   if ( myActor == 0 )
218     return false;
219
220   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
221   if ( anActor->GetObject() == 0 )
222     return false;
223
224   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
225   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
226
227   return anElem && anElem->GetType() == SMDSAbs_Face &&
228     ( anElem->NbNodes() == ( anElem->IsQuadratic() ? 8 : 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 &&
248     ( anElem->NbNodes() == ( anElem->IsQuadratic() ? 8  : 4 ));
249 }
250
251 //=======================================================================
252 // name    : SMESHGUI_QuadrangleFilter::SetActor
253 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
254 //           enumeration. All filters must have different ids
255 //=======================================================================
256 int SMESHGUI_QuadrangleFilter::GetId() const
257 {
258   return SMESHGUI_QuadFilter;
259 }
260
261 //=======================================================================
262 // name    : SMESHGUI_QuadrangleFilter::IsNodeFilter
263 // Purpose : Returns true if filter is intended for nodes
264 //=======================================================================
265 bool SMESHGUI_QuadrangleFilter::IsNodeFilter() const
266 {
267   return false;
268 }
269
270
271 /*
272   Class       : SMESHGUI_TriangleFilter
273   Description : Verify whether selected cell is triangle
274 */
275
276
277 //=======================================================================
278 // name    : SMESHGUI_TriangleFilter::SMESHGUI_TriangleFilter
279 // Purpose : Constructor
280 //=======================================================================
281 SMESHGUI_TriangleFilter::SMESHGUI_TriangleFilter()
282 : SMESHGUI_Filter()
283 {
284 }
285
286 SMESHGUI_TriangleFilter::~SMESHGUI_TriangleFilter()
287 {
288 }
289
290 //=======================================================================
291 // name    : SMESHGUI_TriangleFilter::IsValid
292 // Purpose : Verify whether selected cell is triangle
293 //=======================================================================
294 bool SMESHGUI_TriangleFilter::IsValid( const int theCellId ) const
295 {
296   if ( myActor == 0 )
297     return false;
298
299   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
300   if ( anActor->GetObject() == 0 )
301     return false;
302
303   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
304   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
305
306   return anElem && anElem->GetType() == SMDSAbs_Face &&
307     ( anElem->NbNodes() == ( anElem->IsQuadratic() ? 6  : 3 ));
308 }
309
310 //=======================================================================
311 // name    : SMESHGUI_TriangleFilter::IsValid
312 // Purpose : Verify whether selected cell is triangle
313 //=======================================================================
314 bool SMESHGUI_TriangleFilter::IsObjValid( const int theObjId ) const
315 {
316   if ( myActor == 0 )
317     return false;
318
319   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
320   if ( anActor->GetObject() == 0 )
321     return false;
322
323   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
324   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
325
326   return anElem && anElem->GetType() == SMDSAbs_Face &&
327     ( anElem->NbNodes() == ( anElem->IsQuadratic() ? 6  : 3 ));
328 }
329
330 //=======================================================================
331 // name    : SMESHGUI_TriangleFilter::SetActor
332 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
333 //           enumeration. All filters must have different ids
334 //=======================================================================
335 int SMESHGUI_TriangleFilter::GetId() const
336 {
337   return SMESHGUI_TriaFilter;
338 }
339
340 //=======================================================================
341 // name    : SMESHGUI_TriangleFilter::IsNodeFilter
342 // Purpose : Returns true if filter is intended for nodes
343 //=======================================================================
344 bool SMESHGUI_TriangleFilter::IsNodeFilter() const
345 {
346   return false;
347 }
348
349 /*
350   Class       : SMESHGUI_FacesFilter
351   Description : Verify whether selected cell is any face
352 */
353
354
355 //=======================================================================
356 // name    : SMESHGUI_FacesFilter::SMESHGUI_FacesFilter
357 // Purpose : Constructor
358 //=======================================================================
359 SMESHGUI_FacesFilter::SMESHGUI_FacesFilter()
360 : SMESHGUI_Filter()
361 {
362 }
363
364 SMESHGUI_FacesFilter::~SMESHGUI_FacesFilter()
365 {
366 }
367
368 //=======================================================================
369 // name    : SMESHGUI_FacesFilter::IsValid
370 // Purpose : Verify whether selected cell is face
371 //=======================================================================
372 bool SMESHGUI_FacesFilter::IsValid( const int theCellId ) const
373 {
374   if ( myActor == 0 )
375     return false;
376
377   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
378   if ( anActor->GetObject() == 0 )
379     return false;
380
381   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
382   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
383
384   return anElem && anElem->GetType() == SMDSAbs_Face;
385 }
386
387 //=======================================================================
388 // name    : SMESHGUI_FacesFilter::IsValid
389 // Purpose : Verify whether selected cell is face
390 //=======================================================================
391 bool SMESHGUI_FacesFilter::IsObjValid( const int theObjId ) const
392 {
393   if ( myActor == 0 )
394     return false;
395
396   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
397   if ( anActor->GetObject() == 0 )
398     return false;
399
400   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
401   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
402
403   return anElem && anElem->GetType() == SMDSAbs_Face;
404 }
405
406 //=======================================================================
407 // name    : SMESHGUI_FacesFilter::GetId
408 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
409 //           enumeration. All filters must have different ids
410 //=======================================================================
411 int SMESHGUI_FacesFilter::GetId() const
412 {
413   return SMESHGUI_FaceFilter;
414 }
415
416 //=======================================================================
417 // name    : SMESHGUI_FacesFilter::IsNodeFilter
418 // Purpose : Returns true if filter is intended for nodes
419 //=======================================================================
420 bool SMESHGUI_FacesFilter::IsNodeFilter() const
421 {
422   return false;
423 }
424
425
426 /*
427   Class       : SMESHGUI_VolumesFilter
428   Description : Verify whether selected cell is any volume
429 */
430
431
432 //=======================================================================
433 // name    : SMESHGUI_VolumesFilter::SMESHGUI_VolumesFilter
434 // Purpose : Constructor
435 //=======================================================================
436 SMESHGUI_VolumesFilter::SMESHGUI_VolumesFilter()
437 : SMESHGUI_Filter()
438 {
439 }
440
441 SMESHGUI_VolumesFilter::~SMESHGUI_VolumesFilter()
442 {
443 }
444
445 //=======================================================================
446 // name    : SMESHGUI_VolumesFilter::IsValid
447 // Purpose : Verify whether selected cell is volume
448 //=======================================================================
449 bool SMESHGUI_VolumesFilter::IsValid( const int theCellId ) const
450 {
451   if ( myActor == 0 )
452     return false;
453
454   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
455   if ( anActor->GetObject() == 0 )
456     return false;
457
458   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
459   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
460
461   return anElem && anElem->GetType() == SMDSAbs_Volume;
462 }
463
464 //=======================================================================
465 // name    : SMESHGUI_VolumesFilter::IsValid
466 // Purpose : Verify whether selected cell is volume
467 //=======================================================================
468 bool SMESHGUI_VolumesFilter::IsObjValid( const int theObjId ) const
469 {
470   if ( myActor == 0 )
471     return false;
472
473   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
474   if ( anActor->GetObject() == 0 )
475     return false;
476
477   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
478   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
479
480   return anElem && anElem->GetType() == SMDSAbs_Volume;
481 }
482
483 //=======================================================================
484 // name    : SMESHGUI_VolumesFilter::GetId
485 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
486 //           enumeration. All filters must have different ids
487 //=======================================================================
488 int SMESHGUI_VolumesFilter::GetId() const
489 {
490   return SMESHGUI_VolumeFilter;
491 }
492
493 //=======================================================================
494 // name    : SMESHGUI_VolumesFilter::IsNodeFilter
495 // Purpose : Returns true if filter is intended for nodes
496 //=======================================================================
497 bool SMESHGUI_VolumesFilter::IsNodeFilter() const
498 {
499   return false;
500 }