/// <summary> /// Intersection test with an <see cref="AxisAlignedBox2"/>. /// </summary> /// <remarks> /// May return false positives but will never miss an intersection. /// </remarks> /// <param name="box">Box to test.</param> /// <returns>True if interesecting, false otherwise.</returns> public bool Intersects(AxisAlignedBox3 box) { if (box.IsNull) { return(false); } if (box.IsInfinite) { return(true); } // Get centre of the box Vector3 center = box.Center; // Get the half-size of the box Vector3 halfSize = box.HalfSize; // If all points are on outside of any plane, we fail Vector3[] points = box.Corners; for (int i = 0; i < planes.Count; i++) { Plane plane = (Plane)planes[i]; PlaneSide side = plane.GetSide(center, halfSize); if (side == outside) { // Found a splitting plane therefore return not intersecting return(false); } } // couldn't find a splitting plane, assume intersecting return(true); }
/// <summary> /// Returns the side where the aligneBox is. the flag Both indicates an intersecting box. /// one corner ON the plane is sufficient to consider the box and the plane intersecting. /// </summary> /// <param name="box"></param> /// <returns></returns> public PlaneSide GetSide(AxisAlignedBox3 box) { if (box.IsNull) { return(PlaneSide.None); } if (box.IsInfinite) { return(PlaneSide.Both); } return(this.GetSide(box.Center, box.HalfSize)); }
/// <summary> /// Returns whether or not this box intersects another. /// </summary> /// <param name="box2"></param> /// <returns>True if the 2 boxes intersect, false otherwise.</returns> public bool Intersects(AxisAlignedBox3 box2) { // Early-fail for nulls if (this.IsNull || box2.IsNull) { return(false); } if (this.IsInfinite || box2.IsInfinite) { return(true); } // Use up to 6 separating planes if (this.maxVector.X <= box2.minVector.X) { return(false); } if (this.maxVector.Y <= box2.minVector.Y) { return(false); } if (this.maxVector.Z <= box2.minVector.Z) { return(false); } if (this.minVector.X >= box2.maxVector.X) { return(false); } if (this.minVector.Y >= box2.maxVector.Y) { return(false); } if (this.minVector.Z >= box2.maxVector.Z) { return(false); } // otherwise, must be intersecting return(true); }
/// <summary> /// Allows for merging two boxes together (combining). /// </summary> /// <param name="box">Source box.</param> public void Merge(AxisAlignedBox3 box) { if (box.IsNull) { // nothing to merge with in this case, just return return; } else if (box.IsInfinite) { this.IsInfinite = true; } else if (this.IsNull) { SetExtents(box.Minimum, box.Maximum); } else if (!this.IsInfinite) { minVector.Floor(box.Minimum); maxVector.Ceil(box.Maximum); UpdateCorners(); } }
public AxisAlignedBox3(AxisAlignedBox3 box) { SetExtents(box.Minimum, box.Maximum); isNull = box.IsNull; isInfinite = box.IsInfinite; }
/// <summary> /// Calculate the area of intersection of this box and another /// </summary> public AxisAlignedBox3 Intersection(AxisAlignedBox3 b2) { if (!Intersects(b2)) { return(new AxisAlignedBox3()); } Vector3 intMin = Vector3.Zero; Vector3 intMax = Vector3.Zero; Vector3 b2max = b2.maxVector; Vector3 b2min = b2.minVector; if (b2max.X > maxVector.X && maxVector.X > b2min.X) { intMax.X = maxVector.X; } else { intMax.X = b2max.X; } if (b2max.Y > maxVector.Y && maxVector.Y > b2min.Y) { intMax.Y = maxVector.Y; } else { intMax.Y = b2max.Y; } if (b2max.Z > maxVector.Z && maxVector.Z > b2min.Z) { intMax.Z = maxVector.Z; } else { intMax.Z = b2max.Z; } if (b2min.X < minVector.X && minVector.X < b2max.X) { intMin.X = minVector.X; } else { intMin.X = b2min.X; } if (b2min.Y < minVector.Y && minVector.Y < b2max.Y) { intMin.Y = minVector.Y; } else { intMin.Y = b2min.Y; } if (b2min.Z < minVector.Z && minVector.Z < b2max.Z) { intMin.Z = minVector.Z; } else { intMin.Z = b2min.Z; } return(new AxisAlignedBox3(intMin, intMax)); }