Salome HOME
IPAL52698: Convert to quadratic is wrong (hard periodic surfaces)
[modules/smesh.git] / src / SMESH / SMESH_MesherHelper.hxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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 and provides other utilities
62  *
63  * - It allows meshers not to care about creation of medium nodes
64  * when filling a quadratic mesh. Helper does it itself.
65  * It defines order of elements to create when IsQuadraticSubMesh()
66  * is called.
67  * - It provides information on a shape it is initialized with:
68  * periodicity, presence of singularities etc.
69  * - ...
70  */
71 //=======================================================================
72
73 class SMESH_EXPORT SMESH_MesherHelper
74 {
75  public:
76   // ---------- PUBLIC UTILITIES ----------
77   
78   /*!
79    * \brief Returns true if all elements of a sub-mesh are of same shape
80     * \param smDS - sub-mesh to check elements of
81     * \param shape - expected shape of elements
82     * \param nullSubMeshRes - result value for the case of smDS == NULL
83     * \retval bool - check result
84    */
85   static bool IsSameElemGeometry(const SMESHDS_SubMesh* smDS,
86                                  SMDSAbs_GeometryType   shape,
87                                  const bool             nullSubMeshRes = true);
88
89   /*!
90    * \brief Load nodes bound to face into a map of node columns
91     * \param theParam2ColumnMap - map of node columns to fill
92     * \param theFace - the face on which nodes are searched for
93     * \param theBaseSide - the edges holding nodes on which columns' bases
94     * \param theMesh - the mesh containing nodes
95     * \retval bool - false if something is wrong
96    * 
97    * The key of the map is a normalized parameter of each
98    * base node on theBaseSide. Edges in theBaseSide must be sequenced.
99    * This method works in supposition that nodes on the face
100    * forms a structured grid and elements can be quardrangles or triangles
101    */
102   static bool LoadNodeColumns(TParam2ColumnMap &            theParam2ColumnMap,
103                               const TopoDS_Face&            theFace,
104                               const std::list<TopoDS_Edge>& theBaseSide,
105                               SMESHDS_Mesh*                 theMesh,
106                               SMESH_ProxyMesh*              theProxyMesh=0);
107   /*!
108    * \brief Variant of LoadNodeColumns() above with theBaseSide given by one edge
109    */
110   static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
111                               const TopoDS_Face& theFace,
112                               const TopoDS_Edge& theBaseEdge,
113                               SMESHDS_Mesh*      theMesh,
114                               SMESH_ProxyMesh*   theProxyMesh=0);
115   /*!
116    * \brief Return true if 2D mesh on FACE is structured
117    */
118   static bool IsStructured( SMESH_subMesh* faceSM );
119
120   /*!
121    * \brief Return true if 2D mesh on FACE is distored
122    */
123   static bool IsDistorted2D( SMESH_subMesh* faceSM, bool checkUV=false );
124
125   /*!
126    * \brief Returns true if given node is medium
127     * \param n - node to check
128     * \param typeToCheck - type of elements containing the node to ask about node status
129     * \retval bool - check result
130    */
131   static bool IsMedium(const SMDS_MeshNode*      node,
132                        const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
133   /*!
134    * \brief Return support shape of a node
135    * \param node - the node
136    * \param meshDS - mesh DS
137    * \retval TopoDS_Shape - found support shape
138    * \sa SMESH_Algo::VertexNode( const TopoDS_Vertex&, SMESHDS_Mesh* )
139    */
140   static TopoDS_Shape GetSubShapeByNode(const SMDS_MeshNode* node,
141                                         const SMESHDS_Mesh*  meshDS);
142
143   /*!
144    * \brief Return a valid node index, fixing the given one if necessary
145     * \param ind - node index
146     * \param nbNodes - total nb of nodes
147     * \retval int - valid node index
148    */
149   static inline int WrapIndex(int ind, const int nbNodes) {
150     return (( ind %= nbNodes ) < 0 ) ? ind + nbNodes : ind;
151   }
152
153   /*!
154    * \brief Return UV of a point inside a quadrilateral FACE by it's
155    *        normalized parameters within a unit quadrangle and the
156    *        corresponding projections on sub-shapes of the real-world FACE.
157    *        The used calculation method is called Trans-Finite Interpolation (TFI).
158    *  \param x,y - normalized parameters that should be in range [0,1]
159    *  \param a0,a1,a2,a3 - UV of VERTEXes of the FACE == projections on VERTEXes
160    *  \param p0,p1,p2,p3 - UV of the point projections on EDGEs of the FACE
161    *  \return gp_XY - UV of the point on the FACE
162    *
163    *  Y ^              Order of those UV in the FACE is as follows.
164    *    |
165    *   a3   p2    a2
166    *    o---x-----o
167    *    |   :     |
168    *    |   :UV   |
169    * p3 x...O.....x p1
170    *    |   :     |
171    *    o---x-----o    ----> X
172    *   a0   p0    a1
173    */
174   inline static gp_XY calcTFI(double x, double y,
175                               const gp_XY& a0,const gp_XY& a1,const gp_XY& a2,const gp_XY& a3,
176                               const gp_XY& p0,const gp_XY& p1,const gp_XY& p2,const gp_XY& p3);
177
178   /*!
179    * \brief Same as "gp_XY calcTFI(...)" but in 3D
180    */
181   inline static gp_XYZ calcTFI(double x, double y,
182                                const gp_XYZ& a0,const gp_XYZ& a1,const gp_XYZ& a2,const gp_XYZ& a3,
183                                const gp_XYZ& p0,const gp_XYZ& p1,const gp_XYZ& p2,const gp_XYZ& p3);
184   /*!
185    * \brief Count nb of sub-shapes
186     * \param shape - the shape
187     * \param type - the type of sub-shapes to count
188     * \param ignoreSame - if true, use map not to count same shapes, esle use explorer
189     * \retval int - the calculated number
190    */
191   static int Count(const TopoDS_Shape&    shape,
192                    const TopAbs_ShapeEnum type,
193                    const bool             ignoreSame);
194
195   /*!
196    * \brief Return number of unique ancestors of the shape
197    */
198   static int NbAncestors(const TopoDS_Shape& shape,
199                          const SMESH_Mesh&   mesh,
200                          TopAbs_ShapeEnum    ancestorType=TopAbs_SHAPE);
201   /*!
202    * \brief Return iterator on ancestors of the given type
203    */
204   static PShapeIteratorPtr GetAncestors(const TopoDS_Shape& shape,
205                                         const SMESH_Mesh&   mesh,
206                                         TopAbs_ShapeEnum    ancestorType);
207   /*!
208    * \brief Find a common ancestor, of the given type, of two shapes
209    */
210   static TopoDS_Shape GetCommonAncestor(const TopoDS_Shape& shape1,
211                                         const TopoDS_Shape& shape2,
212                                         const SMESH_Mesh&   mesh,
213                                         TopAbs_ShapeEnum    ancestorType);
214   /*!
215    * \brief Return orientation of sub-shape in the main shape
216    */
217   static TopAbs_Orientation GetSubShapeOri(const TopoDS_Shape& shape,
218                                            const TopoDS_Shape& subShape);
219
220   static bool IsSubShape( const TopoDS_Shape& shape, const TopoDS_Shape& mainShape );
221
222   static bool IsSubShape( const TopoDS_Shape& shape, SMESH_Mesh* aMesh );
223
224   static bool IsBlock( const TopoDS_Shape& shape );
225
226   static double MaxTolerance( const TopoDS_Shape& shape );
227
228   static double GetAngle( const TopoDS_Edge & E1, const TopoDS_Edge & E2,
229                           const TopoDS_Face & F,  const TopoDS_Vertex & V,
230                           gp_Vec* faceNormal=0);
231
232   static bool IsClosedEdge( const TopoDS_Edge& anEdge );
233
234   static TopoDS_Vertex IthVertex( const bool is2nd, TopoDS_Edge anEdge, const bool CumOri=true );
235
236   static TopAbs_ShapeEnum GetGroupType(const TopoDS_Shape& group,
237                                        const bool          avoidCompound=false);
238
239
240 public:
241   // ---------- PUBLIC INSTANCE METHODS ----------
242
243   // constructor
244   SMESH_MesherHelper(SMESH_Mesh& theMesh);
245
246   SMESH_Mesh* GetMesh() const { return myMesh; }
247     
248   SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
249     
250   /*!
251    * Check submesh for given shape: if all elements on this shape are quadratic,
252    * quadratic elements will be created. Also fill myTLinkNodeMap
253    */
254   bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
255   /*!
256    * \brief Set order of elements to create without calling IsQuadraticSubMesh()
257    */
258
259   /*!
260    * \brief Set myCreateQuadratic flag
261    */
262   void SetIsQuadratic(const bool theBuildQuadratic)
263   { myCreateQuadratic = theBuildQuadratic; }
264
265   /*!
266    * \brief Set myCreateBiQuadratic flag
267    */
268   void SetIsBiQuadratic(const bool theBuildBiQuadratic)
269   { myCreateBiQuadratic = theBuildBiQuadratic; }
270   
271   /*!
272    * \brief Return myCreateQuadratic flag
273    */
274   bool GetIsQuadratic() const { return myCreateQuadratic; }
275
276   /*
277    * \brief Find out elements orientation on a geometrical face
278    */
279   bool IsReversedSubMesh (const TopoDS_Face& theFace);
280
281   /*!
282    * \brief Return myCreateBiQuadratic flag
283    */
284   bool GetIsBiQuadratic() const { return myCreateBiQuadratic; }
285
286   /*!
287    * \brief Move medium nodes of faces and volumes to fix distorted elements
288    * \param error - container of fixed distorted elements
289    * \param volumeOnly - fix nodes on geom faces or not if the shape is solid
290    */
291   void FixQuadraticElements(SMESH_ComputeErrorPtr& error, bool volumeOnly=true);
292
293   /*!
294    * \brief To set created elements on the shape set by IsQuadraticSubMesh()
295    *        or the next methods. By defaul elements are set on the shape if
296    *        a mesh has no shape to be meshed
297    */
298   bool SetElementsOnShape(bool toSet)
299   { bool res = mySetElemOnShape; mySetElemOnShape = toSet; return res; }
300
301   /*!
302    * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
303    */
304   void SetSubShape(const int           subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
305   void SetSubShape(const TopoDS_Shape& subShape);
306   /*!
307    * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape() 
308     * \retval int - shape index in SMESHDS
309    */
310   int GetSubShapeID() const { return myShapeID; }
311   /*!
312    * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape() 
313    */
314   const TopoDS_Shape& GetSubShape() const  { return myShape; }
315
316   /*!
317    * Creates a node (!Note ID before u=0.,v0.)
318    */
319   SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0, double u=0., double v=0.);
320   /*!
321    * Creates quadratic or linear edge
322    */
323   SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
324                          const SMDS_MeshNode* n2,
325                          const int id = 0, 
326                          const bool force3d = true);
327   /*!
328    * Creates quadratic or linear triangle
329    */
330   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
331                          const SMDS_MeshNode* n2,
332                          const SMDS_MeshNode* n3,
333                          const int id=0, 
334                          const bool force3d = false);
335   /*!
336    * Creates bi-quadratic, quadratic or linear quadrangle
337    */
338   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
339                          const SMDS_MeshNode* n2,
340                          const SMDS_MeshNode* n3,
341                          const SMDS_MeshNode* n4,
342                          const int id = 0,
343                          const bool force3d = false);
344   /*!
345    * Creates polygon, with additional nodes in quadratic mesh
346    */
347   SMDS_MeshFace* AddPolygonalFace (const std::vector<const SMDS_MeshNode*>& nodes,
348                                    const int id = 0,
349                                    const bool force3d = false);
350   /*!
351    * Creates quadratic or linear tetrahedron
352    */
353   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
354                              const SMDS_MeshNode* n2,
355                              const SMDS_MeshNode* n3,
356                              const SMDS_MeshNode* n4,
357                              const int id = 0,
358                              const bool force3d = true);
359   /*!
360    * Creates quadratic or linear pyramid
361    */
362   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
363                              const SMDS_MeshNode* n2,
364                              const SMDS_MeshNode* n3,
365                              const SMDS_MeshNode* n4,
366                              const SMDS_MeshNode* n5,
367                              const int id = 0,
368                              const bool force3d = true);
369   /*!
370    * Creates quadratic or linear pentahedron
371    */
372   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
373                              const SMDS_MeshNode* n2,
374                              const SMDS_MeshNode* n3,
375                              const SMDS_MeshNode* n4,
376                              const SMDS_MeshNode* n5,
377                              const SMDS_MeshNode* n6,
378                              const int id = 0, 
379                              const bool force3d = true);
380   /*!
381    * Creates bi-quadratic, quadratic or linear hexahedron
382    */
383   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
384                              const SMDS_MeshNode* n2,
385                              const SMDS_MeshNode* n3,
386                              const SMDS_MeshNode* n4,
387                              const SMDS_MeshNode* n5,
388                              const SMDS_MeshNode* n6,
389                              const SMDS_MeshNode* n7,
390                              const SMDS_MeshNode* n8,
391                              const int id = 0, 
392                              bool force3d = true);
393
394   /*!
395    * Creates LINEAR!!!!!!!!! octahedron
396    */
397   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
398                              const SMDS_MeshNode* n2,
399                              const SMDS_MeshNode* n3,
400                              const SMDS_MeshNode* n4,
401                              const SMDS_MeshNode* n5,
402                              const SMDS_MeshNode* n6,
403                              const SMDS_MeshNode* n7,
404                              const SMDS_MeshNode* n8,
405                              const SMDS_MeshNode* n9,
406                              const SMDS_MeshNode* n10,
407                              const SMDS_MeshNode* n11,
408                              const SMDS_MeshNode* n12,
409                              const int id = 0, 
410                              bool force3d = true);
411
412   /*!
413    * Creates polyhedron. In quadratic mesh, adds medium nodes
414    */
415   SMDS_MeshVolume* AddPolyhedralVolume (const std::vector<const SMDS_MeshNode*>& nodes,
416                                         const std::vector<int>&                  quantities,
417                                         const int                                ID=0,
418                                         const bool                               force3d = true);
419   /*!
420    * \brief Enables fixing node parameters on EDGEs and FACEs by
421    * GetNodeU(...,check=true), GetNodeUV(...,check=true), CheckNodeUV() and
422    * CheckNodeU() in case if a node lies on a shape set via SetSubShape().
423    * Default is False
424    */
425   void ToFixNodeParameters(bool toFix);
426
427   /*!
428    * \brief Return U of the given node on the edge
429    */
430   double GetNodeU(const TopoDS_Edge&   theEdge,
431                   const SMDS_MeshNode* theNode,
432                   const SMDS_MeshNode* inEdgeNode=0,
433                   bool*                check=0) const;
434   /*!
435    * \brief Return node UV on face
436    *  \param inFaceNode - a node of element being created located inside a face
437    *  \param check - if provided, returns result of UV check that it enforces
438    */
439   gp_XY GetNodeUV(const TopoDS_Face&   F,
440                   const SMDS_MeshNode* n,
441                   const SMDS_MeshNode* inFaceNode=0,
442                   bool*                check=0) const;
443   /*!
444    * \brief Check and fix node UV on a face
445    *  \param force - check even if checks of other nodes on this face passed OK
446    *  \param distXYZ - returns result distance and point coordinates
447    *  \retval bool - false if UV is bad and could not be fixed
448    */
449   bool CheckNodeUV(const TopoDS_Face&   F,
450                    const SMDS_MeshNode* n,
451                    gp_XY&               uv,
452                    const double         tol,
453                    const bool           force=false,
454                    double               distXYZ[4]=0) const;
455   /*!
456    * \brief Check and fix node U on an edge
457    *  \param force - check even if checks of other nodes on this edge passed OK
458    *  \param distXYZ - returns result distance and point coordinates
459    *  \retval bool - false if U is bad and could not be fixed
460    */
461   bool CheckNodeU(const TopoDS_Edge&   E,
462                   const SMDS_MeshNode* n,
463                   double&              u,
464                   const double         tol,
465                   const bool           force=false,
466                   double               distXYZ[4]=0) const;
467   /*!
468    * \brief Return middle UV taking in account surface period
469    */
470   static gp_XY GetMiddleUV(const Handle(Geom_Surface)& surface,
471                            const gp_XY&                uv1,
472                            const gp_XY&                uv2);
473   /*!
474    * \brief Return UV for the central node of a biquadratic triangle
475    */
476   static gp_XY GetCenterUV(const gp_XY& uv1,
477                            const gp_XY& uv2, 
478                            const gp_XY& uv3, 
479                            const gp_XY& uv12,
480                            const gp_XY& uv23,
481                            const gp_XY& uv31,
482                            bool *       isBadTria=0);
483   /*!
484    * \brief Define a pointer to wrapper over a function of gp_XY class,
485    *       suitable to pass as xyFunPtr to ApplyIn2D().
486    *       For exaple gp_XY_FunPtr(Added) defines pointer gp_XY_Added to function
487    *       calling gp_XY::Added(gp_XY), which is to be used like following
488    *       ApplyIn2D(surf, uv1, uv2, gp_XY_Added)
489    */
490 #define gp_XY_FunPtr(meth) \
491   static gp_XY __gpXY_##meth (const gp_XY& uv1, const gp_XY& uv2) { return uv1.meth( uv2 ); } \
492   static xyFunPtr gp_XY_##meth = & __gpXY_##meth
493
494   /*!
495    * \brief Perform given operation on two 2d points in parameric space of given surface.
496    *        It takes into account period of the surface. Use gp_XY_FunPtr macro
497    *        to easily define pointer to function of gp_XY class.
498    */
499   static gp_XY ApplyIn2D(Handle(Geom_Surface) surface,
500                          const gp_XY&         uv1,
501                          const gp_XY&         uv2,
502                          xyFunPtr             fun,
503                          const bool           resultInPeriod=true);
504
505   /*!
506    * \brief Move node positions on a FACE within surface period
507    *  \param [in] face - the FACE
508    *  \param [inout] uv - node positions to adjust
509    *  \param [in] nbUV - nb of \a uv
510    */
511   void AdjustByPeriod( const TopoDS_Face& face, gp_XY uv[], const int nbUV );
512
513   /*!
514    * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
515    *  \retval bool - return true if the face is periodic
516    *
517    * If F is Null, answer about subshape set through IsQuadraticSubMesh() or
518    * SetSubShape()
519    */
520   bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
521
522   /*!
523    * \brief Return projector intitialized by given face without location, which is returned
524    */
525   GeomAPI_ProjectPointOnSurf& GetProjector(const TopoDS_Face& F,
526                                            TopLoc_Location&   loc,
527                                            double             tol=0 ) const; 
528
529   /*!
530    * \brief Check if shape is a degenerated edge or it's vertex
531     * \param subShape - edge or vertex index in SMESHDS
532     * \retval bool - true if subShape is a degenerated shape
533     *
534     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
535    */
536   bool IsDegenShape(const int subShape) const
537   { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
538   /*!
539    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
540    *        has a degenerated edges
541     * \retval bool - true if it has
542    */
543   bool HasDegeneratedEdges() const { return !myDegenShapeIds.empty(); }
544
545   /*!
546    * \brief Check if shape is a seam edge or it's vertex
547     * \param subShape - edge or vertex index in SMESHDS
548     * \retval bool - true if subShape is a seam shape
549     *
550     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
551     * Seam shape has two 2D alternative represenations on the face
552    */
553   bool IsSeamShape(const int subShape) const
554   { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
555   /*!
556    * \brief Check if shape is a seam edge or it's vertex
557     * \param subShape - edge or vertex
558     * \retval bool - true if subShape is a seam shape
559     *
560     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
561     * Seam shape has two 2D alternative represenations on the face
562    */
563   bool IsSeamShape(const TopoDS_Shape& subShape) const
564   { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
565   /*!
566    * \brief Return true if an edge or a vertex encounters twice in face wire
567    *  \param subShape - Id of edge or vertex
568    */
569   bool IsRealSeam(const int subShape) const
570   { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
571   /*!
572    * \brief Return true if an edge or a vertex encounters twice in face wire
573    *  \param subShape - edge or vertex
574    */
575   bool IsRealSeam(const TopoDS_Shape& subShape) const
576   { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
577   /*!
578    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
579    *        has a seam edge, i.e. an edge that has two parametric representations
580    *        on a surface
581    *  \retval bool - true if it has
582    */
583   bool HasSeam() const { return !mySeamShapeIds.empty(); }
584   /*!
585    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
586    *        has a seam edge that encounters twice in a wire
587    *  \retval bool - true if it has
588    */
589   bool HasRealSeam() const { return HasSeam() && ( *mySeamShapeIds.begin() < 0 ); }
590   /*!
591    * \brief Return index of periodic parametric direction of a closed face
592    *  \retval int - 1 for U, 2 for V direction
593    */
594   int GetPeriodicIndex() const { return myParIndex; }
595   /*!
596    * \brief Return an alternative parameter for a node on seam
597    */
598   double GetOtherParam(const double param) const;
599
600   /*!
601    * \brief Return existing or create new medium nodes between given ones
602    *  \param force3d - true means node creation at the middle between the
603    *                   two given nodes, else node position is found on its
604    *                   supporting geometrical shape, if any.
605    *  \param expectedSupport - shape type corresponding to element being created
606    *                           , e.g TopAbs_EDGE if SMDSAbs_Edge is created
607    *                           basing on \a n1 and \a n2
608    */
609   const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
610                                      const SMDS_MeshNode* n2,
611                                      const bool           force3d,
612                                      TopAbs_ShapeEnum     expectedSupport=TopAbs_SHAPE);
613   /*!
614    * \brief Return existing or create a new central node for a quardilateral
615    *       quadratic face given its 8 nodes.
616    *  \param force3d - true means node creation in between the given nodes,
617    *                   else node position is found on a geometrical face if any.
618    */
619   const SMDS_MeshNode* GetCentralNode(const SMDS_MeshNode* n1,
620                                       const SMDS_MeshNode* n2,
621                                       const SMDS_MeshNode* n3,
622                                       const SMDS_MeshNode* n4,
623                                       const SMDS_MeshNode* n12,
624                                       const SMDS_MeshNode* n23,
625                                       const SMDS_MeshNode* n34,
626                                       const SMDS_MeshNode* n41,
627                                       bool                 force3d);
628   /*!
629    * \brief Return existing or create a new central node for a 
630    *       quadratic triangle given its 6 nodes.
631    *  \param force3d - true means node creation in between the given nodes,
632    *                   else node position is found on a geometrical face if any.
633    */
634   const SMDS_MeshNode* GetCentralNode(const SMDS_MeshNode* n1,
635                                       const SMDS_MeshNode* n2,
636                                       const SMDS_MeshNode* n3,
637                                       const SMDS_MeshNode* n12,
638                                       const SMDS_MeshNode* n23,
639                                       const SMDS_MeshNode* n31,
640                                       bool                 force3d);
641   /*!
642    * \brief Return index and type of the shape (EDGE or FACE only) to set a medium node on
643    */
644   std::pair<int, TopAbs_ShapeEnum> GetMediumPos(const SMDS_MeshNode* n1,
645                                                 const SMDS_MeshNode* n2,
646                                                 const bool           useCurSubShape=false,
647                                                 TopAbs_ShapeEnum     expectedSupport=TopAbs_SHAPE);
648   /*!
649    * \brief Add a link in my data structure
650    */
651   void AddTLinkNode(const SMDS_MeshNode* n1,
652                     const SMDS_MeshNode* n2,
653                     const SMDS_MeshNode* n12);
654   /*!
655    * \brief Add many links in my data structure
656    */
657   void AddTLinkNodeMap(const TLinkNodeMap& aMap)
658     { myTLinkNodeMap.insert(aMap.begin(), aMap.end()); }
659
660   bool AddTLinks(const SMDS_MeshEdge*   edge);
661   bool AddTLinks(const SMDS_MeshFace*   face);
662   bool AddTLinks(const SMDS_MeshVolume* vol);
663
664   /**
665    * Returns myTLinkNodeMap
666    */
667   const TLinkNodeMap& GetTLinkNodeMap() const { return myTLinkNodeMap; }
668
669   /**
670    * Check mesh without geometry for: if all elements on this shape are quadratic,
671    * quadratic elements will be created.
672    * Used then generated 3D mesh without geometry.
673    */
674   enum MType{ LINEAR, QUADRATIC, COMP };
675   MType IsQuadraticMesh();
676   
677   virtual ~SMESH_MesherHelper();
678
679  protected:
680
681   /*!
682    * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
683    *  \param uv1 - UV on the seam
684    *  \param uv2 - UV within a face
685    *  \retval gp_Pnt2d - selected UV
686    */
687   gp_Pnt2d getUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
688
689   const SMDS_MeshNode* getMediumNodeOnComposedWire(const SMDS_MeshNode* n1,
690                                                    const SMDS_MeshNode* n2,
691                                                    bool                 force3d);
692
693   double getFaceMaxTol( const TopoDS_Shape& face ) const;
694
695  private:
696
697   // Forbiden copy constructor
698   SMESH_MesherHelper (const SMESH_MesherHelper& theOther);
699
700   // key of a map of bi-quadratic face to it's central node
701   struct TBiQuad: public std::pair<int, std::pair<int, int> >
702   {
703     TBiQuad(const SMDS_MeshNode* n1,
704             const SMDS_MeshNode* n2, 
705             const SMDS_MeshNode* n3,
706             const SMDS_MeshNode* n4=0)
707     {
708       TIDSortedNodeSet s;
709       s.insert(n1);
710       s.insert(n2);
711       s.insert(n3);
712       if ( n4 ) s.insert(n4);
713       TIDSortedNodeSet::iterator n = s.begin();
714       first = (*n++)->GetID();
715       second.first = (*n++)->GetID();
716       second.second = (*n++)->GetID();
717     }
718   };
719
720   // maps used during creation of quadratic elements
721   TLinkNodeMap                              myTLinkNodeMap;       // medium nodes on links
722   std::map< TBiQuad, const SMDS_MeshNode* > myMapWithCentralNode; // central nodes of faces
723
724   std::set< int > myDegenShapeIds;
725   std::set< int > mySeamShapeIds;
726   double          myPar1[2], myPar2[2]; // U and V bounds of a closed periodic surface
727   int             myParIndex;     // bounds' index (1-U, 2-V, 3-both)
728
729   std::map< int, double > myFaceMaxTol;
730
731   typedef std::map< int, GeomAPI_ProjectPointOnSurf* >  TID2ProjectorOnSurf;
732   typedef std::map< int, GeomAPI_ProjectPointOnCurve* > TID2ProjectorOnCurve;
733   TID2ProjectorOnSurf  myFace2Projector;
734   TID2ProjectorOnCurve myEdge2Projector;
735
736   TopoDS_Shape    myShape;
737   SMESH_Mesh*     myMesh;
738   int             myShapeID;
739
740   bool            myCreateQuadratic;
741   bool            myCreateBiQuadratic;
742   bool            mySetElemOnShape;
743   bool            myFixNodeParameters;
744
745   std::map< int,bool > myNodePosShapesValidity;
746   bool toCheckPosOnShape(int shapeID ) const;
747   void setPosOnShapeValidity(int shapeID, bool ok ) const;
748 };
749
750 //=======================================================================
751 inline gp_XY
752 SMESH_MesherHelper::calcTFI(double x, double y,
753                             const gp_XY& a0,const gp_XY& a1,const gp_XY& a2,const gp_XY& a3,
754                             const gp_XY& p0,const gp_XY& p1,const gp_XY& p2,const gp_XY& p3)
755 {
756   return
757     ((1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3 ) -
758     ((1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3);
759 }
760 //=======================================================================
761 inline gp_XYZ
762 SMESH_MesherHelper::calcTFI(double x, double y,
763                             const gp_XYZ& a0,const gp_XYZ& a1,const gp_XYZ& a2,const gp_XYZ& a3,
764                             const gp_XYZ& p0,const gp_XYZ& p1,const gp_XYZ& p2,const gp_XYZ& p3)
765 {
766   return
767     ((1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3 ) -
768     ((1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3);
769 }
770 //=======================================================================
771
772 #endif