Exemplo n.º 1
0
 public void TestInit()
 {
     _tree = new DynamicQuadTree(new BoundingBox(new Vector3(0, 0, float.MinValue),
                                                new Vector3(100000, 100000, float.MaxValue)));
     _r = new Random();
     _mockEntities = new List<IWorldEntity>();
 }
Exemplo n.º 2
0
 private static int CheckChildrenEntityCount(DynamicQuadTree node)
 {
     if (!node.IsLeaf)
     {
         var reportedAmount = node.NumEntities;
         var calculatedAmount = node.Children.Sum(c => CheckChildrenEntityCount(c));
         Assert.AreEqual(reportedAmount, calculatedAmount);
         return calculatedAmount;
     }
     return node.NumEntities;
 }
Exemplo n.º 3
0
        // Returns child node that should contain entity,
        // BASED ON POSITION ONLY
        private DynamicQuadTree GetChildContaining(IWorldEntity e)
        {
            Contract.Requires(e != null);
            Contract.Ensures(Contract.Result <DynamicQuadTree>() != null);

            DynamicQuadTree node = null;

            foreach (var child in childNodes)
            {
                if (child.Boundaries.Contains(e.Position).Equals(ContainmentType.Contains))
                {
                    node = child;
                }
            }
            return(node);
        }
Exemplo n.º 4
0
        // Returns child node that should contain entity,
        // BASED ON POSITION ONLY
        private DynamicQuadTree GetChildContaining(IWorldEntity e)
        {
            Contract.Requires(e != null);
            Contract.Ensures(Contract.Result <DynamicQuadTree>() != null);

            DynamicQuadTree node = null;

            foreach (var child in _childNodes)
            {
// ReSharper disable PossibleNullReferenceException
                if (child.Boundaries.Contains(e.Position).Equals(ContainmentType.Contains))
                {
// ReSharper restore PossibleNullReferenceException
                    node = child;
                }
            }
            return(node);
        }
Exemplo n.º 5
0
 public void TestCleanup()
 {
     _tree = null;
     _r = null;
     _mockEntities = null;
 }
Exemplo n.º 6
0
        private void Partition()
        {
            Contract.Requires(_isLeaf);

            /*
             *
             * Min.X  HalfX Max.X
             * +------------+ Max.Y
             * |      |     |
             * |      |     |
             * |------------| HalfY
             * |      |     |
             * |      |     |
             * +------------+ Min.Y
             *
             * Since we are using 3d bounding boxes,
             * but Quadtree itself is 2d, let's just
             * use float.MinValue and float.MaxValue
             * so our Contains() check are true for
             * whatever height entity is in.
             *
             * My mother always told me i can't be an artist,
             * i think she was right.
             */

            var max = Boundaries.Max;
            var min = Boundaries.Min;
            float halfX = Boundaries.Min.X + Length / 2;
            float halfY = Boundaries.Min.Y + Width / 2;

            _childNodes = new DynamicQuadTree[ChildrenSize];

            _childNodes[NorthEast] = new DynamicQuadTree(new BoundingBox(new Vector3(halfX,halfY,float.MinValue),
                                                                         new Vector3(max.X,max.Y,float.MaxValue)));

            _childNodes[SouthEast] = new DynamicQuadTree(new BoundingBox(new Vector3(halfX, min.Y, float.MinValue),
                                                                         new Vector3(max.X, halfY, float.MaxValue)));

            _childNodes[SouthWest] = new DynamicQuadTree(new BoundingBox(new Vector3(min.X, min.Y, float.MinValue),
                                                                         new Vector3(halfX, halfY, float.MaxValue)));

            _childNodes[NorthWest] = new DynamicQuadTree(new BoundingBox(new Vector3(min.X, halfY, float.MinValue),
                                                                         new Vector3(halfX, max.Y, float.MaxValue)));
            foreach (var n in _childNodes)
                n.Parent = this;

            _isLeaf = false;

            foreach (var e in _bucket)
                AddEntity(e);

            _bucket = null;

            _numEntities = 0;
            foreach (var c in _childNodes)
                _numEntities += c.NumEntities;
        }
Exemplo n.º 7
0
        private void Partition()
        {
            Contract.Requires(isLeaf);

            /*
             *
             * Min.X  HalfX Max.X
             * +------------+ Max.Y
             * |      |     |
             * |      |     |
             * |------------| HalfY
             * |      |     |
             * |      |     |
             * +------------+ Min.Y
             *
             * Since we are using 3d bounding boxes,
             * but Quadtree itself is 2d, let's just
             * use float.MinValue and float.MaxValue
             * so our Contains() check are true for
             * whatever height entity is in.
             *
             * My mother always told me i can't be an artist,
             * i think she was right.
             */

            var   Max   = Boundaries.Max;
            var   Min   = Boundaries.Min;
            float HalfX = Boundaries.Min.X + Length / 2;
            float HalfY = Boundaries.Min.Y + Width / 2;

            childNodes = new DynamicQuadTree[CHILDREN_SIZE];

            childNodes[NORTH_EAST] = new DynamicQuadTree(new BoundingBox(new Vector3(HalfX, HalfY, float.MinValue),
                                                                         new Vector3(Max.X, Max.Y, float.MaxValue)));

            childNodes[SOUTH_EAST] = new DynamicQuadTree(new BoundingBox(new Vector3(HalfX, Min.Y, float.MinValue),
                                                                         new Vector3(Max.X, HalfY, float.MaxValue)));

            childNodes[SOUTH_WEST] = new DynamicQuadTree(new BoundingBox(new Vector3(Min.X, Min.Y, float.MinValue),
                                                                         new Vector3(HalfX, HalfY, float.MaxValue)));

            childNodes[NORTH_WEST] = new DynamicQuadTree(new BoundingBox(new Vector3(Min.X, HalfY, float.MinValue),
                                                                         new Vector3(HalfX, Max.Y, float.MaxValue)));
            foreach (var n in childNodes)
            {
                n.parent = this;
            }

            isLeaf = false;

            foreach (var e in bucket)
            {
                AddEntity(e);
            }

            bucket = null;

            numEntities = 0;
            foreach (var c in childNodes)
            {
                numEntities += c.NumEntities;
            }
        }
Exemplo n.º 8
0
        private void Partition()
        {
            Contract.Requires(_isLeaf);

            /*
             *
             * Min.X  HalfX Max.X
             * +------------+ Max.Y
             * |      |     |
             * |      |     |
             * |------------| HalfY
             * |      |     |
             * |      |     |
             * +------------+ Min.Y
             *
             * Since we are using 3d bounding boxes,
             * but Quadtree itself is 2d, let's just
             * use float.MinValue and float.MaxValue
             * so our Contains() check are true for
             * whatever height entity is in.
             *
             * My mother always told me i can't be an artist,
             * i think she was right.
             */

            var   max   = Boundaries.Max;
            var   min   = Boundaries.Min;
            float halfX = Boundaries.Min.X + Length / 2;
            float halfY = Boundaries.Min.Y + Width / 2;

            _childNodes = new DynamicQuadTree[ChildrenSize];

            _childNodes[NorthEast] = new DynamicQuadTree(new BoundingBox(new Vector3(halfX, halfY, float.MinValue),
                                                                         new Vector3(max.X, max.Y, float.MaxValue)));

            _childNodes[SouthEast] = new DynamicQuadTree(new BoundingBox(new Vector3(halfX, min.Y, float.MinValue),
                                                                         new Vector3(max.X, halfY, float.MaxValue)));

            _childNodes[SouthWest] = new DynamicQuadTree(new BoundingBox(new Vector3(min.X, min.Y, float.MinValue),
                                                                         new Vector3(halfX, halfY, float.MaxValue)));

            _childNodes[NorthWest] = new DynamicQuadTree(new BoundingBox(new Vector3(min.X, halfY, float.MinValue),
                                                                         new Vector3(halfX, max.Y, float.MaxValue)));
            foreach (var n in _childNodes)
            {
                n.Parent = this;
            }

            _isLeaf = false;

            foreach (var e in _bucket)
            {
                AddEntity(e);
            }

            _bucket = null;

            _numEntities = 0;
            foreach (var c in _childNodes)
            {
                _numEntities += c.NumEntities;
            }
        }