Salome HOME
IMP19942 - Convert group on geometry into group of elements
[modules/smesh.git] / src / SMESH / SMESH_Algo.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_Algo.hxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 #ifndef _SMESH_ALGO_HXX_
30 #define _SMESH_ALGO_HXX_
31
32 #include "SMESH_SMESH.hxx"
33
34 #include "SMESH_Hypothesis.hxx"
35 #include "SMESH_ComputeError.hxx"
36 #include "SMESH_Comment.hxx"
37
38 #include <TopoDS_Shape.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <GeomAbs_Shape.hxx>
41
42 #include <string>
43 #include <vector>
44 #include <list>
45 #include <map>
46
47 class SMESH_Gen;
48 class SMESH_Mesh;
49 class SMESH_HypoFilter;
50 class TopoDS_Vertex;
51 class TopoDS_Face;
52 class TopoDS_Shape;
53 class SMESHDS_Mesh;
54 class SMDS_MeshNode;
55 class SMESH_subMesh;
56 class SMESH_MesherHelper;
57
58
59 class SMESH_EXPORT SMESH_Algo:public SMESH_Hypothesis
60 {
61 public:
62   /*!
63    * \brief Creates algorithm
64     * \param hypId - algorithm ID
65     * \param studyId - study ID
66     * \param gen - SMESH_Gen
67    */
68   SMESH_Algo(int hypId, int studyId, SMESH_Gen * gen);
69
70   /*!
71    * \brief Destructor
72    */
73   virtual ~ SMESH_Algo();
74
75   /*!
76    * \brief Saves nothing in a stream
77     * \param save - the stream
78     * \retval virtual std::ostream & - the stream
79    */
80   virtual std::ostream & SaveTo(std::ostream & save);
81
82   /*!
83    * \brief Loads nothing from a stream
84     * \param load - the stream
85     * \retval virtual std::ostream & - the stream
86    */
87   virtual std::istream & LoadFrom(std::istream & load);
88
89   /*!
90    * \brief Returns all types of compatible hypotheses
91    */
92   const std::vector < std::string > & GetCompatibleHypothesis();
93
94   /*!
95    * \brief Check hypothesis definition to mesh a shape
96     * \param aMesh - the mesh
97     * \param aShape - the shape
98     * \param aStatus - check result
99     * \retval bool - true if hypothesis is well defined
100    */
101   virtual bool CheckHypothesis(SMESH_Mesh&                          aMesh,
102                                const TopoDS_Shape&                  aShape,
103                                SMESH_Hypothesis::Hypothesis_Status& aStatus) = 0;
104   /*!
105    * \brief Computes mesh on a shape
106     * \param aMesh - the mesh
107     * \param aShape - the shape
108     * \retval bool - is a success
109     *
110     * Algorithms that !NeedDescretBoundary() || !OnlyUnaryInput() are
111     * to set SMESH_ComputeError returned by SMESH_submesh::GetComputeError()
112     * to report problematic subshapes
113    */
114   virtual bool Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape) = 0;
115
116   /*!
117    * \brief Computes mesh without geometry
118     * \param aMesh - the mesh
119     * \param aHelper - helper that must be used for adding elements to \aaMesh
120     * \retval bool - is a success
121     *
122     * The method is called if ( !aMesh->HasShapeToMesh() )
123    */
124   virtual bool Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper);
125
126   /*!
127    * \brief Returns a list of compatible hypotheses used to mesh a shape
128     * \param aMesh - the mesh 
129     * \param aShape - the shape
130     * \param ignoreAuxiliary - do not include auxiliary hypotheses in the list
131     * \retval const std::list <const SMESHDS_Hypothesis*> - hypotheses list
132    * 
133    *  List the hypothesis used by the algorithm associated to the shape.
134    *  Hypothesis associated to father shape -are- taken into account (see
135    *  GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
136    *  the algorithm. This method could be surcharged by specific algorithms, in 
137    *  case of several hypothesis simultaneously applicable.
138    */
139   virtual const std::list <const SMESHDS_Hypothesis *> &
140   GetUsedHypothesis(SMESH_Mesh &         aMesh,
141                     const TopoDS_Shape & aShape,
142                     const bool           ignoreAuxiliary=true);
143   /*!
144    * \brief Returns a list of compatible hypotheses assigned to a shape in a mesh
145     * \param aMesh - the mesh 
146     * \param aShape - the shape
147     * \param ignoreAuxiliary - do not include auxiliary hypotheses in the list
148     * \retval const std::list <const SMESHDS_Hypothesis*> - hypotheses list
149    * 
150    *  List the relevant hypothesis associated to the shape. Relevant hypothesis
151    *  have a name (type) listed in the algorithm. Hypothesis associated to
152    *  father shape -are not- taken into account (see GetUsedHypothesis)
153    */
154   const list <const SMESHDS_Hypothesis *> &
155   GetAppliedHypothesis(SMESH_Mesh &         aMesh,
156                        const TopoDS_Shape & aShape,
157                        const bool           ignoreAuxiliary=true);
158   /*!
159    * \brief Make the filter recognize only compatible hypotheses
160    * \param theFilter - the filter to initialize
161    * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
162    * \retval bool - true if the algo has compatible hypotheses
163    */
164   bool InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
165                                  const bool         ignoreAuxiliary) const;
166   /*!
167    * \brief Initialize my parameter values by the mesh built on the geometry
168    *
169    * Just return false as the algorithm does not hold parameters values
170    */
171   virtual bool SetParametersByMesh(const SMESH_Mesh* theMesh,
172                                    const TopoDS_Shape& theShape);
173   /*!
174    * \brief return compute error
175    */
176   SMESH_ComputeErrorPtr GetComputeError() const;
177   /*!
178    * \brief initialize compute error
179    */
180   void InitComputeError();
181
182 public:
183   // ==================================================================
184   // Algo features influencing how Compute() is called:
185   // in what turn and with what input shape
186   // ==================================================================
187
188   // SMESH_Hypothesis::GetDim();
189   // 1 - dimention of target mesh
190
191   bool OnlyUnaryInput() const { return _onlyUnaryInput; }
192   // 2 - is collection of tesselatable shapes inacceptable as input;
193   // "collection" means a shape containing shapes of dim equal
194   // to GetDim().
195   // Algo which can process a collection shape should expect
196   // an input temporary shape that is neither MainShape nor
197   // its child.
198
199   bool NeedDescretBoundary() const { return _requireDescretBoundary; }
200   // 3 - is a Dim-1 mesh prerequisite
201
202   bool NeedShape() const { return _requireShape; }
203   // 4 - is shape existance required
204
205   bool SupportSubmeshes() const { return _supportSubmeshes; }
206   // 5 - whether supports submeshes if !NeedDescretBoundary()
207
208
209 public:
210   // ==================================================================
211   // Methods to track non hierarchical dependencies between submeshes 
212   // ==================================================================
213
214   /*!
215    * \brief Sets event listener to submeshes if necessary
216     * \param subMesh - submesh where algo is set
217    *
218    * This method is called when a submesh gets HYP_OK algo_state.
219    * After being set, event listener is notified on each event of a submesh.
220    * By default non listener is set
221    */
222   virtual void SetEventListener(SMESH_subMesh* subMesh);
223   
224   /*!
225    * \brief Allow algo to do something after persistent restoration
226     * \param subMesh - restored submesh
227    *
228    * This method is called only if a submesh has HYP_OK algo_state.
229    */
230   virtual void SubmeshRestored(SMESH_subMesh* subMesh);
231   
232 public:
233   // ==================================================================
234   // Common algo utilities
235   // ==================================================================
236   /*!
237    * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
238    * \param theMesh - The mesh containing nodes
239    * \param theEdge - The geometrical edge of interest
240    * \param theParams - The resulting vector of sorted node parameters
241    * \retval bool - false if not all parameters are OK
242    */
243   static bool GetNodeParamOnEdge(const SMESHDS_Mesh*     theMesh,
244                                  const TopoDS_Edge&      theEdge,
245                                  std::vector< double > & theParams);
246   /*!
247    * \brief Fill map of node parameter on geometrical edge to node it-self
248    * \param theMesh - The mesh containing nodes
249    * \param theEdge - The geometrical edge of interest
250    * \param theNodes - The resulting map
251    * \param ignoreMediumNodes - to store medium nodes of quadratic elements or not
252    * \retval bool - false if not all parameters are OK
253    */
254   static bool GetSortedNodesOnEdge(const SMESHDS_Mesh*                        theMesh,
255                                    const TopoDS_Edge&                         theEdge,
256                                    const bool                                 ignoreMediumNodes,
257                                    std::map< double, const SMDS_MeshNode* > & theNodes);
258   /*!
259    * \brief Find out elements orientation on a geometrical face
260    * \param theFace - The face correctly oriented in the shape being meshed
261    * \param theMeshDS - The mesh data structure
262    * \retval bool - true if the face normal and the normal of first element
263    *                in the correspoding submesh point in different directions
264    */
265   static bool IsReversedSubMesh (const TopoDS_Face&  theFace,
266                                  SMESHDS_Mesh*       theMeshDS);
267   /*!
268    * \brief Compute length of an edge
269     * \param E - the edge
270     * \retval double - the length
271    */
272   static double EdgeLength(const TopoDS_Edge & E);
273
274   /*!
275    * \brief Return continuity of two edges
276     * \param E1 - the 1st edge
277     * \param E2 - the 2nd edge
278     * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
279    */
280   static GeomAbs_Shape Continuity(const TopoDS_Edge & E1, const TopoDS_Edge & E2);
281
282   /*!
283    * \brief Return true if an edge can be considered as a continuation of another
284    */
285   static bool IsContinuous(const TopoDS_Edge & E1, const TopoDS_Edge & E2) {
286     return ( Continuity( E1, E2 ) >= GeomAbs_G1 );
287   }
288
289   /*!
290    * \brief Return the node built on a vertex
291     * \param V - the vertex
292     * \param meshDS - mesh
293     * \retval const SMDS_MeshNode* - found node or NULL
294    */
295   static const SMDS_MeshNode* VertexNode(const TopoDS_Vertex& V,
296                                          SMESHDS_Mesh*        meshDS);
297
298 protected:
299
300   /*!
301    * \brief store error and comment and then return ( error == COMPERR_OK )
302    */
303   bool error(int error, const SMESH_Comment& comment = "");
304   /*!
305    * \brief store COMPERR_ALGO_FAILED error and comment and then return false
306    */
307   bool error(const SMESH_Comment& comment = "")
308   { return error(COMPERR_ALGO_FAILED, comment); }
309   /*!
310    * \brief store error and return error->IsOK()
311    */
312   bool error(SMESH_ComputeErrorPtr error);
313   /*!
314    * \brief store a bad input element preventing computation,
315    *        which may be a temporary one i.e. not residing the mesh,
316    *        then it will be deleted by InitComputeError()
317    */
318   void addBadInputElement(const SMDS_MeshElement* elem);
319
320 protected:
321
322   std::vector<std::string>              _compatibleHypothesis;
323   std::list<const SMESHDS_Hypothesis *> _appliedHypList;
324   std::list<const SMESHDS_Hypothesis *> _usedHypList;
325
326   // Algo features influencing which Compute() and how is called:
327   // in what turn and with what input shape.
328   // This fields must be redefined if necessary by each descendant at constructor.
329   bool _onlyUnaryInput;         // mesh one shape of GetDim() at once. Default TRUE
330   bool _requireDescretBoundary; // GetDim()-1 mesh must be present. Default TRUE
331   bool _requireShape;           // work with GetDim()-1 mesh bound to geom only. Default TRUE
332   bool _supportSubmeshes;       // if !_requireDescretBoundary. Default FALSE
333
334   // quadratic mesh creation required,
335   // is usually set trough SMESH_MesherHelper::IsQuadraticSubMesh()
336   bool _quadraticMesh;
337
338   int         _error;    //!< SMESH_ComputeErrorName or anything algo specific
339   std::string _comment;  //!< any text explaining what is wrong in Compute()
340   std::list<const SMDS_MeshElement*> _badInputElements; //!< to explain COMPERR_BAD_INPUT_MESH
341 };
342
343 #endif