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