Salome HOME
PR: merged from V5_1_4rc1
[modules/smesh.git] / src / SMESH / SMESH_subMesh.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_subMesh.hxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28 //
29 #ifndef _SMESH_SUBMESH_HXX_
30 #define _SMESH_SUBMESH_HXX_
31
32 #include "SMESH_SMESH.hxx"
33
34 #include "SMESHDS_Mesh.hxx"
35 #include "SMESHDS_SubMesh.hxx"
36 #include "SMESH_Hypothesis.hxx"
37 #include "SMESH_ComputeError.hxx"
38 #include "SMESH_Algo.hxx"
39
40 #include "Utils_SALOME_Exception.hxx"
41
42 #include <TopoDS_Shape.hxx>
43
44 #include <list>
45 #include <map>
46
47 class SMESH_Mesh;
48 class SMESH_Hypothesis;
49 class SMESH_Algo;
50 class SMESH_Gen;
51 class SMESH_subMeshEventListener;
52 class SMESH_subMeshEventListenerData;
53 class SMESH_subMesh;
54
55 typedef SMESH_subMeshEventListener     EventListener;
56 typedef SMESH_subMeshEventListenerData EventListenerData;
57
58 typedef boost::shared_ptr< SMDS_Iterator<SMESH_subMesh*> > SMESH_subMeshIteratorPtr;
59
60
61 class SMESH_EXPORT SMESH_subMesh
62 {
63  public:
64   SMESH_subMesh(int Id, SMESH_Mesh * father, SMESHDS_Mesh * meshDS,
65                 const TopoDS_Shape & aSubShape);
66   virtual ~ SMESH_subMesh();
67
68   int GetId() const;
69
70   SMESH_Mesh* GetFather() { return _father; }
71   
72   SMESHDS_SubMesh * GetSubMeshDS();
73
74   SMESHDS_SubMesh* CreateSubMeshDS();
75   // Explicit SMESHDS_SubMesh creation method, required for persistence mechanism
76
77   SMESH_subMesh *GetFirstToCompute();
78
79   const std::map < int, SMESH_subMesh * >& DependsOn();
80   //const map < int, SMESH_subMesh * >&Dependants();
81   /*!
82    * \brief Return iterator on the submeshes this one depends on
83    */
84   SMESH_subMeshIteratorPtr getDependsOnIterator(const bool includeSelf,
85                                                 const bool complexShapeFirst);
86
87   const TopoDS_Shape & GetSubShape() const;
88
89   enum compute_state
90   {
91     NOT_READY, READY_TO_COMPUTE,
92     COMPUTE_OK, FAILED_TO_COMPUTE
93     };
94   enum algo_state
95   {
96     NO_ALGO, MISSING_HYP, HYP_OK
97     };
98   enum algo_event
99   {
100     ADD_HYP          , ADD_ALGO,
101     REMOVE_HYP       , REMOVE_ALGO,
102     ADD_FATHER_HYP   , ADD_FATHER_ALGO,
103     REMOVE_FATHER_HYP, REMOVE_FATHER_ALGO,
104     MODIF_HYP
105     };
106   enum compute_event
107   {
108     MODIF_ALGO_STATE, COMPUTE,
109     CLEAN, SUBMESH_COMPUTED, SUBMESH_RESTORED,
110     MESH_ENTITY_REMOVED, CHECK_COMPUTE_STATE
111     };
112   enum event_type
113   {
114     ALGO_EVENT, COMPUTE_EVENT
115   };
116
117   // ==================================================================
118   // Members to track non hierarchical dependencies between submeshes 
119   // ==================================================================
120
121   /*!
122    * \brief Sets an event listener and its data to a submesh
123     * \param listener - the listener to store
124     * \param data - the listener data to store
125     * \param where - the submesh to store the listener and it's data
126    * 
127    * The method remembers the submesh \awhere it puts the listener in order to delete
128    * them when HYP_OK algo_state is lost
129    * After being set, event listener is notified on each event of \awhere submesh.
130    */
131   void SetEventListener(EventListener*     listener,
132                         EventListenerData* data,
133                         SMESH_subMesh*     where);
134
135   /*!
136    * \brief Return an event listener data
137     * \param listener - the listener whose data is
138     * \retval EventListenerData* - found data, maybe NULL
139    */
140   EventListenerData* GetEventListenerData(EventListener* listener) const;
141
142   /*!
143    * \brief Unregister the listener and delete it and it's data
144     * \param listener - the event listener to delete
145    */
146   void DeleteEventListener(EventListener* listener);
147
148 protected:
149
150   //!< event listeners to notify
151   std::map< EventListener*, EventListenerData* >           myEventListeners;
152   //!< event listeners to delete when HYP_OK algo_state is lost
153   std::list< std::pair< SMESH_subMesh*, EventListener* > > myOwnListeners;
154
155   /*!
156    * \brief Sets an event listener and its data to a submesh
157     * \param listener - the listener to store
158     * \param data - the listener data to store
159    * 
160    * After being set, event listener is notified on each event of a submesh.
161    */
162   void SetEventListener(EventListener* listener, EventListenerData* data);
163
164   /*!
165    * \brief Notify stored event listeners on the occured event
166    * \param event - algo_event or compute_event itself
167    * \param eventType - algo_event or compute_event
168    * \param hyp - hypothesis, if eventType is algo_event
169    */
170   void NotifyListenersOnEvent( const int         event,
171                                const event_type  eventType,
172                                SMESH_Hypothesis* hyp = 0);
173
174   /*!
175    * \brief Delete event listeners depending on algo of this submesh
176    */
177   void DeleteOwnListeners();
178
179   // ==================================================================
180
181 public:
182
183   SMESH_Hypothesis::Hypothesis_Status
184     AlgoStateEngine(int event, SMESH_Hypothesis * anHyp);
185
186   SMESH_Hypothesis::Hypothesis_Status
187     SubMeshesAlgoStateEngine(int event, SMESH_Hypothesis * anHyp);
188
189   int GetAlgoState() const { return _algoState; }
190   int GetComputeState() const { return _computeState; };
191   SMESH_ComputeErrorPtr& GetComputeError() { return _computeError; }
192
193   void DumpAlgoState(bool isMain);
194
195   bool ComputeStateEngine(int event);
196
197   bool Evaluate(MapShapeNbElems& aResMap);
198
199   bool IsConform(const SMESH_Algo* theAlgo);
200   // check if a conform mesh will be produced by the Algo
201
202   bool CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const;
203   // return true if theHypothesis can be attached to me:
204   // its dimention is checked
205
206   static bool IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
207                                     const TopAbs_ShapeEnum  theShapeType);
208
209   bool IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis) const
210   { return IsApplicableHypotesis( theHypothesis, _subShape.ShapeType() ); }
211   // return true if theHypothesis can be used to mesh me:
212   // its shape type is checked
213   
214   SMESH_Hypothesis::Hypothesis_Status CheckConcurentHypothesis (const int theHypType);
215   // check if there are several applicable hypothesis on fathers
216
217   /*!
218    * \brief Return true if no mesh entities is bound to the submesh
219    */
220   bool IsEmpty() const;
221
222   bool IsMeshComputed() const;
223   // check if _subMeshDS contains mesh elements
224
225   /*!
226    * \brief Allow algo->Compute() if a subshape of lower dim is meshed but
227    *        none mesh entity is bound to it
228    */
229   void SetIsAlwaysComputed(bool isAlCo);
230   bool IsAlwaysComputed() { return _alwaysComputed; }
231
232   
233   /*!
234    * \brief  Find common submeshes (based on shared subshapes with other
235    * \param theOther submesh to check
236    * \param theCommonIds set of common submesh IDs
237    * NOTE: this method does not cleat set before collect common IDs
238    */
239   bool FindIntersection( const SMESH_subMesh *           theOther,
240                          std::set<const SMESH_subMesh*>& theSetOfCommon ) const;
241
242 protected:
243   // ==================================================================
244   void InsertDependence(const TopoDS_Shape aSubShape);
245
246   bool SubMeshesComputed();
247
248   bool SubMeshesReady();
249
250   void RemoveSubMeshElementsAndNodes();
251   void UpdateDependantsState(const compute_event theEvent);
252   void UpdateSubMeshState(const compute_state theState);
253   void ComputeSubMeshStateEngine(int event);
254   void CleanDependants();
255   void CleanDependsOn();
256   void SetAlgoState(int state);
257
258   /*!
259    * \brief Return a shape containing all sub-shapes of the MainShape that can be
260    * meshed at once along with _subShape
261    */
262   TopoDS_Shape GetCollection(SMESH_Gen * theGen,
263                              SMESH_Algo* theAlgo,
264                              bool &      theSubComputed);
265
266   /*!
267    * \brief Apply theAlgo to all subshapes in theCollection
268    */
269   bool ApplyToCollection (SMESH_Algo*         theAlgo,
270                           const TopoDS_Shape& theCollection);
271
272   /*!
273    * \brief Update compute_state by _computeError
274     * \retval bool - false if there are errors
275    */
276   bool CheckComputeError(SMESH_Algo* theAlgo, const TopoDS_Shape& theShape=TopoDS_Shape());
277
278   /*!
279    * \brief Return a hypothesis attached to theShape.
280    * 
281    * If theHyp is provided, similar but not same hypotheses
282    * is returned; else an applicable ones having theHypType
283    * is returned
284    */
285   const SMESH_Hypothesis* GetSimilarAttached(const TopoDS_Shape&      theShape,
286                                              const SMESH_Hypothesis * theHyp,
287                                              const int                theHypType = 0);
288   // 
289
290 protected:
291
292   TopoDS_Shape          _subShape;
293   SMESHDS_SubMesh *     _subMeshDS;
294   SMESH_Mesh *          _father;
295   int                   _Id;
296
297   std::map < int, SMESH_subMesh * >_mapDepend;
298   bool                  _dependenceAnalysed;
299
300   int                   _algoState;
301   int                   _computeState;
302   SMESH_ComputeErrorPtr _computeError;
303
304   // allow algo->Compute() if a subshape of lower dim is meshed but
305   // none mesh entity is bound to it. Eg StdMeshers_CompositeSegment_1D can
306   // mesh several edges as a whole and leave some of them  without mesh entities
307   bool                  _alwaysComputed;
308
309 };
310
311 #endif