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