Exemplo n.º 1
0
        //#endif

        //#endfor instanced to '3Dd'

        //#foreach instanced to '3Df'


        //#ifdef 3D


        /// <summary>
        /// Intersect triangle and ray, fast barycentric coordinate version.
        /// </summary>
        /// <param name="t">Triangle.</param>
        /// <param name="Normal">Possibly precomputed normal.</param>
        /// <param name="r">The ray.</param>
        /// <param name="p">The output collision, valid if true is returned.</param>
        /// <returns>Intersection result</returns>
        public static bool Intersect(Triangle3f t, Ray3f r, float maxDist, out Vector3f p)
        {
            Vector3f Normal = t.Normal;

            // We compute distance of ray of plane.
            float d = -((r.Origin - t.A) * Normal) / (r.Direction * Normal);

            // We exit quickly if intersection occurs behind ray, or if intersection does not
            // exist.
            if (d < 0.0 || d >= maxDist || float.IsNaN(d))
            {
                p = Vector3f.Zero;
                return(false);
            }

            // We now compute the actual point on the plane.
            p = r.Origin + d * r.Direction;

            // We have to determine if point lies inside triangle. We do it using barycentric
            // coordinates. We solve the system with two unknowns.
            Vector2f uv = t.GetBarycentric(p);

            // Is it inside.
            if (Triangle3f.IsBaryCentricInside(uv))
            {
                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        //#endif

        //#endfor instanced to '3Dd'

        //#foreach instanced to '3Df'


        //#ifdef 3D


        /// <summary>
        /// Triangle-Line intersection helper (only 3D version).
        /// </summary>
        /// <param name="t">The triangle.</param>
        /// <param name="r">The line.</param>
        /// <param name="p">Point of intersection.</param>
        /// <returns>Does intersection exist.</returns>
        private static bool Intersect(Triangle3f t, Line3f r, out Vector3f p)
        {
            Vector3f Normal = t.Normal;

            // We compute distance of line of plane.
            float d = -((r.A - t.A) * Normal) / (r.Direction * Normal);

            // We now compute the actual point on the plane.
            p = r.A + d * r.Direction;

            // We have to determine if point lies inside triangle. We do it using barycentric
            // coordinates. We solve the system with two unknowns.
            Vector2f uv = t.GetBarycentric(p);

            // Is it inside.
            return(Triangle3f.IsBaryCentricInside(uv));
        }
Exemplo n.º 3
0
        //#endif

        //#endfor instanced to '3Dd'

        //#foreach instanced to '3Df'


        //#ifdef 3D


        /// <summary>
        /// A line-triangle intersection.
        /// </summary>
        /// <remarks>Only point is returned. In case of line segment lying on top of triangle, one
        /// point is only returned.</remarks>
        /// <param name="line">The line segment</param>
        /// <param name="triangle">The triangle</param>
        /// <param name="point">The intersection point.</param>
        /// <returns>Does intersection occur</returns>
        private static bool Intersect(LineSegment3f line, Triangle3f triangle, out Vector3f point)
        {
            // We compute distance of ray of plane.
            float d = -((line.A - triangle.A) * triangle.Normal) / (line.Direction * triangle.Normal);

            // We exit quickly if intersection does not occur on line.
            if (d < 0.0f || d > 1.0f)
            {
                point = Vector3f.Zero;
                return(false);
            }

            // We now compute the actual point on the plane.
            point = line.A + d * line.Direction;

            // We have to determine if point lies inside triangle. We do it using barycentric
            // coordinates. We solve the system with two unknowns.
            Vector2f uv = triangle.GetBarycentric(point);

            // Is it inside.
            return(Triangle3f.IsBaryCentricInside(uv));
        }
Exemplo n.º 4
0
 /// <summary>
 /// Always prefer this split over the tesellation because it is faster and there is
 /// no need to allocate collection. This only works for quads/rectanges.
 /// </summary>
 /// <param name="t1">The first triangle.</param>
 /// <param name="t2">The second triangle.</param>
 public void Split(out Triangle3f t1, out Triangle3f t2)
 {
     t1 = new Triangle3f(A, B, C);
     t2 = new Triangle3f(A, C, D);
 }