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