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>
43 class GeomAPI_ProjectPointOnSurf;
45 typedef std::map<SMESH_TLink, const SMDS_MeshNode*> TLinkNodeMap;
46 typedef std::map<SMESH_TLink, const SMDS_MeshNode*>::iterator ItTLinkNode;
48 typedef SMDS_Iterator<const TopoDS_Shape*> PShapeIterator;
49 typedef boost::shared_ptr< PShapeIterator > PShapeIteratorPtr;
51 typedef std::vector<const SMDS_MeshNode* > TNodeColumn;
52 typedef std::map< double, TNodeColumn > TParam2ColumnMap;
54 typedef gp_XY (*xyFunPtr)(const gp_XY& uv1, const gp_XY& uv2);
56 //=======================================================================
58 * \brief It helps meshers to add elements
60 * It allow meshers not to care about creation of medium nodes
61 * when filling a quadratic mesh. Helper does it itself.
62 * It defines degree of elements to create when IsQuadraticSubMesh()
65 //=======================================================================
67 class SMESH_EXPORT SMESH_MesherHelper
70 // ---------- PUBLIC UTILITIES ----------
73 * \brief Returns true if given node is medium
74 * \param n - node to check
75 * \param typeToCheck - type of elements containing the node to ask about node status
76 * \retval bool - check result
78 static bool IsMedium(const SMDS_MeshNode* node,
79 const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
82 * \brief Load nodes bound to face into a map of node columns
83 * \param theParam2ColumnMap - map of node columns to fill
84 * \param theFace - the face on which nodes are searched for
85 * \param theBaseEdge - the edge nodes of which are columns' bases
86 * \param theMesh - the mesh containing nodes
87 * \retval bool - false if something is wrong
89 * The key of the map is a normalized parameter of each
90 * base node on theBaseEdge.
91 * This method works in supposition that nodes on the face
92 * forms a rectangular grid and elements can be quardrangles or triangles
94 static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
95 const TopoDS_Face& theFace,
96 const TopoDS_Edge& theBaseEdge,
97 SMESHDS_Mesh* theMesh);
99 * \brief Return support shape of a node
100 * \param node - the node
101 * \param meshDS - mesh DS
102 * \retval TopoDS_Shape - found support shape
104 static TopoDS_Shape GetSubShapeByNode(const SMDS_MeshNode* node,
105 SMESHDS_Mesh* meshDS);
108 * \brief Return a valid node index, fixing the given one if necessary
109 * \param ind - node index
110 * \param nbNodes - total nb of nodes
111 * \retval int - valid node index
113 static int WrapIndex(const int ind, const int nbNodes) {
114 if ( ind < 0 ) return nbNodes + ind % nbNodes;
115 if ( ind >= nbNodes ) return ind % nbNodes;
120 * \brief Return number of unique ancestors of the shape
122 static int NbAncestors(const TopoDS_Shape& shape,
123 const SMESH_Mesh& mesh,
124 TopAbs_ShapeEnum ancestorType=TopAbs_SHAPE);
126 * \brief Return iterator on ancestors of the given type
128 static PShapeIteratorPtr GetAncestors(const TopoDS_Shape& shape,
129 const SMESH_Mesh& mesh,
130 TopAbs_ShapeEnum ancestorType);
133 * \brief Return orientation of sub-shape in the main shape
135 static TopAbs_Orientation GetSubShapeOri(const TopoDS_Shape& shape,
136 const TopoDS_Shape& subShape);
138 static bool IsSubShape( const TopoDS_Shape& shape, const TopoDS_Shape& mainShape );
140 static bool IsSubShape( const TopoDS_Shape& shape, SMESH_Mesh* aMesh );
144 // ---------- PUBLIC INSTANCE METHODS ----------
147 SMESH_MesherHelper(SMESH_Mesh& theMesh);
149 SMESH_Mesh* GetMesh() const { return myMesh; }
151 SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
154 * Check submesh for given shape: if all elements on this shape are quadratic,
155 * quadratic elements will be created. Also fill myTLinkNodeMap
157 bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
159 * \brief Set order of elements to create without calling IsQuadraticSubMesh()
161 void SetIsQuadratic(const bool theBuildQuadratic)
162 { myCreateQuadratic = theBuildQuadratic; }
164 * \brief Return myCreateQuadratic flag
166 bool GetIsQuadratic() const { return myCreateQuadratic; }
169 * \brief Move medium nodes of faces and volumes to fix distorted elements
170 * \param volumeOnly - fix nodes on geom faces or not if the shape is solid
172 void FixQuadraticElements(bool volumeOnly=true);
175 * \brief To set created elements on the shape set by IsQuadraticSubMesh()
176 * or the next methods. By defaul elements are set on the shape if
177 * a mesh has no shape to be meshed
179 void SetElementsOnShape(bool toSet) { mySetElemOnShape = toSet; }
182 * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
184 void SetSubShape(const int subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
185 void SetSubShape(const TopoDS_Shape& subShape);
187 * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape()
188 * \retval int - shape index in SMESHDS
190 int GetSubShapeID() const { return myShapeID; }
192 * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape()
194 const TopoDS_Shape& GetSubShape() const { return myShape; }
199 SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0);
201 * Creates quadratic or linear edge
203 SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
204 const SMDS_MeshNode* n2,
206 const bool force3d = true);
208 * Creates quadratic or linear triangle
210 SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
211 const SMDS_MeshNode* n2,
212 const SMDS_MeshNode* n3,
214 const bool force3d = false);
216 * Creates quadratic or linear quadrangle
218 SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
219 const SMDS_MeshNode* n2,
220 const SMDS_MeshNode* n3,
221 const SMDS_MeshNode* n4,
223 const bool force3d = false);
225 * Creates quadratic or linear tetraahedron
227 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
228 const SMDS_MeshNode* n2,
229 const SMDS_MeshNode* n3,
230 const SMDS_MeshNode* n4,
232 const bool force3d = true);
234 * Creates quadratic or linear pyramid
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,
242 const bool force3d = true);
244 * Creates quadratic or linear pentahedron
246 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
247 const SMDS_MeshNode* n2,
248 const SMDS_MeshNode* n3,
249 const SMDS_MeshNode* n4,
250 const SMDS_MeshNode* n5,
251 const SMDS_MeshNode* n6,
253 const bool force3d = true);
255 * Creates quadratic or linear hexahedron
257 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
258 const SMDS_MeshNode* n2,
259 const SMDS_MeshNode* n3,
260 const SMDS_MeshNode* n4,
261 const SMDS_MeshNode* n5,
262 const SMDS_MeshNode* n6,
263 const SMDS_MeshNode* n7,
264 const SMDS_MeshNode* n8,
266 bool force3d = true);
268 * \brief Return U of the given node on the edge
270 double GetNodeU(const TopoDS_Edge& theEdge,
271 const SMDS_MeshNode* theNode,
274 * \brief Return node UV on face
275 * \param inFaceNode - a node of element being created located inside a face
277 gp_XY GetNodeUV(const TopoDS_Face& F,
278 const SMDS_MeshNode* n,
279 const SMDS_MeshNode* inFaceNode=0,
280 bool* check=0) const;
282 * \brief Check and fix node UV on a face
283 * \param force - check even if checks of other nodes on this face passed OK
284 * \retval bool - false if UV is bad and could not be fixed
286 bool CheckNodeUV(const TopoDS_Face& F,
287 const SMDS_MeshNode* n,
290 const bool force=false) const;
292 * \brief Check and fix node U on an edge
293 * \param force - check even if checks of other nodes on this edge passed OK
294 * \retval bool - false if U is bad and could not be fixed
296 bool CheckNodeU(const TopoDS_Edge& E,
297 const SMDS_MeshNode* n,
300 const bool force=false) const;
302 * \brief Return middle UV taking in account surface period
304 static gp_XY GetMiddleUV(const Handle(Geom_Surface)& surface,
308 * \brief Define a pointer to wrapper over a function of gp_XY class,
309 * suitable to pass as xyFunPtr to applyIn2D().
310 * For exaple gp_XY_FunPtr(Added) defines pointer gp_XY_Added to function
311 * calling gp_XY::Added(gp_XY), which is to be used like following
312 * applyIn2D(surf, uv1, uv2, gp_XY_Added)
314 #define gp_XY_FunPtr(meth) \
315 static gp_XY __gpXY_##meth (const gp_XY& uv1, const gp_XY& uv2) { return uv1.meth( uv2 ); } \
316 static xyFunPtr gp_XY_##meth = & __gpXY_##meth
319 * \brief Perform given operation on two 2d points in parameric space of given surface.
320 * It takes into account period of the surface. Use gp_XY_FunPtr macro
321 * to easily define pointer to function of gp_XY class.
323 static gp_XY applyIn2D(const Handle(Geom_Surface)& surface,
327 const bool resultInPeriod=true);
330 * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
331 * \retval bool - return true if the face is periodic
333 * If F is Null, answer about subshape set through IsQuadraticSubMesh() or
336 bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
339 * \brief Return projector intitialized by given face without location, which is returned
341 GeomAPI_ProjectPointOnSurf& GetProjector(const TopoDS_Face& F,
342 TopLoc_Location& loc,
343 double tol=0 ) const;
346 * \brief Check if shape is a degenerated edge or it's vertex
347 * \param subShape - edge or vertex index in SMESHDS
348 * \retval bool - true if subShape is a degenerated shape
350 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
352 bool IsDegenShape(const int subShape) const
353 { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
355 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
356 * has a degenerated edges
357 * \retval bool - true if it has
359 bool HasDegeneratedEdges() const { return !myDegenShapeIds.empty(); }
362 * \brief Check if shape is a seam edge or it's vertex
363 * \param subShape - edge or vertex index in SMESHDS
364 * \retval bool - true if subShape is a seam shape
366 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
367 * Seam shape has two 2D alternative represenations on the face
369 bool IsSeamShape(const int subShape) const
370 { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
372 * \brief Check if shape is a seam edge or it's vertex
373 * \param subShape - edge or vertex
374 * \retval bool - true if subShape is a seam shape
376 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
377 * Seam shape has two 2D alternative represenations on the face
379 bool IsSeamShape(const TopoDS_Shape& subShape) const
380 { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
382 * \brief Return true if an edge or a vertex encounters twice in face wire
383 * \param subShape - Id of edge or vertex
385 bool IsRealSeam(const int subShape) const
386 { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
388 * \brief Return true if an edge or a vertex encounters twice in face wire
389 * \param subShape - edge or vertex
391 bool IsRealSeam(const TopoDS_Shape& subShape) const
392 { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
394 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
396 * \retval bool - true if it has
398 bool HasSeam() const { return !mySeamShapeIds.empty(); }
400 * \brief Return index of periodic parametric direction of a closed face
401 * \retval int - 1 for U, 2 for V direction
403 int GetPeriodicIndex() const { return myParIndex; }
405 * \brief Return an alternative parameter for a node on seam
407 double GetOtherParam(const double param) const;
410 * \brief Return existing or create new medium nodes between given ones
411 * \param force3d - true means node creation at the middle between the
412 * two given nodes, else node position is found on its
413 * supporting geometrical shape, if any.
415 const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
416 const SMDS_MeshNode* n2,
419 * \brief Add a link in my data structure
421 void AddTLinkNode(const SMDS_MeshNode* n1,
422 const SMDS_MeshNode* n2,
423 const SMDS_MeshNode* n12);
425 * \brief Add many links in my data structure
427 void AddTLinkNodeMap(const TLinkNodeMap& aMap)
428 { myTLinkNodeMap.insert(aMap.begin(), aMap.end()); }
431 * Returns myTLinkNodeMap
433 const TLinkNodeMap& GetTLinkNodeMap() const { return myTLinkNodeMap; }
436 * Check mesh without geometry for: if all elements on this shape are quadratic,
437 * quadratic elements will be created.
438 * Used then generated 3D mesh without geometry.
440 enum MType{ LINEAR, QUADRATIC, COMP };
441 MType IsQuadraticMesh();
443 virtual ~SMESH_MesherHelper();
448 * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
449 * \param uv1 - UV on the seam
450 * \param uv2 - UV within a face
451 * \retval gp_Pnt2d - selected UV
453 gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
457 // Forbiden copy constructor
458 SMESH_MesherHelper (const SMESH_MesherHelper& theOther) {};
460 // special map for using during creation of quadratic elements
461 TLinkNodeMap myTLinkNodeMap;
463 std::set< int > myDegenShapeIds;
464 std::set< int > mySeamShapeIds;
465 double myPar1[2], myPar2[2]; // U and V bounds of a closed periodic surface
466 int myParIndex; // bounds' index (1-U, 2-V, 3-both)
468 typedef std::map< int, GeomAPI_ProjectPointOnSurf* > TID2Projector;
469 TID2Projector myFace2Projector;
471 TopoDS_Shape myShape;
475 // to create quadratic elements
476 bool myCreateQuadratic;
477 bool mySetElemOnShape;
478 std::set< int > myOkNodePosShapes;