Salome HOME
+#include "SMESH_MeshEditor.hxx" // needed for many meshers
[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 <TopoDS_Face.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <gp_Pnt2d.hxx>
38
39 #include <map>
40
41 typedef std::pair<const SMDS_MeshNode*, const SMDS_MeshNode*> NLink;
42 typedef std::map<NLink, const SMDS_MeshNode*>                 NLinkNodeMap;
43 typedef std::map<NLink, const SMDS_MeshNode*>::iterator       ItNLinkNode;
44
45 /*!
46  * \brief It helps meshers to add elements
47  *
48  * It allow meshers not to care about creation of medium nodes
49  * when filling a quadratic mesh. Helper does it itself.
50  * It defines degree of elements to create when IsQuadraticSubMesh()
51  * is called.
52  */
53
54 typedef std::vector<const SMDS_MeshNode* > TNodeColumn;
55 typedef std::map< double, TNodeColumn > TParam2ColumnMap;
56
57 class SMESH_EXPORT SMESH_MesherHelper
58 {
59 public:
60   // ---------- PUBLIC UTILITIES ----------
61   
62   /*!
63    * \brief Returns true if given node is medium
64     * \param n - node to check
65     * \param typeToCheck - type of elements containing the node to ask about node status
66     * \retval bool - check result
67    */
68   static bool IsMedium(const SMDS_MeshNode*      node,
69                        const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
70
71   /*!
72    * \brief Load nodes bound to face into a map of node columns
73     * \param theParam2ColumnMap - map of node columns to fill
74     * \param theFace - the face on which nodes are searched for
75     * \param theBaseEdge - the edge nodes of which are columns' bases
76     * \param theMesh - the mesh containing nodes
77     * \retval bool - false if something is wrong
78    * 
79    * The key of the map is a normalized parameter of each
80    * base node on theBaseEdge.
81    * This method works in supposition that nodes on the face
82    * forms a rectangular grid and elements can be quardrangles or triangles
83    */
84   static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
85                               const TopoDS_Face& theFace,
86                               const TopoDS_Edge& theBaseEdge,
87                               SMESHDS_Mesh*      theMesh);
88   /*!
89    * \brief Return support shape of a node
90    * \param node - the node
91    * \param meshDS - mesh DS
92    * \retval TopoDS_Shape - found support shape
93    */
94   static const TopoDS_Shape& GetSubShapeByNode(const SMDS_MeshNode* node,
95                                                SMESHDS_Mesh*        meshDS)
96   { return meshDS->IndexToShape( node->GetPosition()->GetShapeId() ); }
97
98   /*!
99    * \brief Return a valid node index, fixing the given one if necessary
100     * \param ind - node index
101     * \param nbNodes - total nb of nodes
102     * \retval int - valid node index
103    */
104   static int WrapIndex(const int ind, const int nbNodes) {
105     if ( ind < 0 ) return nbNodes + ind % nbNodes;
106     if ( ind >= nbNodes ) return ind % nbNodes;
107     return ind;
108   }
109
110   /*!
111    * \brief Return number of unique ancestors of the shape
112    */
113   static int NbAncestors(const TopoDS_Shape& shape,
114                          const SMESH_Mesh&   mesh,
115                          TopAbs_ShapeEnum    ancestorType=TopAbs_SHAPE);
116
117 public:
118   // ---------- PUBLIC INSTANCE METHODS ----------
119
120   // constructor
121   SMESH_MesherHelper(SMESH_Mesh& theMesh);
122
123   SMESH_Mesh* GetMesh() const { return myMesh; }
124     
125   SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
126     
127   /*!
128    * Check submesh for given shape: if all elements on this shape are quadratic,
129    * quadratic elements will be created. Also fill myNLinkNodeMap
130    */
131   bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
132   /*!
133    * \brief Set order of elements to create without calling IsQuadraticSubMesh()
134    */
135   void SetIsQuadratic(const bool theBuildQuadratic)
136   { myCreateQuadratic = theBuildQuadratic; }
137   /*!
138    * \brief Return myCreateQuadratic flag
139    */
140   bool GetIsQuadratic() const { return myCreateQuadratic; }
141
142   /*!
143    * \brief To set created elements on the shape set by IsQuadraticSubMesh()
144    *        or the next methods. By defaul elements are set on the shape if
145    *        a mesh has no shape to be meshed
146    */
147   void SetElementsOnShape(bool toSet) { mySetElemOnShape = toSet; }
148
149   /*!
150    * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
151    */
152   void SetSubShape(const int           subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
153   void SetSubShape(const TopoDS_Shape& subShape);
154   /*!
155    * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape() 
156     * \retval int - shape index in SMESHDS
157    */
158   int GetSubShapeID() const { return myShapeID; }
159   /*!
160    * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape() 
161    */
162   TopoDS_Shape GetSubShape() const  { return myShape; }
163
164   /*!
165    * Creates a node
166    */
167   SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0);
168   /*!
169    * Creates quadratic or linear edge
170    */
171   SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
172                          const SMDS_MeshNode* n2,
173                          const int id = 0, 
174                          const bool force3d = true);
175   /*!
176    * Creates quadratic or linear triangle
177    */
178   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
179                          const SMDS_MeshNode* n2,
180                          const SMDS_MeshNode* n3,
181                          const int id=0, 
182                          const bool force3d = false);
183   /*!
184    * Creates quadratic or linear quadrangle
185    */
186   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
187                          const SMDS_MeshNode* n2,
188                          const SMDS_MeshNode* n3,
189                          const SMDS_MeshNode* n4,
190                          const int id = 0,
191                          const bool force3d = false);
192   /*!
193    * Creates quadratic or linear tetraahedron
194    */
195   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
196                              const SMDS_MeshNode* n2,
197                              const SMDS_MeshNode* n3,
198                              const SMDS_MeshNode* n4,
199                              const int id = 0,
200                              const bool force3d = true);
201   /*!
202    * Creates quadratic or linear pyramid
203    */
204   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
205                              const SMDS_MeshNode* n2,
206                              const SMDS_MeshNode* n3,
207                              const SMDS_MeshNode* n4,
208                              const SMDS_MeshNode* n5,
209                              const int id = 0,
210                              const bool force3d = true);
211   /*!
212    * Creates quadratic or linear pentahedron
213    */
214   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
215                              const SMDS_MeshNode* n2,
216                              const SMDS_MeshNode* n3,
217                              const SMDS_MeshNode* n4,
218                              const SMDS_MeshNode* n5,
219                              const SMDS_MeshNode* n6,
220                              const int id = 0, 
221                              const bool force3d = true);
222   /*!
223    * Creates quadratic or linear hexahedron
224    */
225   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
226                              const SMDS_MeshNode* n2,
227                              const SMDS_MeshNode* n3,
228                              const SMDS_MeshNode* n4,
229                              const SMDS_MeshNode* n5,
230                              const SMDS_MeshNode* n6,
231                              const SMDS_MeshNode* n7,
232                              const SMDS_MeshNode* n8,
233                              const int id = 0, 
234                              bool force3d = true);
235   /*!
236    * \brief Return U of the given node on the edge
237    */
238   double GetNodeU(const TopoDS_Edge&   theEdge,
239                   const SMDS_MeshNode* theNode);
240   /*!
241    * \brief Return node UV on face
242     * \param inFaceNode - a node of element being created located inside a face
243    */
244   gp_XY GetNodeUV(const TopoDS_Face&   F,
245                   const SMDS_MeshNode* n,
246                   const SMDS_MeshNode* inFaceNode=0) const;
247   /*!
248    * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
249     * \retval bool - return true if the face is periodic
250     *
251     * if F is Null, answer about subshape set through IsQuadraticSubMesh() or
252     * SetSubShape()
253    */
254   bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
255
256   /*!
257    * \brief Check if shape is a degenerated edge or it's vertex
258     * \param subShape - edge or vertex index in SMESHDS
259     * \retval bool - true if subShape is a degenerated shape
260     *
261     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
262    */
263   bool IsDegenShape(const int subShape) const
264   { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
265   /*!
266    * \brief Check if shape is a seam edge or it's vertex
267     * \param subShape - edge or vertex index in SMESHDS
268     * \retval bool - true if subShape is a seam shape
269     *
270     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
271     * Seam shape has two 2D alternative represenations on the face
272    */
273   bool IsSeamShape(const int subShape) const
274   { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
275   /*!
276    * \brief Check if shape is a seam edge or it's vertex
277     * \param subShape - edge or vertex
278     * \retval bool - true if subShape is a seam shape
279     *
280     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
281     * Seam shape has two 2D alternative represenations on the face
282    */
283   bool IsSeamShape(const TopoDS_Shape& subShape) const
284   { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
285   /*!
286    * \brief Return true if an edge or a vertex encounters twice in face wire
287    *  \param subShape - Id of edge or vertex
288    */
289   bool IsRealSeam(const int subShape) const
290   { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
291   /*!
292    * \brief Return true if an edge or a vertex encounters twice in face wire
293    *  \param subShape - edge or vertex
294    */
295   bool IsRealSeam(const TopoDS_Shape& subShape) const
296   { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
297   /*!
298    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
299    *        has a seam edge
300     * \retval bool - true if it has
301    */
302   bool HasSeam() const { return !mySeamShapeIds.empty(); }
303   /*!
304    * \brief Return index of periodic parametric direction of a closed face
305     * \retval int - 1 for U, 2 for V direction
306    */
307   int GetPeriodicIndex() const { return myParIndex; }
308   /*!
309    * \brief Return an alternative parameter for a node on seam
310    */
311   double GetOtherParam(const double param) const;
312
313   /**
314    * Special function for search or creation medium node
315    */
316   const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
317                                      const SMDS_MeshNode* n2,
318                                      const bool force3d);
319   /*!
320    * Auxilary function for filling myNLinkNodeMap
321    */
322   void AddNLinkNode(const SMDS_MeshNode* n1,
323                     const SMDS_MeshNode* n2,
324                     const SMDS_MeshNode* n12);
325   /**
326    * Auxilary function for filling myNLinkNodeMap
327    */
328   void AddNLinkNodeMap(const NLinkNodeMap& aMap)
329     { myNLinkNodeMap.insert(aMap.begin(), aMap.end()); }
330
331   /**
332    * Returns myNLinkNodeMap
333    */
334   const NLinkNodeMap& GetNLinkNodeMap() const { return myNLinkNodeMap; }
335
336   /**
337    * Check mesh without geometry for: if all elements on this shape are quadratic,
338    * quadratic elements will be created.
339    * Used then generated 3D mesh without geometry.
340    */
341   enum MType{ LINEAR, QUADRATIC, COMP };
342   MType IsQuadraticMesh();
343   
344 protected:
345
346   /*!
347    * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
348     * \param uv1 - UV on the seam
349     * \param uv2 - UV within a face
350     * \retval gp_Pnt2d - selected UV
351    */
352   gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
353
354  private:
355
356   // Forbiden copy constructor
357   SMESH_MesherHelper (const SMESH_MesherHelper& theOther) {};
358
359   // special map for using during creation of quadratic elements
360   NLinkNodeMap    myNLinkNodeMap;
361
362   std::set< int > myDegenShapeIds;
363   std::set< int > mySeamShapeIds;
364   double          myPar1, myPar2; // bounds of a closed periodic surface
365   int             myParIndex;     // bounds' index (1-U, 2-V)
366
367   TopoDS_Shape    myShape;
368   SMESH_Mesh*     myMesh;
369   int             myShapeID;
370
371   // to create quadratic elements
372   bool            myCreateQuadratic;
373   bool            mySetElemOnShape;
374
375 };
376
377
378 #endif