Esempio n. 1
0
        public static DistanceType CheckDistances(Tri tri1, Tri tri2)
        {
            //plane equation: p2 = normal2 * X + d2
            float distance1 = Vector3.Dot(tri2.normal, tri1.v0) + tri2.d;
            float distance2 = Vector3.Dot(tri2.normal, tri1.v1) + tri2.d;
            float distance3 = Vector3.Dot(tri2.normal, tri1.v2) + tri2.d;

            if (Math.Abs(distance1) > float.Epsilon &&
                Math.Abs(distance2) > float.Epsilon &&
                Math.Abs(distance3) > float.Epsilon)
            {
                if ((distance1 < 0 && distance2 < 0 && distance3 < 0)
                    ||
                    (distance1 > 0 && distance2 > 0 && distance3 > 0))
                {
                    return(DistanceType.NON_ZERO_SAME_SIGN);
                }

                else
                {
                    return(DistanceType.NON_ZERO_DIFFERENT_SIGN);
                }
            }
            else
            {
                return(DistanceType.ZERO);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Tri tri1 = new Tri(new float[9] {
                0, 10, 20, 60, 60, 60, 10, 10, 60
            });
            Tri tri2 = new Tri(new float[9] {
                0, 0, 20, 60, -60, 60, 10, -10, 60
            });
            var isIntersect = TriTriOverlap.IsTriTriIntersect(tri1, tri2);

            Console.WriteLine("isIntersect: " + isIntersect);

            tri1 = new Tri(new float[9] {
                0, 10, 20, 60, 60, 60, 10, 10, 60
            });
            tri2 = new Tri(new float[9] {
                0, 20, 20, 60, -60, 60, 10, -10, 60
            });
            isIntersect = TriTriOverlap.IsTriTriIntersect(tri1, tri2);
            Console.WriteLine("isIntersect: " + isIntersect);

            tri1 = new Tri(new float[9] {
                -20, -20, 0, 20, 20, 20, -10, -10, 20,
            });
            tri2 = new Tri(new float[9] {
                20, -30, 0, 30, 30, 10, -15, -15, 25
            });
            isIntersect = TriTriOverlap.IsTriTriIntersect(tri1, tri2);
            Console.WriteLine("isIntersect: " + isIntersect);


            //            var triPoints_modelSpace = [
            //0, 10, 20, 60, 60, 60, 10, 10, 60,
            //0, 20, 20, 60, -60, 60, 10, -10, 60]
        }
Esempio n. 3
0
        public static bool IsTriTriIntersect(Tri tri1, Tri tri2)
        {
            var distanceTri1_2 = CheckDistances(tri1, tri2);

            if (distanceTri1_2 == DistanceType.NON_ZERO_SAME_SIGN)
            {
                return(false);
            }

            var distanceTri2_1 = CheckDistances(tri2, tri1);

            if (distanceTri2_1 == DistanceType.NON_ZERO_SAME_SIGN)
            {
                return(false);
            }

            if (distanceTri1_2 == DistanceType.ZERO || distanceTri2_1 == DistanceType.ZERO)
            {
                return(IsCoplanarIntersect(tri1, tri2));
            }

            var L_intersectLine = Vector3.Cross(tri1.normal, tri2.normal);
            var max             = Math.Abs(L_intersectLine.X);
            var index           = 0;
            var bb = Math.Abs(L_intersectLine.Y);
            var cc = Math.Abs(L_intersectLine.Z);

            if (bb > max)
            {
                max = bb; index = 1;
            }
            if (cc > max)
            {
                max = cc; index = 2;
            }

            float vp0, vp1, vp2, up0, up1, up2;

            vp0 = tri1.v0.ElemByIndex(index);
            vp1 = tri1.v1.ElemByIndex(index);
            vp2 = tri1.v2.ElemByIndex(index);

            up0 = tri2.v0.ElemByIndex(index);
            up1 = tri2.v1.ElemByIndex(index);
            up2 = tri2.v2.ElemByIndex(index);

            // compute interval for triangle 1
            float a = 0, b = 0, c = 0, x0 = 0, x1 = 0;
            float dv0    = Vector3.Dot(tri2.normal, tri1.v0) + tri2.d;
            float dv1    = Vector3.Dot(tri2.normal, tri1.v1) + tri2.d;
            float dv2    = Vector3.Dot(tri2.normal, tri1.v2) + tri2.d;
            float dv0dv1 = dv0 * dv1;
            float dv0dv2 = dv0 * dv2;

            if (ComputeIntervals(vp0, vp1, vp2, dv0, dv1, dv2, dv0dv1, dv0dv2, ref a, ref b, ref c, ref x0, ref x1))
            {
                return(false);
                //return TriTriCoplanar(n1, v0, v1, v2, u0, u1, u2);
            }


            // compute interval for triangle 1
            float du0    = Vector3.Dot(tri1.normal, tri2.v0) + tri1.d;
            float du1    = Vector3.Dot(tri1.normal, tri2.v1) + tri1.d;
            float du2    = Vector3.Dot(tri1.normal, tri2.v2) + tri1.d;
            float du0du1 = du0 * du1;
            float du0du2 = du0 * du2;

            // compute interval for triangle 2
            float d = 0, e = 0, f = 0, y0 = 0, y1 = 0;

            if (ComputeIntervals(up0, up1, up2, du0, du1, du2, du0du1, du0du2, ref d, ref e, ref f, ref y0, ref y1))
            {
                return(false);
                //return TriTriCoplanar(n1, v0, v1, v2, u0, u1, u2);
            }

            float xx, yy, xxyy, tmp;

            xx   = x0 * x1;
            yy   = y0 * y1;
            xxyy = xx * yy;

            tmp = a * xxyy;

            Vector2 isect1 = Vector2.Zero, isect2 = Vector2.Zero;


            isect1.X = tmp + b * x1 * yy;
            isect1.Y = tmp + c * x0 * yy;

            tmp      = d * xxyy;
            isect2.X = tmp + e * xx * y1;
            isect2.Y = tmp + f * xx * y0;

            Sort(isect1);
            Sort(isect2);

            return(!(isect1.Y < isect2.X || isect2.Y < isect1.X));
        }
Esempio n. 4
0
 public static bool IsCoplanarIntersect(Tri tri1, Tri tri2)
 {
     return(true);
 }