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