Example #1
0
        public override void ForEachWithin(IntRectangle rectangle, EnumerateItemDelegate <T> func)
        {
            rectangle = IntRectangle.FromPoints(
                IntVectorMath.MinMax(rectangle.LeftTopFront - Offset, IntVector.Zero, SizeVector),
                IntVectorMath.MinMax(rectangle.RightBottomBack - Offset, IntVector.Zero, SizeVector)
                );

            base.ForEachWithin(rectangle, (block, coords) => func(block, coords + Offset));
        }
Example #2
0
 public void ForEachWithin(IntRectangle rectangle, EnumerateItemDelegate <T> func)
 {
     for (var x = rectangle.Left; x < rectangle.Right; x++)
     {
         for (var y = rectangle.Top; y < rectangle.Bottom; y++)
         {
             for (var z = rectangle.Front; z < rectangle.Back; z++)
             {
                 var item = _items[x, y, z];
                 if (item != null)
                 {
                     func(item, new IntVector(x, y, z));
                 }
             }
         }
     }
 }
Example #3
0
        public void Test1()
        {
            var coordinateSystem = CoordinateSystem.CreateHorizontalCircular(0, 10);
            var grid             = new CircularBinaryGrid <string>(coordinateSystem);
            var rect             = new IntRectangle(0, 0, 0, 2, 2, 1);
            var calls            = new Dictionary <IntVector, string>();
            EnumerateItemDelegate <string> func = (item, position) => calls.Add(position, item);

            AddItems(grid, new IntRectangle(-1, -1, 0, 4, 4, 1));

            grid.ForEachWithin(rect, func);

            Assert.AreEqual(4, calls.Count);
            AssertItem(calls, new IntVector(0, 0, 0));
            AssertItem(calls, new IntVector(1, 0, 0));
            AssertItem(calls, new IntVector(0, 1, 0));
            AssertItem(calls, new IntVector(1, 1, 0));
        }
Example #4
0
        public void TestEnumerateAll()
        {
            var coordinateSystem = CoordinateSystem.CreateHorizontalCircular(0, 16);
            var grid             = new CircularBinaryGrid <string>(coordinateSystem);
            var calls            = new Dictionary <IntVector, string>();
            EnumerateItemDelegate <string> func = (item, position) =>
            {
                calls.Add(position, item);
            };

            AddItems(grid, new IntRectangle(0, 0, 0, 16, 16, 1));

            grid.ForEach(func);

            for (var x = 0; x < 16; x++)
            {
                for (var y = 0; y < 16; y++)
                {
                    AssertItem(calls, new IntVector(x, y, 0));
                }
            }
        }
 public void ForEach(EnumerateItemDelegate <T> func)
 {
     Grid.ForEach(func);
 }
Example #6
0
 public void ForEach(EnumerateItemDelegate <T> func)
 {
     base.ForEachWithin(new IntRectangle(0, 0, 0, Size, Size, Size), (block, coords) => func(block, coords + Offset));
 }
        public virtual void ForEachWithin(IntRectangle rectangle, EnumerateItemDelegate <T> func)
        {
            var halfSize = HalfSize;
            var minX     = rectangle.Left;
            var maxX     = rectangle.Right;
            var minY     = rectangle.Top;
            var maxY     = rectangle.Bottom;
            var minZ     = rectangle.Front;
            var maxZ     = rectangle.Back;

            var children = Children;

            // 0|0|0
            if (children[0] != null && minX < halfSize && minY < halfSize)
            {
                children[0].ForEachWithin(
                    IntRectangle.FromLeftTopFrontRightBottomBack(
                        minX, minY, minZ,
                        Math.Min(halfSize, maxX), Math.Min(halfSize, maxY), Math.Min(halfSize, maxZ)),
                    (block, coords) => func(block, coords)
                    );
            }

            // 1|0|0
            if (children[1] != null && minY < halfSize && maxX >= halfSize)
            {
                children[1].ForEachWithin(
                    IntRectangle.FromLeftTopFrontRightBottomBack(
                        Math.Max(0, minX - halfSize), minY, minZ,
                        maxX - halfSize, Math.Min(halfSize, maxY), Math.Min(halfSize, maxZ)),
                    (block, coords) => func(block, coords + IntVector.Right * halfSize)
                    );
            }

            // 0|1|0
            if (children[2] != null && minX < halfSize && maxY >= halfSize)
            {
                children[2].ForEachWithin(
                    IntRectangle.FromLeftTopFrontRightBottomBack(
                        minX, Math.Max(0, minY - halfSize), minZ,
                        Math.Min(halfSize, maxX), maxY - halfSize, Math.Min(halfSize, maxZ)),
                    (block, coords) => func(block, coords + IntVector.Down * halfSize)
                    );
            }

            // 1|1|0
            if (children[3] != null && maxX >= halfSize && maxY >= halfSize)
            {
                children[3].ForEachWithin(
                    IntRectangle.FromLeftTopFrontRightBottomBack(
                        Math.Max(0, minX - halfSize), Math.Max(0, minY - halfSize), minZ,
                        maxX - halfSize, maxY - halfSize, Math.Min(halfSize, maxZ)),
                    (block, coords) => func(block, coords + IntVector.RightDown * halfSize)
                    );
            }

            // 0|0|1
            if (children[4] != null && minX < halfSize && minY < halfSize)
            {
                children[4].ForEachWithin(
                    IntRectangle.FromLeftTopFrontRightBottomBack(
                        minX, minY, Math.Max(0, minZ - halfSize),
                        Math.Min(halfSize, maxX), Math.Min(halfSize, maxY), maxZ - halfSize),
                    (block, coords) => func(block, coords + IntVector.Back * halfSize)
                    );
            }

            // 1|0|1
            if (children[5] != null && minY < halfSize && maxX >= halfSize)
            {
                children[5].ForEachWithin(
                    IntRectangle.FromLeftTopFrontRightBottomBack(
                        Math.Max(0, minX - halfSize), minY, Math.Max(0, minZ - halfSize),
                        maxX - halfSize, Math.Min(halfSize, maxY), maxZ - halfSize),
                    (block, coords) => func(block, coords + IntVector.RightBack * halfSize)
                    );
            }

            // 0|1|1
            if (children[6] != null && minX < halfSize && maxY >= halfSize)
            {
                children[6].ForEachWithin(
                    IntRectangle.FromLeftTopFrontRightBottomBack(
                        minX, Math.Max(0, minY - halfSize), Math.Max(0, minZ - halfSize),
                        Math.Min(halfSize, maxX), maxY - halfSize, maxZ - halfSize),
                    (block, coords) => func(block, coords + IntVector.DownBack * halfSize)
                    );
            }

            // 1|1|1
            if (children[7] != null && maxX >= halfSize && maxY >= halfSize)
            {
                children[7].ForEachWithin(
                    IntRectangle.FromLeftTopFrontRightBottomBack(
                        Math.Max(0, minX - halfSize), Math.Max(0, minY - halfSize), Math.Max(0, minZ - halfSize),
                        maxX - halfSize, maxY - halfSize, maxZ - halfSize),
                    (block, coords) => func(block, coords + HalfSizeVector)
                    );
            }
        }