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