예제 #1
0
        public static bool IsRayInsectAABB3(Vector3 rayOrigin, Vector3 rayDirection, Vector3 min, Vector3 max, ref GeoInsectPointArrayInfo insect)
        {
            bool isInsect = GeoLineUtils.IsLineInsectAABB3(rayOrigin, rayDirection + rayOrigin, min, max, ref insect);

            if (isInsect)
            {
                List <Vector3> tmp  = new List <Vector3>();
                List <Vector3> tmp1 = insect.mHitGlobalPoint.mPointArray;
                for (int i = 0; i < tmp1.Count; ++i)
                {
                    Vector3 v = tmp1[i];
                    if (GeoRayUtils.IsPointInRay3(rayOrigin, rayDirection, ref v))
                    {
                        tmp.Add(v);
                    }
                }
                if (tmp.Count > 0)
                {
                    insect.mHitGlobalPoint.mPointArray = tmp;
                    insect.mHitGlobalPoint.mPointArray.Sort((v1, v2) =>
                    {
                        return((v1 - rayOrigin).sqrMagnitude.CompareTo((v2 - rayOrigin).sqrMagnitude));
                    });
                }
                else
                {
                    insect.mIsIntersect = false;
                }
            }
            return(insect.mIsIntersect);
        }
예제 #2
0
        public static bool IsRayInsectAABB2(Vector2 rayOrigin, Vector2 rayDirection, Vector2 min, Vector2 max, ref GeoInsectPointArrayInfo insect)
        {
            bool isInsect = GeoLineUtils.IsLineInsectAABB2(rayOrigin, rayDirection + rayOrigin, 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 (GeoRayUtils.IsPointInRay2(rayOrigin, rayDirection, ref v2))
                    {
                        tmp.Add(v);
                    }
                }
                if (tmp.Count > 0)
                {
                    insect.mHitGlobalPoint.mPointArray = tmp;
                }
                else
                {
                    insect.mIsIntersect = false;
                }
            }
            return(insect.mIsIntersect);
        }
예제 #3
0
        public float IsRayIntersect(Vector2 pos, Vector2 dir)
        {
            float d = 1e10f;
            GeoInsectPointInfo info = new GeoInsectPointInfo();

            if (GeoRayUtils.IsRayInsectSegment2(pos, dir, mP1, mP2, ref info))
            {
                d = info.mLength;
            }
            info = new GeoInsectPointInfo();
            if (GeoRayUtils.IsRayInsectSegment2(pos, dir, mP3, mP2, ref info))
            {
                d = Math.Min(d, info.mLength);
            }
            info = new GeoInsectPointInfo();
            if (GeoRayUtils.IsRayInsectSegment2(pos, dir, mP1, mP4, ref info))
            {
                d = Math.Min(d, info.mLength);
            }
            info = new GeoInsectPointInfo();
            if (GeoRayUtils.IsRayInsectSegment2(pos, dir, mP3, mP4, ref info))
            {
                d = Math.Min(d, info.mLength);
            }
            return(d);
        }
예제 #4
0
        public bool IsIntersect(ref GeoRay2 dist, ref GeoInsectPointArrayInfo insect)
        {
            bool isInsect = GeoRayUtils.IsRayInsectAABB2(dist.mOrigin, dist.mDirection, mAABB2.mMin, mAABB2.mMax, ref insect);

            if (isInsect)
            {
                insect.mHitObject2 = this;
                insect.mLength     = (GeoUtils.ToVector2(insect.mHitGlobalPoint.mPointArray[0]) - dist.mOrigin).magnitude;
            }
            return(isInsect);
        }
예제 #5
0
        public bool IsIntersect(ref GeoRay3 dist, ref GeoInsectPointArrayInfo insect)
        {
            GeoInsectPointInfo info = new GeoInsectPointInfo();
            bool isInsect           = GeoRayUtils.IsRayInsectTriangle3(dist.mOrigin, dist.mDirection, mP1, mP2, mP3, ref info);

            if (isInsect)
            {
                insect.mHitObject2 = this;
                insect.mHitGlobalPoint.mPointArray.Add(info.mHitGlobalPoint);
                insect.mLength = (info.mHitGlobalPoint - dist.mOrigin).magnitude;
            }
            return(isInsect);
        }
예제 #6
0
        public bool IsIntersect(ref GeoRay2 dist, ref GeoInsectPointArrayInfo insect)
        {
            GeoInsectPointInfo info = new GeoInsectPointInfo();
            bool isInsect           = GeoRayUtils.IsRayInsectSegment2(dist.mOrigin, dist.mDirection, mSeg.mP1, mSeg.mP2, ref info);

            insect.mIsIntersect = isInsect;
            if (isInsect)
            {
                insect.mHitObject2 = this;
                insect.mHitGlobalPoint.mPointArray.Add(info.mHitGlobalPoint);
                insect.mLength = (GeoUtils.ToVector2(info.mHitGlobalPoint) - dist.mOrigin).magnitude;
            }
            return(isInsect);
        }
예제 #7
0
        public bool IsIntersect(ref GeoRay2 dist, ref GeoInsectPointArrayInfo insect)
        {
            bool isInsect = GeoRayUtils.IsRayInsectTriangle2(dist.mOrigin, dist.mDirection, mP1, mP2, mP3, ref insect);

            if (isInsect)
            {
                insect.mHitObject2 = this;
                float min = 1e5f;
                foreach (Vector3 v in insect.mHitGlobalPoint.mPointArray)
                {
                    float len = (GeoUtils.ToVector2(v) - dist.mOrigin).magnitude;
                    if (len < min)
                    {
                        min = len;
                    }
                }
                insect.mLength = min;
            }
            return(isInsect);
        }
예제 #8
0
        public bool GetIntersection(GeoRay2 ray, ref GeoInsectPointArrayInfo intersection, bool occlusion)
        {
            if (mFlatTreeList.Count == 0)
            {
                return(false);
            }
            intersection.mIsIntersect = false;
            intersection.mLength      = 999999999.0f;
            intersection.mHitObject2  = null;
            int closer, other;

            BVHTraversal[] todo = new BVHTraversal[64];
            todo[0] = new BVHTraversal();
            int stackptr = 0;

            todo[stackptr].mIndex  = 0;
            todo[stackptr].mLength = -9999999.0f;
            while (stackptr >= 0)
            {
                int   ni   = todo[stackptr].mIndex;
                float near = todo[stackptr].mLength;
                stackptr--;
                BVHFlatNode2 node = mFlatTreeList[ni];
                // 对叶节点做相交测试
                if (node.mRightOffset == 0)
                {
                    bool hit = false;
                    for (int o = 0; o < node.mLeafCount; ++o)
                    {
                        GeoInsectPointArrayInfo current = new GeoInsectPointArrayInfo();
                        BVHObject2 obj = mBuildPrims[(int)node.mStartIndex + o];
                        hit = obj.IsIntersect(ref ray, ref current);
                        if (hit)
                        {
                            if (occlusion)
                            {
                                intersection = current;
                                return(true);
                            }
                            if (current.mLength < intersection.mLength)
                            {
                                intersection = current;
                            }
                        }
                    }
                }
                else
                {
                    closer = ni + 1;
                    other  = ni + (int)node.mRightOffset;
                    // 对父结点做测试
                    GeoInsectPointArrayInfo in1 = new GeoInsectPointArrayInfo();
                    GeoInsectPointArrayInfo in2 = new GeoInsectPointArrayInfo();
                    bool hitc0 = GeoRayUtils.IsRayInsectAABB2(ray.mOrigin, ray.mDirection, mFlatTreeList[closer].mBox.mMin, mFlatTreeList[closer].mBox.mMax, ref in1);
                    bool hitc1 = GeoRayUtils.IsRayInsectAABB2(ray.mOrigin, ray.mDirection, mFlatTreeList[other].mBox.mMin, mFlatTreeList[other].mBox.mMax, ref in2);

                    if (hitc0 && hitc1)
                    {
                        float l0 = (GeoUtils.ToVector2(in1.mHitGlobalPoint[0]) - ray.mOrigin).magnitude;
                        float l2 = (GeoUtils.ToVector2(in2.mHitGlobalPoint[0]) - ray.mOrigin).magnitude;
                        if (l2 < l0)
                        {
                            float temp = l0;
                            l0 = l2;
                            l2 = temp;
                            int itemp = closer;
                            closer = other;
                            other  = itemp;
                        }
                        todo[++stackptr] = new BVHTraversal(other, l2);
                        todo[++stackptr] = new BVHTraversal(closer, l0);
                    }
                    else if (hitc0)
                    {
                        float l0 = (GeoUtils.ToVector2(in1.mHitGlobalPoint[0]) - ray.mOrigin).magnitude;
                        todo[++stackptr] = new BVHTraversal(closer, l0);
                    }
                    else if (hitc1)
                    {
                        float l2 = (GeoUtils.ToVector2(in2.mHitGlobalPoint[0]) - ray.mOrigin).magnitude;
                        todo[++stackptr] = new BVHTraversal(other, l2);
                    }
                }
            }
            if (intersection.mHitObject2 != null)
            {
                intersection.mHitGlobalPoint.Clear();
                intersection.mHitGlobalPoint.Add(ray.mOrigin + ray.mDirection * intersection.mLength);
            }
            return(intersection.mHitObject2 != null);
        }
예제 #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 IsTriangleInsectRay2(Vector2 p1, Vector2 p2, Vector2 p3, Vector2 rayOrigin, Vector2 rayDirection, ref GeoInsectPointInfo insect)
 {
     return(GeoRayUtils.IsRayInsectLine2(rayOrigin, rayDirection, rayOrigin, rayDirection, ref insect));
 }
예제 #11
0
 public static bool IsAABBInsectRay3(Vector3 min, Vector3 max, Vector3 rayOrigin, Vector3 rayDirection, ref GeoInsectPointArrayInfo insect)
 {
     return(GeoRayUtils.IsRayInsectAABB3(rayOrigin, rayDirection, min, max, ref insect));
 }