예제 #1
0
        public static List <CorpBox> FilterBoxes(this List <CorpBox> corpBoxes)
        {
            var sw = new Stopwatch();

            sw.Start();
            var quadTreeRect = new QuadTreeRect <CorpBox>();
            // Copies for ease of use/readability
            var resultsList = new List <CorpBox>(corpBoxes);

            resultsList = resultsList.Where(x => x.Rank >= 0.5).OrderByDescending(x => x.Rank).ToList();
            quadTreeRect.AddRange(resultsList);
            sw.Stop();
            Console.WriteLine($"QuadTree gen took: {sw.ElapsedMilliseconds}ms");
            var results       = new List <CorpBox>(resultsList);
            var boxesToRemove = new List <CorpBox>();

            // Real bad performance but somewhat straight forward.
            foreach (var corpBox in resultsList)
            {
                // Already removed
                if (boxesToRemove.Contains(corpBox))
                {
                    continue;
                }
                var rectIntersectsWith = quadTreeRect.GetObjects(corpBox.Rect);

                // No intersections, keep
                if (rectIntersectsWith.Count == 0)
                {
                    continue;
                }

                foreach (var box in rectIntersectsWith)
                {
                    // Same instance
                    if (box == corpBox)
                    {
                        continue;
                    }
                    var jIndex = corpBox.CalculateJaccardIndex(box);
                    if (jIndex > 0.4 && !boxesToRemove.Contains(box))
                    {
                        boxesToRemove.Add(box);
                        quadTreeRect.Remove(box);
                    }
                }
            }

            // Remove all boxes to remove, leaves final list of boxes.
            foreach (var corpBox in boxesToRemove)
            {
                results.Remove(corpBox);
            }

            return(results);
        }
예제 #2
0
            public QuadTree(int treeDepth, QuadTreeRect treeRect)
            {
                maxDepth  = 5;
                maxAmount = 4;

                chiledShapes = new List <ShapeObject>();

                TreeDepth = treeDepth;
                TreeRect  = treeRect;
            }
예제 #3
0
        public EntityManager()
        {
            this.entitiesArea = new QuadTreeRect <IEntity>();

            this.entitiesToChunks = new Dictionary <IEntity, ILandChunk>();

            this.entitiesToBooking = new Dictionary <IEntity, BookingEntity>();

            this.area = new IntRect();
        }
예제 #4
0
            void CreateChiledTree()
            {
                chiledTrees = new QuadTree[4];

                int   chiledDepth = TreeDepth + 1;
                float chiledSize  = TreeRect.Width / 2;

                QuadTreeRect chiledRect = new QuadTreeRect(TreeRect.X, TreeRect.Y, chiledSize, chiledSize);

                chiledTrees[0] = new QuadTree(chiledDepth, chiledRect);

                chiledRect     = new QuadTreeRect(TreeRect.X + chiledSize, TreeRect.Y, chiledSize, chiledSize);
                chiledTrees[1] = new QuadTree(chiledDepth, chiledRect);

                chiledRect     = new QuadTreeRect(TreeRect.X, TreeRect.Y + chiledSize, chiledSize, chiledSize);
                chiledTrees[2] = new QuadTree(chiledDepth, chiledRect);

                chiledRect     = new QuadTreeRect(TreeRect.X + chiledSize, TreeRect.Y + chiledSize, chiledSize, chiledSize);
                chiledTrees[3] = new QuadTree(chiledDepth, chiledRect);
            }
예제 #5
0
            public List <ShapeObject> TakeShapesOfLine(Vector3 origin, Vector3 dirction)
            {
                List <ShapeObject> takes = new List <ShapeObject>();

                if (!QuadTreeRect.LineOverlap(TreeRect, origin, dirction))
                {
                    return(takes);
                }

                takes.AddRange(chiledShapes);

                if (chiledTrees == null)
                {
                    return(takes);
                }

                for (int i = 0; i < chiledTrees.Length; i++)
                {
                    takes.AddRange(chiledTrees[i].TakeShapesOfLine(origin, dirction));
                }

                return(takes);
            }
예제 #6
0
            //Take
            public List <ShapeObject> TakeShapesInRect(QuadTreeRect rect)
            {
                List <ShapeObject> takes = new List <ShapeObject>();

                if (!QuadTreeRect.RectOverlap(TreeRect, rect))
                {
                    return(takes);
                }

                takes.AddRange(chiledShapes);

                if (chiledTrees == null)
                {
                    return(takes);
                }

                for (int i = 0; i < chiledTrees.Length; i++)
                {
                    takes.AddRange(chiledTrees[i].TakeShapesInRect(rect));
                }

                return(takes);
            }
예제 #7
0
 public bool IsShapeOverlapTreeRect(ShapeObject shape)
 {
     return(QuadTreeRect.RectOverlap(TreeRect, shape.ShapeToRect));
 }
예제 #8
0
        public void UpdateViewports()
        {
            Stopwatch st = new Stopwatch();
            // st.Start();
            int count = 0;


            QuadTree <Player> quadTree = new QuadTree <Player>(Sector.mapSize, Sector.mapSize, new PlayerBounds(), 10, 4);

            foreach (Player p in players)
            {
                quadTree.Insert(p);
            }

            int c2 = 0;

            st.Start();

            foreach (Player player in players)
            {
                QuadTreeRect searchArea     = new QuadTreeRect(player.xpos - Sector.CheckRadius, player.ypos - Sector.CheckRadius, Sector.CheckRadius * 2, Sector.CheckRadius * 2);
                Player[]     playerInSearch = quadTree.FindObjects(searchArea);

                //times.Add((int)st.ElapsedMilliseconds);

                foreach (Player checkPlayer in playerInSearch)
                {
                    if (player == checkPlayer)
                    {
                        c2++;
                        continue;
                    }

                    if (InRadius(player, checkPlayer))
                    {
                        player.AddToViewport(checkPlayer);
                        //checkPlayer.AddToViewport(player);
                    }
                    else
                    {
                        //checkPlayer.RemoveFromViewport(player);
                        //player.RemoveFromViewport(checkPlayer);
                    }

                    count++;
                }
            }



            st.Stop();

            int totalPlayersInAllViews = 0;
            int max = 0;
            int min = int.MaxValue;

            foreach (Player p in players)
            {
                int piv = p.PlayersInViewCount();
                totalPlayersInAllViews += piv;
                if (piv > max)
                {
                    max = piv;
                }
                if (piv < min)
                {
                    min = piv;
                }
            }
            float average = (float)totalPlayersInAllViews / players.Length;

            Console.WriteLine(string.Format("QuadSectoring: Time {0}ms, Avg Viewport Size {1}, Max {2}. Min {3}, Check {4}, c2 {5}", st.ElapsedMilliseconds, average, max, min, count, c2));
            //foreach(int i in times)
            //{
            //    Console.WriteLine(i);

            //}

            runningCount += count;
            runningTime  += st.ElapsedMilliseconds;
            iterations   += 1;
            runningISize += average;
        }
예제 #9
0
        public void UpdateViewportsAlternative()
        {
            int   size   = 1000;
            float radius = 25.0f;
            QuadTree <Vector2> quadTree    = new QuadTree <Vector2>(size, size, new MyCustomBounds());
            Random             random      = new Random();
            List <Vector2>     myPositions = new List <Vector2>();

            for (int ii = 0; ii < 1000; ii++)
            {
                myPositions.Add(new Vector2((float)(size * random.NextDouble()), (float)(size * random.NextDouble())));
            }


            Stopwatch st = new Stopwatch();

            st.Start();

            foreach (Vector2 position in myPositions)
            {
                quadTree.Insert(position);
            }

            st.Stop();

            Console.WriteLine("Quadtree Build Time: " + st.ElapsedMilliseconds);

            st.Restart();

            int   count1      = 0;
            float dcount1     = 0.0f;
            int   checkcount1 = 0;

            foreach (Vector2 p in myPositions)
            {
                // Vector2 p = myPositions[0];

                QuadTreeRect searchArea = new QuadTreeRect(p.X - radius, p.Y - radius, radius * 2, radius * 2);
                Vector2[]    positions  = quadTree.FindObjects(searchArea);

                foreach (Vector2 pos in positions)
                {
                    if (pos == p)
                    {
                        continue;
                    }
                    checkcount1++;

                    float distance = Vector2.Distance(p, pos);
                    if (distance < radius)
                    {
                        count1++;
                        dcount1 += distance;
                    }
                }
                //}
            }
            st.Stop();

            Console.WriteLine("With Quadtree:    " + st.ElapsedMilliseconds + "  Counted: " + count1 + " D: " + dcount1 + " Checks: " + checkcount1);

            st.Restart();
            int   count2      = 0;
            float dcount2     = 0.0f;
            int   checkcount2 = 0;

            foreach (Vector2 i in myPositions)
            {
                // Vector2 i = myPositions[0];

                foreach (Vector2 j in myPositions)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    checkcount2++;
                    float distance = Vector2.Distance(i, j);
                    if (distance < radius)
                    {
                        count2++;
                        dcount2 += distance;
                    }
                }
            }
            st.Stop();

            Console.WriteLine("Without Quadtree: " + st.ElapsedMilliseconds + "  Counted: " + count2 + " D: " + dcount2 + " Checks: " + checkcount2);


            //Stopwatch st = new Stopwatch();
            //st.Start();
            //int count = 0;


            //QuadTree<Vector2> quadTree = new QuadTree<Vector2>(Sector.mapSize, Sector.mapSize, new Vector2Bounds());

            //foreach (Player p in players)
            //{
            //    quadTree.Insert(new Vector2(p.xpos,p.ypos));
            //}

            //int c2 = 0;
            //foreach (Player player in players)
            //{
            //    QuadTreeRect searchArea = new QuadTreeRect(player.xpos, player.ypos, Sector.CheckRadius / 6, Sector.CheckRadius / 6);
            //    Vector2[] playerInSearch = quadTree.FindObjects(searchArea);

            //    //times.Add((int)st.ElapsedMilliseconds);

            //    //foreach (Player checkPlayer in playerInSearch)
            //    //{

            //    //    if (player == checkPlayer)
            //    //    {
            //    //        c2++;
            //    //        continue;
            //    //    }

            //    //    if (InRadius(player, checkPlayer))
            //    //    {
            //    //        player.AddToViewport(checkPlayer);
            //    //        checkPlayer.AddToViewport(player);
            //    //    }
            //    //    else
            //    //    {
            //    //        checkPlayer.RemoveFromViewport(player);
            //    //        player.RemoveFromViewport(checkPlayer);
            //    //    }

            //    //    count++;


            //    //}

            //}



            //st.Stop();

            //int totalPlayersInAllViews = 0;
            //int max = 0;
            //int min = int.MaxValue;
            //foreach (Player p in players)
            //{
            //    int piv = p.PlayersInViewCount();
            //    totalPlayersInAllViews += piv;
            //    if (piv > max) max = piv;
            //    if (piv < min) min = piv;

            //}
            //float average = (float)totalPlayersInAllViews / players.Length;
            //Console.WriteLine(string.Format("QuadSectoring: Time {0}ms, Avg Viewport Size {1}, Max {2}. Min {3}, Check {4}, c2 {5}", st.ElapsedMilliseconds, average, max, min, count, c2));
            ////foreach(int i in times)
            ////{
            ////    Console.WriteLine(i);

            ////}
        }