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