1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: SMESH_MesherHelper.cxx
21 // Created: 15.02.06 15:22:41
22 // Author: Sergey KUUL
23 // Copyright: Open CASCADE 2006
26 #include "SMESH_MesherHelper.hxx"
28 #include "SMDS_FacePosition.hxx"
29 #include "SMDS_EdgePosition.hxx"
30 #include "SMESH_MeshEditor.hxx"
32 #include <BRepAdaptor_Surface.hxx>
33 #include <BRepTools.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRepTools_WireExplorer.hxx>
36 #include <Geom2d_Curve.hxx>
37 #include <Geom_Curve.hxx>
38 #include <Geom_Surface.hxx>
39 #include <ShapeAnalysis.hxx>
41 #include <TopExp_Explorer.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
43 #include <TopTools_MapOfShape.hxx>
45 #include <gp_Pnt2d.hxx>
47 #include <Standard_Failure.hxx>
48 #include <Standard_ErrorHandler.hxx>
50 #include <utilities.h>
52 #define RETURN_BAD_RESULT(msg) { MESSAGE(msg); return false; }
54 //================================================================================
58 //================================================================================
60 SMESH_MesherHelper::SMESH_MesherHelper(SMESH_Mesh& theMesh)
61 : myMesh(&theMesh), myShapeID(-1), myCreateQuadratic(false)
63 mySetElemOnShape = ( ! myMesh->HasShapeToMesh() );
66 //=======================================================================
67 //function : CheckShape
69 //=======================================================================
71 bool SMESH_MesherHelper::IsQuadraticSubMesh(const TopoDS_Shape& aSh)
73 SMESHDS_Mesh* meshDS = GetMeshDS();
74 // we can create quadratic elements only if all elements
75 // created on subshapes of given shape are quadratic
76 // also we have to fill myNLinkNodeMap
77 myCreateQuadratic = true;
78 mySeamShapeIds.clear();
79 myDegenShapeIds.clear();
80 TopAbs_ShapeEnum subType( aSh.ShapeType()==TopAbs_FACE ? TopAbs_EDGE : TopAbs_FACE );
81 SMDSAbs_ElementType elemType( subType==TopAbs_FACE ? SMDSAbs_Face : SMDSAbs_Edge );
83 TopExp_Explorer exp( aSh, subType );
84 for (; exp.More() && myCreateQuadratic; exp.Next()) {
85 if ( SMESHDS_SubMesh * subMesh = meshDS->MeshElements( exp.Current() )) {
86 if ( SMDS_ElemIteratorPtr it = subMesh->GetElements() ) {
88 const SMDS_MeshElement* e = it->next();
89 if ( e->GetType() != elemType || !e->IsQuadratic() ) {
90 myCreateQuadratic = false;
95 switch ( e->NbNodes() ) {
97 AddNLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(2)); break;
99 AddNLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(3));
100 AddNLinkNode(e->GetNode(1),e->GetNode(2),e->GetNode(4));
101 AddNLinkNode(e->GetNode(2),e->GetNode(0),e->GetNode(5)); break;
103 AddNLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(4));
104 AddNLinkNode(e->GetNode(1),e->GetNode(2),e->GetNode(5));
105 AddNLinkNode(e->GetNode(2),e->GetNode(3),e->GetNode(6));
106 AddNLinkNode(e->GetNode(3),e->GetNode(0),e->GetNode(7));
109 myCreateQuadratic = false;
118 if(!myCreateQuadratic) {
119 myNLinkNodeMap.clear();
123 return myCreateQuadratic;
126 //================================================================================
128 * \brief Set geomerty to make elements on
129 * \param aSh - geomertic shape
131 //================================================================================
133 void SMESH_MesherHelper::SetSubShape(const int aShID)
135 if ( aShID == myShapeID )
138 SetSubShape( GetMeshDS()->IndexToShape( aShID ));
140 SetSubShape( TopoDS_Shape() );
143 //================================================================================
145 * \brief Set geomerty to make elements on
146 * \param aSh - geomertic shape
148 //================================================================================
150 void SMESH_MesherHelper::SetSubShape(const TopoDS_Shape& aSh)
152 if ( myShape.IsSame( aSh ))
156 mySeamShapeIds.clear();
157 myDegenShapeIds.clear();
159 if ( myShape.IsNull() ) {
163 SMESHDS_Mesh* meshDS = GetMeshDS();
164 myShapeID = meshDS->ShapeToIndex(aSh);
166 // treatment of periodic faces
167 for ( TopExp_Explorer eF( aSh, TopAbs_FACE ); eF.More(); eF.Next() )
169 const TopoDS_Face& face = TopoDS::Face( eF.Current() );
170 BRepAdaptor_Surface surface( face );
171 if ( surface.IsUPeriodic() || surface.IsVPeriodic() )
173 for (TopExp_Explorer exp( face, TopAbs_EDGE ); exp.More(); exp.Next())
175 // look for a seam edge
176 const TopoDS_Edge& edge = TopoDS::Edge( exp.Current() );
177 if ( BRep_Tool::IsClosed( edge, face )) {
178 // initialize myPar1, myPar2 and myParIndex
179 if ( mySeamShapeIds.empty() ) {
181 BRep_Tool::UVPoints( edge, face, uv1, uv2 );
182 if ( Abs( uv1.Coord(1) - uv2.Coord(1) ) < Abs( uv1.Coord(2) - uv2.Coord(2) ))
184 myParIndex = 1; // U periodic
185 myPar1 = surface.FirstUParameter();
186 myPar2 = surface.LastUParameter();
189 myParIndex = 2; // V periodic
190 myPar1 = surface.FirstVParameter();
191 myPar2 = surface.LastVParameter();
194 // store seam shape indices, negative if shape encounters twice
195 int edgeID = meshDS->ShapeToIndex( edge );
196 mySeamShapeIds.insert( IsSeamShape( edgeID ) ? -edgeID : edgeID );
197 for ( TopExp_Explorer v( edge, TopAbs_VERTEX ); v.More(); v.Next() ) {
198 int vertexID = meshDS->ShapeToIndex( v.Current() );
199 mySeamShapeIds.insert( IsSeamShape( vertexID ) ? -vertexID : vertexID );
203 // look for a degenerated edge
204 if ( BRep_Tool::Degenerated( edge )) {
205 myDegenShapeIds.insert( meshDS->ShapeToIndex( edge ));
206 for ( TopExp_Explorer v( edge, TopAbs_VERTEX ); v.More(); v.Next() )
207 myDegenShapeIds.insert( meshDS->ShapeToIndex( v.Current() ));
214 //================================================================================
216 * \brief Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
217 * \param F - the face
218 * \retval bool - return true if the face is periodic
220 //================================================================================
222 bool SMESH_MesherHelper::GetNodeUVneedInFaceNode(const TopoDS_Face& F) const
224 if ( F.IsNull() ) return !mySeamShapeIds.empty();
226 if ( !F.IsNull() && !myShape.IsNull() && myShape.IsSame( F ))
227 return !mySeamShapeIds.empty();
229 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( F );
230 if ( !aSurface.IsNull() )
231 return ( aSurface->IsUPeriodic() || aSurface->IsVPeriodic() );
236 //=======================================================================
237 //function : IsMedium
239 //=======================================================================
241 bool SMESH_MesherHelper::IsMedium(const SMDS_MeshNode* node,
242 const SMDSAbs_ElementType typeToCheck)
244 return SMESH_MeshEditor::IsMedium( node, typeToCheck );
247 //=======================================================================
248 //function : AddNLinkNode
250 //=======================================================================
252 * Auxilary function for filling myNLinkNodeMap
254 void SMESH_MesherHelper::AddNLinkNode(const SMDS_MeshNode* n1,
255 const SMDS_MeshNode* n2,
256 const SMDS_MeshNode* n12)
258 NLink link( n1, n2 );
259 if ( n1 > n2 ) link = NLink( n2, n1 );
260 // add new record to map
261 myNLinkNodeMap.insert( make_pair(link,n12));
264 //=======================================================================
266 * \brief Select UV on either of 2 pcurves of a seam edge, closest to the given UV
267 * \param uv1 - UV on the seam
268 * \param uv2 - UV within a face
269 * \retval gp_Pnt2d - selected UV
271 //=======================================================================
273 gp_Pnt2d SMESH_MesherHelper::GetUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const
275 double p1 = uv1.Coord( myParIndex );
276 double p2 = uv2.Coord( myParIndex );
277 double p3 = ( Abs( p1 - myPar1 ) < Abs( p1 - myPar2 )) ? myPar2 : myPar1;
278 if ( Abs( p2 - p1 ) > Abs( p2 - p3 ))
280 gp_Pnt2d result = uv1;
281 result.SetCoord( myParIndex, p1 );
285 //=======================================================================
287 * \brief Return node UV on face
288 * \param F - the face
289 * \param n - the node
290 * \param n2 - a node of element being created located inside a face
291 * \retval gp_XY - resulting UV
293 * Auxilary function called form GetMediumNode()
295 //=======================================================================
297 gp_XY SMESH_MesherHelper::GetNodeUV(const TopoDS_Face& F,
298 const SMDS_MeshNode* n,
299 const SMDS_MeshNode* n2) const
301 gp_Pnt2d uv( 1e100, 1e100 );
302 const SMDS_PositionPtr Pos = n->GetPosition();
303 if(Pos->GetTypeOfPosition()==SMDS_TOP_FACE)
305 // node has position on face
306 const SMDS_FacePosition* fpos =
307 static_cast<const SMDS_FacePosition*>(n->GetPosition().get());
308 uv = gp_Pnt2d(fpos->GetUParameter(),fpos->GetVParameter());
310 else if(Pos->GetTypeOfPosition()==SMDS_TOP_EDGE)
312 // node has position on edge => it is needed to find
313 // corresponding edge from face, get pcurve for this
314 // edge and recieve value from this pcurve
315 const SMDS_EdgePosition* epos =
316 static_cast<const SMDS_EdgePosition*>(n->GetPosition().get());
317 SMESHDS_Mesh* meshDS = GetMeshDS();
318 int edgeID = Pos->GetShapeId();
319 TopoDS_Edge E = TopoDS::Edge(meshDS->IndexToShape(edgeID));
321 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(E, F, f, l);
322 uv = C2d->Value( epos->GetUParameter() );
323 // for a node on a seam edge select one of UVs on 2 pcurves
324 if ( n2 && IsSeamShape( edgeID ) )
325 uv = GetUVOnSeam( uv, GetNodeUV( F, n2, 0 ));
327 else if(Pos->GetTypeOfPosition()==SMDS_TOP_VERTEX)
329 if ( int vertexID = n->GetPosition()->GetShapeId() ) {
331 const TopoDS_Vertex& V = TopoDS::Vertex(GetMeshDS()->IndexToShape(vertexID));
333 uv = BRep_Tool::Parameters( V, F );
335 catch (Standard_Failure& exc) {
339 for ( TopExp_Explorer vert(F,TopAbs_VERTEX); !ok && vert.More(); vert.Next() )
340 ok = ( V == vert.Current() );
343 MESSAGE ( "SMESH_MesherHelper::GetNodeUV(); Vertex " << vertexID
344 << " not in face " << GetMeshDS()->ShapeToIndex( F ) );
346 // get UV of a vertex closest to the node
348 gp_Pnt pn ( n->X(),n->Y(),n->Z() );
349 for ( TopExp_Explorer vert(F,TopAbs_VERTEX); !ok && vert.More(); vert.Next() ) {
350 TopoDS_Vertex curV = TopoDS::Vertex( vert.Current() );
351 gp_Pnt p = BRep_Tool::Pnt( curV );
352 double curDist = p.SquareDistance( pn );
353 if ( curDist < dist ) {
355 uv = BRep_Tool::Parameters( curV, F );
356 if ( dist < DBL_MIN ) break;
361 TopTools_ListIteratorOfListOfShape it( myMesh->GetAncestors( V ));
362 for ( ; it.More(); it.Next() ) {
363 if ( it.Value().ShapeType() == TopAbs_EDGE ) {
364 const TopoDS_Edge & edge = TopoDS::Edge( it.Value() );
366 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(edge, F, f, l);
367 if ( !C2d.IsNull() ) {
368 double u = ( V == TopExp::FirstVertex( edge ) ) ? f : l;
369 uv = C2d->Value( u );
376 if ( n2 && IsSeamShape( vertexID ) )
377 uv = GetUVOnSeam( uv, GetNodeUV( F, n2, 0 ));
383 //=======================================================================
385 * \brief Return node U on edge
386 * \param E - the Edge
387 * \param n - the node
388 * \retval double - resulting U
390 * Auxilary function called form GetMediumNode()
392 //=======================================================================
394 double SMESH_MesherHelper::GetNodeU(const TopoDS_Edge& E,
395 const SMDS_MeshNode* n)
398 const SMDS_PositionPtr Pos = n->GetPosition();
399 if(Pos->GetTypeOfPosition()==SMDS_TOP_EDGE) {
400 const SMDS_EdgePosition* epos =
401 static_cast<const SMDS_EdgePosition*>(n->GetPosition().get());
402 param = epos->GetUParameter();
404 else if(Pos->GetTypeOfPosition()==SMDS_TOP_VERTEX) {
405 SMESHDS_Mesh * meshDS = GetMeshDS();
406 int vertexID = n->GetPosition()->GetShapeId();
407 const TopoDS_Vertex& V = TopoDS::Vertex(meshDS->IndexToShape(vertexID));
408 param = BRep_Tool::Parameter( V, E );
413 //=======================================================================
414 //function : GetMediumNode
416 //=======================================================================
418 * Special function for search or creation medium node
420 const SMDS_MeshNode* SMESH_MesherHelper::GetMediumNode(const SMDS_MeshNode* n1,
421 const SMDS_MeshNode* n2,
424 TopAbs_ShapeEnum shapeType = myShape.IsNull() ? TopAbs_SHAPE : myShape.ShapeType();
426 NLink link(( n1 < n2 ? n1 : n2 ), ( n1 < n2 ? n2 : n1 ));
427 ItNLinkNode itLN = myNLinkNodeMap.find( link );
428 if ( itLN != myNLinkNodeMap.end() ) {
429 return (*itLN).second;
432 // create medium node
434 SMESHDS_Mesh* meshDS = GetMeshDS();
435 int faceID = -1, edgeID = -1;
436 const SMDS_PositionPtr Pos1 = n1->GetPosition();
437 const SMDS_PositionPtr Pos2 = n2->GetPosition();
439 if( myShape.IsNull() )
441 if( Pos1->GetTypeOfPosition()==SMDS_TOP_FACE ) {
442 faceID = Pos1->GetShapeId();
444 else if( Pos2->GetTypeOfPosition()==SMDS_TOP_FACE ) {
445 faceID = Pos2->GetShapeId();
448 if( Pos1->GetTypeOfPosition()==SMDS_TOP_EDGE ) {
449 edgeID = Pos1->GetShapeId();
451 if( Pos2->GetTypeOfPosition()==SMDS_TOP_EDGE ) {
452 edgeID = Pos2->GetShapeId();
457 // we try to create medium node using UV parameters of
458 // nodes, else - medium between corresponding 3d points
459 if(faceID>-1 || shapeType == TopAbs_FACE) {
460 // obtaining a face and 2d points for nodes
462 if( myShape.IsNull() )
463 F = TopoDS::Face(meshDS->IndexToShape(faceID));
465 F = TopoDS::Face(myShape);
469 gp_XY p1 = GetNodeUV(F,n1,n2);
470 gp_XY p2 = GetNodeUV(F,n2,n1);
472 //checking if surface is periodic
473 Handle(Geom_Surface) S = BRep_Tool::Surface(F);
474 Standard_Real UF,UL,VF,VL;
475 S->Bounds(UF,UL,VF,VL);
478 Standard_Boolean isUPeriodic = S->IsUPeriodic();
480 Standard_Real UPeriod = S->UPeriod();
481 Standard_Real p2x = p2.X()+ShapeAnalysis::AdjustByPeriod(p2.X(),p1.X(),UPeriod);
482 Standard_Real pmid = (p1.X()+p2x)/2.;
483 u = pmid+ShapeAnalysis::AdjustToPeriod(pmid,UF,UL);
486 u= (p1.X()+p2.X())/2.;
488 Standard_Boolean isVPeriodic = S->IsVPeriodic();
490 Standard_Real VPeriod = S->VPeriod();
491 Standard_Real p2y = p2.Y()+ShapeAnalysis::AdjustByPeriod(p2.Y(),p1.Y(),VPeriod);
492 Standard_Real pmid = (p1.Y()+p2y)/2.;
493 v = pmid+ShapeAnalysis::AdjustToPeriod(pmid,VF,VL);
496 v = (p1.Y()+p2.Y())/2.;
498 gp_Pnt P = S->Value(u, v);
499 n12 = meshDS->AddNode(P.X(), P.Y(), P.Z());
500 meshDS->SetNodeOnFace(n12, faceID, u, v);
501 myNLinkNodeMap.insert(NLinkNodeMap::value_type(link,n12));
504 if (edgeID>-1 || shapeType == TopAbs_EDGE) {
507 if( myShape.IsNull() )
508 E = TopoDS::Edge(meshDS->IndexToShape(edgeID));
510 E = TopoDS::Edge(myShape);
514 double p1 = GetNodeU(E,n1);
515 double p2 = GetNodeU(E,n2);
518 Handle(Geom_Curve) C = BRep_Tool::Curve(E, f, l);
521 Standard_Boolean isPeriodic = C->IsPeriodic();
524 Standard_Real Period = C->Period();
525 Standard_Real p = p2+ShapeAnalysis::AdjustByPeriod(p2,p1,Period);
526 Standard_Real pmid = (p1+p)/2.;
527 u = pmid+ShapeAnalysis::AdjustToPeriod(pmid,C->FirstParameter(),C->LastParameter());
532 gp_Pnt P = C->Value( u );
533 n12 = meshDS->AddNode(P.X(), P.Y(), P.Z());
534 meshDS->SetNodeOnEdge(n12, edgeID, u);
535 myNLinkNodeMap.insert(NLinkNodeMap::value_type(link,n12));
541 double x = ( n1->X() + n2->X() )/2.;
542 double y = ( n1->Y() + n2->Y() )/2.;
543 double z = ( n1->Z() + n2->Z() )/2.;
544 n12 = meshDS->AddNode(x,y,z);
546 meshDS->SetNodeOnEdge(n12, edgeID);
548 meshDS->SetNodeOnFace(n12, faceID);
550 meshDS->SetNodeInVolume(n12, myShapeID);
551 myNLinkNodeMap.insert(NLinkNodeMap::value_type(link,n12));
556 //=======================================================================
560 //=======================================================================
562 SMDS_MeshNode* SMESH_MesherHelper::AddNode(double x, double y, double z, int ID)
564 SMESHDS_Mesh * meshDS = GetMeshDS();
565 SMDS_MeshNode* node = 0;
567 node = meshDS->AddNodeWithID( x, y, z, ID );
569 node = meshDS->AddNode( x, y, z );
570 if ( mySetElemOnShape && myShapeID > 0 ) {
571 switch ( myShape.ShapeType() ) {
572 case TopAbs_SOLID: meshDS->SetNodeInVolume( node, myShapeID); break;
573 case TopAbs_SHELL: meshDS->SetNodeInVolume( node, myShapeID); break;
574 case TopAbs_FACE: meshDS->SetNodeOnFace( node, myShapeID); break;
575 case TopAbs_EDGE: meshDS->SetNodeOnEdge( node, myShapeID); break;
576 case TopAbs_VERTEX: meshDS->SetNodeOnVertex( node, myShapeID); break;
583 //=======================================================================
585 * Creates quadratic or linear edge
587 //=======================================================================
589 SMDS_MeshEdge* SMESH_MesherHelper::AddEdge(const SMDS_MeshNode* n1,
590 const SMDS_MeshNode* n2,
594 SMESHDS_Mesh * meshDS = GetMeshDS();
596 SMDS_MeshEdge* edge = 0;
597 if (myCreateQuadratic) {
598 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
600 edge = meshDS->AddEdgeWithID(n1, n2, n12, id);
602 edge = meshDS->AddEdge(n1, n2, n12);
606 edge = meshDS->AddEdgeWithID(n1, n2, id);
608 edge = meshDS->AddEdge(n1, n2);
611 if ( mySetElemOnShape && myShapeID > 0 )
612 meshDS->SetMeshElementOnShape( edge, myShapeID );
617 //=======================================================================
619 * Creates quadratic or linear triangle
621 //=======================================================================
623 SMDS_MeshFace* SMESH_MesherHelper::AddFace(const SMDS_MeshNode* n1,
624 const SMDS_MeshNode* n2,
625 const SMDS_MeshNode* n3,
629 SMESHDS_Mesh * meshDS = GetMeshDS();
630 SMDS_MeshFace* elem = 0;
631 if(!myCreateQuadratic) {
633 elem = meshDS->AddFaceWithID(n1, n2, n3, id);
635 elem = meshDS->AddFace(n1, n2, n3);
638 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
639 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
640 const SMDS_MeshNode* n31 = GetMediumNode(n3,n1,force3d);
643 elem = meshDS->AddFaceWithID(n1, n2, n3, n12, n23, n31, id);
645 elem = meshDS->AddFace(n1, n2, n3, n12, n23, n31);
647 if ( mySetElemOnShape && myShapeID > 0 )
648 meshDS->SetMeshElementOnShape( elem, myShapeID );
653 //=======================================================================
655 * Creates quadratic or linear quadrangle
657 //=======================================================================
659 SMDS_MeshFace* SMESH_MesherHelper::AddFace(const SMDS_MeshNode* n1,
660 const SMDS_MeshNode* n2,
661 const SMDS_MeshNode* n3,
662 const SMDS_MeshNode* n4,
666 SMESHDS_Mesh * meshDS = GetMeshDS();
667 SMDS_MeshFace* elem = 0;
668 if(!myCreateQuadratic) {
670 elem = meshDS->AddFaceWithID(n1, n2, n3, n4, id);
672 elem = meshDS->AddFace(n1, n2, n3, n4);
675 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
676 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
677 const SMDS_MeshNode* n34 = GetMediumNode(n3,n4,force3d);
678 const SMDS_MeshNode* n41 = GetMediumNode(n4,n1,force3d);
681 elem = meshDS->AddFaceWithID(n1, n2, n3, n4, n12, n23, n34, n41, id);
683 elem = meshDS->AddFace(n1, n2, n3, n4, n12, n23, n34, n41);
685 if ( mySetElemOnShape && myShapeID > 0 )
686 meshDS->SetMeshElementOnShape( elem, myShapeID );
691 //=======================================================================
693 * Creates quadratic or linear volume
695 //=======================================================================
697 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
698 const SMDS_MeshNode* n2,
699 const SMDS_MeshNode* n3,
700 const SMDS_MeshNode* n4,
701 const SMDS_MeshNode* n5,
702 const SMDS_MeshNode* n6,
706 SMESHDS_Mesh * meshDS = GetMeshDS();
707 SMDS_MeshVolume* elem = 0;
708 if(!myCreateQuadratic) {
710 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, id);
712 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6);
715 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
716 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
717 const SMDS_MeshNode* n31 = GetMediumNode(n3,n1,force3d);
719 const SMDS_MeshNode* n45 = GetMediumNode(n4,n5,force3d);
720 const SMDS_MeshNode* n56 = GetMediumNode(n5,n6,force3d);
721 const SMDS_MeshNode* n64 = GetMediumNode(n6,n4,force3d);
723 const SMDS_MeshNode* n14 = GetMediumNode(n1,n4,force3d);
724 const SMDS_MeshNode* n25 = GetMediumNode(n2,n5,force3d);
725 const SMDS_MeshNode* n36 = GetMediumNode(n3,n6,force3d);
728 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6,
729 n12, n23, n31, n45, n56, n64, n14, n25, n36, id);
731 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6,
732 n12, n23, n31, n45, n56, n64, n14, n25, n36);
734 if ( mySetElemOnShape && myShapeID > 0 )
735 meshDS->SetMeshElementOnShape( elem, myShapeID );
740 //=======================================================================
742 * Creates quadratic or linear volume
744 //=======================================================================
746 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
747 const SMDS_MeshNode* n2,
748 const SMDS_MeshNode* n3,
749 const SMDS_MeshNode* n4,
753 SMESHDS_Mesh * meshDS = GetMeshDS();
754 SMDS_MeshVolume* elem = 0;
755 if(!myCreateQuadratic) {
757 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, id);
759 elem = meshDS->AddVolume(n1, n2, n3, n4);
762 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
763 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
764 const SMDS_MeshNode* n31 = GetMediumNode(n3,n1,force3d);
766 const SMDS_MeshNode* n14 = GetMediumNode(n1,n4,force3d);
767 const SMDS_MeshNode* n24 = GetMediumNode(n2,n4,force3d);
768 const SMDS_MeshNode* n34 = GetMediumNode(n3,n4,force3d);
771 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n12, n23, n31, n14, n24, n34, id);
773 elem = meshDS->AddVolume(n1, n2, n3, n4, n12, n23, n31, n14, n24, n34);
775 if ( mySetElemOnShape && myShapeID > 0 )
776 meshDS->SetMeshElementOnShape( elem, myShapeID );
781 //=======================================================================
783 * Creates quadratic or linear pyramid
785 //=======================================================================
787 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
788 const SMDS_MeshNode* n2,
789 const SMDS_MeshNode* n3,
790 const SMDS_MeshNode* n4,
791 const SMDS_MeshNode* n5,
795 SMDS_MeshVolume* elem = 0;
796 if(!myCreateQuadratic) {
798 elem = GetMeshDS()->AddVolumeWithID(n1, n2, n3, n4, n5, id);
800 elem = GetMeshDS()->AddVolume(n1, n2, n3, n4, n5);
803 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
804 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
805 const SMDS_MeshNode* n34 = GetMediumNode(n3,n4,force3d);
806 const SMDS_MeshNode* n41 = GetMediumNode(n4,n1,force3d);
808 const SMDS_MeshNode* n15 = GetMediumNode(n1,n5,force3d);
809 const SMDS_MeshNode* n25 = GetMediumNode(n2,n5,force3d);
810 const SMDS_MeshNode* n35 = GetMediumNode(n3,n5,force3d);
811 const SMDS_MeshNode* n45 = GetMediumNode(n4,n5,force3d);
814 elem = GetMeshDS()->AddVolumeWithID ( n1, n2, n3, n4, n5,
819 elem = GetMeshDS()->AddVolume( n1, n2, n3, n4, n5,
823 if ( mySetElemOnShape && myShapeID > 0 )
824 GetMeshDS()->SetMeshElementOnShape( elem, myShapeID );
829 //=======================================================================
831 * Creates quadratic or linear hexahedron
833 //=======================================================================
835 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
836 const SMDS_MeshNode* n2,
837 const SMDS_MeshNode* n3,
838 const SMDS_MeshNode* n4,
839 const SMDS_MeshNode* n5,
840 const SMDS_MeshNode* n6,
841 const SMDS_MeshNode* n7,
842 const SMDS_MeshNode* n8,
846 SMESHDS_Mesh * meshDS = GetMeshDS();
847 SMDS_MeshVolume* elem = 0;
848 if(!myCreateQuadratic) {
850 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, n7, n8, id);
852 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6, n7, n8);
855 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
856 const SMDS_MeshNode* n23 = GetMediumNode(n2,n3,force3d);
857 const SMDS_MeshNode* n34 = GetMediumNode(n3,n4,force3d);
858 const SMDS_MeshNode* n41 = GetMediumNode(n4,n1,force3d);
860 const SMDS_MeshNode* n56 = GetMediumNode(n5,n6,force3d);
861 const SMDS_MeshNode* n67 = GetMediumNode(n6,n7,force3d);
862 const SMDS_MeshNode* n78 = GetMediumNode(n7,n8,force3d);
863 const SMDS_MeshNode* n85 = GetMediumNode(n8,n5,force3d);
865 const SMDS_MeshNode* n15 = GetMediumNode(n1,n5,force3d);
866 const SMDS_MeshNode* n26 = GetMediumNode(n2,n6,force3d);
867 const SMDS_MeshNode* n37 = GetMediumNode(n3,n7,force3d);
868 const SMDS_MeshNode* n48 = GetMediumNode(n4,n8,force3d);
871 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, n7, n8,
872 n12, n23, n34, n41, n56, n67,
873 n78, n85, n15, n26, n37, n48, id);
875 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6, n7, n8,
876 n12, n23, n34, n41, n56, n67,
877 n78, n85, n15, n26, n37, n48);
879 if ( mySetElemOnShape && myShapeID > 0 )
880 meshDS->SetMeshElementOnShape( elem, myShapeID );
885 //=======================================================================
887 * \brief Load nodes bound to face into a map of node columns
888 * \param theParam2ColumnMap - map of node columns to fill
889 * \param theFace - the face on which nodes are searched for
890 * \param theBaseEdge - the edge nodes of which are columns' bases
891 * \param theMesh - the mesh containing nodes
892 * \retval bool - false if something is wrong
894 * The key of the map is a normalized parameter of each
895 * base node on theBaseEdge.
896 * This method works in supposition that nodes on the face
897 * forms a rectangular grid and elements can be quardrangles or triangles
899 //=======================================================================
901 bool SMESH_MesherHelper::LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
902 const TopoDS_Face& theFace,
903 const TopoDS_Edge& theBaseEdge,
904 SMESHDS_Mesh* theMesh)
906 // get vertices of theBaseEdge
907 TopoDS_Vertex vfb, vlb, vft; // first and last, bottom and top vertices
908 TopoDS_Edge eFrw = TopoDS::Edge( theBaseEdge.Oriented( TopAbs_FORWARD ));
909 TopExp::Vertices( eFrw, vfb, vlb );
911 // find the other edges of theFace and orientation of e1
912 TopoDS_Edge e1, e2, eTop;
913 bool rev1, CumOri = false;
914 TopExp_Explorer exp( theFace, TopAbs_EDGE );
916 for ( ; exp.More(); exp.Next() ) {
917 if ( ++nbEdges > 4 ) {
918 return false; // more than 4 edges in theFace
920 TopoDS_Edge e = TopoDS::Edge( exp.Current() );
921 if ( theBaseEdge.IsSame( e ))
923 TopoDS_Vertex vCommon;
924 if ( !TopExp::CommonVertex( theBaseEdge, e, vCommon ))
926 else if ( vCommon.IsSame( vfb )) {
928 vft = TopExp::LastVertex( e1, CumOri );
929 rev1 = vfb.IsSame( vft );
931 vft = TopExp::FirstVertex( e1, CumOri );
937 return false; // less than 4 edges in theFace
939 if ( e2.IsNull() && vfb.IsSame( vlb ))
942 // submeshes corresponding to shapes
943 SMESHDS_SubMesh* smFace = theMesh->MeshElements( theFace );
944 SMESHDS_SubMesh* smb = theMesh->MeshElements( theBaseEdge );
945 SMESHDS_SubMesh* smt = theMesh->MeshElements( eTop );
946 SMESHDS_SubMesh* sm1 = theMesh->MeshElements( e1 );
947 SMESHDS_SubMesh* sm2 = theMesh->MeshElements( e2 );
948 SMESHDS_SubMesh* smVfb = theMesh->MeshElements( vfb );
949 SMESHDS_SubMesh* smVlb = theMesh->MeshElements( vlb );
950 SMESHDS_SubMesh* smVft = theMesh->MeshElements( vft );
951 if (!smFace || !smb || !smt || !sm1 || !sm2 || !smVfb || !smVlb || !smVft ) {
952 RETURN_BAD_RESULT( "NULL submesh " <<smFace<<" "<<smb<<" "<<smt<<" "<<
953 sm1<<" "<<sm2<<" "<<smVfb<<" "<<smVlb<<" "<<smVft);
955 if ( smb->NbNodes() != smt->NbNodes() || sm1->NbNodes() != sm2->NbNodes() ) {
956 RETURN_BAD_RESULT(" Diff nb of nodes on opposite edges" );
958 if (smVfb->NbNodes() != 1 || smVlb->NbNodes() != 1 || smVft->NbNodes() != 1) {
959 RETURN_BAD_RESULT("Empty submesh of vertex");
961 // define whether mesh is quadratic
962 bool isQuadraticMesh = false;
963 SMDS_ElemIteratorPtr eIt = smFace->GetElements();
964 if ( !eIt->more() ) {
965 RETURN_BAD_RESULT("No elements on the face");
967 const SMDS_MeshElement* e = eIt->next();
968 isQuadraticMesh = e->IsQuadratic();
970 if ( sm1->NbNodes() * smb->NbNodes() != smFace->NbNodes() ) {
971 // check quadratic case
972 if ( isQuadraticMesh ) {
973 // what if there are quadrangles and triangles mixed?
974 // int n1 = sm1->NbNodes()/2;
975 // int n2 = smb->NbNodes()/2;
976 // int n3 = sm1->NbNodes() - n1;
977 // int n4 = smb->NbNodes() - n2;
978 // int nf = sm1->NbNodes()*smb->NbNodes() - n3*n4;
979 // if( nf != smFace->NbNodes() ) {
980 // MESSAGE( "Wrong nb face nodes: " <<
981 // sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
986 RETURN_BAD_RESULT( "Wrong nb face nodes: " <<
987 sm1->NbNodes()<<" "<<smb->NbNodes()<<" "<<smFace->NbNodes());
991 int vsize = sm1->NbNodes() + 2;
992 int hsize = smb->NbNodes() + 2;
993 if(isQuadraticMesh) {
994 vsize = vsize - sm1->NbNodes()/2 -1;
995 hsize = hsize - smb->NbNodes()/2 -1;
998 // load nodes from theBaseEdge
1000 std::set<const SMDS_MeshNode*> loadedNodes;
1001 const SMDS_MeshNode* nullNode = 0;
1003 std::vector<const SMDS_MeshNode*> & nVecf = theParam2ColumnMap[ 0.];
1004 nVecf.resize( vsize, nullNode );
1005 loadedNodes.insert( nVecf[ 0 ] = smVfb->GetNodes()->next() );
1007 std::vector<const SMDS_MeshNode*> & nVecl = theParam2ColumnMap[ 1.];
1008 nVecl.resize( vsize, nullNode );
1009 loadedNodes.insert( nVecl[ 0 ] = smVlb->GetNodes()->next() );
1012 BRep_Tool::Range( eFrw, f, l );
1013 double range = l - f;
1014 SMDS_NodeIteratorPtr nIt = smb->GetNodes();
1015 const SMDS_MeshNode* node;
1016 while ( nIt->more() ) {
1018 if(IsMedium(node, SMDSAbs_Edge))
1020 const SMDS_EdgePosition* pos =
1021 dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
1025 double u = ( pos->GetUParameter() - f ) / range;
1026 std::vector<const SMDS_MeshNode*> & nVec = theParam2ColumnMap[ u ];
1027 nVec.resize( vsize, nullNode );
1028 loadedNodes.insert( nVec[ 0 ] = node );
1030 if ( theParam2ColumnMap.size() != hsize ) {
1031 RETURN_BAD_RESULT( "Wrong node positions on theBaseEdge" );
1034 // load nodes from e1
1036 std::map< double, const SMDS_MeshNode*> sortedNodes; // sort by param on edge
1037 nIt = sm1->GetNodes();
1038 while ( nIt->more() ) {
1042 const SMDS_EdgePosition* pos =
1043 dynamic_cast<const SMDS_EdgePosition*>( node->GetPosition().get() );
1047 sortedNodes.insert( std::make_pair( pos->GetUParameter(), node ));
1049 loadedNodes.insert( nVecf[ vsize - 1 ] = smVft->GetNodes()->next() );
1050 std::map< double, const SMDS_MeshNode*>::iterator u_n = sortedNodes.begin();
1051 int row = rev1 ? vsize - 1 : 0;
1052 int dRow = rev1 ? -1 : +1;
1053 for ( ; u_n != sortedNodes.end(); u_n++ ) {
1055 loadedNodes.insert( nVecf[ row ] = u_n->second );
1058 // try to load the rest nodes
1060 // get all faces from theFace
1061 TIDSortedElemSet allFaces, foundFaces;
1062 eIt = smFace->GetElements();
1063 while ( eIt->more() ) {
1064 const SMDS_MeshElement* e = eIt->next();
1065 if ( e->GetType() == SMDSAbs_Face )
1066 allFaces.insert( e );
1068 // Starting from 2 neighbour nodes on theBaseEdge, look for a face
1069 // the nodes belong to, and between the nodes of the found face,
1070 // look for a not loaded node considering this node to be the next
1071 // in a column of the starting second node. Repeat, starting
1072 // from nodes next to the previous starting nodes in their columns,
1073 // and so on while a face can be found. Then go the the next pair
1074 // of nodes on theBaseEdge.
1075 TParam2ColumnMap::iterator par_nVec_1 = theParam2ColumnMap.begin();
1076 TParam2ColumnMap::iterator par_nVec_2 = par_nVec_1;
1079 for ( par_nVec_2++; par_nVec_2 != theParam2ColumnMap.end(); par_nVec_1++, par_nVec_2++ ) {
1082 const SMDS_MeshNode* n1 = par_nVec_1->second[ row ];
1083 const SMDS_MeshNode* n2 = par_nVec_2->second[ row ];
1084 const SMDS_MeshElement* face = 0;
1085 bool lastColOnClosedFace = ( nVecf[ row ] == n2 );
1087 // look for a face by 2 nodes
1088 face = SMESH_MeshEditor::FindFaceInSet( n1, n2, allFaces, foundFaces );
1090 int nbFaceNodes = face->NbNodes();
1091 if ( face->IsQuadratic() )
1093 if ( nbFaceNodes>4 ) {
1094 RETURN_BAD_RESULT(" Too many nodes in a face: " << nbFaceNodes );
1096 // look for a not loaded node of the <face>
1098 const SMDS_MeshNode* n3 = 0; // a node defferent from n1 and n2
1099 for ( int i = 0; i < nbFaceNodes && !found; ++i ) {
1100 node = face->GetNode( i );
1101 found = loadedNodes.insert( node ).second;
1102 if ( !found && node != n1 && node != n2 )
1105 if ( lastColOnClosedFace && row + 1 < vsize ) {
1106 node = nVecf[ row + 1 ];
1107 found = ( face->GetNodeIndex( node ) >= 0 );
1110 if ( ++row > vsize - 1 ) {
1111 RETURN_BAD_RESULT( "Too many nodes in column "<< col <<": "<< row+1);
1113 par_nVec_2->second[ row ] = node;
1114 foundFaces.insert( face );
1116 if ( nbFaceNodes==4 ) {
1117 n1 = par_nVec_1->second[ row ];
1120 else if ( nbFaceNodes==3 && n3 == par_nVec_1->second[ row + 1 ] ) {
1124 RETURN_BAD_RESULT( "Not quad mesh, column "<< col );
1128 while ( face && n1 && n2 );
1130 if ( row < vsize - 1 ) {
1131 MESSAGE( "Too few nodes in column "<< col <<": "<< row+1);
1132 MESSAGE( "Base node 1: "<< par_nVec_1->second[0]);
1133 MESSAGE( "Base node 2: "<< par_nVec_2->second[0]);
1134 if ( n1 ) { MESSAGE( "Current node 1: "<< n1); }
1135 else { MESSAGE( "Current node 1: NULL"); }
1136 if ( n2 ) { MESSAGE( "Current node 2: "<< n2); }
1137 else { MESSAGE( "Current node 2: NULL"); }
1138 MESSAGE( "first base node: "<< theParam2ColumnMap.begin()->second[0]);
1139 MESSAGE( "last base node: "<< theParam2ColumnMap.rbegin()->second[0]);
1142 } // loop on columns
1147 //=======================================================================
1149 * Check mesh without geometry for: if all elements on this shape are quadratic,
1150 * quadratic elements will be created.
1151 * Used then generated 3D mesh without geometry.
1153 //=======================================================================
1155 SMESH_MesherHelper:: MType SMESH_MesherHelper::IsQuadraticMesh()
1157 int NbAllEdgsAndFaces=0;
1158 int NbQuadFacesAndEdgs=0;
1159 int NbFacesAndEdges=0;
1160 //All faces and edges
1161 NbAllEdgsAndFaces = myMesh->NbEdges() + myMesh->NbFaces();
1163 //Quadratic faces and edges
1164 NbQuadFacesAndEdgs = myMesh->NbEdges(ORDER_QUADRATIC) + myMesh->NbFaces(ORDER_QUADRATIC);
1166 //Linear faces and edges
1167 NbFacesAndEdges = myMesh->NbEdges(ORDER_LINEAR) + myMesh->NbFaces(ORDER_LINEAR);
1169 if (NbAllEdgsAndFaces == NbQuadFacesAndEdgs) {
1171 return SMESH_MesherHelper::QUADRATIC;
1173 else if (NbAllEdgsAndFaces == NbFacesAndEdges) {
1175 return SMESH_MesherHelper::LINEAR;
1178 //Mesh with both type of elements
1179 return SMESH_MesherHelper::COMP;
1182 //=======================================================================
1184 * \brief Return an alternative parameter for a node on seam
1186 //=======================================================================
1188 double SMESH_MesherHelper::GetOtherParam(const double param) const
1190 return fabs(param-myPar1) < fabs(param-myPar2) ? myPar2 : myPar1;