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