/// <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 double?Intersects(Rayd 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((double)txmin); } else { return(null); } }
/// <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 double?Intersects(Rayd 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((double)(dot - Functions.Sqrt(rr - temp))); }
/// <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(Rayd left, Rayd right) { return(left == right); }
/// <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 Point3d Parametrize(Rayd ray, double t) { return(ray.Position + (t * ray.Direction)); }
/// <summary> /// Writes the given <see cref="Rayd"/> to a Ibasa.IO.BinaryWriter. /// </summary> public static void Write(this Ibasa.IO.BinaryWriter writer, Rayd ray) { Point.Write(writer, ray.Position); Vector.Write(writer, ray.Direction); }