+//function : getOtherShape
+//purpose :
+//=======================================================================
+
+static TopoDS_Shape getOtherShape(const TopoDS_Shape& theS,
+ const TopTools_ListOfShape& theSList)
+{
+ TopTools_ListIteratorOfListOfShape anIt( theSList );
+ for ( ; anIt.More(); anIt.Next() )
+ if (!theS.IsSame( anIt.Value() ))
+ return anIt.Value();
+
+ return TopoDS_Shape();
+}
+
+//=======================================================================
+//function : findVOnE
+//purpose : on theE, find a vertex close to theV, such that an edge
+// passing through it is an itersection of theF1 and theF2.
+// theE intersects theE2 at theV
+//=======================================================================
+
+static Standard_Boolean findVOnE(const TopoDS_Vertex & theV,
+ const TopoDS_Edge& theE,
+ const TopoDS_Edge& theE2,
+ const TopoDS_Shape& theF1,
+ const TopoDS_Shape& theF2,
+ const Handle(BRepAlgo_AsDes)& theAsDes,
+ TopoDS_Vertex & theFoundV)
+{
+ Standard_Real MinDist2 = ::RealLast();
+ gp_Pnt P;
+
+ // check all vertices on theE
+ const TopTools_ListOfShape& aVList = theAsDes->Descendant( theE );
+ TopTools_ListIteratorOfListOfShape anIt( aVList );
+ if (anIt.More())
+ P = BRep_Tool::Pnt( theV );
+ for ( ; anIt.More(); anIt.Next() )
+ {
+ // check by distance
+ TopoDS_Vertex & V = TopoDS::Vertex( anIt.Value() );
+ Standard_Real dist2 = P.SquareDistance( BRep_Tool::Pnt( V ));
+ if (dist2 < MinDist2)
+ MinDist2 = dist2;
+ else
+ continue;
+
+ // V is a candidate if among edges passing through V there is one
+ // which is an intersection of theF1 and theF2
+ TopTools_ListIteratorOfListOfShape anEIt( theAsDes->Ascendant( V ));
+ Standard_Boolean isOk = Standard_False;
+ for ( ; !isOk && anEIt.More(); anEIt.Next() )
+ {
+ const TopoDS_Shape & E2 = anEIt.Value();
+ if ( theE2.IsSame( E2 ))
+ continue;
+ const TopTools_ListOfShape & aFList = theAsDes->Ascendant( E2 );
+ if (aFList.IsEmpty())
+ continue;
+ if ( theF1.IsSame( aFList.First() ))
+ isOk = theF2.IsSame( aFList.Last() );
+ else
+ isOk = theF2.IsSame( aFList.First() ) && theF1.IsSame( aFList.Last() );
+ }
+ if (isOk)
+ theFoundV = V;
+ }
+
+ if (theFoundV.IsNull())
+ return Standard_False;
+
+ // check that MinDist2 is not too large
+ Standard_Real f, l;
+ TopLoc_Location L;
+ Handle(Geom_Curve) aCurve = BRep_Tool::Curve( theE, L, f, l );
+ gp_Pnt P1 = aCurve->Value( f );
+ gp_Pnt P2 = aCurve->Value( 0.3 * f + 0.7 * l );
+ //gp_Pnt P2 = aCurve->Value( 0.5 * ( f + l ));
+ if (MinDist2 > P1.SquareDistance( P2 ))
+ return Standard_False;
+
+#ifdef DEB
+ MESSAGE("findVOnE: found MinDist = " << sqrt (MinDist2));
+#endif
+
+ return Standard_True;
+}
+
+//=======================================================================
+//function : AddVonE
+//purpose : Put V in AsDes as intersection of E1 and E2.
+// Check that vertex equal to V already exists on one
+// of edges, in such a case, V is not added but
+// existing vertex is updated to be on E1 and E2 and
+// is returned insead of V.