Пример #1
0
        public void Run(List <FortuneSite> sites, PoolLinkedList <VEdge> edges, double minX, double minY, double maxX, double maxY)
        {
            eventQueue.Initialize(5 * sites.Count);
            beachLine.Clear();
            deleted.Clear();

            foreach (var s in sites)
            {
                var fEvent = ObjectPool <FortuneSiteEvent> .Get();

                fEvent.Initialize(s);
                eventQueue.Insert(fEvent);
            }

            //init edge list
            while (eventQueue.Count != 0)
            {
                var fEvent = eventQueue.Pop();
                if (fEvent is FortuneSiteEvent)
                {
                    beachLine.AddBeachSection((FortuneSiteEvent)fEvent, eventQueue, deleted, edges);
                    ObjectPool <FortuneSiteEvent> .Recycle((FortuneSiteEvent)fEvent);
                }
                else
                {
                    if (deleted.Contains((FortuneCircleEvent)fEvent))
                    {
                        deleted.Remove((FortuneCircleEvent)fEvent);
                    }
                    else
                    {
                        beachLine.RemoveBeachSection((FortuneCircleEvent)fEvent, eventQueue, deleted, edges);
                    }
                    ObjectPool <FortuneCircleEvent> .Recycle((FortuneCircleEvent)fEvent);
                }
            }


            //clip edges
            var edgeNode = edges.First;

            while (edgeNode != null)
            {
                var edge = edgeNode.Value;
                var next = edgeNode.Next;

                var valid = ClipEdge(edge, minX, minY, maxX, maxY);
                if (!valid)
                {
                    edges.Remove(edgeNode);
                    if (edgeNode.Value.Neighbor != null)
                    {
                        ObjectPool <VEdge> .Recycle(edgeNode.Value.Neighbor);
                    }
                    ObjectPool <VEdge> .Recycle(edgeNode.Value);
                }
                //advance
                edgeNode = next;
            }
        }
Пример #2
0
        public static LinkedList <VEdge> Run(List <FortuneSite> sites, double minX, double minY, double maxX, double maxY)
        {
            var eventQueue = new MinHeap <FortuneEvent>(5 * sites.Count);

            foreach (var s in sites)
            {
                eventQueue.Insert(new FortuneSiteEvent(s));
            }
            //init tree
            var beachLine = new BeachLine();
            var edges     = new LinkedList <VEdge>();
            var deleted   = new HashSet <FortuneCircleEvent>();

            //init edge list
            while (eventQueue.Count != 0)
            {
                var fEvent = eventQueue.Pop();
                if (fEvent is FortuneSiteEvent)
                {
                    beachLine.AddBeachSection((FortuneSiteEvent)fEvent, eventQueue, deleted, edges);
                }
                else
                {
                    if (deleted.Contains((FortuneCircleEvent)fEvent))
                    {
                        deleted.Remove((FortuneCircleEvent)fEvent);
                    }
                    else
                    {
                        beachLine.RemoveBeachSection((FortuneCircleEvent)fEvent, eventQueue, deleted, edges);
                    }
                }
            }


            //clip edges
            var edgeNode = edges.First;

            while (edgeNode != null)
            {
                var edge = edgeNode.Value;
                var next = edgeNode.Next;

                var valid = ClipEdge(edge, minX, minY, maxX, maxY);
                if (!valid)
                {
                    edges.Remove(edgeNode);
                }
                //advance
                edgeNode = next;
            }
            return(edges);
        }
Пример #3
0
    public LinkedList <Edge> GenerateVoronoi(List <FortuneSite> sites, double minX, double minY, double maxX, double maxY)
    {
        MinHeap <FortuneEvent> eventQueue = new MinHeap <FortuneEvent>(5 * sites.Count);

        foreach (FortuneSite site in sites)
        {
            eventQueue.Insert(new FortuneSiteEvent(site));
        }

        BeachLine                    beachLine = new BeachLine();
        LinkedList <Edge>            edges     = new LinkedList <Edge>();
        HashSet <FortuneCircleEvent> deleted   = new HashSet <FortuneCircleEvent>();

        while (eventQueue.count != 0)
        {
            FortuneEvent fortuneEvent = eventQueue.Pop();

            if (fortuneEvent is FortuneSiteEvent)
            {
                beachLine.AddSection((FortuneSiteEvent)fortuneEvent, eventQueue, deleted, edges);
            }
            else
            {
                if (deleted.Contains((FortuneCircleEvent)fortuneEvent))
                {
                    deleted.Remove((FortuneCircleEvent)fortuneEvent);
                }
                else
                {
                    beachLine.RemoveBeachSection((FortuneCircleEvent)fortuneEvent, eventQueue, deleted, edges);
                }
            }
        }

        LinkedListNode <Edge> edgeNode = edges.First;

        while (edgeNode != null)
        {
            Edge edge = edgeNode.Value;
            LinkedListNode <Edge> nextNode = edgeNode.Next;

            if (!ClipEdge(edge, minX, minY, maxX, maxY))
            {
                edges.Remove(edgeNode);
            }

            edgeNode = nextNode;
        }

        return(edges);
    }