1 // Copyright (C) 2007-2010 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
23 // File: SMESH_MesherHelper.hxx
24 // Created: 15.02.06 14:48:09
25 // Author: Sergey KUUL
27 #ifndef SMESH_MesherHelper_HeaderFile
28 #define SMESH_MesherHelper_HeaderFile
30 #include "SMESH_SMESH.hxx"
32 #include "SMESH_MeshEditor.hxx" // needed for many meshers
33 #include <SMDS_MeshNode.hxx>
34 #include <SMDS_QuadraticEdge.hxx>
36 #include <Geom_Surface.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Shape.hxx>
39 #include <gp_Pnt2d.hxx>
44 class GeomAPI_ProjectPointOnSurf;
45 class GeomAPI_ProjectPointOnCurve;
46 class SMESH_ProxyMesh;
48 typedef std::map<SMESH_TLink, const SMDS_MeshNode*> TLinkNodeMap;
49 typedef std::map<SMESH_TLink, const SMDS_MeshNode*>::iterator ItTLinkNode;
51 typedef SMDS_Iterator<const TopoDS_Shape*> PShapeIterator;
52 typedef boost::shared_ptr< PShapeIterator > PShapeIteratorPtr;
54 typedef std::vector<const SMDS_MeshNode* > TNodeColumn;
55 typedef std::map< double, TNodeColumn > TParam2ColumnMap;
57 typedef gp_XY (*xyFunPtr)(const gp_XY& uv1, const gp_XY& uv2);
59 //=======================================================================
61 * \brief It helps meshers to add elements
63 * It allow meshers not to care about creation of medium nodes
64 * when filling a quadratic mesh. Helper does it itself.
65 * It defines degree of elements to create when IsQuadraticSubMesh()
68 //=======================================================================
70 class SMESH_EXPORT SMESH_MesherHelper
73 // ---------- PUBLIC UTILITIES ----------
76 * \brief Returns true if given node is medium
77 * \param n - node to check
78 * \param typeToCheck - type of elements containing the node to ask about node status
79 * \retval bool - check result
81 static bool IsMedium(const SMDS_MeshNode* node,
82 const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
85 * \brief Load nodes bound to face into a map of node columns
86 * \param theParam2ColumnMap - map of node columns to fill
87 * \param theFace - the face on which nodes are searched for
88 * \param theBaseEdge - the edge nodes of which are columns' bases
89 * \param theMesh - the mesh containing nodes
90 * \retval bool - false if something is wrong
92 * The key of the map is a normalized parameter of each
93 * base node on theBaseEdge.
94 * This method works in supposition that nodes on the face
95 * forms a rectangular grid and elements can be quardrangles or triangles
97 static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
98 const TopoDS_Face& theFace,
99 const TopoDS_Edge& theBaseEdge,
100 SMESHDS_Mesh* theMesh,
101 SMESH_ProxyMesh* theProxyMesh=0);
103 * \brief Return support shape of a node
104 * \param node - the node
105 * \param meshDS - mesh DS
106 * \retval TopoDS_Shape - found support shape
108 static TopoDS_Shape GetSubShapeByNode(const SMDS_MeshNode* node,
109 const SMESHDS_Mesh* meshDS);
112 * \brief Return a valid node index, fixing the given one if necessary
113 * \param ind - node index
114 * \param nbNodes - total nb of nodes
115 * \retval int - valid node index
117 static int WrapIndex(const int ind, const int nbNodes) {
118 if ( ind < 0 ) return nbNodes + ind % nbNodes;
119 if ( ind >= nbNodes ) return ind % nbNodes;
124 * \brief Return number of unique ancestors of the shape
126 static int NbAncestors(const TopoDS_Shape& shape,
127 const SMESH_Mesh& mesh,
128 TopAbs_ShapeEnum ancestorType=TopAbs_SHAPE);
130 * \brief Return iterator on ancestors of the given type
132 static PShapeIteratorPtr GetAncestors(const TopoDS_Shape& shape,
133 const SMESH_Mesh& mesh,
134 TopAbs_ShapeEnum ancestorType);
137 * \brief Return orientation of sub-shape in the main shape
139 static TopAbs_Orientation GetSubShapeOri(const TopoDS_Shape& shape,
140 const TopoDS_Shape& subShape);
142 static bool IsSubShape( const TopoDS_Shape& shape, const TopoDS_Shape& mainShape );
144 static bool IsSubShape( const TopoDS_Shape& shape, SMESH_Mesh* aMesh );
146 static double MaxTolerance( const TopoDS_Shape& shape );
148 static bool IsClosedEdge( const TopoDS_Edge& anEdge );
152 // ---------- PUBLIC INSTANCE METHODS ----------
155 SMESH_MesherHelper(SMESH_Mesh& theMesh);
157 SMESH_Mesh* GetMesh() const { return myMesh; }
159 SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
162 * Check submesh for given shape: if all elements on this shape are quadratic,
163 * quadratic elements will be created. Also fill myTLinkNodeMap
165 bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
167 * \brief Set order of elements to create without calling IsQuadraticSubMesh()
169 void SetIsQuadratic(const bool theBuildQuadratic)
170 { myCreateQuadratic = theBuildQuadratic; }
172 * \brief Return myCreateQuadratic flag
174 bool GetIsQuadratic() const { return myCreateQuadratic; }
177 * \brief Move medium nodes of faces and volumes to fix distorted elements
178 * \param volumeOnly - fix nodes on geom faces or not if the shape is solid
180 void FixQuadraticElements(bool volumeOnly=true);
183 * \brief To set created elements on the shape set by IsQuadraticSubMesh()
184 * or the next methods. By defaul elements are set on the shape if
185 * a mesh has no shape to be meshed
187 void SetElementsOnShape(bool toSet) { mySetElemOnShape = toSet; }
190 * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
192 void SetSubShape(const int subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
193 void SetSubShape(const TopoDS_Shape& subShape);
195 * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape()
196 * \retval int - shape index in SMESHDS
198 int GetSubShapeID() const { return myShapeID; }
200 * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape()
202 const TopoDS_Shape& GetSubShape() const { return myShape; }
207 SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0);
209 * Creates quadratic or linear edge
211 SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
212 const SMDS_MeshNode* n2,
214 const bool force3d = true);
216 * Creates quadratic or linear triangle
218 SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
219 const SMDS_MeshNode* n2,
220 const SMDS_MeshNode* n3,
222 const bool force3d = false);
224 * Creates quadratic or linear quadrangle
226 SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
227 const SMDS_MeshNode* n2,
228 const SMDS_MeshNode* n3,
229 const SMDS_MeshNode* n4,
231 const bool force3d = false);
234 * Creates polygon, with additional nodes in quadratic mesh
236 SMDS_MeshFace* AddPolygonalFace (const std::vector<const SMDS_MeshNode*>& nodes,
238 const bool force3d = false);
240 * Creates quadratic or linear tetrahedron
242 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
243 const SMDS_MeshNode* n2,
244 const SMDS_MeshNode* n3,
245 const SMDS_MeshNode* n4,
247 const bool force3d = true);
249 * Creates quadratic or linear pyramid
251 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
252 const SMDS_MeshNode* n2,
253 const SMDS_MeshNode* n3,
254 const SMDS_MeshNode* n4,
255 const SMDS_MeshNode* n5,
257 const bool force3d = true);
259 * Creates quadratic or linear pentahedron
261 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
262 const SMDS_MeshNode* n2,
263 const SMDS_MeshNode* n3,
264 const SMDS_MeshNode* n4,
265 const SMDS_MeshNode* n5,
266 const SMDS_MeshNode* n6,
268 const bool force3d = true);
270 * Creates quadratic or linear hexahedron
272 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
273 const SMDS_MeshNode* n2,
274 const SMDS_MeshNode* n3,
275 const SMDS_MeshNode* n4,
276 const SMDS_MeshNode* n5,
277 const SMDS_MeshNode* n6,
278 const SMDS_MeshNode* n7,
279 const SMDS_MeshNode* n8,
281 bool force3d = true);
284 * Creates polyhedron. In quadratic mesh, adds medium nodes
286 SMDS_MeshVolume* AddPolyhedralVolume (const std::vector<const SMDS_MeshNode*>& nodes,
287 const std::vector<int>& quantities,
289 const bool force3d = true);
291 * \brief Return U of the given node on the edge
293 double GetNodeU(const TopoDS_Edge& theEdge,
294 const SMDS_MeshNode* theNode,
295 const SMDS_MeshNode* inEdgeNode=0,
298 * \brief Return node UV on face
299 * \param inFaceNode - a node of element being created located inside a face
301 gp_XY GetNodeUV(const TopoDS_Face& F,
302 const SMDS_MeshNode* n,
303 const SMDS_MeshNode* inFaceNode=0,
304 bool* check=0) const;
306 * \brief Check and fix node UV on a face
307 * \param force - check even if checks of other nodes on this face passed OK
308 * \param distXYZ - returns result distance and point coordinates
309 * \retval bool - false if UV is bad and could not be fixed
311 bool CheckNodeUV(const TopoDS_Face& F,
312 const SMDS_MeshNode* n,
315 const bool force=false,
316 double distXYZ[4]=0) const;
318 * \brief Check and fix node U on an edge
319 * \param force - check even if checks of other nodes on this edge passed OK
320 * \param distXYZ - returns result distance and point coordinates
321 * \retval bool - false if U is bad and could not be fixed
323 bool CheckNodeU(const TopoDS_Edge& E,
324 const SMDS_MeshNode* n,
327 const bool force=false,
328 double distXYZ[4]=0) const;
330 * \brief Return middle UV taking in account surface period
332 static gp_XY GetMiddleUV(const Handle(Geom_Surface)& surface,
336 * \brief Define a pointer to wrapper over a function of gp_XY class,
337 * suitable to pass as xyFunPtr to applyIn2D().
338 * For exaple gp_XY_FunPtr(Added) defines pointer gp_XY_Added to function
339 * calling gp_XY::Added(gp_XY), which is to be used like following
340 * applyIn2D(surf, uv1, uv2, gp_XY_Added)
342 #define gp_XY_FunPtr(meth) \
343 static gp_XY __gpXY_##meth (const gp_XY& uv1, const gp_XY& uv2) { return uv1.meth( uv2 ); } \
344 static xyFunPtr gp_XY_##meth = & __gpXY_##meth
347 * \brief Perform given operation on two 2d points in parameric space of given surface.
348 * It takes into account period of the surface. Use gp_XY_FunPtr macro
349 * to easily define pointer to function of gp_XY class.
351 static gp_XY applyIn2D(const Handle(Geom_Surface)& surface,
355 const bool resultInPeriod=true);
358 * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
359 * \retval bool - return true if the face is periodic
361 * If F is Null, answer about subshape set through IsQuadraticSubMesh() or
364 bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
367 * \brief Return projector intitialized by given face without location, which is returned
369 GeomAPI_ProjectPointOnSurf& GetProjector(const TopoDS_Face& F,
370 TopLoc_Location& loc,
371 double tol=0 ) const;
374 * \brief Check if shape is a degenerated edge or it's vertex
375 * \param subShape - edge or vertex index in SMESHDS
376 * \retval bool - true if subShape is a degenerated shape
378 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
380 bool IsDegenShape(const int subShape) const
381 { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
383 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
384 * has a degenerated edges
385 * \retval bool - true if it has
387 bool HasDegeneratedEdges() const { return !myDegenShapeIds.empty(); }
390 * \brief Check if shape is a seam edge or it's vertex
391 * \param subShape - edge or vertex index in SMESHDS
392 * \retval bool - true if subShape is a seam shape
394 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
395 * Seam shape has two 2D alternative represenations on the face
397 bool IsSeamShape(const int subShape) const
398 { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
400 * \brief Check if shape is a seam edge or it's vertex
401 * \param subShape - edge or vertex
402 * \retval bool - true if subShape is a seam shape
404 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
405 * Seam shape has two 2D alternative represenations on the face
407 bool IsSeamShape(const TopoDS_Shape& subShape) const
408 { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
410 * \brief Return true if an edge or a vertex encounters twice in face wire
411 * \param subShape - Id of edge or vertex
413 bool IsRealSeam(const int subShape) const
414 { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
416 * \brief Return true if an edge or a vertex encounters twice in face wire
417 * \param subShape - edge or vertex
419 bool IsRealSeam(const TopoDS_Shape& subShape) const
420 { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
422 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
424 * \retval bool - true if it has
426 bool HasSeam() const { return !mySeamShapeIds.empty(); }
428 * \brief Return index of periodic parametric direction of a closed face
429 * \retval int - 1 for U, 2 for V direction
431 int GetPeriodicIndex() const { return myParIndex; }
433 * \brief Return an alternative parameter for a node on seam
435 double GetOtherParam(const double param) const;
438 * \brief Return existing or create new medium nodes between given ones
439 * \param force3d - true means node creation at the middle between the
440 * two given nodes, else node position is found on its
441 * supporting geometrical shape, if any.
443 const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
444 const SMDS_MeshNode* n2,
447 * \brief Add a link in my data structure
449 void AddTLinkNode(const SMDS_MeshNode* n1,
450 const SMDS_MeshNode* n2,
451 const SMDS_MeshNode* n12);
453 * \brief Add many links in my data structure
455 void AddTLinkNodeMap(const TLinkNodeMap& aMap)
456 { myTLinkNodeMap.insert(aMap.begin(), aMap.end()); }
459 * Returns myTLinkNodeMap
461 const TLinkNodeMap& GetTLinkNodeMap() const { return myTLinkNodeMap; }
464 * Check mesh without geometry for: if all elements on this shape are quadratic,
465 * quadratic elements will be created.
466 * Used then generated 3D mesh without geometry.
468 enum MType{ LINEAR, QUADRATIC, COMP };
469 MType IsQuadraticMesh();
471 virtual ~SMESH_MesherHelper();
476 * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
477 * \param uv1 - UV on the seam
478 * \param uv2 - UV within a face
479 * \retval gp_Pnt2d - selected UV
481 gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
483 const SMDS_MeshNode* getMediumNodeOnComposedWire(const SMDS_MeshNode* n1,
484 const SMDS_MeshNode* n2,
488 // Forbiden copy constructor
489 SMESH_MesherHelper (const SMESH_MesherHelper& theOther) {};
491 // special map for using during creation of quadratic elements
492 TLinkNodeMap myTLinkNodeMap;
494 std::set< int > myDegenShapeIds;
495 std::set< int > mySeamShapeIds;
496 double myPar1[2], myPar2[2]; // U and V bounds of a closed periodic surface
497 int myParIndex; // bounds' index (1-U, 2-V, 3-both)
499 typedef std::map< int, GeomAPI_ProjectPointOnSurf* > TID2ProjectorOnSurf;
500 TID2ProjectorOnSurf myFace2Projector;
501 typedef std::map< int, GeomAPI_ProjectPointOnCurve* > TID2ProjectorOnCurve;
502 TID2ProjectorOnCurve myEdge2Projector;
504 TopoDS_Shape myShape;
508 // to create quadratic elements
509 bool myCreateQuadratic;
510 bool mySetElemOnShape;
512 std::map< int,bool > myNodePosShapesValidity;
513 bool toCheckPosOnShape(int shapeID ) const;
514 void setPosOnShapeValidity(int shapeID, bool ok ) const;