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