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