Salome HOME
0022100: EDF 2413 SMESH: Take into account TRIA7
[modules/smesh.git] / src / SMESH / SMESH_MesherHelper.hxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:      SMESH_MesherHelper.hxx
24 // Created:   15.02.06 14:48:09
25 // Author:    Sergey KUUL
26 //
27 #ifndef SMESH_MesherHelper_HeaderFile
28 #define SMESH_MesherHelper_HeaderFile
29
30 #include "SMESH_SMESH.hxx"
31
32 #include "SMESH_MeshEditor.hxx" // needed for many meshers
33 #include <SMDS_MeshNode.hxx>
34 #include <SMDS_QuadraticEdge.hxx>
35
36 #include <Geom_Surface.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <gp_Pnt2d.hxx>
40
41 #include <map>
42 #include <vector>
43
44 class GeomAPI_ProjectPointOnSurf;
45 class GeomAPI_ProjectPointOnCurve;
46 class SMESH_ProxyMesh;
47
48 typedef std::map<SMESH_TLink, const SMDS_MeshNode*>           TLinkNodeMap;
49 typedef std::map<SMESH_TLink, const SMDS_MeshNode*>::iterator ItTLinkNode;
50
51 typedef SMDS_Iterator<const TopoDS_Shape*>  PShapeIterator;
52 typedef boost::shared_ptr< PShapeIterator > PShapeIteratorPtr;
53   
54 typedef std::vector<const SMDS_MeshNode* > TNodeColumn;
55 typedef std::map< double, TNodeColumn >    TParam2ColumnMap;
56
57 typedef gp_XY (*xyFunPtr)(const gp_XY& uv1, const gp_XY& uv2);
58
59 //=======================================================================
60 /*!
61  * \brief It helps meshers to add elements 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 rectangular 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 Returns true if given node is medium
122     * \param n - node to check
123     * \param typeToCheck - type of elements containing the node to ask about node status
124     * \retval bool - check result
125    */
126   static bool IsMedium(const SMDS_MeshNode*      node,
127                        const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
128   /*!
129    * \brief Return support shape of a node
130    * \param node - the node
131    * \param meshDS - mesh DS
132    * \retval TopoDS_Shape - found support shape
133    */
134   static TopoDS_Shape GetSubShapeByNode(const SMDS_MeshNode* node,
135                                         const SMESHDS_Mesh*  meshDS);
136
137   /*!
138    * \brief Return a valid node index, fixing the given one if necessary
139     * \param ind - node index
140     * \param nbNodes - total nb of nodes
141     * \retval int - valid node index
142    */
143   static int WrapIndex(const int ind, const int nbNodes) {
144     if ( ind < 0 ) return nbNodes + ind % nbNodes;
145     if ( ind >= nbNodes ) return ind % nbNodes;
146     return ind;
147   }
148
149   /*!
150    * \brief Return UV of a point inside a quadrilateral FACE by it's
151    *        normalized parameters within a unit quadrangle and the
152    *        corresponding projections on sub-shapes of the real-world FACE.
153    *        The used calculation method is called Trans-Finite Interpolation (TFI).
154    *  \param x,y - normalized parameters that should be in range [0,1]
155    *  \param a0,a1,a2,a3 - UV of VERTEXes of the FACE == projections on VERTEXes
156    *  \param p0,p1,p2,p3 - UV of the point projections on EDGEs of the FACE
157    *  \return gp_XY - UV of the point on the FACE
158    *
159    * Order of those UV in the FACE is as follows.
160    *   a4   p3    a3
161    *    o---x-----o
162    *    |   :     |
163    *    |   :UV   |
164    * p4 x...O.....x p2
165    *    |   :     |
166    *    o---x-----o
167    *   a1   p1    a2
168    */
169   inline static gp_XY calcTFI(double x, double y,
170                               const gp_XY a0,const gp_XY a1,const gp_XY a2,const gp_XY a3,
171                               const gp_XY p0,const gp_XY p1,const gp_XY p2,const gp_XY p3);
172
173   /*!
174    * \brief Same as "gp_XY calcTFI(...)" but in 3D
175    */
176   inline static gp_XYZ calcTFI(double x, double y,
177                                const gp_XYZ a0,const gp_XYZ a1,const gp_XYZ a2,const gp_XYZ a3,
178                                const gp_XYZ p0,const gp_XYZ p1,const gp_XYZ p2,const gp_XYZ p3);
179   /*!
180    * \brief Count nb of sub-shapes
181     * \param shape - the shape
182     * \param type - the type of sub-shapes to count
183     * \param ignoreSame - if true, use map not to count same shapes, esle use explorer
184     * \retval int - the calculated number
185    */
186   static int Count(const TopoDS_Shape&    shape,
187                    const TopAbs_ShapeEnum type,
188                    const bool             ignoreSame);
189
190   /*!
191    * \brief Return number of unique ancestors of the shape
192    */
193   static int NbAncestors(const TopoDS_Shape& shape,
194                          const SMESH_Mesh&   mesh,
195                          TopAbs_ShapeEnum    ancestorType=TopAbs_SHAPE);
196   /*!
197    * \brief Return iterator on ancestors of the given type
198    */
199   static PShapeIteratorPtr GetAncestors(const TopoDS_Shape& shape,
200                                         const SMESH_Mesh&   mesh,
201                                         TopAbs_ShapeEnum    ancestorType);
202   /*!
203    * \brief Find a common ancestor, of the given type, of two shapes
204    */
205   static TopoDS_Shape GetCommonAncestor(const TopoDS_Shape& shape1,
206                                         const TopoDS_Shape& shape2,
207                                         const SMESH_Mesh&   mesh,
208                                         TopAbs_ShapeEnum    ancestorType);
209   /*!
210    * \brief Return orientation of sub-shape in the main shape
211    */
212   static TopAbs_Orientation GetSubShapeOri(const TopoDS_Shape& shape,
213                                            const TopoDS_Shape& subShape);
214
215   static bool IsSubShape( const TopoDS_Shape& shape, const TopoDS_Shape& mainShape );
216
217   static bool IsSubShape( const TopoDS_Shape& shape, SMESH_Mesh* aMesh );
218
219   static double MaxTolerance( const TopoDS_Shape& shape );
220
221   static bool IsClosedEdge( const TopoDS_Edge& anEdge );
222
223   static TopoDS_Vertex IthVertex( const bool is2nd, TopoDS_Edge anEdge, const bool CumOri=true );
224
225   static TopAbs_ShapeEnum GetGroupType(const TopoDS_Shape& group,
226                                        const bool          avoidCompound=false);
227
228
229 public:
230   // ---------- PUBLIC INSTANCE METHODS ----------
231
232   // constructor
233   SMESH_MesherHelper(SMESH_Mesh& theMesh);
234
235   SMESH_Mesh* GetMesh() const { return myMesh; }
236     
237   SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
238     
239   /*!
240    * Check submesh for given shape: if all elements on this shape are quadratic,
241    * quadratic elements will be created. Also fill myTLinkNodeMap
242    */
243   bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
244   /*!
245    * \brief Set order of elements to create without calling IsQuadraticSubMesh()
246    */
247
248   /*!
249    * \brief Set myCreateQuadratic flag
250    */
251   void SetIsQuadratic(const bool theBuildQuadratic)
252   { myCreateQuadratic = theBuildQuadratic; }
253
254   /*!
255    * \brief Set myCreateBiQuadratic flag
256    */
257   void SetIsBiQuadratic(const bool theBuildBiQuadratic)
258   { myCreateBiQuadratic = theBuildBiQuadratic; }
259   
260   /*!
261    * \brief Return myCreateQuadratic flag
262    */
263   bool GetIsQuadratic() const { return myCreateQuadratic; }
264
265   /*
266    * \brief Find out elements orientation on a geometrical face
267    */
268   bool IsReversedSubMesh (const TopoDS_Face& theFace);
269
270   /*!
271    * \brief Return myCreateBiQuadratic flag
272    */
273   bool GetIsBiQuadratic() const { return myCreateBiQuadratic; }
274
275   /*!
276    * \brief Move medium nodes of faces and volumes to fix distorted elements
277    * \param error - container of fixed distorted elements
278    * \param volumeOnly - fix nodes on geom faces or not if the shape is solid
279    */
280   void FixQuadraticElements(SMESH_ComputeErrorPtr& error, bool volumeOnly=true);
281
282   /*!
283    * \brief To set created elements on the shape set by IsQuadraticSubMesh()
284    *        or the next methods. By defaul elements are set on the shape if
285    *        a mesh has no shape to be meshed
286    */
287   bool SetElementsOnShape(bool toSet)
288   { bool res = mySetElemOnShape; mySetElemOnShape = toSet; return res; }
289
290   /*!
291    * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
292    */
293   void SetSubShape(const int           subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
294   void SetSubShape(const TopoDS_Shape& subShape);
295   /*!
296    * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape() 
297     * \retval int - shape index in SMESHDS
298    */
299   int GetSubShapeID() const { return myShapeID; }
300   /*!
301    * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape() 
302    */
303   const TopoDS_Shape& GetSubShape() const  { return myShape; }
304
305   /*!
306    * Creates a node
307    */
308   SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0, double u=0., double v=0.);
309   /*!
310    * Creates quadratic or linear edge
311    */
312   SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
313                          const SMDS_MeshNode* n2,
314                          const int id = 0, 
315                          const bool force3d = true);
316   /*!
317    * Creates quadratic or linear triangle
318    */
319   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
320                          const SMDS_MeshNode* n2,
321                          const SMDS_MeshNode* n3,
322                          const int id=0, 
323                          const bool force3d = false);
324   /*!
325    * Creates bi-quadratic, quadratic or linear quadrangle
326    */
327   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
328                          const SMDS_MeshNode* n2,
329                          const SMDS_MeshNode* n3,
330                          const SMDS_MeshNode* n4,
331                          const int id = 0,
332                          const bool force3d = false);
333   /*!
334    * Creates polygon, with additional nodes in quadratic mesh
335    */
336   SMDS_MeshFace* AddPolygonalFace (const std::vector<const SMDS_MeshNode*>& nodes,
337                                    const int id = 0,
338                                    const bool force3d = false);
339   /*!
340    * Creates quadratic or linear tetrahedron
341    */
342   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
343                              const SMDS_MeshNode* n2,
344                              const SMDS_MeshNode* n3,
345                              const SMDS_MeshNode* n4,
346                              const int id = 0,
347                              const bool force3d = true);
348   /*!
349    * Creates quadratic or linear pyramid
350    */
351   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
352                              const SMDS_MeshNode* n2,
353                              const SMDS_MeshNode* n3,
354                              const SMDS_MeshNode* n4,
355                              const SMDS_MeshNode* n5,
356                              const int id = 0,
357                              const bool force3d = true);
358   /*!
359    * Creates quadratic or linear pentahedron
360    */
361   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
362                              const SMDS_MeshNode* n2,
363                              const SMDS_MeshNode* n3,
364                              const SMDS_MeshNode* n4,
365                              const SMDS_MeshNode* n5,
366                              const SMDS_MeshNode* n6,
367                              const int id = 0, 
368                              const bool force3d = true);
369   /*!
370    * Creates bi-quadratic, quadratic or linear hexahedron
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 SMDS_MeshNode* n7,
379                              const SMDS_MeshNode* n8,
380                              const int id = 0, 
381                              bool force3d = true);
382
383   /*!
384    * Creates LINEAR!!!!!!!!! octahedron
385    */
386   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
387                              const SMDS_MeshNode* n2,
388                              const SMDS_MeshNode* n3,
389                              const SMDS_MeshNode* n4,
390                              const SMDS_MeshNode* n5,
391                              const SMDS_MeshNode* n6,
392                              const SMDS_MeshNode* n7,
393                              const SMDS_MeshNode* n8,
394                              const SMDS_MeshNode* n9,
395                              const SMDS_MeshNode* n10,
396                              const SMDS_MeshNode* n11,
397                              const SMDS_MeshNode* n12,
398                              const int id = 0, 
399                              bool force3d = true);
400
401   /*!
402    * Creates polyhedron. In quadratic mesh, adds medium nodes
403    */
404   SMDS_MeshVolume* AddPolyhedralVolume (const std::vector<const SMDS_MeshNode*>& nodes,
405                                         const std::vector<int>&                  quantities,
406                                         const int                                ID=0,
407                                         const bool                               force3d = true);
408   /*!
409    * \brief Enables fixing node parameters on EDGEs and FACEs by
410    * GetNodeU(...,check=true), GetNodeUV(...,check=true), CheckNodeUV() and
411    * CheckNodeU() in case if a node lies on a shape set via SetSubShape().
412    * Default is False
413    */
414   void ToFixNodeParameters(bool toFix);
415
416   /*!
417    * \brief Return U of the given node on the edge
418    */
419   double GetNodeU(const TopoDS_Edge&   theEdge,
420                   const SMDS_MeshNode* theNode,
421                   const SMDS_MeshNode* inEdgeNode=0,
422                   bool*                check=0);
423   /*!
424    * \brief Return node UV on face
425    *  \param inFaceNode - a node of element being created located inside a face
426    *  \param check - if provided, returns result of UV check that it enforces
427    */
428   gp_XY GetNodeUV(const TopoDS_Face&   F,
429                   const SMDS_MeshNode* n,
430                   const SMDS_MeshNode* inFaceNode=0,
431                   bool*                check=0) const;
432   /*!
433    * \brief Check and fix node UV on a face
434    *  \param force - check even if checks of other nodes on this face passed OK
435    *  \param distXYZ - returns result distance and point coordinates
436    *  \retval bool - false if UV is bad and could not be fixed
437    */
438   bool CheckNodeUV(const TopoDS_Face&   F,
439                    const SMDS_MeshNode* n,
440                    gp_XY&               uv,
441                    const double         tol,
442                    const bool           force=false,
443                    double               distXYZ[4]=0) const;
444   /*!
445    * \brief Check and fix node U on an edge
446    *  \param force - check even if checks of other nodes on this edge passed OK
447    *  \param distXYZ - returns result distance and point coordinates
448    *  \retval bool - false if U is bad and could not be fixed
449    */
450   bool CheckNodeU(const TopoDS_Edge&   E,
451                   const SMDS_MeshNode* n,
452                   double&              u,
453                   const double         tol,
454                   const bool           force=false,
455                   double               distXYZ[4]=0) const;
456   /*!
457    * \brief Return middle UV taking in account surface period
458    */
459   static gp_XY GetMiddleUV(const Handle(Geom_Surface)& surface,
460                            const gp_XY&                uv1,
461                            const gp_XY&                uv2);
462   /*!
463    * \brief Define a pointer to wrapper over a function of gp_XY class,
464    *       suitable to pass as xyFunPtr to applyIn2D().
465    *       For exaple gp_XY_FunPtr(Added) defines pointer gp_XY_Added to function
466    *       calling gp_XY::Added(gp_XY), which is to be used like following
467    *       applyIn2D(surf, uv1, uv2, gp_XY_Added)
468    */
469 #define gp_XY_FunPtr(meth) \
470   static gp_XY __gpXY_##meth (const gp_XY& uv1, const gp_XY& uv2) { return uv1.meth( uv2 ); } \
471   static xyFunPtr gp_XY_##meth = & __gpXY_##meth
472
473   /*!
474    * \brief Perform given operation on two 2d points in parameric space of given surface.
475    *        It takes into account period of the surface. Use gp_XY_FunPtr macro
476    *        to easily define pointer to function of gp_XY class.
477    */
478   static gp_XY applyIn2D(const Handle(Geom_Surface)& surface,
479                          const gp_XY&                uv1,
480                          const gp_XY&                uv2,
481                          xyFunPtr                    fun,
482                          const bool                  resultInPeriod=true);
483                           
484   /*!
485    * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
486     * \retval bool - return true if the face is periodic
487     *
488     * If F is Null, answer about subshape set through IsQuadraticSubMesh() or
489     * SetSubShape()
490    */
491   bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
492
493   /*!
494    * \brief Return projector intitialized by given face without location, which is returned
495    */
496   GeomAPI_ProjectPointOnSurf& GetProjector(const TopoDS_Face& F,
497                                            TopLoc_Location&   loc,
498                                            double             tol=0 ) const; 
499
500   /*!
501    * \brief Check if shape is a degenerated edge or it's vertex
502     * \param subShape - edge or vertex index in SMESHDS
503     * \retval bool - true if subShape is a degenerated shape
504     *
505     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
506    */
507   bool IsDegenShape(const int subShape) const
508   { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
509   /*!
510    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
511    *        has a degenerated edges
512     * \retval bool - true if it has
513    */
514   bool HasDegeneratedEdges() const { return !myDegenShapeIds.empty(); }
515
516   /*!
517    * \brief Check if shape is a seam edge or it's vertex
518     * \param subShape - edge or vertex index in SMESHDS
519     * \retval bool - true if subShape is a seam shape
520     *
521     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
522     * Seam shape has two 2D alternative represenations on the face
523    */
524   bool IsSeamShape(const int subShape) const
525   { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
526   /*!
527    * \brief Check if shape is a seam edge or it's vertex
528     * \param subShape - edge or vertex
529     * \retval bool - true if subShape is a seam shape
530     *
531     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
532     * Seam shape has two 2D alternative represenations on the face
533    */
534   bool IsSeamShape(const TopoDS_Shape& subShape) const
535   { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
536   /*!
537    * \brief Return true if an edge or a vertex encounters twice in face wire
538    *  \param subShape - Id of edge or vertex
539    */
540   bool IsRealSeam(const int subShape) const
541   { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
542   /*!
543    * \brief Return true if an edge or a vertex encounters twice in face wire
544    *  \param subShape - edge or vertex
545    */
546   bool IsRealSeam(const TopoDS_Shape& subShape) const
547   { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
548   /*!
549    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
550    *        has a seam edge
551     * \retval bool - true if it has
552    */
553   bool HasSeam() const { return !mySeamShapeIds.empty(); }
554   /*!
555    * \brief Return index of periodic parametric direction of a closed face
556     * \retval int - 1 for U, 2 for V direction
557    */
558   int GetPeriodicIndex() const { return myParIndex; }
559   /*!
560    * \brief Return an alternative parameter for a node on seam
561    */
562   double GetOtherParam(const double param) const;
563
564   /*!
565    * \brief Return existing or create new medium nodes between given ones
566    *  \param force3d - true means node creation at the middle between the
567    *                   two given nodes, else node position is found on its
568    *                   supporting geometrical shape, if any.
569    */
570   const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
571                                      const SMDS_MeshNode* n2,
572                                      const bool force3d);
573   /*!
574    * \brief Return existing or create a new central node for a quardilateral
575    *       quadratic face given its 8 nodes.
576    *  \param force3d - true means node creation in between the given nodes,
577    *                   else node position is found on a geometrical face if any.
578    */
579   const SMDS_MeshNode* GetCentralNode(const SMDS_MeshNode* n1,
580                                       const SMDS_MeshNode* n2,
581                                       const SMDS_MeshNode* n3,
582                                       const SMDS_MeshNode* n4,
583                                       const SMDS_MeshNode* n12,
584                                       const SMDS_MeshNode* n23,
585                                       const SMDS_MeshNode* n34,
586                                       const SMDS_MeshNode* n41,
587                                       bool                 force3d);
588   /*!
589    * \brief Return existing or create a new central node for a 
590    *       quadratic triangle given its 6 nodes.
591    *  \param force3d - true means node creation in between the given nodes,
592    *                   else node position is found on a geometrical face if any.
593    */
594   const SMDS_MeshNode* GetCentralNode(const SMDS_MeshNode* n1,
595                                       const SMDS_MeshNode* n2,
596                                       const SMDS_MeshNode* n3,
597                                       const SMDS_MeshNode* n12,
598                                       const SMDS_MeshNode* n23,
599                                       const SMDS_MeshNode* n31,
600                                       bool                 force3d);
601   /*!
602    * \brief Return index and type of the shape (EDGE or FACE only) to set a medium node on
603    */
604   std::pair<int, TopAbs_ShapeEnum> GetMediumPos(const SMDS_MeshNode* n1,
605                                                 const SMDS_MeshNode* n2,
606                                                 const bool           useCurSubShape=false);
607   /*!
608    * \brief Add a link in my data structure
609    */
610   void AddTLinkNode(const SMDS_MeshNode* n1,
611                     const SMDS_MeshNode* n2,
612                     const SMDS_MeshNode* n12);
613   /*!
614    * \brief Add many links in my data structure
615    */
616   void AddTLinkNodeMap(const TLinkNodeMap& aMap)
617     { myTLinkNodeMap.insert(aMap.begin(), aMap.end()); }
618
619   void AddTLinks(const SMDS_MeshEdge*   edge);
620   void AddTLinks(const SMDS_MeshFace*   face);
621   void AddTLinks(const SMDS_MeshVolume* vol);
622
623   /**
624    * Returns myTLinkNodeMap
625    */
626   const TLinkNodeMap& GetTLinkNodeMap() const { return myTLinkNodeMap; }
627
628   /**
629    * Check mesh without geometry for: if all elements on this shape are quadratic,
630    * quadratic elements will be created.
631    * Used then generated 3D mesh without geometry.
632    */
633   enum MType{ LINEAR, QUADRATIC, COMP };
634   MType IsQuadraticMesh();
635   
636   virtual ~SMESH_MesherHelper();
637
638  protected:
639
640   /*!
641    * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
642    *  \param uv1 - UV on the seam
643    *  \param uv2 - UV within a face
644    *  \retval gp_Pnt2d - selected UV
645    */
646   gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
647
648   const SMDS_MeshNode* getMediumNodeOnComposedWire(const SMDS_MeshNode* n1,
649                                                    const SMDS_MeshNode* n2,
650                                                    bool                 force3d);
651  private:
652
653   // Forbiden copy constructor
654   SMESH_MesherHelper (const SMESH_MesherHelper& theOther);
655
656   // key of a map of bi-quadratic face to it's central node
657   struct TBiQuad: public std::pair<int, std::pair<int, int> >
658   {
659     TBiQuad(const SMDS_MeshNode* n1,
660             const SMDS_MeshNode* n2, 
661             const SMDS_MeshNode* n3,
662             const SMDS_MeshNode* n4=0)
663     {
664       TIDSortedNodeSet s;
665       s.insert(n1);
666       s.insert(n2);
667       s.insert(n3);
668       if ( n4 ) s.insert(n4);
669       TIDSortedNodeSet::iterator n = s.begin();
670       first = (*n++)->GetID();
671       second.first = (*n++)->GetID();
672       second.second = (*n++)->GetID();
673     }
674   };
675
676   // maps used during creation of quadratic elements
677   TLinkNodeMap                              myTLinkNodeMap;       // medium nodes on links
678   std::map< TBiQuad, const SMDS_MeshNode* > myMapWithCentralNode; // central nodes of faces
679
680   std::set< int > myDegenShapeIds;
681   std::set< int > mySeamShapeIds;
682   double          myPar1[2], myPar2[2]; // U and V bounds of a closed periodic surface
683   int             myParIndex;     // bounds' index (1-U, 2-V, 3-both)
684
685   typedef std::map< int, GeomAPI_ProjectPointOnSurf* > TID2ProjectorOnSurf;
686   TID2ProjectorOnSurf myFace2Projector;
687   typedef std::map< int, GeomAPI_ProjectPointOnCurve* > TID2ProjectorOnCurve;
688   TID2ProjectorOnCurve myEdge2Projector;
689
690   TopoDS_Shape    myShape;
691   SMESH_Mesh*     myMesh;
692   int             myShapeID;
693
694   bool            myCreateQuadratic;
695   bool            myCreateBiQuadratic;
696   bool            mySetElemOnShape;
697   bool            myFixNodeParameters;
698
699   std::map< int,bool > myNodePosShapesValidity;
700   bool toCheckPosOnShape(int shapeID ) const;
701   void setPosOnShapeValidity(int shapeID, bool ok ) const;
702 };
703
704 //=======================================================================
705 inline gp_XY
706 SMESH_MesherHelper::calcTFI(double x, double y,
707                             const gp_XY a0,const gp_XY a1,const gp_XY a2,const gp_XY a3,
708                             const gp_XY p0,const gp_XY p1,const gp_XY p2,const gp_XY p3)
709 {
710   return
711     ((1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3 ) -
712     ((1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3);
713 }
714 //=======================================================================
715 inline gp_XYZ
716 SMESH_MesherHelper::calcTFI(double x, double y,
717                             const gp_XYZ a0,const gp_XYZ a1,const gp_XYZ a2,const gp_XYZ a3,
718                             const gp_XYZ p0,const gp_XYZ p1,const gp_XYZ p2,const gp_XYZ p3)
719 {
720   return
721     ((1 - y) * p0 + x * p1 + y * p2 + (1 - x) * p3 ) -
722     ((1 - x) * (1 - y) * a0 + x * (1 - y) * a1 + x * y * a2 + (1 - x) * y * a3);
723 }
724 //=======================================================================
725
726 #endif