1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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.cxx
24 // Created: 15.02.06 15:22:41
25 // Author: Sergey KUUL
27 #include "SMESH_MesherHelper.hxx"
29 #include "SMDS_EdgePosition.hxx"
30 #include "SMDS_FaceOfNodes.hxx"
31 #include "SMDS_FacePosition.hxx"
32 #include "SMDS_IteratorOnIterators.hxx"
33 #include "SMDS_VolumeTool.hxx"
34 #include "SMESH_Block.hxx"
35 #include "SMESH_HypoFilter.hxx"
36 #include "SMESH_MeshAlgos.hxx"
37 #include "SMESH_ProxyMesh.hxx"
38 #include "SMESH_subMesh.hxx"
40 #include <BRepAdaptor_Curve.hxx>
41 #include <BRepAdaptor_Surface.hxx>
42 #include <BRepTools.hxx>
43 #include <BRep_Tool.hxx>
44 #include <Geom2d_Curve.hxx>
45 #include <GeomAPI_ProjectPointOnCurve.hxx>
46 #include <GeomAPI_ProjectPointOnSurf.hxx>
47 #include <Geom_Curve.hxx>
48 #include <Geom_RectangularTrimmedSurface.hxx>
49 #include <Geom_Surface.hxx>
50 #include <ShapeAnalysis.hxx>
52 #include <TopExp_Explorer.hxx>
53 #include <TopTools_ListIteratorOfListOfShape.hxx>
54 #include <TopTools_MapIteratorOfMapOfShape.hxx>
55 #include <TopTools_MapOfShape.hxx>
58 #include <gp_Pnt2d.hxx>
59 #include <gp_Trsf.hxx>
61 #include <Standard_Failure.hxx>
62 #include <Standard_ErrorHandler.hxx>
64 #include <utilities.h>
70 #define RETURN_BAD_RESULT(msg) { MESSAGE(msg); return false; }
74 inline SMESH_TNodeXYZ XYZ(const SMDS_MeshNode* n) { return SMESH_TNodeXYZ(n); }
76 enum { U_periodic = 1, V_periodic = 2 };
79 //================================================================================
83 //================================================================================
85 SMESH_MesherHelper::SMESH_MesherHelper(SMESH_Mesh& theMesh)
89 myCreateQuadratic(false),
90 myCreateBiQuadratic(false),
91 myFixNodeParameters(false)
93 myPar1[0] = myPar2[0] = myPar1[1] = myPar2[1] = 0;
94 mySetElemOnShape = ( ! myMesh->HasShapeToMesh() );
97 //=======================================================================
98 //function : ~SMESH_MesherHelper
100 //=======================================================================
102 SMESH_MesherHelper::~SMESH_MesherHelper()
105 TID2ProjectorOnSurf::iterator i_proj = myFace2Projector.begin();
106 for ( ; i_proj != myFace2Projector.end(); ++i_proj )
107 delete i_proj->second;
110 TID2ProjectorOnCurve::iterator i_proj = myEdge2Projector.begin();
111 for ( ; i_proj != myEdge2Projector.end(); ++i_proj )
112 delete i_proj->second;
116 //=======================================================================
117 //function : IsQuadraticSubMesh
118 //purpose : Check submesh for given shape: if all elements on this shape
119 // are quadratic, quadratic elements will be created.
120 // Also fill myTLinkNodeMap
121 //=======================================================================
123 bool SMESH_MesherHelper::IsQuadraticSubMesh(const TopoDS_Shape& aSh)
125 SMESHDS_Mesh* meshDS = GetMeshDS();
126 // we can create quadratic elements only if all elements
127 // created on sub-shapes of given shape are quadratic
128 // also we have to fill myTLinkNodeMap
129 myCreateQuadratic = true;
130 mySeamShapeIds.clear();
131 myDegenShapeIds.clear();
132 TopAbs_ShapeEnum subType( aSh.ShapeType()==TopAbs_FACE ? TopAbs_EDGE : TopAbs_FACE );
133 if ( aSh.ShapeType()==TopAbs_COMPOUND )
135 TopoDS_Iterator subIt( aSh );
137 subType = ( subIt.Value().ShapeType()==TopAbs_FACE ) ? TopAbs_EDGE : TopAbs_FACE;
139 SMDSAbs_ElementType elemType( subType==TopAbs_FACE ? SMDSAbs_Face : SMDSAbs_Edge );
142 //int nbOldLinks = myTLinkNodeMap.size();
144 if ( !myMesh->HasShapeToMesh() )
146 if (( myCreateQuadratic = myMesh->NbFaces( ORDER_QUADRATIC )))
148 SMDS_FaceIteratorPtr fIt = meshDS->facesIterator();
149 while ( fIt->more() )
150 AddTLinks( static_cast< const SMDS_MeshFace* >( fIt->next() ));
155 TopExp_Explorer exp( aSh, subType );
156 TopTools_MapOfShape checkedSubShapes;
157 for (; exp.More() && myCreateQuadratic; exp.Next()) {
158 if ( !checkedSubShapes.Add( exp.Current() ))
159 continue; // needed if aSh is compound of solids
160 if ( SMESHDS_SubMesh * subMesh = meshDS->MeshElements( exp.Current() )) {
161 if ( SMDS_ElemIteratorPtr it = subMesh->GetElements() ) {
163 const SMDS_MeshElement* e = it->next();
164 if ( e->GetType() != elemType || !e->IsQuadratic() ) {
165 myCreateQuadratic = false;
170 switch ( e->NbCornerNodes() ) {
172 AddTLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(2)); break;
174 AddTLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(3));
175 AddTLinkNode(e->GetNode(1),e->GetNode(2),e->GetNode(4));
176 AddTLinkNode(e->GetNode(2),e->GetNode(0),e->GetNode(5)); break;
178 AddTLinkNode(e->GetNode(0),e->GetNode(1),e->GetNode(4));
179 AddTLinkNode(e->GetNode(1),e->GetNode(2),e->GetNode(5));
180 AddTLinkNode(e->GetNode(2),e->GetNode(3),e->GetNode(6));
181 AddTLinkNode(e->GetNode(3),e->GetNode(0),e->GetNode(7));
184 myCreateQuadratic = false;
194 // if ( nbOldLinks == myTLinkNodeMap.size() ) -- 0023068
195 if ( myTLinkNodeMap.empty() )
196 myCreateQuadratic = false;
198 if ( !myCreateQuadratic )
199 myTLinkNodeMap.clear();
203 return myCreateQuadratic;
206 //=======================================================================
207 //function : SetSubShape
208 //purpose : Set geometry to make elements on
209 //=======================================================================
211 void SMESH_MesherHelper::SetSubShape(const int aShID)
213 if ( aShID == myShapeID )
216 SetSubShape( GetMeshDS()->IndexToShape( aShID ));
218 SetSubShape( TopoDS_Shape() );
221 //=======================================================================
222 //function : SetSubShape
223 //purpose : Set geometry to create elements on
224 //=======================================================================
226 void SMESH_MesherHelper::SetSubShape(const TopoDS_Shape& aSh)
228 if ( myShape.IsSame( aSh ))
232 mySeamShapeIds.clear();
233 myDegenShapeIds.clear();
235 if ( myShape.IsNull() ) {
239 SMESHDS_Mesh* meshDS = GetMeshDS();
240 myShapeID = meshDS->ShapeToIndex(aSh);
243 // treatment of periodic faces
244 for ( TopExp_Explorer eF( aSh, TopAbs_FACE ); eF.More(); eF.Next() )
246 const TopoDS_Face& face = TopoDS::Face( eF.Current() );
247 BRepAdaptor_Surface surf( face, false );
248 if ( surf.IsUPeriodic() || surf.IsUClosed() ) {
249 myParIndex |= U_periodic;
250 myPar1[0] = surf.FirstUParameter();
251 myPar2[0] = surf.LastUParameter();
253 if ( surf.IsVPeriodic() || surf.IsVClosed() ) {
254 myParIndex |= V_periodic;
255 myPar1[1] = surf.FirstVParameter();
256 myPar2[1] = surf.LastVParameter();
260 for (TopExp_Explorer exp( face, TopAbs_EDGE ); exp.More(); exp.Next())
262 // look for a "seam" edge, a real seam or an edge on period boundary
263 TopoDS_Edge edge = TopoDS::Edge( exp.Current() );
264 const int edgeID = meshDS->ShapeToIndex( edge );
267 BRep_Tool::UVPoints( edge, face, uv1, uv2 );
268 const double du = Abs( uv1.Coord(1) - uv2.Coord(1) );
269 const double dv = Abs( uv1.Coord(2) - uv2.Coord(2) );
271 bool isSeam = BRep_Tool::IsClosed( edge, face );
272 if ( isSeam ) // real seam - having two pcurves on face
274 // pcurve can lie not on pediod boundary (22582, mesh_Quadratic_01/C9)
277 double u1 = uv1.Coord(1);
279 BRep_Tool::UVPoints( edge, face, uv1, uv2 );
280 double u2 = uv1.Coord(1);
281 myPar1[0] = Min( u1, u2 );
282 myPar2[0] = Max( u1, u2 );
286 double v1 = uv1.Coord(2);
288 BRep_Tool::UVPoints( edge, face, uv1, uv2 );
289 double v2 = uv1.Coord(2);
290 myPar1[1] = Min( v1, v2 );
291 myPar2[1] = Max( v1, v2 );
294 else //if ( !isSeam )
296 // one pcurve but on period boundary (22772, mesh_Quadratic_01/D1)
297 if (( myParIndex & U_periodic ) && du < Precision::PConfusion() )
299 isSeam = ( Abs( uv1.Coord(1) - myPar1[0] ) < Precision::PConfusion() ||
300 Abs( uv1.Coord(1) - myPar2[0] ) < Precision::PConfusion() );
302 else if (( myParIndex & V_periodic ) && dv < Precision::PConfusion() )
304 isSeam = ( Abs( uv1.Coord(2) - myPar1[1] ) < Precision::PConfusion() ||
305 Abs( uv1.Coord(2) - myPar2[1] ) < Precision::PConfusion() );
307 if ( isSeam ) // vertices are on period boundary, check a middle point (23032)
309 double f,l, r = 0.2345;
310 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface( edge, face, f, l );
317 uv2 = C2d->Value( f * r + l * ( 1.-r ));
318 if ( du < Precision::PConfusion() )
319 isSeam = ( Abs( uv1.Coord(1) - uv2.Coord(1) ) < Precision::PConfusion() );
321 isSeam = ( Abs( uv1.Coord(2) - uv2.Coord(2) ) < Precision::PConfusion() );
327 // store seam shape indices, negative if shape encounters twice ('real seam')
328 mySeamShapeIds.insert( IsSeamShape( edgeID ) ? -edgeID : edgeID );
329 for ( TopExp_Explorer v( edge, TopAbs_VERTEX ); v.More(); v.Next() ) {
330 int vertexID = meshDS->ShapeToIndex( v.Current() );
331 mySeamShapeIds.insert( IsSeamShape( vertexID ) ? -vertexID : vertexID );
335 // look for a degenerated edge
336 if ( SMESH_Algo::isDegenerated( edge )) {
337 myDegenShapeIds.insert( edgeID );
338 for ( TopExp_Explorer v( edge, TopAbs_VERTEX ); v.More(); v.Next() )
339 myDegenShapeIds.insert( meshDS->ShapeToIndex( v.Current() ));
341 if ( !BRep_Tool::SameParameter( edge ) ||
342 !BRep_Tool::SameRange( edge ))
344 setPosOnShapeValidity( edgeID, false );
350 //=======================================================================
351 //function : GetNodeUVneedInFaceNode
352 //purpose : Check if inFaceNode argument is necessary for call GetNodeUV(F,..)
353 // Return true if the face is periodic.
354 // If F is Null, answer about sub-shape set through IsQuadraticSubMesh() or
356 //=======================================================================
358 bool SMESH_MesherHelper::GetNodeUVneedInFaceNode(const TopoDS_Face& F) const
360 if ( F.IsNull() ) return !mySeamShapeIds.empty();
362 if ( !F.IsNull() && !myShape.IsNull() && myShape.IsSame( F ))
363 return !mySeamShapeIds.empty();
366 Handle(Geom_Surface) aSurface = BRep_Tool::Surface( F,loc );
367 if ( !aSurface.IsNull() )
368 return ( aSurface->IsUPeriodic() || aSurface->IsVPeriodic() );
373 //=======================================================================
374 //function : IsMedium
376 //=======================================================================
378 bool SMESH_MesherHelper::IsMedium(const SMDS_MeshNode* node,
379 const SMDSAbs_ElementType typeToCheck)
381 return SMESH_MeshEditor::IsMedium( node, typeToCheck );
384 //=======================================================================
385 //function : GetSubShapeByNode
386 //purpose : Return support shape of a node
387 //=======================================================================
389 TopoDS_Shape SMESH_MesherHelper::GetSubShapeByNode(const SMDS_MeshNode* node,
390 const SMESHDS_Mesh* meshDS)
392 int shapeID = node ? node->getshapeId() : 0;
393 if ( 0 < shapeID && shapeID <= meshDS->MaxShapeIndex() )
394 return meshDS->IndexToShape( shapeID );
396 return TopoDS_Shape();
400 //=======================================================================
401 //function : AddTLinkNode
402 //purpose : add a link in my data structure
403 //=======================================================================
405 void SMESH_MesherHelper::AddTLinkNode(const SMDS_MeshNode* n1,
406 const SMDS_MeshNode* n2,
407 const SMDS_MeshNode* n12)
409 // add new record to map
410 SMESH_TLink link( n1, n2 );
411 myTLinkNodeMap.insert( make_pair(link,n12));
414 //================================================================================
416 * \brief Add quadratic links of edge to own data structure
418 //================================================================================
420 bool SMESH_MesherHelper::AddTLinks(const SMDS_MeshEdge* edge)
422 if ( edge && edge->IsQuadratic() )
423 AddTLinkNode(edge->GetNode(0), edge->GetNode(1), edge->GetNode(2));
429 //================================================================================
431 * \brief Add quadratic links of face to own data structure
433 //================================================================================
435 bool SMESH_MesherHelper::AddTLinks(const SMDS_MeshFace* f)
439 switch ( f->NbNodes() ) {
441 // myMapWithCentralNode.insert
442 // ( make_pair( TBiQuad( f->GetNode(0),f->GetNode(1),f->GetNode(2) ),
444 // break; -- add medium nodes as well
446 AddTLinkNode(f->GetNode(0),f->GetNode(1),f->GetNode(3));
447 AddTLinkNode(f->GetNode(1),f->GetNode(2),f->GetNode(4));
448 AddTLinkNode(f->GetNode(2),f->GetNode(0),f->GetNode(5)); break;
451 // myMapWithCentralNode.insert
452 // ( make_pair( TBiQuad( f->GetNode(0),f->GetNode(1),f->GetNode(2),f->GetNode(3) ),
454 // break; -- add medium nodes as well
456 AddTLinkNode(f->GetNode(0),f->GetNode(1),f->GetNode(4));
457 AddTLinkNode(f->GetNode(1),f->GetNode(2),f->GetNode(5));
458 AddTLinkNode(f->GetNode(2),f->GetNode(3),f->GetNode(6));
459 AddTLinkNode(f->GetNode(3),f->GetNode(0),f->GetNode(7)); break;
466 //================================================================================
468 * \brief Add quadratic links of volume to own data structure
470 //================================================================================
472 bool SMESH_MesherHelper::AddTLinks(const SMDS_MeshVolume* volume)
474 if ( volume->IsQuadratic() )
476 SMDS_VolumeTool vTool( volume );
477 const SMDS_MeshNode** nodes = vTool.GetNodes();
479 for ( int iF = 1; iF < vTool.NbFaces(); ++iF )
481 const int nbN = vTool.NbFaceNodes( iF );
482 const int* iNodes = vTool.GetFaceNodesIndices( iF );
483 for ( int i = 0; i < nbN; )
485 int iN1 = iNodes[i++];
486 int iN12 = iNodes[i++];
488 if ( iN1 > iN2 ) std::swap( iN1, iN2 );
489 int linkID = iN1 * vTool.NbNodes() + iN2;
490 pair< set<int>::iterator, bool > it_isNew = addedLinks.insert( linkID );
491 if ( it_isNew.second )
492 AddTLinkNode( nodes[iN1], nodes[iN2], nodes[iN12] );
494 addedLinks.erase( it_isNew.first ); // each link encounters only twice
496 if ( vTool.NbNodes() == 27 )
498 const SMDS_MeshNode* nFCenter = nodes[ vTool.GetCenterNodeIndex( iF )];
499 if ( nFCenter->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE )
500 myMapWithCentralNode.insert
501 ( make_pair( TBiQuad( nodes[ iNodes[0]], nodes[ iNodes[1]],
502 nodes[ iNodes[2]], nodes[ iNodes[3]] ),
511 //================================================================================
513 * \brief Return true if position of nodes on the shape hasn't yet been checked or
514 * the positions proved to be invalid
516 //================================================================================
518 bool SMESH_MesherHelper::toCheckPosOnShape(int shapeID ) const
520 map< int,bool >::const_iterator id_ok = myNodePosShapesValidity.find( shapeID );
521 return ( id_ok == myNodePosShapesValidity.end() || !id_ok->second );
524 //================================================================================
526 * \brief Set validity of positions of nodes on the shape.
527 * Once set, validity is not changed
529 //================================================================================
531 void SMESH_MesherHelper::setPosOnShapeValidity(int shapeID, bool ok ) const
533 std::map< int,bool >::iterator sh_ok =
534 ((SMESH_MesherHelper*)this)->myNodePosShapesValidity.insert( make_pair( shapeID, ok)).first;
539 //=======================================================================
540 //function : ToFixNodeParameters
541 //purpose : Enables fixing node parameters on EDGEs and FACEs in
542 // GetNodeU(...,check=true), GetNodeUV(...,check=true), CheckNodeUV() and
543 // CheckNodeU() in case if a node lies on a shape set via SetSubShape().
545 //=======================================================================
547 void SMESH_MesherHelper::ToFixNodeParameters(bool toFix)
549 myFixNodeParameters = toFix;
553 //=======================================================================
554 //function : getUVOnSeam
555 //purpose : Select UV on either of 2 pcurves of a seam edge, closest to the given UV
556 //=======================================================================
558 gp_Pnt2d SMESH_MesherHelper::getUVOnSeam( const gp_Pnt2d& uv1, const gp_Pnt2d& uv2 ) const
560 gp_Pnt2d result = uv1;
561 for ( int i = U_periodic; i <= V_periodic ; ++i )
563 if ( myParIndex & i )
565 double p1 = uv1.Coord( i );
566 double dp1 = Abs( p1-myPar1[i-1]), dp2 = Abs( p1-myPar2[i-1]);
567 if ( myParIndex == i ||
568 dp1 < ( myPar2[i-1] - myPar1[i-1] ) / 100. ||
569 dp2 < ( myPar2[i-1] - myPar1[i-1] ) / 100. )
571 double p2 = uv2.Coord( i );
572 double p1Alt = ( dp1 < dp2 ) ? myPar2[i-1] : myPar1[i-1];
573 if ( Abs( p2 - p1 ) > Abs( p2 - p1Alt ))
574 result.SetCoord( i, p1Alt );
581 //=======================================================================
582 //function : GetNodeUV
583 //purpose : Return node UV on face
584 //=======================================================================
586 gp_XY SMESH_MesherHelper::GetNodeUV(const TopoDS_Face& F,
587 const SMDS_MeshNode* n,
588 const SMDS_MeshNode* n2,
591 gp_Pnt2d uv( Precision::Infinite(), Precision::Infinite() );
593 const SMDS_PositionPtr Pos = n->GetPosition();
595 if ( Pos->GetTypeOfPosition() == SMDS_TOP_FACE )
597 // node has position on face
598 const SMDS_FacePosition* fpos = static_cast<const SMDS_FacePosition*>( Pos );
599 uv.SetCoord( fpos->GetUParameter(), fpos->GetVParameter() );
601 uvOK = CheckNodeUV( F, n, uv.ChangeCoord(), 2.*getFaceMaxTol( F )); // 2. from 22830
603 else if ( Pos->GetTypeOfPosition() == SMDS_TOP_EDGE )
605 // node has position on EDGE => it is needed to find
606 // corresponding EDGE from FACE, get pcurve for this
607 // EDGE and retrieve value from this pcurve
608 const SMDS_EdgePosition* epos = static_cast<const SMDS_EdgePosition*>( Pos );
609 const int edgeID = n->getshapeId();
610 const TopoDS_Edge& E = TopoDS::Edge( GetMeshDS()->IndexToShape( edgeID ));
611 double f, l, u = epos->GetUParameter();
612 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface( E, F, f, l );
613 bool validU = ( !C2d.IsNull() && ( f < u ) && ( u < l ));
614 if ( validU ) uv = C2d->Value( u );
615 else uv.SetCoord( Precision::Infinite(),0.);
616 if ( check || !validU )
617 uvOK = CheckNodeUV( F, n, uv.ChangeCoord(), 2.*getFaceMaxTol( F ),/*force=*/ !validU );
619 // for a node on a seam EDGE select one of UVs on 2 pcurves
620 if ( n2 && IsSeamShape( edgeID ))
622 uv = getUVOnSeam( uv, GetNodeUV( F, n2, 0, check ));
625 { // adjust uv to period
627 Handle(Geom_Surface) S = BRep_Tool::Surface(F,loc);
628 Standard_Boolean isUPeriodic = S->IsUPeriodic();
629 Standard_Boolean isVPeriodic = S->IsVPeriodic();
631 if ( isUPeriodic || isVPeriodic ) {
632 Standard_Real UF,UL,VF,VL;
633 S->Bounds(UF,UL,VF,VL);
634 if ( isUPeriodic ) newUV.SetX( uv.X() + ShapeAnalysis::AdjustToPeriod(uv.X(),UF,UL));
635 if ( isVPeriodic ) newUV.SetY( uv.Y() + ShapeAnalysis::AdjustToPeriod(uv.Y(),VF,VL));
639 gp_Pnt2d uv2 = GetNodeUV( F, n2, 0, check );
640 if ( isUPeriodic && Abs( uv.X()-uv2.X() ) < Abs( newUV.X()-uv2.X() ))
641 newUV.SetX( uv.X() );
642 if ( isVPeriodic && Abs( uv.Y()-uv2.Y() ) < Abs( newUV.Y()-uv2.Y() ))
643 newUV.SetY( uv.Y() );
649 else if ( Pos->GetTypeOfPosition() == SMDS_TOP_VERTEX )
651 if ( int vertexID = n->getshapeId() ) {
652 const TopoDS_Vertex& V = TopoDS::Vertex(GetMeshDS()->IndexToShape(vertexID));
654 uv = BRep_Tool::Parameters( V, F );
657 catch (Standard_Failure& exc) {
661 if ( !IsSubShape( V, F ))
663 MESSAGE("GetNodeUV() Vertex "<< vertexID <<" not in face "<< GetMeshDS()->ShapeToIndex(F));
664 // get UV of a vertex closest to the node
666 gp_Pnt pn = XYZ( n );
667 for ( TopExp_Explorer vert( F,TopAbs_VERTEX ); !uvOK && vert.More(); vert.Next() ) {
668 TopoDS_Vertex curV = TopoDS::Vertex( vert.Current() );
669 gp_Pnt p = BRep_Tool::Pnt( curV );
670 double curDist = p.SquareDistance( pn );
671 if ( curDist < dist ) {
673 uv = BRep_Tool::Parameters( curV, F );
674 uvOK = ( dist < DBL_MIN );
681 TopTools_ListIteratorOfListOfShape it( myMesh->GetAncestors( V ));
682 for ( ; it.More(); it.Next() ) {
683 if ( it.Value().ShapeType() == TopAbs_EDGE ) {
684 const TopoDS_Edge & edge = TopoDS::Edge( it.Value() );
686 Handle(Geom2d_Curve) C2d = BRep_Tool::CurveOnSurface(edge, F, f, l);
687 if ( !C2d.IsNull() ) {
688 double u = ( V == IthVertex( 0, edge )) ? f : l;
689 uv = C2d->Value( u );
695 if ( !uvOK && V.Orientation() == TopAbs_INTERNAL )
697 Handle(ShapeAnalysis_Surface) projector = GetSurface( F );
698 if ( n2 ) uv = GetNodeUV( F, n2 );
699 if ( Precision::IsInfinite( uv.X() ))
700 uv = projector->NextValueOfUV( uv, BRep_Tool::Pnt( V ), BRep_Tool::Tolerance( F ));
702 uv = projector->ValueOfUV( BRep_Tool::Pnt( V ), BRep_Tool::Tolerance( F ));
703 uvOK = ( projector->Gap() < getFaceMaxTol( F ));
707 if ( n2 && IsSeamShape( vertexID ))
709 bool isSeam = ( myShape.IsSame( F ));
711 SMESH_MesherHelper h( *myMesh );
713 isSeam = IsSeamShape( vertexID );
717 uv = getUVOnSeam( uv, GetNodeUV( F, n2, 0 ));
723 uvOK = CheckNodeUV( F, n, uv.ChangeCoord(), 2.*getFaceMaxTol( F ));
726 if ( check && !uvOK )
732 //=======================================================================
733 //function : CheckNodeUV
734 //purpose : Check and fix node UV on a face
735 //=======================================================================
737 bool SMESH_MesherHelper::CheckNodeUV(const TopoDS_Face& F,
738 const SMDS_MeshNode* n,
742 double distXYZ[4]) const
744 int shapeID = n->getshapeId();
746 if (( infinit = ( Precision::IsInfinite( uv.X() ) || Precision::IsInfinite( uv.Y() ))) ||
748 ( uv.X() == 0. && uv.Y() == 0. ) ||
749 ( toCheckPosOnShape( shapeID )))
751 // check that uv is correct
753 Handle(Geom_Surface) surface = BRep_Tool::Surface( F,loc );
754 gp_Pnt nodePnt = XYZ( n ), surfPnt(0,0,0);
756 if ( !loc.IsIdentity() ) nodePnt.Transform( loc.Transformation().Inverted() );
758 (dist = nodePnt.Distance( surfPnt = surface->Value( uv.X(), uv.Y() ))) > tol )
760 setPosOnShapeValidity( shapeID, false );
761 if ( !infinit && distXYZ ) {
762 surfPnt.Transform( loc );
764 distXYZ[1] = surfPnt.X(); distXYZ[2] = surfPnt.Y(); distXYZ[3]=surfPnt.Z();
766 // uv incorrect, project the node to surface
767 GeomAPI_ProjectPointOnSurf& projector = GetProjector( F, loc, tol );
768 projector.Perform( nodePnt );
769 if ( !projector.IsDone() || projector.NbPoints() < 1 )
771 MESSAGE( "SMESH_MesherHelper::CheckNodeUV() failed to project" );
774 Quantity_Parameter U,V;
775 projector.LowerDistanceParameters(U,V);
777 surfPnt = surface->Value( U, V );
778 dist = nodePnt.Distance( surfPnt );
780 surfPnt.Transform( loc );
782 distXYZ[1] = surfPnt.X(); distXYZ[2] = surfPnt.Y(); distXYZ[3]=surfPnt.Z();
786 MESSAGE( "SMESH_MesherHelper::CheckNodeUV(), invalid projection" );
789 // store the fixed UV on the face
790 if ( myShape.IsSame(F) && shapeID == myShapeID && myFixNodeParameters )
791 const_cast<SMDS_MeshNode*>(n)->SetPosition
792 ( SMDS_PositionPtr( new SMDS_FacePosition( U, V )));
794 else if ( myShape.IsSame(F) && uv.Modulus() > numeric_limits<double>::min() )
796 setPosOnShapeValidity( shapeID, true );
802 //=======================================================================
803 //function : GetProjector
804 //purpose : Return projector intitialized by given face without location, which is returned
805 //=======================================================================
807 GeomAPI_ProjectPointOnSurf& SMESH_MesherHelper::GetProjector(const TopoDS_Face& F,
808 TopLoc_Location& loc,
811 Handle(Geom_Surface) surface = BRep_Tool::Surface( F,loc );
812 int faceID = GetMeshDS()->ShapeToIndex( F );
813 TID2ProjectorOnSurf& i2proj = const_cast< TID2ProjectorOnSurf&>( myFace2Projector );
814 TID2ProjectorOnSurf::iterator i_proj = i2proj.find( faceID );
815 if ( i_proj == i2proj.end() )
817 if ( tol == 0 ) tol = BRep_Tool::Tolerance( F );
818 double U1, U2, V1, V2;
819 surface->Bounds(U1, U2, V1, V2);
820 GeomAPI_ProjectPointOnSurf* proj = new GeomAPI_ProjectPointOnSurf();
821 proj->Init( surface, U1, U2, V1, V2, tol );
822 i_proj = i2proj.insert( make_pair( faceID, proj )).first;
824 return *( i_proj->second );
827 //=======================================================================
828 //function : GetSurface
829 //purpose : Return a cached ShapeAnalysis_Surface of a FACE
830 //=======================================================================
832 Handle(ShapeAnalysis_Surface) SMESH_MesherHelper::GetSurface(const TopoDS_Face& F ) const
834 Handle(Geom_Surface) surface = BRep_Tool::Surface( F );
835 int faceID = GetMeshDS()->ShapeToIndex( F );
836 TID2Surface::iterator i_surf = myFace2Surface.find( faceID );
837 if ( i_surf == myFace2Surface.end() && faceID )
839 Handle(ShapeAnalysis_Surface) surf( new ShapeAnalysis_Surface( surface ));
840 i_surf = myFace2Surface.insert( make_pair( faceID, surf )).first;
842 return i_surf->second;
847 gp_XY AverageUV(const gp_XY& uv1, const gp_XY& uv2) { return ( uv1 + uv2 ) / 2.; }
848 gp_XY_FunPtr(Added); // define gp_XY_Added pointer to function calling gp_XY::Added(gp_XY)
849 gp_XY_FunPtr(Subtracted);
852 //=======================================================================
853 //function : ApplyIn2D
854 //purpose : Perform given operation on two 2d points in parameric space of given surface.
855 // It takes into account period of the surface. Use gp_XY_FunPtr macro
856 // to easily define pointer to function of gp_XY class.
857 //=======================================================================
859 gp_XY SMESH_MesherHelper::ApplyIn2D(Handle(Geom_Surface) surface,
863 const bool resultInPeriod)
865 if ( surface->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface )))
866 surface = Handle(Geom_RectangularTrimmedSurface)::DownCast( surface )->BasisSurface();
867 Standard_Boolean isUPeriodic = surface.IsNull() ? false : surface->IsUPeriodic();
868 Standard_Boolean isVPeriodic = surface.IsNull() ? false : surface->IsVPeriodic();
869 if ( !isUPeriodic && !isVPeriodic )
872 // move uv2 not far than half-period from uv1
874 uv2.X()+(isUPeriodic ? ShapeAnalysis::AdjustByPeriod(uv2.X(),uv1.X(),surface->UPeriod()) :0);
876 uv2.Y()+(isVPeriodic ? ShapeAnalysis::AdjustByPeriod(uv2.Y(),uv1.Y(),surface->VPeriod()) :0);
879 gp_XY res = fun( uv1, gp_XY(u2,v2) );
881 // move result within period
882 if ( resultInPeriod )
884 Standard_Real UF,UL,VF,VL;
885 surface->Bounds(UF,UL,VF,VL);
887 res.SetX( res.X() + ShapeAnalysis::AdjustToPeriod(res.X(),UF,UL));
889 res.SetY( res.Y() + ShapeAnalysis::AdjustToPeriod(res.Y(),VF,VL));
895 //=======================================================================
896 //function : AdjustByPeriod
897 //purpose : Move node positions on a FACE within surface period
898 //=======================================================================
900 void SMESH_MesherHelper::AdjustByPeriod( const TopoDS_Face& face, gp_XY uv[], const int nbUV )
902 SMESH_MesherHelper h( *myMesh ), *ph = face.IsSame( myShape ) ? this : &h;
903 ph->SetSubShape( face );
905 for ( int iCoo = U_periodic; iCoo <= V_periodic; ++iCoo )
906 if ( ph->GetPeriodicIndex() & iCoo )
908 const double period = ( ph->myPar2[iCoo-1] - ph->myPar1[iCoo-1] );
909 const double xRef = uv[0].Coord( iCoo );
910 for ( int i = 1; i < nbUV; ++i )
912 double x = uv[i].Coord( iCoo );
913 double dx = ShapeAnalysis::AdjustByPeriod( x, xRef, period );
914 uv[i].SetCoord( iCoo, x + dx );
919 //=======================================================================
920 //function : GetMiddleUV
921 //purpose : Return middle UV taking in account surface period
922 //=======================================================================
924 gp_XY SMESH_MesherHelper::GetMiddleUV(const Handle(Geom_Surface)& surface,
929 // the proper place of getting basic surface seems to be in ApplyIn2D()
930 // but we put it here to decrease a risk of regressions just before releasing a version
931 // Handle(Geom_Surface) surf = surface;
932 // while ( !surf.IsNull() && surf->IsKind(STANDARD_TYPE(Geom_RectangularTrimmedSurface )))
933 // surf = Handle(Geom_RectangularTrimmedSurface)::DownCast( surf )->BasisSurface();
935 return ApplyIn2D( surface, p1, p2, & AverageUV );
938 //=======================================================================
939 //function : GetCenterUV
940 //purpose : Return UV for the central node of a biquadratic triangle
941 //=======================================================================
943 gp_XY SMESH_MesherHelper::GetCenterUV(const gp_XY& uv1,
949 bool * isBadTria/*=0*/)
952 gp_XY uvAvg = ( uv12 + uv23 + uv31 ) / 3.;
954 if (( badTria = (( uvAvg - uv1 ) * ( uvAvg - uv23 ) > 0 )))
955 uvAvg = ( uv1 + uv23 ) / 2.;
956 else if (( badTria = (( uvAvg - uv2 ) * ( uvAvg - uv31 ) > 0 )))
957 uvAvg = ( uv2 + uv31 ) / 2.;
958 else if (( badTria = (( uvAvg - uv3 ) * ( uvAvg - uv12 ) > 0 )))
959 uvAvg = ( uv3 + uv12 ) / 2.;
962 *isBadTria = badTria;
966 //=======================================================================
967 //function : GetNodeU
968 //purpose : Return node U on edge
969 //=======================================================================
971 double SMESH_MesherHelper::GetNodeU(const TopoDS_Edge& E,
972 const SMDS_MeshNode* n,
973 const SMDS_MeshNode* inEdgeNode,
976 double param = Precision::Infinite();
978 const SMDS_PositionPtr pos = n->GetPosition();
979 if ( pos->GetTypeOfPosition()==SMDS_TOP_EDGE )
981 const SMDS_EdgePosition* epos = static_cast<const SMDS_EdgePosition*>( pos );
982 param = epos->GetUParameter();
984 else if( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX )
986 if ( inEdgeNode && TopExp::FirstVertex( E ).IsSame( TopExp::LastVertex( E ))) // issue 0020128
989 BRep_Tool::Range( E, f,l );
990 double uInEdge = GetNodeU( E, inEdgeNode );
991 param = ( fabs( uInEdge - f ) < fabs( l - uInEdge )) ? f : l;
995 SMESHDS_Mesh * meshDS = GetMeshDS();
996 int vertexID = n->getshapeId();
997 const TopoDS_Vertex& V = TopoDS::Vertex(meshDS->IndexToShape(vertexID));
998 param = BRep_Tool::Parameter( V, E );
1003 double tol = BRep_Tool::Tolerance( E );
1004 double f,l; BRep_Tool::Range( E, f,l );
1005 bool force = ( param < f-tol || param > l+tol );
1006 if ( !force && pos->GetTypeOfPosition()==SMDS_TOP_EDGE )
1007 force = ( GetMeshDS()->ShapeToIndex( E ) != n->getshapeId() );
1009 *check = CheckNodeU( E, n, param, 2*tol, force );
1014 //=======================================================================
1015 //function : CheckNodeU
1016 //purpose : Check and fix node U on an edge
1017 // Return false if U is bad and could not be fixed
1018 //=======================================================================
1020 bool SMESH_MesherHelper::CheckNodeU(const TopoDS_Edge& E,
1021 const SMDS_MeshNode* n,
1025 double distXYZ[4]) const
1027 int shapeID = n->getshapeId();
1029 if (( infinit = Precision::IsInfinite( u )) ||
1032 ( toCheckPosOnShape( shapeID )))
1034 TopLoc_Location loc; double f,l;
1035 Handle(Geom_Curve) curve = BRep_Tool::Curve( E,loc,f,l );
1036 if ( curve.IsNull() ) // degenerated edge
1038 if ( u+tol < f || u-tol > l )
1040 double r = Max( 0.5, 1 - tol*n->GetID()); // to get a unique u on edge
1046 gp_Pnt nodePnt = SMESH_TNodeXYZ( n );
1047 if ( !loc.IsIdentity() ) nodePnt.Transform( loc.Transformation().Inverted() );
1049 double dist = 2*tol;
1052 curvPnt = curve->Value( u );
1053 dist = nodePnt.Distance( curvPnt );
1055 curvPnt.Transform( loc );
1057 distXYZ[1] = curvPnt.X(); distXYZ[2] = curvPnt.Y(); distXYZ[3]=curvPnt.Z();
1062 setPosOnShapeValidity( shapeID, false );
1063 // u incorrect, project the node to the curve
1064 int edgeID = GetMeshDS()->ShapeToIndex( E );
1065 TID2ProjectorOnCurve& i2proj = const_cast< TID2ProjectorOnCurve&>( myEdge2Projector );
1066 TID2ProjectorOnCurve::iterator i_proj =
1067 i2proj.insert( make_pair( edgeID, (GeomAPI_ProjectPointOnCurve*) 0 )).first;
1068 if ( !i_proj->second )
1070 i_proj->second = new GeomAPI_ProjectPointOnCurve();
1071 i_proj->second->Init( curve, f, l );
1073 GeomAPI_ProjectPointOnCurve* projector = i_proj->second;
1074 projector->Perform( nodePnt );
1075 if ( projector->NbPoints() < 1 )
1077 MESSAGE( "SMESH_MesherHelper::CheckNodeU() failed to project" );
1080 Quantity_Parameter U = projector->LowerDistanceParameter();
1082 curvPnt = curve->Value( u );
1083 dist = nodePnt.Distance( curvPnt );
1085 curvPnt.Transform( loc );
1087 distXYZ[1] = curvPnt.X(); distXYZ[2] = curvPnt.Y(); distXYZ[3]=curvPnt.Z();
1091 MESSAGE( "CheckNodeU(), invalid projection; distance " << dist << "; tol " << tol );
1094 // store the fixed U on the edge
1095 if ( myShape.IsSame(E) && shapeID == myShapeID && myFixNodeParameters )
1096 const_cast<SMDS_MeshNode*>(n)->SetPosition
1097 ( SMDS_PositionPtr( new SMDS_EdgePosition( U )));
1099 else if ( fabs( u ) > numeric_limits<double>::min() )
1101 setPosOnShapeValidity( shapeID, true );
1103 if (( u < f-tol || u > l+tol ) && force )
1105 MESSAGE("u < f-tol || u > l+tol ; u " << u << " f " << f << " l " << l);
1106 // node is on vertex but is set on periodic but trimmed edge (issue 0020890)
1109 // do not use IsPeriodic() as Geom_TrimmedCurve::IsPeriodic () returns false
1110 double period = curve->Period();
1111 u = ( u < f ) ? u + period : u - period;
1113 catch (Standard_Failure& exc)
1123 //=======================================================================
1124 //function : GetMediumPos
1125 //purpose : Return index and type of the shape (EDGE or FACE only) to
1126 // set a medium node on
1127 //param : useCurSubShape - if true, returns the shape set via SetSubShape()
1129 //param : expectedSupport - shape type corresponding to element being created,
1130 // e.g TopAbs_EDGE if SMDSAbs_Edge is created
1131 // basing on \a n1 and \a n2
1132 // Calling GetMediumPos() with useCurSubShape=true is OK only for the
1133 // case where the lower dim mesh is already constructed and converted to quadratic,
1134 // else, nodes on EDGEs are assigned to FACE, for example.
1135 //=======================================================================
1137 std::pair<int, TopAbs_ShapeEnum>
1138 SMESH_MesherHelper::GetMediumPos(const SMDS_MeshNode* n1,
1139 const SMDS_MeshNode* n2,
1140 const bool useCurSubShape,
1141 TopAbs_ShapeEnum expectedSupport)
1143 if ( useCurSubShape && !myShape.IsNull() )
1144 return std::make_pair( myShapeID, myShape.ShapeType() );
1146 TopAbs_ShapeEnum shapeType = TopAbs_SHAPE;
1150 if (( myShapeID == n1->getshapeId() || myShapeID == n2->getshapeId() ) && myShapeID > 0 )
1152 shapeType = myShape.ShapeType();
1153 shapeID = myShapeID;
1155 else if ( n1->getshapeId() == n2->getshapeId() )
1157 shapeID = n2->getshapeId();
1158 shape = GetSubShapeByNode( n1, GetMeshDS() );
1160 else // 2 different shapes
1162 const SMDS_TypeOfPosition Pos1 = n1->GetPosition()->GetTypeOfPosition();
1163 const SMDS_TypeOfPosition Pos2 = n2->GetPosition()->GetTypeOfPosition();
1165 if ( Pos1 == SMDS_TOP_3DSPACE || Pos2 == SMDS_TOP_3DSPACE )
1169 else if ( Pos1 == SMDS_TOP_FACE || Pos2 == SMDS_TOP_FACE )
1172 if ( Pos1 != SMDS_TOP_FACE || Pos2 != SMDS_TOP_FACE ) // not 2 FACEs
1174 if ( Pos1 != SMDS_TOP_FACE ) std::swap( n1,n2 );
1175 TopoDS_Shape F = GetSubShapeByNode( n1, GetMeshDS() );
1176 TopoDS_Shape S = GetSubShapeByNode( n2, GetMeshDS() );
1177 if ( IsSubShape( S, F ))
1179 shapeType = TopAbs_FACE;
1180 shapeID = n1->getshapeId();
1184 else if ( Pos1 == SMDS_TOP_EDGE && Pos2 == SMDS_TOP_EDGE )
1186 TopoDS_Shape E1 = GetSubShapeByNode( n1, GetMeshDS() );
1187 TopoDS_Shape E2 = GetSubShapeByNode( n2, GetMeshDS() );
1188 shape = GetCommonAncestor( E1, E2, *myMesh, TopAbs_FACE );
1190 else if ( Pos1 == SMDS_TOP_VERTEX && Pos2 == SMDS_TOP_VERTEX )
1192 TopoDS_Shape V1 = GetSubShapeByNode( n1, GetMeshDS() );
1193 TopoDS_Shape V2 = GetSubShapeByNode( n2, GetMeshDS() );
1194 shape = GetCommonAncestor( V1, V2, *myMesh, TopAbs_EDGE );
1195 if ( shape.IsNull() ) shape = GetCommonAncestor( V1, V2, *myMesh, TopAbs_FACE );
1197 else // on VERTEX and EDGE
1199 if ( Pos1 != SMDS_TOP_VERTEX ) std::swap( n1,n2 );
1200 TopoDS_Shape V = GetSubShapeByNode( n1, GetMeshDS() );
1201 TopoDS_Shape E = GetSubShapeByNode( n2, GetMeshDS() );
1202 if ( IsSubShape( V, E ))
1205 shape = GetCommonAncestor( V, E, *myMesh, TopAbs_FACE );
1209 if ( !shape.IsNull() )
1212 shapeID = GetMeshDS()->ShapeToIndex( shape );
1213 shapeType = shape.ShapeType(); // EDGE or FACE
1215 if ( expectedSupport < shapeType &&
1216 expectedSupport != TopAbs_SHAPE &&
1217 !myShape.IsNull() &&
1218 myShape.ShapeType() == expectedSupport )
1220 // e.g. a side of triangle connects nodes on the same EDGE but does not
1221 // lie on this EDGE (an arc with a coarse mesh)
1222 // => shapeType == TopAbs_EDGE, expectedSupport == TopAbs_FACE;
1223 // hope that myShape is a right shape, return it if the found shape
1224 // has converted elements of corresponding dim (segments in our example)
1225 int nbConvertedElems = 0;
1226 SMDSAbs_ElementType type = ( shapeType == TopAbs_FACE ? SMDSAbs_Face : SMDSAbs_Edge );
1227 for ( int iN = 0; iN < 2; ++iN )
1229 const SMDS_MeshNode* n = iN ? n2 : n1;
1230 SMDS_ElemIteratorPtr it = n->GetInverseElementIterator( type );
1231 while ( it->more() )
1233 const SMDS_MeshElement* elem = it->next();
1234 if ( elem->getshapeId() == shapeID &&
1235 elem->IsQuadratic() )
1242 if ( nbConvertedElems == 2 )
1244 shapeType = myShape.ShapeType();
1245 shapeID = myShapeID;
1249 return make_pair( shapeID, shapeType );
1252 //=======================================================================
1253 //function : GetCentralNode
1254 //purpose : Return existing or create a new central node for a quardilateral
1255 // quadratic face given its 8 nodes.
1256 //@param : force3d - true means node creation in between the given nodes,
1257 // else node position is found on a geometrical face if any.
1258 //=======================================================================
1260 const SMDS_MeshNode* SMESH_MesherHelper::GetCentralNode(const SMDS_MeshNode* n1,
1261 const SMDS_MeshNode* n2,
1262 const SMDS_MeshNode* n3,
1263 const SMDS_MeshNode* n4,
1264 const SMDS_MeshNode* n12,
1265 const SMDS_MeshNode* n23,
1266 const SMDS_MeshNode* n34,
1267 const SMDS_MeshNode* n41,
1270 SMDS_MeshNode *centralNode = 0; // central node to return
1272 // Find an existing central node
1274 TBiQuad keyOfMap(n1,n2,n3,n4);
1275 std::map<TBiQuad, const SMDS_MeshNode* >::iterator itMapCentralNode;
1276 itMapCentralNode = myMapWithCentralNode.find( keyOfMap );
1277 if ( itMapCentralNode != myMapWithCentralNode.end() )
1279 return (*itMapCentralNode).second;
1282 // Get type of shape for the new central node
1284 TopAbs_ShapeEnum shapeType = TopAbs_SHAPE;
1288 TopTools_ListIteratorOfListOfShape it;
1290 std::map< int, int > faceId2nbNodes;
1291 std::map< int, int > ::iterator itMapWithIdFace;
1293 SMESHDS_Mesh* meshDS = GetMeshDS();
1295 // check if a face lies on a FACE, i.e. its all corner nodes lie either on the FACE or
1296 // on sub-shapes of the FACE
1297 if ( GetMesh()->HasShapeToMesh() )
1299 const SMDS_MeshNode* nodes[] = { n1, n2, n3, n4 };
1300 for(int i = 0; i < 4; i++)
1302 shape = GetSubShapeByNode( nodes[i], meshDS );
1303 if ( shape.IsNull() ) break;
1304 if ( shape.ShapeType() == TopAbs_SOLID )
1306 solidID = nodes[i]->getshapeId();
1307 shapeType = TopAbs_SOLID;
1310 if ( shape.ShapeType() == TopAbs_FACE )
1312 faceID = nodes[i]->getshapeId();
1313 itMapWithIdFace = faceId2nbNodes.insert( std::make_pair( faceID, 0 ) ).first;
1314 itMapWithIdFace->second++;
1318 PShapeIteratorPtr it = GetAncestors( shape, *GetMesh(), TopAbs_FACE );
1319 while ( const TopoDS_Shape* face = it->next() )
1321 faceID = meshDS->ShapeToIndex( *face );
1322 itMapWithIdFace = faceId2nbNodes.insert( std::make_pair( faceID, 0 )).first;
1323 itMapWithIdFace->second++;
1328 if ( solidID < 1 && !faceId2nbNodes.empty() ) // SOLID not found
1330 // find ID of the FACE the four corner nodes belong to
1331 itMapWithIdFace = faceId2nbNodes.find( myShapeID ); // IPAL52698
1332 if ( itMapWithIdFace != faceId2nbNodes.end() &&
1333 itMapWithIdFace->second == 4 )
1335 shapeType = TopAbs_FACE;
1340 itMapWithIdFace = faceId2nbNodes.begin();
1341 for ( ; itMapWithIdFace != faceId2nbNodes.end(); ++itMapWithIdFace)
1343 if ( itMapWithIdFace->second == 4 )
1345 shapeType = TopAbs_FACE;
1346 faceID = (*itMapWithIdFace).first;
1354 if ( shapeType == TopAbs_FACE )
1356 F = TopoDS::Face( meshDS->IndexToShape( faceID ));
1363 bool toCheck = true;
1364 if ( !F.IsNull() && !force3d )
1366 Handle(ShapeAnalysis_Surface) surface = GetSurface( F );
1367 if ( HasDegeneratedEdges() || surface->HasSingularities( 1e-7 ))
1369 gp_Pnt center = calcTFI (0.5, 0.5, // IPAL0052863
1370 SMESH_TNodeXYZ(n1), SMESH_TNodeXYZ(n2),
1371 SMESH_TNodeXYZ(n3), SMESH_TNodeXYZ(n4),
1372 SMESH_TNodeXYZ(n12), SMESH_TNodeXYZ(n23),
1373 SMESH_TNodeXYZ(n34), SMESH_TNodeXYZ(n41));
1374 gp_Pnt2d uv12 = GetNodeUV( F, n12, n3, &toCheck );
1375 uvAvg = surface->NextValueOfUV( uv12, center, BRep_Tool::Tolerance( F )).XY();
1380 GetNodeUV( F,n1, n3, &toCheck ),
1381 GetNodeUV( F,n2, n4, &toCheck ),
1382 GetNodeUV( F,n3, n1, &toCheck ),
1383 GetNodeUV( F,n4, n2, &toCheck ),
1384 GetNodeUV( F,n12, n3 ),
1385 GetNodeUV( F,n23, n4 ),
1386 GetNodeUV( F,n34, n2 ),
1387 GetNodeUV( F,n41, n2 )
1389 AdjustByPeriod( F, uv, 8 ); // put uv[] within a period (IPAL52698)
1391 uvAvg = calcTFI (0.5, 0.5, uv[0],uv[1],uv[2],uv[3], uv[4],uv[5],uv[6],uv[7] );
1393 P = surface->Value( uvAvg );
1394 centralNode = meshDS->AddNode( P.X(), P.Y(), P.Z() );
1395 // if ( mySetElemOnShape ) node is not elem!
1396 meshDS->SetNodeOnFace( centralNode, faceID, uvAvg.X(), uvAvg.Y() );
1398 else // ( force3d || F.IsNull() )
1400 P = calcTFI (0.5, 0.5,
1401 SMESH_TNodeXYZ(n1), SMESH_TNodeXYZ(n2),
1402 SMESH_TNodeXYZ(n3), SMESH_TNodeXYZ(n4),
1403 SMESH_TNodeXYZ(n12), SMESH_TNodeXYZ(n23),
1404 SMESH_TNodeXYZ(n34), SMESH_TNodeXYZ(n41));
1405 centralNode = meshDS->AddNode( P.X(), P.Y(), P.Z() );
1407 if ( !F.IsNull() ) // force3d
1409 uvAvg = (GetNodeUV(F,n1,n3,&toCheck) +
1410 GetNodeUV(F,n2,n4,&toCheck) +
1411 GetNodeUV(F,n3,n1,&toCheck) +
1412 GetNodeUV(F,n4,n2,&toCheck)) / 4;
1413 //CheckNodeUV( F, centralNode, uvAvg, 2*BRep_Tool::Tolerance( F ), /*force=*/true);
1414 meshDS->SetNodeOnFace( centralNode, faceID, uvAvg.X(), uvAvg.Y() );
1416 else if ( solidID > 0 )
1418 meshDS->SetNodeInVolume( centralNode, solidID );
1420 else if ( myShapeID > 0 && mySetElemOnShape )
1422 meshDS->SetMeshElementOnShape( centralNode, myShapeID );
1425 myMapWithCentralNode.insert( std::make_pair( keyOfMap, centralNode ) );
1429 //=======================================================================
1430 //function : GetCentralNode
1431 //purpose : Return existing or create a new central node for a
1432 // quadratic triangle given its 6 nodes.
1433 //@param : force3d - true means node creation in between the given nodes,
1434 // else node position is found on a geometrical face if any.
1435 //=======================================================================
1437 const SMDS_MeshNode* SMESH_MesherHelper::GetCentralNode(const SMDS_MeshNode* n1,
1438 const SMDS_MeshNode* n2,
1439 const SMDS_MeshNode* n3,
1440 const SMDS_MeshNode* n12,
1441 const SMDS_MeshNode* n23,
1442 const SMDS_MeshNode* n31,
1445 SMDS_MeshNode *centralNode = 0; // central node to return
1447 // Find an existing central node
1449 TBiQuad keyOfMap(n1,n2,n3);
1450 std::map<TBiQuad, const SMDS_MeshNode* >::iterator itMapCentralNode;
1451 itMapCentralNode = myMapWithCentralNode.find( keyOfMap );
1452 if ( itMapCentralNode != myMapWithCentralNode.end() )
1454 return (*itMapCentralNode).second;
1457 // Get type of shape for the new central node
1459 TopAbs_ShapeEnum shapeType = TopAbs_SHAPE;
1463 TopTools_ListIteratorOfListOfShape it;
1465 std::map< int, int > faceId2nbNodes;
1466 std::map< int, int > ::iterator itMapWithIdFace;
1468 SMESHDS_Mesh* meshDS = GetMeshDS();
1470 // check if a face lies on a FACE, i.e. its all corner nodes lie either on the FACE or
1471 // on sub-shapes of the FACE
1472 if ( GetMesh()->HasShapeToMesh() )
1474 const SMDS_MeshNode* nodes[] = { n1, n2, n3 };
1475 for(int i = 0; i < 3; i++)
1477 shape = GetSubShapeByNode( nodes[i], meshDS );
1478 if ( shape.IsNull() ) break;
1479 if ( shape.ShapeType() == TopAbs_SOLID )
1481 solidID = nodes[i]->getshapeId();
1482 shapeType = TopAbs_SOLID;
1485 if ( shape.ShapeType() == TopAbs_FACE )
1487 faceID = nodes[i]->getshapeId();
1488 itMapWithIdFace = faceId2nbNodes.insert( std::make_pair( faceID, 0 ) ).first;
1489 itMapWithIdFace->second++;
1493 PShapeIteratorPtr it = GetAncestors(shape, *GetMesh(), TopAbs_FACE );
1494 while ( const TopoDS_Shape* face = it->next() )
1496 faceID = meshDS->ShapeToIndex( *face );
1497 itMapWithIdFace = faceId2nbNodes.insert( std::make_pair( faceID, 0 ) ).first;
1498 itMapWithIdFace->second++;
1503 if ( solidID < 1 && !faceId2nbNodes.empty() ) // SOLID not found
1505 // find ID of the FACE the four corner nodes belong to
1506 itMapWithIdFace = faceId2nbNodes.find( myShapeID ); // IPAL52698
1507 if ( itMapWithIdFace != faceId2nbNodes.end() &&
1508 itMapWithIdFace->second == 4 )
1510 shapeType = TopAbs_FACE;
1515 itMapWithIdFace = faceId2nbNodes.begin();
1516 for ( ; itMapWithIdFace != faceId2nbNodes.end(); ++itMapWithIdFace)
1518 if ( itMapWithIdFace->second == 3 )
1520 shapeType = TopAbs_FACE;
1521 faceID = (*itMapWithIdFace).first;
1531 if ( shapeType == TopAbs_FACE )
1533 F = TopoDS::Face( meshDS->IndexToShape( faceID ));
1534 bool checkOK = true, badTria = false;
1536 GetNodeUV( F, n1, n23, &checkOK ),
1537 GetNodeUV( F, n2, n31, &checkOK ),
1538 GetNodeUV( F, n3, n12, &checkOK ),
1539 GetNodeUV( F, n12, n3, &checkOK ),
1540 GetNodeUV( F, n23, n1, &checkOK ),
1541 GetNodeUV( F, n31, n2, &checkOK )
1543 AdjustByPeriod( F, uv, 6 ); // put uv[] within a period (IPAL52698)
1545 uvAvg = GetCenterUV( uv[0],uv[1],uv[2], uv[3],uv[4],uv[5], &badTria );
1547 if ( badTria || !checkOK )
1551 // Create a central node
1554 if ( !F.IsNull() && !force3d )
1556 TopLoc_Location loc;
1557 Handle( Geom_Surface ) S = BRep_Tool::Surface( F, loc );
1558 P = S->Value( uvAvg.X(), uvAvg.Y() ).Transformed( loc );
1559 centralNode = meshDS->AddNode( P.X(), P.Y(), P.Z() );
1560 // if ( mySetElemOnShape ) node is not elem!
1561 meshDS->SetNodeOnFace( centralNode, faceID, uvAvg.X(), uvAvg.Y() );
1563 else // ( force3d || F.IsNull() )
1565 P = ( SMESH_TNodeXYZ( n12 ) +
1566 SMESH_TNodeXYZ( n23 ) +
1567 SMESH_TNodeXYZ( n31 ) ) / 3;
1568 centralNode = meshDS->AddNode( P.X(), P.Y(), P.Z() );
1570 if ( !F.IsNull() ) // force3d
1572 meshDS->SetNodeOnFace( centralNode, faceID, uvAvg.X(), uvAvg.Y() );
1574 else if ( solidID > 0 )
1576 meshDS->SetNodeInVolume( centralNode, solidID );
1578 else if ( myShapeID > 0 && mySetElemOnShape )
1580 meshDS->SetMeshElementOnShape( centralNode, myShapeID );
1583 myMapWithCentralNode.insert( std::make_pair( keyOfMap, centralNode ) );
1587 //=======================================================================
1588 //function : GetMediumNode
1589 //purpose : Return existing or create a new medium node between given ones
1590 //=======================================================================
1592 const SMDS_MeshNode* SMESH_MesherHelper::GetMediumNode(const SMDS_MeshNode* n1,
1593 const SMDS_MeshNode* n2,
1595 TopAbs_ShapeEnum expectedSupport)
1597 // Find existing node
1599 SMESH_TLink link(n1,n2);
1600 ItTLinkNode itLN = myTLinkNodeMap.find( link );
1601 if ( itLN != myTLinkNodeMap.end() ) {
1602 return (*itLN).second;
1605 // Create medium node
1608 SMESHDS_Mesh* meshDS = GetMeshDS();
1610 if ( IsSeamShape( n1->getshapeId() ))
1611 // to get a correct UV of a node on seam, the second node must have checked UV
1612 std::swap( n1, n2 );
1614 // get type of shape for the new medium node
1615 int faceID = -1, edgeID = -1;
1616 TopoDS_Edge E; double u [2] = {0.,0.};
1617 TopoDS_Face F; gp_XY uv[2];
1618 bool uvOK[2] = { true, true };
1619 const bool useCurSubShape = ( !myShape.IsNull() && myShape.ShapeType() == TopAbs_EDGE );
1621 pair<int, TopAbs_ShapeEnum> pos = GetMediumPos( n1, n2, useCurSubShape, expectedSupport );
1623 // get positions of the given nodes on shapes
1624 if ( pos.second == TopAbs_FACE )
1626 F = TopoDS::Face(meshDS->IndexToShape( faceID = pos.first ));
1627 uv[0] = GetNodeUV(F,n1,n2, force3d ? 0 : &uvOK[0]);
1629 ( HasDegeneratedEdges() || GetSurface( F )->HasSingularities( 1e-7 )))
1631 // IPAL52850 (degen VERTEX not at singularity)
1632 // project middle point to a surface
1633 SMESH_TNodeXYZ p1( n1 ), p2( n2 );
1634 gp_Pnt pMid = 0.5 * ( p1 + p2 );
1635 Handle(ShapeAnalysis_Surface) projector = GetSurface( F );
1638 uvMid = projector->NextValueOfUV( uv[0], pMid, BRep_Tool::Tolerance( F ));
1640 uvMid = projector->ValueOfUV( pMid, getFaceMaxTol( F ));
1641 if ( projector->Gap() * projector->Gap() < ( p1 - p2 ).SquareModulus() / 4 )
1643 gp_Pnt pProj = projector->Value( uvMid );
1644 n12 = meshDS->AddNode( pProj.X(), pProj.Y(), pProj.Z() );
1645 meshDS->SetNodeOnFace( n12, faceID, uvMid.X(), uvMid.Y() );
1646 myTLinkNodeMap.insert( make_pair ( link, n12 ));
1650 uv[1] = GetNodeUV(F,n2,n1, force3d ? 0 : &uvOK[1]);
1652 else if ( pos.second == TopAbs_EDGE )
1654 const SMDS_PositionPtr Pos1 = n1->GetPosition();
1655 const SMDS_PositionPtr Pos2 = n2->GetPosition();
1656 if ( Pos1->GetTypeOfPosition()==SMDS_TOP_EDGE &&
1657 Pos2->GetTypeOfPosition()==SMDS_TOP_EDGE &&
1658 n1->getshapeId() != n2->getshapeId() )
1661 return getMediumNodeOnComposedWire(n1,n2,force3d);
1663 E = TopoDS::Edge(meshDS->IndexToShape( edgeID = pos.first ));
1665 u[0] = GetNodeU(E,n1,n2, force3d ? 0 : &uvOK[0]);
1666 u[1] = GetNodeU(E,n2,n1, force3d ? 0 : &uvOK[1]);
1668 catch ( Standard_Failure& f )
1670 // issue 22502 / a node is on VERTEX not belonging to E
1671 // issue 22568 / both nodes are on non-connected VERTEXes
1672 return getMediumNodeOnComposedWire(n1,n2,force3d);
1676 if ( !force3d & uvOK[0] && uvOK[1] )
1678 // we try to create medium node using UV parameters of
1679 // nodes, else - medium between corresponding 3d points
1682 //if ( uvOK[0] && uvOK[1] )
1684 if ( IsDegenShape( n1->getshapeId() )) {
1685 if ( myParIndex & U_periodic ) uv[0].SetCoord( 1, uv[1].Coord( 1 ));
1686 else uv[0].SetCoord( 2, uv[1].Coord( 2 ));
1688 else if ( IsDegenShape( n2->getshapeId() )) {
1689 if ( myParIndex & U_periodic ) uv[1].SetCoord( 1, uv[0].Coord( 1 ));
1690 else uv[1].SetCoord( 2, uv[0].Coord( 2 ));
1692 TopLoc_Location loc;
1693 Handle(Geom_Surface) S = BRep_Tool::Surface(F,loc);
1694 gp_XY UV = GetMiddleUV( S, uv[0], uv[1] );
1695 gp_Pnt P = S->Value( UV.X(), UV.Y() ).Transformed(loc);
1696 n12 = meshDS->AddNode(P.X(), P.Y(), P.Z());
1697 // if ( mySetElemOnShape ) node is not elem!
1698 meshDS->SetNodeOnFace(n12, faceID, UV.X(), UV.Y());
1699 myTLinkNodeMap.insert(make_pair(link,n12));
1703 else if ( !E.IsNull() )
1706 Handle(Geom_Curve) C = BRep_Tool::Curve(E, f, l);
1709 Standard_Boolean isPeriodic = C->IsPeriodic();
1712 Standard_Real Period = C->Period();
1713 Standard_Real p = u[1]+ShapeAnalysis::AdjustByPeriod(u[1],u[0],Period);
1714 Standard_Real pmid = (u[0]+p)/2.;
1715 U = pmid+ShapeAnalysis::AdjustToPeriod(pmid,C->FirstParameter(),C->LastParameter());
1720 gp_Pnt P = C->Value( U );
1721 n12 = meshDS->AddNode(P.X(), P.Y(), P.Z());
1722 //if ( mySetElemOnShape ) node is not elem!
1723 meshDS->SetNodeOnEdge(n12, edgeID, U);
1724 myTLinkNodeMap.insert(make_pair(link,n12));
1731 double x = ( n1->X() + n2->X() )/2.;
1732 double y = ( n1->Y() + n2->Y() )/2.;
1733 double z = ( n1->Z() + n2->Z() )/2.;
1734 n12 = meshDS->AddNode(x,y,z);
1736 //if ( mySetElemOnShape ) node is not elem!
1740 gp_XY UV = ( uv[0] + uv[1] ) / 2.;
1741 CheckNodeUV( F, n12, UV, 2 * BRep_Tool::Tolerance( F ), /*force=*/true);
1742 meshDS->SetNodeOnFace(n12, faceID, UV.X(), UV.Y() );
1744 else if ( !E.IsNull() )
1746 double U = ( u[0] + u[1] ) / 2.;
1747 CheckNodeU( E, n12, U, 2 * BRep_Tool::Tolerance( E ), /*force=*/true);
1748 meshDS->SetNodeOnEdge(n12, edgeID, U);
1750 else if ( myShapeID > 0 && mySetElemOnShape )
1752 meshDS->SetMeshElementOnShape(n12, myShapeID);
1756 myTLinkNodeMap.insert( make_pair( link, n12 ));
1760 //================================================================================
1762 * \brief Makes a medium node if nodes reside different edges
1764 //================================================================================
1766 const SMDS_MeshNode* SMESH_MesherHelper::getMediumNodeOnComposedWire(const SMDS_MeshNode* n1,
1767 const SMDS_MeshNode* n2,
1770 SMESH_TNodeXYZ p1( n1 ), p2( n2 );
1771 gp_Pnt middle = 0.5 * p1 + 0.5 * p2;
1772 SMDS_MeshNode* n12 = AddNode( middle.X(), middle.Y(), middle.Z() );
1774 // To find position on edge and 3D position for n12,
1775 // project <middle> to 2 edges and select projection most close to <middle>
1777 TopoDS_Edge bestEdge;
1778 double u = 0, distMiddleProj = Precision::Infinite(), distXYZ[4], f,l;
1780 // get shapes under the nodes
1781 TopoDS_Shape shape[2];
1783 for ( int is2nd = 0; is2nd < 2; ++is2nd )
1785 const SMDS_MeshNode* n = is2nd ? n2 : n1;
1786 TopoDS_Shape S = GetSubShapeByNode( n, GetMeshDS() );
1788 shape[ nbShapes++ ] = S;
1791 vector< TopoDS_Shape > edges;
1792 for ( int iS = 0; iS < nbShapes; ++iS )
1794 switch ( shape[iS].ShapeType() ) {
1797 edges.push_back( shape[iS] );
1803 if ( nbShapes == 2 && iS==0 && shape[1-iS].ShapeType() == TopAbs_VERTEX )
1804 edge = GetCommonAncestor( shape[iS], shape[1-iS], *myMesh, TopAbs_EDGE );
1806 if ( edge.IsNull() )
1808 PShapeIteratorPtr eIt = GetAncestors( shape[iS], *myMesh, TopAbs_EDGE );
1809 while( const TopoDS_Shape* e = eIt->next() )
1810 edges.push_back( *e );
1816 if ( nbShapes == 1 || shape[1-iS].ShapeType() < TopAbs_EDGE )
1817 for ( TopExp_Explorer e( shape[iS], TopAbs_EDGE ); e.More(); e.Next() )
1818 edges.push_back( e.Current() );
1825 // project to get U of projection and distance from middle to projection
1826 for ( size_t iE = 0; iE < edges.size(); ++iE )
1828 const TopoDS_Edge& edge = TopoDS::Edge( edges[ iE ]);
1829 distXYZ[0] = distMiddleProj;
1831 CheckNodeU( edge, n12, testU, 2 * BRep_Tool::Tolerance(edge), /*force=*/true, distXYZ );
1832 if ( distXYZ[0] < distMiddleProj )
1834 distMiddleProj = distXYZ[0];
1840 // // both projections failed; set n12 on the edge of n1 with U of a common vertex
1841 // TopoDS_Vertex vCommon;
1842 // if ( TopExp::CommonVertex( edges[0], edges[1], vCommon ))
1843 // u = BRep_Tool::Parameter( vCommon, edges[0] );
1846 // double f,l, u0 = GetNodeU( edges[0], n1 );
1847 // BRep_Tool::Range( edges[0],f,l );
1848 // u = ( fabs(u0-f) < fabs(u0-l) ) ? f : l;
1851 // distMiddleProj = 0;
1854 if ( !bestEdge.IsNull() )
1856 // move n12 to position of a successfull projection
1857 //double tol = BRep_Tool::Tolerance(edges[ iOkEdge ]);
1858 if ( !force3d /*&& distMiddleProj > 2*tol*/ )
1860 TopLoc_Location loc;
1861 Handle(Geom_Curve) curve = BRep_Tool::Curve( bestEdge,loc,f,l );
1862 gp_Pnt p = curve->Value( u ).Transformed( loc );
1863 GetMeshDS()->MoveNode( n12, p.X(), p.Y(), p.Z() );
1865 //if ( mySetElemOnShape ) node is not elem!
1867 int edgeID = GetMeshDS()->ShapeToIndex( bestEdge );
1868 if ( edgeID != n12->getshapeId() )
1869 GetMeshDS()->UnSetNodeOnShape( n12 );
1870 GetMeshDS()->SetNodeOnEdge(n12, edgeID, u);
1873 myTLinkNodeMap.insert( make_pair( SMESH_TLink(n1,n2), n12 ));
1878 //=======================================================================
1879 //function : AddNode
1880 //purpose : Creates a node
1881 //=======================================================================
1883 SMDS_MeshNode* SMESH_MesherHelper::AddNode(double x, double y, double z, int ID,
1886 SMESHDS_Mesh * meshDS = GetMeshDS();
1887 SMDS_MeshNode* node = 0;
1889 node = meshDS->AddNodeWithID( x, y, z, ID );
1891 node = meshDS->AddNode( x, y, z );
1892 if ( mySetElemOnShape && myShapeID > 0 ) { // node is not elem ?
1893 switch ( myShape.ShapeType() ) {
1894 case TopAbs_SOLID: meshDS->SetNodeInVolume( node, myShapeID); break;
1895 case TopAbs_SHELL: meshDS->SetNodeInVolume( node, myShapeID); break;
1896 case TopAbs_FACE: meshDS->SetNodeOnFace( node, myShapeID, u, v); break;
1897 case TopAbs_EDGE: meshDS->SetNodeOnEdge( node, myShapeID, u); break;
1898 case TopAbs_VERTEX: meshDS->SetNodeOnVertex( node, myShapeID); break;
1905 //=======================================================================
1906 //function : AddEdge
1907 //purpose : Creates quadratic or linear edge
1908 //=======================================================================
1910 SMDS_MeshEdge* SMESH_MesherHelper::AddEdge(const SMDS_MeshNode* n1,
1911 const SMDS_MeshNode* n2,
1915 SMESHDS_Mesh * meshDS = GetMeshDS();
1917 SMDS_MeshEdge* edge = 0;
1918 if (myCreateQuadratic) {
1919 const SMDS_MeshNode* n12 = GetMediumNode(n1,n2,force3d);
1921 edge = meshDS->AddEdgeWithID(n1, n2, n12, id);
1923 edge = meshDS->AddEdge(n1, n2, n12);
1927 edge = meshDS->AddEdgeWithID(n1, n2, id);
1929 edge = meshDS->AddEdge(n1, n2);
1932 if ( mySetElemOnShape && myShapeID > 0 )
1933 meshDS->SetMeshElementOnShape( edge, myShapeID );
1938 //=======================================================================
1939 //function : AddFace
1940 //purpose : Creates quadratic or linear triangle
1941 //=======================================================================
1943 SMDS_MeshFace* SMESH_MesherHelper::AddFace(const SMDS_MeshNode* n1,
1944 const SMDS_MeshNode* n2,
1945 const SMDS_MeshNode* n3,
1949 SMESHDS_Mesh * meshDS = GetMeshDS();
1950 SMDS_MeshFace* elem = 0;
1952 if( n1==n2 || n2==n3 || n3==n1 )
1955 if(!myCreateQuadratic) {
1957 elem = meshDS->AddFaceWithID(n1, n2, n3, id);
1959 elem = meshDS->AddFace(n1, n2, n3);
1962 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_FACE );
1963 const SMDS_MeshNode* n23 = GetMediumNode( n2, n3, force3d, TopAbs_FACE );
1964 const SMDS_MeshNode* n31 = GetMediumNode( n3, n1, force3d, TopAbs_FACE );
1965 if(myCreateBiQuadratic)
1967 const SMDS_MeshNode* nCenter = GetCentralNode(n1, n2, n3, n12, n23, n31, force3d);
1969 elem = meshDS->AddFaceWithID(n1, n2, n3, n12, n23, n31, nCenter, id);
1971 elem = meshDS->AddFace(n1, n2, n3, n12, n23, n31, nCenter);
1976 elem = meshDS->AddFaceWithID(n1, n2, n3, n12, n23, n31, id);
1978 elem = meshDS->AddFace(n1, n2, n3, n12, n23, n31);
1981 if ( mySetElemOnShape && myShapeID > 0 )
1982 meshDS->SetMeshElementOnShape( elem, myShapeID );
1987 //=======================================================================
1988 //function : AddFace
1989 //purpose : Creates bi-quadratic, quadratic or linear quadrangle
1990 //=======================================================================
1992 SMDS_MeshFace* SMESH_MesherHelper::AddFace(const SMDS_MeshNode* n1,
1993 const SMDS_MeshNode* n2,
1994 const SMDS_MeshNode* n3,
1995 const SMDS_MeshNode* n4,
1999 SMESHDS_Mesh * meshDS = GetMeshDS();
2000 SMDS_MeshFace* elem = 0;
2003 return AddFace(n1,n3,n4,id,force3d);
2006 return AddFace(n1,n2,n4,id,force3d);
2009 return AddFace(n1,n2,n3,id,force3d);
2012 return AddFace(n1,n2,n4,id,force3d);
2015 return AddFace(n1,n2,n3,id,force3d);
2018 return AddFace(n1,n2,n3,id,force3d);
2021 if(!myCreateQuadratic) {
2023 elem = meshDS->AddFaceWithID(n1, n2, n3, n4, id);
2025 elem = meshDS->AddFace(n1, n2, n3, n4);
2028 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_FACE );
2029 const SMDS_MeshNode* n23 = GetMediumNode( n2, n3, force3d, TopAbs_FACE );
2030 const SMDS_MeshNode* n34 = GetMediumNode( n3, n4, force3d, TopAbs_FACE );
2031 const SMDS_MeshNode* n41 = GetMediumNode( n4, n1, force3d, TopAbs_FACE );
2032 if(myCreateBiQuadratic)
2034 const SMDS_MeshNode* nCenter = GetCentralNode(n1, n2, n3, n4, n12, n23, n34, n41, force3d);
2036 elem = meshDS->AddFaceWithID(n1, n2, n3, n4, n12, n23, n34, n41, nCenter, id);
2038 elem = meshDS->AddFace(n1, n2, n3, n4, n12, n23, n34, n41, nCenter);
2043 elem = meshDS->AddFaceWithID(n1, n2, n3, n4, n12, n23, n34, n41, id);
2045 elem = meshDS->AddFace(n1, n2, n3, n4, n12, n23, n34, n41);
2048 if ( mySetElemOnShape && myShapeID > 0 )
2049 meshDS->SetMeshElementOnShape( elem, myShapeID );
2054 //=======================================================================
2055 //function : AddPolygonalFace
2056 //purpose : Creates polygon, with additional nodes in quadratic mesh
2057 //=======================================================================
2059 SMDS_MeshFace* SMESH_MesherHelper::AddPolygonalFace (const vector<const SMDS_MeshNode*>& nodes,
2063 SMESHDS_Mesh * meshDS = GetMeshDS();
2064 SMDS_MeshFace* elem = 0;
2066 if(!myCreateQuadratic)
2069 elem = meshDS->AddPolygonalFaceWithID(nodes, id);
2071 elem = meshDS->AddPolygonalFace(nodes);
2075 vector<const SMDS_MeshNode*> newNodes( nodes.size() * 2 );
2077 for ( size_t i = 0; i < nodes.size(); ++i )
2079 const SMDS_MeshNode* n1 = nodes[i];
2080 const SMDS_MeshNode* n2 = nodes[(i+1)%nodes.size()];
2081 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_FACE );
2082 newNodes.push_back( n12 );
2085 elem = meshDS->AddQuadPolygonalFaceWithID(newNodes, id);
2087 elem = meshDS->AddQuadPolygonalFace(newNodes);
2089 if ( mySetElemOnShape && myShapeID > 0 )
2090 meshDS->SetMeshElementOnShape( elem, myShapeID );
2095 //=======================================================================
2096 //function : AddVolume
2097 //purpose : Creates quadratic or linear prism
2098 //=======================================================================
2100 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
2101 const SMDS_MeshNode* n2,
2102 const SMDS_MeshNode* n3,
2103 const SMDS_MeshNode* n4,
2104 const SMDS_MeshNode* n5,
2105 const SMDS_MeshNode* n6,
2109 SMESHDS_Mesh * meshDS = GetMeshDS();
2110 SMDS_MeshVolume* elem = 0;
2111 if(!myCreateQuadratic) {
2113 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, id);
2115 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6);
2118 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_SOLID );
2119 const SMDS_MeshNode* n23 = GetMediumNode( n2, n3, force3d, TopAbs_SOLID );
2120 const SMDS_MeshNode* n31 = GetMediumNode( n3, n1, force3d, TopAbs_SOLID );
2122 const SMDS_MeshNode* n45 = GetMediumNode( n4, n5, force3d, TopAbs_SOLID );
2123 const SMDS_MeshNode* n56 = GetMediumNode( n5, n6, force3d, TopAbs_SOLID );
2124 const SMDS_MeshNode* n64 = GetMediumNode( n6, n4, force3d, TopAbs_SOLID );
2126 const SMDS_MeshNode* n14 = GetMediumNode( n1, n4, force3d, TopAbs_SOLID );
2127 const SMDS_MeshNode* n25 = GetMediumNode( n2, n5, force3d, TopAbs_SOLID );
2128 const SMDS_MeshNode* n36 = GetMediumNode( n3, n6, force3d, TopAbs_SOLID );
2131 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6,
2132 n12, n23, n31, n45, n56, n64, n14, n25, n36, id);
2134 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6,
2135 n12, n23, n31, n45, n56, n64, n14, n25, n36);
2137 if ( mySetElemOnShape && myShapeID > 0 )
2138 meshDS->SetMeshElementOnShape( elem, myShapeID );
2143 //=======================================================================
2144 //function : AddVolume
2145 //purpose : Creates quadratic or linear tetrahedron
2146 //=======================================================================
2148 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
2149 const SMDS_MeshNode* n2,
2150 const SMDS_MeshNode* n3,
2151 const SMDS_MeshNode* n4,
2155 SMESHDS_Mesh * meshDS = GetMeshDS();
2156 SMDS_MeshVolume* elem = 0;
2157 if(!myCreateQuadratic) {
2159 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, id);
2161 elem = meshDS->AddVolume(n1, n2, n3, n4);
2164 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_SOLID );
2165 const SMDS_MeshNode* n23 = GetMediumNode( n2, n3, force3d, TopAbs_SOLID );
2166 const SMDS_MeshNode* n31 = GetMediumNode( n3, n1, force3d, TopAbs_SOLID );
2168 const SMDS_MeshNode* n14 = GetMediumNode( n1, n4, force3d, TopAbs_SOLID );
2169 const SMDS_MeshNode* n24 = GetMediumNode( n2, n4, force3d, TopAbs_SOLID );
2170 const SMDS_MeshNode* n34 = GetMediumNode( n3, n4, force3d, TopAbs_SOLID );
2173 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n12, n23, n31, n14, n24, n34, id);
2175 elem = meshDS->AddVolume(n1, n2, n3, n4, n12, n23, n31, n14, n24, n34);
2177 if ( mySetElemOnShape && myShapeID > 0 )
2178 meshDS->SetMeshElementOnShape( elem, myShapeID );
2183 //=======================================================================
2184 //function : AddVolume
2185 //purpose : Creates quadratic or linear pyramid
2186 //=======================================================================
2188 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
2189 const SMDS_MeshNode* n2,
2190 const SMDS_MeshNode* n3,
2191 const SMDS_MeshNode* n4,
2192 const SMDS_MeshNode* n5,
2196 SMDS_MeshVolume* elem = 0;
2197 if(!myCreateQuadratic) {
2199 elem = GetMeshDS()->AddVolumeWithID(n1, n2, n3, n4, n5, id);
2201 elem = GetMeshDS()->AddVolume(n1, n2, n3, n4, n5);
2204 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_SOLID );
2205 const SMDS_MeshNode* n23 = GetMediumNode( n2, n3, force3d, TopAbs_SOLID );
2206 const SMDS_MeshNode* n34 = GetMediumNode( n3, n4, force3d, TopAbs_SOLID );
2207 const SMDS_MeshNode* n41 = GetMediumNode( n4, n1, force3d, TopAbs_SOLID );
2209 const SMDS_MeshNode* n15 = GetMediumNode( n1, n5, force3d, TopAbs_SOLID );
2210 const SMDS_MeshNode* n25 = GetMediumNode( n2, n5, force3d, TopAbs_SOLID );
2211 const SMDS_MeshNode* n35 = GetMediumNode( n3, n5, force3d, TopAbs_SOLID );
2212 const SMDS_MeshNode* n45 = GetMediumNode( n4, n5, force3d, TopAbs_SOLID );
2215 elem = GetMeshDS()->AddVolumeWithID ( n1, n2, n3, n4, n5,
2220 elem = GetMeshDS()->AddVolume( n1, n2, n3, n4, n5,
2222 n15, n25, n35, n45);
2224 if ( mySetElemOnShape && myShapeID > 0 )
2225 GetMeshDS()->SetMeshElementOnShape( elem, myShapeID );
2230 //=======================================================================
2231 //function : AddVolume
2232 //purpose : Creates tri-quadratic, quadratic or linear hexahedron
2233 //=======================================================================
2235 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
2236 const SMDS_MeshNode* n2,
2237 const SMDS_MeshNode* n3,
2238 const SMDS_MeshNode* n4,
2239 const SMDS_MeshNode* n5,
2240 const SMDS_MeshNode* n6,
2241 const SMDS_MeshNode* n7,
2242 const SMDS_MeshNode* n8,
2246 SMESHDS_Mesh * meshDS = GetMeshDS();
2247 SMDS_MeshVolume* elem = 0;
2248 if(!myCreateQuadratic) {
2250 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, n7, n8, id);
2252 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6, n7, n8);
2255 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_SOLID );
2256 const SMDS_MeshNode* n23 = GetMediumNode( n2, n3, force3d, TopAbs_SOLID );
2257 const SMDS_MeshNode* n34 = GetMediumNode( n3, n4, force3d, TopAbs_SOLID );
2258 const SMDS_MeshNode* n41 = GetMediumNode( n4, n1, force3d, TopAbs_SOLID );
2260 const SMDS_MeshNode* n56 = GetMediumNode( n5, n6, force3d, TopAbs_SOLID );
2261 const SMDS_MeshNode* n67 = GetMediumNode( n6, n7, force3d, TopAbs_SOLID );
2262 const SMDS_MeshNode* n78 = GetMediumNode( n7, n8, force3d, TopAbs_SOLID );
2263 const SMDS_MeshNode* n85 = GetMediumNode( n8, n5, force3d, TopAbs_SOLID );
2265 const SMDS_MeshNode* n15 = GetMediumNode( n1, n5, force3d, TopAbs_SOLID );
2266 const SMDS_MeshNode* n26 = GetMediumNode( n2, n6, force3d, TopAbs_SOLID );
2267 const SMDS_MeshNode* n37 = GetMediumNode( n3, n7, force3d, TopAbs_SOLID );
2268 const SMDS_MeshNode* n48 = GetMediumNode( n4, n8, force3d, TopAbs_SOLID );
2269 if ( myCreateBiQuadratic )
2271 const SMDS_MeshNode* n1234 = GetCentralNode( n1,n2,n3,n4,n12,n23,n34,n41,force3d );
2272 const SMDS_MeshNode* n1256 = GetCentralNode( n1,n2,n5,n6,n12,n26,n56,n15,force3d );
2273 const SMDS_MeshNode* n2367 = GetCentralNode( n2,n3,n6,n7,n23,n37,n67,n26,force3d );
2274 const SMDS_MeshNode* n3478 = GetCentralNode( n3,n4,n7,n8,n34,n48,n78,n37,force3d );
2275 const SMDS_MeshNode* n1458 = GetCentralNode( n1,n4,n5,n8,n41,n48,n15,n85,force3d );
2276 const SMDS_MeshNode* n5678 = GetCentralNode( n5,n6,n7,n8,n56,n67,n78,n85,force3d );
2278 vector<gp_XYZ> pointsOnShapes( SMESH_Block::ID_Shell );
2280 pointsOnShapes[ SMESH_Block::ID_V000 ] = SMESH_TNodeXYZ( n4 );
2281 pointsOnShapes[ SMESH_Block::ID_V100 ] = SMESH_TNodeXYZ( n8 );
2282 pointsOnShapes[ SMESH_Block::ID_V010 ] = SMESH_TNodeXYZ( n3 );
2283 pointsOnShapes[ SMESH_Block::ID_V110 ] = SMESH_TNodeXYZ( n7 );
2284 pointsOnShapes[ SMESH_Block::ID_V001 ] = SMESH_TNodeXYZ( n1 );
2285 pointsOnShapes[ SMESH_Block::ID_V101 ] = SMESH_TNodeXYZ( n5 );
2286 pointsOnShapes[ SMESH_Block::ID_V011 ] = SMESH_TNodeXYZ( n2 );
2287 pointsOnShapes[ SMESH_Block::ID_V111 ] = SMESH_TNodeXYZ( n6 );
2289 pointsOnShapes[ SMESH_Block::ID_Ex00 ] = SMESH_TNodeXYZ( n48 );
2290 pointsOnShapes[ SMESH_Block::ID_Ex10 ] = SMESH_TNodeXYZ( n37 );
2291 pointsOnShapes[ SMESH_Block::ID_E0y0 ] = SMESH_TNodeXYZ( n15 );
2292 pointsOnShapes[ SMESH_Block::ID_E1y0 ] = SMESH_TNodeXYZ( n26 );
2293 pointsOnShapes[ SMESH_Block::ID_Ex01 ] = SMESH_TNodeXYZ( n34 );
2294 pointsOnShapes[ SMESH_Block::ID_Ex11 ] = SMESH_TNodeXYZ( n78 );
2295 pointsOnShapes[ SMESH_Block::ID_E0y1 ] = SMESH_TNodeXYZ( n12 );
2296 pointsOnShapes[ SMESH_Block::ID_E1y1 ] = SMESH_TNodeXYZ( n56 );
2297 pointsOnShapes[ SMESH_Block::ID_E00z ] = SMESH_TNodeXYZ( n41 );
2298 pointsOnShapes[ SMESH_Block::ID_E10z ] = SMESH_TNodeXYZ( n85 );
2299 pointsOnShapes[ SMESH_Block::ID_E01z ] = SMESH_TNodeXYZ( n23 );
2300 pointsOnShapes[ SMESH_Block::ID_E11z ] = SMESH_TNodeXYZ( n67 );
2302 pointsOnShapes[ SMESH_Block::ID_Fxy0 ] = SMESH_TNodeXYZ( n3478 );
2303 pointsOnShapes[ SMESH_Block::ID_Fxy1 ] = SMESH_TNodeXYZ( n1256 );
2304 pointsOnShapes[ SMESH_Block::ID_Fx0z ] = SMESH_TNodeXYZ( n1458 );
2305 pointsOnShapes[ SMESH_Block::ID_Fx1z ] = SMESH_TNodeXYZ( n2367 );
2306 pointsOnShapes[ SMESH_Block::ID_F0yz ] = SMESH_TNodeXYZ( n1234 );
2307 pointsOnShapes[ SMESH_Block::ID_F1yz ] = SMESH_TNodeXYZ( n5678 );
2309 gp_XYZ centerCube(0.5, 0.5, 0.5);
2311 SMESH_Block::ShellPoint( centerCube, pointsOnShapes, nCenterElem );
2312 const SMDS_MeshNode* nCenter =
2313 meshDS->AddNode( nCenterElem.X(), nCenterElem.Y(), nCenterElem.Z() );
2314 meshDS->SetNodeInVolume( nCenter, myShapeID );
2317 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, n7, n8,
2318 n12, n23, n34, n41, n56, n67,
2319 n78, n85, n15, n26, n37, n48,
2320 n1234, n1256, n2367, n3478, n1458, n5678, nCenter, id);
2322 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6, n7, n8,
2323 n12, n23, n34, n41, n56, n67,
2324 n78, n85, n15, n26, n37, n48,
2325 n1234, n1256, n2367, n3478, n1458, n5678, nCenter);
2330 elem = meshDS->AddVolumeWithID(n1, n2, n3, n4, n5, n6, n7, n8,
2331 n12, n23, n34, n41, n56, n67,
2332 n78, n85, n15, n26, n37, n48, id);
2334 elem = meshDS->AddVolume(n1, n2, n3, n4, n5, n6, n7, n8,
2335 n12, n23, n34, n41, n56, n67,
2336 n78, n85, n15, n26, n37, n48);
2339 if ( mySetElemOnShape && myShapeID > 0 )
2340 meshDS->SetMeshElementOnShape( elem, myShapeID );
2345 //=======================================================================
2346 //function : AddVolume
2347 //purpose : Creates LINEAR!!!!!!!!! octahedron
2348 //=======================================================================
2350 SMDS_MeshVolume* SMESH_MesherHelper::AddVolume(const SMDS_MeshNode* n1,
2351 const SMDS_MeshNode* n2,
2352 const SMDS_MeshNode* n3,
2353 const SMDS_MeshNode* n4,
2354 const SMDS_MeshNode* n5,
2355 const SMDS_MeshNode* n6,
2356 const SMDS_MeshNode* n7,
2357 const SMDS_MeshNode* n8,
2358 const SMDS_MeshNode* n9,
2359 const SMDS_MeshNode* n10,
2360 const SMDS_MeshNode* n11,
2361 const SMDS_MeshNode* n12,
2365 SMESHDS_Mesh * meshDS = GetMeshDS();
2366 SMDS_MeshVolume* elem = 0;
2368 elem = meshDS->AddVolumeWithID(n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,id);
2370 elem = meshDS->AddVolume(n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12);
2371 if ( mySetElemOnShape && myShapeID > 0 )
2372 meshDS->SetMeshElementOnShape( elem, myShapeID );
2376 //=======================================================================
2377 //function : AddPolyhedralVolume
2378 //purpose : Creates polyhedron. In quadratic mesh, adds medium nodes
2379 //=======================================================================
2382 SMESH_MesherHelper::AddPolyhedralVolume (const std::vector<const SMDS_MeshNode*>& nodes,
2383 const std::vector<int>& quantities,
2387 SMESHDS_Mesh * meshDS = GetMeshDS();
2388 SMDS_MeshVolume* elem = 0;
2389 if(!myCreateQuadratic)
2392 elem = meshDS->AddPolyhedralVolumeWithID(nodes, quantities, id);
2394 elem = meshDS->AddPolyhedralVolume(nodes, quantities);
2398 vector<const SMDS_MeshNode*> newNodes;
2399 vector<int> newQuantities;
2400 for ( size_t iFace = 0, iN = 0; iFace < quantities.size(); ++iFace )
2402 int nbNodesInFace = quantities[iFace];
2403 newQuantities.push_back(0);
2404 for ( int i = 0; i < nbNodesInFace; ++i )
2406 const SMDS_MeshNode* n1 = nodes[ iN + i ];
2407 newNodes.push_back( n1 );
2408 newQuantities.back()++;
2410 const SMDS_MeshNode* n2 = nodes[ iN + ( i+1==nbNodesInFace ? 0 : i+1 )];
2411 // if ( n1->GetPosition()->GetTypeOfPosition() != SMDS_TOP_3DSPACE &&
2412 // n2->GetPosition()->GetTypeOfPosition() != SMDS_TOP_3DSPACE )
2414 const SMDS_MeshNode* n12 = GetMediumNode( n1, n2, force3d, TopAbs_SOLID );
2415 newNodes.push_back( n12 );
2416 newQuantities.back()++;
2419 iN += nbNodesInFace;
2422 elem = meshDS->AddPolyhedralVolumeWithID( newNodes, newQuantities, id );
2424 elem = meshDS->AddPolyhedralVolume( newNodes, newQuantities );
2426 if ( mySetElemOnShape && myShapeID > 0 )
2427 meshDS->SetMeshElementOnShape( elem, myShapeID );
2434 //================================================================================
2436 * \brief Check if a node belongs to any face of sub-mesh
2438 //================================================================================
2440 bool isNodeInSubMesh( const SMDS_MeshNode* n, const SMESHDS_SubMesh* sm )
2442 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator( SMDSAbs_Face );
2443 while ( fIt->more() )
2444 if ( sm->Contains( fIt->next() ))
2450 //=======================================================================
2451 //function : IsSameElemGeometry
2452 //purpose : Returns true if all elements of a sub-mesh are of same shape
2453 //=======================================================================
2455 bool SMESH_MesherHelper::IsSameElemGeometry(const SMESHDS_SubMesh* smDS,
2456 SMDSAbs_GeometryType shape,
2457 const bool nullSubMeshRes)
2459 if ( !smDS ) return nullSubMeshRes;
2461 SMDS_ElemIteratorPtr elemIt = smDS->GetElements();
2462 while ( elemIt->more() ) {
2463 const SMDS_MeshElement* e = elemIt->next();
2464 if ( e->GetGeomType() != shape )
2470 //=======================================================================
2471 //function : LoadNodeColumns
2472 //purpose : Load nodes bound to face into a map of node columns
2473 //=======================================================================
2475 bool SMESH_MesherHelper::LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
2476 const TopoDS_Face& theFace,
2477 const TopoDS_Edge& theBaseEdge,
2478 SMESHDS_Mesh* theMesh,
2479 SMESH_ProxyMesh* theProxyMesh)
2481 return LoadNodeColumns(theParam2ColumnMap,
2483 std::list<TopoDS_Edge>(1,theBaseEdge),
2488 //=======================================================================
2489 //function : LoadNodeColumns
2490 //purpose : Load nodes bound to face into a map of node columns
2491 //=======================================================================
2493 bool SMESH_MesherHelper::LoadNodeColumns(TParam2ColumnMap & theParam2ColumnMap,
2494 const TopoDS_Face& theFace,
2495 const std::list<TopoDS_Edge>& theBaseSide,
2496 SMESHDS_Mesh* theMesh,
2497 SMESH_ProxyMesh* theProxyMesh)
2499 // get a right sub-mesh of theFace
2501 const SMESHDS_SubMesh* faceSubMesh = 0;
2504 faceSubMesh = theProxyMesh->GetSubMesh( theFace );
2505 if ( !faceSubMesh ||
2506 faceSubMesh->NbElements() == 0 ||
2507 theProxyMesh->IsTemporary( faceSubMesh->GetElements()->next() ))
2509 // can use a proxy sub-mesh with not temporary elements only
2515 faceSubMesh = theMesh->MeshElements( theFace );
2516 if ( !faceSubMesh || faceSubMesh->NbElements() == 0 )
2519 if ( theParam2ColumnMap.empty() )
2521 // get data of edges for normalization of params
2522 vector< double > length;
2524 list<TopoDS_Edge>::const_iterator edge;
2526 for ( edge = theBaseSide.begin(); edge != theBaseSide.end(); ++edge )
2528 double len = std::max( 1e-10, SMESH_Algo::EdgeLength( *edge ));
2530 length.push_back( len );
2534 // get nodes on theBaseEdge sorted by param on edge and initialize theParam2ColumnMap with them
2535 edge = theBaseSide.begin();
2536 for ( int iE = 0; edge != theBaseSide.end(); ++edge, ++iE )
2538 map< double, const SMDS_MeshNode*> sortedBaseNN;
2539 SMESH_Algo::GetSortedNodesOnEdge( theMesh, *edge,/*noMedium=*/true, sortedBaseNN );
2541 map< double, const SMDS_MeshNode*>::iterator u_n;
2542 // pb with mesh_Projection_2D_00/A1 fixed by adding expectedSupport arg to GetMediumPos()
2543 // so the following solution is commented (hope forever :)
2545 // SMESH_Algo::GetSortedNodesOnEdge( theMesh, *edge,/*noMedium=*/true, sortedBaseNN,
2546 // // SMDSAbs_Edge here is needed to be coherent with
2547 // // StdMeshers_FaceSide used by Quadrangle to get nodes
2548 // // on EDGE; else pb in mesh_Projection_2D_00/A1 where a
2549 // // medium node on EDGE is medium in a triangle but not
2552 // if ( faceSubMesh->GetElements()->next()->IsQuadratic() )
2553 // // filter off nodes medium in faces on theFace (same pb with mesh_Projection_2D_00/A1)
2554 // for ( u_n = sortedBaseNN.begin(); u_n != sortedBaseNN.end() ; )
2556 // const SMDS_MeshNode* node = u_n->second;
2557 // SMDS_ElemIteratorPtr faceIt = node->GetInverseElementIterator( SMDSAbs_Face );
2558 // if ( faceIt->more() && node ) {
2559 // const SMDS_MeshElement* face = faceIt->next();
2560 // if ( faceSubMesh->Contains( face ) && face->IsMediumNode( node ))
2564 // sortedBaseNN.erase( u_n++ );
2568 if ( sortedBaseNN.empty() ) continue;
2570 u_n = sortedBaseNN.begin();
2571 if ( theProxyMesh ) // from sortedBaseNN remove nodes not shared by faces of faceSubMesh
2573 const SMDS_MeshNode* n1 = (++sortedBaseNN.begin())->second;
2574 const SMDS_MeshNode* n2 = (++sortedBaseNN.rbegin())->second;
2575 bool allNodesAreProxy = ( n1 != theProxyMesh->GetProxyNode( n1 ) &&
2576 n2 != theProxyMesh->GetProxyNode( n2 ));
2577 if ( allNodesAreProxy )
2578 for ( u_n = sortedBaseNN.begin(); u_n != sortedBaseNN.end(); u_n++ )
2579 u_n->second = theProxyMesh->GetProxyNode( u_n->second );
2581 if ( u_n = sortedBaseNN.begin(), !isNodeInSubMesh( u_n->second, faceSubMesh ))
2583 while ( ++u_n != sortedBaseNN.end() && !isNodeInSubMesh( u_n->second, faceSubMesh ));
2584 sortedBaseNN.erase( sortedBaseNN.begin(), u_n );
2586 if ( !sortedBaseNN.empty() )
2587 if ( u_n = --sortedBaseNN.end(), !isNodeInSubMesh( u_n->second, faceSubMesh ))
2589 while ( u_n != sortedBaseNN.begin() && !isNodeInSubMesh( (--u_n)->second, faceSubMesh ));
2590 sortedBaseNN.erase( ++u_n, sortedBaseNN.end() );
2592 if ( sortedBaseNN.empty() ) continue;
2596 BRep_Tool::Range( *edge, f, l );
2597 if ( edge->Orientation() == TopAbs_REVERSED ) std::swap( f, l );
2598 const double coeff = 1. / ( l - f ) * length[iE] / fullLen;
2599 const double prevPar = theParam2ColumnMap.empty() ? 0 : theParam2ColumnMap.rbegin()->first;
2600 for ( u_n = sortedBaseNN.begin(); u_n != sortedBaseNN.end(); u_n++ )
2602 double par = prevPar + coeff * ( u_n->first - f );
2603 TParam2ColumnMap::iterator u2nn =
2604 theParam2ColumnMap.insert( theParam2ColumnMap.end(), make_pair( par, TNodeColumn()));
2605 u2nn->second.push_back( u_n->second );
2608 if ( theParam2ColumnMap.size() < 2 )
2613 size_t prevNbRows = theParam2ColumnMap.begin()->second.size(); // current, at least 1 here
2614 size_t expectNbRows = faceSubMesh->NbElements() / ( theParam2ColumnMap.size()-1 ); // to be added
2616 // fill theParam2ColumnMap column by column by passing from nodes on
2617 // theBaseEdge up via mesh faces on theFace
2619 TParam2ColumnMap::iterator par_nVec_1, par_nVec_2;
2620 par_nVec_2 = theParam2ColumnMap.begin();
2621 par_nVec_1 = par_nVec_2++;
2622 TIDSortedElemSet emptySet, avoidSet;
2623 for ( ; par_nVec_2 != theParam2ColumnMap.end(); ++par_nVec_1, ++par_nVec_2 )
2625 vector<const SMDS_MeshNode*>& nCol1 = par_nVec_1->second;
2626 vector<const SMDS_MeshNode*>& nCol2 = par_nVec_2->second;
2627 nCol1.resize( prevNbRows + expectNbRows );
2628 nCol2.resize( prevNbRows + expectNbRows );
2630 int i1, i2; size_t foundNbRows = 0;
2631 const SMDS_MeshNode *n1 = nCol1[ prevNbRows-1 ];
2632 const SMDS_MeshNode *n2 = nCol2[ prevNbRows-1 ];
2633 // find face sharing node n1 and n2 and belonging to faceSubMesh
2634 while ( const SMDS_MeshElement* face =
2635 SMESH_MeshAlgos::FindFaceInSet( n1, n2, emptySet, avoidSet, &i1, &i2))
2637 if ( faceSubMesh->Contains( face ))
2639 int nbNodes = face->NbCornerNodes();
2642 if ( foundNbRows + 1 > expectNbRows )
2644 n1 = face->GetNode( (i2+2) % 4 ); // opposite corner of quadrangle face
2645 n2 = face->GetNode( (i1+2) % 4 );
2646 nCol1[ prevNbRows + foundNbRows] = n1;
2647 nCol2[ prevNbRows + foundNbRows] = n2;
2650 avoidSet.insert( face );
2652 if ((size_t) foundNbRows != expectNbRows )
2656 return ( theParam2ColumnMap.size() > 1 &&
2657 theParam2ColumnMap.begin()->second.size() == prevNbRows + expectNbRows );
2662 //================================================================================
2664 * \brief Return true if a node is at a corner of a 2D structured mesh of FACE
2666 //================================================================================
2668 bool isCornerOfStructure( const SMDS_MeshNode* n,
2669 const SMESHDS_SubMesh* faceSM,
2670 SMESH_MesherHelper& faceAnalyser )
2672 int nbFacesInSM = 0;
2674 SMDS_ElemIteratorPtr fIt = n->GetInverseElementIterator( SMDSAbs_Face );
2675 while ( fIt->more() )
2676 nbFacesInSM += faceSM->Contains( fIt->next() );
2678 if ( nbFacesInSM == 1 )
2681 if ( nbFacesInSM == 2 && n->GetPosition()->GetTypeOfPosition() == SMDS_TOP_VERTEX )
2683 return faceAnalyser.IsRealSeam( n->getshapeId() );
2689 //=======================================================================
2690 //function : IsStructured
2691 //purpose : Return true if 2D mesh on FACE is a structured rectangle
2692 //=======================================================================
2694 bool SMESH_MesherHelper::IsStructured( SMESH_subMesh* faceSM )
2696 SMESHDS_SubMesh* fSM = faceSM->GetSubMeshDS();
2697 if ( !fSM || fSM->NbElements() == 0 )
2700 list< TopoDS_Edge > edges;
2701 list< int > nbEdgesInWires;
2702 int nbWires = SMESH_Block::GetOrderedEdges( TopoDS::Face( faceSM->GetSubShape() ),
2703 edges, nbEdgesInWires );
2704 if ( nbWires != 1 /*|| nbEdgesInWires.front() != 4*/ ) // allow composite sides
2707 // algo: find corners of a structure and then analyze nb of faces and
2708 // length of structure sides
2710 SMESHDS_Mesh* meshDS = faceSM->GetFather()->GetMeshDS();
2711 SMESH_MesherHelper faceAnalyser( *faceSM->GetFather() );
2712 faceAnalyser.SetSubShape( faceSM->GetSubShape() );
2714 // rotate edges to get the first node being at corner
2715 // (in principle it's not necessary because so far none SALOME algo can make
2716 // such a structured mesh that all corner nodes are not on VERTEXes)
2717 bool isCorner = false;
2718 int nbRemainEdges = nbEdgesInWires.front();
2720 TopoDS_Vertex V = IthVertex( 0, edges.front() );
2721 isCorner = isCornerOfStructure( SMESH_Algo::VertexNode( V, meshDS ),
2724 edges.splice( edges.end(), edges, edges.begin() );
2728 while ( !isCorner && nbRemainEdges > 0 );
2733 // get all nodes from EDGEs
2734 list< const SMDS_MeshNode* > nodes;
2735 list< TopoDS_Edge >::iterator edge = edges.begin();
2736 for ( ; edge != edges.end(); ++edge )
2738 map< double, const SMDS_MeshNode* > u2Nodes;
2739 if ( !SMESH_Algo::GetSortedNodesOnEdge( meshDS, *edge,
2740 /*skipMedium=*/true, u2Nodes ))
2743 list< const SMDS_MeshNode* > edgeNodes;
2744 map< double, const SMDS_MeshNode* >::iterator u2n = u2Nodes.begin();
2745 for ( ; u2n != u2Nodes.end(); ++u2n )
2746 edgeNodes.push_back( u2n->second );
2747 if ( edge->Orientation() == TopAbs_REVERSED )
2748 edgeNodes.reverse();
2750 if ( !nodes.empty() && nodes.back() == edgeNodes.front() )
2751 edgeNodes.pop_front();
2752 nodes.splice( nodes.end(), edgeNodes, edgeNodes.begin(), edgeNodes.end() );
2755 // get length of structured sides
2756 vector<int> nbEdgesInSide;
2758 list< const SMDS_MeshNode* >::iterator n = ++nodes.begin();
2759 for ( ; n != nodes.end(); ++n )
2762 if ( isCornerOfStructure( *n, fSM, faceAnalyser )) {
2763 nbEdgesInSide.push_back( nbEdges );
2769 if ( nbEdgesInSide.size() != 4 )
2771 if ( nbEdgesInSide[0] != nbEdgesInSide[2] )
2773 if ( nbEdgesInSide[1] != nbEdgesInSide[3] )
2775 if ( nbEdgesInSide[0] * nbEdgesInSide[1] != fSM->NbElements() )
2781 //=======================================================================
2782 //function : IsDistorted2D
2783 //purpose : Return true if 2D mesh on FACE is ditorted
2784 //=======================================================================
2786 bool SMESH_MesherHelper::IsDistorted2D( SMESH_subMesh* faceSM,
2789 if ( !faceSM || faceSM->GetSubShape().ShapeType() != TopAbs_FACE )
2792 bool haveBadFaces = false;
2794 SMESH_MesherHelper helper( *faceSM->GetFather() );
2795 helper.SetSubShape( faceSM->GetSubShape() );
2797 const TopoDS_Face& F = TopoDS::Face( faceSM->GetSubShape() );
2798 SMESHDS_SubMesh* smDS = helper.GetMeshDS()->MeshElements( F );
2799 if ( !smDS || smDS->NbElements() == 0 ) return false;
2801 SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
2802 double prevArea = 0;
2803 vector< const SMDS_MeshNode* > nodes;
2805 bool* toCheckUV = checkUV ? & checkUV : 0;
2806 while ( faceIt->more() && !haveBadFaces )
2808 const SMDS_MeshElement* face = faceIt->next();
2811 nodes.resize( face->NbCornerNodes() );
2812 SMDS_MeshElement::iterator n = face->begin_nodes();
2813 for ( size_t i = 0; i < nodes.size(); ++n, ++i )
2816 // avoid elems on degenarate shapes as UV on them can be wrong
2817 if ( helper.HasDegeneratedEdges() )
2819 bool isOnDegen = false;
2820 for ( size_t i = 0; ( i < nodes.size() && !isOnDegen ); ++i )
2821 isOnDegen = helper.IsDegenShape( nodes[ i ]->getshapeId() );
2825 // prepare to getting UVs
2826 const SMDS_MeshNode* inFaceNode = 0;
2827 if ( helper.HasSeam() ) {
2828 for ( size_t i = 0; ( i < nodes.size() && !inFaceNode ); ++i )
2829 if ( !helper.IsSeamShape( nodes[ i ]->getshapeId() ))
2830 inFaceNode = nodes[ i ];
2835 uv.resize( nodes.size() );
2836 for ( size_t i = 0; i < nodes.size(); ++i )
2837 uv[ i ] = helper.GetNodeUV( F, nodes[ i ], inFaceNode, toCheckUV );
2839 // compare orientation of triangles
2840 double faceArea = 0;
2841 for ( int iT = 0, nbT = nodes.size()-2; iT < nbT; ++iT )
2843 gp_XY v1 = uv[ iT+1 ] - uv[ 0 ];
2844 gp_XY v2 = uv[ iT+2 ] - uv[ 0 ];
2845 faceArea += v2 ^ v1;
2847 haveBadFaces = ( faceArea * prevArea < 0 );
2848 prevArea = faceArea;
2851 return haveBadFaces;
2854 //================================================================================
2856 * \brief Find out elements orientation on a geometrical face
2857 * \param theFace - The face correctly oriented in the shape being meshed
2858 * \retval bool - true if the face normal and the normal of first element
2859 * in the correspoding submesh point in different directions
2861 //================================================================================
2863 bool SMESH_MesherHelper::IsReversedSubMesh (const TopoDS_Face& theFace)
2865 if ( theFace.IsNull() )
2868 // find out orientation of a meshed face
2869 int faceID = GetMeshDS()->ShapeToIndex( theFace );
2870 TopoDS_Shape aMeshedFace = GetMeshDS()->IndexToShape( faceID );
2871 bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
2873 const SMESHDS_SubMesh * aSubMeshDSFace = GetMeshDS()->MeshElements( faceID );
2874 if ( !aSubMeshDSFace )
2877 // find an element on a bounday of theFace
2878 SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
2879 const SMDS_MeshNode* nn[2];
2880 while ( iteratorElem->more() ) // loop on elements on theFace
2882 const SMDS_MeshElement* elem = iteratorElem->next();
2883 if ( ! elem ) continue;
2885 // look for 2 nodes on EDGE
2886 int nbNodes = elem->NbCornerNodes();
2887 nn[0] = elem->GetNode( nbNodes-1 );
2888 for ( int iN = 0; iN < nbNodes; ++iN )
2890 nn[1] = elem->GetNode( iN );
2891 if ( nn[0]->GetPosition()->GetDim() < 2 &&
2892 nn[1]->GetPosition()->GetDim() < 2 )
2894 TopoDS_Shape s0 = GetSubShapeByNode( nn[0], GetMeshDS() );
2895 TopoDS_Shape s1 = GetSubShapeByNode( nn[1], GetMeshDS() );
2896 TopoDS_Shape E = GetCommonAncestor( s0, s1, *myMesh, TopAbs_EDGE );
2897 if ( !E.IsNull() && !s0.IsSame( s1 ))
2901 for ( TopExp_Explorer exp( theFace, TopAbs_EDGE ); exp.More(); exp.Next() )
2902 if ( E.IsSame( exp.Current() )) {
2904 E = exp.Current(); // to know orientation
2909 double u0 = GetNodeU( TopoDS::Edge( E ), nn[0], nn[1], &ok );
2910 double u1 = GetNodeU( TopoDS::Edge( E ), nn[1], nn[0], &ok );
2911 // check that the 2 nodes are connected with a segment (IPAL53055)
2912 if ( SMESHDS_SubMesh* sm = GetMeshDS()->MeshElements( E ))
2913 if ( sm->NbElements() > 0 && !GetMeshDS()->FindEdge( nn[0], nn[1] ))
2917 isReversed = ( u0 > u1 );
2918 if ( E.Orientation() == TopAbs_REVERSED )
2919 isReversed = !isReversed;
2929 // find an element with a good normal
2931 bool normalOK = false;
2933 iteratorElem = aSubMeshDSFace->GetElements();
2934 while ( !normalOK && iteratorElem->more() ) // loop on elements on theFace
2936 const SMDS_MeshElement* elem = iteratorElem->next();
2937 if ( ! SMESH_MeshAlgos::FaceNormal( elem, const_cast<gp_XYZ&>( Ne.XYZ() ), /*normalized=*/0 ))
2941 // get UV of a node inside theFACE
2942 SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
2943 const SMDS_MeshNode* nInFace = 0;
2944 int iPosDim = SMDS_TOP_VERTEX;
2945 while ( nodesIt->more() ) // loop on nodes
2947 const SMDS_MeshNode* n = static_cast<const SMDS_MeshNode*>( nodesIt->next() );
2948 if ( n->GetPosition()->GetTypeOfPosition() >= iPosDim )
2951 iPosDim = n->GetPosition()->GetTypeOfPosition();
2954 uv = GetNodeUV( theFace, nInFace, 0, &normalOK );
2959 // face normal at node position
2960 TopLoc_Location loc;
2961 Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
2962 // if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 )
2963 // some surfaces not detected as GeomAbs_C1 are nevertheless correct for meshing
2964 if ( surf.IsNull() || surf->Continuity() < GeomAbs_C0 )
2967 gp_Vec d1u, d1v; gp_Pnt p;
2968 surf->D1( uv.X(), uv.Y(), p, d1u, d1v );
2969 gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
2971 if ( theFace.Orientation() == TopAbs_REVERSED )
2974 return Ne * Nf < 0.;
2977 //=======================================================================
2979 //purpose : Count nb of sub-shapes
2980 //=======================================================================
2982 int SMESH_MesherHelper::Count(const TopoDS_Shape& shape,
2983 const TopAbs_ShapeEnum type,
2984 const bool ignoreSame)
2987 TopTools_IndexedMapOfShape map;
2988 TopExp::MapShapes( shape, type, map );
2989 return map.Extent();
2993 for ( TopExp_Explorer exp( shape, type ); exp.More(); exp.Next() )