Exemplo n.º 1
0
        /// <summary>
        /// Expand the bounding box to include the given bounding sphere
        /// </summary>
        /// <param name="sh"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void ExpandBy(IBoundingSphere sh)
        {
            if (!sh.Valid())
            {
                return;
            }

            if (sh.Center.X - sh.Radius < _min.X)
            {
                _min.X = sh.Center.X - sh.Radius;
            }
            if (sh.Center.X + sh.Radius > _max.X)
            {
                _max.X = sh.Center.X + sh.Radius;
            }

            if (sh.Center.Y - sh.Radius < _min.Y)
            {
                _min.Y = sh.Center.Y - sh.Radius;
            }
            if (sh.Center.Y + sh.Radius > _max.Y)
            {
                _max.Y = sh.Center.Y + sh.Radius;
            }

            if (sh.Center.Z - sh.Radius < _min.Z)
            {
                _min.Z = sh.Center.Z - sh.Radius;
            }
            if (sh.Center.Z + sh.Radius > _max.Z)
            {
                _max.Z = sh.Center.Z + sh.Radius;
            }
        }
        private bool Intersects(IBoundingSphere bs)
        {
            if (!bs.Valid())
            {
                return(true);
            }

            var startToCenter = Start - bs.Center;

            double c = startToCenter.LengthSquared() - System.Math.Pow(bs.Radius, 2);

            if (c < 0.0)
            {
                return(true);
            }

            var    startToEnd = End - Start;
            double a          = startToEnd.LengthSquared();

            double b = Vector3.Dot(startToCenter, startToEnd) * 2.0;

            double d = b * b - 4.0 * a * c;

            if (d < 0.0)
            {
                return(false);
            }

            d = System.Math.Sqrt(d);

            double div = 1.0 / (2.0 * a);

            double r1 = (-b - d) * div;
            double r2 = (-b + d) * div;

            if (r1 <= 0.0 && r2 <= 0.0)
            {
                return(false);
            }

            if (r1 >= 1.0 && r2 >= 1.0)
            {
                return(false);
            }

            if (IntersectionLimit == IntersectionLimitModes.LimitNearest && 0 != Intersections.Count())
            {
                if (startToCenter.Length() > Intersections.First().StartToIntersectionDist)
                {
                    return(false);
                }

                //double ratio = (startToCenter.Length() - bs.Radius) / System.Math.Sqrt(a);

                //if (ratio >= Intersections.First().Ratio) return false;
            }

            // passed all the rejection tests so line must intersect bounding sphere, return true.
            return(true);
        }