Salome HOME
7bcbe813c3f522770d1cfea1a9c0cc48a94f9c8c
[modules/smesh.git] / src / SMESH / SMESH_HypoFilter.hxx
1 // Copyright (C) 2007-2013  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
23 //  SMESH SMESH : implementaion of SMESH idl descriptions
24 //  File   : SMESH_HypoFilter.hxx
25 //  Module : SMESH
26 //
27 #ifndef SMESH_HypoFilter_HeaderFile
28 #define SMESH_HypoFilter_HeaderFile
29
30 #include "SMESH_SMESH.hxx"
31
32 // ===========================
33 // Filter of SMESH_Hypothesis
34 // ===========================
35
36 #include <list>
37 #include <string>
38 #include <TopoDS_Shape.hxx>
39 #include <TopTools_MapOfShape.hxx>
40
41 class SMESH_HypoFilter;
42 class SMESH_Hypothesis;
43 class SMESH_Mesh;
44
45 class SMESH_EXPORT SMESH_HypoPredicate {
46  public:
47   virtual bool IsOk(const SMESH_Hypothesis* aHyp,
48                     const TopoDS_Shape&     aShape) const = 0;
49   // check aHyp or/and aShape it is assigned to
50   virtual ~SMESH_HypoPredicate() {}
51  private:
52   int _logical_op;
53   friend class SMESH_HypoFilter;
54 };
55
56 class SMESH_EXPORT SMESH_HypoFilter: public SMESH_HypoPredicate
57 {
58  public:
59   // Create and add predicates.
60   // Added predicates will be destroyed by filter when it dies
61   SMESH_HypoFilter();
62   SMESH_HypoFilter( SMESH_HypoPredicate* aPredicate, bool notNagate = true );
63   // notNagate==false means !aPredicate->IsOk()
64   SMESH_HypoFilter & Init  ( SMESH_HypoPredicate* aPredicate, bool notNagate = true );
65   SMESH_HypoFilter & And   ( SMESH_HypoPredicate* aPredicate );
66   SMESH_HypoFilter & AndNot( SMESH_HypoPredicate* aPredicate );
67   SMESH_HypoFilter & Or    ( SMESH_HypoPredicate* aPredicate );
68   SMESH_HypoFilter & OrNot ( SMESH_HypoPredicate* aPredicate );
69
70   // Create predicates
71   static SMESH_HypoPredicate* IsAlgo();
72   static SMESH_HypoPredicate* IsAuxiliary();
73   static SMESH_HypoPredicate* IsApplicableTo(const TopoDS_Shape& theShape);
74   static SMESH_HypoPredicate* IsAssignedTo(const TopoDS_Shape& theShape);
75   static SMESH_HypoPredicate* Is(const SMESH_Hypothesis* theHypo);
76   static SMESH_HypoPredicate* IsGlobal(const TopoDS_Shape& theMainShape);
77   static SMESH_HypoPredicate* IsMoreLocalThan(const TopoDS_Shape& theShape,
78                                               const SMESH_Mesh&   theMesh);
79   static SMESH_HypoPredicate* HasName(const std::string & theName);
80   static SMESH_HypoPredicate* HasDim(const int theDim);
81   static SMESH_HypoPredicate* HasType(const int theHypType);
82
83   bool IsEmpty() const { return myPredicates.empty(); }
84
85   /*!
86    * \brief check aHyp or/and aShape it is assigned to
87    */
88   bool IsOk (const SMESH_Hypothesis* aHyp,
89              const TopoDS_Shape&     aShape) const;
90   /*!
91    * \brief return true if contains no predicates
92    */
93   bool IsAny() const { return myPredicates.empty(); }
94
95   ~SMESH_HypoFilter();
96
97
98  protected:
99   // fields
100
101   std::list<SMESH_HypoPredicate*> myPredicates;
102
103   // private methods
104
105   enum Logical { AND, AND_NOT, OR, OR_NOT };
106   enum Comparison { EQUAL, NOT_EQUAL, MORE, LESS };
107
108   SMESH_HypoFilter(const SMESH_HypoFilter& other){}
109
110   void add( Logical bool_op, SMESH_HypoPredicate* pred )
111   {
112     if ( pred ) {
113       pred->_logical_op = bool_op;
114       myPredicates.push_back( pred );
115     }
116   }
117
118   // predicates implementation
119
120   template <typename TValue>
121     struct templPredicate: public SMESH_HypoPredicate {
122       Comparison _comp;
123       TValue     _val;
124       virtual TValue Value(const SMESH_Hypothesis* aHyp) const = 0;
125       virtual bool IsOk(const SMESH_Hypothesis* aHyp, const TopoDS_Shape& ) const
126       {
127         if      ( _comp == EQUAL )     return _val == Value( aHyp );
128         else if ( _comp == NOT_EQUAL ) return _val != Value( aHyp );
129         else if ( _comp == MORE )      return _val < Value( aHyp );
130         else                           return _val > Value( aHyp );
131       }
132     };
133
134   struct NamePredicate : public SMESH_HypoPredicate {
135     std::string _name;
136     NamePredicate( std::string name ): _name(name){}
137     bool IsOk(const SMESH_Hypothesis* aHyp,
138               const TopoDS_Shape&     aShape) const;
139   };
140   
141   struct TypePredicate : public templPredicate< int > {
142     TypePredicate( Comparison comp, int hypType )
143     { _comp = comp; _val = hypType; }
144     int Value( const SMESH_Hypothesis* aHyp ) const;
145   };
146   
147   struct DimPredicate : public templPredicate< int > {
148     DimPredicate( Comparison comp, int dim )
149     { _comp = comp; _val = dim; }
150     int Value( const SMESH_Hypothesis* aHyp ) const;
151   };
152   
153   struct ApplicablePredicate : public SMESH_HypoPredicate {
154     int _shapeType;
155     ApplicablePredicate( const TopoDS_Shape& theShape );
156     bool IsOk(const SMESH_Hypothesis* aHyp,
157               const TopoDS_Shape&     aShape) const;
158   };
159         
160   struct InstancePredicate : public SMESH_HypoPredicate {
161     const SMESH_Hypothesis* _hypo;
162     InstancePredicate( const SMESH_Hypothesis* hypo ):_hypo(hypo){}
163     bool IsOk(const SMESH_Hypothesis* aHyp,
164               const TopoDS_Shape&     aShape) const;
165   };
166         
167   struct IsAssignedToPredicate : public SMESH_HypoPredicate {
168     TopoDS_Shape _mainShape;
169     IsAssignedToPredicate( const TopoDS_Shape& mainShape ):_mainShape(mainShape){}
170     bool IsOk(const SMESH_Hypothesis* aHyp,
171               const TopoDS_Shape&     aShape) const;
172   };
173         
174   struct IsMoreLocalThanPredicate : public SMESH_HypoPredicate {
175     TopoDS_Shape        _shape;
176     const SMESH_Mesh&   _mesh;
177     TopTools_MapOfShape _preferableShapes;
178     IsMoreLocalThanPredicate( const TopoDS_Shape& shape,
179                               const SMESH_Mesh&   mesh )
180       :_shape(shape),_mesh(mesh) { findPreferable(); }
181     bool IsOk(const SMESH_Hypothesis* aHyp,
182               const TopoDS_Shape&     aShape) const;
183     void findPreferable();
184   };
185         
186   struct IsAuxiliaryPredicate : public SMESH_HypoPredicate {
187     bool IsOk(const SMESH_Hypothesis* aHyp,
188               const TopoDS_Shape&     aShape) const;
189   };
190         
191 };
192
193
194 #endif