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