Exemple #1
0
        Site RightRegion(HalfEdge hE, Site lowestSite)
        {
            Edge e = hE.e;

            if (e == null)
            {
                return(lowestSite);
            }
            return(e.Site(LR.Other(hE.lr)));
        }
Exemple #2
0
        private Site RightRegion(Halfedge he, Site bottomMostSite)
        {
            Edge edge = he.edge;

            if (edge == null)
            {
                return(bottomMostSite);
            }
            return(edge.Site(LR.Other(he.leftRight)));
        }
Exemple #3
0
    private List <Vector2f> ClipToBounds(Rectf bounds)
    {
        List <Vector2f> points = new List <Vector2f>();
        int             n      = this.edges.Count;
        int             i      = 0;
        Edge            edge;

        // Linear search for an initial visible edge.
        while (i < n && !this.edges[i].Visible)
        {
            i++;
        }

        // Reached end of edge list without encountering a visible edge.
        if (i == n)
        {
            // No edges visible
            return(new List <Vector2f>());
        }

        edge = this.edges[i];
        LR orientation = this.edgeOrientations[i];

        points.Add(edge.ClippedVertices[orientation]);
        points.Add(edge.ClippedVertices[LR.Other(orientation)]);

        // Continue to process any additional visible edges.
        for (int j = i + 1; j < n; j++)
        {
            edge = this.edges[j];
            if (!edge.Visible)
            {
                continue;
            }
            this.Connect(ref points, j, bounds);
        }
        // Close up the polygon by adding another corner point of the bounds if needed:
        this.Connect(ref points, i, bounds, true);

        return(points);
    }
Exemple #4
0
        private List <Vector2f> ClipToBounds(Rectf bounds)
        {
            List <Vector2f> points = new List <Vector2f>();
            int             n      = edges.Count;
            int             i      = 0;
            Edge            edge;

            while (i < n && !edges[i].Visible())
            {
                i++;
            }

            if (i == n)
            {
                // No edges visible
                return(new List <Vector2f>());
            }
            edge = edges[i];
            LR orientation = edgeOrientations[i];

            points.Add(edge.ClippedEnds[orientation]);
            points.Add(edge.ClippedEnds[LR.Other(orientation)]);

            for (int j = i + 1; j < n; j++)
            {
                edge = edges[j];
                if (!edge.Visible())
                {
                    continue;
                }
                Connect(ref points, j, bounds);
            }
            // Close up the polygon by adding another corner point of the bounds if needed:
            Connect(ref points, i, bounds, true);

            return(points);
        }
Exemple #5
0
        private List <Point> ClipToBounds(Rectangle bounds)
        {
            List <Point> points = new List <Point>();
            int          n      = edges.Count;
            int          i      = 0;
            Edge         e;

            while (i < n && (edges[i].GetVisible() == false))
            {
                i++;
            }

            if (i == n)
            {
                return(new List <Point>());
            }

            e = edges[i];
            LR orient = edgeOrientations[i];

            points.Add(e.GetClippedEnds()[orient]);
            points.Add(e.GetClippedEnds()[LR.Other(orient)]);

            for (int j = i + 1; j < n; j++)
            {
                e = edges[j];
                if (e.GetVisible() == false)
                {
                    continue;
                }
                Connect(points, j, bounds, false);
            }
            Connect(points, i, bounds, true);

            return(points);
        }
Exemple #6
0
        private void FortunesAlgorithm()
        {
            Site     newSite, bottomSite, topSite, tempSite;
            Vertex   v, vertex;
            Vector2f newIntStar = Vector2f.zero;
            LR       leftRight;
            Halfedge lbnd, rbnd, llbnd, rrbnd, bisector;
            Edge     edge;

            Rectf dataBounds = sites.GetSitesBounds();

            int sqrtSitesNb            = (int)Math.Sqrt(sites.Count() + 4);
            HalfedgePriorityQueue heap = new HalfedgePriorityQueue(dataBounds.y, dataBounds.height, sqrtSitesNb);
            EdgeList        edgeList   = new EdgeList(dataBounds.x, dataBounds.width, sqrtSitesNb);
            List <Halfedge> halfEdges  = new List <Halfedge>();
            List <Vertex>   vertices   = new List <Vertex>();

            Site bottomMostSite = sites.Next();

            newSite = sites.Next();

            while (true)
            {
                if (!heap.Empty())
                {
                    newIntStar = heap.Min();
                }

                if (newSite != null &&
                    (heap.Empty() || CompareByYThenX(newSite, newIntStar) < 0))
                {
                    // New site is smallest
                    //Debug.Log("smallest: new site " + newSite);

                    // Step 8:
                    lbnd = edgeList.EdgeListLeftNeighbor(newSite.Coord); // The halfedge just to the left of newSite
                                                                         //UnityEngine.Debug.Log("lbnd: " + lbnd);
                    rbnd = lbnd.edgeListRightNeighbor;                   // The halfedge just to the right
                                                                         //UnityEngine.Debug.Log("rbnd: " + rbnd);
                    bottomSite = RightRegion(lbnd, bottomMostSite);      // This is the same as leftRegion(rbnd)
                                                                         // This Site determines the region containing the new site
                                                                         //UnityEngine.Debug.Log("new Site is in region of existing site: " + bottomSite);

                    // Step 9
                    edge = Edge.CreateBisectingEdge(bottomSite, newSite);
                    //UnityEngine.Debug.Log("new edge: " + edge);
                    edges.Add(edge);

                    bisector = Halfedge.Create(edge, LR.LEFT);
                    halfEdges.Add(bisector);
                    // Inserting two halfedges into edgelist constitutes Step 10:
                    // Insert bisector to the right of lbnd:
                    edgeList.Insert(lbnd, bisector);

                    // First half of Step 11:
                    if ((vertex = Vertex.Intersect(lbnd, bisector)) != null)
                    {
                        vertices.Add(vertex);
                        heap.Remove(lbnd);
                        lbnd.vertex = vertex;
                        lbnd.ystar  = vertex.y + newSite.Dist(vertex);
                        heap.Insert(lbnd);
                    }

                    lbnd     = bisector;
                    bisector = Halfedge.Create(edge, LR.RIGHT);
                    halfEdges.Add(bisector);
                    // Second halfedge for Step 10::
                    // Insert bisector to the right of lbnd:
                    edgeList.Insert(lbnd, bisector);

                    // Second half of Step 11:
                    if ((vertex = Vertex.Intersect(bisector, rbnd)) != null)
                    {
                        vertices.Add(vertex);
                        bisector.vertex = vertex;
                        bisector.ystar  = vertex.y + newSite.Dist(vertex);
                        heap.Insert(bisector);
                    }

                    newSite = sites.Next();
                }
                else if (!heap.Empty())
                {
                    // Intersection is smallest
                    lbnd       = heap.ExtractMin();
                    llbnd      = lbnd.edgeListLeftNeighbor;
                    rbnd       = lbnd.edgeListRightNeighbor;
                    rrbnd      = rbnd.edgeListRightNeighbor;
                    bottomSite = LeftRegion(lbnd, bottomMostSite);
                    topSite    = RightRegion(rbnd, bottomMostSite);
                    // These three sites define a Delaunay triangle
                    // (not actually using these for anything...)
                    // triangles.Add(new Triangle(bottomSite, topSite, RightRegion(lbnd, bottomMostSite)));

                    v = lbnd.vertex;
                    v.SetIndex();
                    lbnd.edge.SetVertex(lbnd.leftRight, v);
                    rbnd.edge.SetVertex(rbnd.leftRight, v);
                    edgeList.Remove(lbnd);
                    heap.Remove(rbnd);
                    edgeList.Remove(rbnd);
                    leftRight = LR.LEFT;
                    if (bottomSite.y > topSite.y)
                    {
                        tempSite   = bottomSite;
                        bottomSite = topSite;
                        topSite    = tempSite;
                        leftRight  = LR.RIGHT;
                    }
                    edge = Edge.CreateBisectingEdge(bottomSite, topSite);
                    edges.Add(edge);
                    bisector = Halfedge.Create(edge, leftRight);
                    halfEdges.Add(bisector);
                    edgeList.Insert(llbnd, bisector);
                    edge.SetVertex(LR.Other(leftRight), v);
                    if ((vertex = Vertex.Intersect(llbnd, bisector)) != null)
                    {
                        vertices.Add(vertex);
                        heap.Remove(llbnd);
                        llbnd.vertex = vertex;
                        llbnd.ystar  = vertex.y + bottomSite.Dist(vertex);
                        heap.Insert(llbnd);
                    }
                    if ((vertex = Vertex.Intersect(bisector, rrbnd)) != null)
                    {
                        vertices.Add(vertex);
                        bisector.vertex = vertex;
                        bisector.ystar  = vertex.y + bottomSite.Dist(vertex);
                        heap.Insert(bisector);
                    }
                }
                else
                {
                    break;
                }
            }

            // Heap should be empty now
            heap.Dispose();
            edgeList.Dispose();

            foreach (Halfedge halfedge in halfEdges)
            {
                halfedge.ReallyDispose();
            }
            halfEdges.Clear();

            // we need the vertices to clip the edges
            foreach (Edge e in edges)
            {
                e.ClipVertices(plotBounds);
            }
            // But we don't actually ever use them again!
            foreach (Vertex ve in vertices)
            {
                ve.Dispose();
            }
            vertices.Clear();
        }
Exemple #7
0
        private void Connect(ref List <Vector2f> points, int j, Rectf bounds, bool closingUp = false)
        {
            Vector2f rightPoint     = points[points.Count - 1];
            Edge     newEdge        = edges[j];
            LR       newOrientation = edgeOrientations[j];

            // The point that must be conected to rightPoint:
            Vector2f newPoint = newEdge.ClippedEnds[newOrientation];

            if (!CloseEnough(rightPoint, newPoint))
            {
                // The points do not coincide, so they must have been clipped at the bounds;
                // see if they are on the same border of the bounds:
                if (rightPoint.x != newPoint.x && rightPoint.y != newPoint.y)
                {
                    // They are on different borders of the bounds;
                    // insert one or two corners of bounds as needed to hook them up:
                    // (NOTE this will not be correct if the region should take up more than
                    // half of the bounds rect, for then we will have gone the wrong way
                    // around the bounds and included the smaller part rather than the larger)
                    int   rightCheck = BoundsCheck.Check(rightPoint, bounds);
                    int   newCheck = BoundsCheck.Check(newPoint, bounds);
                    float px, py;
                    if ((rightCheck & BoundsCheck.RIGHT) != 0)
                    {
                        px = bounds.right;

                        if ((newCheck & BoundsCheck.BOTTOM) != 0)
                        {
                            py = bounds.bottom;
                            points.Add(new Vector2f(px, py));
                        }
                        else if ((newCheck & BoundsCheck.TOP) != 0)
                        {
                            py = bounds.top;
                            points.Add(new Vector2f(px, py));
                        }
                        else if ((newCheck & BoundsCheck.LEFT) != 0)
                        {
                            if (rightPoint.y - bounds.y + newPoint.y - bounds.y < bounds.height)
                            {
                                py = bounds.top;
                            }
                            else
                            {
                                py = bounds.bottom;
                            }
                            points.Add(new Vector2f(px, py));
                            points.Add(new Vector2f(bounds.left, py));
                        }
                    }
                    else if ((rightCheck & BoundsCheck.LEFT) != 0)
                    {
                        px = bounds.left;

                        if ((newCheck & BoundsCheck.BOTTOM) != 0)
                        {
                            py = bounds.bottom;
                            points.Add(new Vector2f(px, py));
                        }
                        else if ((newCheck & BoundsCheck.TOP) != 0)
                        {
                            py = bounds.top;
                            points.Add(new Vector2f(px, py));
                        }
                        else if ((newCheck & BoundsCheck.RIGHT) != 0)
                        {
                            if (rightPoint.y - bounds.y + newPoint.y - bounds.y < bounds.height)
                            {
                                py = bounds.top;
                            }
                            else
                            {
                                py = bounds.bottom;
                            }
                            points.Add(new Vector2f(px, py));
                            points.Add(new Vector2f(bounds.right, py));
                        }
                    }
                    else if ((rightCheck & BoundsCheck.TOP) != 0)
                    {
                        py = bounds.top;

                        if ((newCheck & BoundsCheck.RIGHT) != 0)
                        {
                            px = bounds.right;
                            points.Add(new Vector2f(px, py));
                        }
                        else if ((newCheck & BoundsCheck.LEFT) != 0)
                        {
                            px = bounds.left;
                            points.Add(new Vector2f(px, py));
                        }
                        else if ((newCheck & BoundsCheck.BOTTOM) != 0)
                        {
                            if (rightPoint.x - bounds.x + newPoint.x - bounds.x < bounds.width)
                            {
                                px = bounds.left;
                            }
                            else
                            {
                                px = bounds.right;
                            }
                            points.Add(new Vector2f(px, py));
                            points.Add(new Vector2f(px, bounds.bottom));
                        }
                    }
                    else if ((rightCheck & BoundsCheck.BOTTOM) != 0)
                    {
                        py = bounds.bottom;

                        if ((newCheck & BoundsCheck.RIGHT) != 0)
                        {
                            px = bounds.right;
                            points.Add(new Vector2f(px, py));
                        }
                        else if ((newCheck & BoundsCheck.LEFT) != 0)
                        {
                            px = bounds.left;
                            points.Add(new Vector2f(px, py));
                        }
                        else if ((newCheck & BoundsCheck.TOP) != 0)
                        {
                            if (rightPoint.x - bounds.x + newPoint.x - bounds.x < bounds.width)
                            {
                                px = bounds.left;
                            }
                            else
                            {
                                px = bounds.right;
                            }
                            points.Add(new Vector2f(px, py));
                            points.Add(new Vector2f(px, bounds.top));
                        }
                    }
                }
                if (closingUp)
                {
                    // newEdge's ends have already been added
                    return;
                }
                points.Add(newPoint);
            }
            Vector2f newRightPoint = newEdge.ClippedEnds[LR.Other(newOrientation)];

            if (!CloseEnough(points[0], newRightPoint))
            {
                points.Add(newRightPoint);
            }
        }
Exemple #8
0
        private void Fortunes()
        {
            Site nSite;
            Site bottomSite;
            Site topSite;
            Site tSite;

            Vertex v1;
            Vertex v2;

            Point newIntStar = null;

            LR       lr;
            HalfEdge lBnd;
            HalfEdge rBnd;
            HalfEdge llBnd;
            HalfEdge rrBnd;
            HalfEdge bisector;

            Edge e;

            Rectangle dataBounds = sites.GetSitesBounds();

            int sqrtNSites             = (int)Math.Sqrt(sites.GetLength() + 4);
            HalfEdgePriorityQueue heap = new HalfEdgePriorityQueue(dataBounds.y, dataBounds.height, sqrtNSites);
            EdgeList        edgeList   = new EdgeList(dataBounds.x, dataBounds.width, sqrtNSites);
            List <HalfEdge> halfEdges  = new List <HalfEdge>();
            List <Vertex>   vertices   = new List <Vertex>();

            Site lowestSite = this.sites.Next();

            nSite = sites.Next();

            for (;;)
            {
                if (heap.Empty() == false)
                {
                    newIntStar = heap.Min();
                }

                if (nSite != null &&
                    (heap.Empty() || CompareByYThenX(nSite, newIntStar) < 0))
                {
                    lBnd       = edgeList.EdgeListLeftNeighbor(nSite.coord);
                    rBnd       = lBnd.edgeListRightNeighbor;
                    bottomSite = RightRegion(lBnd, lowestSite);
                    e          = Edge.CreateBisectingEdge(bottomSite, nSite);
                    this.edges.Add(e);

                    bisector = HalfEdge.Create(e, LR.LEFT);
                    halfEdges.Add(bisector);
                    //Console.WriteLine(lBnd.edgeListLeftNeighbor);
                    //Console.WriteLine(lBnd.edgeListRightNeighbor);
                    edgeList.Insert(lBnd, bisector);

                    if ((v2 = Vertex.Intersect(lBnd, bisector)) != null)
                    {
                        vertices.Add(v2);
                        heap.Remove(lBnd);
                        lBnd.v     = v2;
                        lBnd.yStar = v2.GetCoord().y + nSite.Dist(v2);
                        heap.Insert(lBnd);
                    }

                    lBnd     = bisector;
                    bisector = HalfEdge.Create(e, LR.RIGHT);
                    halfEdges.Add(bisector);
                    edgeList.Insert(lBnd, bisector);

                    if ((v2 = Vertex.Intersect(bisector, rBnd)) != null)
                    {
                        vertices.Add(v2);
                        bisector.v     = v2;
                        bisector.yStar = v2.GetCoord().y + nSite.Dist(v2);
                        heap.Insert(bisector);
                    }

                    nSite = this.sites.Next();
                }
                else if (heap.Empty() == false)
                {
                    lBnd  = heap.ExtractMin();
                    llBnd = lBnd.edgeListLeftNeighbor;
                    rBnd  = lBnd.edgeListRightNeighbor;
                    rrBnd = rBnd.edgeListRightNeighbor;

                    bottomSite = LeftRegion(lBnd, lowestSite);
                    topSite    = RightRegion(rBnd, lowestSite);

                    v1 = lBnd.v;
                    v1.SetIndex();
                    lBnd.e.SetVertex(lBnd.lr, v1);
                    rBnd.e.SetVertex(rBnd.lr, v1);
                    edgeList.Remove(lBnd);
                    heap.Remove(rBnd);
                    edgeList.Remove(rBnd);
                    lr = LR.LEFT;
                    if (bottomSite.coord.y > topSite.coord.y)
                    {
                        tSite      = bottomSite;
                        bottomSite = topSite;
                        topSite    = tSite;
                        lr         = LR.RIGHT;
                    }
                    e = Edge.CreateBisectingEdge(bottomSite, topSite);
                    this.edges.Add(e);
                    bisector = HalfEdge.Create(e, lr);
                    halfEdges.Add(bisector);
                    edgeList.Insert(llBnd, bisector);
                    e.SetVertex(LR.Other(lr), v1);

                    if ((v2 = Vertex.Intersect(llBnd, bisector)) != null)
                    {
                        vertices.Add(v2);
                        heap.Remove(llBnd);
                        llBnd.v     = v2;
                        llBnd.yStar = v2.GetCoord().y + bottomSite.Dist(v2);
                        heap.Insert(llBnd);
                    }
                    if ((v2 = Vertex.Intersect(bisector, rrBnd)) != null)
                    {
                        vertices.Add(v2);
                        bisector.v     = v2;
                        bisector.yStar = v2.GetCoord().y + bottomSite.Dist(v2);
                        heap.Insert(bisector);
                    }
                }
                else
                {
                    break;
                }
            }

            heap.Dispose();
            edgeList.Dispose();

            foreach (HalfEdge hE in halfEdges)
            {
                hE.Kill();
            }
            halfEdges.Clear();

            foreach (Edge tE in this.edges)
            {
                tE.ClipVertices(bounds);
            }

            foreach (Vertex v0 in vertices)
            {
                v0.Dispose();
            }
            vertices.Clear();
        }
Exemple #9
0
        private void Connect(List <Point> points, int j, Rectangle bounds, bool Closing)
        {
            Point rightPoint = points[points.Count - 1];
            Edge  nEdge      = edges[j];
            LR    nOrient    = edgeOrientations[j];
            Point nPoint     = nEdge.GetClippedEnds()[nOrient];

            if (CloseEnough(rightPoint, nPoint))
            {
                if ((rightPoint.x != nPoint.x) && (rightPoint.y != nPoint.y))
                {
                    int rightCheck = BoundsCheck.Check(rightPoint, bounds);
                    int nCheck     = BoundsCheck.Check(nPoint, bounds);

                    double pX;
                    double pY;

                    if ((rightCheck & BoundsCheck.RIGHT) != 0)
                    {
                        pX = bounds.right;
                        if ((nCheck & BoundsCheck.BOTTOM) != 0)
                        {
                            pY = bounds.bottom;
                            points.Add(new Point(pX, pY));
                        }
                        else if ((nCheck & BoundsCheck.TOP) != 0)
                        {
                            pY = bounds.top;
                            points.Add(new Point(pX, pY));
                        }
                        else if ((nCheck & BoundsCheck.LEFT) != 0)
                        {
                            if (((rightPoint.y - bounds.y + nPoint.y) - bounds.y) < bounds.height)
                            {
                                pY = bounds.top;
                            }
                            else
                            {
                                pY = bounds.bottom;
                            }
                            points.Add(new Point(pX, pY));
                            points.Add(new Point(bounds.left, pY));
                        }
                    }
                    else if ((rightCheck & BoundsCheck.LEFT) != 0)
                    {
                        pX = bounds.left;
                        if ((nCheck & BoundsCheck.BOTTOM) != 0)
                        {
                            pY = bounds.bottom;
                            points.Add(new Point(pX, pY));
                        }
                        else if ((nCheck & BoundsCheck.TOP) != 0)
                        {
                            pY = bounds.top;
                            points.Add(new Point(pX, pY));
                        }
                        else if ((nCheck & BoundsCheck.RIGHT) != 0)
                        {
                            if (((rightPoint.y - bounds.y + nPoint.y) - bounds.y) < bounds.height)
                            {
                                pY = bounds.top;
                            }
                            else
                            {
                                pY = bounds.bottom;
                            }
                            points.Add(new Point(pX, pY));
                            points.Add(new Point(bounds.right, pY));
                        }
                    }
                    else if ((rightCheck & BoundsCheck.TOP) != 0)
                    {
                        pY = bounds.top;
                        if ((nCheck & BoundsCheck.RIGHT) != 0)
                        {
                            pX = bounds.right;
                            points.Add(new Point(pX, pY));
                        }
                        else if ((nCheck & BoundsCheck.LEFT) != 0)
                        {
                            pX = bounds.left;
                            points.Add(new Point(pX, pY));
                        }
                        else if ((nCheck & BoundsCheck.BOTTOM) != 0)
                        {
                            if (((rightPoint.x - bounds.x + nPoint.x) - bounds.x) < bounds.width)
                            {
                                pX = bounds.left;
                            }
                            else
                            {
                                pX = bounds.right;
                            }
                            points.Add(new Point(pX, pY));
                            points.Add(new Point(pX, bounds.bottom));
                        }
                    }
                    else if ((rightCheck & BoundsCheck.BOTTOM) != 0)
                    {
                        pY = bounds.bottom;
                        if ((nCheck & BoundsCheck.RIGHT) != 0)
                        {
                            pX = bounds.right;
                            points.Add(new Point(pX, pY));
                        }
                        else if ((nCheck & BoundsCheck.LEFT) != 0)
                        {
                            pX = bounds.left;
                            points.Add(new Point(pX, pY));
                        }
                        else if ((nCheck & BoundsCheck.TOP) != 0)
                        {
                            if (((rightPoint.x - bounds.x + nPoint.x) - bounds.x) < bounds.width)
                            {
                                pX = bounds.left;
                            }
                            else
                            {
                                pX = bounds.right;
                            }
                            points.Add(new Point(pX, pY));
                            points.Add(new Point(pX, bounds.top));
                        }
                    }
                }

                if (Closing)
                {
                    return;
                }
                points.Add(nPoint);
            }
            Point nRightPoint = nEdge.GetClippedEnds()[LR.Other(nOrient)];

            if (!CloseEnough(points[0], nRightPoint))
            {
                points.Add(nRightPoint);
            }
        }