Salome HOME
8aeaf7bb67636f791f51f7a2ac0ab62f5aef2aeb
[modules/smesh.git] / src / SMESH / SMESH_Algo.hxx
1 // Copyright (C) 2007-2014  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 : implementaion 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 <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 #include <set>
47
48 class SMDS_MeshNode;
49 class SMESHDS_Mesh;
50 class SMESHDS_SubMesh;
51 class SMESH_Gen;
52 class SMESH_HypoFilter;
53 class SMESH_Mesh;
54 class SMESH_MesherHelper;
55 class SMESH_ProxyMesh;
56 class SMESH_subMesh;
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 algorith
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     bool IsCompatible( const Features& algo2 ) const;
94   };
95   /*!
96    * \brief Returns 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 studyId - study ID
107     * \param gen - SMESH_Gen
108    */
109   SMESH_Algo(int hypId, int studyId, 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 Returns 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   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 Sets _computeCanceled to true. It's usage depends on
169    *        implementation of a particular mesher.
170    */
171   virtual void CancelCompute();
172
173   /*!
174    * \brief If possible, returns progress of computation [0.,1.]
175    */
176   virtual double GetProgress() const;
177
178   /*!
179    * \brief evaluates size of prospective mesh on a shape
180     * \param aMesh - the mesh
181     * \param aShape - the shape
182     * \param aNbElems - prospective number of elements by types
183     * \retval bool - is a success
184    */
185   virtual bool Evaluate(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape,
186                         MapShapeNbElems& aResMap) = 0;
187
188   /*!
189    * \brief Returns a list of compatible hypotheses used to mesh a shape
190     * \param aMesh - the mesh 
191     * \param aShape - the shape
192     * \param ignoreAuxiliary - do not include auxiliary hypotheses in the list
193     * \retval const std::list <const SMESHDS_Hypothesis*> - hypotheses list
194    * 
195    *  List the hypothesis used by the algorithm associated to the shape.
196    *  Hypothesis associated to father shape -are- taken into account (see
197    *  GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
198    *  the algorithm. This method could be surcharged by specific algorithms, in 
199    *  case of several hypothesis simultaneously applicable.
200    */
201   virtual const std::list <const SMESHDS_Hypothesis *> &
202   GetUsedHypothesis(SMESH_Mesh &         aMesh,
203                     const TopoDS_Shape & aShape,
204                     const bool           ignoreAuxiliary=true) const;
205   /*!
206    * \brief Returns a list of compatible hypotheses assigned to a shape in a mesh
207     * \param aMesh - the mesh 
208     * \param aShape - the shape
209     * \param ignoreAuxiliary - do not include auxiliary hypotheses in the list
210     * \retval const std::list <const SMESHDS_Hypothesis*> - hypotheses list
211    * 
212    *  List the relevant hypothesis associated to the shape. Relevant hypothesis
213    *  have a name (type) listed in the algorithm. Hypothesis associated to
214    *  father shape -are not- taken into account (see GetUsedHypothesis)
215    */
216   const list <const SMESHDS_Hypothesis *> &
217   GetAppliedHypothesis(SMESH_Mesh &         aMesh,
218                        const TopoDS_Shape & aShape,
219                        const bool           ignoreAuxiliary=true) const;
220   /*!
221    * \brief Make the filter recognize only compatible hypotheses
222    * \param theFilter - the filter to initialize
223    * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
224    * \retval bool - true if the algo has compatible hypotheses
225    */
226   bool InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
227                                  const bool         ignoreAuxiliary) const;
228   /*!
229    * \brief Just return false as the algorithm does not hold parameters values
230    */
231   virtual bool SetParametersByMesh(const SMESH_Mesh* theMesh, const TopoDS_Shape& theShape);
232   virtual bool SetParametersByDefaults(const TDefaults& dflts, const SMESH_Mesh* theMesh=0);
233
234   /*!
235    * \brief return compute error
236    */
237   SMESH_ComputeErrorPtr GetComputeError() const;
238   /*!
239    * \brief initialize compute error etc. before call of Compute()
240    */
241   void InitComputeError();
242   /*!
243    * \brief Return compute progress by nb of calls of this method
244    */
245   double GetProgressByTic() const;
246   /*!
247    * Return a vector of sub-meshes to Compute()
248    */
249   std::vector<SMESH_subMesh*>& SubMeshesToCompute() { return _smToCompute; }
250
251 public:
252   // ==================================================================
253   // Algo features influencing how Compute() is called:
254   // in what turn and with what input shape
255   // ==================================================================
256
257   // SMESH_Hypothesis::GetDim();
258   // 1 - dimention of target mesh
259
260   bool OnlyUnaryInput() const { return _onlyUnaryInput; }
261   // 2 - is collection of tesselatable shapes inacceptable as input;
262   // "collection" means a shape containing shapes of dim equal
263   // to GetDim().
264   // Algo which can process a collection shape should expect
265   // an input temporary shape that is neither MainShape nor
266   // its child.
267
268   bool NeedDiscreteBoundary() const { return _requireDiscreteBoundary; }
269   // 3 - is a Dim-1 mesh prerequisite
270
271   bool NeedShape() const { return _requireShape; }
272   // 4 - is shape existance required
273
274   bool SupportSubmeshes() const { return _supportSubmeshes; }
275   // 5 - whether supports submeshes if !NeedDiscreteBoundary()
276
277   bool NeedLowerHyps(int dim) const { return _neededLowerHyps[ dim ]; }
278   // 6 - if algo !NeedDiscreteBoundary() but requires presence of
279   // hypotheses of dimension <dim> to generate all-dimensional mesh.
280   // This info is used not to issue warnings on hiding of lower global algos.
281
282 public:
283   // ==================================================================
284   // Methods to track non hierarchical dependencies between submeshes 
285   // ==================================================================
286
287   /*!
288    * \brief Sets event listener to submeshes if necessary
289     * \param subMesh - submesh where algo is set
290    *
291    * This method is called when a submesh gets HYP_OK algo_state.
292    * After being set, event listener is notified on each event of a submesh.
293    * By default none listener is set
294    */
295   virtual void SetEventListener(SMESH_subMesh* subMesh);
296   
297   /*!
298    * \brief Allow algo to do something after persistent restoration
299     * \param subMesh - restored submesh
300    *
301    * This method is called only if a submesh has HYP_OK algo_state.
302    */
303   virtual void SubmeshRestored(SMESH_subMesh* subMesh);
304   
305 public:
306   // ==================================================================
307   // Common algo utilities
308   // ==================================================================
309   /*!
310    * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
311    * \param theMesh - The mesh containing nodes
312    * \param theEdge - The geometrical edge of interest
313    * \param theParams - The resulting vector of sorted node parameters
314    * \retval bool - false if not all parameters are OK
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    * \retval bool - false if not all parameters are OK
326    */
327   static bool GetSortedNodesOnEdge(const SMESHDS_Mesh*                        theMesh,
328                                    const TopoDS_Edge&                         theEdge,
329                                    const bool                                 ignoreMediumNodes,
330                                    std::map< double, const SMDS_MeshNode* > & theNodes);
331   /*!
332    * Moved to SMESH_MesherHelper
333    */
334   // static bool IsReversedSubMesh (const TopoDS_Face&  theFace,
335   //                                SMESHDS_Mesh*       theMeshDS);
336   /*!
337    * \brief Compute length of an edge
338     * \param E - the edge
339     * \retval double - the length
340    */
341   static double EdgeLength(const TopoDS_Edge & E);
342
343   //static int NumberOfWires(const TopoDS_Shape& S);
344   int NumberOfPoints(SMESH_Mesh& aMesh,const TopoDS_Wire& W);
345
346   /*!
347    * \brief Return continuity of two edges
348     * \param E1 - the 1st edge
349     * \param E2 - the 2nd edge
350     * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
351    */
352   static GeomAbs_Shape Continuity(TopoDS_Edge E1, TopoDS_Edge E2);
353
354   /*!
355    * \brief Return true if an edge can be considered as a continuation of another
356    */
357   static bool IsContinuous(const TopoDS_Edge & E1, const TopoDS_Edge & E2) {
358     return ( Continuity( E1, E2 ) >= GeomAbs_G1 );
359   }
360   /*!
361    * \brief Return true if an edge can be considered straight
362    */
363   static bool IsStraight( const TopoDS_Edge & E, const bool degenResult=false );
364   /*!
365    * \brief Return true if an edge has no 3D curve
366    */
367   static bool isDegenerated( const TopoDS_Edge & E );
368
369   /*!
370    * \brief Return the node built on a vertex
371     * \param V - the vertex
372     * \param meshDS - mesh
373     * \retval const SMDS_MeshNode* - found node or NULL
374    */
375   static const SMDS_MeshNode* VertexNode(const TopoDS_Vertex& V, const SMESHDS_Mesh* meshDS);
376
377   enum EMeshError { MEr_OK = 0, MEr_HOLES, MEr_BAD_ORI, MEr_EMPTY };
378
379   /*!
380    * \brief Finds topological errors of a sub-mesh 
381    */
382   static EMeshError GetMeshError(SMESH_subMesh* subMesh);
383
384  protected:
385
386   /*!
387    * \brief store error and comment and then return ( error == COMPERR_OK )
388    */
389   bool error(int error, const SMESH_Comment& comment = "");
390   /*!
391    * \brief store COMPERR_ALGO_FAILED error and comment and then return false
392    */
393   bool error(const SMESH_Comment& comment = "")
394   { return error(COMPERR_ALGO_FAILED, comment); }
395   /*!
396    * \brief store error and return error->IsOK()
397    */
398   bool error(SMESH_ComputeErrorPtr error);
399   /*!
400    * \brief store a bad input element preventing computation,
401    *        which may be a temporary one i.e. not residing the mesh,
402    *        then it will be deleted by InitComputeError()
403    */
404   void addBadInputElement(const SMDS_MeshElement* elem);
405
406   void addBadInputElements(const SMESHDS_SubMesh* sm,
407                            const bool             addNodes=false);
408
409 protected:
410
411   std::vector<std::string>              _compatibleHypothesis;
412   std::list<const SMESHDS_Hypothesis *> _appliedHypList;
413   std::list<const SMESHDS_Hypothesis *> _usedHypList;
414
415   // Algo features influencing which Compute() and how is called:
416   // in what turn and with what input shape.
417   // These fields must be redefined if necessary by each descendant at constructor.
418   bool _onlyUnaryInput;         // mesh one shape of GetDim() at once. Default TRUE
419   bool _requireDiscreteBoundary;// GetDim()-1 mesh must be present. Default TRUE
420   bool _requireShape;           // work with GetDim()-1 mesh bound to geom only. Default TRUE
421   bool _supportSubmeshes;       // if !_requireDiscreteBoundary. Default FALSE
422   bool _neededLowerHyps[4];     // hyp dims needed by algo that !NeedDiscreteBoundary(). Df. FALSE
423
424   // indicates if quadratic mesh creation is required,
425   // is usually set like this: _quadraticMesh = SMESH_MesherHelper::IsQuadraticSubMesh(shape)
426   bool _quadraticMesh;
427
428   int         _error;    //!< SMESH_ComputeErrorName or anything algo specific
429   std::string _comment;  //!< any text explaining what is wrong in Compute()
430   std::list<const SMDS_MeshElement*> _badInputElements; //!< to explain COMPERR_BAD_INPUT_MESH
431
432   volatile bool _computeCanceled; //!< is set to True while computing to stop it
433
434   double        _progress;        /* progress of Compute() [0.,1.],
435                                      to be set by an algo really tracking the progress */
436   int           _progressTic;     // counter of calls from SMESH_Mesh::GetComputeProgress()
437   std::vector<SMESH_subMesh*> _smToCompute; // sub-meshes to Compute()
438 };
439
440
441 class SMESH_EXPORT SMESH_0D_Algo: public SMESH_Algo
442 {
443 public:
444   SMESH_0D_Algo(int hypId, int studyId,  SMESH_Gen* gen);
445 };
446
447 class SMESH_EXPORT SMESH_1D_Algo: public SMESH_Algo
448 {
449 public:
450   SMESH_1D_Algo(int hypId, int studyId,  SMESH_Gen* gen);
451 };
452
453 class SMESH_EXPORT SMESH_2D_Algo: public SMESH_Algo
454 {
455 public:
456   SMESH_2D_Algo(int hypId, int studyId, SMESH_Gen* gen);
457   /*!
458    * \brief Method in which an algorithm generating a structured mesh
459    *        fixes positions of in-face nodes after there movement
460    *        due to insertion of viscous layers.
461    */
462   virtual bool FixInternalNodes(const SMESH_ProxyMesh& mesh,
463                                 const TopoDS_Face&     face);
464 };
465
466 class SMESH_EXPORT SMESH_3D_Algo: public SMESH_Algo
467 {
468 public:
469   SMESH_3D_Algo(int hypId, int studyId, SMESH_Gen* gen);
470 };
471
472 #endif