public static float PointClosest2Triangle2(Vector2 p1, Vector2 p2, Vector2 p3, ref Vector2 p, ref Vector2 close)
 {
     if (IsPointInTriangle2(p1, p2, p3, ref p))
     {
         // 内部不处理
         close = p;
         return(0.0f);
     }
     else
     {
         Vector2 tmp = new Vector2();
         float   min = float.MaxValue;
         float   d   = GeoSegmentUtils.PointClosest2Segment2(p1, p2, ref p, ref tmp);
         if (d < min)
         {
             min   = d;
             close = tmp;
         }
         d = GeoSegmentUtils.PointClosest2Segment2(p3, p2, ref p, ref tmp);
         if (d < min)
         {
             min   = d;
             close = tmp;
         }
         d = GeoSegmentUtils.PointClosest2Segment2(p1, p3, ref p, ref tmp);
         if (d < min)
         {
             min   = d;
             close = tmp;
         }
         return(min);
     }
 }
        public static bool IsTriangleInsectTriangle2(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 p4, Vector2 p5, Vector2 p6, ref GeoInsectPointArrayInfo insect)
        {
            GeoInsectPointArrayInfo tmp = new GeoInsectPointArrayInfo();
            List <Vector2>          tri = new List <Vector2>();

            tri.Add(p1);
            tri.Add(p2);
            tri.Add(p3);
            bool inSect = false;

            for (int i = 0; i < 3; ++i)
            {
                int j = (i + 1) % 3;
                inSect = GeoSegmentUtils.IsSegmentInsectOrOverlapTriangle2(tri[i], tri[j], p4, p5, p6, ref tmp) || inSect;
            }
            if (inSect)
            {
                insect.mHitGlobalPoint.mPointArray.AddRange(tmp.mHitGlobalPoint.mPointArray);
            }
            insect.mIsIntersect = insect.mHitGlobalPoint.mPointArray.Count > 0;
            if (insect.mIsIntersect)
            {
                insect.UniquePoint();
            }
            return(insect.mIsIntersect);
        }
Esempio n. 3
0
        public static bool IsAABBInsectTriangle2(Vector2 min, Vector2 max, Vector2 p1, Vector2 p2, Vector2 p3, ref GeoInsectPointArrayInfo insect)
        {
            // x = minx x = maxx
            // y = miny y = maxy
            List <Vector2> res = new List <Vector2>();
            Vector2        v   = new Vector2();
            List <Vector2> tmp = new List <Vector2>();

            tmp.Add(p1);
            tmp.Add(p2);
            tmp.Add(p3);
            for (int i = 0; i < 3; ++i)
            {
                int j = (i + 1) % 3;
                if (GeoSegmentUtils.InterpolateX2(tmp[i], tmp[j], min[0], ref v))
                {
                    if (v.y <= max[1] && v.y >= min[1])
                    {
                        res.Add(v);
                    }
                }
                v = new Vector2();
                if (GeoSegmentUtils.InterpolateX2(tmp[i], tmp[j], max[0], ref v))
                {
                    if (v.y <= max[1] && v.y >= min[1])
                    {
                        res.Add(v);
                    }
                }
                v = new Vector2();
                if (GeoSegmentUtils.InterpolateY2(tmp[i], tmp[j], min[1], ref v))
                {
                    if (v.x <= max[0] && v.x >= min[0])
                    {
                        res.Add(v);
                    }
                }
                v = new Vector2();
                if (GeoSegmentUtils.InterpolateY2(tmp[i], tmp[j], max[1], ref v))
                {
                    if (v.x <= max[0] && v.x >= min[0])
                    {
                        res.Add(v);
                    }
                }
            }
            insect.mIsIntersect = res.Count > 0;
            if (insect.mIsIntersect)
            {
                foreach (Vector2 vt in res)
                {
                    insect.mHitGlobalPoint.mPointArray.Add(new Vector3(vt.x, vt.y, 0.0f));
                }
            }
            return(insect.mIsIntersect);
        }
        public static bool IsTriangleInsectCircle3(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 center, float r, GeoPlane plane, ref GeoInsectPointArrayInfo insect)
        {
            bool isInsect = GeoPlaneUtils.IsPlaneInsectTriangle(plane.mNormal, plane.mD, p1, p2, p3, ref insect);

            if (isInsect)
            {
                if (insect.mHitGlobalPoint.mPointArray.Count == 3) // 共面, 转化成 2d
                {
                    insect.Clear();
                    return(IsTriangleInsectPlaneCircle2(p1, p2, p3, center, r, plane, ref insect));
                }
                else if (insect.mHitGlobalPoint.mPointArray.Count == 1)
                {
                    if (GeoCircleUtils.IsInSphere(center, r, insect.mHitGlobalPoint.mPointArray[0]))
                    {
                        return(true);
                    }
                }
                else if (insect.mHitGlobalPoint.mPointArray.Count == 2)
                {
                    Vector3 seg1 = plane.TransformToLocal(insect.mHitGlobalPoint.mPointArray[0]);
                    Vector3 seg2 = plane.TransformToLocal(insect.mHitGlobalPoint.mPointArray[1]);
                    Vector3 c1   = plane.TransformToLocal(center);
                    Vector2 p12  = new Vector2(seg1.x, seg1.z);
                    Vector2 p22  = new Vector2(seg2.x, seg2.z);
                    Vector2 c2   = new Vector2(c1.x, c1.z);
                    insect.Clear();
                    GeoInsectPointArrayInfo temp = new GeoInsectPointArrayInfo();
                    if (GeoSegmentUtils.IsSegmentInsectCircle2(p12, p22, c2, r, ref temp))
                    {
                        insect.mIsIntersect = true;
                        foreach (Vector3 v in temp.mHitGlobalPoint.mPointArray)
                        {
                            Vector3 vv = new Vector3(v.x, 0.0f, v.y);
                            vv = plane.TransformToGlobal(vv);
                            insect.mHitGlobalPoint.mPointArray.Add(vv);
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }
Esempio n. 5
0
        public static bool IsSegmentInsectAABB2(Vector2 seg1, Vector2 seg2, Vector2 min, Vector2 max, ref GeoInsectPointArrayInfo insect)
        {
            bool isInsect = GeoLineUtils.IsLineInsectAABB2(seg1, seg2, min, max, ref insect);

            if (isInsect)
            {
                List <Vector3> tmp = new List <Vector3>();
                foreach (Vector3 v in insect.mHitGlobalPoint.mPointArray)
                {
                    Vector2 v2 = new Vector2(v.x, v.y);
                    if (GeoSegmentUtils.IsPointInSegment2(seg1, seg2, ref v2))
                    {
                        tmp.Add(v);
                    }
                }
                insect.mHitGlobalPoint.mPointArray = tmp;
            }
            return(insect.mIsIntersect);
        }
Esempio n. 6
0
        public static bool IsSegmentInsectOrOverlapTriangle2(Vector2 seg1, Vector2 seg2, Vector2 p1, Vector2 p2, Vector2 p3, ref GeoInsectPointArrayInfo insect)
        {
            // 在三角形内部
            if (GeoTriangleUtils.IsPointInTriangle2(p1, p2, p3, ref seg1) && GeoTriangleUtils.IsPointInTriangle2(p1, p2, p3, ref seg2))
            {
                insect.mIsIntersect = true;
                insect.mHitGlobalPoint.mPointArray.Add(new Vector3(seg1.x, seg1.y, 0.0f));
                insect.mHitGlobalPoint.mPointArray.Add(new Vector3(seg2.x, seg2.y, 0.0f));
                return(true);
            }
            // 相交测试(不包含 边的重叠)
            bool isInsect = GeoSegmentUtils.IsSegmentInsectTriangle2(seg1, seg2, p1, p2, p3, ref insect);

            if (!isInsect)
            {
                // 边重叠 测试
                isInsect = GeoSegmentUtils.IsSegmentOverlapTriangle2(seg1, seg2, p1, p2, p3, ref insect);
            }
            return(insect.mIsIntersect);
        }
        public static bool IsTriangleInsectCircle2(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 center, float r, ref GeoInsectPointArrayInfo insect)
        {
            GeoInsectPointArrayInfo tmp = new GeoInsectPointArrayInfo();

            if (GeoSegmentUtils.IsSegmentInsectCircle2(p1, p2, center, r, ref tmp))
            {
                insect.mHitGlobalPoint.mPointArray.AddRange(tmp.mHitGlobalPoint.mPointArray);
            }
            tmp.Clear();
            if (GeoSegmentUtils.IsSegmentInsectCircle2(p3, p2, center, r, ref tmp))
            {
                insect.mHitGlobalPoint.mPointArray.AddRange(tmp.mHitGlobalPoint.mPointArray);
            }
            tmp.Clear();
            if (GeoSegmentUtils.IsSegmentInsectCircle2(p1, p3, center, r, ref tmp))
            {
                insect.mHitGlobalPoint.mPointArray.AddRange(tmp.mHitGlobalPoint.mPointArray);
            }
            tmp.Clear();
            return(false);
        }
Esempio n. 8
0
        public static bool IsAABBInsectCircle3(Vector3 min, Vector3 max, GeoPlane plane1, Vector3 center, float r, GeoPlane plane2, ref GeoInsectPointArrayInfo insect)
        {
            float dot = Vector3.Dot(plane1.mNormal, plane2.mNormal);

            if (1 - Mathf.Abs(dot) < 1e-5f)
            {
                if (GeoPlaneUtils.IsPointOnPlane(plane2.mNormal, plane2.mD, min))
                {
                    return(IsAABBInsectCirclePlane2(min, max, center, r, plane1, ref insect)); // should use plane1
                }
                return(false);
            }
            GeoInsectPointArrayInfo tmp = new GeoInsectPointArrayInfo();
            bool isInsect = GeoPlaneUtils.IsPlaneInsectCircle(plane1.mNormal, plane1.mD, center, r, plane2, ref tmp);

            if (isInsect)
            {
                if (tmp.mHitGlobalPoint.mPointArray.Count == 1)
                {
                    Vector3 line1 = tmp.mHitGlobalPoint.mPointArray[0];
                    if (GeoAABBUtils.IsPointInAABB2Plane(min, max, plane1, ref line1))
                    {
                        insect.mIsIntersect = true;
                        insect.mHitGlobalPoint.mPointArray.Add(line1);
                        return(true);
                    }
                }
                else
                {
                    Vector3 seg1 = tmp.mHitGlobalPoint.mPointArray[0];
                    Vector3 seg2 = seg1 + tmp.mHitGlobalPoint.mPointArray[1];
                    return(GeoSegmentUtils.IsSegmentInsectAABB2Plane(seg1, seg2, min, max, plane1, ref insect));
                }
            }
            return(false);
        }
Esempio n. 9
0
        public static bool IsSegmentInsectAABB3(Vector3 seg1, Vector3 seg2, Vector3 min, Vector2 max, ref GeoInsectPointArrayInfo insect)
        {
            bool isInsect = GeoLineUtils.IsLineInsectAABB3(seg1, seg2, min, max, ref insect);

            if (isInsect)
            {
                List <Vector3> tmp = new List <Vector3>();
                foreach (Vector3 v in insect.mHitGlobalPoint.mPointArray)
                {
                    Vector3 v2 = v;
                    if (GeoSegmentUtils.IsPointInSegment3(seg1, seg2, ref v2))
                    {
                        tmp.Add(v);
                    }
                }
                insect.mIsIntersect = false;
                if (tmp.Count > 0)
                {
                    insect.mIsIntersect = true;
                    insect.mHitGlobalPoint.mPointArray = tmp;
                }
            }
            return(insect.mIsIntersect);
        }
Esempio n. 10
0
 public static bool IsAABBInsectSegment3(Vector3 min, Vector3 max, Vector3 seg1, Vector3 seg2, ref GeoInsectPointArrayInfo insect)
 {
     return(GeoSegmentUtils.IsSegmentInsectAABB3(seg1, seg2, min, max, ref insect));
 }
Esempio n. 11
0
 public static bool IsRayInsectSegment3(Vector3 rayOrigin, Vector3 rayDirection, Vector3 seg1, Vector3 seg2, ref GeoInsectPointInfo insect)
 {
     return(GeoSegmentUtils.IsSegmentInsectRay3(seg1, seg2, rayOrigin, rayDirection, ref insect));
 }
 public static bool IsTriangleInsectSegment3(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 seg1, Vector3 seg2, ref GeoInsectPointArrayInfo insect)
 {
     return(GeoSegmentUtils.IsSegmentInsectTriangle3(seg1, seg2, p1, p2, p3, ref insect));
 }
        public static bool IsTriangleInsectTriangle3(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, Vector3 p5, Vector3 p6, ref GeoInsectPointArrayInfo insect)
        {
            /*
             * // 外面进行 剔除 处理
             * Vector3 min1 = Vector3.Min(p1, p2);
             * min1 = Vector3.Min(min1, p3);
             * Vector3 max1 = Vector3.Max(p1, p2);
             * max1 = Vector3.Max(max1, p3);
             * Vector3 min2 = Vector3.Min(p4, p5);
             * min2 = Vector3.Min(min2, p6);
             * Vector3 max2 = Vector3.Max(p4, p5);
             * max2 = Vector3.Max(max2, p6);
             * if (!GeoAABBUtils.IsAABBInsectAABB3(min1, max1, min2, max2))
             * {
             *  return false;
             * }
             */
            // 417
            Vector3 p12     = p2 - p1;
            Vector3 p13     = p3 - p1;
            Vector3 normal1 = Vector3.Cross(p12, p13);
            Vector3 p45     = p5 - p4;
            Vector3 p46     = p6 - p4;
            Vector3 normal2 = Vector3.Cross(p45, p46);

            normal1.Normalize();
            normal2.Normalize();
            float dot = Vector3.Dot(normal1, normal2);

            if (1 - Mathf.Abs(dot) < 1e-5f) // 共面
            {
                return(IsTriangleInsectTrianglePlane2(p1, p2, p3, p4, p5, p6, ref insect));
            }
            float d1 = -Vector3.Dot(normal1, p1);
            float d2 = -Vector3.Dot(normal2, p4);
            GeoInsectPointArrayInfo temp1 = new GeoInsectPointArrayInfo();

            if (GeoPlaneUtils.IsPlaneInsectTriangle(normal1, d1, p4, p5, p6, ref temp1))
            {
                GeoInsectPointArrayInfo temp2 = new GeoInsectPointArrayInfo();
                if (GeoPlaneUtils.IsPlaneInsectTriangle(normal2, d2, p1, p2, p3, ref temp2))
                {
                    int count1 = temp1.mHitGlobalPoint.mPointArray.Count;
                    int count2 = temp2.mHitGlobalPoint.mPointArray.Count;
                    if (count1 == 1 && count2 == 1)
                    {
                        bool isEq = temp1.mHitGlobalPoint.mPointArray[0] == temp2.mHitGlobalPoint.mPointArray[0];
                        if (isEq)
                        {
                            insect.mIsIntersect = true;
                            insect.mHitGlobalPoint.mPointArray.Add(temp1.mHitGlobalPoint.mPointArray[0]);
                            return(true);
                        }
                    }
                    else if (count1 == 2 && count2 == 2)
                    {
                        Vector3 s1 = temp1.mHitGlobalPoint.mPointArray[0];
                        Vector3 s2 = temp1.mHitGlobalPoint.mPointArray[1];
                        Vector3 s3 = temp2.mHitGlobalPoint.mPointArray[0];
                        Vector3 s4 = temp2.mHitGlobalPoint.mPointArray[1];
                        GeoSegmentUtils.IsSegmentOverlapSegment3(s1, s2, s3, s4, ref insect);
                    }
                }
            }
            return(insect.mIsIntersect);
        }