Пример #1
0
        /// <summary>
        /// Check if the specified bounds intersect with anything in the tree. See also: GetColliding.
        /// </summary>
        /// <param name="checkBounds">Bounds to check.</param>
        /// <returns>True if there was a collision.</returns>
        public bool IsColliding(ref Line checkBounds, Filter <T> flt)
        {
            // Are the input bounds at least partially in this node?
            if (!bounds.IntersectRay(checkBounds))
            {
                return(false);
            }


            // Check against any objects in this node
            for (int i = 0; i < objects.Count; i++)
            {
                var obj = objects[i];
                if ((flt == null || flt.Invoke(obj.Obj)) && obj.Bounds.IntersectRay(checkBounds))
                {
                    return(true);
                }
            }


            // Check children
            if (children != null)
            {
                for (int i = 0; i < 8; i++)
                {
                    if (children[i].IsColliding(ref checkBounds, flt))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Пример #2
0
        /// <summary>
        /// Return objects that are within maxDistance of the specified ray.
        /// </summary>
        /// <param name="ray">The ray.</param>
        /// <param name="maxDistance">Maximum distance from the ray to consider.</param>
        /// <param name="result">List result.</param>
        /// <returns>Objects within range.</returns>
        public void GetNearby(ref Line ray, ref float maxDistance, List <T> result)
        {
            // Does the ray hit this node at all?
            // Note: Expanding the bounds is not exactly the same as a real distance check, but it's fast.
            // TODO: Does someone have a fast AND accurate formula to do this check?
            //bounds.ExpandToContain(new XYZ(maxDistance * 2, maxDistance * 2, maxDistance * 2));

            HLBoundingBoxXYZ d = new HLBoundingBoxXYZ(bounds.MidPoint, bounds.Size);

            d.ExpandToContain(new XYZ(bounds.Max.X + maxDistance, bounds.Max.Y + maxDistance, bounds.Max.Z + maxDistance));
            d.ExpandToContain(new XYZ(bounds.Min.X - maxDistance, bounds.Min.Y - maxDistance, bounds.Min.Z - maxDistance));

            bool intersected = d.IntersectRay(ray);

            d.Size = actualBoundsSize;
            if (!intersected)
            {
                return;
            }
            // Check against any objects in this node
            for (int i = 0; i < objects.Count; i++)
            {
                if (DistanceToRay(ray, objects[i].Pos) <= maxDistance)
                {
                    result.Add(objects[i].Obj);
                }
            }

            // Check children
            if (children != null)
            {
                for (int i = 0; i < 8; i++)
                {
                    children[i].GetNearby(ref ray, ref maxDistance, result);
                }
            }
        }