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