Salome HOME
Integration of PAL/SALOME V2.1.0c from OCC
[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 /*
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 = ( SMESH_Actor* )myActor;
81   if ( 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() == SMESHGUI_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     Handle(SALOME_InteractiveObject) anIO = myActor->getIO();
156     if ( !anIO.IsNull() )
157     {
158       SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIO);
159       if(!aMesh->_is_nil())
160         myPred->SetMesh(aMesh);
161     }
162   }
163 }
164
165 //=======================================================================
166 // name    : SMESHGUI_PredicateFilter::SetActor
167 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
168 //           enumeration. All filters must have different ids
169 //=======================================================================
170 int SMESHGUI_PredicateFilter::GetId() const
171 {
172   if      ( myPred->GetElementType() == SMESH::NODE   ) return SMESHGUI_NodeFilter;
173   else if ( myPred->GetElementType() == SMESH::EDGE   ) return SMESHGUI_EdgeFilter;
174   else if ( myPred->GetElementType() == SMESH::FACE   ) return SMESHGUI_FaceFilter;
175   else if ( myPred->GetElementType() == SMESH::VOLUME ) return SMESHGUI_VolumeFilter;
176   else if ( myPred->GetElementType() == SMESH::ALL    ) return SMESHGUI_AllElementsFilter;
177   else                                                  return SMESHGUI_UnknownFilter;
178 }
179
180
181 /*
182   Class       : SMESHGUI_QuadrangleFilter
183   Description : Verify whether selected cell is quadranle
184 */
185
186 //=======================================================================
187 // name    : SMESHGUI_QuadrangleFilter::SMESHGUI_QuadrangleFilter
188 // Purpose : Constructor
189 //=======================================================================
190 SMESHGUI_QuadrangleFilter::SMESHGUI_QuadrangleFilter()
191 : SMESHGUI_Filter()
192 {
193 }
194
195 SMESHGUI_QuadrangleFilter::~SMESHGUI_QuadrangleFilter()
196 {
197 }
198
199 //=======================================================================
200 // name    : SMESHGUI_QuadrangleFilter::IsValid
201 // Purpose : Verify whether selected cell is quadranle
202 //=======================================================================
203 bool SMESHGUI_QuadrangleFilter::IsValid( const int theCellId ) const
204 {
205   if ( myActor == 0 )
206     return false;
207
208   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
209   if ( anActor->GetObject() == 0 )
210     return false;
211   
212   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
213   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
214   
215   return anElem != 0 ? anElem->GetType() != SMDSAbs_Face || anElem->NbNodes() == 4 : false;
216 }
217
218 //=======================================================================
219 // name    : SMESHGUI_QuadrangleFilter::IsValid
220 // Purpose : Verify whether selected cell is quadranle
221 //=======================================================================
222 bool SMESHGUI_QuadrangleFilter::IsObjValid( const int theObjId ) const
223 {
224   if ( myActor == 0 )
225     return false;
226
227   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
228   if ( anActor->GetObject() == 0 )
229     return false;
230   
231   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
232   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
233
234   return anElem != 0 ? anElem->GetType() != SMDSAbs_Face || anElem->NbNodes() == 4 : false;
235 }
236
237 //=======================================================================
238 // name    : SMESHGUI_QuadrangleFilter::SetActor
239 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
240 //           enumeration. All filters must have different ids
241 //=======================================================================
242 int SMESHGUI_QuadrangleFilter::GetId() const
243 {
244   return SMESHGUI_QuadFilter;
245 }
246
247 //=======================================================================
248 // name    : SMESHGUI_QuadrangleFilter::IsNodeFilter
249 // Purpose : Returns true if filter is intended for nodes
250 //=======================================================================
251 bool SMESHGUI_QuadrangleFilter::IsNodeFilter() const
252 {
253   return false;
254 }
255
256
257 /*
258   Class       : SMESHGUI_TriangleFilter
259   Description : Verify whether selected cell is triangle
260 */
261
262
263 //=======================================================================
264 // name    : SMESHGUI_TriangleFilter::SMESHGUI_TriangleFilter
265 // Purpose : Constructor
266 //=======================================================================
267 SMESHGUI_TriangleFilter::SMESHGUI_TriangleFilter()
268 : SMESHGUI_Filter()
269 {
270 }
271
272 SMESHGUI_TriangleFilter::~SMESHGUI_TriangleFilter()
273 {
274 }
275
276 //=======================================================================
277 // name    : SMESHGUI_TriangleFilter::IsValid
278 // Purpose : Verify whether selected cell is triangle
279 //=======================================================================
280 bool SMESHGUI_TriangleFilter::IsValid( const int theCellId ) const
281 {
282   if ( myActor == 0 )
283     return false;
284
285   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
286   if ( anActor->GetObject() == 0 )
287     return false;
288   
289   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
290   const SMDS_MeshElement* anElem = aMesh->FindElement( anActor->GetElemObjId( theCellId ) );
291
292   return anElem != 0 ? anElem->GetType() != SMDSAbs_Face || anElem->NbNodes() == 3 : false;
293 }
294
295 //=======================================================================
296 // name    : SMESHGUI_TriangleFilter::IsValid
297 // Purpose : Verify whether selected cell is triangle
298 //=======================================================================
299 bool SMESHGUI_TriangleFilter::IsObjValid( const int theObjId ) const
300 {
301   if ( myActor == 0 )
302     return false;
303
304   SMESH_Actor* anActor = ( SMESH_Actor* )myActor;
305   if ( anActor->GetObject() == 0 )
306     return false;
307   
308   SMDS_Mesh* aMesh = anActor->GetObject()->GetMesh();
309   const SMDS_MeshElement* anElem = aMesh->FindElement( theObjId );
310
311   return anElem != 0 ? anElem->GetType() != SMDSAbs_Face || anElem->NbNodes() == 3 : false;
312 }
313
314 //=======================================================================
315 // name    : SMESHGUI_TriangleFilter::SetActor
316 // Purpose : Get ID of the filter. Must return value from SMESHGUI_FilterType
317 //           enumeration. All filters must have different ids
318 //=======================================================================
319 int SMESHGUI_TriangleFilter::GetId() const
320 {
321   return SMESHGUI_TriaFilter;
322 }
323
324 //=======================================================================
325 // name    : SMESHGUI_TriangleFilter::IsNodeFilter
326 // Purpose : Returns true if filter is intended for nodes
327 //=======================================================================
328 bool SMESHGUI_TriangleFilter::IsNodeFilter() const
329 {
330   return false;
331 }
332
333
334
335
336
337
338
339
340
341
342