Salome HOME
Merging with WPdev
[modules/smesh.git] / src / SMESH / SMESH_MesherHelper.hxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File:      SMESH_MesherHelper.hxx
21 // Created:   15.02.06 14:48:09
22 // Author:    Sergey KUUL
23
24
25 #ifndef SMESH_MesherHelper_HeaderFile
26 #define SMESH_MesherHelper_HeaderFile
27
28 #include "SMESH_SMESH.hxx"
29
30 #include <SMESH_Mesh.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <SMDS_MeshNode.hxx>
33 #include <TopoDS_Face.hxx>
34 #include <gp_Pnt2d.hxx>
35 #include <SMDS_QuadraticEdge.hxx>
36
37 #include <map>
38
39 typedef pair<const SMDS_MeshNode*, const SMDS_MeshNode*> NLink;
40 typedef map<NLink, const SMDS_MeshNode*> NLinkNodeMap;
41 typedef map<NLink, const SMDS_MeshNode*>::iterator ItNLinkNode;
42
43 /*!
44  * \brief It helps meshers to add elements
45  *
46  * It allow meshers not to care about creation of medium nodes
47  * when filling a quadratic mesh. Helper does it itself.
48  * It defines degree of elements to create when IsQuadraticSubMesh()
49  * is called.
50  */
51
52 typedef std::vector<const SMDS_MeshNode* > TNodeColumn;
53 typedef std::map< double, TNodeColumn > TParam2ColumnMap;
54
55 class SMESH_EXPORT SMESH_MesherHelper
56 {
57 public:
58   // ---------- PUBLIC UTILITIES ----------
59   
60   /*!
61    * \brief Load nodes bound to face into a map of node columns
62     * \param theParam2ColumnMap - map of node columns to fill
63     * \param theFace - the face on which nodes are searched for
64     * \param theBaseEdge - the edge nodes of which are columns' bases
65     * \param theMesh - the mesh containing nodes
66     * \retval bool - false if something is wrong
67    * 
68    * The key of the map is a normalized parameter of each
69    * base node on theBaseEdge.
70    * This method works in supposition that nodes on the face
71    * forms a rectangular grid and elements can be quardrangles or triangles
72    */
73   static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
74                               const TopoDS_Face& theFace,
75                               const TopoDS_Edge& theBaseEdge,
76                               SMESHDS_Mesh*      theMesh);
77   /*!
78    * \brief Return support shape of a node
79    * \param node - the node
80    * \param meshDS - mesh DS
81    * \retval TopoDS_Shape - found support shape
82    */
83   static const TopoDS_Shape& GetSubShapeByNode(const SMDS_MeshNode* node,
84                                                SMESHDS_Mesh*        meshDS)
85   { return meshDS->IndexToShape( node->GetPosition()->GetShapeId() ); }
86
87   /*!
88    * \brief Return a valid node index, fixing the given one if necessary
89     * \param ind - node index
90     * \param nbNodes - total nb of nodes
91     * \retval int - valid node index
92    */
93   static int WrapIndex(const int ind, const int nbNodes) {
94     if ( ind < 0 ) return nbNodes + ind % nbNodes;
95     if ( ind >= nbNodes ) return ind % nbNodes;
96     return ind;
97   }
98
99 public:
100   // ---------- PUBLIC METHODS ----------
101
102   /// Empty constructor
103   SMESH_MesherHelper(SMESH_Mesh& theMesh)
104     { myMesh=(void *)&theMesh; myCreateQuadratic = false; myShapeID=-1;}
105
106   SMESH_Mesh* GetMesh() const { return (SMESH_Mesh*)myMesh; }
107     
108   SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
109     
110   /// Copy constructor
111   //Standard_EXPORT SMESH_MesherHelper (const SMESH_MesherHelper& theOther);
112
113   /// Destructor
114   //Standard_EXPORT virtual ~SMESH_MesherHelper ();
115
116   /**
117    * Check submesh for given shape
118    * Check if all elements on this shape
119    * are quadratic, if yes => set true to myCreateQuadratic 
120    * (default value is false). Also fill myNLinkNodeMap
121    * Returns myCreateQuadratic
122    */
123   bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
124
125   /*!
126    * \brief Returns true if given node is medium
127     * \param n - node to check
128     * \param typeToCheck - type of elements containing the node to ask about node status
129     * \retval bool - check result
130    */
131   static bool IsMedium(const SMDS_MeshNode*      node,
132                        const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
133
134   /**
135    * Auxilary function for filling myNLinkNodeMap
136    */
137   void AddNLinkNode(const SMDS_MeshNode* n1,
138                     const SMDS_MeshNode* n2,
139                     const SMDS_MeshNode* n12);
140
141   /**
142    * Auxilary function for filling myNLinkNodeMap
143    */
144   void AddNLinkNodeMap(const NLinkNodeMap& aMap)
145     { myNLinkNodeMap.insert(aMap.begin(), aMap.end()); }
146
147   /**
148    * Returns myNLinkNodeMap
149    */
150   const NLinkNodeMap& GetNLinkNodeMap() { return myNLinkNodeMap; }
151
152   /*!
153    * \brief Return node UV on face
154     * \param F - the face
155     * \param n - the node
156     * \param inFaceNode - a node of element being created located inside a face
157     * \retval gp_XY - resulting UV
158    * 
159    * Auxilary function called form GetMediumNode()
160    */
161   gp_XY GetNodeUV(const TopoDS_Face&   F,
162                   const SMDS_MeshNode* n,
163                   const SMDS_MeshNode* inFaceNode=0) const;
164
165   /*!
166    * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
167     * \param F - the face
168     * \retval bool - return true if the face is periodic
169     *
170     * if F is Null, answer about subshape set through IsQuadraticSubMesh() or
171     * SetSubShape()
172    */
173   bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
174
175   /*!
176    * \brief Return  U on edge
177     * \param F - the edge
178     * \param n - the node
179     * \retval double - resulting U
180    * 
181    * Auxilary function called from GetMediumNode()
182    */
183   double GetNodeU(const TopoDS_Edge&  E,
184                   const SMDS_MeshNode* n);
185
186
187   /**
188    * Special function for search or creation medium node
189    */
190   const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
191                                      const SMDS_MeshNode* n2,
192                                      const bool force3d);
193
194   /**
195    * Special function for creation quadratic edge
196    */
197   SMDS_QuadraticEdge* AddQuadraticEdge(const SMDS_MeshNode* n1,
198                                        const SMDS_MeshNode* n2,
199                                        const int id = 0, 
200                                        const bool force3d = true);
201
202   /**
203    * Special function for creation quadratic triangle
204    */
205   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
206                          const SMDS_MeshNode* n2,
207                          const SMDS_MeshNode* n3,
208                          const int id=0, 
209                          const bool force3d = false);
210
211   /**
212    * Special function for creation quadratic quadrangle
213    */
214   SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
215                          const SMDS_MeshNode* n2,
216                          const SMDS_MeshNode* n3,
217                          const SMDS_MeshNode* n4,
218                          const int id = 0,
219                          const bool force3d = false);
220
221   /**
222    * Special function for creation quadratic tetraahedron
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 int id = 0,
229                              const bool force3d = true);
230
231
232   /**
233    * Special function for creation quadratic 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   /**
244    * Special function for creation quadratic pentahedron
245    */
246   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
247                              const SMDS_MeshNode* n2,
248                              const SMDS_MeshNode* n3,
249                              const SMDS_MeshNode* n4,
250                              const SMDS_MeshNode* n5,
251                              const SMDS_MeshNode* n6,
252                              const int id = 0, 
253                              const bool force3d = true);
254
255   /**
256    * Special function for creation quadratic hexahedron
257    */
258   SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
259                              const SMDS_MeshNode* n2,
260                              const SMDS_MeshNode* n3,
261                              const SMDS_MeshNode* n4,
262                              const SMDS_MeshNode* n5,
263                              const SMDS_MeshNode* n6,
264                              const SMDS_MeshNode* n7,
265                              const SMDS_MeshNode* n8,
266                              const int id = 0, 
267                              bool force3d = true);
268
269   
270   /*!
271    * \brief Set order of elements to create
272     * \param theBuildQuadratic - to build quadratic or not
273    * 
274    * To be used for quadratic elements creation without preceding
275    * IsQuadraticSubMesh() or AddQuadraticEdge() call
276    */
277   void SetKeyIsQuadratic(const bool theBuildQuadratic)
278   { myCreateQuadratic = theBuildQuadratic; }
279
280   /*!
281    * \brief Return myCreateQuadratic flag
282     * \retval bool - myCreateQuadratic value
283    */
284   bool GetIsQuadratic() const { return myCreateQuadratic; }
285
286   /*!
287    * \brief Set shape to make elements on
288     * \param subShape, subShapeID - shape or its ID (==SMESHDS_Mesh::ShapeToIndex(shape))
289    */
290   void SetSubShape(const int           subShapeID);
291   void SetSubShape(const TopoDS_Shape& subShape);
292
293   /*!
294    * \brief Return shape or its ID, on which created elements are added
295     * \retval int - shape index in SMESHDS
296     *
297     * Shape is set by calling either IsQuadraticSubMesh() or SetSubShape() 
298    */
299   int GetSubShapeID() const { return myShapeID; }
300   /*!
301    * \brief Return shape or its ID, on which created elements are added
302     * \retval TopoDS_Shape - shape
303     *
304     * Shape is set by calling either IsQuadraticSubMesh() or SetSubShape() 
305    */
306   TopoDS_Shape GetSubShape() const  { return myShape; }
307
308   /*!
309    * \brief Check if shape is a seam edge or it's vertex
310     * \param subShape - edge or vertex index in SMESHDS
311     * \retval bool - true if subShape is a seam shape
312     *
313     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
314    */
315   bool IsSeamShape(const int subShape) const
316   { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
317   /*!
318    * \brief Check if shape is a seam edge or it's vertex
319     * \param subShape - edge or vertex
320     * \retval bool - true if subShape is a seam shape
321     *
322     * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
323    */
324   bool IsSeamShape(const TopoDS_Shape& subShape) const
325   { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
326
327   /*!
328    * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
329    *        has a seam edge
330     * \retval bool - true if it has
331    */
332   bool HasSeam() const { return !mySeamShapeIds.empty(); }
333
334   /*!
335    * \brief Return index of periodic parametric direction of a closed face
336     * \retval int - 1 for U, 2 for V direction
337    */
338   int GetPeriodicIndex() const { return myParIndex; }
339
340  protected:
341
342   /*!
343    * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
344     * \param uv1 - UV on the seam
345     * \param uv2 - UV within a face
346     * \retval gp_Pnt2d - selected UV
347    */
348   gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
349
350  private:
351
352   void* myMesh;
353
354   int myShapeID;
355
356   // Key for creation quadratic faces
357   bool myCreateQuadratic;
358
359   // special map for using during creation quadratic faces
360   NLinkNodeMap myNLinkNodeMap;
361
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   TopoDS_Shape    myShape;
366
367 };
368
369
370 #endif