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