コード例 #1
0
        private bool unsetInternal(ref Point2i point)
        {
            if (!aabb.Contains(point))
            {
                return(false);
            }

            if (Type == QuadType.White)
            {
                return(false);
            }

            if (depth < resolution)
            {
                if (Type != QuadType.Grey)
                {
                    subdivide();
                }
            }
            else
            {
                propagateEvent(EventType.Removing);

                this.value = null;

                return(true);
            }

            foreach (var quad in quads)
            {
                if (quad.unsetInternal(ref point))
                {
                    return(true);
                }
            }

            return(false);

            throw new InvalidOperationException("This is not supposed to happen!");
        }
コード例 #2
0
        private bool unsetAABBInternal(ref AABB2i aabb)
        {
            bool otherContains   = aabb.Contains(ref this.aabb);
            bool otherIntersects = aabb.Intersects(ref this.aabb);

            if (!otherContains && otherIntersects)
            {
                if (Type == QuadType.White)
                {
                    return(false);
                }

                bool subdivided = false;
                var  canSub     = depth < resolution;
                if (canSub && Type != QuadType.Grey)
                {
                    subdivide();
                    subdivided = true;
                }

                bool anyChild = false;
                if (canSub)
                {
                    foreach (var quad in quads)
                    {
                        anyChild |= quad.unsetAABBInternal(ref aabb);
                    }
                }

                return(anyChild || subdivided);
            }
            else if (otherContains)
            {
                return(unsetInternal());
            }
            else
            {
                return(false);
            }

            throw new InvalidOperationException("Set didn't fail nor succeed. This is not supposed to happen!");
        }
コード例 #3
0
        public bool UnsetCircle(Point2i point, int radius)
        {
            var rectSize = (int)(radius / Math.Sqrt(2));
            var testAABB = new AABB2i(
                point - new Point2i(rectSize - 1, rectSize - 1),
                point + new Point2i(rectSize - 1, rectSize - 1));

            bool anyOuterUnset = false;

            for (int i = point.X - radius; i <= point.X + radius; i++)
            {
                for (int j = point.Y - radius; j <= point.Y + radius; j++)
                {
                    var currentPoint = new Point2i(i, j);

                    if (testAABB.Contains(currentPoint))
                    {
                        continue;
                    }

                    if ((point - currentPoint).Length() <= radius)
                    {
                        anyOuterUnset |= unsetInternal(ref currentPoint);
                    }
                }
            }

            var anyAABBUnset = unsetAABBInternal(ref testAABB);

            if (anyOuterUnset || anyAABBUnset)
            {
                unsubdivide();
            }

            return(anyOuterUnset || anyAABBUnset);
        }
コード例 #4
0
        public bool SetCircle(Point2i point, int radius, T value)
        {
            var rectSize = (int)(radius / Math.Sqrt(2));
            var testAABB = new AABB2i(point - new Point2i(rectSize, rectSize), point + new Point2i(rectSize, rectSize));

            for (int i = point.X - radius; i <= point.X + radius; i++)
            {
                for (int j = point.Y - radius; j <= point.Y + radius; j++)
                {
                    var currentPoint = new Point2i(i, j);

                    if (testAABB.Contains(currentPoint))
                    {
                        continue;
                    }

                    if ((point - currentPoint).Length() < radius)
                    {
                        if (isPointOutside(currentPoint))
                        {
                            if (findRoot() == this && AutoExpand)
                            {
                                RegionQuadtree <T> newRoot = this;
                                if (newRoot.isPointOutside(currentPoint))
                                {
                                    newRoot = newRoot.ExpandFromCenter();
                                    point  += new Point2i(newRoot.aabb.Width / 4, newRoot.aabb.Height / 4);
                                }

                                return(newRoot.SetCircle(point, radius, value));
                            }
                        }
                    }
                }
            }

            bool anyOuterSet = false;

            for (int i = point.X - radius; i <= point.X + radius; i++)
            {
                for (int j = point.Y - radius; j <= point.Y + radius; j++)
                {
                    var currentPoint = new Point2i(i, j);

                    if (testAABB.Contains(currentPoint))
                    {
                        continue;
                    }

                    if ((point - currentPoint).Length() < radius)
                    {
                        anyOuterSet |= setInternal(ref currentPoint, ref value);
                    }
                }
            }

            var anyAABBSet = setAABBInternal(ref testAABB, ref value);

            if (anyOuterSet || anyAABBSet)
            {
                unsubdivide();
            }

            return(anyOuterSet || anyAABBSet);
        }