1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: SMESH_MesherHelper.hxx
23 // Created: 15.02.06 14:48:09
24 // Author: Sergey KUUL
26 #ifndef SMESH_MesherHelper_HeaderFile
27 #define SMESH_MesherHelper_HeaderFile
29 #include "SMESH_SMESH.hxx"
31 #include "SMESH_MeshEditor.hxx" // needed for many meshers
32 #include <SMDS_MeshNode.hxx>
33 #include <SMDS_QuadraticEdge.hxx>
35 #include <TopoDS_Face.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <gp_Pnt2d.hxx>
41 typedef std::pair<const SMDS_MeshNode*, const SMDS_MeshNode*> NLink;
42 typedef std::map<NLink, const SMDS_MeshNode*> NLinkNodeMap;
43 typedef std::map<NLink, const SMDS_MeshNode*>::iterator ItNLinkNode;
46 * \brief It helps meshers to add elements
48 * It allow meshers not to care about creation of medium nodes
49 * when filling a quadratic mesh. Helper does it itself.
50 * It defines degree of elements to create when IsQuadraticSubMesh()
54 typedef std::vector<const SMDS_MeshNode* > TNodeColumn;
55 typedef std::map< double, TNodeColumn > TParam2ColumnMap;
57 class SMESH_EXPORT SMESH_MesherHelper
60 // ---------- PUBLIC UTILITIES ----------
63 * \brief Returns true if given node is medium
64 * \param n - node to check
65 * \param typeToCheck - type of elements containing the node to ask about node status
66 * \retval bool - check result
68 static bool IsMedium(const SMDS_MeshNode* node,
69 const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
72 * \brief Load nodes bound to face into a map of node columns
73 * \param theParam2ColumnMap - map of node columns to fill
74 * \param theFace - the face on which nodes are searched for
75 * \param theBaseEdge - the edge nodes of which are columns' bases
76 * \param theMesh - the mesh containing nodes
77 * \retval bool - false if something is wrong
79 * The key of the map is a normalized parameter of each
80 * base node on theBaseEdge.
81 * This method works in supposition that nodes on the face
82 * forms a rectangular grid and elements can be quardrangles or triangles
84 static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
85 const TopoDS_Face& theFace,
86 const TopoDS_Edge& theBaseEdge,
87 SMESHDS_Mesh* theMesh);
89 * \brief Return support shape of a node
90 * \param node - the node
91 * \param meshDS - mesh DS
92 * \retval TopoDS_Shape - found support shape
94 static const TopoDS_Shape& GetSubShapeByNode(const SMDS_MeshNode* node,
96 { return meshDS->IndexToShape( node->GetPosition()->GetShapeId() ); }
99 * \brief Return a valid node index, fixing the given one if necessary
100 * \param ind - node index
101 * \param nbNodes - total nb of nodes
102 * \retval int - valid node index
104 static int WrapIndex(const int ind, const int nbNodes) {
105 if ( ind < 0 ) return nbNodes + ind % nbNodes;
106 if ( ind >= nbNodes ) return ind % nbNodes;
111 * \brief Return number of unique ancestors of the shape
113 static int NbAncestors(const TopoDS_Shape& shape,
114 const SMESH_Mesh& mesh,
115 TopAbs_ShapeEnum ancestorType=TopAbs_SHAPE);
118 // ---------- PUBLIC INSTANCE METHODS ----------
121 SMESH_MesherHelper(SMESH_Mesh& theMesh);
123 SMESH_Mesh* GetMesh() const { return myMesh; }
125 SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
128 * Check submesh for given shape: if all elements on this shape are quadratic,
129 * quadratic elements will be created. Also fill myNLinkNodeMap
131 bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
133 * \brief Set order of elements to create without calling IsQuadraticSubMesh()
135 void SetIsQuadratic(const bool theBuildQuadratic)
136 { myCreateQuadratic = theBuildQuadratic; }
138 * \brief Return myCreateQuadratic flag
140 bool GetIsQuadratic() const { return myCreateQuadratic; }
143 * \brief To set created elements on the shape set by IsQuadraticSubMesh()
144 * or the next methods. By defaul elements are set on the shape if
145 * a mesh has no shape to be meshed
147 void SetElementsOnShape(bool toSet) { mySetElemOnShape = toSet; }
150 * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
152 void SetSubShape(const int subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
153 void SetSubShape(const TopoDS_Shape& subShape);
155 * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape()
156 * \retval int - shape index in SMESHDS
158 int GetSubShapeID() const { return myShapeID; }
160 * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape()
162 TopoDS_Shape GetSubShape() const { return myShape; }
167 SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0);
169 * Creates quadratic or linear edge
171 SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
172 const SMDS_MeshNode* n2,
174 const bool force3d = true);
176 * Creates quadratic or linear triangle
178 SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
179 const SMDS_MeshNode* n2,
180 const SMDS_MeshNode* n3,
182 const bool force3d = false);
184 * Creates quadratic or linear quadrangle
186 SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
187 const SMDS_MeshNode* n2,
188 const SMDS_MeshNode* n3,
189 const SMDS_MeshNode* n4,
191 const bool force3d = false);
193 * Creates quadratic or linear tetraahedron
195 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
196 const SMDS_MeshNode* n2,
197 const SMDS_MeshNode* n3,
198 const SMDS_MeshNode* n4,
200 const bool force3d = true);
202 * Creates quadratic or linear pyramid
204 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
205 const SMDS_MeshNode* n2,
206 const SMDS_MeshNode* n3,
207 const SMDS_MeshNode* n4,
208 const SMDS_MeshNode* n5,
210 const bool force3d = true);
212 * Creates quadratic or linear pentahedron
214 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
215 const SMDS_MeshNode* n2,
216 const SMDS_MeshNode* n3,
217 const SMDS_MeshNode* n4,
218 const SMDS_MeshNode* n5,
219 const SMDS_MeshNode* n6,
221 const bool force3d = true);
223 * Creates quadratic or linear hexahedron
225 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
226 const SMDS_MeshNode* n2,
227 const SMDS_MeshNode* n3,
228 const SMDS_MeshNode* n4,
229 const SMDS_MeshNode* n5,
230 const SMDS_MeshNode* n6,
231 const SMDS_MeshNode* n7,
232 const SMDS_MeshNode* n8,
234 bool force3d = true);
236 * \brief Return U of the given node on the edge
238 double GetNodeU(const TopoDS_Edge& theEdge,
239 const SMDS_MeshNode* theNode);
241 * \brief Return node UV on face
242 * \param inFaceNode - a node of element being created located inside a face
244 gp_XY GetNodeUV(const TopoDS_Face& F,
245 const SMDS_MeshNode* n,
246 const SMDS_MeshNode* inFaceNode=0) const;
248 * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
249 * \retval bool - return true if the face is periodic
251 * if F is Null, answer about subshape set through IsQuadraticSubMesh() or
254 bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
257 * \brief Check if shape is a degenerated edge or it's vertex
258 * \param subShape - edge or vertex index in SMESHDS
259 * \retval bool - true if subShape is a degenerated shape
261 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
263 bool IsDegenShape(const int subShape) const
264 { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
266 * \brief Check if shape is a seam edge or it's vertex
267 * \param subShape - edge or vertex index in SMESHDS
268 * \retval bool - true if subShape is a seam shape
270 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
271 * Seam shape has two 2D alternative represenations on the face
273 bool IsSeamShape(const int subShape) const
274 { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
276 * \brief Check if shape is a seam edge or it's vertex
277 * \param subShape - edge or vertex
278 * \retval bool - true if subShape is a seam shape
280 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
281 * Seam shape has two 2D alternative represenations on the face
283 bool IsSeamShape(const TopoDS_Shape& subShape) const
284 { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
286 * \brief Return true if an edge or a vertex encounters twice in face wire
287 * \param subShape - Id of edge or vertex
289 bool IsRealSeam(const int subShape) const
290 { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
292 * \brief Return true if an edge or a vertex encounters twice in face wire
293 * \param subShape - edge or vertex
295 bool IsRealSeam(const TopoDS_Shape& subShape) const
296 { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
298 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
300 * \retval bool - true if it has
302 bool HasSeam() const { return !mySeamShapeIds.empty(); }
304 * \brief Return index of periodic parametric direction of a closed face
305 * \retval int - 1 for U, 2 for V direction
307 int GetPeriodicIndex() const { return myParIndex; }
309 * \brief Return an alternative parameter for a node on seam
311 double GetOtherParam(const double param) const;
314 * Special function for search or creation medium node
316 const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
317 const SMDS_MeshNode* n2,
320 * Auxilary function for filling myNLinkNodeMap
322 void AddNLinkNode(const SMDS_MeshNode* n1,
323 const SMDS_MeshNode* n2,
324 const SMDS_MeshNode* n12);
326 * Auxilary function for filling myNLinkNodeMap
328 void AddNLinkNodeMap(const NLinkNodeMap& aMap)
329 { myNLinkNodeMap.insert(aMap.begin(), aMap.end()); }
332 * Returns myNLinkNodeMap
334 const NLinkNodeMap& GetNLinkNodeMap() const { return myNLinkNodeMap; }
337 * Check mesh without geometry for: if all elements on this shape are quadratic,
338 * quadratic elements will be created.
339 * Used then generated 3D mesh without geometry.
341 enum MType{ LINEAR, QUADRATIC, COMP };
342 MType IsQuadraticMesh();
347 * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
348 * \param uv1 - UV on the seam
349 * \param uv2 - UV within a face
350 * \retval gp_Pnt2d - selected UV
352 gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
356 // Forbiden copy constructor
357 SMESH_MesherHelper (const SMESH_MesherHelper& theOther) {};
359 // special map for using during creation of quadratic elements
360 NLinkNodeMap myNLinkNodeMap;
362 std::set< int > myDegenShapeIds;
363 std::set< int > mySeamShapeIds;
364 double myPar1, myPar2; // bounds of a closed periodic surface
365 int myParIndex; // bounds' index (1-U, 2-V)
367 TopoDS_Shape myShape;
371 // to create quadratic elements
372 bool myCreateQuadratic;
373 bool mySetElemOnShape;