Salome HOME
0020725: EDF 1242 SMESH : Crash avec Convert lin--> quad avec BLSURF/GHS3D on 64bits
[modules/smesh.git] / src / SMESH / SMESH_MesherHelper.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 // File:      SMESH_MesherHelper.hxx
23 // Created:   15.02.06 14:48:09
24 // Author:    Sergey KUUL
25 //
26 #ifndef SMESH_MesherHelper_HeaderFile
27 #define SMESH_MesherHelper_HeaderFile
28
29 #include "SMESH_SMESH.hxx"
30
31 #include "SMESH_MeshEditor.hxx" // needed for many meshers
32 #include <SMDS_MeshNode.hxx>
33 #include <SMDS_QuadraticEdge.hxx>
34
35 #include <Geom_Surface.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <gp_Pnt2d.hxx>
39
40 #include <map>
41
42 typedef std::map<SMESH_TLink, const SMDS_MeshNode*>           TLinkNodeMap;
43 typedef std::map<SMESH_TLink, const SMDS_MeshNode*>::iterator ItTLinkNode;
44
45 /*!
46  * \brief It helps meshers to add elements
47  *
48  * It allow meshers not to care about creation of medium nodes
49  * when filling a quadratic mesh. Helper does it itself.
50  * It defines degree of elements to create when IsQuadraticSubMesh()
51  * is called.
52  */
53
54 typedef std::vector<const SMDS_MeshNode* > TNodeColumn;
55 typedef std::map< double, TNodeColumn > TParam2ColumnMap;
56
57 class SMESH_EXPORT SMESH_MesherHelper
58 {
59 public:
60   // ---------- PUBLIC UTILITIES ----------
61   
62   /*!
63    * \brief Returns true if given node is medium
64     * \param n - node to check
65     * \param typeToCheck - type of elements containing the node to ask about node status
66     * \retval bool - check result
67    */
68   static bool IsMedium(const SMDS_MeshNode*      node,
69                        const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
70
71   /*!
72    * \brief Load nodes bound to face into a map of node columns
73     * \param theParam2ColumnMap - map of node columns to fill
74     * \param theFace - the face on which nodes are searched for
75     * \param theBaseEdge - the edge nodes of which are columns' bases
76     * \param theMesh - the mesh containing nodes
77     * \retval bool - false if something is wrong
78    * 
79    * The key of the map is a normalized parameter of each
80    * base node on theBaseEdge.
81    * This method works in supposition that nodes on the face
82    * forms a rectangular grid and elements can be quardrangles or triangles
83    */
84   static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
85                               const TopoDS_Face& theFace,
86                               const TopoDS_Edge& theBaseEdge,
87                               SMESHDS_Mesh*      theMesh);
88   /*!
89    * \brief Return support shape of a node
90    * \param node - the node
91    * \param meshDS - mesh DS
92    * \retval TopoDS_Shape - found support shape
93    */
94   static TopoDS_Shape GetSubShapeByNode(const SMDS_MeshNode* node,
95                                         SMESHDS_Mesh*        meshDS);
96
97   /*!
98    * \brief Return a valid node index, fixing the given one if necessary
99     * \param ind - node index
100     * \param nbNodes - total nb of nodes
101     * \retval int - valid node index
102    */
103   static int WrapIndex(const int ind, const int nbNodes) {
104     if ( ind < 0 ) return nbNodes + ind % nbNodes;
105     if ( ind >= nbNodes ) return ind % nbNodes;
106     return ind;
107   }
108
109   /*!
110    * \brief Return number of unique ancestors of the shape
111    */
112   static int NbAncestors(const TopoDS_Shape& shape,
113                          const SMESH_Mesh&   mesh,
114                          TopAbs_ShapeEnum    ancestorType=TopAbs_SHAPE);
115
116 public:
117   // ---------- PUBLIC INSTANCE METHODS ----------
118
119   // constructor
120   SMESH_MesherHelper(SMESH_Mesh& theMesh);
121
122   SMESH_Mesh* GetMesh() const { return myMesh; }
123     
124   SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
125     
126   /*!
127    * Check submesh for given shape: if all elements on this shape are quadratic,
128    * quadratic elements will be created. Also fill myTLinkNodeMap
129    */
130   bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
131   /*!
132    * \brief Set order of elements to create without calling IsQuadraticSubMesh()
133    */
134   void SetIsQuadratic(const bool theBuildQuadratic)
135   { myCreateQuadratic = theBuildQuadratic; }
136   /*!
137    * \brief Return myCreateQuadratic flag
138    */
139   bool GetIsQuadratic() const { return myCreateQuadratic; }
140
141   /*!
142    * \brief Move medium nodes of faces and volumes to fix distorted elements
143    * \param volumeOnly - fix nodes on geom faces or not if the shape is solid
144    */
145   void FixQuadraticElements(bool volumeOnly=true);
146
147   /*!
148    * \brief To set created elements on the shape set by IsQuadraticSubMesh()
149    *        or the next methods. By defaul elements are set on the shape if
150    *        a mesh has no shape to be meshed
151    */
152   void SetElementsOnShape(bool toSet) { mySetElemOnShape = toSet; }
153
154   /*!
155    * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
156    */
157   void SetSubShape(const int           subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
158   void SetSubShape(const TopoDS_Shape& subShape);
159   /*!
160    * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape() 
161     * \retval int - shape index in SMESHDS
162    */
163   int GetSubShapeID() const { return myShapeID; }
164   /*!
165    * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape() 
166    */
167   TopoDS_Shape GetSubShape() const  { return myShape; }
168
169   /*!
170    * Creates a node
171    */
172   SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0);
173   /*!
174    * Creates quadratic or linear edge
175    */
176   SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
177                          const SMDS_MeshNode* n2,
178                          const int id = 0, 
179                          const bool force3d = true);
180   /*!
181    * Creates quadratic or linear triangle
182    */
183   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
184                          const SMDS_MeshNode* n2,
185                          const SMDS_MeshNode* n3,
186                          const int id=0, 
187                          const bool force3d = false);
188   /*!
189    * Creates quadratic or linear quadrangle
190    */
191   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
192                          const SMDS_MeshNode* n2,
193                          const SMDS_MeshNode* n3,
194                          const SMDS_MeshNode* n4,
195                          const int id = 0,
196                          const bool force3d = false);
197   /*!
198    * Creates quadratic or linear tetraahedron
199    */
200   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
201                              const SMDS_MeshNode* n2,
202                              const SMDS_MeshNode* n3,
203                              const SMDS_MeshNode* n4,
204                              const int id = 0,
205                              const bool force3d = true);
206   /*!
207    * Creates quadratic or linear pyramid
208    */
209   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
210                              const SMDS_MeshNode* n2,
211                              const SMDS_MeshNode* n3,
212                              const SMDS_MeshNode* n4,
213                              const SMDS_MeshNode* n5,
214                              const int id = 0,
215                              const bool force3d = true);
216   /*!
217    * Creates quadratic or linear pentahedron
218    */
219   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
220                              const SMDS_MeshNode* n2,
221                              const SMDS_MeshNode* n3,
222                              const SMDS_MeshNode* n4,
223                              const SMDS_MeshNode* n5,
224                              const SMDS_MeshNode* n6,
225                              const int id = 0, 
226                              const bool force3d = true);
227   /*!
228    * Creates quadratic or linear hexahedron
229    */
230   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
231                              const SMDS_MeshNode* n2,
232                              const SMDS_MeshNode* n3,
233                              const SMDS_MeshNode* n4,
234                              const SMDS_MeshNode* n5,
235                              const SMDS_MeshNode* n6,
236                              const SMDS_MeshNode* n7,
237                              const SMDS_MeshNode* n8,
238                              const int id = 0, 
239                              bool force3d = true);
240   /*!
241    * \brief Return U of the given node on the edge
242    */
243   double GetNodeU(const TopoDS_Edge&   theEdge,
244                   const SMDS_MeshNode* theNode,
245                   bool*                check=0);
246   /*!
247    * \brief Return node UV on face
248    *  \param inFaceNode - a node of element being created located inside a face
249    */
250   gp_XY GetNodeUV(const TopoDS_Face&   F,
251                   const SMDS_MeshNode* n,
252                   const SMDS_MeshNode* inFaceNode=0,
253                   bool*                check=0) const;
254   /*!
255    * \brief Check and fix node UV on a face
256    *  \param force - check even if checks of other nodes on this face passed OK
257    *  \retval bool - false if UV is bad and could not be fixed
258    */
259   bool CheckNodeUV(const TopoDS_Face&   F,
260                    const SMDS_MeshNode* n,
261                    gp_XY&               uv,
262                    const double         tol,
263                    const bool           force=false) const;
264   /*!
265    * \brief Check and fix node U on an edge
266    *  \param force - check even if checks of other nodes on this edge passed OK
267    *  \retval bool - false if U is bad and could not be fixed
268    */
269   bool CheckNodeU(const TopoDS_Edge&   E,
270                   const SMDS_MeshNode* n,
271                   double&              u,
272                   const double         tol,
273                   const bool           force=false) const;
274   /*!
275    * \brief Return middle UV taking in account surface period
276    */
277   static gp_XY GetMiddleUV(const Handle(Geom_Surface)& surface,
278                            const gp_XY&                uv1,
279                            const gp_XY&                uv2);
280   /*!
281    * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
282     * \retval bool - return true if the face is periodic
283     *
284     * If F is Null, answer about subshape set through IsQuadraticSubMesh() or
285     * SetSubShape()
286    */
287   bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
288
289   /*!
290    * \brief Check if shape is a degenerated edge or it's vertex
291     * \param subShape - edge or vertex index in SMESHDS
292     * \retval bool - true if subShape is a degenerated shape
293     *
294     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
295    */
296   bool IsDegenShape(const int subShape) const
297   { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
298   /*!
299    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
300    *        has a degenerated edges
301     * \retval bool - true if it has
302    */
303   bool HasDegeneratedEdges() const { return !myDegenShapeIds.empty(); }
304
305   /*!
306    * \brief Check if shape is a seam edge or it's vertex
307     * \param subShape - edge or vertex index in SMESHDS
308     * \retval bool - true if subShape is a seam shape
309     *
310     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
311     * Seam shape has two 2D alternative represenations on the face
312    */
313   bool IsSeamShape(const int subShape) const
314   { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
315   /*!
316    * \brief Check if shape is a seam edge or it's vertex
317     * \param subShape - edge or vertex
318     * \retval bool - true if subShape is a seam shape
319     *
320     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
321     * Seam shape has two 2D alternative represenations on the face
322    */
323   bool IsSeamShape(const TopoDS_Shape& subShape) const
324   { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
325   /*!
326    * \brief Return true if an edge or a vertex encounters twice in face wire
327    *  \param subShape - Id of edge or vertex
328    */
329   bool IsRealSeam(const int subShape) const
330   { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
331   /*!
332    * \brief Return true if an edge or a vertex encounters twice in face wire
333    *  \param subShape - edge or vertex
334    */
335   bool IsRealSeam(const TopoDS_Shape& subShape) const
336   { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
337   /*!
338    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
339    *        has a seam edge
340     * \retval bool - true if it has
341    */
342   bool HasSeam() const { return !mySeamShapeIds.empty(); }
343   /*!
344    * \brief Return index of periodic parametric direction of a closed face
345     * \retval int - 1 for U, 2 for V direction
346    */
347   int GetPeriodicIndex() const { return myParIndex; }
348   /*!
349    * \brief Return an alternative parameter for a node on seam
350    */
351   double GetOtherParam(const double param) const;
352
353   /*!
354    * \brief Return existing or create new medium nodes between given ones
355    *  \param force3d - true means node creation at the middle between the
356    *                   two given nodes, else node position is found on its
357    *                   supporting geometrical shape, if any.
358    */
359   const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
360                                      const SMDS_MeshNode* n2,
361                                      const bool force3d);
362   /*!
363    * \brief Add a link in my data structure
364    */
365   void AddTLinkNode(const SMDS_MeshNode* n1,
366                     const SMDS_MeshNode* n2,
367                     const SMDS_MeshNode* n12);
368   /*!
369    * \brief Add many links in my data structure
370    */
371   void AddTLinkNodeMap(const TLinkNodeMap& aMap)
372     { myTLinkNodeMap.insert(aMap.begin(), aMap.end()); }
373
374   /**
375    * Returns myTLinkNodeMap
376    */
377   const TLinkNodeMap& GetTLinkNodeMap() const { return myTLinkNodeMap; }
378
379   /**
380    * Check mesh without geometry for: if all elements on this shape are quadratic,
381    * quadratic elements will be created.
382    * Used then generated 3D mesh without geometry.
383    */
384   enum MType{ LINEAR, QUADRATIC, COMP };
385   MType IsQuadraticMesh();
386   
387 protected:
388
389   /*!
390    * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
391     * \param uv1 - UV on the seam
392     * \param uv2 - UV within a face
393     * \retval gp_Pnt2d - selected UV
394    */
395   gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
396
397  private:
398
399   // Forbiden copy constructor
400   SMESH_MesherHelper (const SMESH_MesherHelper& theOther) {};
401
402   // special map for using during creation of quadratic elements
403   TLinkNodeMap    myTLinkNodeMap;
404
405   std::set< int > myDegenShapeIds;
406   std::set< int > mySeamShapeIds;
407   double          myPar1[2], myPar2[2]; // U and V bounds of a closed periodic surface
408   int             myParIndex;     // bounds' index (1-U, 2-V, 3-both)
409
410   TopoDS_Shape    myShape;
411   SMESH_Mesh*     myMesh;
412   int             myShapeID;
413
414   // to create quadratic elements
415   bool            myCreateQuadratic;
416   bool            mySetElemOnShape;
417   std::set< int > myOkNodePosShapes;
418
419 };
420
421
422 #endif