+ double getUOnEdgeByPoint( const size_t iEdge,
+ const NodePoint* point,
+ SinuousFace& sinuFace )
+ {
+ if ( point->_edgeInd == iEdge )
+ return point->_u;
+
+ TopoDS_Vertex V0 = TopExp::FirstVertex( sinuFace._sinuEdges[ iEdge ]);
+ TopoDS_Vertex V1 = TopExp::LastVertex ( sinuFace._sinuEdges[ iEdge ]);
+ gp_Pnt p0 = BRep_Tool::Pnt( V0 );
+ gp_Pnt p1 = BRep_Tool::Pnt( V1 );
+ gp_Pnt p = point->Point( sinuFace._sinuCurves );
+
+ double f,l;
+ BRep_Tool::Range( sinuFace._sinuEdges[ iEdge ], f,l );
+ return p.SquareDistance( p0 ) < p.SquareDistance( p1 ) ? f : l;
+ }
+
+ //================================================================================
+ /*!
+ * \brief Move coincident nodes to make node params on EDGE unique
+ * \param [in] theHelper - the helper
+ * \param [in] thePointsOnE - nodes on two opposite river sides
+ * \param [in] theSinuFace - the sinuous FACE
+ * \param [out] theNodes2Merge - the map of nodes to merge
+ */
+ //================================================================================
+
+ void separateNodes( SMESH_MesherHelper& theHelper,
+ const SMESH_MAT2d::MedialAxis& theMA,
+ TMAPar2NPoints & thePointsOnE,
+ SinuousFace& theSinuFace,
+ const vector< bool >& theIsComputedEdge)
+ {
+ if ( thePointsOnE.size() < 2 )
+ return;
+
+ SMESHDS_Mesh* meshDS = theHelper.GetMeshDS();
+ const vector<TopoDS_Edge>& theSinuEdges = theSinuFace._sinuEdges;
+ const vector< Handle(Geom_Curve) >& curves = theSinuFace._sinuCurves;
+
+ //SMESH_MAT2d::BoundaryPoint bp[2];
+ //const SMESH_MAT2d::Branch& branch = *theMA.getBranch(0);
+
+ typedef TMAPar2NPoints::iterator TIterator;
+
+ for ( int iSide = 0; iSide < 2; ++iSide ) // loop on two sinuous sides
+ {
+ // get a tolerance to compare points
+ double tol = Precision::Confusion();
+ for ( size_t i = 0; i < theSinuFace._sinuSide[ iSide ].size(); ++i )
+ tol = Max( tol , BRep_Tool::Tolerance( theSinuFace._sinuSide[ iSide ][ i ]));
+
+ // find coincident points
+ TIterator u2NP = thePointsOnE.begin();
+ vector< TIterator > sameU2NP( 1, u2NP++ );
+ while ( u2NP != thePointsOnE.end() )
+ {
+ for ( ; u2NP != thePointsOnE.end(); ++u2NP )
+ {
+ NodePoint& np1 = get( sameU2NP.back()->second, iSide );
+ NodePoint& np2 = get( u2NP ->second, iSide );
+
+ if (( !np1._node || !np2._node ) &&
+ ( np1.Point( curves ).SquareDistance( np2.Point( curves )) < tol*tol ))
+ {
+ sameU2NP.push_back( u2NP );
+ }
+ else if ( sameU2NP.size() == 1 )
+ {
+ sameU2NP[ 0 ] = u2NP;
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ if ( sameU2NP.size() > 1 )
+ {
+ // find an existing node on VERTEX among sameU2NP and get underlying EDGEs
+ const SMDS_MeshNode* existingNode = 0;
+ set< size_t > edgeInds;
+ NodePoint* np;
+ for ( size_t i = 0; i < sameU2NP.size(); ++i )
+ {
+ np = &get( sameU2NP[i]->second, iSide );
+ if ( np->_node )
+ if ( !existingNode || np->_node->GetPosition()->GetDim() == 0 )
+ existingNode = np->_node;
+ edgeInds.insert( np->_edgeInd );
+ }
+ list< const SMDS_MeshNode* >& mergeNodes = theSinuFace._nodesToMerge[ existingNode ];
+
+ TIterator u2NPprev = sameU2NP.front();
+ TIterator u2NPnext = sameU2NP.back() ;
+ if ( u2NPprev->first < 0. ) ++u2NPprev;
+ if ( u2NPnext->first > 1. ) --u2NPnext;
+
+ set< size_t >::iterator edgeID = edgeInds.begin();
+ for ( ; edgeID != edgeInds.end(); ++edgeID )
+ {
+ // get U range on iEdge within which the equal points will be distributed
+ double u0, u1;
+ np = &get( u2NPprev->second, iSide );
+ u0 = getUOnEdgeByPoint( *edgeID, np, theSinuFace );
+
+ np = &get( u2NPnext->second, iSide );
+ u1 = getUOnEdgeByPoint( *edgeID, np, theSinuFace );
+
+ if ( u0 == u1 )
+ {
+ if ( u2NPprev != thePointsOnE.begin() ) --u2NPprev;
+ if ( u2NPnext != --thePointsOnE.end() ) ++u2NPnext;
+ np = &get( u2NPprev->second, iSide );
+ u0 = getUOnEdgeByPoint( *edgeID, np, theSinuFace );
+ np = &get( u2NPnext->second, iSide );
+ u1 = getUOnEdgeByPoint( *edgeID, np, theSinuFace );
+ }
+
+ // distribute points and create nodes
+ double du = ( u1 - u0 ) / ( sameU2NP.size() + 1 /*!existingNode*/ );
+ double u = u0 + du;
+ for ( size_t i = 0; i < sameU2NP.size(); ++i )
+ {
+ np = &get( sameU2NP[i]->second, iSide );
+ if ( !np->_node && *edgeID == np->_edgeInd )
+ {
+ np->_u = u;
+ u += du;
+ gp_Pnt p = np->Point( curves );
+ np->_node = meshDS->AddNode( p.X(), p.Y(), p.Z() );
+ meshDS->SetNodeOnEdge( np->_node, theSinuEdges[ *edgeID ], np->_u );
+
+ if ( theIsComputedEdge[ *edgeID ])
+ mergeNodes.push_back( np->_node );
+ }
+ }
+ }
+
+ sameU2NP.resize( 1 );
+ u2NP = ++sameU2NP.back();
+ sameU2NP[ 0 ] = u2NP;
+
+ } // if ( sameU2NP.size() > 1 )
+ } // while ( u2NP != thePointsOnE.end() )
+ } // for ( int iSide = 0; iSide < 2; ++iSide )
+
+ return;
+ } // separateNodes()
+
+
+ //================================================================================
+ /*!
+ * \brief Find association of nodes existing on the sinuous sides of a ring
+ *
+ * TMAPar2NPoints filled here is used in setQuadSides() only if theSinuFace.IsRing()
+ * to find most distant nodes of the inner and the outer wires
+ */
+ //================================================================================
+
+ void assocNodes( SMESH_MesherHelper& theHelper,
+ SinuousFace& theSinuFace,
+ const SMESH_MAT2d::MedialAxis& theMA,
+ TMAPar2NPoints & thePointsOnE )
+ {
+ SMESH_Mesh* mesh = theHelper.GetMesh();
+ SMESHDS_Mesh* meshDS = theHelper.GetMeshDS();
+
+ list< TopoDS_Edge > ee1( theSinuFace._sinuSide [0].begin(), theSinuFace._sinuSide [0].end() );
+ list< TopoDS_Edge > ee2( theSinuFace._sinuSide [1].begin(), theSinuFace._sinuSide [1].end() );
+ StdMeshers_FaceSide sideOut( theSinuFace.Face(), ee1, mesh, true, true, &theHelper );
+ StdMeshers_FaceSide sideIn ( theSinuFace.Face(), ee2, mesh, true, true, &theHelper );
+ const UVPtStructVec& uvsOut = sideOut.GetUVPtStruct();
+ const UVPtStructVec& uvsIn = sideIn.GetUVPtStruct();
+ // if ( uvs1.size() != uvs2.size() )
+ // return;
+
+ const SMESH_MAT2d::Branch& branch = *theMA.getBranch(0);
+ SMESH_MAT2d::BoundaryPoint bp[2];
+ SMESH_MAT2d::BranchPoint brp;
+
+ map< double, const SMDS_MeshNode* > nodeParams; // params of existing nodes
+ map< double, const SMDS_MeshNode* >::iterator u2n;
+
+ // find a node of sideOut most distant from sideIn
+
+ vector< BRepAdaptor_Curve > curvesIn( theSinuFace._sinuSide[1].size() );
+ for ( size_t iE = 0; iE < theSinuFace._sinuSide[1].size(); ++iE )
+ curvesIn[ iE ].Initialize( theSinuFace._sinuSide[1][iE] );
+
+ double maxDist = 0;
+ SMESH_MAT2d::BoundaryPoint bpIn; // closest IN point
+ const SMDS_MeshNode* nOut = 0;
+ const size_t nbEOut = theSinuFace._sinuSide[0].size();
+ for ( size_t iE = 0; iE < nbEOut; ++iE )
+ {
+ const TopoDS_Edge& E = theSinuFace._sinuSide[0][iE];
+
+ if ( !SMESH_Algo::GetSortedNodesOnEdge( meshDS, E, /*skipMedium=*/true, nodeParams ))
+ return;
+ for ( u2n = nodeParams.begin(); u2n != nodeParams.end(); ++u2n )
+ {
+ // point on EDGE (u2n) --> MA point (brp)
+ if ( !theMA.getBoundary().getBranchPoint( iE, u2n->first, brp ) ||
+ !branch.getBoundaryPoints( brp, bp[0], bp[1] ))
+ return;
+ gp_Pnt pOut = SMESH_TNodeXYZ( u2n->second );
+ gp_Pnt pIn = curvesIn[ bp[1]._edgeIndex - nbEOut ].Value( bp[1]._param );
+ double dist = pOut.SquareDistance( pIn );
+ if ( dist > maxDist )
+ {
+ maxDist = dist;
+ nOut = u2n->second;
+ bpIn = bp[1];
+ }
+ }
+ }
+ const SMDS_MeshNode* nIn = 0;
+ if ( !SMESH_Algo::GetSortedNodesOnEdge( meshDS,
+ theSinuFace._sinuEdges[ bpIn._edgeIndex ],
+ /*skipMedium=*/true,
+ nodeParams ))
+ return;
+ u2n = nodeParams.lower_bound( bpIn._param );
+ if ( u2n == nodeParams.end() )
+ nIn = nodeParams.rbegin()->second;
+ else
+ nIn = u2n->second;
+
+ // find position of distant nodes in uvsOut and uvsIn
+ size_t iDistOut, iDistIn;
+ for ( iDistOut = 0; iDistOut < uvsOut.size(); ++iDistOut )
+ {
+ if ( uvsOut[iDistOut].node == nOut )
+ break;
+ }
+ for ( iDistIn = 0; iDistIn < uvsIn.size(); ++iDistIn )
+ {
+ if ( uvsIn[iDistIn].node == nIn )
+ break;
+ }
+ if ( iDistOut == uvsOut.size() || iDistIn == uvsIn.size() )
+ return;
+
+ // store opposite nodes in thePointsOnE (param and EDGE have no sense)
+ pair< NodePoint, NodePoint > oppNodes( NodePoint( nOut, 0, 0 ), NodePoint( nIn, 0, 0));
+ thePointsOnE.insert( make_pair( uvsOut[ iDistOut ].normParam, oppNodes ));
+ int iOut = iDistOut, iIn = iDistIn;
+ int i, nbNodes = std::min( uvsOut.size(), uvsIn.size() );
+ if ( nbNodes > 5 ) nbNodes = 5;
+ for ( i = 0, ++iOut, --iIn; i < nbNodes; ++iOut, --iIn, ++i )
+ {
+ iOut = theHelper.WrapIndex( iOut, uvsOut.size() );
+ iIn = theHelper.WrapIndex( iIn, uvsIn.size() );
+ oppNodes.first._node = uvsOut[ iOut ].node;
+ oppNodes.second._node = uvsIn[ iIn ].node;
+ thePointsOnE.insert( make_pair( uvsOut[ iOut ].normParam, oppNodes ));
+ }
+
+ return;
+ } // assocNodes()
+
+ //================================================================================
+ /*!
+ * \brief Setup sides of SinuousFace::_quad
+ * \param [in] theHelper - helper
+ * \param [in] thePointsOnEdges - NodePoint's on sinuous sides
+ * \param [in,out] theSinuFace - the FACE
+ * \param [in] the1dAlgo - algorithm to use for radial discretization of a ring FACE
+ * \return bool - is OK
+ */
+ //================================================================================
+
+ bool setQuadSides(SMESH_MesherHelper& theHelper,
+ const TMAPar2NPoints& thePointsOnEdges,
+ SinuousFace& theFace,
+ SMESH_Algo* the1dAlgo)
+ {
+ SMESH_Mesh* mesh = theHelper.GetMesh();
+ const TopoDS_Face& face = theFace._quad->face;
+ SMESH_ProxyMesh::Ptr proxyMesh = StdMeshers_ViscousLayers2D::Compute( *mesh, face );
+ if ( !proxyMesh )
+ return false;
+
+ list< TopoDS_Edge > side[4];
+ side[0].insert( side[0].end(), theFace._shortSide[0].begin(), theFace._shortSide[0].end() );
+ side[1].insert( side[1].end(), theFace._sinuSide [1].begin(), theFace._sinuSide [1].end() );
+ side[2].insert( side[2].end(), theFace._shortSide[1].begin(), theFace._shortSide[1].end() );
+ side[3].insert( side[3].end(), theFace._sinuSide [0].begin(), theFace._sinuSide [0].end() );
+
+ for ( int i = 0; i < 4; ++i )
+ {
+ theFace._quad->side[i] = StdMeshers_FaceSide::New( face, side[i], mesh, i < QUAD_TOP_SIDE,
+ /*skipMediumNodes=*/true,
+ &theHelper, proxyMesh );
+ }
+
+ if ( theFace.IsRing() )
+ {
+ // --------------------------------------
+ // Discretize a ring in radial direction
+ // --------------------------------------
+
+ if ( thePointsOnEdges.size() < 4 )
+ return false;
+
+ int nbOut = theFace._quad->side[ 1 ].GetUVPtStruct().size();
+ int nbIn = theFace._quad->side[ 3 ].GetUVPtStruct().size();
+ if ( nbOut == 0 || nbIn == 0 )
+ return false;
+
+ // find most distant opposite nodes
+ double maxDist = 0, dist;
+ TMAPar2NPoints::const_iterator u2NPdist, u2NP = thePointsOnEdges.begin();
+ for ( ; u2NP != thePointsOnEdges.end(); ++u2NP )
+ {
+ SMESH_TNodeXYZ xyz( u2NP->second.first._node ); // node out
+ dist = xyz.SquareDistance( u2NP->second.second._node );// node in
+ if ( dist > maxDist )
+ {
+ u2NPdist = u2NP;
+ maxDist = dist;
+ }
+ }
+ // compute distribution of radial nodes
+ list< double > params; // normalized params
+ static_cast< StdMeshers_QuadFromMedialAxis_1D2D::Algo1D* >
+ ( the1dAlgo )->ComputeDistribution( theHelper,
+ SMESH_TNodeXYZ( u2NPdist->second.first._node ),
+ SMESH_TNodeXYZ( u2NPdist->second.second._node ),
+ params );
+
+ // add a radial quad side
+
+ theHelper.SetElementsOnShape( true );
+ u2NP = thePointsOnEdges.begin();
+ const SMDS_MeshNode* nOut = u2NP->second.first._node;
+ const SMDS_MeshNode* nIn = u2NP->second.second._node;
+ nOut = proxyMesh->GetProxyNode( nOut );
+ nIn = proxyMesh->GetProxyNode( nIn );
+ gp_XY uvOut = theHelper.GetNodeUV( face, nOut );
+ gp_XY uvIn = theHelper.GetNodeUV( face, nIn );
+ Handle(Geom_Surface) surface = BRep_Tool::Surface( face );
+ UVPtStructVec uvsNew; UVPtStruct uvPt;
+ uvPt.node = nOut;
+ uvPt.u = uvOut.X();
+ uvPt.v = uvOut.Y();
+ uvsNew.push_back( uvPt );
+ for (list<double>::iterator itU = params.begin(); itU != params.end(); ++itU )
+ {
+ gp_XY uv = ( 1 - *itU ) * uvOut + *itU * uvIn; // applied in direction Out -> In
+ gp_Pnt p = surface->Value( uv.X(), uv.Y() );
+ uvPt.node = theHelper.AddNode( p.X(), p.Y(), p.Z(), /*id=*/0, uv.X(), uv.Y() );
+ uvPt.u = uv.X();
+ uvPt.v = uv.Y();
+ uvsNew.push_back( uvPt );
+ }
+ uvPt.node = nIn;
+ uvPt.u = uvIn.X();
+ uvPt.v = uvIn.Y();
+ uvsNew.push_back( uvPt );
+
+ theFace._quad->side[ 0 ] = StdMeshers_FaceSide::New( uvsNew );
+ theFace._quad->side[ 2 ] = theFace._quad->side[ 0 ];
+ if ( nbIn != nbOut )
+ theFace._quad->side[ 2 ] = StdMeshers_FaceSide::New( uvsNew );
+
+ // assure that the outer sinuous side starts at nOut
+ {
+ const UVPtStructVec& uvsOut = theFace._quad->side[ 3 ].GetUVPtStruct(); // _sinuSide[0]
+ size_t i; // find UVPtStruct holding nOut
+ for ( i = 0; i < uvsOut.size(); ++i )
+ if ( nOut == uvsOut[i].node )
+ break;
+ if ( i == uvsOut.size() )
+ return false;
+
+ if ( i != 0 && i != uvsOut.size()-1 )
+ {
+ // create a new OUT quad side
+ uvsNew.clear();
+ uvsNew.reserve( uvsOut.size() );
+ uvsNew.insert( uvsNew.end(), uvsOut.begin() + i, uvsOut.end() );
+ uvsNew.insert( uvsNew.end(), uvsOut.begin() + 1, uvsOut.begin() + i + 1);
+ theFace._quad->side[ 3 ] = StdMeshers_FaceSide::New( uvsNew );
+ }
+ }
+
+ // rotate the IN side if opposite nodes of IN and OUT sides don't match
+
+ const SMDS_MeshNode * nIn0 = theFace._quad->side[ 1 ].First().node;
+ if ( nIn0 != nIn )
+ {
+ nIn = proxyMesh->GetProxyNode( nIn );
+ const UVPtStructVec& uvsIn = theFace._quad->side[ 1 ].GetUVPtStruct(); // _sinuSide[1]
+ size_t i; // find UVPtStruct holding nIn
+ for ( i = 0; i < uvsIn.size(); ++i )
+ if ( nIn == uvsIn[i].node )
+ break;
+ if ( i == uvsIn.size() )
+ return false;
+
+ // create a new IN quad side
+ uvsNew.clear();
+ uvsNew.reserve( uvsIn.size() );
+ uvsNew.insert( uvsNew.end(), uvsIn.begin() + i, uvsIn.end() );
+ uvsNew.insert( uvsNew.end(), uvsIn.begin() + 1, uvsIn.begin() + i + 1);
+ theFace._quad->side[ 1 ] = StdMeshers_FaceSide::New( uvsNew );
+ }
+
+ if ( theFace._quad->side[ 1 ].GetUVPtStruct().empty() ||
+ theFace._quad->side[ 3 ].GetUVPtStruct().empty() )
+ return false;
+
+ } // if ( theFace.IsRing() )
+
+ return true;
+
+ } // setQuadSides()
+