/// <summary>
        /// Tests for intersection between a <see cref="Segment"/> and a <see cref="Plane"/>
        /// </summary>
        /// <param name="line">A <see cref="Segment"/> instance.</param>
        /// <param name="plane">A <see cref="Plane"/> instance.</param>
        /// <returns>An <see cref="IntersectionPair"/> instance containing the intersection information.</returns>
        public static IntersectionPair Intersects(Segment segment, Plane plane)
        {
            if (TestIntersection(segment, plane) == false)
                return new IntersectionPair(false, Vector3F.Zero);

            Vector3F dir = segment.P1 - segment.P0;
            float d  = Vector3F.Dot(plane.Normal, dir);
            float t  = (plane.Constant - Vector3F.Dot(plane.Normal, segment.P0)) / d;

            return new IntersectionPair(true, segment.P0 + dir*t);
        }
        /// <summary>
        /// Tests for intersection between a <see cref="Segment"/> and a <see cref="Plane"/>.
        /// </summary>
        /// <param name="line">A <see cref="Segment"/> instance.</param>
        /// <param name="plane">A <see cref="Plane"/> instance.</param>
        /// <returns><see langword="true"/> if intersection occurs; otherwise, <see langword="false"/>.</returns>
        public static bool TestIntersection(Segment segment, Plane plane)
        {
            // Get the position of the line's end point relative to the plane.
            int sign0 = (int)plane.GetSign(segment.P0);
            int sign1 = (int)plane.GetSign(segment.P1);

            // Intersection occurs if the 2 endpoints are at oposite sides of the plane.
            return ( ((sign0 > 0) && (sign1 < 0)) || ((sign0 < 0) && (sign0 > 0)) );
        }
        /// <summary>
        /// Tests for intersection between a <see cref="Ray"/> and a <see cref="Plane"/>.
        /// </summary>
        /// <param name="ray">A <see cref="Ray"/> instance.</param>
        /// <param name="plane">A <see cref="Plane"/> instance.</param>
        /// <returns>An <see cref="IntersectionPair"/> instance containing the intersection information.</returns>
        public static IntersectionPair Intersects(Ray ray, Plane plane)
        {
            bool intersect = false;
            Vector3F hitPoint = Vector3F.Zero;
            float denominator = Vector3F.Dot(plane.Normal, ray.Direction);

            // Check if the ray is parrallel to the plane
            if (MathFunctions.ApproxEquals(denominator, 0.0f))
            {
                // If the line is parallel to the plane it only intersects the plane if it is on the plane.
                intersect = (plane.GetSign(ray.Origin) == MathFunctions.Sign.Zero);
                if (intersect)
                    hitPoint = ray.Origin;
            }
            else
            {
                float t = (plane.Constant - Vector3F.Dot(plane.Normal, ray.Origin)) / denominator;
                hitPoint = ray.Origin + ray.Direction * t;
                intersect = true;
            }

            return new IntersectionPair(intersect, hitPoint);
        }
예제 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Plane"/> class using given a plane to assign values from.
 /// </summary>
 /// <param name="p">A 3D plane to assign values from.</param>
 public Plane(Plane p)
 {
     _normal	= p.Normal;
     _const	= p.Constant;
 }
 /// <summary>
 /// Builds a matrix that reflects the coordinate system about a plane.
 /// </summary>
 /// <param name="p">A <see cref="Plane"/> instance.</param>
 /// <returns>A <see cref="Matrix4F"/> representing the reflection transformation.</returns>
 /// <remarks>
 /// This method normalizes the plane's equation before creating the reflection matrix.
 /// </remarks>
 public static Matrix4F Reflect(Plane p)
 {
     // TODO: Implement this.
     throw new NotImplementedException();
 }
 /// <summary>
 /// Builds a matrix that flattens geometry into a plane.
 /// </summary>
 /// <param name="lightPosition">A <see cref="Vector4F"/> instance representing the light source position.</param>
 /// <param name="p">A <see cref="Plane"/> instance.</param>
 /// <returns>A <see cref="Matrix4F"/> representing the shadow transformation.</returns>
 /// <remarks>
 /// This method flattens geometry into a plane, as if it were casting a shadow from a light.
 /// </remarks>
 public static Matrix4F Shadow(Vector4F lightPosition, Plane p)
 {
     // TODO: Implement this.
     throw new NotImplementedException();
 }
 /// <summary>
 /// Calculates the distance between a point and a plane.
 /// </summary>
 /// <param name="point">A <see cref="Vector3F"/> instance.</param>
 /// <param name="plane">A <see cref="Plane"/> instance.</param>
 /// <returns>The distance between a point and a plane.</returns>
 /// <remarks>
 /// <p>
 ///  A positive return value means teh point is on the positive side of the plane.
 ///  A negative return value means teh point is on the negative side of the plane.
 ///  A zero return value means the point is on the plane.
 /// </p>
 /// <p>
 ///  The absolute value of the return value is the true distance only when the plane normal is
 ///  a unit length vector. 
 /// </p>
 /// </remarks>
 public static float Distance(Vector3F point, Plane plane)
 {
     return Vector3F.Dot(plane.Normal, point) + plane.Constant;
 }