Example #1
0
        private static void RunAssertion <L>(
            int numPoints,
            int numMoves,
            int queryFrameSize,
            SupportQuadTreeConfig config,
            SupportQuadTreeToolNonUnique <L> tools)
        {
            var random   = new Random();
            L   quadTree = tools.factory.Invoke(config);
            var points   = tools.generator.Generate(random, numPoints, config.X, config.Y, config.Width, config.Height);

            foreach (var point in points)
            {
                tools.adderNonUnique.Invoke(quadTree, point);
            }

            for (var i = 0; i < numMoves; i++)
            {
                SupportRectangleWithId moved = points[(random.Next(points.Count))];
                Move(moved, quadTree, random, config, tools);

                var    startX  = moved.X - queryFrameSize;
                var    startY  = moved.Y - queryFrameSize;
                double widthQ  = queryFrameSize * 2;
                double heightQ = queryFrameSize * 2;
                ICollection <object> values = tools.querier.Invoke(quadTree, startX, startY, widthQ, heightQ);
                AssertIds(points, values, startX, startY, widthQ, heightQ, tools.pointInsideChecking);
            }
        }
Example #2
0
        private static void RunAssertion <L>(
            int numPoints,
            int numMoves,
            int queryFrameSize,
            SupportQuadTreeConfig config,
            SupportQuadTreeToolUnique <L> tools,
            double rectangleWidth,
            double rectangleHeight)
        {
            var random   = new Random();
            var quadTree = tools.factory.Invoke(config);

            // generate
            var points = GenerateIntegerCoordinates(random, numPoints, config, rectangleWidth, rectangleHeight);

            // add
            foreach (var point in points.Values)
            {
                tools.adderUnique.Invoke(quadTree, point);
            }

            // move points
            for (var i = 0; i < numMoves; i++)
            {
                var p = MovePoint(points, quadTree, random, config, tools.adderUnique, tools.remover);

                var    qx      = p.X - queryFrameSize;
                var    qy      = p.Y - queryFrameSize;
                double qwidth  = queryFrameSize * 2;
                double qheight = queryFrameSize * 2;
                var    values  = tools.querier.Invoke(quadTree, qx, qy, qwidth, qheight);
                AssertIds(points.Values, values, qx, qy, qwidth, qheight, tools.pointInsideChecking);
            }
        }
Example #3
0
        private static IDictionary <XYPoint, SupportRectangleWithId> GenerateIntegerCoordinates(
            Random random,
            int numPoints,
            SupportQuadTreeConfig config,
            double rectangleWidth,
            double rectangleHeight)
        {
            IDictionary <XYPoint, SupportRectangleWithId> result = new Dictionary <XYPoint, SupportRectangleWithId>();
            var pointNum = 0;

            while (result.Count < numPoints)
            {
                var x = (int)config.X + random.Next((int)config.Width);
                var y = (int)config.Y + random.Next((int)config.Height);
                var p = new XYPoint(x, y);
                if (result.ContainsKey(p))
                {
                    continue;
                }

                result.Put(p, new SupportRectangleWithId("P" + pointNum, x, y, rectangleWidth, rectangleHeight));
                pointNum++;
            }

            return(result);
        }
Example #4
0
        private static XYPoint MovePoint <L>(
            IDictionary <XYPoint, SupportRectangleWithId> points,
            L quadTree,
            Random random,
            SupportQuadTreeConfig config,
            AdderUnique <L> adder,
            Remover <L> remover)
        {
            var     coordinates = points.Keys.ToArray();
            XYPoint oldCoordinate;
            XYPoint newCoordinate;

            while (true)
            {
                oldCoordinate = coordinates[random.Next(coordinates.Length)];
                var direction = random.Next(4);
                var newX      = oldCoordinate.X;
                var newY      = oldCoordinate.Y;
                if (direction == 0 && newX > config.X)
                {
                    newX--;
                }

                if (direction == 1 && newY > config.Y)
                {
                    newY--;
                }

                if (direction == 2 && newX < config.X + config.Width - 1)
                {
                    newX++;
                }

                if (direction == 3 && newY < config.Y + config.Height - 1)
                {
                    newY++;
                }

                newCoordinate = new XYPoint(newX, newY);
                if (!points.ContainsKey(newCoordinate))
                {
                    break;
                }
            }

            var moved = points.Delete(oldCoordinate);

            remover.Invoke(quadTree, moved);
            moved.X = newCoordinate.X;
            moved.Y = newCoordinate.Y;
            adder.Invoke(quadTree, moved);
            points.Put(newCoordinate, moved);

            // Comment-me-in:
            // log.info("Moving " + moved.getId() + " from " + printPoint(oldCoordinate.getX(), oldCoordinate.getY()) + " to " + printPoint(newCoordinate.getX(), newCoordinate.getY()));

            return(newCoordinate);
        }
Example #5
0
        private static void Move <L>(
            SupportRectangleWithId rectangle,
            L quadTree,
            Random random,
            SupportQuadTreeConfig config,
            SupportQuadTreeToolNonUnique <L> tools)
        {
            tools.remover.Invoke(quadTree, rectangle);

            double newX;
            double newY;

            while (true)
            {
                int direction = random.Next(4);
                newX = rectangle.X;
                newY = rectangle.Y;
                if (direction == 0)
                {
                    newX--;
                }
                else if (direction == 1)
                {
                    newY--;
                }
                else if (direction == 2)
                {
                    newX++;
                }
                else if (direction == 3)
                {
                    newY++;
                }

                if (tools.pointInsideChecking)
                {
                    if (BoundingBox.ContainsPoint(config.X, config.Y, config.Width, config.Height, newX, newY))
                    {
                        break;
                    }
                }
                else
                {
                    if (BoundingBox.IntersectsBoxIncludingEnd(config.X, config.Y, config.MaxX, config.MaxY, newX, newY, rectangle.W, rectangle.H))
                    {
                        break;
                    }
                }
            }

            // Comment-me-in:
            // log.info("Moving " + point.getId() + " from " + printPoint(point.getX(), point.getY()) + " to " + printPoint(newX, newY));

            rectangle.X = newX;
            rectangle.Y = newY;
            tools.adderNonUnique.Invoke(quadTree, rectangle);
        }
        private static void RunAssertion <L>(
            int numPoints,
            int numQueries,
            SupportQuadTreeConfig config,
            SupportQuadTreeToolUnique <L> tools)
        {
            L quadTree = tools.factory.Invoke(config);

            // generate
            var random     = new Random();
            var rectangles = tools.generator.Generate(random, numPoints, config.X, config.Y, config.Width, config.Height);

            // add
            foreach (var rectangle in rectangles)
            {
                tools.adderUnique.Invoke(quadTree, rectangle);
            }

            // query
            for (var i = 0; i < numQueries; i++)
            {
                SupportQuadTreeUtil.RandomQuery(
                    quadTree,
                    rectangles,
                    random,
                    config.X,
                    config.Y,
                    config.Width,
                    config.Height,
                    tools.querier,
                    tools.pointInsideChecking);
            }

            // remove point-by-point
            while (!rectangles.IsEmpty())
            {
                int removeIndex = random.Next(rectangles.Count);
                SupportRectangleWithId removed = rectangles.DeleteAt(removeIndex);
                tools.remover.Invoke(quadTree, removed);

                for (var i = 0; i < numQueries; i++)
                {
                    SupportQuadTreeUtil.RandomQuery(
                        quadTree,
                        rectangles,
                        random,
                        config.X,
                        config.Y,
                        config.Width,
                        config.Height,
                        tools.querier,
                        tools.pointInsideChecking);
                }
            }
        }
Example #7
0
        private static void RunAssertionPointsUnique <L>(
            int numPoints,
            SupportQuadTreeConfig config,
            SupportQuadTreeToolUnique <L> tools)
        {
            var random   = new Random();
            L   quadTree = tools.factory.Invoke(config);
            var points   = tools.generator.Generate(random, numPoints, config.X, config.Y, config.Width, config.Height);

            // add
            foreach (var p in points)
            {
                tools.adderUnique.Invoke(quadTree, p);
            }

            // find all individually
            foreach (var p in points)
            {
                ICollection <object> values = tools.querier.Invoke(quadTree, p.X, p.Y, 0.9, 0.9);
                Assert.IsTrue(values != null && !values.IsEmpty(), "Failed to find " + p);
                Assert.AreEqual(1, values.Count);
                Assert.AreEqual(p.Id, values.First());
            }

            // get all content
            ICollection <object> all = tools.querier.Invoke(quadTree, config.X, config.Y, config.Width, config.Height);

            Assert.AreEqual(points.Count, all.Count);
            Assert.AreEqual(points.Count, new HashSet <object>(all).Count);
            foreach (var value in all)
            {
                Assert.IsInstanceOf <string>(value);
            }

            // remove all
            foreach (var p in points)
            {
                tools.remover.Invoke(quadTree, p);
            }

            ICollection <object> valuesLater = tools.querier.Invoke(quadTree, config.X, config.Y, config.Width, config.Height);

            Assert.IsNull(valuesLater);
        }