Пример #1
0
		public static Halfedge Create(Edge edge, LR lr) {
			if (pool.Count > 0) {
				return pool.Dequeue().Init(edge,lr);
			} else {
				return new Halfedge(edge,lr);
			}
		}
Пример #2
0
		private Halfedge Init(Edge edge, LR lr) {
			this.edge = edge;
			leftRight = lr;
			nextInPriorityQueue = null;
			vertex = null;

			return this;
		}
Пример #3
0
		public void ReallyDispose() {
			edgeListLeftNeighbor = null;
			edgeListRightNeighbor = null;
			nextInPriorityQueue = null;
			edge = null;
			leftRight = null;
			vertex = null;
			pool.Enqueue(this);
		}
Пример #4
0
        private Halfedge Init(Edge edge, LR lr)
        {
            this.edge           = edge;
            leftRight           = lr;
            nextInPriorityQueue = null;
            vertex = null;

            return(this);
        }
Пример #5
0
        private Halfedge Init(Edge edge, LR lr)
        {
            this.Edge                = edge;
            this.LeftRight           = lr;
            this.NextInPriorityQueue = null;
            this.Vertex              = null;

            return(this);
        }
Пример #6
0
 public void ReallyDispose(VoronoiManager manager)
 {
     edgeListLeftNeighbor  = null;
     edgeListRightNeighbor = null;
     nextInPriorityQueue   = null;
     edge      = null;
     leftRight = null;
     vertex    = null;
     manager.Release(this);
 }
Пример #7
0
 public void ReallyDispose()
 {
     edgeListLeftNeighbor  = null;
     edgeListRightNeighbor = null;
     nextInPriorityQueue   = null;
     edge      = null;
     leftRight = null;
     vertex    = null;
     pool.Enqueue(this);
 }
Пример #8
0
        private Site RightRegion(Halfedge he, Site bottomMostSite)
        {
            Edge edge = he.edge;

            if (edge == null)
            {
                return(bottomMostSite);
            }
            return(edge.Site(LR.Other(he.leftRight)));
        }
Пример #9
0
 public void SetVertex(LR leftRight, Vertex v)
 {
     if (leftRight == LR.LEFT)
     {
         this.LeftVertex = v;
     }
     else
     {
         this.RightVertex = v;
     }
 }
Пример #10
0
 public void SetVertex(LR leftRight, Vertex v)
 {
     if (leftRight == LR.LEFT)
     {
         leftVertex = v;
     }
     else
     {
         rightVertex = v;
     }
 }
Пример #11
0
 public static Halfedge Create(Edge edge, LR lr)
 {
     if (pool.Count > 0)
     {
         return(pool.Dequeue().Init(edge, lr));
     }
     else
     {
         return(new Halfedge(edge, lr));
     }
 }
Пример #12
0
		public void Dispose() {
			if (edgeListLeftNeighbor != null || edgeListRightNeighbor != null) {
				// still in EdgeList
				return;
			}
			if (nextInPriorityQueue != null) {
				// still in PriorityQueue
				return;
			}
			edge = null;
			leftRight = null;
			vertex = null;
			pool.Enqueue(this);
		}
Пример #13
0
 public void Dispose()
 {
     if (edgeListLeftNeighbor != null || edgeListRightNeighbor != null)
     {
         // still in EdgeList
         return;
     }
     if (nextInPriorityQueue != null)
     {
         // still in PriorityQueue
         return;
     }
     edge      = null;
     leftRight = null;
     vertex    = null;
     pool.Enqueue(this);
 }
Пример #14
0
 public void Dispose(VoronoiManager manager)
 {
     if (edgeListLeftNeighbor != null || edgeListRightNeighbor != null)
     {
         // still in EdgeList
         return;
     }
     if (nextInPriorityQueue != null)
     {
         // still in PriorityQueue
         return;
     }
     edge      = null;
     leftRight = null;
     vertex    = null;
     manager.Release(this);
 }
Пример #15
0
 public T this[LR index]
 {
     get
     {
         return(index == LR.LEFT ? left : right);
     }
     set
     {
         if (index == LR.LEFT)
         {
             left = value;
         }
         else
         {
             right = value;
         }
     }
 }
Пример #16
0
        private List <Vector2f> ClipToBounds(Rectf bounds)
        {
            List <Vector2f> points = new List <Vector2f>();
            int             n      = this.Edges.Count;
            int             i      = 0;
            Edge            edge;

            // Look for the index of the first 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);
        }
Пример #17
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);
        }
Пример #18
0
		public void SetVertex(LR leftRight, Vertex v) {
			if (leftRight == LR.LEFT) {
				leftVertex = v;
			} else {
				rightVertex = v;
			}
		}
Пример #19
0
        private void FortunesAlgorithm()
        {
            Site     newSite, bottomSite, topSite, tempSite;
            Vertex   v, vertex;
            Vector2  newIntStar = Vector2.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();
        }
Пример #20
0
		public Vertex Vertex(LR leftRight) {
			return leftRight == LR.LEFT ? leftVertex : rightVertex;
		}
Пример #21
0
 public Vertex Vertex(LR leftRight)
 {
     return(leftRight == LR.LEFT ? this.LeftVertex : this.RightVertex);
 }
Пример #22
0
 public Vertex Vertex(LR leftRight)
 {
     return(leftRight == LR.LEFT ? leftVertex : rightVertex);
 }
Пример #23
0
 public Halfedge(Edge edge, LR lr)
 {
     Init(edge, lr);
 }
Пример #24
0
		public Site Site(LR leftRight) {
			return sites[leftRight];
		}
Пример #25
0
 public static Halfedge Create(VoronoiManager manager, Edge edge, LR lr)
 {
     return(manager.ObtainHalfedge().Init(edge, lr));
 }
Пример #26
0
		public Halfedge(Edge edge, LR lr) {
			Init(edge, lr);
		}
Пример #27
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 = this.sitelist.GetSitesBounds();

            int sqrtSitesNb            = (int)Math.Sqrt(sitelist.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 = this.sitelist.Next();

            newSite = this.sitelist.Next();

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

                if (newSite != null && (heap.Empty || CompareByYThenX(newSite, newIntStar) < 0))
                {
                    // Step 8:
                    lbnd       = edgeList.EdgeListLeftNeighbor(newSite.Coord); // The halfedge just to the left of newSite.
                    rbnd       = lbnd.EdgeListRightNeighbor;                   // The halfedge just to the right.
                    bottomSite = RightRegion(lbnd, bottomMostSite);            // This is the same as leftRegion(rbnd).

                    // Step 9:
                    edge = Edge.CreateBisectingEdge(bottomSite, newSite);
                    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 = sitelist.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(this.PlotBounds);
            }
        }
Пример #28
0
 public Halfedge(Edge edge, LR lr)
 {
     this.Init(edge, lr);
 }
Пример #29
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);
            }
        }
Пример #30
0
 public Site Site(LR leftRight)
 {
     return(sites[leftRight]);
 }