+ const Standard_Real aParam = aPIt.Value();
+ if (Abs(theParameter - aParam) < Precision::PConfusion() ||
+ aPoint.SquareDistance(theCurve.Value(aParam)) <=
+ Precision::SquareConfusion())
+ {
+ Standard_Integer aIntCount = 0;
+ if (aLI != 0)
+ {
+ if (aPI == 0)
+ {
+ theParameters.Prepend(aEndParams.First());
+ }
+ else
+ {
+ theParameters.Append(aEndParams.Last());
+ }
+ ++aIntCount;
+ }
+ return aIntCount;
+ }
+ }
+ }
+
+ // Calculate the position to insert.
+ NCollection_Sequence<Standard_Real>::Iterator aPIt(theParameters);
+ if (aPIt.More() && aPIt.Value() < theParameter)
+ {
+ NCollection_Sequence<Standard_Real>::Iterator aPIt2 = aPIt;
+ aPIt2.Next();
+ for (; aPIt2.More() && aPIt2.Value() < theParameter;
+ aPIt.Next(), aPIt2.Next());
+ theParameters.InsertAfter(aPIt, theParameter);
+ }
+ else
+ {
+ theParameters.Prepend(theParameter);
+ }
+ return 1;
+}
+
+static Standard_Integer IntersectEdge(
+ const TopoDS_Edge& theEdge1,
+ const TopoDS_Edge& theEdge2,
+ NCollection_Sequence<Standard_Real>& theParameters)
+{
+ // Process the ends.
+ Standard_Integer aIntCount = 0;
+ BRepAdaptor_Curve aCurve1 = BRepAdaptor_Curve(theEdge1);
+ BRepAdaptor_Curve aCurve2 = BRepAdaptor_Curve(theEdge2);
+ const gp_Pnt aEndPs[] = {aCurve2.Value(aCurve2.FirstParameter()),
+ aCurve2.Value(aCurve2.LastParameter())};
+ for (Standard_Integer aPI = 0; aPI < 2; ++aPI)
+ {
+ Standard_Real aParameter;
+ if (ProjectPointToCurve(aEndPs[aPI], aCurve1, aParameter) <=
+ Precision::SquareConfusion())
+ {
+ AppendIntersectionPoint(aCurve1, aParameter, theParameters);
+ }
+ }
+
+ // Process the internal extrema.
+ Extrema_ExtCC aAlgo(aCurve1, aCurve2);
+ if (aAlgo.IsDone())
+ {
+ const Standard_Integer aECount = aAlgo.NbExt();
+ for (Standard_Integer aEN = 1; aEN <= aECount; ++aEN)
+ {
+ Extrema_POnCurv aP1, aP2;
+ aAlgo.Points(aEN, aP1, aP2);
+ if (aP1.Value().SquareDistance(aP2.Value()) <=
+ Precision::SquareConfusion())
+ {
+ AppendIntersectionPoint(aCurve1, aP1.Parameter(), theParameters);
+ }
+ }
+ }
+ return aIntCount;
+}
+
+static Standard_Integer IntersectCurve(
+ const NCollection_Vector<TopoDS_Edge>& theEdges,
+ const TopoDS_Wire& theWire,
+ NCollection_Vector<NCollection_Sequence<Standard_Real> >& theParameters)
+{
+ Standard_Integer aIntCount = 0;
+ const Standard_Integer aECount1 = theEdges.Size();
+ for (Standard_Integer aEI1 = 0; aEI1 < aECount1; ++aEI1)
+ {
+ const TopoDS_Edge& aEdge1 = theEdges(aEI1);
+ TopExp_Explorer aEIt2(theWire, TopAbs_EDGE);
+ for (; aEIt2.More(); aEIt2.Next())
+ {
+ aIntCount += IntersectEdge(aEdge1,
+ TopoDS::Edge(aEIt2.Current()), theParameters(aEI1));
+ }
+ }
+ return aIntCount;
+}
+
+bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
+ const Handle( HYDROData_PolylineXY )& thePolyline,
+ const gp_Pnt2d& thePoint,
+ double theTolerance ) const
+{
+ std::vector<gp_Pnt2d> aPointsList( 1 );
+ aPointsList[0] = thePoint;
+ std::vector<TopoDS_Wire> aCurves = GetWires( thePolyline );
+ bool isOK = true;
+ for( int i=0, n=aCurves.size(); i<n; i++ )
+ {
+ std::vector<TopoDS_Shape> aCurvesList = Split( aCurves[i], thePoint, theTolerance );
+ bool isLocalOK = CreatePolylines( theDoc, thePolyline->GetName(), aCurvesList, true );
+ isOK = isOK && isLocalOK;
+ }
+ return isOK;
+}
+
+bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
+ const Handle( HYDROData_PolylineXY )& thePolyline,
+ const Handle( HYDROData_PolylineXY )& theTool,
+ double theTolerance ) const
+{
+ HYDROData_SequenceOfObjects aSeq;
+ aSeq.Append( theTool );
+ return split( theDoc, thePolyline, aSeq, theTolerance, -1 );
+}
+
+bool HYDROData_PolylineOperator::Split( const Handle( HYDROData_Document )& theDoc,
+ const HYDROData_SequenceOfObjects& thePolylines,
+ double theTolerance )
+{
+ int f = thePolylines.Lower(), l = thePolylines.Upper();
+ for( int i=f; i<=l; i++ )
+ {
+ Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
+ if( !split( theDoc, aPolyline, thePolylines, theTolerance, i ) )
+ return false;
+ }
+ return true;
+}
+
+bool HYDROData_PolylineOperator::Merge( const Handle( HYDROData_Document )& theDoc,
+ const QString& theName,
+ const HYDROData_SequenceOfObjects& thePolylines,
+ bool isConnectByNewSegment,
+ double theTolerance )
+{
+ TopoDS_Shape aMergedPolyline;
+
+ int f = thePolylines.Lower(), l = thePolylines.Upper();
+ for( int i=f; i<=l; i++ )
+ {
+ Handle( HYDROData_PolylineXY ) aPolyline = Handle( HYDROData_PolylineXY )::DownCast( thePolylines.Value( i ) );
+ std::vector<TopoDS_Wire> aCurves = GetWires( aPolyline );
+ for( int j=0, m=aCurves.size(); j<m; j++ )
+ if( !Merge( aMergedPolyline, aCurves[j], isConnectByNewSegment, theTolerance ) )
+ return false;
+ }
+
+ std::vector<TopoDS_Shape> aShapes( 1 );
+ aShapes[0] = aMergedPolyline;
+ CreatePolylines( theDoc, theName, aShapes, false );
+
+ return true;
+}
+
+bool HYDROData_PolylineOperator::Merge( TopoDS_Shape& theShape, const TopoDS_Wire& theWire,
+ bool isConnectByNewSegment, double theTolerance )
+{
+ //TODO
+ return true;
+}
+
+bool HYDROData_PolylineOperator::split( const Handle( HYDROData_Document )& theDoc,
+ const Handle( HYDROData_PolylineXY )& thePolyline,
+ const HYDROData_SequenceOfObjects& theTools,
+ double theTolerance,
+ int theIgnoreIndex ) const
+{
+ std::vector<TopoDS_Wire> aCurves = GetWires( thePolyline );
+ std::vector<TopoDS_Wire> aToolCurves;
+ for( int i=theTools.Lower(), n=theTools.Upper(); i<=n; i++ )
+ if( i!=theIgnoreIndex )
+ {
+ Handle( HYDROData_PolylineXY ) aToolPolyline =
+ Handle( HYDROData_PolylineXY )::DownCast( theTools.Value( i ) );
+ append( aToolCurves, GetWires( aToolPolyline ) );
+ }
+
+ const int aPSCount = aCurves.size();
+ const int aTSCount = aToolCurves.size();
+ std::vector<TopoDS_Shape> aResult;
+ for (int aPSI = 0; aPSI < aPSCount; ++aPSI)
+ {
+ NCollection_Vector<TopoDS_Edge> aCurve;
+ Standard_Boolean aIsClosed;
+ if (!WireToCurve(aCurves[aPSI], aCurve, aIsClosed))
+ {
+ continue;
+ }
+
+ NCollection_Vector<NCollection_Sequence<Standard_Real> > aParams;
+ aParams.SetValue(aPSCount - 1, NCollection_Sequence<Standard_Real>());
+ for (int aTSI = 0; aTSI < aTSCount; ++aTSI)
+ {
+ IntersectCurve(aCurve, aToolCurves[aTSI], aParams);
+ }
+
+ NCollection_Vector<NCollection_Vector<TopoDS_Edge> > aSplittedCurves;
+ SplitCurveByPoints(aCurve, aParams, aSplittedCurves);
+
+ Standard_Boolean aIsClosed2 = aIsClosed;
+ if (aIsClosed2)
+ {
+ const NCollection_Sequence<Standard_Real>& aPs = aParams.First();
+ if (!aPs.IsEmpty())
+ {
+ const TopoDS_Edge& aEdge = aCurve.First();
+ const Standard_Boolean isForward =
+ (aEdge.Orientation() != TopAbs_REVERSED);
+ const Standard_Real aParam = isForward ? aPs.First() : aPs.Last();
+ BRepAdaptor_Curve aCurve(aEdge);
+ const Standard_Real aEndParam = isForward ?
+ aCurve.FirstParameter() : aCurve.LastParameter();
+ aIsClosed2 = (Abs(aParam - aEndParam) > Precision::PConfusion());
+ }
+
+ if (aIsClosed2)
+ {
+ const NCollection_Sequence<Standard_Real>& aPs = aParams.Last();
+ if (!aPs.IsEmpty())
+ {
+ const TopoDS_Edge& aEdge = aCurve.Last();
+ const Standard_Boolean isForward =
+ (aEdge.Orientation() != TopAbs_REVERSED);
+ const Standard_Real aParam = isForward ? aPs.Last() : aPs.First();
+ BRepAdaptor_Curve aCurve(aEdge);
+ const Standard_Real aEndParam = isForward ?
+ aCurve.LastParameter() : aCurve.FirstParameter();
+ aIsClosed2 = (Abs(aParam - aEndParam) >= Precision::PConfusion());
+ }
+ }
+ }
+
+ Standard_Integer aFSCI = 0, aLSCI = aSplittedCurves.Size() - 1;
+ if (aIsClosed2 && aFSCI < aLSCI)
+ {
+ TopoDS_Wire aWire;
+ CurveToWire(aSplittedCurves(aLSCI), aSplittedCurves(aFSCI), aWire);
+ aResult.push_back(aWire);
+ ++aFSCI;
+ --aLSCI;
+ }
+
+ for (Standard_Integer aSCI = aFSCI; aSCI <= aLSCI; ++aSCI)
+ {
+ TopoDS_Wire aWire;
+ CurveToWire(aSplittedCurves(aSCI), aWire);
+ aResult.push_back(aWire);
+ }
+ }
+
+ CreatePolylines(theDoc, thePolyline->GetName(), aResult, true);
+ return true;
+}
+
+std::vector<TopoDS_Wire> HYDROData_PolylineOperator::GetWires( const Handle( HYDROData_PolylineXY )& thePolyline )
+{
+ std::vector<TopoDS_Wire> aResult;
+
+ TopoDS_Shape aShape = thePolyline->GetShape();
+
+ if( aShape.ShapeType()==TopAbs_WIRE )
+ {
+ aResult.push_back( TopoDS::Wire( aShape ) );
+ }
+ else
+ {
+ TopExp_Explorer anExp( aShape, TopAbs_WIRE );
+ for( ; anExp.More(); anExp.Next() )
+ {
+ aResult.push_back( TopoDS::Wire( anExp.Current() ) );