Exemple #1
0
        /// <summary>
        /// Determines whether a ray intersects the specified box.
        /// </summary>
        /// <param name="ray">The ray which will be tested for intersection.</param>
        /// <param name="box">A box that will be tested for intersection.</param>
        /// <returns>Distance at which the ray intersects the box or null if there is no intersection.</returns>
        public static float?Intersects(Rayf ray, Boxi box)
        {
            return(null);

            var  invDir = Vector.Reciprocal(ray.Direction);
            bool signX  = invDir.X < 0;
            bool signY  = invDir.Y < 0;
            bool signZ  = invDir.Z < 0;
            var  min    = signX ? box.Right : box.Left;
            var  max    = signX ? box.Left : box.Right;
            var  txmin  = (min - ray.Position.X) * invDir.X;
            var  txmax  = (max - ray.Position.X) * invDir.X;

            min = signY ? box.Top : box.Bottom;
            max = signY ? box.Bottom : box.Top;
            var tymin = (min - ray.Position.Y) * invDir.Y;
            var tymax = (max - ray.Position.Y) * invDir.Y;

            if ((txmin > tymax) || (tymin > txmax))
            {
                return(null);
            }
            if (tymin > txmin)
            {
                txmin = tymin;
            }
            if (tymax < txmax)
            {
                txmax = tymax;
            }
            min = signZ ? box.Back : box.Front;
            max = signZ ? box.Front : box.Back;
            var tzmin = (min - ray.Position.Z) * invDir.Z;
            var tzmax = (max - ray.Position.Z) * invDir.Z;

            if ((txmin > tzmax) || (tzmin > txmax))
            {
                return(null);
            }
            if (tzmin > txmin)
            {
                txmin = tzmin;
            }
            if (tzmax < txmax)
            {
                txmax = tzmax;
            }
            if (txmin < double.PositiveInfinity && txmax >= 0)
            {
                return((float)txmin);
            }
            else
            {
                return(null);
            }
        }
Exemple #2
0
        /// <summary>
        /// Determines whether a ray intersects the specified sphere.
        /// </summary>
        /// <param name="ray">The ray which will be tested for intersection.</param>
        /// <param name="sphere">A sphere that will be tested for intersection.</param>
        /// <returns>Distance at which the ray intersects the sphere or null if there is no intersection.</returns>
        public static float?Intersects(Rayf ray, Spherei sphere)
        {
            var distance = sphere.Center - ray.Position;
            var pyth     = Vector.AbsoluteSquared(distance);
            var rr       = sphere.Radius * sphere.Radius;

            if (pyth <= rr)
            {
                return(0);
            }
            double dot = Vector.Dot(distance, ray.Direction);

            if (dot < 0)
            {
                return(null);
            }
            var temp = pyth - (dot * dot);

            if (temp > rr)
            {
                return(null);
            }
            return((float)(dot - Functions.Sqrt(rr - temp)));
        }
Exemple #3
0
 /// <summary>
 /// Returns a value that indicates whether two rays are equal.
 /// </summary>
 /// <param name="left">The first ray to compare.</param>
 /// <param name="right">The second ray to compare.</param>
 /// <returns>true if the left and right are equal; otherwise, false.</returns>
 public static bool Equals(Rayf left, Rayf right)
 {
     return(left == right);
 }
Exemple #4
0
 /// <summary>
 /// Returns the point in the ray at position t.
 /// </summary>
 /// <param name="ray">The ray to parametrize.</param>
 /// <param name="t">The paramater t.</param>
 /// <returns>The point at t.</returns>
 public static Point3f Parametrize(Rayf ray, float t)
 {
     return(ray.Position + (t * ray.Direction));
 }
Exemple #5
0
 /// <summary>
 /// Writes the given <see cref="Rayf"/> to a Ibasa.IO.BinaryWriter.
 /// </summary>
 public static void Write(this Ibasa.IO.BinaryWriter writer, Rayf ray)
 {
     Point.Write(writer, ray.Position);
     Vector.Write(writer, ray.Direction);
 }