示例#1
0
        /// <summary>
        /// Tests whether the given point contained by this box.
        /// </summary>

        public bool Contains(AxisAlignedBox other)
        {
            if (other.IsNull || IsInfinite)
            {
                return(true);
            }
            if (IsNull || other.IsInfinite)
            {
                return(false);
            }

            if (_minimum.x <= other._minimum.x &&
                _minimum.y <= other._minimum.y &&
                _minimum.z <= other._minimum.z &&
                other._maximum.x <= _maximum.x &&
                other._maximum.y <= _maximum.y &&
                other._maximum.z <= _maximum.z)
            {
                return(true);
            }

            return(false);
        }
示例#2
0
 /// <summary>
 /// Merges the passed in box into the current box. The result is the box which encompasses both.
 /// </summary>
 public void Merge(AxisAlignedBox rhs)
 {
     if (rhs._extent != Extent.Null &&
         _extent != Extent.Infinite)
     {
         if (rhs._extent == Extent.Infinite)
         {
             _extent = Extent.Infinite;
         }
         else if (_extent == Extent.Null)
         {
             SetExtents(rhs._minimum, rhs._maximum);
         }
         else
         {
             Vector3 min = _minimum;
             Vector3 max = _maximum;
             max.MakeCeil(rhs._maximum);
             min.MakeFloor(rhs._minimum);
             SetExtents(min, max);
         }
     }
 }
示例#3
0
        public static bool Intersects(Plane plane, AxisAlignedBox box)
        {
            if (box.IsNull)
            {
                return(false);
            }

            // Get corners of the box
            var corners = box.GetAllCorners();

            // Test which side of the plane the corners are
            // Intersection occurs when at least one corner is on the
            // opposite side to another
            Plane.Side lastSide = plane.GetSide(corners[0]);
            for (int corner = 1; corner < 8; ++corner)
            {
                if (plane.GetSide(corners[corner]) != lastSide)
                {
                    return(true);
                }
            }

            return(false);
        }
示例#4
0
 /// <summary>Tests whether this ray intersects the given plane. A pair structure where the first element indicates whether an intersection occurs, and if true, the second element will indicate the distance along the ray at which it intersects. This can be converted to a point in space by calling getPoint(). </summary>
 public Tuple <bool, float> Intersects(AxisAlignedBox box)
 {
     return(Math.Intersects(this, box));
 }
示例#5
0
        /// <summary>Ray / box intersection, returns boolean result and distance. </summary>
        public static Tuple <bool, float> Intersects(Ray ray, AxisAlignedBox box)
        {
            if (box.IsNull)
            {
                return(Tuple.Create(false, 0.0f));
            }

            float   lowt = 0.0f;
            float   t;
            bool    hit = false;
            Vector3 hitpoint;
            Vector3 min     = box.Minimum;
            Vector3 max     = box.Maximum;
            Vector3 rayorig = ray.Origin;
            Vector3 raydir  = ray.Direction;

            // Check origin inside first
            if (rayorig > min && rayorig < max)
            {
                return(Tuple.Create(true, 0.0f));
            }

            // Check each face in turn, only check closest 3
            // Min x
            if (rayorig.x < min.x && raydir.x > 0)
            {
                t = (min.x - rayorig.x) / raydir.x;
                if (t > 0)
                {
                    // Substitute t back into ray and check bounds and dist
                    hitpoint = rayorig + raydir * t;
                    if (hitpoint.y >= min.y && hitpoint.y <= max.y &&
                        hitpoint.z >= min.z && hitpoint.z <= max.z &&
                        (!hit || t < lowt))
                    {
                        hit  = true;
                        lowt = t;
                    }
                }
            }
            // Max x
            if (rayorig.x > max.x && raydir.x < 0)
            {
                t = (max.x - rayorig.x) / raydir.x;
                if (t > 0)
                {
                    // Substitute t back into ray and check bounds and dist
                    hitpoint = rayorig + raydir * t;
                    if (hitpoint.y >= min.y && hitpoint.y <= max.y &&
                        hitpoint.z >= min.z && hitpoint.z <= max.z &&
                        (!hit || t < lowt))
                    {
                        hit  = true;
                        lowt = t;
                    }
                }
            }
            // Min y
            if (rayorig.y < min.y && raydir.y > 0)
            {
                t = (min.y - rayorig.y) / raydir.y;
                if (t > 0)
                {
                    // Substitute t back into ray and check bounds and dist
                    hitpoint = rayorig + raydir * t;
                    if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
                        hitpoint.z >= min.z && hitpoint.z <= max.z &&
                        (!hit || t < lowt))
                    {
                        hit  = true;
                        lowt = t;
                    }
                }
            }
            // Max y
            if (rayorig.y > max.y && raydir.y < 0)
            {
                t = (max.y - rayorig.y) / raydir.y;
                if (t > 0)
                {
                    // Substitute t back into ray and check bounds and dist
                    hitpoint = rayorig + raydir * t;
                    if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
                        hitpoint.z >= min.z && hitpoint.z <= max.z &&
                        (!hit || t < lowt))
                    {
                        hit  = true;
                        lowt = t;
                    }
                }
            }
            // Min z
            if (rayorig.z < min.z && raydir.z > 0)
            {
                t = (min.z - rayorig.z) / raydir.z;
                if (t > 0)
                {
                    // Substitute t back into ray and check bounds and dist
                    hitpoint = rayorig + raydir * t;
                    if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
                        hitpoint.y >= min.y && hitpoint.y <= max.y &&
                        (!hit || t < lowt))
                    {
                        hit  = true;
                        lowt = t;
                    }
                }
            }
            // Max z
            if (rayorig.z > max.z && raydir.z < 0)
            {
                t = (max.z - rayorig.z) / raydir.z;
                if (t > 0)
                {
                    // Substitute t back into ray and check bounds and dist
                    hitpoint = rayorig + raydir * t;
                    if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
                        hitpoint.y >= min.y && hitpoint.y <= max.y &&
                        (!hit || t < lowt))
                    {
                        hit  = true;
                        lowt = t;
                    }
                }
            }

            return(Tuple.Create(hit, lowt));
        }
示例#6
0
 public Aabb(AxisAlignedBox box)
 {
     Center   = box.Center;
     HalfSize = box.HalfSize;
 }