Exemple #1
0
        private void AddStackLinesToLayer(AlgorithmStatusLayer layer, Stack <Vector> stack)
        {
            var a = stack.ToArray <Vector>();

            for (int i = 0; i < a.Length - 1; i++)
            {
                var ap  = a[i].Alternates;
                var ap2 = a[i + 1].Alternates;

                if (ap == null || ap2 == null)
                {
                    return;
                }

                layer.AddCommand(new AddLineCommand
                {
                    AssociatedAlgorithm = this,
                    StartXIndex         = ap.DotIndexLeft,
                    StartYIndex         = ap.DotIndexTop,
                    EndXIndex           = ap2.DotIndexLeft,
                    EndYIndex           = ap2.DotIndexTop,
                    Comments            = ""
                });
            }
        }
Exemple #2
0
 protected void AddNonIndexedPolygonCommand(AlgorithmStatusLayer layer, PolygonModel poly)
 {
     layer.AddCommand(new AddNonIndexedPolygonCommand
     {
         AssociatedAlgorithm = this,
         Polygon             = poly
     });
 }
Exemple #3
0
 protected void AddNonIndexedLineCommand(AlgorithmStatusLayer layer, Vector v1, Vector v2)
 {
     layer.AddCommand(new AddNonIndexedLineCommand
     {
         AssociatedAlgorithm = this,
         StartX   = v1.X,
         StartY   = v1.Y,
         EndX     = v2.X,
         EndY     = v2.Y,
         Comments = ""
     });
 }
        public override void Run()
        {
            //public List<Vector> UnsortedPoly1 { get; set; }
            //public List<Vector> UnsortedPoly2 { get; set; }

            UnsortedPoly1 = new List <Vector>();

            foreach (var v in InputPolygons[0].Lines)
            {
                UnsortedPoly1.Add(new Vector {
                    X = v.StartPoint.X, Y = v.StartPoint.Y, Alternates = v.StartPoint.Alternates
                });
            }

            UnsortedPoly2 = new List <Vector>();

            foreach (var v in InputPolygons[1].Lines)
            {
                UnsortedPoly2.Add(new Vector {
                    X = v.StartPoint.X, Y = v.StartPoint.Y, Alternates = v.StartPoint.Alternates
                });
            }

            Compute();

            var layer = History.CreateAndAddNewLayer("Final Result");

            if (IntersectingPolygon.Lines == null || IntersectingPolygon.Lines.Count == 0)
            {
                activeStatusLayer.AddCommand(new AddTextStatusCommand
                {
                    AssociatedAlgorithm = this,
                    Comments            = "No intersection of polygons"
                });
            }
            else
            {
                foreach (var line in IntersectingPolygon.Lines)
                {
                    layer.AddCommand(new HighlightNonIndexedLineCommand
                    {
                        StartX = line.StartPoint.X,
                        StartY = line.StartPoint.Y,
                        EndX   = line.EndPoint.X,
                        EndY   = line.EndPoint.Y
                    });
                }
            }
        }
Exemple #5
0
        protected void AddLineCommand(AlgorithmStatusLayer layer, Vector v1, Vector v2)
        {
            if (v1.Alternates == null || v2.Alternates == null)
            {
                //Debugger.Break();
                return;
            }

            layer.AddCommand(new AddLineCommand
            {
                AssociatedAlgorithm = this,
                StartXIndex         = v1.Alternates.DotIndexLeft,
                StartYIndex         = v1.Alternates.DotIndexTop,
                EndXIndex           = v2.Alternates.DotIndexLeft,
                EndYIndex           = v2.Alternates.DotIndexTop,
                Comments            = ""
            });
        }
        public void Compute()
        {
            SortPolygons();

            List <Vector> P = SortedPoly1;
            List <Vector> Q = SortedPoly2;

            int    n = SortedPoly1.Count;
            int    m = SortedPoly2.Count;
            int    a, b;
            int    a1, b1;
            Vector A = new Vector(), B = new Vector();
            int    cross;
            int    bHA, aHB;
            Vector origin = new Vector {
                X = 0, Y = 0
            };
            Vector p = new Vector(), q = new Vector();
            int    inFlag;
            int    aa, ba;
            bool   firstPoint;
            Vector p0 = new Vector();
            int    code;

            a          = 0; b = 0; aa = 0; ba = 0;
            inFlag     = INFLAG_UNKNOWN;
            firstPoint = true;

            activeStatusLayer = History.CreateAndAddNewLayer("Setup");
            activeStatusLayer.AddCommand(new AddTextStatusCommand {
                AssociatedAlgorithm = this,
                Comments            = "The 'in' flag starts as unknown, initial vectors start at index 0"
            });

            do
            {
                a1 = (a + n - 1) % n;
                b1 = (b + m - 1) % m;

                activeStatusLayer = History.CreateAndAddNewLayer("Main loop, a=" + a + ", b=" + b + ", a1=" + a1 + ", b1=" + b1);

                // SubVec(P[a], P[a1], A);
                A.X = P[a].X - P[a1].X;
                A.Y = P[a].Y - P[a1].Y;
                // SubVec(Q[b], Q[b1], B);
                B.X = Q[b].X - Q[b1].X;
                B.Y = Q[b].Y - Q[b1].Y;

                activeStatusLayer.AddCommand(new ClearTextStatusCommand {
                    AssociatedAlgorithm = this
                });
                AddNonIndexedLineCommand(activeStatusLayer, new Vector {
                    X = P[a1].X, Y = P[a1].Y
                }, new Vector {
                    X = P[a].X, Y = P[a].Y
                });
                AddNonIndexedLineCommand(activeStatusLayer, new Vector {
                    X = Q[b1].X, Y = Q[b1].Y
                }, new Vector {
                    X = Q[b].X, Y = Q[b].Y
                });

                cross = AreaSign(origin, A, B);
                aHB   = AreaSign(Q[b1], Q[b], P[a]);
                bHA   = AreaSign(P[a1], P[a], Q[b]);

                activeStatusLayer.AddCommand(new AddTextStatusCommand
                {
                    AssociatedAlgorithm = this,
                    Comments            = "cross=" + cross
                });
                activeStatusLayer.AddCommand(new AddTextStatusCommand
                {
                    AssociatedAlgorithm = this,
                    Comments            = "aHB=" + aHB
                });
                activeStatusLayer.AddCommand(new AddTextStatusCommand
                {
                    AssociatedAlgorithm = this,
                    Comments            = "bHA=" + bHA
                });

                code = SegSegInt(P[a1], P[a], Q[b1], Q[b], p, q);

                activeStatusLayer.AddCommand(new AddTextStatusCommand
                {
                    AssociatedAlgorithm = this,
                    Comments            = "Segment intersection status: " + TranslateCode(code)
                });

                if (code == STATUS_1 || code == STATUS_V)
                {
                    if (inFlag == STATUS_UNKNOWN && firstPoint)
                    {
                        aa         = 0;
                        ba         = 0;
                        firstPoint = false;
                        p0.X       = p.X;
                        p0.Y       = p.Y;
                        // moveto p0
                        MoveTo(p0);
                    }

                    LineTo(p);
                    inFlag = InOut(p, inFlag, aHB, bHA);

                    activeStatusLayer.AddCommand(new AddTextStatusCommand
                    {
                        AssociatedAlgorithm = this,
                        Comments            = "Newly computed 'in' flag: " + TranslateInOut(inFlag)
                    });
                }

                if (code == STATUS_E && GeomMath.DotProduct(A, B) < 0)
                {
                    activeStatusLayer.AddCommand(new AddTextStatusCommand
                    {
                        AssociatedAlgorithm = this,
                        Comments            = "Shared segement and ..."
                    });
                    // print shared segment
                    // return
                }

                if (cross == 0 && aHB < 0 && bHA < 0)
                {
                    // disjoint
                    // return
                    activeStatusLayer.AddCommand(new AddTextStatusCommand
                    {
                        AssociatedAlgorithm = this,
                        Comments            = "Polygons are disjoint"
                    });
                }
                else if (cross == 0 && aHB == 0 && bHA == 0)
                {
                    // advance but do not output point
                    // page 262
                    if (inFlag == INFLAG_PIN)
                    {
                        // b = Advance(b, &ba, m, inflag == Qin, Q[b]);
                        // if inFlag == Qin // always false here
                        //    lineto Q[b]
                        ba++;
                        b = (b + 1) % m;
                        activeStatusLayer.AddCommand(new AddTextStatusCommand
                        {
                            AssociatedAlgorithm = this,
                            Comments            = "Advancing b vector but not including in output as not part of interection"
                        });
                    }
                    else
                    {
                        // a = Advance(a, &aa, n, inflag == Pin, P[a]);
                        // if inFlag == Pin // always false here
                        //    lineto P[a]
                        aa++;
                        a = (a + 1) % n;
                        activeStatusLayer.AddCommand(new AddTextStatusCommand
                        {
                            AssociatedAlgorithm = this,
                            Comments            = "Advancing a vector but not including in output as not part of interection"
                        });
                    }
                }
                else if (cross >= 0)
                {
                    if (bHA > 0)
                    {
                        // if inflag == PIN
                        //    lineto P[a]
                        if (inFlag == INFLAG_PIN)
                        {
                            LineTo(P[a]);
                        }
                        aa++;
                        a = (a + 1) % n;
                        activeStatusLayer.AddCommand(new AddTextStatusCommand
                        {
                            AssociatedAlgorithm = this,
                            Comments            = "Advancing a vector and including in output as part of interection"
                        });
                    }
                    else
                    {
                        // if inflag == QIN
                        //    lineto Q[b]
                        if (inFlag == INFLAG_QIN)
                        {
                            LineTo(Q[b]);
                        }
                        ba++;
                        b = (b + 1) % m;
                        activeStatusLayer.AddCommand(new AddTextStatusCommand
                        {
                            AssociatedAlgorithm = this,
                            Comments            = "Advancing b vector and including in output as part of interection"
                        });
                    }
                }
                else
                {
                    // cross < 0
                    if (aHB > 0)
                    {
                        // if inflag == QIN
                        //    lineto Q[b]
                        if (inFlag == INFLAG_QIN)
                        {
                            LineTo(Q[b]);
                        }
                        ba++;
                        b = (b + 1) % m;
                        activeStatusLayer.AddCommand(new AddTextStatusCommand
                        {
                            AssociatedAlgorithm = this,
                            Comments            = "Advancing b vector and including in output as part of interection"
                        });
                    }
                    else
                    {
                        // if inflag == PIN
                        //     lineto P[a]
                        if (inFlag == INFLAG_PIN)
                        {
                            LineTo(P[a]);
                        }
                        aa++;
                        a = (a + 1) % n;
                        activeStatusLayer.AddCommand(new AddTextStatusCommand
                        {
                            AssociatedAlgorithm = this,
                            Comments            = "Advancing a vector and including in output as part of interection"
                        });
                    }
                }
            } while (((aa < n) || (ba < m)) && (aa < 2 * n) && (ba < 2 * m));

            if (aa >= 2 * n)
            {
                activeStatusLayer.AddCommand(new AddTextStatusCommand
                {
                    AssociatedAlgorithm = this,
                    Comments            = "Termination condition met: aa >= 2*n"
                });
            }
            else if (aa >= n)
            {
                activeStatusLayer.AddCommand(new AddTextStatusCommand
                {
                    AssociatedAlgorithm = this,
                    Comments            = "Termination condition met: aa >= n"
                });
            }

            if (ba >= 2 * m)
            {
                activeStatusLayer.AddCommand(new AddTextStatusCommand
                {
                    AssociatedAlgorithm = this,
                    Comments            = "Termination condition met: ba >= 2*m"
                });
            }
            else if (ba >= m)
            {
                activeStatusLayer.AddCommand(new AddTextStatusCommand
                {
                    AssociatedAlgorithm = this,
                    Comments            = "Termination condition met: ba >= m"
                });
            }

            if (!firstPoint)
            {
                // close intesection, line to p0
                LineTo(p0);
            }

            if (inFlag == INFLAG_UNKNOWN)
            {
                // boundaries do not cross
                activeStatusLayer.AddCommand(new AddTextStatusCommand
                {
                    AssociatedAlgorithm = this,
                    Comments            = "Boundaries of polygons do not cross, no intersection"
                });
            }
        }