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