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