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