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