Salome HOME
Merge from BR_V5_DEV 17Feb09
[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   /*!
110    * \brief Return number of unique ancestors of the shape
111    */
112   static int NbAncestors(const TopoDS_Shape& shape,
113                          const SMESH_Mesh&   mesh,
114                          TopAbs_ShapeEnum    ancestorType=TopAbs_SHAPE);
115
116 public:
117   // ---------- PUBLIC INSTANCE METHODS ----------
118
119   // constructor
120   SMESH_MesherHelper(SMESH_Mesh& theMesh);
121
122   SMESH_Mesh* GetMesh() const { return myMesh; }
123     
124   SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
125     
126   /*!
127    * Check submesh for given shape: if all elements on this shape are quadratic,
128    * quadratic elements will be created. Also fill myNLinkNodeMap
129    */
130   bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
131   /*!
132    * \brief Set order of elements to create without calling IsQuadraticSubMesh()
133    */
134   void SetIsQuadratic(const bool theBuildQuadratic)
135   { myCreateQuadratic = theBuildQuadratic; }
136   /*!
137    * \brief Return myCreateQuadratic flag
138    */
139   bool GetIsQuadratic() const { return myCreateQuadratic; }
140
141   /*!
142    * \brief To set created elements on the shape set by IsQuadraticSubMesh()
143    *        or the next methods. By defaul elements are set on the shape if
144    *        a mesh has no shape to be meshed
145    */
146   void SetElementsOnShape(bool toSet) { mySetElemOnShape = toSet; }
147
148   /*!
149    * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
150    */
151   void SetSubShape(const int           subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
152   void SetSubShape(const TopoDS_Shape& subShape);
153   /*!
154    * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape() 
155     * \retval int - shape index in SMESHDS
156    */
157   int GetSubShapeID() const { return myShapeID; }
158   /*!
159    * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape() 
160    */
161   TopoDS_Shape GetSubShape() const  { return myShape; }
162
163   /*!
164    * Creates a node
165    */
166   SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0);
167   /*!
168    * Creates quadratic or linear edge
169    */
170   SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
171                          const SMDS_MeshNode* n2,
172                          const int id = 0, 
173                          const bool force3d = true);
174   /*!
175    * Creates quadratic or linear triangle
176    */
177   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
178                          const SMDS_MeshNode* n2,
179                          const SMDS_MeshNode* n3,
180                          const int id=0, 
181                          const bool force3d = false);
182   /*!
183    * Creates quadratic or linear quadrangle
184    */
185   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
186                          const SMDS_MeshNode* n2,
187                          const SMDS_MeshNode* n3,
188                          const SMDS_MeshNode* n4,
189                          const int id = 0,
190                          const bool force3d = false);
191   /*!
192    * Creates quadratic or linear tetraahedron
193    */
194   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
195                              const SMDS_MeshNode* n2,
196                              const SMDS_MeshNode* n3,
197                              const SMDS_MeshNode* n4,
198                              const int id = 0,
199                              const bool force3d = true);
200   /*!
201    * Creates quadratic or linear pyramid
202    */
203   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
204                              const SMDS_MeshNode* n2,
205                              const SMDS_MeshNode* n3,
206                              const SMDS_MeshNode* n4,
207                              const SMDS_MeshNode* n5,
208                              const int id = 0,
209                              const bool force3d = true);
210   /*!
211    * Creates quadratic or linear pentahedron
212    */
213   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
214                              const SMDS_MeshNode* n2,
215                              const SMDS_MeshNode* n3,
216                              const SMDS_MeshNode* n4,
217                              const SMDS_MeshNode* n5,
218                              const SMDS_MeshNode* n6,
219                              const int id = 0, 
220                              const bool force3d = true);
221   /*!
222    * Creates quadratic or linear hexahedron
223    */
224   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
225                              const SMDS_MeshNode* n2,
226                              const SMDS_MeshNode* n3,
227                              const SMDS_MeshNode* n4,
228                              const SMDS_MeshNode* n5,
229                              const SMDS_MeshNode* n6,
230                              const SMDS_MeshNode* n7,
231                              const SMDS_MeshNode* n8,
232                              const int id = 0, 
233                              bool force3d = true);
234   /*!
235    * \brief Return U of the given node on the edge
236    */
237   double GetNodeU(const TopoDS_Edge&   theEdge,
238                   const SMDS_MeshNode* theNode);
239   /*!
240    * \brief Return node UV on face
241     * \param inFaceNode - a node of element being created located inside a face
242    */
243   gp_XY GetNodeUV(const TopoDS_Face&   F,
244                   const SMDS_MeshNode* n,
245                   const SMDS_MeshNode* inFaceNode=0) const;
246   /*!
247    * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
248     * \retval bool - return true if the face is periodic
249     *
250     * if F is Null, answer about subshape set through IsQuadraticSubMesh() or
251     * SetSubShape()
252    */
253   bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
254
255   /*!
256    * \brief Check if shape is a degenerated edge or it's vertex
257     * \param subShape - edge or vertex index in SMESHDS
258     * \retval bool - true if subShape is a degenerated shape
259     *
260     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
261    */
262   bool IsDegenShape(const int subShape) const
263   { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
264   /*!
265    * \brief Check if shape is a seam edge or it's vertex
266     * \param subShape - edge or vertex index in SMESHDS
267     * \retval bool - true if subShape is a seam shape
268     *
269     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
270     * Seam shape has two 2D alternative represenations on the face
271    */
272   bool IsSeamShape(const int subShape) const
273   { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
274   /*!
275    * \brief Check if shape is a seam edge or it's vertex
276     * \param subShape - edge or vertex
277     * \retval bool - true if subShape is a seam shape
278     *
279     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
280     * Seam shape has two 2D alternative represenations on the face
281    */
282   bool IsSeamShape(const TopoDS_Shape& subShape) const
283   { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
284   /*!
285    * \brief Return true if an edge or a vertex encounters twice in face wire
286    *  \param subShape - Id of edge or vertex
287    */
288   bool IsRealSeam(const int subShape) const
289   { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
290   /*!
291    * \brief Return true if an edge or a vertex encounters twice in face wire
292    *  \param subShape - edge or vertex
293    */
294   bool IsRealSeam(const TopoDS_Shape& subShape) const
295   { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
296   /*!
297    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
298    *        has a seam edge
299     * \retval bool - true if it has
300    */
301   bool HasSeam() const { return !mySeamShapeIds.empty(); }
302   /*!
303    * \brief Return index of periodic parametric direction of a closed face
304     * \retval int - 1 for U, 2 for V direction
305    */
306   int GetPeriodicIndex() const { return myParIndex; }
307   /*!
308    * \brief Return an alternative parameter for a node on seam
309    */
310   double GetOtherParam(const double param) const;
311
312   /**
313    * Special function for search or creation medium node
314    */
315   const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
316                                      const SMDS_MeshNode* n2,
317                                      const bool force3d);
318   /*!
319    * Auxilary function for filling myNLinkNodeMap
320    */
321   void AddNLinkNode(const SMDS_MeshNode* n1,
322                     const SMDS_MeshNode* n2,
323                     const SMDS_MeshNode* n12);
324   /**
325    * Auxilary function for filling myNLinkNodeMap
326    */
327   void AddNLinkNodeMap(const NLinkNodeMap& aMap)
328     { myNLinkNodeMap.insert(aMap.begin(), aMap.end()); }
329
330   /**
331    * Returns myNLinkNodeMap
332    */
333   const NLinkNodeMap& GetNLinkNodeMap() const { return myNLinkNodeMap; }
334
335   /**
336    * Check mesh without geometry for: if all elements on this shape are quadratic,
337    * quadratic elements will be created.
338    * Used then generated 3D mesh without geometry.
339    */
340   enum MType{ LINEAR, QUADRATIC, COMP };
341   MType IsQuadraticMesh();
342   
343 protected:
344
345   /*!
346    * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
347     * \param uv1 - UV on the seam
348     * \param uv2 - UV within a face
349     * \retval gp_Pnt2d - selected UV
350    */
351   gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
352
353  private:
354
355   // Forbiden copy constructor
356   SMESH_MesherHelper (const SMESH_MesherHelper& theOther) {};
357
358   // special map for using during creation of quadratic elements
359   NLinkNodeMap    myNLinkNodeMap;
360
361   std::set< int > myDegenShapeIds;
362   std::set< int > mySeamShapeIds;
363   double          myPar1, myPar2; // bounds of a closed periodic surface
364   int             myParIndex;     // bounds' index (1-U, 2-V)
365
366   TopoDS_Shape    myShape;
367   SMESH_Mesh*     myMesh;
368   int             myShapeID;
369
370   // to create quadratic elements
371   bool            myCreateQuadratic;
372   bool            mySetElemOnShape;
373
374 };
375
376
377 #endif