Salome HOME
Merge from V6_3_BR 06/06/2011
[modules/smesh.git] / src / SMESH / SMESH_MesherHelper.hxx
1 // Copyright (C) 2007-2011  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
62  *
63  * It allow meshers not to care about creation of medium nodes
64  * when filling a quadratic mesh. Helper does it itself.
65  * It defines degree of elements to create when IsQuadraticSubMesh()
66  * is called.
67  */
68 //=======================================================================
69
70 class SMESH_EXPORT SMESH_MesherHelper
71 {
72 public:
73   // ---------- PUBLIC UTILITIES ----------
74   
75   /*!
76    * \brief Returns true if given node is medium
77     * \param n - node to check
78     * \param typeToCheck - type of elements containing the node to ask about node status
79     * \retval bool - check result
80    */
81   static bool IsMedium(const SMDS_MeshNode*      node,
82                        const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
83
84   /*!
85    * \brief Load nodes bound to face into a map of node columns
86     * \param theParam2ColumnMap - map of node columns to fill
87     * \param theFace - the face on which nodes are searched for
88     * \param theBaseEdge - the edge nodes of which are columns' bases
89     * \param theMesh - the mesh containing nodes
90     * \retval bool - false if something is wrong
91    * 
92    * The key of the map is a normalized parameter of each
93    * base node on theBaseEdge.
94    * This method works in supposition that nodes on the face
95    * forms a rectangular grid and elements can be quardrangles or triangles
96    */
97   static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
98                               const TopoDS_Face& theFace,
99                               const TopoDS_Edge& theBaseEdge,
100                               SMESHDS_Mesh*      theMesh,
101                               SMESH_ProxyMesh*   theProxyMesh=0);
102   /*!
103    * \brief Return support shape of a node
104    * \param node - the node
105    * \param meshDS - mesh DS
106    * \retval TopoDS_Shape - found support shape
107    */
108   static TopoDS_Shape GetSubShapeByNode(const SMDS_MeshNode* node,
109                                         const SMESHDS_Mesh*  meshDS);
110
111   /*!
112    * \brief Return a valid node index, fixing the given one if necessary
113     * \param ind - node index
114     * \param nbNodes - total nb of nodes
115     * \retval int - valid node index
116    */
117   static int WrapIndex(const int ind, const int nbNodes) {
118     if ( ind < 0 ) return nbNodes + ind % nbNodes;
119     if ( ind >= nbNodes ) return ind % nbNodes;
120     return ind;
121   }
122
123   /*!
124    * \brief Return number of unique ancestors of the shape
125    */
126   static int NbAncestors(const TopoDS_Shape& shape,
127                          const SMESH_Mesh&   mesh,
128                          TopAbs_ShapeEnum    ancestorType=TopAbs_SHAPE);
129   /*!
130    * \brief Return iterator on ancestors of the given type
131    */
132   static PShapeIteratorPtr GetAncestors(const TopoDS_Shape& shape,
133                                         const SMESH_Mesh&   mesh,
134                                         TopAbs_ShapeEnum    ancestorType);
135
136   /*!
137    * \brief Return orientation of sub-shape in the main shape
138    */
139   static TopAbs_Orientation GetSubShapeOri(const TopoDS_Shape& shape,
140                                            const TopoDS_Shape& subShape);
141
142   static bool IsSubShape( const TopoDS_Shape& shape, const TopoDS_Shape& mainShape );
143
144   static bool IsSubShape( const TopoDS_Shape& shape, SMESH_Mesh* aMesh );
145
146   static double MaxTolerance( const TopoDS_Shape& shape );
147
148   static bool IsClosedEdge( const TopoDS_Edge& anEdge );
149
150   static TopoDS_Vertex IthVertex( const bool is2nd, TopoDS_Edge anEdge, const bool CumOri=true );
151
152
153 public:
154   // ---------- PUBLIC INSTANCE METHODS ----------
155
156   // constructor
157   SMESH_MesherHelper(SMESH_Mesh& theMesh);
158
159   SMESH_Mesh* GetMesh() const { return myMesh; }
160     
161   SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
162     
163   /*!
164    * Check submesh for given shape: if all elements on this shape are quadratic,
165    * quadratic elements will be created. Also fill myTLinkNodeMap
166    */
167   bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
168   /*!
169    * \brief Set order of elements to create without calling IsQuadraticSubMesh()
170    */
171   void SetIsQuadratic(const bool theBuildQuadratic)
172   { myCreateQuadratic = theBuildQuadratic; }
173   /*!
174    * \brief Return myCreateQuadratic flag
175    */
176   bool GetIsQuadratic() const { return myCreateQuadratic; }
177
178   /*!
179    * \brief Move medium nodes of faces and volumes to fix distorted elements
180    * \param volumeOnly - fix nodes on geom faces or not if the shape is solid
181    */
182   void FixQuadraticElements(bool volumeOnly=true);
183
184   /*!
185    * \brief To set created elements on the shape set by IsQuadraticSubMesh()
186    *        or the next methods. By defaul elements are set on the shape if
187    *        a mesh has no shape to be meshed
188    */
189   void SetElementsOnShape(bool toSet) { mySetElemOnShape = toSet; }
190
191   /*!
192    * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
193    */
194   void SetSubShape(const int           subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
195   void SetSubShape(const TopoDS_Shape& subShape);
196   /*!
197    * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape() 
198     * \retval int - shape index in SMESHDS
199    */
200   int GetSubShapeID() const { return myShapeID; }
201   /*!
202    * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape() 
203    */
204   const TopoDS_Shape& GetSubShape() const  { return myShape; }
205
206   /*!
207    * Creates a node
208    */
209   SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0);
210   /*!
211    * Creates quadratic or linear edge
212    */
213   SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
214                          const SMDS_MeshNode* n2,
215                          const int id = 0, 
216                          const bool force3d = true);
217   /*!
218    * Creates quadratic or linear triangle
219    */
220   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
221                          const SMDS_MeshNode* n2,
222                          const SMDS_MeshNode* n3,
223                          const int id=0, 
224                          const bool force3d = false);
225   /*!
226    * Creates quadratic or linear quadrangle
227    */
228   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
229                          const SMDS_MeshNode* n2,
230                          const SMDS_MeshNode* n3,
231                          const SMDS_MeshNode* n4,
232                          const int id = 0,
233                          const bool force3d = false);
234
235   /*!
236    * Creates polygon, with additional nodes in quadratic mesh
237    */
238   SMDS_MeshFace* AddPolygonalFace (const std::vector<const SMDS_MeshNode*>& nodes,
239                                    const int id = 0,
240                                    const bool force3d = false);
241   /*!
242    * Creates quadratic or linear tetrahedron
243    */
244   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
245                              const SMDS_MeshNode* n2,
246                              const SMDS_MeshNode* n3,
247                              const SMDS_MeshNode* n4,
248                              const int id = 0,
249                              const bool force3d = true);
250   /*!
251    * Creates quadratic or linear pyramid
252    */
253   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
254                              const SMDS_MeshNode* n2,
255                              const SMDS_MeshNode* n3,
256                              const SMDS_MeshNode* n4,
257                              const SMDS_MeshNode* n5,
258                              const int id = 0,
259                              const bool force3d = true);
260   /*!
261    * Creates quadratic or linear pentahedron
262    */
263   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
264                              const SMDS_MeshNode* n2,
265                              const SMDS_MeshNode* n3,
266                              const SMDS_MeshNode* n4,
267                              const SMDS_MeshNode* n5,
268                              const SMDS_MeshNode* n6,
269                              const int id = 0, 
270                              const bool force3d = true);
271   /*!
272    * Creates quadratic or linear hexahedron
273    */
274   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
275                              const SMDS_MeshNode* n2,
276                              const SMDS_MeshNode* n3,
277                              const SMDS_MeshNode* n4,
278                              const SMDS_MeshNode* n5,
279                              const SMDS_MeshNode* n6,
280                              const SMDS_MeshNode* n7,
281                              const SMDS_MeshNode* n8,
282                              const int id = 0, 
283                              bool force3d = true);
284
285   /*!
286    * Creates polyhedron. In quadratic mesh, adds medium nodes
287    */
288   SMDS_MeshVolume* AddPolyhedralVolume (const std::vector<const SMDS_MeshNode*>& nodes,
289                                         const std::vector<int>&                  quantities,
290                                         const int                                ID=0,
291                                         const bool                               force3d = true);
292   /*!
293    * \brief Return U of the given node on the edge
294    */
295   double GetNodeU(const TopoDS_Edge&   theEdge,
296                   const SMDS_MeshNode* theNode,
297                   const SMDS_MeshNode* inEdgeNode=0,
298                   bool*                check=0);
299   /*!
300    * \brief Return node UV on face
301    *  \param inFaceNode - a node of element being created located inside a face
302    */
303   gp_XY GetNodeUV(const TopoDS_Face&   F,
304                   const SMDS_MeshNode* n,
305                   const SMDS_MeshNode* inFaceNode=0,
306                   bool*                check=0) const;
307   /*!
308    * \brief Check and fix node UV on a face
309    *  \param force - check even if checks of other nodes on this face passed OK
310    *  \param distXYZ - returns result distance and point coordinates
311    *  \retval bool - false if UV is bad and could not be fixed
312    */
313   bool CheckNodeUV(const TopoDS_Face&   F,
314                    const SMDS_MeshNode* n,
315                    gp_XY&               uv,
316                    const double         tol,
317                    const bool           force=false,
318                    double               distXYZ[4]=0) const;
319   /*!
320    * \brief Check and fix node U on an edge
321    *  \param force - check even if checks of other nodes on this edge passed OK
322    *  \param distXYZ - returns result distance and point coordinates
323    *  \retval bool - false if U is bad and could not be fixed
324    */
325   bool CheckNodeU(const TopoDS_Edge&   E,
326                   const SMDS_MeshNode* n,
327                   double&              u,
328                   const double         tol,
329                   const bool           force=false,
330                   double               distXYZ[4]=0) const;
331   /*!
332    * \brief Return middle UV taking in account surface period
333    */
334   static gp_XY GetMiddleUV(const Handle(Geom_Surface)& surface,
335                            const gp_XY&                uv1,
336                            const gp_XY&                uv2);
337   /*!
338    * \brief Define a pointer to wrapper over a function of gp_XY class,
339    *       suitable to pass as xyFunPtr to applyIn2D().
340    *       For exaple gp_XY_FunPtr(Added) defines pointer gp_XY_Added to function
341    *       calling gp_XY::Added(gp_XY), which is to be used like following
342    *       applyIn2D(surf, uv1, uv2, gp_XY_Added)
343    */
344 #define gp_XY_FunPtr(meth) \
345   static gp_XY __gpXY_##meth (const gp_XY& uv1, const gp_XY& uv2) { return uv1.meth( uv2 ); } \
346   static xyFunPtr gp_XY_##meth = & __gpXY_##meth
347
348   /*!
349    * \brief Perform given operation on two 2d points in parameric space of given surface.
350    *        It takes into account period of the surface. Use gp_XY_FunPtr macro
351    *        to easily define pointer to function of gp_XY class.
352    */
353   static gp_XY applyIn2D(const Handle(Geom_Surface)& surface,
354                          const gp_XY&                uv1,
355                          const gp_XY&                uv2,
356                          xyFunPtr                    fun,
357                          const bool                  resultInPeriod=true);
358                           
359   /*!
360    * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
361     * \retval bool - return true if the face is periodic
362     *
363     * If F is Null, answer about subshape set through IsQuadraticSubMesh() or
364     * SetSubShape()
365    */
366   bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
367
368   /*!
369    * \brief Return projector intitialized by given face without location, which is returned
370    */
371   GeomAPI_ProjectPointOnSurf& GetProjector(const TopoDS_Face& F,
372                                            TopLoc_Location&   loc,
373                                            double             tol=0 ) const; 
374
375   /*!
376    * \brief Check if shape is a degenerated edge or it's vertex
377     * \param subShape - edge or vertex index in SMESHDS
378     * \retval bool - true if subShape is a degenerated shape
379     *
380     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
381    */
382   bool IsDegenShape(const int subShape) const
383   { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
384   /*!
385    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
386    *        has a degenerated edges
387     * \retval bool - true if it has
388    */
389   bool HasDegeneratedEdges() const { return !myDegenShapeIds.empty(); }
390
391   /*!
392    * \brief Check if shape is a seam edge or it's vertex
393     * \param subShape - edge or vertex index in SMESHDS
394     * \retval bool - true if subShape is a seam shape
395     *
396     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
397     * Seam shape has two 2D alternative represenations on the face
398    */
399   bool IsSeamShape(const int subShape) const
400   { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
401   /*!
402    * \brief Check if shape is a seam edge or it's vertex
403     * \param subShape - edge or vertex
404     * \retval bool - true if subShape is a seam shape
405     *
406     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
407     * Seam shape has two 2D alternative represenations on the face
408    */
409   bool IsSeamShape(const TopoDS_Shape& subShape) const
410   { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
411   /*!
412    * \brief Return true if an edge or a vertex encounters twice in face wire
413    *  \param subShape - Id of edge or vertex
414    */
415   bool IsRealSeam(const int subShape) const
416   { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
417   /*!
418    * \brief Return true if an edge or a vertex encounters twice in face wire
419    *  \param subShape - edge or vertex
420    */
421   bool IsRealSeam(const TopoDS_Shape& subShape) const
422   { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
423   /*!
424    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
425    *        has a seam edge
426     * \retval bool - true if it has
427    */
428   bool HasSeam() const { return !mySeamShapeIds.empty(); }
429   /*!
430    * \brief Return index of periodic parametric direction of a closed face
431     * \retval int - 1 for U, 2 for V direction
432    */
433   int GetPeriodicIndex() const { return myParIndex; }
434   /*!
435    * \brief Return an alternative parameter for a node on seam
436    */
437   double GetOtherParam(const double param) const;
438
439   /*!
440    * \brief Return existing or create new medium nodes between given ones
441    *  \param force3d - true means node creation at the middle between the
442    *                   two given nodes, else node position is found on its
443    *                   supporting geometrical shape, if any.
444    */
445   const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
446                                      const SMDS_MeshNode* n2,
447                                      const bool force3d);
448   /*!
449    * \brief Add a link in my data structure
450    */
451   void AddTLinkNode(const SMDS_MeshNode* n1,
452                     const SMDS_MeshNode* n2,
453                     const SMDS_MeshNode* n12);
454   /*!
455    * \brief Add many links in my data structure
456    */
457   void AddTLinkNodeMap(const TLinkNodeMap& aMap)
458     { myTLinkNodeMap.insert(aMap.begin(), aMap.end()); }
459
460   void AddTLinks(const SMDS_MeshEdge*   edge);
461   void AddTLinks(const SMDS_MeshFace*   face);
462   void AddTLinks(const SMDS_MeshVolume* vol);
463
464   /**
465    * Returns myTLinkNodeMap
466    */
467   const TLinkNodeMap& GetTLinkNodeMap() const { return myTLinkNodeMap; }
468
469   /**
470    * Check mesh without geometry for: if all elements on this shape are quadratic,
471    * quadratic elements will be created.
472    * Used then generated 3D mesh without geometry.
473    */
474   enum MType{ LINEAR, QUADRATIC, COMP };
475   MType IsQuadraticMesh();
476   
477   virtual ~SMESH_MesherHelper();
478
479 protected:
480
481   /*!
482    * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
483     * \param uv1 - UV on the seam
484     * \param uv2 - UV within a face
485     * \retval gp_Pnt2d - selected UV
486    */
487   gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
488
489   const SMDS_MeshNode* getMediumNodeOnComposedWire(const SMDS_MeshNode* n1,
490                                                    const SMDS_MeshNode* n2,
491                                                    bool                 force3d);
492  private:
493
494   // Forbiden copy constructor
495   SMESH_MesherHelper (const SMESH_MesherHelper& theOther) {};
496
497   // special map for using during creation of quadratic elements
498   TLinkNodeMap    myTLinkNodeMap;
499
500   std::set< int > myDegenShapeIds;
501   std::set< int > mySeamShapeIds;
502   double          myPar1[2], myPar2[2]; // U and V bounds of a closed periodic surface
503   int             myParIndex;     // bounds' index (1-U, 2-V, 3-both)
504
505   typedef std::map< int, GeomAPI_ProjectPointOnSurf* > TID2ProjectorOnSurf;
506   TID2ProjectorOnSurf myFace2Projector;
507   typedef std::map< int, GeomAPI_ProjectPointOnCurve* > TID2ProjectorOnCurve;
508   TID2ProjectorOnCurve myEdge2Projector;
509
510   TopoDS_Shape    myShape;
511   SMESH_Mesh*     myMesh;
512   int             myShapeID;
513
514   // to create quadratic elements
515   bool            myCreateQuadratic;
516   bool            mySetElemOnShape;
517
518   std::map< int,bool > myNodePosShapesValidity;
519   bool toCheckPosOnShape(int shapeID ) const;
520   void setPosOnShapeValidity(int shapeID, bool ok ) const;
521
522 };
523
524
525 #endif