Salome HOME
0021370: EDF SMESH: Hexahedron + Composite Side Disretization generate a bad mesh
[modules/smesh.git] / src / SMESH / SMESH_MesherHelper.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 // File:      SMESH_MesherHelper.hxx
24 // Created:   15.02.06 14:48:09
25 // Author:    Sergey KUUL
26 //
27 #ifndef SMESH_MesherHelper_HeaderFile
28 #define SMESH_MesherHelper_HeaderFile
29
30 #include "SMESH_SMESH.hxx"
31
32 #include "SMESH_MeshEditor.hxx" // needed for many meshers
33 #include <SMDS_MeshNode.hxx>
34 #include <SMDS_QuadraticEdge.hxx>
35
36 #include <Geom_Surface.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <gp_Pnt2d.hxx>
40
41 #include <map>
42 #include <vector>
43
44 class GeomAPI_ProjectPointOnSurf;
45 class GeomAPI_ProjectPointOnCurve;
46 class SMESH_ProxyMesh;
47
48 typedef std::map<SMESH_TLink, const SMDS_MeshNode*>           TLinkNodeMap;
49 typedef std::map<SMESH_TLink, const SMDS_MeshNode*>::iterator ItTLinkNode;
50
51 typedef SMDS_Iterator<const TopoDS_Shape*>  PShapeIterator;
52 typedef boost::shared_ptr< PShapeIterator > PShapeIteratorPtr;
53   
54 typedef std::vector<const SMDS_MeshNode* > TNodeColumn;
55 typedef std::map< double, TNodeColumn >    TParam2ColumnMap;
56
57 typedef gp_XY (*xyFunPtr)(const gp_XY& uv1, const gp_XY& uv2);
58
59 //=======================================================================
60 /*!
61  * \brief It helps meshers to add elements
62  *
63  * It allow meshers not to care about creation of medium nodes
64  * when filling a quadratic mesh. Helper does it itself.
65  * It defines degree of elements to create when IsQuadraticSubMesh()
66  * is called.
67  */
68 //=======================================================================
69
70 class SMESH_EXPORT SMESH_MesherHelper
71 {
72 public:
73   // ---------- PUBLIC UTILITIES ----------
74   
75   /*!
76    * \brief Returns true if given node is medium
77     * \param n - node to check
78     * \param typeToCheck - type of elements containing the node to ask about node status
79     * \retval bool - check result
80    */
81   static bool IsMedium(const SMDS_MeshNode*      node,
82                        const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
83
84   /*!
85    * \brief Load nodes bound to face into a map of node columns
86     * \param theParam2ColumnMap - map of node columns to fill
87     * \param theFace - the face on which nodes are searched for
88     * \param theBaseSide - the edges holding nodes on which columns' bases
89     * \param theMesh - the mesh containing nodes
90     * \retval bool - false if something is wrong
91    * 
92    * The key of the map is a normalized parameter of each
93    * base node on theBaseSide. Edges in theBaseSide must be sequenced.
94    * This method works in supposition that nodes on the face
95    * forms a rectangular grid and elements can be quardrangles or triangles
96    */
97   static bool LoadNodeColumns(TParam2ColumnMap &            theParam2ColumnMap,
98                               const TopoDS_Face&            theFace,
99                               const std::list<TopoDS_Edge>& theBaseSide,
100                               SMESHDS_Mesh*                 theMesh,
101                               SMESH_ProxyMesh*              theProxyMesh=0);
102   /*!
103    * \brief Variant of LoadNodeColumns() above with theBaseSide given by one edge
104    */
105   static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
106                               const TopoDS_Face& theFace,
107                               const TopoDS_Edge& theBaseEdge,
108                               SMESHDS_Mesh*      theMesh,
109                               SMESH_ProxyMesh*   theProxyMesh=0);
110   /*!
111    * \brief Return support shape of a node
112    * \param node - the node
113    * \param meshDS - mesh DS
114    * \retval TopoDS_Shape - found support shape
115    */
116   static TopoDS_Shape GetSubShapeByNode(const SMDS_MeshNode* node,
117                                         const SMESHDS_Mesh*  meshDS);
118
119   /*!
120    * \brief Return a valid node index, fixing the given one if necessary
121     * \param ind - node index
122     * \param nbNodes - total nb of nodes
123     * \retval int - valid node index
124    */
125   static int WrapIndex(const int ind, const int nbNodes) {
126     if ( ind < 0 ) return nbNodes + ind % nbNodes;
127     if ( ind >= nbNodes ) return ind % nbNodes;
128     return ind;
129   }
130
131   /*!
132    * \brief Return number of unique ancestors of the shape
133    */
134   static int NbAncestors(const TopoDS_Shape& shape,
135                          const SMESH_Mesh&   mesh,
136                          TopAbs_ShapeEnum    ancestorType=TopAbs_SHAPE);
137   /*!
138    * \brief Return iterator on ancestors of the given type
139    */
140   static PShapeIteratorPtr GetAncestors(const TopoDS_Shape& shape,
141                                         const SMESH_Mesh&   mesh,
142                                         TopAbs_ShapeEnum    ancestorType);
143
144   /*!
145    * \brief Return orientation of sub-shape in the main shape
146    */
147   static TopAbs_Orientation GetSubShapeOri(const TopoDS_Shape& shape,
148                                            const TopoDS_Shape& subShape);
149
150   static bool IsSubShape( const TopoDS_Shape& shape, const TopoDS_Shape& mainShape );
151
152   static bool IsSubShape( const TopoDS_Shape& shape, SMESH_Mesh* aMesh );
153
154   static double MaxTolerance( const TopoDS_Shape& shape );
155
156   static bool IsClosedEdge( const TopoDS_Edge& anEdge );
157
158   static TopoDS_Vertex IthVertex( const bool is2nd, TopoDS_Edge anEdge, const bool CumOri=true );
159
160
161 public:
162   // ---------- PUBLIC INSTANCE METHODS ----------
163
164   // constructor
165   SMESH_MesherHelper(SMESH_Mesh& theMesh);
166
167   SMESH_Mesh* GetMesh() const { return myMesh; }
168     
169   SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
170     
171   /*!
172    * Check submesh for given shape: if all elements on this shape are quadratic,
173    * quadratic elements will be created. Also fill myTLinkNodeMap
174    */
175   bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
176   /*!
177    * \brief Set order of elements to create without calling IsQuadraticSubMesh()
178    */
179   void SetIsQuadratic(const bool theBuildQuadratic)
180   { myCreateQuadratic = theBuildQuadratic; }
181   /*!
182    * \brief Return myCreateQuadratic flag
183    */
184   bool GetIsQuadratic() const { return myCreateQuadratic; }
185
186   /*!
187    * \brief Move medium nodes of faces and volumes to fix distorted elements
188    * \param volumeOnly - fix nodes on geom faces or not if the shape is solid
189    */
190   void FixQuadraticElements(bool volumeOnly=true);
191
192   /*!
193    * \brief To set created elements on the shape set by IsQuadraticSubMesh()
194    *        or the next methods. By defaul elements are set on the shape if
195    *        a mesh has no shape to be meshed
196    */
197   void SetElementsOnShape(bool toSet) { mySetElemOnShape = toSet; }
198
199   /*!
200    * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
201    */
202   void SetSubShape(const int           subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
203   void SetSubShape(const TopoDS_Shape& subShape);
204   /*!
205    * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape() 
206     * \retval int - shape index in SMESHDS
207    */
208   int GetSubShapeID() const { return myShapeID; }
209   /*!
210    * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape() 
211    */
212   const TopoDS_Shape& GetSubShape() const  { return myShape; }
213
214   /*!
215    * Creates a node
216    */
217   SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0);
218   /*!
219    * Creates quadratic or linear edge
220    */
221   SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
222                          const SMDS_MeshNode* n2,
223                          const int id = 0, 
224                          const bool force3d = true);
225   /*!
226    * Creates quadratic or linear triangle
227    */
228   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
229                          const SMDS_MeshNode* n2,
230                          const SMDS_MeshNode* n3,
231                          const int id=0, 
232                          const bool force3d = false);
233   /*!
234    * Creates quadratic or linear quadrangle
235    */
236   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
237                          const SMDS_MeshNode* n2,
238                          const SMDS_MeshNode* n3,
239                          const SMDS_MeshNode* n4,
240                          const int id = 0,
241                          const bool force3d = false);
242
243   /*!
244    * Creates polygon, with additional nodes in quadratic mesh
245    */
246   SMDS_MeshFace* AddPolygonalFace (const std::vector<const SMDS_MeshNode*>& nodes,
247                                    const int id = 0,
248                                    const bool force3d = false);
249   /*!
250    * Creates quadratic or linear tetrahedron
251    */
252   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
253                              const SMDS_MeshNode* n2,
254                              const SMDS_MeshNode* n3,
255                              const SMDS_MeshNode* n4,
256                              const int id = 0,
257                              const bool force3d = true);
258   /*!
259    * Creates quadratic or linear pyramid
260    */
261   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
262                              const SMDS_MeshNode* n2,
263                              const SMDS_MeshNode* n3,
264                              const SMDS_MeshNode* n4,
265                              const SMDS_MeshNode* n5,
266                              const int id = 0,
267                              const bool force3d = true);
268   /*!
269    * Creates quadratic or linear pentahedron
270    */
271   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
272                              const SMDS_MeshNode* n2,
273                              const SMDS_MeshNode* n3,
274                              const SMDS_MeshNode* n4,
275                              const SMDS_MeshNode* n5,
276                              const SMDS_MeshNode* n6,
277                              const int id = 0, 
278                              const bool force3d = true);
279   /*!
280    * Creates quadratic or linear hexahedron
281    */
282   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
283                              const SMDS_MeshNode* n2,
284                              const SMDS_MeshNode* n3,
285                              const SMDS_MeshNode* n4,
286                              const SMDS_MeshNode* n5,
287                              const SMDS_MeshNode* n6,
288                              const SMDS_MeshNode* n7,
289                              const SMDS_MeshNode* n8,
290                              const int id = 0, 
291                              bool force3d = true);
292
293   /*!
294    * Creates polyhedron. In quadratic mesh, adds medium nodes
295    */
296   SMDS_MeshVolume* AddPolyhedralVolume (const std::vector<const SMDS_MeshNode*>& nodes,
297                                         const std::vector<int>&                  quantities,
298                                         const int                                ID=0,
299                                         const bool                               force3d = true);
300   /*!
301    * \brief Return U of the given node on the edge
302    */
303   double GetNodeU(const TopoDS_Edge&   theEdge,
304                   const SMDS_MeshNode* theNode,
305                   const SMDS_MeshNode* inEdgeNode=0,
306                   bool*                check=0);
307   /*!
308    * \brief Return node UV on face
309    *  \param inFaceNode - a node of element being created located inside a face
310    */
311   gp_XY GetNodeUV(const TopoDS_Face&   F,
312                   const SMDS_MeshNode* n,
313                   const SMDS_MeshNode* inFaceNode=0,
314                   bool*                check=0) const;
315   /*!
316    * \brief Check and fix node UV on a face
317    *  \param force - check even if checks of other nodes on this face passed OK
318    *  \param distXYZ - returns result distance and point coordinates
319    *  \retval bool - false if UV is bad and could not be fixed
320    */
321   bool CheckNodeUV(const TopoDS_Face&   F,
322                    const SMDS_MeshNode* n,
323                    gp_XY&               uv,
324                    const double         tol,
325                    const bool           force=false,
326                    double               distXYZ[4]=0) const;
327   /*!
328    * \brief Check and fix node U on an edge
329    *  \param force - check even if checks of other nodes on this edge passed OK
330    *  \param distXYZ - returns result distance and point coordinates
331    *  \retval bool - false if U is bad and could not be fixed
332    */
333   bool CheckNodeU(const TopoDS_Edge&   E,
334                   const SMDS_MeshNode* n,
335                   double&              u,
336                   const double         tol,
337                   const bool           force=false,
338                   double               distXYZ[4]=0) const;
339   /*!
340    * \brief Return middle UV taking in account surface period
341    */
342   static gp_XY GetMiddleUV(const Handle(Geom_Surface)& surface,
343                            const gp_XY&                uv1,
344                            const gp_XY&                uv2);
345   /*!
346    * \brief Define a pointer to wrapper over a function of gp_XY class,
347    *       suitable to pass as xyFunPtr to applyIn2D().
348    *       For exaple gp_XY_FunPtr(Added) defines pointer gp_XY_Added to function
349    *       calling gp_XY::Added(gp_XY), which is to be used like following
350    *       applyIn2D(surf, uv1, uv2, gp_XY_Added)
351    */
352 #define gp_XY_FunPtr(meth) \
353   static gp_XY __gpXY_##meth (const gp_XY& uv1, const gp_XY& uv2) { return uv1.meth( uv2 ); } \
354   static xyFunPtr gp_XY_##meth = & __gpXY_##meth
355
356   /*!
357    * \brief Perform given operation on two 2d points in parameric space of given surface.
358    *        It takes into account period of the surface. Use gp_XY_FunPtr macro
359    *        to easily define pointer to function of gp_XY class.
360    */
361   static gp_XY applyIn2D(const Handle(Geom_Surface)& surface,
362                          const gp_XY&                uv1,
363                          const gp_XY&                uv2,
364                          xyFunPtr                    fun,
365                          const bool                  resultInPeriod=true);
366                           
367   /*!
368    * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
369     * \retval bool - return true if the face is periodic
370     *
371     * If F is Null, answer about subshape set through IsQuadraticSubMesh() or
372     * SetSubShape()
373    */
374   bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
375
376   /*!
377    * \brief Return projector intitialized by given face without location, which is returned
378    */
379   GeomAPI_ProjectPointOnSurf& GetProjector(const TopoDS_Face& F,
380                                            TopLoc_Location&   loc,
381                                            double             tol=0 ) const; 
382
383   /*!
384    * \brief Check if shape is a degenerated edge or it's vertex
385     * \param subShape - edge or vertex index in SMESHDS
386     * \retval bool - true if subShape is a degenerated shape
387     *
388     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
389    */
390   bool IsDegenShape(const int subShape) const
391   { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
392   /*!
393    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
394    *        has a degenerated edges
395     * \retval bool - true if it has
396    */
397   bool HasDegeneratedEdges() const { return !myDegenShapeIds.empty(); }
398
399   /*!
400    * \brief Check if shape is a seam edge or it's vertex
401     * \param subShape - edge or vertex index in SMESHDS
402     * \retval bool - true if subShape is a seam shape
403     *
404     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
405     * Seam shape has two 2D alternative represenations on the face
406    */
407   bool IsSeamShape(const int subShape) const
408   { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
409   /*!
410    * \brief Check if shape is a seam edge or it's vertex
411     * \param subShape - edge or vertex
412     * \retval bool - true if subShape is a seam shape
413     *
414     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
415     * Seam shape has two 2D alternative represenations on the face
416    */
417   bool IsSeamShape(const TopoDS_Shape& subShape) const
418   { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
419   /*!
420    * \brief Return true if an edge or a vertex encounters twice in face wire
421    *  \param subShape - Id of edge or vertex
422    */
423   bool IsRealSeam(const int subShape) const
424   { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
425   /*!
426    * \brief Return true if an edge or a vertex encounters twice in face wire
427    *  \param subShape - edge or vertex
428    */
429   bool IsRealSeam(const TopoDS_Shape& subShape) const
430   { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
431   /*!
432    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
433    *        has a seam edge
434     * \retval bool - true if it has
435    */
436   bool HasSeam() const { return !mySeamShapeIds.empty(); }
437   /*!
438    * \brief Return index of periodic parametric direction of a closed face
439     * \retval int - 1 for U, 2 for V direction
440    */
441   int GetPeriodicIndex() const { return myParIndex; }
442   /*!
443    * \brief Return an alternative parameter for a node on seam
444    */
445   double GetOtherParam(const double param) const;
446
447   /*!
448    * \brief Return existing or create new medium nodes between given ones
449    *  \param force3d - true means node creation at the middle between the
450    *                   two given nodes, else node position is found on its
451    *                   supporting geometrical shape, if any.
452    */
453   const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
454                                      const SMDS_MeshNode* n2,
455                                      const bool force3d);
456   /*!
457    * \brief Add a link in my data structure
458    */
459   void AddTLinkNode(const SMDS_MeshNode* n1,
460                     const SMDS_MeshNode* n2,
461                     const SMDS_MeshNode* n12);
462   /*!
463    * \brief Add many links in my data structure
464    */
465   void AddTLinkNodeMap(const TLinkNodeMap& aMap)
466     { myTLinkNodeMap.insert(aMap.begin(), aMap.end()); }
467
468   void AddTLinks(const SMDS_MeshEdge*   edge);
469   void AddTLinks(const SMDS_MeshFace*   face);
470   void AddTLinks(const SMDS_MeshVolume* vol);
471
472   /**
473    * Returns myTLinkNodeMap
474    */
475   const TLinkNodeMap& GetTLinkNodeMap() const { return myTLinkNodeMap; }
476
477   /**
478    * Check mesh without geometry for: if all elements on this shape are quadratic,
479    * quadratic elements will be created.
480    * Used then generated 3D mesh without geometry.
481    */
482   enum MType{ LINEAR, QUADRATIC, COMP };
483   MType IsQuadraticMesh();
484   
485   virtual ~SMESH_MesherHelper();
486
487 protected:
488
489   /*!
490    * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
491     * \param uv1 - UV on the seam
492     * \param uv2 - UV within a face
493     * \retval gp_Pnt2d - selected UV
494    */
495   gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
496
497   const SMDS_MeshNode* getMediumNodeOnComposedWire(const SMDS_MeshNode* n1,
498                                                    const SMDS_MeshNode* n2,
499                                                    bool                 force3d);
500  private:
501
502   // Forbiden copy constructor
503   SMESH_MesherHelper (const SMESH_MesherHelper& theOther) {};
504
505   // special map for using during creation of quadratic elements
506   TLinkNodeMap    myTLinkNodeMap;
507
508   std::set< int > myDegenShapeIds;
509   std::set< int > mySeamShapeIds;
510   double          myPar1[2], myPar2[2]; // U and V bounds of a closed periodic surface
511   int             myParIndex;     // bounds' index (1-U, 2-V, 3-both)
512
513   typedef std::map< int, GeomAPI_ProjectPointOnSurf* > TID2ProjectorOnSurf;
514   TID2ProjectorOnSurf myFace2Projector;
515   typedef std::map< int, GeomAPI_ProjectPointOnCurve* > TID2ProjectorOnCurve;
516   TID2ProjectorOnCurve myEdge2Projector;
517
518   TopoDS_Shape    myShape;
519   SMESH_Mesh*     myMesh;
520   int             myShapeID;
521
522   // to create quadratic elements
523   bool            myCreateQuadratic;
524   bool            mySetElemOnShape;
525
526   std::map< int,bool > myNodePosShapesValidity;
527   bool toCheckPosOnShape(int shapeID ) const;
528   void setPosOnShapeValidity(int shapeID, bool ok ) const;
529
530 };
531
532
533 #endif