private void CalculateVisibleTangentsFromPolygon(Polygon polygon) {
     this.currentPolygon = polygon;
     AllocateDataStructures();
     OrganizeTangents();
     InitActiveDiagonals();
     Sweep();
 }
 static Polygon PolygonFromBox(Rectangle sb)
 {
     var spolygon =
         new Polygon(new Polyline(sb.LeftBottom, sb.LeftTop, sb.RightTop, sb.RightBottom) { Closed = true });
     return spolygon;
 }
 void SwapPq() {
     Polygon t = P;
     P = Q;
     Q = t;
 }
 internal TangentPair(Polygon polygonP, Polygon polygonQ) {
     PPolygon = polygonP;
     QPolygon = polygonQ;
 }
        private void ProcessPolygonQ(Polygon q) {
            TangentPair tangentPair = new TangentPair(currentPolygon, q);
            if (this.useLeftPTangents)
                tangentPair.CalculateLeftTangents();
            else
                tangentPair.CalculateRightTangents();
            Tuple<int, int> couple = useLeftPTangents ? tangentPair.leftPLeftQ : tangentPair.rightPLeftQ;

            Tangent t0 = new Tangent(currentPolygon[couple.Item1], q[couple.Item2]);
            t0.IsLow = true;
            t0.SeparatingPolygons = !this.useLeftPTangents;
            couple = useLeftPTangents ? tangentPair.leftPRightQ : tangentPair.rightPRightQ;
            Tangent t1 = new Tangent(currentPolygon[couple.Item1], q[couple.Item2]);
            t1.IsLow = false;
            t1.SeparatingPolygons = this.useLeftPTangents;
            t0.Comp = t1;
            t1.Comp = t0;

            this.tangents.Add(t0);
            this.tangents.Add(t1);
            this.diagonals.Add(new Diagonal(t0, t1));
        }
        double GetIdealDistanceBetweenClusterPortAndPort(ClusterBoundaryPort clsp, Port port) {
            var poly = clsp.LoosePolyline;
            var otherClsp = port as ClusterBoundaryPort;
            if (otherClsp != null) {
                var polygon0 = new Polygon(poly);
                var polygon1 = new Polygon(otherClsp.LoosePolyline);
                return Polygon.Distance(polygon0, polygon1);
            }

            var point = port.Location;
            var closestPar = poly.ClosestParameter(point);
            return (point - poly[closestPar]).Length;
        }
 private void AllocateDataStructures(int i) {
     tangents = new List<Tangent>();
     diagonals = new List<Diagonal>();
     activeDiagonalTree = new RbTree<Diagonal>(this.activeDiagonalComparer);
     this.currentPolygon = polygons[i];
 }
Ejemplo n.º 8
0
        static double TestPolygonDistLocal(Polygon a,Polygon b) {
            double ret = double.PositiveInfinity,u,v;
            for(int i = 0;i < a.Count;i++)
                for(int j = 0;j < b.Count;j++)
                    ret = Math.Min(ret,LineSegment.MinDistBetweenLineSegments(a.Pnt(i),a.Pnt(i + 1),b.Pnt(j),
                                                                                b.Pnt(j + 1),out u,out v));

            return ret;
        }
Ejemplo n.º 9
0
 static void TestDist(Polygon a, Polygon b, double dist) {
     double u, v;
     for (int i = 0; i < a.Count; i++)
         for (int j = 0; j < b.Count; j++) {
             var d = LineSegment.MinDistBetweenLineSegments(a.Pnt(i), a.Pnt(i + 1), b.Pnt(j),
                                                                         b.Pnt(j + 1), out u, out v);
             Assert.IsTrue(d >= dist - 0.0000001);
         }
 }
Ejemplo n.º 10
0
        public void PolygonPolygonDistanceTest() {
#if GDI_DEBUG_VIEWER
            GraphViewerGdi.DisplayGeometryGraph.SetShowFunctions();
#endif
            var a = new Polygon(new Polyline(new[] { new Point(0, 0), new Point(0, 100), new Point(42, 109), new Point(100, 100), new Point(100, 0) }));
            var b = new Polygon(new Polyline(new[] { new Point(-2, 105), new Point(50, 130) }));
            Point p0, p1;
            var dist = Polygon.Distance(a, b, out p0, out p1);
            TestDist(a, b, dist);

            // LayoutAlgorithmSettings.ShowDebugCurves(new DebugCurve(new LineSegment(p0,p1)), new DebugCurve("blue", poly0.Polyline), new DebugCurve("red",poly1.Polyline));
            b = new Polygon(new Polyline(new[] { new Point(159, 60), new Point(91, 118) }));
            dist = Polygon.Distance(b, a, out p0, out p1);
            TestDist(a, b, dist);

            b = new Polygon(new Polyline(new[] { new Point(159, 60), new Point(140, 50), new Point(91, 118) }));
            dist = Polygon.Distance(b, a, out p0, out p1);
            //  LayoutAlgorithmSettings.ShowDebugCurves(new DebugCurve(new LineSegment(p0, p1)),
            //    new DebugCurve("blue", a.Polyline), new DebugCurve("red", b.Polyline));

            TestDist(a, b, dist);

        }
Ejemplo n.º 11
0
        public void PolygonPolygonDistanceTest2() {
#if GDI_DEBUG_VIEWER
            GraphViewerGdi.DisplayGeometryGraph.SetShowFunctions();
#endif
            var a = new Polyline(new[] {   new Point(-3397.10020369428, 993.94470736826),
                                            new Point(-3426.74057842555, 1014.3329144183),
                                            new Point(-3426.74057842555, 1045.96907990543),
                                            new Point(-3397.10020369428, 1066.35728695547),
                                            new Point(-3357.98527032, 1066.35728695547),
                                            new Point(-3328.34489558873, 1045.96907990543),
                                            new Point(-3328.34489558873, 1014.3329144183),
                                            new Point(-3357.98527032, 993.94470736826)}) { Closed = true };

            var b = new Polyline(new[] {   new Point(-2588.08967113495, 1130.55203056335),
                                            new Point(-3327.46492624868, 1013.85788393446)}) { Closed = true };

            //DisplayGeometryGraph.ShowDebugCurves(new DebugCurve(100,1,"red",a),new DebugCurve(100,1,"blue",b));
            var pa = new Polygon(a);
            var pb = new Polygon(b);
            Point p0, p1;
            var dist0 = Polygon.Distance(pb, pa, out p1, out p0);
            TestDist(pb, pa, dist0);
            var dist = Polygon.Distance(pa, pb, out p0, out p1);
            TestDist(pa, pb, dist);
        }
 /// <summary>
 /// Distance between polygon and point
 /// </summary>
 static public double Distance(Polygon poly, Point b)
 {
     double res = double.PositiveInfinity;
     for (int i = 0; i < poly.Count; i++) {
         double par;
         double dist = Point.DistToLineSegment(b, poly.points[i].Point, poly.points[(i + 1) % poly.Count].Point, out par);
         res = Math.Min(res, dist);
     }
     return res;
 }
 private static bool PolygonIsLegalDebug(Polygon a)
 {
     var poly = a.Polyline;
     for (var p = poly.StartPoint; p.Next != null && p.Next.Next != null; p = p.Next)
         if (Point.GetTriangleOrientation(p.Point, p.Next.Point, p.Next.Next.Point) ==
             TriangleOrientation.Collinear)
             return false;
     return true;
 }
        /// <summary>
        /// Distance between two polygons
        /// </summary>
        static public double Distance(Polygon a, Polygon b) {
            System.Diagnostics.Debug.Assert(PolygonIsLegalDebug(a));
            System.Diagnostics.Debug.Assert(PolygonIsLegalDebug(b));

            Point p, q;
            return Distance(a, b, out p, out q);
        }
        /// <summary>
        /// Distance between two polygons
        /// p and q are the closest points
        /// The function doesn't work if the polygons intersect each other
        /// </summary>
        static public double Distance(Polygon a, Polygon b, out Point p, out Point q) {
            var tp = new TangentPair(a, b);
            tp.FindClosestPoints(out p, out q);
#if TEST_MSAGL
            if(!ApproximateComparer.Close((p - q).Length,TestPolygonDist(a,b))) {
                using(var stream = File.Open(@"c:\tmp\polygonBug",FileMode.Create)) {
                    var bf = new BinaryFormatter();
                    bf.Serialize(stream, a);
                    bf.Serialize(stream, b);                    
                }

                LayoutAlgorithmSettings.ShowDebugCurves(
                    new DebugCurve(100, 0.1, "red", a.Polyline),
                    new DebugCurve(100, 0.1, "blue", b.Polyline),
                    new DebugCurve(100, 0.1, "black", new LineSegment(p, q)));
                System.Diagnostics.Debug.Fail("wrong distance between two polygons");
            }
#endif
            return (p - q).Length;
        }
        double FindMaxPaddingForTightPolyline(Polyline polyline) {
            var dist = double.MaxValue;
            var polygon = new Polygon(polyline);

            foreach (var poly in RootOfLooseHierarchy.GetAllLeaves().Where(p => p != polyline))
                dist = Math.Min(dist, Polygon.Distance(polygon, new Polygon(poly)));

            //            TraverseHierarchy(RootOfLooseHierarchy, delegate(RectangleNode<Polyline> node) {
            //                                                        if (node.UserData != null)
            //                                                            if (node.UserData != polyline)
            //                                                                dist = Math.Min(dist,
            //                                                                                Polygon.Distance(polygon,
            //                                                                                                 new Polygon(
            //                                                                                                     node.UserData)));
            //                                                    });
            dist = Math.Min(dist, RouterBetweenTwoNodes.DistanceFromPointToPolyline(router.SourcePoint, polyline));
            dist = Math.Min(dist, RouterBetweenTwoNodes.DistanceFromPointToPolyline(router.TargetPoint, polyline));
            return dist;
        }