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