Ejemplo n.º 1
0
 public bool Equals(CSGPlane other)
 {
     if (System.Object.ReferenceEquals(this, other))
     {
         return(true);
     }
     if (System.Object.ReferenceEquals(other, null))
     {
         return(false);
     }
     return(Mathf.Abs(this.Distance(other.pointOnPlane)) <= MathConstants.DistanceEpsilon &&
            Mathf.Abs(other.Distance(this.pointOnPlane)) <= MathConstants.DistanceEpsilon &&
            Mathf.Abs(a - other.a) <= MathConstants.NormalEpsilon &&
            Mathf.Abs(b - other.b) <= MathConstants.NormalEpsilon &&
            Mathf.Abs(c - other.c) <= MathConstants.NormalEpsilon);
 }
Ejemplo n.º 2
0
        public static PlaneAlignment TryIntersection(CSGPlane inPlane1, CSGPlane inPlane2, out Ray intersectionRay)
        {
            var lineDirection = Vector3.Cross(inPlane1.normal, inPlane2.normal);
            var ax            = Mathf.Abs(lineDirection.x);
            var ay            = Mathf.Abs(lineDirection.y);
            var az            = Mathf.Abs(lineDirection.z);
            var aProduct      = ax + ay + az;

            if (float.IsNaN(aProduct) || float.IsInfinity(aProduct))
            {
                intersectionRay = MathConstants.emptyRay;
                return(PlaneAlignment.Invalid);
            }

            if (aProduct < MathConstants.NormalEpsilon)
            {
                intersectionRay = MathConstants.emptyRay;

                var isAligned = Mathf.Sign(Vector3.Dot(lineDirection, inPlane1.normal)) == Mathf.Sign(Vector3.Dot(lineDirection, inPlane2.normal));
                var distance  = inPlane1.Distance(inPlane2.pointOnPlane);
                if (isAligned)
                {
                    // inPlane1 and inPlane2 are both facing in the same direction
                    if (distance > MathConstants.DistanceEpsilon)
                    {
                        return(PlaneAlignment.DisjointAndInFront);                                      // inPlane1 is in front of inPlane2
                    }
                    if (distance < MathConstants.DistanceEpsilon)
                    {
                        return(PlaneAlignment.DisjointAndBehind);                                   // inPlane1 is behind inPlane2
                    }
                    return(PlaneAlignment.CoincidingFacingSameDirection);                           // 'this' is coinciding with 'other'
                }
                else
                {
                    // inPlane1 and inPlane2 are both facing in the opposite direction
                    if (distance > MathConstants.DistanceEpsilon)
                    {
                        return(PlaneAlignment.DisjointFacingOutwards);                                  // the planes are facing outwards
                    }
                    if (distance < MathConstants.DistanceEpsilon)
                    {
                        return(PlaneAlignment.DisjointFacingInwards);                               // the planes are facing inwards
                    }
                    return(PlaneAlignment.CoincidingFacingOppositeDirection);                       // 'this' is coinciding with 'other'
                }
            }

            var point = MathConstants.zeroVector3;

            if (ax > ay)
            {
                if (ax > az)                 // x = 0
                {
                    point.y         = (-inPlane2.d * inPlane1.c + inPlane1.d * inPlane2.c) / lineDirection.x;
                    point.z         = (-inPlane1.d * inPlane2.b + inPlane2.d * inPlane1.b) / lineDirection.x;
                    intersectionRay = new Ray(point, lineDirection);
                    return(PlaneAlignment.Intersecting);
                }
            }
            else
            {
                if (ay > az)                 // y = 0
                {
                    point.x         = (-inPlane1.d * inPlane2.c + inPlane2.d * inPlane1.c) / lineDirection.y;
                    point.z         = (-inPlane2.d * inPlane1.a + inPlane1.d * inPlane2.a) / lineDirection.y;
                    intersectionRay = new Ray(point, lineDirection);
                    return(PlaneAlignment.Intersecting);
                }
            }

            // z = 0
            {
                point.x         = (-inPlane2.d * inPlane1.b + inPlane1.d * inPlane2.b) / lineDirection.z;
                point.y         = (-inPlane1.d * inPlane2.a + inPlane2.d * inPlane1.a) / lineDirection.z;
                intersectionRay = new Ray(point, lineDirection);
                return(PlaneAlignment.Intersecting);
            }
        }