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,
272 const SMDS_MeshNode* inEdgeNode=0,
275 * \brief Return node UV on face
276 * \param inFaceNode - a node of element being created located inside a face
278 gp_XY GetNodeUV(const TopoDS_Face& F,
279 const SMDS_MeshNode* n,
280 const SMDS_MeshNode* inFaceNode=0,
281 bool* check=0) const;
283 * \brief Check and fix node UV on a face
284 * \param force - check even if checks of other nodes on this face passed OK
285 * \retval bool - false if UV is bad and could not be fixed
287 bool CheckNodeUV(const TopoDS_Face& F,
288 const SMDS_MeshNode* n,
291 const bool force=false) const;
293 * \brief Check and fix node U on an edge
294 * \param force - check even if checks of other nodes on this edge passed OK
295 * \retval bool - false if U is bad and could not be fixed
297 bool CheckNodeU(const TopoDS_Edge& E,
298 const SMDS_MeshNode* n,
301 const bool force=false,
302 double* distance=0) const;
304 * \brief Return middle UV taking in account surface period
306 static gp_XY GetMiddleUV(const Handle(Geom_Surface)& surface,
310 * \brief Define a pointer to wrapper over a function of gp_XY class,
311 * suitable to pass as xyFunPtr to applyIn2D().
312 * For exaple gp_XY_FunPtr(Added) defines pointer gp_XY_Added to function
313 * calling gp_XY::Added(gp_XY), which is to be used like following
314 * applyIn2D(surf, uv1, uv2, gp_XY_Added)
316 #define gp_XY_FunPtr(meth) \
317 static gp_XY __gpXY_##meth (const gp_XY& uv1, const gp_XY& uv2) { return uv1.meth( uv2 ); } \
318 static xyFunPtr gp_XY_##meth = & __gpXY_##meth
321 * \brief Perform given operation on two 2d points in parameric space of given surface.
322 * It takes into account period of the surface. Use gp_XY_FunPtr macro
323 * to easily define pointer to function of gp_XY class.
325 static gp_XY applyIn2D(const Handle(Geom_Surface)& surface,
329 const bool resultInPeriod=true);
332 * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
333 * \retval bool - return true if the face is periodic
335 * If F is Null, answer about subshape set through IsQuadraticSubMesh() or
338 bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
341 * \brief Return projector intitialized by given face without location, which is returned
343 GeomAPI_ProjectPointOnSurf& GetProjector(const TopoDS_Face& F,
344 TopLoc_Location& loc,
345 double tol=0 ) const;
348 * \brief Check if shape is a degenerated edge or it's vertex
349 * \param subShape - edge or vertex index in SMESHDS
350 * \retval bool - true if subShape is a degenerated shape
352 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
354 bool IsDegenShape(const int subShape) const
355 { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
357 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
358 * has a degenerated edges
359 * \retval bool - true if it has
361 bool HasDegeneratedEdges() const { return !myDegenShapeIds.empty(); }
364 * \brief Check if shape is a seam edge or it's vertex
365 * \param subShape - edge or vertex index in SMESHDS
366 * \retval bool - true if subShape is a seam shape
368 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
369 * Seam shape has two 2D alternative represenations on the face
371 bool IsSeamShape(const int subShape) const
372 { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
374 * \brief Check if shape is a seam edge or it's vertex
375 * \param subShape - edge or vertex
376 * \retval bool - true if subShape is a seam shape
378 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
379 * Seam shape has two 2D alternative represenations on the face
381 bool IsSeamShape(const TopoDS_Shape& subShape) const
382 { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
384 * \brief Return true if an edge or a vertex encounters twice in face wire
385 * \param subShape - Id of edge or vertex
387 bool IsRealSeam(const int subShape) const
388 { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
390 * \brief Return true if an edge or a vertex encounters twice in face wire
391 * \param subShape - edge or vertex
393 bool IsRealSeam(const TopoDS_Shape& subShape) const
394 { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
396 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
398 * \retval bool - true if it has
400 bool HasSeam() const { return !mySeamShapeIds.empty(); }
402 * \brief Return index of periodic parametric direction of a closed face
403 * \retval int - 1 for U, 2 for V direction
405 int GetPeriodicIndex() const { return myParIndex; }
407 * \brief Return an alternative parameter for a node on seam
409 double GetOtherParam(const double param) const;
412 * \brief Return existing or create new medium nodes between given ones
413 * \param force3d - true means node creation at the middle between the
414 * two given nodes, else node position is found on its
415 * supporting geometrical shape, if any.
417 const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
418 const SMDS_MeshNode* n2,
421 * \brief Add a link in my data structure
423 void AddTLinkNode(const SMDS_MeshNode* n1,
424 const SMDS_MeshNode* n2,
425 const SMDS_MeshNode* n12);
427 * \brief Add many links in my data structure
429 void AddTLinkNodeMap(const TLinkNodeMap& aMap)
430 { myTLinkNodeMap.insert(aMap.begin(), aMap.end()); }
433 * Returns myTLinkNodeMap
435 const TLinkNodeMap& GetTLinkNodeMap() const { return myTLinkNodeMap; }
438 * Check mesh without geometry for: if all elements on this shape are quadratic,
439 * quadratic elements will be created.
440 * Used then generated 3D mesh without geometry.
442 enum MType{ LINEAR, QUADRATIC, COMP };
443 MType IsQuadraticMesh();
445 virtual ~SMESH_MesherHelper();
450 * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
451 * \param uv1 - UV on the seam
452 * \param uv2 - UV within a face
453 * \retval gp_Pnt2d - selected UV
455 gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
457 const SMDS_MeshNode* getMediumNodeOnComposedWire(const SMDS_MeshNode* n1,
458 const SMDS_MeshNode* n2,
462 // Forbiden copy constructor
463 SMESH_MesherHelper (const SMESH_MesherHelper& theOther) {};
465 // special map for using during creation of quadratic elements
466 TLinkNodeMap myTLinkNodeMap;
468 std::set< int > myDegenShapeIds;
469 std::set< int > mySeamShapeIds;
470 double myPar1[2], myPar2[2]; // U and V bounds of a closed periodic surface
471 int myParIndex; // bounds' index (1-U, 2-V, 3-both)
473 typedef std::map< int, GeomAPI_ProjectPointOnSurf* > TID2Projector;
474 TID2Projector myFace2Projector;
476 TopoDS_Shape myShape;
480 // to create quadratic elements
481 bool myCreateQuadratic;
482 bool mySetElemOnShape;
483 std::set< int > myOkNodePosShapes;