Salome HOME
Update of CheckDone
[modules/smesh.git] / src / SMESH / SMESH_Algo.hxx
1 // Copyright (C) 2007-2020  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, 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 //  SMESH SMESH : implementation of SMESH idl descriptions
24 //  File   : SMESH_Algo.hxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //
28 #ifndef _SMESH_ALGO_HXX_
29 #define _SMESH_ALGO_HXX_
30
31 #include "SMESH_SMESH.hxx"
32
33 #include "SMDSAbs_ElementType.hxx"
34 #include "SMESH_Comment.hxx"
35 #include "SMESH_ComputeError.hxx"
36 #include "SMESH_Hypothesis.hxx"
37
38 #include <GeomAbs_Shape.hxx>
39 #include <TopoDS_Shape.hxx>
40
41 #include <string>
42 #include <vector>
43 #include <list>
44 #include <map>
45 #include <set>
46
47 class SMDS_MeshNode;
48 class SMESHDS_Mesh;
49 class SMESHDS_SubMesh;
50 class SMESH_Gen;
51 class SMESH_HypoFilter;
52 class SMESH_Mesh;
53 class SMESH_MesherHelper;
54 class SMESH_ProxyMesh;
55 class SMESH_subMesh;
56 class TopoDS_Edge;
57 class TopoDS_Face;
58 class TopoDS_Shape;
59 class TopoDS_Vertex;
60 class TopoDS_Wire;
61 class gp_XYZ;
62
63 typedef std::map< SMESH_subMesh*, std::vector<int> >           MapShapeNbElems;
64 typedef std::map< SMESH_subMesh*, std::vector<int> >::iterator MapShapeNbElemsItr;
65
66 // ==================================================================================
67 /*!
68  * \brief Root of all algorithms
69  *
70  *  Methods of the class are grouped into several parts:
71  *  - main lifecycle methods, like Compute()
72  *  - methods describing features of the algorithm, like NeedShape()
73  *  - methods related to dependencies between sub-meshes imposed by the algorithm
74  *  - static utilities, like EdgeLength()
75  */
76 // ==================================================================================
77
78 class SMESH_EXPORT SMESH_Algo : public SMESH_Hypothesis
79 {
80  public:
81   //==================================================================================
82   /*!
83    * \brief Structure describing algorithm features
84    */
85   // --------------------------------------------------------------------------------
86   struct Features
87   {
88     int                            _dim;
89     std::set<SMDSAbs_GeometryType> _inElemTypes;  // acceptable types of input mesh element
90     std::set<SMDSAbs_GeometryType> _outElemTypes; // produced types of mesh elements
91     std::string                    _label;        // GUI type name
92
93     Features(): _dim( -1 ) {}
94     bool IsCompatible( const Features& algo2 ) const;
95   };
96   /*!
97    * \brief Return a structure describing algorithm features
98    */
99   static const Features& GetFeatures( const std::string& algoType );
100   const Features&        GetFeatures() const { return GetFeatures( _name ); }
101
102  public:
103   //==================================================================================
104   /*!
105    * \brief Creates algorithm
106     * \param hypId - algorithm ID
107     * \param gen - SMESH_Gen
108    */
109   SMESH_Algo(int hypId, SMESH_Gen * gen);
110
111   /*!
112    * \brief Destructor
113    */
114   virtual ~ SMESH_Algo();
115
116   /*!
117    * \brief Saves nothing in a stream
118     * \param save - the stream
119     * \retval std::ostream & - the stream
120    */
121   virtual std::ostream & SaveTo(std::ostream & save);
122
123   /*!
124    * \brief Loads nothing from a stream
125     * \param load - the stream
126     * \retval std::ostream & - the stream
127    */
128   virtual std::istream & LoadFrom(std::istream & load);
129
130   /*!
131    * \brief Return all types of compatible hypotheses
132    */
133   const std::vector < std::string > & GetCompatibleHypothesis();
134
135   /*!
136    * \brief Check hypothesis definition to mesh a shape
137     * \param aMesh - the mesh
138     * \param aShape - the shape
139     * \param aStatus - check result
140     * \retval bool - true if hypothesis is well defined
141     *
142     * Textual description of a problem can be stored in _comment field.
143    */
144   virtual bool CheckHypothesis(SMESH_Mesh&                          aMesh,
145                                const TopoDS_Shape&                  aShape,
146                                SMESH_Hypothesis::Hypothesis_Status& aStatus) = 0;
147   /*!
148    * \brief Computes mesh on a shape
149     * \param aMesh - the mesh
150     * \param aShape - the shape
151     * \retval bool - is a success
152     *
153     * Algorithms that !NeedDiscreteBoundary() || !OnlyUnaryInput() are
154     * to set SMESH_ComputeError returned by SMESH_submesh::GetComputeError()
155     * to report problematic sub-shapes
156    */
157   virtual bool Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape) = 0;
158
159   /*!
160    * \brief Computes mesh without geometry
161     * \param aMesh - the mesh
162     * \param aHelper - helper that must be used for adding elements to \aaMesh
163     * \retval bool - is a success
164     *
165     * The method is called if ( !aMesh->HasShapeToMesh() )
166    */
167   virtual bool Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper);
168
169   /*!
170    * \brief Return true if the algorithm can mesh a given shape
171    *  \param [in] aShape - shape to check
172    *  \param [in] toCheckAll - if true, this check returns OK if all shapes are OK,
173    *              else, returns OK if at least one shape is OK
174    *  \retval bool - \c true by default
175    */
176   virtual bool IsApplicableToShape(const TopoDS_Shape & shape, bool toCheckAll) const;
177
178   /*!
179    * \brief Sets _computeCanceled to true. It's usage depends on
180    *        implementation of a particular mesher.
181    */
182   virtual void CancelCompute();
183
184   /*!
185    * \brief If possible, returns progress of computation [0.,1.]
186    */
187   virtual double GetProgress() const;
188
189   /*!
190    * \brief evaluates size of prospective mesh on a shape
191    *  \param aMesh - the mesh
192    *  \param aShape - the shape
193    *  \param aResMap - prospective number of elements by SMDSAbs_ElementType by a sub-mesh
194    *  \retval bool - is a success
195    */
196   virtual bool Evaluate(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape,
197                         MapShapeNbElems& aResMap) = 0;
198
199   /*!
200    * \brief Return a list of compatible hypotheses used to mesh a shape
201     * \param aMesh - the mesh 
202     * \param aShape - the shape
203     * \param ignoreAuxiliary - do not include auxiliary hypotheses in the list
204     * \retval const std::list <const SMESHDS_Hypothesis*> - hypotheses list
205    * 
206    *  List the hypothesis used by the algorithm associated to the shape.
207    *  Hypothesis associated to father shape -are- taken into account (see
208    *  GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
209    *  the algorithm. This method could be surcharged by specific algorithms, in 
210    *  case of several hypothesis simultaneously applicable.
211    */
212   virtual const std::list <const SMESHDS_Hypothesis *> &
213   GetUsedHypothesis(SMESH_Mesh &         aMesh,
214                     const TopoDS_Shape & aShape,
215                     const bool           ignoreAuxiliary=true) const;
216   /*!
217    * \brief Return sub-shape to which hypotheses returned by GetUsedHypothesis() are assigned
218    */
219   virtual const std::list < TopoDS_Shape > & GetAssignedShapes() const;
220   /*!
221    * \brief Return the filter recognizing only compatible hypotheses
222    *  \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
223    *  \retval SMESH_HypoFilter* - the filter that can be NULL
224    */
225   const SMESH_HypoFilter* GetCompatibleHypoFilter(const bool ignoreAuxiliary) const;
226
227   /*!
228    * \brief Just return false as the algorithm does not hold parameters values
229    */
230   virtual bool SetParametersByMesh(const SMESH_Mesh* theMesh, const TopoDS_Shape& theShape);
231   virtual bool SetParametersByDefaults(const TDefaults& dflts, const SMESH_Mesh* theMesh=0);
232
233   /*!
234    * \brief return compute error
235    */
236   SMESH_ComputeErrorPtr GetComputeError() const;
237   /*!
238    * \brief initialize compute error etc. before call of Compute()
239    */
240   void InitComputeError();
241   /*!
242    * \brief Return compute progress by nb of calls of this method
243    */
244   double GetProgressByTic() const;
245   /*!
246    * Return a vector of sub-meshes to Compute()
247    */
248   std::vector<SMESH_subMesh*>& SubMeshesToCompute() { return _smToCompute; }
249
250 public:
251   // ==================================================================
252   // Algo features influencing how Compute() is called:
253   // in what turn and with what input shape
254   // ==================================================================
255
256   // SMESH_Hypothesis::GetDim();
257   // 1 - dimension of target mesh
258
259   bool OnlyUnaryInput() const { return _onlyUnaryInput; }
260   // 2 - is collection of tesselatable shapes inacceptable as input;
261   // "collection" means a shape containing shapes of dim equal
262   // to GetDim().
263   // Algo which can process a collection shape should expect
264   // an input temporary shape that is neither MainShape nor
265   // its child.
266
267   bool NeedDiscreteBoundary() const { return _requireDiscreteBoundary; }
268   // 3 - is a Dim-1 mesh prerequisite
269
270   bool NeedShape() const { return _requireShape; }
271   // 4 - is shape existence required
272
273   bool SupportSubmeshes() const { return _supportSubmeshes; }
274   // 5 - whether supports submeshes if !NeedDiscreteBoundary()
275
276   bool NeedLowerHyps(int dim) const { return _neededLowerHyps[ dim ]; }
277   // 6 - if algo !NeedDiscreteBoundary() but requires presence of
278   // hypotheses of dimension <dim> to generate all-dimensional mesh.
279   // This info is used not to issue warnings on hiding of lower global algos.
280
281 public:
282   // ==================================================================
283   // Methods to track non hierarchical dependencies between submeshes 
284   // ==================================================================
285
286   /*!
287    * \brief Sets event listener to submeshes if necessary
288     * \param subMesh - submesh where algo is set
289    *
290    * This method is called when a submesh gets HYP_OK algo_state.
291    * After being set, event listener is notified on each event of a submesh.
292    * By default none listener is set
293    */
294   virtual void SetEventListener(SMESH_subMesh* subMesh);
295   
296   /*!
297    * \brief Allow algo to do something after persistent restoration
298     * \param subMesh - restored submesh
299    *
300    * This method is called only if a submesh has HYP_OK algo_state.
301    */
302   virtual void SubmeshRestored(SMESH_subMesh* subMesh);
303   
304 public:
305   // ==================================================================
306   // Common algo utilities
307   // ==================================================================
308   /*!
309    * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
310    * \param theMesh - The mesh containing nodes
311    * \param theEdge - The geometrical edge of interest
312    * \param theParams - The resulting vector of sorted node parameters
313    * \retval bool - false if not all parameters are OK
314    * \warning Nodes moved to other geometry by MergeNodes() are NOT returned.
315    */
316   static bool GetNodeParamOnEdge(const SMESHDS_Mesh*     theMesh,
317                                  const TopoDS_Edge&      theEdge,
318                                  std::vector< double > & theParams);
319   /*!
320    * \brief Fill map of node parameter on geometrical edge to node it-self
321    * \param theMesh - The mesh containing nodes
322    * \param theEdge - The geometrical edge of interest
323    * \param theNodes - The resulting map
324    * \param ignoreMediumNodes - to store medium nodes of quadratic elements or not
325    * \param typeToCheck - type of elements to check for medium nodes
326    * \retval bool - false if not all parameters are OK
327    * \warning Nodes moved to other geometry by MergeNodes() are NOT returned.
328    */
329   static bool GetSortedNodesOnEdge(const SMESHDS_Mesh*                        theMesh,
330                                    const TopoDS_Edge&                         theEdge,
331                                    const bool                                 ignoreMediumNodes,
332                                    std::map< double, const SMDS_MeshNode* > & theNodes,
333                                    const SMDSAbs_ElementType                  typeToCheck = SMDSAbs_All);
334
335   /*!
336    * \brief Compute length of an edge
337     * \param E - the edge
338     * \retval double - the length
339    */
340   static double EdgeLength(const TopoDS_Edge & E);
341
342   int NumberOfPoints(SMESH_Mesh& aMesh,const TopoDS_Wire& W);
343
344   /*!
345    * \brief Return continuity of two edges
346     * \param E1 - the 1st edge
347     * \param E2 - the 2nd edge
348     * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
349    */
350   static GeomAbs_Shape Continuity(const TopoDS_Edge& E1, const TopoDS_Edge& E2);
351
352   /*!
353    * \brief Return true if an edge can be considered as a continuation of another
354    */
355   static bool IsContinuous(const TopoDS_Edge & E1, const TopoDS_Edge & E2) {
356     return ( Continuity( E1, E2 ) >= GeomAbs_G1 );
357   }
358   /*!
359    * \brief Return true if an edge can be considered straight
360    */
361   static bool IsStraight( const TopoDS_Edge & E, const bool degenResult=false );
362   /*!
363    * \brief Return true if an edge has no 3D curve
364    */
365   static bool isDegenerated( const TopoDS_Edge & E, const bool checkLength=false );
366
367   /*!
368    * \brief Return the node built on a vertex
369     * \param V - the vertex
370     * \param meshDS - mesh data structure
371     * \retval const SMDS_MeshNode* - found node or NULL
372    */
373   static const SMDS_MeshNode* VertexNode(const TopoDS_Vertex& V, const SMESHDS_Mesh* meshDS);
374
375   /*!
376    * \brief Return the node built on a vertex.
377    *        A node moved to other geometry by MergeNodes() is also returned.
378     * \param V - the vertex
379     * \param mesh - mesh
380     * \retval const SMDS_MeshNode* - found node or NULL
381    */
382   static const SMDS_MeshNode* VertexNode(const TopoDS_Vertex& V, const SMESH_Mesh* mesh);
383
384   /*!
385    * \brief Return the node built on a vertex.
386    *        A node moved to other geometry by MergeNodes() is also returned.
387     * \param V - the vertex
388     * \param edgeSM - sub-mesh of a meshed EDGE sharing the vertex
389     * \param mesh - the mesh
390     * \param checkV - if \c true, presence of a node on the vertex is checked
391     * \retval const SMDS_MeshNode* - found node or NULL
392    */
393   static const SMDS_MeshNode* VertexNode(const TopoDS_Vertex&   V,
394                                          const SMESHDS_SubMesh* edgeSM,
395                                          const SMESH_Mesh*      mesh,
396                                          const bool             checkV=true);
397
398   enum EMeshError { MEr_OK = 0, MEr_HOLES, MEr_BAD_ORI, MEr_EMPTY };
399
400   /*!
401    * \brief Finds topological errors of a sub-mesh 
402    */
403   static EMeshError GetMeshError(SMESH_subMesh* subMesh);
404
405  protected:
406
407   /*!
408    * \brief store error and comment and then return ( error == COMPERR_OK )
409    */
410   bool error(int error, const SMESH_Comment& comment = "");
411   /*!
412    * \brief store COMPERR_ALGO_FAILED error and comment and then return false
413    */
414   bool error(const SMESH_Comment& comment = "")
415   { return error(COMPERR_ALGO_FAILED, comment); }
416   /*!
417    * \brief store error and return error->IsOK()
418    */
419   bool error(SMESH_ComputeErrorPtr error);
420   /*!
421    * \brief store a bad input element preventing computation,
422    *        which may be a temporary one i.e. not residing the mesh,
423    *        then it will be deleted by InitComputeError()
424    */
425   void addBadInputElement(const SMDS_MeshElement* elem);
426
427   void addBadInputElements(const SMESHDS_SubMesh* sm,
428                            const bool             addNodes=false);
429
430 protected:
431
432   const SMESH_HypoFilter *              _compatibleAllHypFilter;
433   const SMESH_HypoFilter *              _compatibleNoAuxHypFilter;
434   std::vector<std::string>              _compatibleHypothesis;
435   std::list<const SMESHDS_Hypothesis *> _usedHypList;
436   std::list<TopoDS_Shape>               _assigedShapeList; // _usedHypList assigned to
437   
438
439   // Algo features influencing which Compute() and how is called:
440   // in what turn and with what input shape.
441   // These fields must be redefined if necessary by each descendant at constructor.
442   bool _onlyUnaryInput;         // mesh one shape of GetDim() at once. Default TRUE
443   bool _requireDiscreteBoundary;// GetDim()-1 mesh must be present. Default TRUE
444   bool _requireShape;           // work with GetDim()-1 mesh bound to geom only. Default TRUE
445   bool _supportSubmeshes;       // if !_requireDiscreteBoundary. Default FALSE
446   bool _neededLowerHyps[4];     // hyp dims needed by algo that !_requireDiscreteBoundary. Df. FALSE
447
448   // indicates if quadratic mesh creation is required,
449   // is usually set like this: _quadraticMesh = SMESH_MesherHelper::IsQuadraticSubMesh(shape)
450   bool _quadraticMesh;
451
452   int         _error;    //!< SMESH_ComputeErrorName or anything algo specific
453   std::string _comment;  //!< any text explaining what is wrong in Compute()
454   std::list<const SMDS_MeshElement*> _badInputElements; //!< to explain COMPERR_BAD_INPUT_MESH
455   const SMDS_Mesh* _mesh; //!< mesh being computed, needed to create SMESH_BadInputElements
456
457   volatile bool _computeCanceled; //!< is set to True while computing to stop it
458
459   double        _progress;        /* progress of Compute() [0.,1.],
460                                      to be set by an algo really tracking the progress */
461   int           _progressTic;     // counter of calls from SMESH_Mesh::GetComputeProgress()
462   std::vector<SMESH_subMesh*> _smToCompute; // sub-meshes to Compute()
463 };
464
465
466 class SMESH_EXPORT SMESH_0D_Algo: public SMESH_Algo
467 {
468 public:
469   SMESH_0D_Algo(int hypId, SMESH_Gen* gen);
470 };
471
472 class SMESH_EXPORT SMESH_1D_Algo: public SMESH_Algo
473 {
474 public:
475   SMESH_1D_Algo(int hypId, SMESH_Gen* gen);
476   /*!
477    * \brief Return true if the algorithm can mesh a given shape
478    *  \param [in] aShape - shape to check
479    *  \param [in] toCheckAll - if true, this check returns OK if all shapes are OK,
480    *              else, returns OK if at least one shape is OK
481    *  \retval bool - \c true by default
482    */
483   virtual bool IsApplicableToShape(const TopoDS_Shape & shape, bool toCheckAll) const;
484 };
485
486 class SMESH_EXPORT SMESH_2D_Algo: public SMESH_Algo
487 {
488 public:
489   SMESH_2D_Algo(int hypId, SMESH_Gen* gen);
490   /*!
491    * \brief Return true if the algorithm can mesh a given shape
492    *  \param [in] aShape - shape to check
493    *  \param [in] toCheckAll - if true, this check returns OK if all shapes are OK,
494    *              else, returns OK if at least one shape is OK
495    *  \retval bool - \c true by default
496    */
497   virtual bool IsApplicableToShape(const TopoDS_Shape & shape, bool toCheckAll) const;
498   /*!
499    * \brief Method in which an algorithm generating a structured mesh
500    *        fixes positions of in-face nodes after there movement
501    *        due to insertion of viscous layers.
502    */
503   virtual bool FixInternalNodes(const SMESH_ProxyMesh& mesh,
504                                 const TopoDS_Face&     face);
505 };
506
507 class SMESH_EXPORT SMESH_3D_Algo: public SMESH_Algo
508 {
509 public:
510   SMESH_3D_Algo(int hypId, SMESH_Gen* gen);
511   /*!
512    * \brief Return true if the algorithm can mesh a given shape
513    *  \param [in] aShape - shape to check
514    *  \param [in] toCheckAll - if true, this check returns OK if all shapes are OK,
515    *              else, returns OK if at least one shape is OK
516    *  \retval bool - \c true by default
517    */
518   virtual bool IsApplicableToShape(const TopoDS_Shape & shape, bool toCheckAll) const;
519 };
520
521 #endif