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