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