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