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