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