コード例 #1
0
        public static bool IsRayInsectRay3(Vector3 rayOrigin1, Vector3 rayDirection1, Vector3 rayOrigin2, Vector3 rayDirection2, ref GeoInsectPointInfo insect)
        {
            Vector3 close1, close2;
            bool    isinsect = GeoUtils.RayRayClosestPoint(rayOrigin1, rayDirection1, rayOrigin2, rayDirection2, out close1, out close2);

            if (isinsect)
            {
                Vector3 t = close1 - close2;
                if (t.magnitude < 1e-5f)
                {
                    insect.mIsIntersect    = true;
                    insect.mHitGlobalPoint = close1;
                    return(true);
                }
            }
            return(false);
        }
コード例 #2
0
        // 线
        public static bool IsLineInsectLine2(Vector2 line1, Vector2 line2, Vector2 line3, Vector2 line4, ref GeoInsectPointInfo insect)
        {
            Vector3 line11 = new Vector3(line1.x, line1.y, 0.0f);
            Vector3 line12 = new Vector3(line2.x, line2.y, 0.0f);
            Vector3 line21 = new Vector3(line3.x, line3.y, 0.0f);
            Vector3 line22 = new Vector3(line4.x, line4.y, 0.0f);

            return(IsLineInsectLine3(line11, line12, line21, line22, ref insect));
        }
コード例 #3
0
        public static bool IsRayInsectRay2(Vector2 rayOrigin1, Vector2 rayDirection1, Vector2 rayOrigin2, Vector2 rayDirection2, ref GeoInsectPointInfo insect)
        {
            Vector3 rayOrigin11    = GeoUtils.ToVector2(rayOrigin1);
            Vector3 rayDirection11 = GeoUtils.ToVector2(rayDirection1);
            Vector3 rayOrigin21    = GeoUtils.ToVector2(rayOrigin2);
            Vector3 rayDirection21 = GeoUtils.ToVector2(rayDirection2);

            return(IsRayInsectRay3(rayOrigin11, rayDirection11, rayOrigin21, rayDirection21, ref insect));
        }
コード例 #4
0
        public static bool IsPlaneInsectRay(Vector3 normal, float d, Vector3 origin, Vector3 direction, ref GeoInsectPointInfo insect)
        {
            float t = Vector3.Dot(normal, direction);

            if (t == 0) // 平行 或 在平面内
            {
                if (IsPointOnPlane(normal, d, origin))
                {
                    // 在平面内
                    return(false);
                }
                else
                {
                    // 平行
                    return(false);
                }
            }
            float up = -Vector3.Dot(normal, origin) + d;

            t = up / t;
            if (t > 0)
            {
                insect.mIsIntersect    = true;
                insect.mHitGlobalPoint = origin + t * direction;
            }
            return(insect.mIsIntersect);
        }
コード例 #5
0
        public static bool IsPlaneInsectLine(Vector3 normal, float d, Vector3 p1, Vector3 p2, ref GeoInsectPointInfo insect)
        {
            Vector3 direction = p2 - p1;

            direction.Normalize(); // not required
            float t = Vector3.Dot(normal, direction);

            if (t == 0) // 平行 或 在平面内
            {
                if (IsPointOnPlane(normal, d, p1))
                {
                    // 在平面内
                    return(false);
                }
                else
                {
                    // 平行
                    return(false);
                }
            }
            float up = -Vector3.Dot(normal, p1) + d;

            t = up / t;
            insect.mIsIntersect    = true;
            insect.mHitGlobalPoint = p1 + t * direction;
            return(true);
        }
コード例 #6
0
        public static bool IsSegmentInsectSegment3(Vector3 seg1, Vector3 seg2, Vector3 seg3, Vector3 seg4, ref GeoInsectPointInfo insect)
        {
            Vector3 close1, close2;
            bool    isinsect = GeoUtils.SegmentSegmentClosestPoint(seg1, seg2, seg3, seg4, out close1, out close2);

            if (isinsect)
            {
                Vector3 t = close1 - close2;
                if (t.sqrMagnitude < GeoUtils.PRECISION)
                {
                    insect.mIsIntersect    = true;
                    insect.mHitGlobalPoint = close1;
                    return(true);
                }
            }
            insect.mIsIntersect = false;
            return(false);
        }
コード例 #7
0
        public static bool IsSegmentInsectRay2(Vector2 seg1, Vector2 seg2, Vector2 rayOrigin, Vector2 rayDirection, ref GeoInsectPointInfo insect)
        {
            Vector3 seg11         = GeoUtils.ToVector3(seg1);
            Vector3 seg21         = GeoUtils.ToVector3(seg2);
            Vector3 rayOrigin1    = GeoUtils.ToVector3(rayOrigin);
            Vector3 rayDirection1 = GeoUtils.ToVector3(rayDirection);

            return(IsSegmentInsectRay3(seg11, seg21, rayOrigin1, rayDirection1, ref insect));
        }
コード例 #8
0
 public static bool IsTriangleInsectRay2(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 rayOrigin, Vector2 rayDirection, ref GeoInsectPointInfo insect)
 {
     return(GeoRayUtils.IsRayInsectLine2(rayOrigin, rayDirection, rayOrigin, rayDirection, ref insect));
 }
コード例 #9
0
 public static bool IsTriangleInsectRay3(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 rayOrigin, Vector3 rayDirection, ref GeoInsectPointInfo insect)
 {
     return(GeoRayUtils.IsRayInsectTriangle3(rayOrigin, rayDirection, p1, p2, p3, ref insect));
 }
コード例 #10
0
        public static bool IsLineInsectRay2(Vector2 line1, Vector2 line2, Vector2 rayOrigin, Vector2 rayDirection, ref GeoInsectPointInfo insect)
        {
            Vector3 line11 = new Vector3(line1.x, line1.y, 0.0f);
            Vector3 line12 = new Vector3(line2.x, line2.y, 0.0f);
            Vector3 rayO   = new Vector3(rayOrigin.x, rayOrigin.y, 0.0f);
            Vector3 rayD   = new Vector3(rayDirection.x, rayDirection.y, 0.0f);

            return(IsLineInsectRay3(line11, line12, rayO, rayD, ref insect));
        }
コード例 #11
0
        public static bool IsLineInsectRay3(Vector3 line1, Vector3 line2, Vector3 rayOrigin, Vector3 rayDirection, ref GeoInsectPointInfo insect)
        {
            Vector3 close1, close2;
            bool    issect = GeoUtils.RayLineClosestPoint(rayOrigin, rayDirection, line1, line2, out close1, out close2);

            if (issect)
            {
                Vector3 t = close1 - close2;
                if (t.sqrMagnitude < GeoUtils.PRECISION)
                {
                    insect.mHitGlobalPoint = close1;
                    insect.mIsIntersect    = true;
                    return(true);
                }
            }
            insect.mIsIntersect = false;
            return(false);
        }
コード例 #12
0
        public static bool IsLineInsectSegment3(Vector3 line1, Vector3 line2, Vector3 seg1, Vector3 seg2, ref GeoInsectPointInfo insect)
        {
            Vector3 closest1, closest2;
            bool    issect = GeoUtils.LineSegmentClosestPoint(line1, line2, seg1, seg2, out closest1, out closest2);

            if (issect)
            {
                Vector3 t = closest1 - closest2;
                if (t.sqrMagnitude < GeoUtils.PRECISION)
                {
                    insect.mHitGlobalPoint = closest1;
                    insect.mIsIntersect    = true;
                    return(true);
                }
            }
            insect.mIsIntersect = false;
            return(false);
        }
コード例 #13
0
        public static bool IsLineInsectSegment2(Vector2 line1, Vector2 line2, Vector2 seg1, Vector2 seg2, ref GeoInsectPointInfo insect)
        {
            Vector3 line11 = new Vector3(line1.x, line1.y, 0.0f);
            Vector3 line21 = new Vector3(line2.x, line2.y, 0.0f);
            Vector3 seg11  = new Vector3(seg1.x, seg1.y, 0.0f);
            Vector3 seg21  = new Vector3(seg2.x, seg2.y, 0.0f);

            return(IsLineInsectSegment3(line11, line21, seg11, seg21, ref insect));
        }
コード例 #14
0
        public static bool IsLineInsectTriangle3(Vector3 line1, Vector3 line2, Vector3 p1, Vector3 p2, Vector3 p3, ref GeoInsectPointInfo insect)
        {
            // 376 计算几何
            Vector3 d       = line2 - line1;
            Vector3 line1p1 = line1 - p1;
            Vector3 v2v1    = p2 - p1;
            Vector3 v3v1    = p3 - p1;
            Vector3 normal  = Vector3.Cross(v2v1, v3v1);
            float   dot     = Vector3.Dot(normal, d.normalized);

            if (dot < GeoUtils.PRECISION && dot > -GeoUtils.PRECISION)
            {
                // 平行或 共面
                dot = Vector3.Dot(normal, line1p1);
                if (dot < GeoUtils.PRECISION && dot > -GeoUtils.PRECISION)
                {
                    // 共面  此处应该转化为 线与线段 相交
                    // 暂时处理为 false
                    return(false);
                }
                else
                {
                    // 平行
                    return(false);
                }
            }
            Vector3 d31     = Vector3.Cross(d, v3v1);
            float   d31v2v1 = Vector3.Dot(d31, v2v1);
            float   tmp     = 1.0f / d31v2v1;
            float   t       = Vector3.Dot(Vector3.Cross(line1p1, v2v1), v3v1);
            float   u       = Vector3.Dot(Vector3.Cross(d, v3v1), line1p1);
            float   v       = Vector3.Dot(Vector3.Cross(line1p1, v2v1), d);

            if (u >= 0 && u <= 1 && v >= 0 && v <= 1 && (u + v) <= 1)
            {
                insect.mIsIntersect    = true;
                insect.mHitGlobalPoint = line1p1 + t * d;
                return(true);
            }
            return(false);
        }
コード例 #15
0
        public static bool IsSegmentInsectCircle3(Vector3 seg1, Vector3 seg2, Vector3 center, float r, GeoPlane plane, ref GeoInsectPointInfo insect)
        {
            bool isInsect = GeoPlaneUtils.IsPlaneInsectSegment(plane.mNormal, plane.mD, seg1, seg2, ref insect);

            if (isInsect)
            {
                if (GeoCircleUtils.IsInSphere(center, r, insect.mHitGlobalPoint))
                {
                    return(true);
                }
            }
            insect.mIsIntersect = isInsect;
            return(isInsect);
        }
コード例 #16
0
 public static bool IsTriangleInsectLine3(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 line1, Vector3 line2, ref GeoInsectPointInfo insect)
 {
     return(GeoLineUtils.IsLineInsectTriangle3(line1, line2, p1, p2, p3, ref insect));
 }
コード例 #17
0
        // 线段 相交 和 重叠 分开 计算 (线段重叠 不认为 相交)
        public static bool IsSegmentInsectSegment2(Vector2 seg1, Vector2 seg2, Vector2 seg3, Vector2 seg4, ref GeoInsectPointInfo insect)
        {
            Vector3 seg11 = GeoUtils.ToVector3(seg1);
            Vector3 seg21 = GeoUtils.ToVector3(seg2);
            Vector3 seg31 = GeoUtils.ToVector3(seg3);
            Vector3 seg41 = GeoUtils.ToVector3(seg4);

            return(IsSegmentInsectSegment3(seg11, seg21, seg31, seg41, ref insect));
        }
コード例 #18
0
 public static bool IsRayInsectCircle3(Vector3 rayOrigin, Vector3 rayDirection, Vector3 center, float r, GeoPlane plane, ref GeoInsectPointInfo insect)
 {
     if (GeoPlaneUtils.IsPlaneInsectRay(plane.mNormal, plane.mD, rayOrigin, rayDirection, ref insect))
     {
         if (GeoCircleUtils.IsInSphere(center, r, insect.mHitGlobalPoint))
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #19
0
 public static bool IsSegmentInsectLine3(Vector3 seg1, Vector3 seg2, Vector3 line1, Vector3 line2, ref GeoInsectPointInfo insect)
 {
     return(GeoLineUtils.IsLineInsectSegment3(line1, line2, seg1, seg2, ref insect));
 }
コード例 #20
0
        public static bool IsRayInsectTriangle3(Vector3 rayOrigin, Vector3 rayDirection, Vector3 p1, Vector3 p2, Vector3 p3, ref GeoInsectPointInfo insect)
        {
            // 376 计算几何
            Vector3 d       = rayDirection;
            Vector3 line1p1 = rayOrigin - p1;
            Vector3 v2v1    = p2 - p1;
            Vector3 v3v1    = p3 - p1;
            Vector3 normal  = Vector3.Cross(v2v1, v3v1);
            float   dot     = Vector3.Dot(normal, d.normalized);

            if (dot < 1e-5f && dot > -1e-5f)
            {
                // 平行或 共面
                dot = Vector3.Dot(normal, line1p1);
                if (dot < 1e-5f && dot > -1e-5f)
                {
                    // 共面  此处应该转化为 线与线段 相交
                    // 暂时处理为 false
                    return(false);
                }
                else
                {
                    // 平行
                    return(false);
                }
            }

            Vector3 d31     = Vector3.Cross(d, v3v1);
            float   d31v2v1 = Vector3.Dot(d31, v2v1);
            float   tmp     = 1.0f / d31v2v1;
            float   t       = Vector3.Dot(Vector3.Cross(line1p1, v2v1), v3v1) * tmp;
            float   u       = Vector3.Dot(Vector3.Cross(d, v3v1), line1p1) * tmp;
            float   v       = Vector3.Dot(Vector3.Cross(line1p1, v2v1), d) * tmp;

            if (u >= 0 && u <= 1 && v >= 0 && v <= 1 && (u + v) <= 1 && t > 0)
            {
                insect.mIsIntersect    = true;
                insect.mHitGlobalPoint = rayOrigin + t * d;
                return(true);
            }
            return(false);
        }
コード例 #21
0
        public static bool IsSegmentInsectRay3(Vector3 seg1, Vector3 seg2, Vector3 rayOrigin, Vector3 rayDirection, ref GeoInsectPointInfo insect)
        {
            Vector3 close1, close2;
            bool    isinsect = GeoUtils.RaySegmentClosestPoint(rayOrigin, rayDirection, seg1, seg2, out close1, out close2);

            if (isinsect)
            {
                Vector3 t = close1 - close2;
                if (t.sqrMagnitude < GeoUtils.PRECISION)
                {
                    insect.mIsIntersect    = true;
                    insect.mHitGlobalPoint = close1;
                    insect.mLength         = (rayOrigin - close1).magnitude;
                }
            }
            return(insect.mIsIntersect);
        }
コード例 #22
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));
 }
コード例 #23
0
        public static bool IsPlaneInsectSegment(Vector3 normal, float d, Vector3 p1, Vector3 p2, ref GeoInsectPointInfo insect)
        {
            Vector3 direction = p2 - p1;
            float   t         = Vector3.Dot(normal, direction);

            if (t == 0) // 平行 或 在平面内
            {
                if (IsPointOnPlane(normal, d, p1))
                {
                    // 在平面内
                    return(false);
                }
                else
                {
                    // 平行
                    return(false);
                }
            }
            float up = -Vector3.Dot(normal, p1) + d;

            t = up / t;
            if (t > 0 && t < 1)
            {
                insect.mIsIntersect    = true;
                insect.mHitGlobalPoint = p1 + t * direction;
            }
            return(insect.mIsIntersect);
        }
コード例 #24
0
 public static bool IsRayInsectLine3(Vector3 rayOrigin, Vector3 rayDirection, Vector3 line1, Vector3 line2, ref GeoInsectPointInfo insect)
 {
     return(GeoLineUtils.IsLineInsectRay3(line1, line2, rayOrigin, rayDirection, ref insect));
 }
コード例 #25
0
        public static bool IsPlaneInsectTriangle(Vector3 normal, float d, Vector3 p1, Vector3 p2, Vector3 p3, ref GeoInsectPointArrayInfo insect)
        {
            // 414
            List <Vector3> tri = new List <Vector3>();

            tri.Add(p1);
            tri.Add(p2);
            tri.Add(p3);
            List <Vector3> pPos = new List <Vector3>();
            List <Vector3> pOn  = new List <Vector3>();

            foreach (Vector3 p in tri)
            {
                bool p1P = IsPointInPositiveHalf(normal, d, p);
                if (p1P)
                {
                    pPos.Add(p);
                }
                else
                {
                    p1P = IsPointOnPlane(normal, d, p);
                    if (p1P)
                    {
                        pOn.Add(p);
                    }
                }
            }
            if (pPos.Count == 3 && pOn.Count == 0) // 三点都在 一侧
            {
                return(false);
            }
            if (pPos.Count == 2 && pOn.Count == 1) // 一点在上,另两点同侧
            {
                insect.mIsIntersect = true;
                insect.mHitGlobalPoint.mPointArray.Add(pOn[0]);
                return(true);
            }
            if (pPos.Count == 1 && pOn.Count == 2)
            {
                insect.mIsIntersect = true;
                insect.mHitGlobalPoint.mPointArray.Add(pOn[0]);
                insect.mHitGlobalPoint.mPointArray.Add(pOn[1]);
                return(true);
            }
            if (pPos.Count == 0 && pOn.Count == 3) // 共面
            {
                insect.mIsIntersect = true;
                insect.mHitGlobalPoint.mPointArray.Add(pOn[0]);
                insect.mHitGlobalPoint.mPointArray.Add(pOn[1]);
                insect.mHitGlobalPoint.mPointArray.Add(pOn[2]);
                return(true);
            }
            insect.mIsIntersect = true;
            GeoInsectPointInfo temp = new GeoInsectPointInfo();

            for (int i = 0; i < 3; ++i)
            {
                if (IsPlaneInsectSegment(normal, d, tri[i], tri[(i + 1) % 3], ref temp))
                {
                    insect.mHitGlobalPoint.mPointArray.Add(temp.mHitGlobalPoint);
                }
            }
            return(true);
        }
コード例 #26
0
        public static bool IsLineInsectLine3(Vector3 line1, Vector3 line2, Vector3 line3, Vector3 line4, ref GeoInsectPointInfo insect)
        {
            Vector3 closest1, closest2;
            bool    isInsect = GeoUtils.LineLineClosestPoint(line1, line2, line3, line4, out closest1, out closest2);

            if (isInsect)
            {
                Vector3 t = closest1 - closest2;
                if (t.magnitude < 1e-5f)
                {
                    insect.mHitGlobalPoint = closest1;
                    insect.mIsIntersect    = true;
                    return(true);
                }
            }
            insect.mIsIntersect = false;
            return(false);
        }