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