Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/smesh.git] / src / SMESH / SMESH_Algo.hxx
1 //  SMESH SMESH : implementaion of SMESH idl descriptions
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : SMESH_Algo.hxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //  $Header$
28
29 #ifndef _SMESH_ALGO_HXX_
30 #define _SMESH_ALGO_HXX_
31
32 #include "SMESH_SMESH.hxx"
33
34 #include "SMESH_Hypothesis.hxx"
35 #include "SMESH_ComputeError.hxx"
36 #include "SMESH_Comment.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
46 class SMESH_Gen;
47 class SMESH_Mesh;
48 class SMESH_HypoFilter;
49 class TopoDS_Vertex;
50 class TopoDS_Face;
51 class TopoDS_Shape;
52 class SMESHDS_Mesh;
53 class SMDS_MeshNode;
54 class SMESH_subMesh;
55 class SMESH_MesherHelper;
56
57
58 class SMESH_EXPORT SMESH_Algo:public SMESH_Hypothesis
59 {
60 public:
61   /*!
62    * \brief Creates algorithm
63     * \param hypId - algorithm ID
64     * \param studyId - study ID
65     * \param gen - SMESH_Gen
66    */
67   SMESH_Algo(int hypId, int studyId, SMESH_Gen * gen);
68
69   /*!
70    * \brief Destructor
71    */
72   virtual ~ SMESH_Algo();
73
74   /*!
75    * \brief Saves nothing in a stream
76     * \param save - the stream
77     * \retval virtual std::ostream & - the stream
78    */
79   virtual std::ostream & SaveTo(std::ostream & save);
80
81   /*!
82    * \brief Loads nothing from a stream
83     * \param load - the stream
84     * \retval virtual std::ostream & - the stream
85    */
86   virtual std::istream & LoadFrom(std::istream & load);
87
88   /*!
89    * \brief Returns all types of compatible hypotheses
90    */
91   const std::vector < std::string > & GetCompatibleHypothesis();
92
93   /*!
94    * \brief Check hypothesis definition to mesh a shape
95     * \param aMesh - the mesh
96     * \param aShape - the shape
97     * \param aStatus - check result
98     * \retval bool - true if hypothesis is well defined
99    */
100   virtual bool CheckHypothesis(SMESH_Mesh&                          aMesh,
101                                const TopoDS_Shape&                  aShape,
102                                SMESH_Hypothesis::Hypothesis_Status& aStatus) = 0;
103   /*!
104    * \brief Computes mesh on a shape
105     * \param aMesh - the mesh
106     * \param aShape - the shape
107     * \retval bool - is a success
108     *
109     * Algorithms that !NeedDescretBoundary() || !OnlyUnaryInput() are
110     * to set SMESH_ComputeError returned by SMESH_submesh::GetComputeError()
111     * to report problematic subshapes
112    */
113   virtual bool Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape) = 0;
114
115   /*!
116    * \brief Computes mesh without geometry
117     * \param aMesh - the mesh
118     * \param aHelper - helper that must be used for adding elements to \aaMesh
119     * \retval bool - is a success
120     *
121     * The method is called if ( !aMesh->HasShapeToMesh() )
122    */
123   virtual bool Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper);
124
125   /*!
126    * \brief Returns a list of compatible hypotheses used to mesh a shape
127     * \param aMesh - the mesh 
128     * \param aShape - the shape
129     * \param ignoreAuxiliary - do not include auxiliary hypotheses in the list
130     * \retval const std::list <const SMESHDS_Hypothesis*> - hypotheses list
131    * 
132    *  List the hypothesis used by the algorithm associated to the shape.
133    *  Hypothesis associated to father shape -are- taken into account (see
134    *  GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
135    *  the algorithm. This method could be surcharged by specific algorithms, in 
136    *  case of several hypothesis simultaneously applicable.
137    */
138   virtual const std::list <const SMESHDS_Hypothesis *> &
139   GetUsedHypothesis(SMESH_Mesh &         aMesh,
140                     const TopoDS_Shape & aShape,
141                     const bool           ignoreAuxiliary=true);
142   /*!
143    * \brief Returns a list of compatible hypotheses assigned to a shape in a mesh
144     * \param aMesh - the mesh 
145     * \param aShape - the shape
146     * \param ignoreAuxiliary - do not include auxiliary hypotheses in the list
147     * \retval const std::list <const SMESHDS_Hypothesis*> - hypotheses list
148    * 
149    *  List the relevant hypothesis associated to the shape. Relevant hypothesis
150    *  have a name (type) listed in the algorithm. Hypothesis associated to
151    *  father shape -are not- taken into account (see GetUsedHypothesis)
152    */
153   const list <const SMESHDS_Hypothesis *> &
154   GetAppliedHypothesis(SMESH_Mesh &         aMesh,
155                        const TopoDS_Shape & aShape,
156                        const bool           ignoreAuxiliary=true);
157   /*!
158    * \brief Make the filter recognize only compatible hypotheses
159    * \param theFilter - the filter to initialize
160    * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
161    * \retval bool - true if the algo has compatible hypotheses
162    */
163   bool InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
164                                  const bool         ignoreAuxiliary) const;
165   /*!
166    * \brief Initialize my parameter values by the mesh built on the geometry
167    *
168    * Just return false as the algorithm does not hold parameters values
169    */
170   virtual bool SetParametersByMesh(const SMESH_Mesh* theMesh,
171                                    const TopoDS_Shape& theShape);
172   /*!
173    * \brief return compute error
174    */
175   SMESH_ComputeErrorPtr GetComputeError() const;
176   /*!
177    * \brief initialize compute error
178    */
179   void InitComputeError();
180
181 public:
182   // ==================================================================
183   // Algo features influencing how Compute() is called:
184   // in what turn and with what input shape
185   // ==================================================================
186
187   // SMESH_Hypothesis::GetDim();
188   // 1 - dimention of target mesh
189
190   bool OnlyUnaryInput() const { return _onlyUnaryInput; }
191   // 2 - is collection of tesselatable shapes inacceptable as input;
192   // "collection" means a shape containing shapes of dim equal
193   // to GetDim().
194   // Algo which can process a collection shape should expect
195   // an input temporary shape that is neither MainShape nor
196   // its child.
197
198   bool NeedDescretBoundary() const { return _requireDescretBoundary; }
199   // 3 - is a Dim-1 mesh prerequisite
200
201   bool NeedShape() const { return _requireShape; }
202   // 4 - is shape existance required
203
204 public:
205   // ==================================================================
206   // Methods to track non hierarchical dependencies between submeshes 
207   // ==================================================================
208
209   /*!
210    * \brief Sets event listener to submeshes if necessary
211     * \param subMesh - submesh where algo is set
212    *
213    * This method is called when a submesh gets HYP_OK algo_state.
214    * After being set, event listener is notified on each event of a submesh.
215    * By default non listener is set
216    */
217   virtual void SetEventListener(SMESH_subMesh* subMesh);
218   
219   /*!
220    * \brief Allow algo to do something after persistent restoration
221     * \param subMesh - restored submesh
222    *
223    * This method is called only if a submesh has HYP_OK algo_state.
224    */
225   virtual void SubmeshRestored(SMESH_subMesh* subMesh);
226   
227 public:
228   // ==================================================================
229   // Common algo utilities
230   // ==================================================================
231   /*!
232    * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
233    * \param theMesh - The mesh containing nodes
234    * \param theEdge - The geometrical edge of interest
235    * \param theParams - The resulting vector of sorted node parameters
236    * \retval bool - false if not all parameters are OK
237    */
238   static bool GetNodeParamOnEdge(const SMESHDS_Mesh*     theMesh,
239                                  const TopoDS_Edge&      theEdge,
240                                  std::vector< double > & theParams);
241   /*!
242    * \brief Find out elements orientation on a geometrical face
243    * \param theFace - The face correctly oriented in the shape being meshed
244    * \param theMeshDS - The mesh data structure
245    * \retval bool - true if the face normal and the normal of first element
246    *                in the correspoding submesh point in different directions
247    */
248   static bool IsReversedSubMesh (const TopoDS_Face&  theFace,
249                                  SMESHDS_Mesh*       theMeshDS);
250   /*!
251    * \brief Compute length of an edge
252     * \param E - the edge
253     * \retval double - the length
254    */
255   static double EdgeLength(const TopoDS_Edge & E);
256
257   /*!
258    * \brief Return continuity of two edges
259     * \param E1 - the 1st edge
260     * \param E2 - the 2nd edge
261     * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
262    */
263   static GeomAbs_Shape Continuity(const TopoDS_Edge & E1, const TopoDS_Edge & E2);
264
265   /*!
266    * \brief Return true if an edge can be considered as a continuation of another
267    */
268   static bool IsContinuous(const TopoDS_Edge & E1, const TopoDS_Edge & E2) {
269     return ( Continuity( E1, E2 ) >= GeomAbs_G1 );
270   }
271
272   /*!
273    * \brief Return the node built on a vertex
274     * \param V - the vertex
275     * \param meshDS - mesh
276     * \retval const SMDS_MeshNode* - found node or NULL
277    */
278   static const SMDS_MeshNode* VertexNode(const TopoDS_Vertex& V,
279                                          SMESHDS_Mesh*        meshDS);
280
281 protected:
282
283   /*!
284    * \brief store error and comment and then return ( error == COMPERR_OK )
285    */
286   bool error(int error, const SMESH_Comment& comment = "");
287   /*!
288    * \brief store COMPERR_ALGO_FAILED error and comment and then return false
289    */
290   bool error(const SMESH_Comment& comment = "")
291   { return error(COMPERR_ALGO_FAILED, comment); }
292   /*!
293    * \brief store error and return error->IsOK()
294    */
295   bool error(SMESH_ComputeErrorPtr error);
296
297 protected:
298
299   std::vector<std::string>              _compatibleHypothesis;
300   std::list<const SMESHDS_Hypothesis *> _appliedHypList;
301   std::list<const SMESHDS_Hypothesis *> _usedHypList;
302
303   bool _onlyUnaryInput;
304   bool _requireDescretBoundary;
305   bool _requireShape;
306
307   // quadratic mesh creation required,
308   // is usually set trough SMESH_MesherHelper::IsQuadraticSubMesh()
309   bool _quadraticMesh;
310
311   int         _error;    //!< SMESH_ComputeErrorName or anything algo specific
312   std::string _comment;  //!< any text explaining what is wrong in Compute()
313 };
314
315 #endif