Пример #1
0
        public static PlaneIntersectionType Intersects(XMVector v0, XMVector v1, XMVector v2, XMVector plane)
        {
            XMVector one = XMGlobalConstants.One;

            Debug.Assert(Internal.XMPlaneIsUnit(plane), "Reviewed");

            // Set w of the points to one so we can dot4 with a plane.
            XMVector tV0 = new XMVector(v0.X, v0.Y, v0.Z, one.W);
            XMVector tV1 = new XMVector(v1.X, v1.Y, v1.Z, one.W);
            XMVector tV2 = new XMVector(v2.X, v2.Y, v2.Z, one.W);

            XMVector outside, inside;

            Internal.FastIntersectTrianglePlane(tV0, tV1, tV2, plane, out outside, out inside);

            // If the triangle is outside any plane it is outside.
            if (XMVector4.EqualInt(outside, XMVector.TrueInt))
            {
                return(PlaneIntersectionType.Front);
            }

            // If the triangle is inside all planes it is inside.
            if (XMVector4.EqualInt(inside, XMVector.TrueInt))
            {
                return(PlaneIntersectionType.Back);
            }

            // The triangle is not inside all planes or outside a plane it intersects.
            return(PlaneIntersectionType.Intersecting);
        }
Пример #2
0
        public PlaneIntersectionType Intersects(XMVector plane)
        {
            Debug.Assert(Internal.XMPlaneIsUnit(plane), "Reviewed");

            // Load the box.
            XMVector v_center  = this.center;
            XMVector v_extents = this.extents;

            // Set w of the center to one so we can dot4 with a plane.
            v_center.W = 1.0f;

            XMVector outside, inside;

            Internal.FastIntersectAxisAlignedBoxPlane(v_center, v_extents, plane, out outside, out inside);

            // If the box is outside any plane it is outside.
            if (XMVector4.EqualInt(outside, XMVector.TrueInt))
            {
                return(PlaneIntersectionType.Front);
            }

            // If the box is inside all planes it is inside.
            if (XMVector4.EqualInt(inside, XMVector.TrueInt))
            {
                return(PlaneIntersectionType.Back);
            }

            // The box is not inside all planes or outside a plane it intersects.
            return(PlaneIntersectionType.Intersecting);
        }
        public PlaneIntersectionType Intersects(XMVector plane)
        {
            Debug.Assert(Internal.XMPlaneIsUnit(plane), "Reviewed");

            // Load the sphere.
            XMVector v_center = this.center;
            XMVector v_radius = XMVector.Replicate(this.radius);

            // Set w of the center to one so we can dot4 with a plane.
            v_center.W = 1.0f;

            XMVector outside, inside;

            Internal.FastIntersectSpherePlane(v_center, v_radius, plane, out outside, out inside);

            // If the sphere is outside any plane it is outside.
            if (XMVector4.EqualInt(outside, XMVector.TrueInt))
            {
                return(PlaneIntersectionType.Front);
            }

            // If the sphere is inside all planes it is inside.
            if (XMVector4.EqualInt(inside, XMVector.TrueInt))
            {
                return(PlaneIntersectionType.Back);
            }

            // The sphere is not inside all planes or outside a plane it intersects.
            return(PlaneIntersectionType.Intersecting);
        }
Пример #4
0
        public ContainmentType ContainedBy(
            XMVector plane0,
            XMVector plane1,
            XMVector plane2,
            XMVector plane3,
            XMVector plane4,
            XMVector plane5)
        {
            // Load the box.
            XMVector v_center  = this.center;
            XMVector v_extents = this.extents;

            // Set w of the center to one so we can dot4 with a plane.
            v_center.W = 1.0f;

            XMVector outside, inside;

            // Test against each plane.
            Internal.FastIntersectAxisAlignedBoxPlane(v_center, v_extents, plane0, out outside, out inside);

            XMVector anyOutside = outside;
            XMVector allInside  = inside;

            Internal.FastIntersectAxisAlignedBoxPlane(v_center, v_extents, plane1, out outside, out inside);
            anyOutside = XMVector.OrInt(anyOutside, outside);
            allInside  = XMVector.AndInt(allInside, inside);

            Internal.FastIntersectAxisAlignedBoxPlane(v_center, v_extents, plane2, out outside, out inside);
            anyOutside = XMVector.OrInt(anyOutside, outside);
            allInside  = XMVector.AndInt(allInside, inside);

            Internal.FastIntersectAxisAlignedBoxPlane(v_center, v_extents, plane3, out outside, out inside);
            anyOutside = XMVector.OrInt(anyOutside, outside);
            allInside  = XMVector.AndInt(allInside, inside);

            Internal.FastIntersectAxisAlignedBoxPlane(v_center, v_extents, plane4, out outside, out inside);
            anyOutside = XMVector.OrInt(anyOutside, outside);
            allInside  = XMVector.AndInt(allInside, inside);

            Internal.FastIntersectAxisAlignedBoxPlane(v_center, v_extents, plane5, out outside, out inside);
            anyOutside = XMVector.OrInt(anyOutside, outside);
            allInside  = XMVector.AndInt(allInside, inside);

            // If the box is outside any plane it is outside.
            if (XMVector4.EqualInt(anyOutside, XMVector.TrueInt))
            {
                return(ContainmentType.Disjoint);
            }

            // If the box is inside all planes it is inside.
            if (XMVector4.EqualInt(allInside, XMVector.TrueInt))
            {
                return(ContainmentType.Contains);
            }

            // The box is not inside all planes or outside a plane, it may intersect.
            return(ContainmentType.Intersects);
        }
        public bool Intersects(XMVector v0, XMVector v1, XMVector v2)
        {
            // Load the sphere.
            XMVector v_center = this.center;
            XMVector v_radius = XMVector.Replicate(this.radius);

            // Compute the plane of the triangle (has to be normalized).
            XMVector n = XMVector3.Normalize(XMVector3.Cross(v1 - v0, v2 - v0));

            // Assert that the triangle is not degenerate.
            Debug.Assert(!XMVector3.Equal(n, XMGlobalConstants.Zero), "Reviewed");

            // Find the nearest feature on the triangle to the sphere.
            XMVector dist = XMVector3.Dot(v_center - v0, n);

            // If the center of the sphere is farther from the plane of the triangle than
            // the radius of the sphere, then there cannot be an intersection.
            XMVector noIntersection = XMVector.Less(dist, -v_radius);

            noIntersection = XMVector.OrInt(noIntersection, XMVector.Greater(dist, v_radius));

            // Project the center of the sphere onto the plane of the triangle.
            XMVector point = v_center - (n * dist);

            // Is it inside all the edges? If so we intersect because the distance
            // to the plane is less than the radius.
            XMVector intersection = Internal.PointOnPlaneInsideTriangle(point, v0, v1, v2);

            // Find the nearest point on each edge.
            XMVector radiusSq = v_radius * v_radius;

            // Edge 0,1
            point = Internal.PointOnLineSegmentNearestPoint(v0, v1, v_center);

            // If the distance to the center of the sphere to the point is less than
            // the radius of the sphere then it must intersect.
            intersection = XMVector.OrInt(intersection, XMVector.LessOrEqual(XMVector3.LengthSquare(v_center - point), radiusSq));

            // Edge 1,2
            point = Internal.PointOnLineSegmentNearestPoint(v1, v2, v_center);

            // If the distance to the center of the sphere to the point is less than
            // the radius of the sphere then it must intersect.
            intersection = XMVector.OrInt(intersection, XMVector.LessOrEqual(XMVector3.LengthSquare(v_center - point), radiusSq));

            // Edge 2,0
            point = Internal.PointOnLineSegmentNearestPoint(v2, v0, v_center);

            // If the distance to the center of the sphere to the point is less than
            // the radius of the sphere then it must intersect.
            intersection = XMVector.OrInt(intersection, XMVector.LessOrEqual(XMVector3.LengthSquare(v_center - point), radiusSq));

            return(XMVector4.EqualInt(XMVector.AndComplementInt(intersection, noIntersection), XMVector.TrueInt));
        }
Пример #6
0
        public static bool Intersects(XMVector origin, XMVector direction, XMVector v0, XMVector v1, XMVector v2, out float uCoordinate, out float vCoordinate, out float distance)
        {
            Debug.Assert(Internal.XMVector3IsUnit(direction), "Reviewed");

            XMVector zero = XMGlobalConstants.Zero;

            XMVector e1 = v1 - v0;
            XMVector e2 = v2 - v0;

            // p = Direction ^ e2;
            XMVector p = XMVector3.Cross(direction, e2);

            // det = e1 * p;
            XMVector det = XMVector3.Dot(e1, p);

            XMVector u, v, t;

            if (XMVector3.GreaterOrEqual(det, CollisionGlobalConstants.RayEpsilon))
            {
                // Determinate is positive (front side of the triangle).
                XMVector s = origin - v0;

                // u = s * p;
                u = XMVector3.Dot(s, p);

                XMVector noIntersection = XMVector.Less(u, zero);
                noIntersection = XMVector.OrInt(noIntersection, XMVector.Greater(u, det));

                // q = s ^ e1;
                XMVector q = XMVector3.Cross(s, e1);

                // v = Direction * q;
                v = XMVector3.Dot(direction, q);

                noIntersection = XMVector.OrInt(noIntersection, XMVector.Less(v, zero));
                noIntersection = XMVector.OrInt(noIntersection, XMVector.Greater(u + v, det));

                // t = e2 * q;
                t = XMVector3.Dot(e2, q);

                noIntersection = XMVector.OrInt(noIntersection, XMVector.Less(t, zero));

                if (XMVector4.EqualInt(noIntersection, XMVector.TrueInt))
                {
                    uCoordinate = 0.0f;
                    vCoordinate = 0.0f;
                    distance    = 0.0f;
                    return(false);
                }
            }
            else if (XMVector3.LessOrEqual(det, CollisionGlobalConstants.RayNegEpsilon))
            {
                // Determinate is negative (back side of the triangle).
                XMVector s = origin - v0;

                // u = s * p;
                u = XMVector3.Dot(s, p);

                XMVector noIntersection = XMVector.Greater(u, zero);
                noIntersection = XMVector.OrInt(noIntersection, XMVector.Less(u, det));

                // q = s ^ e1;
                XMVector q = XMVector3.Cross(s, e1);

                // v = Direction * q;
                v = XMVector3.Dot(direction, q);

                noIntersection = XMVector.OrInt(noIntersection, XMVector.Greater(v, zero));
                noIntersection = XMVector.OrInt(noIntersection, XMVector.Less(u + v, det));

                // t = e2 * q;
                t = XMVector3.Dot(e2, q);

                noIntersection = XMVector.OrInt(noIntersection, XMVector.Greater(t, zero));

                if (XMVector4.EqualInt(noIntersection, XMVector.TrueInt))
                {
                    uCoordinate = 0.0f;
                    vCoordinate = 0.0f;
                    distance    = 0.0f;
                    return(false);
                }
            }
            else
            {
                // Parallel ray.
                uCoordinate = 0.0f;
                vCoordinate = 0.0f;
                distance    = 0.0f;
                return(false);
            }

            // (u / det) and (v / dev) are the barycentric coordinates of the intersection.

            u = XMVector.Divide(u, det);
            v = XMVector.Divide(v, det);
            t = XMVector.Divide(t, det);

            // Store the x-component to *pDist
            u.StoreFloat(out uCoordinate);
            v.StoreFloat(out vCoordinate);
            t.StoreFloat(out distance);

            return(true);
        }
Пример #7
0
        public static ContainmentType ContainedBy(
            XMVector v0,
            XMVector v1,
            XMVector v2,
            XMVector plane0,
            XMVector plane1,
            XMVector plane2,
            XMVector plane3,
            XMVector plane4,
            XMVector plane5)
        {
            XMVector one = XMGlobalConstants.One;

            // Set w of the points to one so we can dot4 with a plane.
            XMVector tV0 = new XMVector(v0.X, v0.Y, v0.Z, one.W);
            XMVector tV1 = new XMVector(v1.X, v1.Y, v1.Z, one.W);
            XMVector tV2 = new XMVector(v2.X, v2.Y, v2.Z, one.W);

            XMVector outside, inside;

            // Test against each plane.
            Internal.FastIntersectTrianglePlane(tV0, tV1, tV2, plane0, out outside, out inside);

            XMVector anyOutside = outside;
            XMVector allInside  = inside;

            Internal.FastIntersectTrianglePlane(tV0, tV1, tV2, plane1, out outside, out inside);
            anyOutside = XMVector.OrInt(anyOutside, outside);
            allInside  = XMVector.AndInt(allInside, inside);

            Internal.FastIntersectTrianglePlane(tV0, tV1, tV2, plane2, out outside, out inside);
            anyOutside = XMVector.OrInt(anyOutside, outside);
            allInside  = XMVector.AndInt(allInside, inside);

            Internal.FastIntersectTrianglePlane(tV0, tV1, tV2, plane3, out outside, out inside);
            anyOutside = XMVector.OrInt(anyOutside, outside);
            allInside  = XMVector.AndInt(allInside, inside);

            Internal.FastIntersectTrianglePlane(tV0, tV1, tV2, plane4, out outside, out inside);
            anyOutside = XMVector.OrInt(anyOutside, outside);
            allInside  = XMVector.AndInt(allInside, inside);

            Internal.FastIntersectTrianglePlane(tV0, tV1, tV2, plane5, out outside, out inside);
            anyOutside = XMVector.OrInt(anyOutside, outside);
            allInside  = XMVector.AndInt(allInside, inside);

            // If the triangle is outside any plane it is outside.
            if (XMVector4.EqualInt(anyOutside, XMVector.TrueInt))
            {
                return(ContainmentType.Disjoint);
            }

            // If the triangle is inside all planes it is inside.
            if (XMVector4.EqualInt(allInside, XMVector.TrueInt))
            {
                return(ContainmentType.Contains);
            }

            // The triangle is not inside all planes or outside a plane, it may intersect.
            return(ContainmentType.Intersects);
        }