+//================================================================================
+/*!
+ * \brief Return true if a node is on a boundary of 2D mesh.
+ * Optionally returns two neighboring boundary nodes (or more in non-manifold mesh)
+ */
+//================================================================================
+
+bool SMESH_MeshAlgos::IsOn2DBoundary( const SMDS_MeshNode* theNode,
+ std::vector< const SMDS_MeshNode*> * theNeibors )
+{
+ typedef NCollection_DataMap< SMESH_TLink, int, SMESH_TLink > TLinkCountMap;
+ TLinkCountMap linkCountMap( 10 );
+
+ int nbFreeLinks = 0;
+ for ( SMDS_ElemIteratorPtr fIt = theNode->GetInverseElementIterator(SMDSAbs_Face); fIt->more(); )
+ {
+ const SMDS_MeshElement* face = fIt->next();
+ const int nbCorners = face->NbCornerNodes();
+
+ int iN = face->GetNodeIndex( theNode );
+ int iPrev = ( iN - 1 + nbCorners ) % nbCorners;
+ int iNext = ( iN + 1 ) % nbCorners;
+
+ for ( int i : { iPrev, iNext } )
+ {
+ SMESH_TLink link( theNode, face->GetNode( i ));
+ int* count = linkCountMap.ChangeSeek( link );
+ if ( count ) ++( *count );
+ else linkCountMap.Bind( link, 1 );
+
+ if ( !count ) ++nbFreeLinks;
+ else --nbFreeLinks;
+ }
+ }
+
+ if ( theNeibors )
+ {
+ theNeibors->clear();
+ theNeibors->reserve( nbFreeLinks );
+ for ( TLinkCountMap::Iterator linkIt( linkCountMap ); linkIt.More(); linkIt.Next() )
+ if ( linkIt.Value() == 1 )
+ {
+ theNeibors->push_back( linkIt.Key().node1() );
+ if ( theNeibors->back() == theNode )
+ theNeibors->back() = linkIt.Key().node2();
+ }
+ }
+ return nbFreeLinks > 0;
+}
+
+//================================================================================
+/*!
+ * \brief Return true if node1 encounters first in the face and node2, after
+ */
+//================================================================================
+
+bool SMESH_MeshAlgos::IsRightOrder( const SMDS_MeshElement* face,
+ const SMDS_MeshNode* node0,
+ const SMDS_MeshNode* node1 )
+{
+ int i0 = face->GetNodeIndex( node0 );
+ int i1 = face->GetNodeIndex( node1 );
+ if ( face->IsQuadratic() )
+ {
+ if ( face->IsMediumNode( node0 ))
+ {
+ i0 -= ( face->NbNodes()/2 - 1 );
+ i1 *= 2;
+ }
+ else
+ {
+ i1 -= ( face->NbNodes()/2 - 1 );
+ i0 *= 2;
+ }
+ }
+ int diff = i1 - i0;
+ return ( diff == 1 ) || ( diff == -face->NbNodes()+1 );
+}
+
+//=======================================================================
+/*!
+ * \brief Partition given 1D elements into groups of contiguous edges.
+ * A node where number of meeting edges != 2 is a group end.
+ * An optional startNode is used to orient groups it belongs to.
+ * \return a list of edge groups and a list of corresponding node groups.
+ * If a group is closed, the first and last nodes of the group are same.
+ */
+//=======================================================================
+
+void SMESH_MeshAlgos::Get1DBranches( SMDS_ElemIteratorPtr theEdgeIt,
+ TElemGroupVector& theEdgeGroups,
+ TNodeGroupVector& theNodeGroups,
+ const SMDS_MeshNode* theStartNode )
+{
+ if ( !theEdgeIt )
+ return;
+
+ // build map of nodes and their adjacent edges
+
+ typedef std::vector< const SMDS_MeshNode* > TNodeVec;
+ typedef std::vector< const SMDS_MeshElement* > TEdgeVec;
+ typedef NCollection_DataMap< const SMDS_MeshNode*, TEdgeVec, SMESH_Hasher > TEdgesByNodeMap;
+ TEdgesByNodeMap edgesByNode;
+
+ while ( theEdgeIt->more() )
+ {
+ const SMDS_MeshElement* edge = theEdgeIt->next();
+ if ( edge->GetType() != SMDSAbs_Edge )
+ continue;
+
+ const SMDS_MeshNode* nodes[2] = { edge->GetNode(0), edge->GetNode(1) };
+ for ( int i = 0; i < 2; ++i )
+ {
+ TEdgeVec* nodeEdges = edgesByNode.ChangeSeek( nodes[i] );
+ if ( !nodeEdges )
+ {
+ nodeEdges = edgesByNode.Bound( nodes[i], TEdgeVec() );
+ nodeEdges->reserve(2);
+ }
+ nodeEdges->push_back( edge );
+ }
+ }
+
+ if ( edgesByNode.IsEmpty() )
+ return;
+
+
+ // build edge branches
+
+ TElemGroupVector branches(2);
+ TNodeGroupVector nodeBranches(2);
+
+ while ( !edgesByNode.IsEmpty() )
+ {
+ if ( !theStartNode || !edgesByNode.IsBound( theStartNode ))
+ {
+ theStartNode = TEdgesByNodeMap::Iterator( edgesByNode ).Key();
+ }
+
+ size_t nbBranches = 0;
+ bool startIsBranchEnd = false;
+
+ while ( edgesByNode.IsBound( theStartNode ))
+ {
+ // initialize a new branch
+
+ ++nbBranches;
+ if ( branches.size() < nbBranches )
+ {
+ branches.push_back ( TEdgeVec() );
+ nodeBranches.push_back( TNodeVec() );
+ }
+ TEdgeVec & branch = branches [ nbBranches - 1 ];
+ TNodeVec & nodeBranch = nodeBranches[ nbBranches - 1 ];
+ branch.clear();
+ nodeBranch.clear();
+ {
+ TEdgeVec& edges = edgesByNode( theStartNode );
+ startIsBranchEnd = ( edges.size() != 2 );
+
+ int nbEdges = 0;
+ const SMDS_MeshElement* startEdge = 0;
+ for ( size_t i = 0; i < edges.size(); ++i )
+ {
+ if ( !startEdge && edges[i] )
+ {
+ startEdge = edges[i];
+ edges[i] = 0;
+ }
+ nbEdges += bool( edges[i] );
+ }
+ if ( nbEdges == 0 )
+ edgesByNode.UnBind( theStartNode );
+ if ( !startEdge )
+ continue;
+
+ branch.push_back( startEdge );
+
+ nodeBranch.push_back( theStartNode );
+ nodeBranch.push_back( branch.back()->GetNode(0) );
+ if ( nodeBranch.back() == theStartNode )
+ nodeBranch.back() = branch.back()->GetNode(1);
+ }
+
+ // fill the branch
+
+ bool isBranchEnd = false;
+ TEdgeVec* edgesPtr;
+
+ while (( !isBranchEnd ) && ( edgesPtr = edgesByNode.ChangeSeek( nodeBranch.back() )))
+ {
+ TEdgeVec& edges = *edgesPtr;
+
+ isBranchEnd = ( edges.size() != 2 );
+
+ const SMDS_MeshNode* lastNode = nodeBranch.back();
+
+ switch ( edges.size() )
+ {
+ case 1:
+ edgesByNode.UnBind( lastNode );
+ break;
+
+ case 2:
+ {
+ if ( const SMDS_MeshElement* nextEdge = edges[ edges[0] == branch.back() ])
+ {
+ branch.push_back( nextEdge );
+
+ const SMDS_MeshNode* nextNode = nextEdge->GetNode(0);
+ if ( nodeBranch.back() == nextNode )
+ nextNode = nextEdge->GetNode(1);
+ nodeBranch.push_back( nextNode );
+ }
+ edgesByNode.UnBind( lastNode );
+ break;
+ }
+
+ default:
+ int nbEdges = 0;
+ for ( size_t i = 0; i < edges.size(); ++i )
+ {
+ if ( edges[i] == branch.back() )
+ edges[i] = 0;
+ nbEdges += bool( edges[i] );
+ }
+ if ( nbEdges == 0 )
+ edgesByNode.UnBind( lastNode );
+ }
+ }
+ } // while ( edgesByNode.IsBound( theStartNode ))
+
+
+ // put the found branches to the result
+
+ if ( nbBranches == 2 && !startIsBranchEnd ) // join two branches starting at the same node
+ {
+ std::reverse( nodeBranches[0].begin(), nodeBranches[0].end() );
+ nodeBranches[0].pop_back();
+ nodeBranches[0].reserve( nodeBranches[0].size() + nodeBranches[1].size() );
+ nodeBranches[0].insert( nodeBranches[0].end(),
+ nodeBranches[1].begin(), nodeBranches[1].end() );
+
+ std::reverse( branches[0].begin(), branches[0].end() );
+ branches[0].reserve( branches[0].size() + branches[1].size() );
+ branches[0].insert( branches[0].end(), branches[1].begin(), branches[1].end() );
+
+ nodeBranches[1].clear();
+ branches[1].clear();
+ }
+
+ for ( size_t i = 0; i < nbBranches; ++i )
+ {
+ if ( branches[i].empty() )
+ continue;
+
+ theEdgeGroups.push_back( TEdgeVec() );
+ theEdgeGroups.back().swap( branches[i] );
+
+ theNodeGroups.push_back( TNodeVec() );
+ theNodeGroups.back().swap( nodeBranches[i] );
+ }
+
+ } // while ( !edgesByNode.IsEmpty() )
+
+ return;
+}
+