private double[] GetIntersectionsFromBook(Tuple4 origin, Tuple4 dir)
        {
            if (!Constants.EpsilonCompare(1.0, dir.Length()))
            {
                throw new ArgumentException("Direction should be normalized", nameof(dir));
            }

            var sphereToRay  = Tuple4.Subtract(origin, Tuple4.ZeroPoint);
            var a            = Tuple4.DotProduct(dir, dir);
            var b            = 2 * Tuple4.DotProduct(dir, sphereToRay);
            var c            = Tuple4.DotProduct(sphereToRay, sphereToRay) - 1.0;
            var discriminant = b * b - 4 * a * c;

            if (discriminant < 0.0)
            {
                return(null);
            }
            var discriminantSqrt = Math.Sqrt(discriminant);
            var t0 = (-b - discriminantSqrt) / (2 * a);
            var t1 = (-b + discriminantSqrt) / (2 * a);

            // Ray originates inside sphere
            // When t > 0 that is intersection in the direction of the ray
            // other intersection is in the opposite direction
            return(new double[] { t0, t1 });
        }
Exemple #2
0
        private double[] GetIntersections(Tuple4 origin, Tuple4 dir)
        {
            if (!Constants.EpsilonCompare(1.0, dir.Length()))
            {
                throw new ArgumentException("Direction should be normalized", nameof(dir));
            }

            var t = 0.0;

            while (t < maxDistance)
            {
                var p = Tuple4.Geometry3D.MovePoint(origin, dir, t);
                var d = DistanceFrom(p);
                if (Math.Abs(d) < epsilon)
                {
                    return(new double[] { t });
                }
                t += d;
            }

            return(null);
        }
        private double[] GetIntersections(Tuple4 origin, Tuple4 dir)
        {
            if (!Constants.EpsilonCompare(1.0, dir.Length()))
            {
                throw new ArgumentException("Direction should be normalized", nameof(dir));
            }

            var l   = Tuple4.Subtract(Center, origin);
            var tca = Tuple4.DotProduct(l, dir);
            var d2  = Tuple4.DotProduct(l, l) - tca * tca;

            if (d2 > radius2)
            {
                return(null);
            }
            var thc = Math.Sqrt(radius2 - d2);
            var t0  = tca - thc;
            var t1  = tca + thc;

            // Ray originates inside sphere
            // When t > 0 that is intersection in the direction of the ray
            // other intersection is in the opposite direction
            return(new double[] { t0, t1 });
        }