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;
45 typedef SMDS_Iterator<const TopoDS_Shape*> PShapeIterator;
46 typedef boost::shared_ptr< PShapeIterator > PShapeIteratorPtr;
48 typedef std::vector<const SMDS_MeshNode* > TNodeColumn;
49 typedef std::map< double, TNodeColumn > TParam2ColumnMap;
51 //=======================================================================
53 * \brief It helps meshers to add elements
55 * It allow meshers not to care about creation of medium nodes
56 * when filling a quadratic mesh. Helper does it itself.
57 * It defines degree of elements to create when IsQuadraticSubMesh()
60 //=======================================================================
62 class SMESH_EXPORT SMESH_MesherHelper
65 // ---------- PUBLIC UTILITIES ----------
68 * \brief Returns true if given node is medium
69 * \param n - node to check
70 * \param typeToCheck - type of elements containing the node to ask about node status
71 * \retval bool - check result
73 static bool IsMedium(const SMDS_MeshNode* node,
74 const SMDSAbs_ElementType typeToCheck = SMDSAbs_All);
77 * \brief Load nodes bound to face into a map of node columns
78 * \param theParam2ColumnMap - map of node columns to fill
79 * \param theFace - the face on which nodes are searched for
80 * \param theBaseEdge - the edge nodes of which are columns' bases
81 * \param theMesh - the mesh containing nodes
82 * \retval bool - false if something is wrong
84 * The key of the map is a normalized parameter of each
85 * base node on theBaseEdge.
86 * This method works in supposition that nodes on the face
87 * forms a rectangular grid and elements can be quardrangles or triangles
89 static bool LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
90 const TopoDS_Face& theFace,
91 const TopoDS_Edge& theBaseEdge,
92 SMESHDS_Mesh* theMesh);
94 * \brief Return support shape of a node
95 * \param node - the node
96 * \param meshDS - mesh DS
97 * \retval TopoDS_Shape - found support shape
99 static TopoDS_Shape GetSubShapeByNode(const SMDS_MeshNode* node,
100 SMESHDS_Mesh* meshDS);
103 * \brief Return a valid node index, fixing the given one if necessary
104 * \param ind - node index
105 * \param nbNodes - total nb of nodes
106 * \retval int - valid node index
108 static int WrapIndex(const int ind, const int nbNodes) {
109 if ( ind < 0 ) return nbNodes + ind % nbNodes;
110 if ( ind >= nbNodes ) return ind % nbNodes;
115 * \brief Return number of unique ancestors of the shape
117 static int NbAncestors(const TopoDS_Shape& shape,
118 const SMESH_Mesh& mesh,
119 TopAbs_ShapeEnum ancestorType=TopAbs_SHAPE);
121 * \brief Return iterator on ancestors of the given type
123 static PShapeIteratorPtr GetAncestors(const TopoDS_Shape& shape,
124 const SMESH_Mesh& mesh,
125 TopAbs_ShapeEnum ancestorType);
128 * \brief Return orientation of sub-shape in the main shape
130 static TopAbs_Orientation GetSubShapeOri(const TopoDS_Shape& shape,
131 const TopoDS_Shape& subShape);
134 // ---------- PUBLIC INSTANCE METHODS ----------
137 SMESH_MesherHelper(SMESH_Mesh& theMesh);
139 SMESH_Mesh* GetMesh() const { return myMesh; }
141 SMESHDS_Mesh* GetMeshDS() const { return GetMesh()->GetMeshDS(); }
144 * Check submesh for given shape: if all elements on this shape are quadratic,
145 * quadratic elements will be created. Also fill myTLinkNodeMap
147 bool IsQuadraticSubMesh(const TopoDS_Shape& theShape);
149 * \brief Set order of elements to create without calling IsQuadraticSubMesh()
151 void SetIsQuadratic(const bool theBuildQuadratic)
152 { myCreateQuadratic = theBuildQuadratic; }
154 * \brief Return myCreateQuadratic flag
156 bool GetIsQuadratic() const { return myCreateQuadratic; }
159 * \brief Move medium nodes of faces and volumes to fix distorted elements
160 * \param volumeOnly - fix nodes on geom faces or not if the shape is solid
162 void FixQuadraticElements(bool volumeOnly=true);
165 * \brief To set created elements on the shape set by IsQuadraticSubMesh()
166 * or the next methods. By defaul elements are set on the shape if
167 * a mesh has no shape to be meshed
169 void SetElementsOnShape(bool toSet) { mySetElemOnShape = toSet; }
172 * \brief Set shape to make elements on without calling IsQuadraticSubMesh()
174 void SetSubShape(const int subShapeID);//!==SMESHDS_Mesh::ShapeToIndex(shape)
175 void SetSubShape(const TopoDS_Shape& subShape);
177 * \brief Return ID of the shape set by IsQuadraticSubMesh() or SetSubShape()
178 * \retval int - shape index in SMESHDS
180 int GetSubShapeID() const { return myShapeID; }
182 * \brief Return the shape set by IsQuadraticSubMesh() or SetSubShape()
184 const TopoDS_Shape& GetSubShape() const { return myShape; }
189 SMDS_MeshNode* AddNode(double x, double y, double z, int ID = 0);
191 * Creates quadratic or linear edge
193 SMDS_MeshEdge* AddEdge(const SMDS_MeshNode* n1,
194 const SMDS_MeshNode* n2,
196 const bool force3d = true);
198 * Creates quadratic or linear triangle
200 SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
201 const SMDS_MeshNode* n2,
202 const SMDS_MeshNode* n3,
204 const bool force3d = false);
206 * Creates quadratic or linear quadrangle
208 SMDS_MeshFace* AddFace(const SMDS_MeshNode* n1,
209 const SMDS_MeshNode* n2,
210 const SMDS_MeshNode* n3,
211 const SMDS_MeshNode* n4,
213 const bool force3d = false);
215 * Creates quadratic or linear tetraahedron
217 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
218 const SMDS_MeshNode* n2,
219 const SMDS_MeshNode* n3,
220 const SMDS_MeshNode* n4,
222 const bool force3d = true);
224 * Creates quadratic or linear pyramid
226 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
227 const SMDS_MeshNode* n2,
228 const SMDS_MeshNode* n3,
229 const SMDS_MeshNode* n4,
230 const SMDS_MeshNode* n5,
232 const bool force3d = true);
234 * Creates quadratic or linear pentahedron
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,
243 const bool force3d = true);
245 * Creates quadratic or linear hexahedron
247 SMDS_MeshVolume* AddVolume(const SMDS_MeshNode* n1,
248 const SMDS_MeshNode* n2,
249 const SMDS_MeshNode* n3,
250 const SMDS_MeshNode* n4,
251 const SMDS_MeshNode* n5,
252 const SMDS_MeshNode* n6,
253 const SMDS_MeshNode* n7,
254 const SMDS_MeshNode* n8,
256 bool force3d = true);
258 * \brief Return U of the given node on the edge
260 double GetNodeU(const TopoDS_Edge& theEdge,
261 const SMDS_MeshNode* theNode,
264 * \brief Return node UV on face
265 * \param inFaceNode - a node of element being created located inside a face
267 gp_XY GetNodeUV(const TopoDS_Face& F,
268 const SMDS_MeshNode* n,
269 const SMDS_MeshNode* inFaceNode=0,
270 bool* check=0) const;
272 * \brief Check and fix node UV on a face
273 * \param force - check even if checks of other nodes on this face passed OK
274 * \retval bool - false if UV is bad and could not be fixed
276 bool CheckNodeUV(const TopoDS_Face& F,
277 const SMDS_MeshNode* n,
280 const bool force=false) const;
282 * \brief Check and fix node U on an edge
283 * \param force - check even if checks of other nodes on this edge passed OK
284 * \retval bool - false if U is bad and could not be fixed
286 bool CheckNodeU(const TopoDS_Edge& E,
287 const SMDS_MeshNode* n,
290 const bool force=false) const;
292 * \brief Return middle UV taking in account surface period
294 static gp_XY GetMiddleUV(const Handle(Geom_Surface)& surface,
298 * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
299 * \retval bool - return true if the face is periodic
301 * If F is Null, answer about subshape set through IsQuadraticSubMesh() or
304 bool GetNodeUVneedInFaceNode(const TopoDS_Face& F = TopoDS_Face()) const;
307 * \brief Check if shape is a degenerated edge or it's vertex
308 * \param subShape - edge or vertex index in SMESHDS
309 * \retval bool - true if subShape is a degenerated shape
311 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called
313 bool IsDegenShape(const int subShape) const
314 { return myDegenShapeIds.find( subShape ) != myDegenShapeIds.end(); }
316 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
317 * has a degenerated edges
318 * \retval bool - true if it has
320 bool HasDegeneratedEdges() const { return !myDegenShapeIds.empty(); }
323 * \brief Check if shape is a seam edge or it's vertex
324 * \param subShape - edge or vertex index in SMESHDS
325 * \retval bool - true if subShape is a seam shape
327 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
328 * Seam shape has two 2D alternative represenations on the face
330 bool IsSeamShape(const int subShape) const
331 { return mySeamShapeIds.find( subShape ) != mySeamShapeIds.end(); }
333 * \brief Check if shape is a seam edge or it's vertex
334 * \param subShape - edge or vertex
335 * \retval bool - true if subShape is a seam shape
337 * It works only if IsQuadraticSubMesh() or SetSubShape() has been called.
338 * Seam shape has two 2D alternative represenations on the face
340 bool IsSeamShape(const TopoDS_Shape& subShape) const
341 { return IsSeamShape( GetMeshDS()->ShapeToIndex( subShape )); }
343 * \brief Return true if an edge or a vertex encounters twice in face wire
344 * \param subShape - Id of edge or vertex
346 bool IsRealSeam(const int subShape) const
347 { return mySeamShapeIds.find( -subShape ) != mySeamShapeIds.end(); }
349 * \brief Return true if an edge or a vertex encounters twice in face wire
350 * \param subShape - edge or vertex
352 bool IsRealSeam(const TopoDS_Shape& subShape) const
353 { return IsRealSeam( GetMeshDS()->ShapeToIndex( subShape)); }
355 * \brief Check if the shape set through IsQuadraticSubMesh() or SetSubShape()
357 * \retval bool - true if it has
359 bool HasSeam() const { return !mySeamShapeIds.empty(); }
361 * \brief Return index of periodic parametric direction of a closed face
362 * \retval int - 1 for U, 2 for V direction
364 int GetPeriodicIndex() const { return myParIndex; }
366 * \brief Return an alternative parameter for a node on seam
368 double GetOtherParam(const double param) const;
371 * \brief Return existing or create new medium nodes between given ones
372 * \param force3d - true means node creation at the middle between the
373 * two given nodes, else node position is found on its
374 * supporting geometrical shape, if any.
376 const SMDS_MeshNode* GetMediumNode(const SMDS_MeshNode* n1,
377 const SMDS_MeshNode* n2,
380 * \brief Add a link in my data structure
382 void AddTLinkNode(const SMDS_MeshNode* n1,
383 const SMDS_MeshNode* n2,
384 const SMDS_MeshNode* n12);
386 * \brief Add many links in my data structure
388 void AddTLinkNodeMap(const TLinkNodeMap& aMap)
389 { myTLinkNodeMap.insert(aMap.begin(), aMap.end()); }
392 * Returns myTLinkNodeMap
394 const TLinkNodeMap& GetTLinkNodeMap() const { return myTLinkNodeMap; }
397 * Check mesh without geometry for: if all elements on this shape are quadratic,
398 * quadratic elements will be created.
399 * Used then generated 3D mesh without geometry.
401 enum MType{ LINEAR, QUADRATIC, COMP };
402 MType IsQuadraticMesh();
407 * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
408 * \param uv1 - UV on the seam
409 * \param uv2 - UV within a face
410 * \retval gp_Pnt2d - selected UV
412 gp_Pnt2d GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const;
416 // Forbiden copy constructor
417 SMESH_MesherHelper (const SMESH_MesherHelper& theOther) {};
419 // special map for using during creation of quadratic elements
420 TLinkNodeMap myTLinkNodeMap;
422 std::set< int > myDegenShapeIds;
423 std::set< int > mySeamShapeIds;
424 double myPar1[2], myPar2[2]; // U and V bounds of a closed periodic surface
425 int myParIndex; // bounds' index (1-U, 2-V, 3-both)
427 TopoDS_Shape myShape;
431 // to create quadratic elements
432 bool myCreateQuadratic;
433 bool mySetElemOnShape;
434 std::set< int > myOkNodePosShapes;