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