Example #1
0
        public static bool IsAABBInsectTriangle2Plane(Vector3 min, Vector3 max, GeoPlane plane, Vector3 p1, Vector3 p2, Vector3 p3, ref GeoInsectPointArrayInfo insect)
        {
            Vector2 p11, p21, p31;

            GeoPlaneUtils.PlaneTransformLocalTriangle(p1, p2, p3, plane, out p11, out p21, out p31);
            Vector3 vmin     = plane.TransformToLocal(min);
            Vector3 vmax     = plane.TransformToLocal(max);
            bool    isInsect = IsAABBInsectTriangle2(vmin, vmax, p11, p21, p31, ref insect);

            if (isInsect)
            {
                insect.mIsIntersect = true;
                insect.mHitGlobalPoint.mPointArray = GeoPlaneUtils.PlaneTransformGlobal(insect.mHitGlobalPoint.mPointArray, plane);
            }
            return(insect.mIsIntersect);
        }
Example #2
0
        public static float PointClosest2Triangle3(Vector3 p1, Vector3 p2, Vector3 p3, ref Vector3 p, ref Vector3 close)
        {
            GeoPlane plane = new GeoPlane(p1, p2, p3);
            float    d     = GeoPlaneUtils.PointClosestToPlaneAbs(plane.mNormal, plane.mD, p, ref close);

            if (GeoTriangleUtils.IsPointInTriangle3(p1, p2, p3, ref close))
            {
                return(d);
            }
            else
            {
                Vector3 close1 = new Vector3();
                float   dt     = PointClosest2PlaneTriangle2(p1, p2, p3, plane, ref p, ref close1);
                return((float)Math.Sqrt(d * d + dt * dt));
            }
        }
Example #3
0
        public static GeoPointsArray3 BuildConvexHull(GeoPointsArray3 points)
        {
            points.Distinct();
            GeoPlane plane = new GeoPlane(Vector3.zero, 0);

            if (IsOnSamePlane(points, ref plane))
            {
                GeoPointsArray2 point2 = new GeoPointsArray2(GeoPlaneUtils.PlaneTransformLocal(points.mPointArray, plane));
                point2 = BuildConvexHull(point2);
                return(new GeoPointsArray3(GeoPlaneUtils.PlaneTransformGlobal(point2.mPointArray, plane)));
            }
            else
            {
                return(new GeoPointsArray3(QuickHull.BuildHull(points)));
            }
        }
Example #4
0
        public static float PointClosest2PlaneTriangle2(Vector3 p1, Vector3 p2, Vector3 p3, GeoPlane plane, ref Vector3 p, ref Vector3 close)
        {
            Vector2 p11, p21, p31, pt;

            GeoPlaneUtils.PlaneTransformLocalTriangle(p1, p2, p3, plane, out p11, out p21, out p31);
            Vector3 pp = plane.TransformToLocal(p);

            pt = new Vector2(pp.x, pp.z);
            Vector2 close1 = new Vector2();
            float   d      = PointClosest2Triangle2(p11, p21, p31, ref pt, ref close1);

            close.x = close1.x;
            close.y = 0;
            close.z = close1.y;
            close   = plane.TransformToGlobal(close);
            return(d);
        }
Example #5
0
        public static bool IsPlaneInsectAABB2(Vector3 normal, float d, Vector3 min, Vector3 max, GeoPlane plane, ref GeoInsectPointArrayInfo insect)
        {
            float dot = Vector3.Dot(normal, plane.mNormal);

            if (1 - Mathf.Abs(dot) < 1e-5f)
            {
                return(false);
            }
            GeoInsectPointArrayInfo tmp = new GeoInsectPointArrayInfo();
            bool isInsect = GeoPlaneUtils.IsPlaneInsectPlane(normal, d, plane.mNormal, plane.mD, ref tmp);

            if (isInsect)
            {
                Vector3 l1 = tmp.mHitGlobalPoint.mPointArray[0];
                Vector3 l2 = l1 + tmp.mHitGlobalPoint.mPointArray[1];
                isInsect = GeoLineUtils.IsLineInsectAABBPlane2(l1, l2, min, max, plane, ref insect);
            }
            return(insect.mIsIntersect);
        }
Example #6
0
        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);
        }
Example #7
0
        public static bool IsPointInAABB2Plane(Vector3 min, Vector3 max, GeoPlane plane, ref Vector3 p)
        {
            if (!GeoPlaneUtils.IsPointOnPlane(plane.mNormal, plane.mD, p))
            {
                return(false);
            }
            Vector3 size = max - min;
            float   sx   = Vector3.Dot(size, plane.mAxisX);
            float   sz   = Vector3.Dot(size, plane.mAxisZ);
            Vector3 pc   = p - min;
            float   d1   = Vector3.Dot(pc, plane.mAxisX);
            float   d2   = Vector3.Dot(pc, plane.mAxisZ);

            if (d1 < sx && d2 < sz)
            {
                return(true);
            }
            return(false);
        }
Example #8
0
        public static bool IsLineInsectCircle3(Vector3 line1, Vector3 line2, Vector3 center, float r, GeoPlane plane, ref GeoInsectPointArrayInfo insect)
        {
            if (GeoPlaneUtils.IsPointOnPlane(plane.mNormal, plane.mD, line1) && GeoPlaneUtils.IsPointOnPlane(plane.mNormal, plane.mD, line2))
            {
                return(IsLineInsectCirclePlane2(line1, line2, center, r, plane, ref insect));
            }
            GeoInsectPointInfo info = new GeoInsectPointInfo();
            bool isInsect           = GeoPlaneUtils.IsPlaneInsectLine(plane.mNormal, plane.mD, line1, line2, ref info);

            if (isInsect)
            {
                if (GeoCircleUtils.IsInSphere(center, r, info.mHitGlobalPoint))
                {
                    insect.mIsIntersect = true;
                    insect.mHitGlobalPoint.mPointArray.Add(info.mHitGlobalPoint);
                    return(true);
                }
                insect.mIsIntersect = false;
            }
            return(false);
        }
Example #9
0
        // 两三角面 共面的情况
        private static bool IsTriangleInsectTrianglePlane2(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, Vector3 p5, Vector3 p6, ref GeoInsectPointArrayInfo insect)
        {
            GeoPlane plane = GeoPlaneUtils.CreateFromTriangle(p1, p2, p3);
            Vector2  p11, p21, p31, p41, p51, p61;

            GeoPlaneUtils.PlaneTransformLocalTriangle(p1, p2, p3, plane, out p11, out p21, out p31);
            GeoPlaneUtils.PlaneTransformLocalTriangle(p4, p5, p6, plane, out p41, out p51, out p61);
            GeoInsectPointArrayInfo tmp = new GeoInsectPointArrayInfo();
            bool isInsect = IsTriangleInsectTriangle2(p11, p21, p31, p41, p51, p61, ref tmp);

            if (isInsect)
            {
                foreach (Vector3 v in tmp.mHitGlobalPoint.mPointArray)
                {
                    Vector3 vt = new Vector3(v.x, 0.0f, v.z);
                    vt = plane.TransformToGlobal(vt);
                    insect.mHitGlobalPoint.mPointArray.Add(vt);
                }
                insect.mIsIntersect = insect.mHitGlobalPoint.mPointArray.Count > 0;
            }
            return(insect.mIsIntersect);
        }
Example #10
0
        public static bool IsCircleInsectCircle3(Vector3 center1, float r1, GeoPlane plane1, Vector3 center2, float r2, GeoPlane plane2, ref GeoInsectPointArrayInfo insect)
        {
            float dot = Mathf.Abs(Vector3.Dot(plane1.mNormal, plane2.mNormal));

            if (1 - dot < 1e-5f)
            {
                if (GeoPlaneUtils.IsPointOnPlane(plane2.mNormal, plane2.mD, center1)) // 共面
                {
                    return(IsCircleInsectCirclePlane2(center1, r1, center2, r2, plane2, ref insect));
                }
                return(false);
            }
            GeoInsectPointArrayInfo tmp = new GeoInsectPointArrayInfo();
            bool isInsect = GeoPlaneUtils.IsPlaneInsectCircle(plane1.mNormal, plane1.mD, center2, r2, plane2, ref tmp);

            if (isInsect)
            {
                Vector3 lin1 = tmp.mHitGlobalPoint.mPointArray[0];
                Vector3 lin2 = lin1 + tmp.mHitGlobalPoint.mPointArray[1];
                GeoLineUtils.IsLineInsectCirclePlane2(lin1, lin2, center1, r1, plane1, ref insect);
            }
            return(insect.mIsIntersect);
        }
Example #11
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);
        }
Example #12
0
        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);
        }
Example #13
0
 private bool IsNotFacePoints(Vector3 p)
 {
     return(!GeoPlaneUtils.IsPointOnPlane(mPlane.mNormal, mPlane.mD, p));
 }