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