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