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