1 // Copyright (C) 2007-2012 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 and provides other utilities
63 * - It allows meshers not to care about creation of medium nodes
64 * when filling a quadratic mesh. Helper does it itself.
65 * It defines order of elements to create when IsQuadraticSubMesh()
67 * - It provides information on a shape it is initialized with:
68 * periodicity, presence of singularities etc.
71 //=======================================================================
73 class SMESH_EXPORT SMESH_MesherHelper
76 // ---------- PUBLIC UTILITIES ----------
79 * \brief Returns true if all elements of a sub-mesh are of same shape
80 * \param smDS - sub-mesh to check elements of
81 * \param shape - expected shape of elements
82 * \param nullSubMeshRes - result value for the case of smDS == NULL
83 * \retval bool - check result
85 static bool IsSameElemGeometry(const SMESHDS_SubMesh* smDS,
86 SMDSAbs_GeometryType shape,
87 const bool nullSubMeshRes = true);
90 * \brief Load nodes bound to face into a map of node columns
91 * \param theParam2ColumnMap - map of node columns to fill
92 * \param theFace - the face on which nodes are searched for
93 * \param theBaseSide - the edges holding nodes on which columns' bases
94 * \param theMesh - the mesh containing nodes
95 * \retval bool - false if something is wrong
97 * The key of the map is a normalized parameter of each
98 * base node on theBaseSide. Edges in theBaseSide must be sequenced.
99 * This method works in supposition that nodes on the face
100 * forms a rectangular grid and elements can be quardrangles or triangles
102 static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
103 const TopoDS_Face& theFace,
104 const std::list<TopoDS_Edge>& theBaseSide,
105 SMESHDS_Mesh* theMesh,
106 SMESH_ProxyMesh* theProxyMesh=0);
108 * \brief Variant of LoadNodeColumns() above with theBaseSide given by one edge
110 static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
111 const TopoDS_Face& theFace,
112 const TopoDS_Edge& theBaseEdge,
113 SMESHDS_Mesh* theMesh,
114 SMESH_ProxyMesh* theProxyMesh=0);
116 * \brief Return true if 2D mesh on FACE is structured
118 static bool IsStructured( SMESH_subMesh* faceSM );
121 * \brief Returns true if given node is medium
122 * \param n - node to check
123 * \param typeToCheck - type of elements containing the node to ask about node status
124 * \retval bool - check result
126 static bool IsMedium(const SMDS_MeshNode* node,
127 const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
129 * \brief Return support shape of a node
130 * \param node - the node
131 * \param meshDS - mesh DS
132 * \retval TopoDS_Shape - found support shape
134 static TopoDS_Shape GetSubShapeByNode(const SMDS_MeshNode* node,
135 const SMESHDS_Mesh* meshDS);
138 * \brief Return a valid node index, fixing the given one if necessary
139 * \param ind - node index
140 * \param nbNodes - total nb of nodes
141 * \retval int - valid node index
143 static int WrapIndex(const int ind, const int nbNodes) {
144 if ( ind < 0 ) return nbNodes + ind % nbNodes;
145 if ( ind >= nbNodes ) return ind % nbNodes;
150 * \brief Count nb of sub-shapes
151 * \param shape - the shape
152 * \param type - the type of sub-shapes to count
153 * \param ignoreSame - if true, use map not to count same shapes, esle use explorer
154 * \retval int - the calculated number
156 static int Count(const TopoDS_Shape& shape,
157 const TopAbs_ShapeEnum type,
158 const bool ignoreSame);
161 * \brief Return number of unique ancestors of the shape
163 static int NbAncestors(const TopoDS_Shape& shape,
164 const SMESH_Mesh& mesh,
165 TopAbs_ShapeEnum ancestorType=TopAbs_SHAPE);
167 * \brief Return iterator on ancestors of the given type
169 static PShapeIteratorPtr GetAncestors(const TopoDS_Shape& shape,
170 const SMESH_Mesh& mesh,
171 TopAbs_ShapeEnum ancestorType);
173 * \brief Find a common ancestor, of the given type, of two shapes
175 static TopoDS_Shape GetCommonAncestor(const TopoDS_Shape& shape1,
176 const TopoDS_Shape& shape2,
177 const SMESH_Mesh& mesh,
178 TopAbs_ShapeEnum ancestorType);
180 * \brief Return orientation of sub-shape in the main shape
182 static TopAbs_Orientation GetSubShapeOri(const TopoDS_Shape& shape,
183 const TopoDS_Shape& subShape);
185 static bool IsSubShape( const TopoDS_Shape& shape, const TopoDS_Shape& mainShape );
187 static bool IsSubShape( const TopoDS_Shape& shape, SMESH_Mesh* aMesh );
189 static double MaxTolerance( const TopoDS_Shape& shape );
191 static bool IsClosedEdge( const TopoDS_Edge& anEdge );
193 static TopoDS_Vertex IthVertex( const bool is2nd, TopoDS_Edge anEdge, const bool CumOri=true );
195 static TopAbs_ShapeEnum GetGroupType(const TopoDS_Shape& group,
196 const bool avoidCompound=false);
200 // ---------- PUBLIC INSTANCE METHODS ----------
203 SMESH_MesherHelper(SMESH_Mesh& theMesh);
205 SMESH_Mesh* GetMesh() const { return myMesh; }
207 SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
210 * Check submesh for given shape: if all elements on this shape are quadratic,
211 * quadratic elements will be created. Also fill myTLinkNodeMap
213 bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
215 * \brief Set order of elements to create without calling IsQuadraticSubMesh()
217 void SetIsQuadratic(const bool theBuildQuadratic)
218 { myCreateQuadratic = theBuildQuadratic; }
220 * \brief Return myCreateQuadratic flag
222 bool GetIsQuadratic() const { return myCreateQuadratic; }
225 * \brief Move medium nodes of faces and volumes to fix distorted elements
226 * \param error - container of fixed distorted elements
227 * \param volumeOnly - fix nodes on geom faces or not if the shape is solid
229 void FixQuadraticElements(SMESH_ComputeErrorPtr& error, bool volumeOnly=true);
232 * \brief To set created elements on the shape set by IsQuadraticSubMesh()
233 * or the next methods. By defaul elements are set on the shape if
234 * a mesh has no shape to be meshed
236 bool SetElementsOnShape(bool toSet)
237 { bool res = mySetElemOnShape; mySetElemOnShape = toSet; return res; }
240 * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
242 void SetSubShape(const int subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
243 void SetSubShape(const TopoDS_Shape& subShape);
245 * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape()
246 * \retval int - shape index in SMESHDS
248 int GetSubShapeID() const { return myShapeID; }
250 * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape()
252 const TopoDS_Shape& GetSubShape() const { return myShape; }
257 SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0, double u=0., double v=0.);
259 * Creates quadratic or linear edge
261 SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
262 const SMDS_MeshNode* n2,
264 const bool force3d = true);
266 * Creates quadratic or linear triangle
268 SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
269 const SMDS_MeshNode* n2,
270 const SMDS_MeshNode* n3,
272 const bool force3d = false);
274 * Creates quadratic or linear quadrangle
276 SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
277 const SMDS_MeshNode* n2,
278 const SMDS_MeshNode* n3,
279 const SMDS_MeshNode* n4,
281 const bool force3d = false);
284 * Creates polygon, with additional nodes in quadratic mesh
286 SMDS_MeshFace* AddPolygonalFace (const std::vector<const SMDS_MeshNode*>& nodes,
288 const bool force3d = false);
290 * Creates quadratic or linear tetrahedron
292 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
293 const SMDS_MeshNode* n2,
294 const SMDS_MeshNode* n3,
295 const SMDS_MeshNode* n4,
297 const bool force3d = true);
299 * Creates quadratic or linear pyramid
301 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
302 const SMDS_MeshNode* n2,
303 const SMDS_MeshNode* n3,
304 const SMDS_MeshNode* n4,
305 const SMDS_MeshNode* n5,
307 const bool force3d = true);
309 * Creates quadratic or linear pentahedron
311 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
312 const SMDS_MeshNode* n2,
313 const SMDS_MeshNode* n3,
314 const SMDS_MeshNode* n4,
315 const SMDS_MeshNode* n5,
316 const SMDS_MeshNode* n6,
318 const bool force3d = true);
320 * Creates quadratic or linear hexahedron
322 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
323 const SMDS_MeshNode* n2,
324 const SMDS_MeshNode* n3,
325 const SMDS_MeshNode* n4,
326 const SMDS_MeshNode* n5,
327 const SMDS_MeshNode* n6,
328 const SMDS_MeshNode* n7,
329 const SMDS_MeshNode* n8,
331 bool force3d = true);
334 * Creates LINEAR!!!!!!!!! octahedron
336 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
337 const SMDS_MeshNode* n2,
338 const SMDS_MeshNode* n3,
339 const SMDS_MeshNode* n4,
340 const SMDS_MeshNode* n5,
341 const SMDS_MeshNode* n6,
342 const SMDS_MeshNode* n7,
343 const SMDS_MeshNode* n8,
344 const SMDS_MeshNode* n9,
345 const SMDS_MeshNode* n10,
346 const SMDS_MeshNode* n11,
347 const SMDS_MeshNode* n12,
349 bool force3d = true);
352 * Creates polyhedron. In quadratic mesh, adds medium nodes
354 SMDS_MeshVolume* AddPolyhedralVolume (const std::vector<const SMDS_MeshNode*>& nodes,
355 const std::vector<int>& quantities,
357 const bool force3d = true);
359 * \brief Enables fixing node parameters on EDGEs and FACEs by
360 * GetNodeU(...,check=true), GetNodeUV(...,check=true), CheckNodeUV() and
361 * CheckNodeU() in case if a node lies on a shape set via SetSubShape().
364 void ToFixNodeParameters(bool toFix);
367 * \brief Return U of the given node on the edge
369 double GetNodeU(const TopoDS_Edge& theEdge,
370 const SMDS_MeshNode* theNode,
371 const SMDS_MeshNode* inEdgeNode=0,
374 * \brief Return node UV on face
375 * \param inFaceNode - a node of element being created located inside a face
377 gp_XY GetNodeUV(const TopoDS_Face& F,
378 const SMDS_MeshNode* n,
379 const SMDS_MeshNode* inFaceNode=0,
380 bool* check=0) const;
382 * \brief Check and fix node UV on a face
383 * \param force - check even if checks of other nodes on this face passed OK
384 * \param distXYZ - returns result distance and point coordinates
385 * \retval bool - false if UV is bad and could not be fixed
387 bool CheckNodeUV(const TopoDS_Face& F,
388 const SMDS_MeshNode* n,
391 const bool force=false,
392 double distXYZ[4]=0) const;
394 * \brief Check and fix node U on an edge
395 * \param force - check even if checks of other nodes on this edge passed OK
396 * \param distXYZ - returns result distance and point coordinates
397 * \retval bool - false if U is bad and could not be fixed
399 bool CheckNodeU(const TopoDS_Edge& E,
400 const SMDS_MeshNode* n,
403 const bool force=false,
404 double distXYZ[4]=0) const;
406 * \brief Return middle UV taking in account surface period
408 static gp_XY GetMiddleUV(const Handle(Geom_Surface)& surface,
412 * \brief Define a pointer to wrapper over a function of gp_XY class,
413 * suitable to pass as xyFunPtr to applyIn2D().
414 * For exaple gp_XY_FunPtr(Added) defines pointer gp_XY_Added to function
415 * calling gp_XY::Added(gp_XY), which is to be used like following
416 * applyIn2D(surf, uv1, uv2, gp_XY_Added)
418 #define gp_XY_FunPtr(meth) \
419 static gp_XY __gpXY_##meth (const gp_XY& uv1, const gp_XY& uv2) { return uv1.meth( uv2 ); } \
420 static xyFunPtr gp_XY_##meth = & __gpXY_##meth
423 * \brief Perform given operation on two 2d points in parameric space of given surface.
424 * It takes into account period of the surface. Use gp_XY_FunPtr macro
425 * to easily define pointer to function of gp_XY class.
427 static gp_XY applyIn2D(const Handle(Geom_Surface)& surface,
431 const bool resultInPeriod=true);
434 * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
435 * \retval bool - return true if the face is periodic
437 * If F is Null, answer about subshape set through IsQuadraticSubMesh() or
440 bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
443 * \brief Return projector intitialized by given face without location, which is returned
445 GeomAPI_ProjectPointOnSurf& GetProjector(const TopoDS_Face& F,
446 TopLoc_Location& loc,
447 double tol=0 ) const;
450 * \brief Check if shape is a degenerated edge or it's vertex
451 * \param subShape - edge or vertex index in SMESHDS
452 * \retval bool - true if subShape is a degenerated shape
454 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
456 bool IsDegenShape(const int subShape) const
457 { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
459 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
460 * has a degenerated edges
461 * \retval bool - true if it has
463 bool HasDegeneratedEdges() const { return !myDegenShapeIds.empty(); }
466 * \brief Check if shape is a seam edge or it's vertex
467 * \param subShape - edge or vertex index in SMESHDS
468 * \retval bool - true if subShape is a seam shape
470 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
471 * Seam shape has two 2D alternative represenations on the face
473 bool IsSeamShape(const int subShape) const
474 { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
476 * \brief Check if shape is a seam edge or it's vertex
477 * \param subShape - edge or vertex
478 * \retval bool - true if subShape is a seam shape
480 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
481 * Seam shape has two 2D alternative represenations on the face
483 bool IsSeamShape(const TopoDS_Shape& subShape) const
484 { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
486 * \brief Return true if an edge or a vertex encounters twice in face wire
487 * \param subShape - Id of edge or vertex
489 bool IsRealSeam(const int subShape) const
490 { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
492 * \brief Return true if an edge or a vertex encounters twice in face wire
493 * \param subShape - edge or vertex
495 bool IsRealSeam(const TopoDS_Shape& subShape) const
496 { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
498 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
500 * \retval bool - true if it has
502 bool HasSeam() const { return !mySeamShapeIds.empty(); }
504 * \brief Return index of periodic parametric direction of a closed face
505 * \retval int - 1 for U, 2 for V direction
507 int GetPeriodicIndex() const { return myParIndex; }
509 * \brief Return an alternative parameter for a node on seam
511 double GetOtherParam(const double param) const;
514 * \brief Return existing or create new medium nodes between given ones
515 * \param force3d - true means node creation at the middle between the
516 * two given nodes, else node position is found on its
517 * supporting geometrical shape, if any.
519 const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
520 const SMDS_MeshNode* n2,
523 * \brief Return index and type of the shape (EDGE or FACE only) to set a medium node on
525 std::pair<int, TopAbs_ShapeEnum> GetMediumPos(const SMDS_MeshNode* n1,
526 const SMDS_MeshNode* n2);
528 * \brief Add a link in my data structure
530 void AddTLinkNode(const SMDS_MeshNode* n1,
531 const SMDS_MeshNode* n2,
532 const SMDS_MeshNode* n12);
534 * \brief Add many links in my data structure
536 void AddTLinkNodeMap(const TLinkNodeMap& aMap)
537 { myTLinkNodeMap.insert(aMap.begin(), aMap.end()); }
539 void AddTLinks(const SMDS_MeshEdge* edge);
540 void AddTLinks(const SMDS_MeshFace* face);
541 void AddTLinks(const SMDS_MeshVolume* vol);
544 * Returns myTLinkNodeMap
546 const TLinkNodeMap& GetTLinkNodeMap() const { return myTLinkNodeMap; }
549 * Check mesh without geometry for: if all elements on this shape are quadratic,
550 * quadratic elements will be created.
551 * Used then generated 3D mesh without geometry.
553 enum MType{ LINEAR, QUADRATIC, COMP };
554 MType IsQuadraticMesh();
556 virtual ~SMESH_MesherHelper();
561 * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
562 * \param uv1 - UV on the seam
563 * \param uv2 - UV within a face
564 * \retval gp_Pnt2d - selected UV
566 gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
568 const SMDS_MeshNode* getMediumNodeOnComposedWire(const SMDS_MeshNode* n1,
569 const SMDS_MeshNode* n2,
573 // Forbiden copy constructor
574 SMESH_MesherHelper (const SMESH_MesherHelper& theOther) {};
576 // special map for using during creation of quadratic elements
577 TLinkNodeMap myTLinkNodeMap;
579 std::set< int > myDegenShapeIds;
580 std::set< int > mySeamShapeIds;
581 double myPar1[2], myPar2[2]; // U and V bounds of a closed periodic surface
582 int myParIndex; // bounds' index (1-U, 2-V, 3-both)
584 typedef std::map< int, GeomAPI_ProjectPointOnSurf* > TID2ProjectorOnSurf;
585 TID2ProjectorOnSurf myFace2Projector;
586 typedef std::map< int, GeomAPI_ProjectPointOnCurve* > TID2ProjectorOnCurve;
587 TID2ProjectorOnCurve myEdge2Projector;
589 TopoDS_Shape myShape;
593 bool myCreateQuadratic;
594 bool mySetElemOnShape;
595 bool myFixNodeParameters;
597 std::map< int,bool > myNodePosShapesValidity;
598 bool toCheckPosOnShape(int shapeID ) const;
599 void setPosOnShapeValidity(int shapeID, bool ok ) const;