Exemple #1
0
        public static VoronoiGraph ComputeVoronoiGraph(IEnumerable Datapoints)
        {
            BinaryPriorityQueue PQ             = new BinaryPriorityQueue();
            Hashtable           CurrentCircles = new Hashtable();
            VoronoiGraph        VG             = new VoronoiGraph();
            VNode RootNode = null;

            foreach (Vector V in Datapoints)
            {
                PQ.Push(new VDataEvent(V));
            }
            while (PQ.Count > 0)
            {
                VEvent      VE = PQ.Pop() as VEvent;
                VDataNode[] CircleCheckList;
                if (VE is VDataEvent)
                {
                    RootNode = VNode.ProcessDataEvent(VE as VDataEvent, RootNode, VG, VE.Y, out CircleCheckList);
                }
                else if (VE is VCircleEvent)
                {
                    CurrentCircles.Remove(((VCircleEvent)VE).NodeN);
                    if (!((VCircleEvent)VE).Valid)
                    {
                        continue;
                    }
                    RootNode = VNode.ProcessCircleEvent(VE as VCircleEvent, RootNode, VG, VE.Y, out CircleCheckList);
                }
                else
                {
                    throw new Exception("Got event of type " + VE.GetType().ToString() + "!");
                }
                foreach (VDataNode VD in CircleCheckList)
                {
                    if (CurrentCircles.ContainsKey(VD))
                    {
                        ((VCircleEvent)CurrentCircles[VD]).Valid = false;
                        CurrentCircles.Remove(VD);
                    }
                    VCircleEvent VCE = VNode.CircleCheckDataNode(VD, VE.Y);
                    if (VCE != null)
                    {
                        PQ.Push(VCE);
                        CurrentCircles[VD] = VCE;
                    }
                }
                if (VE is VDataEvent)
                {
                    Vector DP = ((VDataEvent)VE).DataPoint;
                    foreach (VCircleEvent VCE in CurrentCircles.Values)
                    {
                        if (MathTools.Dist(DP[0], DP[1], VCE.Center[0], VCE.Center[1]) < VCE.Y - VCE.Center[1] && Math.Abs(MathTools.Dist(DP[0], DP[1], VCE.Center[0], VCE.Center[1]) - (VCE.Y - VCE.Center[1])) > 1e-10)
                        {
                            VCE.Valid = false;
                        }
                    }
                }
            }
            return(VG);
        }
Exemple #2
0
 public PriorityQueueState()
 {
     Queue        = new BinaryPriorityQueue(new CacheItemComparer());
     EnqueueCount = 0;
     DequeueCount = 0;
     DisableCount = 0;
     PeekCount    = 0;
     UpdateCount  = 0;
 }
Exemple #3
0
 public PriorityQueueState(string listName, string testsName)
 {
     Queue        = new BinaryPriorityQueue(new CacheItemComparer());
     EnqueueCount = 0;
     DequeueCount = 0;
     DisableCount = 0;
     PeekCount    = 0;
     ListName     = listName;
     TestsName    = testsName;
 }
        /// <summary>
        /// Creates a prefix tree from a given character weights hash table.
        /// </summary>
        ///
        /// <param name="symbolWeights">The character weights.</param>
        public PrefixTree(long[] symbolWeights)
        {
            // Create the (binary) priority queue of prefix tree nodes (leaves).
            BinaryPriorityQueue binaryPriorityQueue = new BinaryPriorityQueue();

            // Populate the (binary) priority queue of prefix tree nodes (leaves).
            for (int symbol = 0; symbol <= byte.MaxValue; symbol++)
            {
                if (symbolWeights[(byte)symbol] == 0)
                {
                    continue;
                }
                PrefixTreeNode prefixTreeNode = new PrefixTreeNode((byte)symbol, (long)symbolWeights[(byte)symbol]);
                binaryPriorityQueue.Push(prefixTreeNode);
            }

            // Build the prefix tree using a binary priority queue.
            while (binaryPriorityQueue.Count != 1)
            {
                // The node with the smallest weight becomes the right child of its parent.
                PrefixTreeNode rightChildNode = (PrefixTreeNode)binaryPriorityQueue.Pop();

                // The node with the second smallest weight becomes the left child of its parent.
                PrefixTreeNode leftChildNode = (PrefixTreeNode)binaryPriorityQueue.Pop();

                // Create the parent node and enqueue it.
                PrefixTreeNode parentNode = new PrefixTreeNode(leftChildNode, rightChildNode);
                binaryPriorityQueue.Push(parentNode);
            }

            // The last remaning node becomes the root node of the prefix tree.
            rootNode = (PrefixTreeNode)binaryPriorityQueue.Pop();

            // Recursively assign prefixes to the nodes strating from the root node.
            AssignPrefixes();
        }
Exemple #5
0
        public static bool FTestSimplePolygon(IEnumerable <Vector> poly)
        {
            var polyList = poly?.ToList();
            var sweep    = T.MinValue;

            if (polyList == null)
            {
                throw new ArgumentException($"poly is null in {nameof(FTestSimplePolygon)}");
            }

            if (polyList.Count == 0)
            {
                return(true);
            }

            if (polyList[0].Rank != 2)
            {
                throw new ArgumentException("Calling FTestSimplePolygon with non-2D points");
            }

            var edgeList   = new List <RbLineSegment>();
            var eventQueue = new BinaryPriorityQueue <SimplePolygonEvent>((e1, e2) => CmpVectors(e1.Vertex, e2.Vertex));

            for (var iVtx = 0; iVtx < polyList.Count; iVtx++)
            {
                var vtxLow  = polyList[iVtx];
                var vtxHigh = polyList[(iVtx + 1) % polyList.Count];
                var cmp     = CmpVectors(vtxLow, vtxHigh);
                if (cmp == 0)
                {
                    // We ignore zero length sides
                    continue;
                }
                if (cmp > 0)
                {
                    (vtxLow, vtxHigh) = (vtxHigh, vtxLow);
                }
                // ReSharper disable once AccessToModifiedClosure
                edgeList.Add(new RbLineSegment(vtxLow, vtxHigh, () => sweep));
                eventQueue.Add(new SimplePolygonEvent(vtxLow, iVtx, false));
                eventQueue.Add(new SimplePolygonEvent(vtxHigh, iVtx, true));
            }

            var segTable = new MeshNavRbTree();

            while (eventQueue.Count != 0)
            {
                var nextEvent = eventQueue.Pop();
                if (nextEvent.IsRightEndpoint)
                {
                    // We have to remove the corresponding line segment from consideration
                    segTable.DeleteBracketed(edgeList[nextEvent.SegIndex]);
                }
                else
                {
                    sweep = nextEvent.Vertex.X;
                    var lsCur = edgeList[nextEvent.SegIndex];

                    // Insert the corresponding line segment
                    (RbLineSegment high, RbLineSegment low) = segTable.InsertBracketed(lsCur);
                    if (lsCur.Intersects(low) || lsCur.Intersects(high))
                    {
                        return(false);
                    }
                }
            }
            return(true);

            int CmpVectors(Vector v1, Vector v2)
            {
                var cmp = v1.X.CompareTo(v2.X);

                if (cmp == 0)
                {
                    cmp = v1.Y.CompareTo(v2.Y);
                }
                return(cmp);
            }
        }
        public static VoronoiGraph ComputeVoronoiGraph(IEnumerable Datapoints)
        {
            BinaryPriorityQueue PQ             = new BinaryPriorityQueue();
            Hashtable           CurrentCircles = new Hashtable();
            VoronoiGraph        VG             = new VoronoiGraph();
            VNode RootNode = null;

            foreach (Vector V in Datapoints)
            {
                PQ.Push(new VDataEvent(V));
            }
            while (PQ.Count > 0)
            {
                VEvent      VE = PQ.Pop() as VEvent;
                VDataNode[] CircleCheckList;
                if (VE is VDataEvent)
                {
                    RootNode = VNode.ProcessDataEvent(VE as VDataEvent, RootNode, VG, VE.Y, out CircleCheckList);
                }
                else if (VE is VCircleEvent)
                {
                    CurrentCircles.Remove(((VCircleEvent)VE).NodeN);
                    if (!((VCircleEvent)VE).Valid)
                    {
                        continue;
                    }
                    RootNode = VNode.ProcessCircleEvent(VE as VCircleEvent, RootNode, VG, VE.Y, out CircleCheckList);
                }
                else
                {
                    throw new Exception("Got event of type " + VE.GetType().ToString() + "!");
                }
                foreach (VDataNode VD in CircleCheckList)
                {
                    if (CurrentCircles.ContainsKey(VD))
                    {
                        ((VCircleEvent)CurrentCircles[VD]).Valid = false;
                        CurrentCircles.Remove(VD);
                    }
                    VCircleEvent VCE = VNode.CircleCheckDataNode(VD, VE.Y);
                    if (VCE != null)
                    {
                        PQ.Push(VCE);
                        CurrentCircles[VD] = VCE;
                    }
                }
                if (VE is VDataEvent)
                {
                    Vector DP = ((VDataEvent)VE).DataPoint;
                    foreach (VCircleEvent VCE in CurrentCircles.Values)
                    {
                        if (MathTools.Dist(DP[0], DP[1], VCE.Center[0], VCE.Center[1]) < VCE.Y - VCE.Center[1] && Math.Abs(MathTools.Dist(DP[0], DP[1], VCE.Center[0], VCE.Center[1]) - (VCE.Y - VCE.Center[1])) > 1e-10)
                        {
                            VCE.Valid = false;
                        }
                    }
                }
            }
            VNode.CleanUpTree(RootNode);
            foreach (VoronoiEdge VE in VG.Edges)
            {
                if (VE.Done)
                {
                    continue;
                }
                if (VE.VVertexB == Fortune.VVUnkown)
                {
                    VE.AddVertex(Fortune.VVInfinite);
                    if (Math.Abs(VE.LeftData[1] - VE.RightData[1]) < 1e-10 && VE.LeftData[0] < VE.RightData[0])
                    {
                        Vector T = VE.LeftData;
                        VE.LeftData  = VE.RightData;
                        VE.RightData = T;
                    }
                }
            }

            ArrayList MinuteEdges = new ArrayList();

            foreach (VoronoiEdge VE in VG.Edges)
            {
                if (!VE.IsPartlyInfinite && VE.VVertexA.Equals(VE.VVertexB))
                {
                    MinuteEdges.Add(VE);
                    // prevent rounding errors from expanding to holes
                    foreach (VoronoiEdge VE2 in VG.Edges)
                    {
                        if (VE2.VVertexA.Equals(VE.VVertexA))
                        {
                            VE2.VVertexA = VE.VVertexA;
                        }
                        if (VE2.VVertexB.Equals(VE.VVertexA))
                        {
                            VE2.VVertexB = VE.VVertexA;
                        }
                    }
                }
            }
            foreach (VoronoiEdge VE in MinuteEdges)
            {
                VG.Edges.Remove(VE);
            }

            return(VG);
        }
 public static BinaryPriorityQueue ReadOnly(BinaryPriorityQueue P)
 {
     return(new BinaryPriorityQueue(ArrayList.ReadOnly(P.InnerList), P.Comparer, false));
 }
 public static BinaryPriorityQueue Syncronized(BinaryPriorityQueue P)
 {
     return(new BinaryPriorityQueue(ArrayList.Synchronized(P.InnerList), P.Comparer, false));
 }
Exemple #9
0
        public static VoronoiGraph ComputeVoronoiGraph(Dictionary<Point, VoronoiCell> cells)
        {
            BinaryPriorityQueue PQ = new BinaryPriorityQueue();
            Hashtable CurrentCircles = new Hashtable();
            VoronoiGraph VG = new VoronoiGraph();
            VNode RootNode = null;
            foreach(Point V in cells.Keys)
            {
                PQ.Push(new VDataEvent(V));
            }
            while(PQ.Count>0)
            {
                VEvent VE = PQ.Pop() as VEvent;
                VDataNode[] CircleCheckList;
                if(VE is VDataEvent)
                {
                    RootNode = VNode.ProcessDataEvent(VE as VDataEvent,RootNode,VG,VE.Y,out CircleCheckList);
                }
                else if(VE is VCircleEvent)
                {
                    CurrentCircles.Remove(((VCircleEvent)VE).NodeN);
                    if(!((VCircleEvent)VE).Valid)
                        continue;
                    RootNode = VNode.ProcessCircleEvent(VE as VCircleEvent,RootNode,VG,VE.Y,out CircleCheckList);
                }
                else throw new Exception("Got event of type "+VE.GetType().ToString()+"!");
                foreach(VDataNode VD in CircleCheckList)
                {
                    if(CurrentCircles.ContainsKey(VD))
                    {
                        ((VCircleEvent)CurrentCircles[VD]).Valid=false;
                        CurrentCircles.Remove(VD);
                    }
                    VCircleEvent VCE = VNode.CircleCheckDataNode(VD,VE.Y);
                    if(VCE!=null)
                    {
                        PQ.Push(VCE);
                        CurrentCircles[VD]=VCE;
                    }
                }
                if(VE is VDataEvent)
                {
                    Point DP = ((VDataEvent)VE).DataPoint;
                    foreach(VCircleEvent VCE in CurrentCircles.Values)
                    {
                        if (MathF.Dist(DP.X, DP.Y, VCE.Center.X, VCE.Center.Y) < VCE.Y - VCE.Center.Y && Math.Abs(MathF.Dist(DP.X, DP.Y, VCE.Center.X, VCE.Center.Y) - (VCE.Y - VCE.Center.Y)) > 1e-10)
                            VCE.Valid = false;
                    }
                }
            }
            VNode.CleanUpTree(RootNode);
            foreach(VoronoiEdge VE in VG.Edges)
            {
                if(VE.Done)
                    continue;
                if(VE.VVertexB.IsUndefined)
                {
                    VE.AddVertex(Fortune.VVInfinite);
                    if(Math.Abs(VE.LeftData.Y-VE.RightData.Y)<1e-10 && VE.LeftData.X<VE.RightData.X)
                    {
                        Point T = VE.LeftData;
                        VE.LeftData = VE.RightData;
                        VE.RightData = T;
                    }
                }
            }

            ArrayList MinuteEdges = new ArrayList();

            foreach (var edge in VG.Edges)
            {
                double ax = Math.Round(edge.VVertexA.X, Vector.Precision), ay = Math.Round(edge.VVertexA.Y, Vector.Precision),
                       bx = Math.Round(edge.VVertexB.X, Vector.Precision), by = Math.Round(edge.VVertexB.Y, Vector.Precision);
                if (ax == bx && ay == by)
                {
                    MinuteEdges.Add(edge);
                    continue;
                }

                edge.VVertexA = new Point(ax, ay);
                edge.VVertexB = new Point(bx, by);
            }

            foreach(VoronoiEdge VE in MinuteEdges)
                VG.Edges.Remove(VE);

            foreach (var edge in VG.Edges)
            {
                VoronoiCell rightCell = cells[edge.RightData], leftCell = cells[edge.LeftData];

                if (!rightCell.Edges.Contains(edge))
                {
                    rightCell.Edges.Add(edge);
                }
                if (!leftCell.Edges.Contains(edge))
                {
                    leftCell.Edges.Add(edge);
                }
            }

            VG.Cells = cells;

            return VG;
        }
        public static VoronoiGraph FortuneAlgo(List<Vector> points)
        {
            BinaryPriorityQueue PQ = new BinaryPriorityQueue();
            Hashtable CurrentCircles = new Hashtable();
            VoronoiGraph VG = new VoronoiGraph();
            VNode RootNode = null;
            for (int i = 0; i < points.Count; ++i)
                PQ.Push(new VDataEvent(points[i]));
            while (PQ.Count > 0)
            {
                VEvent VE = PQ.Pop() as VEvent;
                VDataNode[] CircleCheckList;
                if (VE is VDataEvent)
                    RootNode = ProcessDataEvent(VE as VDataEvent, RootNode, VG, VE.Y, out CircleCheckList);
                else if (VE is VCircleEvent)
                {
                    CurrentCircles.Remove(((VCircleEvent)VE).NodeN);
                    if (!((VCircleEvent)VE).Valid)
                        continue;
                    RootNode = ProcessCircleEvent(VE as VCircleEvent, RootNode, VG, VE.Y, out CircleCheckList);
                }
                else throw new Exception("Got event of type " + VE.GetType().ToString() + "!");
                foreach (VDataNode VD in CircleCheckList)
                {
                    if (CurrentCircles.ContainsKey(VD))
                    {
                        ((VCircleEvent)CurrentCircles[VD]).Valid = false;
                        CurrentCircles.Remove(VD);
                    }
                    VCircleEvent VCE = CircleCheckDataNode(VD, VE.Y);
                    if (VCE != null)
                    {
                        PQ.Push(VCE);
                        CurrentCircles[VD] = VCE;
                    }
                }
                if (VE is VDataEvent)
                {
                    Vector DP = ((VDataEvent)VE).DataPoint;
                    foreach (VCircleEvent VCE in CurrentCircles.Values)
                    {
                        if (MathTools.Dist(DP[0], DP[1], VCE.Center[0], VCE.Center[1]) < VCE.Y - VCE.Center[1] && Math.Abs(MathTools.Dist(DP[0], DP[1], VCE.Center[0], VCE.Center[1]) - (VCE.Y - VCE.Center[1])) > 1e-10)
                            VCE.Valid = false;
                    }
                }
            }
            VNode.CleanUpTree(RootNode);
            foreach (VoronoiEdge VE in VG.Edges)
            {
                if (VE.Done)
                    continue;
                if (VE.VVertexB == Fortune.VVUnkown)
                {
                    VE.AddVertex(Fortune.VVInfinite);
                    if (Math.Abs(VE.LeftData[1] - VE.RightData[1]) < 1e-10 && VE.LeftData[0] < VE.RightData[0])
                    {
                        Vector T = VE.LeftData;
                        VE.LeftData = VE.RightData;
                        VE.RightData = T;
                    }
                }
            }

            ArrayList MinuteEdges = new ArrayList();
            foreach (VoronoiEdge VE in VG.Edges)
            {
                if (!VE.IsPartlyInfinite && VE.VVertexA.Equals(VE.VVertexB))
                {
                    MinuteEdges.Add(VE);
                    foreach (VoronoiEdge VE2 in VG.Edges)
                    {
                        if (VE2.VVertexA.Equals(VE.VVertexA))
                            VE2.VVertexA = VE.VVertexA;
                        if (VE2.VVertexB.Equals(VE.VVertexA))
                            VE2.VVertexB = VE.VVertexA;
                    }
                }
            }
            foreach (VoronoiEdge VE in MinuteEdges)
                VG.Edges.Remove(VE);

            return VG;
        }
Exemple #11
0
 public Scanline(IEnumerable <TEvent> events, Func <TEvent, TScan> fnScanItem, Func <TScan, TScan, int> compare = null)
 {
     _pq     = new BinaryPriorityQueue <TScan>(compare);
     _events = events.Select(fnScanItem).GetEnumerator();
 }
        private void resolvePath(AStarComponent aStarComponent, AStarNode currentNode_in, AStarNode goalNode_in, AStarGraph graph_in)
        {
            BinaryPriorityQueue<AStarNode> openset =
                new BinaryPriorityQueue<AStarNode>(AStarNode.sortFScoreAscending());
            AStarNode currentNode = currentNode_in;
            AStarNode goalNode = goalNode_in;
            AStarGraph graph = graph_in;

            if (currentNode != null)
            {
                aStarComponent.ClearPath();

                for (int i = 0; i < graph.Nodes.Count; i++)
                {
                    graph.Nodes[i].Initialize();
                }

                openset.Push(currentNode);
                currentNode.FScore = this.getFScore(currentNode, goalNode);

                while (openset.Count > 0)
                {
                    AStarNode current;

                    current = openset.Pop();

                    List<AStarNode> neighbors = current.GetNeighbors();

                    if (current.GetPosition() == goalNode.GetPosition())
                    {
                        AStarNode node = current;

                        while (node.GetOptimalPathParent() != null)
                        {
                            aStarComponent.Path.Push(node);
                            node = node.GetOptimalPathParent();
                        }

                        //aStarComponent.Path.Push(node);

                        break;
                    }

                    current.Processed = true;

                    for (int i = 0; i < neighbors.Count; i++)
                    {
                        AStarNode neighbor = neighbors[i];

                        if (!neighbor.Processed)
                        {
                            float tentative_g_score =
                                current.GScore + (current.GetPosition() - neighbor.GetPosition()).Length();

                            if (!neighbor.Visited || tentative_g_score <= neighbor.GScore)
                            {
                                neighbor.SetOptimalPathParent(current);
                                neighbor.GScore = tentative_g_score;
                                neighbor.FScore = neighbor.GScore + this.getFScore(neighbor, goalNode);

                                if (!neighbor.Visited)
                                {
                                    neighbor.Visited = true;
                                }
                                openset.Push(neighbor);
                            }
                        }
                    }
                }
            }
        }
Exemple #13
0
        public static VoronoiGraph ComputeVoronoiGraph(IEnumerable Datapoints)
        {
            BinaryPriorityQueue PQ             = new BinaryPriorityQueue();
            Hashtable           CurrentCircles = new Hashtable();
            VoronoiGraph        VG             = new VoronoiGraph();
            VNode RootNode = null;

            foreach (Vector V in Datapoints)
            {
                PQ.Push(new VDataEvent(V));
            }
            while (PQ.Count > 0)
            {
                VEvent      VE = PQ.Pop() as VEvent;
                VDataNode[] CircleCheckList;
                if (VE is VDataEvent)
                {
                    RootNode = VNode.ProcessDataEvent(VE as VDataEvent, RootNode, VG, VE.Y, out CircleCheckList);
                }
                else if (VE is VCircleEvent)
                {
                    CurrentCircles.Remove(((VCircleEvent)VE).NodeN);
                    if (!((VCircleEvent)VE).Valid)
                    {
                        continue;
                    }
                    RootNode = VNode.ProcessCircleEvent(VE as VCircleEvent, RootNode, VG, VE.Y, out CircleCheckList);
                }
                else
                {
                    throw new Exception("Got event of type " + VE.GetType().ToString() + "!");
                }
                foreach (VDataNode VD in CircleCheckList)
                {
                    if (CurrentCircles.ContainsKey(VD))
                    {
                        ((VCircleEvent)CurrentCircles[VD]).Valid = false;
                        CurrentCircles.Remove(VD);
                    }
                    VCircleEvent VCE = VNode.CircleCheckDataNode(VD, VE.Y);
                    if (VCE != null)
                    {
                        PQ.Push(VCE);
                        CurrentCircles[VD] = VCE;
                    }
                }
                if (VE is VDataEvent)
                {
                    Vector DP = ((VDataEvent)VE).DataPoint;
                    foreach (VCircleEvent VCE in CurrentCircles.Values)
                    {
                        if (MathTools.Dist(DP[0], DP[1], VCE.Center[0], VCE.Center[1]) < VCE.Y - VCE.Center[1] && Math.Abs(MathTools.Dist(DP[0], DP[1], VCE.Center[0], VCE.Center[1]) - (VCE.Y - VCE.Center[1])) > 1e-10)
                        {
                            VCE.Valid = false;
                        }
                    }
                }
            }

            VG.Regions = new Dictionary <Vector, VoronoiRegion>();

            foreach (VoronoiEdge e in VG.Edges)
            {
                if (e.VVertexA == VVUnkown || e.VVertexB == VVUnkown)
                {
                    continue;
                }

                for (int i = 0; i < 2; ++i)
                {
                    VoronoiRegion r;
                    Vector        key   = i == 0 ? e.LeftData : e.RightData;
                    bool          exist = VG.Regions.TryGetValue(key, out r);

                    if (exist)
                    {
                    }
                    else
                    {
                        r = new VoronoiRegion();
                    }
                    if (r.Coners.IndexOf(e.VVertexA) == -1)
                    {
                        r.Coners.Add(e.VVertexA);
                    }
                    if (r.Coners.IndexOf(e.VVertexB) == -1)
                    {
                        r.Coners.Add(e.VVertexB);
                    }

                    r.Edges.Add(e);

                    if (exist)
                    {
                    }
                    else
                    {
                        VG.Regions.Add(key, r);
                    }
                }
            }

            return(VG);
        }
Exemple #14
0
 public VoronoiMapper(IEnumerable <Vector> datapoints)
 {
     Datapoints = datapoints;
     DataPq     = new BinaryPriorityQueue();
 }