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 <Geom_Surface.hxx>
36 #include <TopoDS_Face.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <gp_Pnt2d.hxx>
42 typedef std::map<SMESH_TLink, const SMDS_MeshNode*> TLinkNodeMap;
43 typedef std::map<SMESH_TLink, const SMDS_MeshNode*>::iterator ItTLinkNode;
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 TopoDS_Shape GetSubShapeByNode(const SMDS_MeshNode* node,
95 SMESHDS_Mesh* meshDS);
98 * \brief Return a valid node index, fixing the given one if necessary
99 * \param ind - node index
100 * \param nbNodes - total nb of nodes
101 * \retval int - valid node index
103 static int WrapIndex(const int ind, const int nbNodes) {
104 if ( ind < 0 ) return nbNodes + ind % nbNodes;
105 if ( ind >= nbNodes ) return ind % nbNodes;
110 * \brief Return number of unique ancestors of the shape
112 static int NbAncestors(const TopoDS_Shape& shape,
113 const SMESH_Mesh& mesh,
114 TopAbs_ShapeEnum ancestorType=TopAbs_SHAPE);
117 * \brief Return orientation of sub-shape in the main shape
119 static TopAbs_Orientation GetSubShapeOri(const TopoDS_Shape& shape,
120 const TopoDS_Shape& subShape);
123 // ---------- PUBLIC INSTANCE METHODS ----------
126 SMESH_MesherHelper(SMESH_Mesh& theMesh);
128 SMESH_Mesh* GetMesh() const { return myMesh; }
130 SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
133 * Check submesh for given shape: if all elements on this shape are quadratic,
134 * quadratic elements will be created. Also fill myTLinkNodeMap
136 bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
138 * \brief Set order of elements to create without calling IsQuadraticSubMesh()
140 void SetIsQuadratic(const bool theBuildQuadratic)
141 { myCreateQuadratic = theBuildQuadratic; }
143 * \brief Return myCreateQuadratic flag
145 bool GetIsQuadratic() const { return myCreateQuadratic; }
148 * \brief Move medium nodes of faces and volumes to fix distorted elements
149 * \param volumeOnly - fix nodes on geom faces or not if the shape is solid
151 void FixQuadraticElements(bool volumeOnly=true);
154 * \brief To set created elements on the shape set by IsQuadraticSubMesh()
155 * or the next methods. By defaul elements are set on the shape if
156 * a mesh has no shape to be meshed
158 void SetElementsOnShape(bool toSet) { mySetElemOnShape = toSet; }
161 * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
163 void SetSubShape(const int subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
164 void SetSubShape(const TopoDS_Shape& subShape);
166 * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape()
167 * \retval int - shape index in SMESHDS
169 int GetSubShapeID() const { return myShapeID; }
171 * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape()
173 TopoDS_Shape GetSubShape() const { return myShape; }
178 SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0);
180 * Creates quadratic or linear edge
182 SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
183 const SMDS_MeshNode* n2,
185 const bool force3d = true);
187 * Creates quadratic or linear triangle
189 SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
190 const SMDS_MeshNode* n2,
191 const SMDS_MeshNode* n3,
193 const bool force3d = false);
195 * Creates quadratic or linear quadrangle
197 SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
198 const SMDS_MeshNode* n2,
199 const SMDS_MeshNode* n3,
200 const SMDS_MeshNode* n4,
202 const bool force3d = false);
204 * Creates quadratic or linear tetraahedron
206 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
207 const SMDS_MeshNode* n2,
208 const SMDS_MeshNode* n3,
209 const SMDS_MeshNode* n4,
211 const bool force3d = true);
213 * Creates quadratic or linear pyramid
215 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
216 const SMDS_MeshNode* n2,
217 const SMDS_MeshNode* n3,
218 const SMDS_MeshNode* n4,
219 const SMDS_MeshNode* n5,
221 const bool force3d = true);
223 * Creates quadratic or linear pentahedron
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,
232 const bool force3d = true);
234 * Creates quadratic or linear hexahedron
236 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
237 const SMDS_MeshNode* n2,
238 const SMDS_MeshNode* n3,
239 const SMDS_MeshNode* n4,
240 const SMDS_MeshNode* n5,
241 const SMDS_MeshNode* n6,
242 const SMDS_MeshNode* n7,
243 const SMDS_MeshNode* n8,
245 bool force3d = true);
247 * \brief Return U of the given node on the edge
249 double GetNodeU(const TopoDS_Edge& theEdge,
250 const SMDS_MeshNode* theNode,
253 * \brief Return node UV on face
254 * \param inFaceNode - a node of element being created located inside a face
256 gp_XY GetNodeUV(const TopoDS_Face& F,
257 const SMDS_MeshNode* n,
258 const SMDS_MeshNode* inFaceNode=0,
259 bool* check=0) const;
261 * \brief Check and fix node UV on a face
262 * \param force - check even if checks of other nodes on this face passed OK
263 * \retval bool - false if UV is bad and could not be fixed
265 bool CheckNodeUV(const TopoDS_Face& F,
266 const SMDS_MeshNode* n,
269 const bool force=false) const;
271 * \brief Check and fix node U on an edge
272 * \param force - check even if checks of other nodes on this edge passed OK
273 * \retval bool - false if U is bad and could not be fixed
275 bool CheckNodeU(const TopoDS_Edge& E,
276 const SMDS_MeshNode* n,
279 const bool force=false) const;
281 * \brief Return middle UV taking in account surface period
283 static gp_XY GetMiddleUV(const Handle(Geom_Surface)& surface,
287 * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
288 * \retval bool - return true if the face is periodic
290 * If F is Null, answer about subshape set through IsQuadraticSubMesh() or
293 bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
296 * \brief Check if shape is a degenerated edge or it's vertex
297 * \param subShape - edge or vertex index in SMESHDS
298 * \retval bool - true if subShape is a degenerated shape
300 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
302 bool IsDegenShape(const int subShape) const
303 { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
305 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
306 * has a degenerated edges
307 * \retval bool - true if it has
309 bool HasDegeneratedEdges() const { return !myDegenShapeIds.empty(); }
312 * \brief Check if shape is a seam edge or it's vertex
313 * \param subShape - edge or vertex index in SMESHDS
314 * \retval bool - true if subShape is a seam shape
316 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
317 * Seam shape has two 2D alternative represenations on the face
319 bool IsSeamShape(const int subShape) const
320 { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
322 * \brief Check if shape is a seam edge or it's vertex
323 * \param subShape - edge or vertex
324 * \retval bool - true if subShape is a seam shape
326 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
327 * Seam shape has two 2D alternative represenations on the face
329 bool IsSeamShape(const TopoDS_Shape& subShape) const
330 { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
332 * \brief Return true if an edge or a vertex encounters twice in face wire
333 * \param subShape - Id of edge or vertex
335 bool IsRealSeam(const int subShape) const
336 { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
338 * \brief Return true if an edge or a vertex encounters twice in face wire
339 * \param subShape - edge or vertex
341 bool IsRealSeam(const TopoDS_Shape& subShape) const
342 { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
344 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
346 * \retval bool - true if it has
348 bool HasSeam() const { return !mySeamShapeIds.empty(); }
350 * \brief Return index of periodic parametric direction of a closed face
351 * \retval int - 1 for U, 2 for V direction
353 int GetPeriodicIndex() const { return myParIndex; }
355 * \brief Return an alternative parameter for a node on seam
357 double GetOtherParam(const double param) const;
360 * \brief Return existing or create new medium nodes between given ones
361 * \param force3d - true means node creation at the middle between the
362 * two given nodes, else node position is found on its
363 * supporting geometrical shape, if any.
365 const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
366 const SMDS_MeshNode* n2,
369 * \brief Add a link in my data structure
371 void AddTLinkNode(const SMDS_MeshNode* n1,
372 const SMDS_MeshNode* n2,
373 const SMDS_MeshNode* n12);
375 * \brief Add many links in my data structure
377 void AddTLinkNodeMap(const TLinkNodeMap& aMap)
378 { myTLinkNodeMap.insert(aMap.begin(), aMap.end()); }
381 * Returns myTLinkNodeMap
383 const TLinkNodeMap& GetTLinkNodeMap() const { return myTLinkNodeMap; }
386 * Check mesh without geometry for: if all elements on this shape are quadratic,
387 * quadratic elements will be created.
388 * Used then generated 3D mesh without geometry.
390 enum MType{ LINEAR, QUADRATIC, COMP };
391 MType IsQuadraticMesh();
396 * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
397 * \param uv1 - UV on the seam
398 * \param uv2 - UV within a face
399 * \retval gp_Pnt2d - selected UV
401 gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
405 // Forbiden copy constructor
406 SMESH_MesherHelper (const SMESH_MesherHelper& theOther) {};
408 // special map for using during creation of quadratic elements
409 TLinkNodeMap myTLinkNodeMap;
411 std::set< int > myDegenShapeIds;
412 std::set< int > mySeamShapeIds;
413 double myPar1[2], myPar2[2]; // U and V bounds of a closed periodic surface
414 int myParIndex; // bounds' index (1-U, 2-V, 3-both)
416 TopoDS_Shape myShape;
420 // to create quadratic elements
421 bool myCreateQuadratic;
422 bool mySetElemOnShape;
423 std::set< int > myOkNodePosShapes;