public void Contains(ref AxisAlignedBox box, out ContainmentType result) { // FIXME: Is this a bug? // If the bounding box is of W * D * H = 0, then return disjoint if (box.Min == box.Max) { result = ContainmentType.Disjoint; return; } int i; ContainmentType contained; Vector3F[] corners = box.GetCorners(); // First we assume completely disjoint. So if we find a point that is contained, we break out of this loop for (i = 0; i < corners.Length; i++) { this.Contains(ref corners[i], out contained); if (contained != ContainmentType.Disjoint) { break; } } if (i == corners.Length) // This means we checked all the corners and they were all disjoint { result = ContainmentType.Disjoint; return; } if (i != 0) // if i is not equal to zero, we can fastpath and say that this box intersects { // because we know at least one point is outside and one is inside. result = ContainmentType.Intersects; return; } // If we get here, it means the first (and only) point we checked was actually contained in the frustum. // So we assume that all other points will also be contained. If one of the points is disjoint, we can // exit immediately saying that the result is Intersects i++; for (; i < corners.Length; i++) { this.Contains(ref corners[i], out contained); if (contained != ContainmentType.Contains) { result = ContainmentType.Intersects; return; } } // If we get here, then we know all the points were actually contained, therefore result is Contains result = ContainmentType.Contains; return; }
public ContainmentType Contains(AxisAlignedBox box) { //check if all corner is in sphere bool inside = true; foreach (Vector3F corner in box.GetCorners()) { if (this.Contains(corner) == ContainmentType.Disjoint) { inside = false; break; } } if (inside) { return(ContainmentType.Contains); } //check if the distance from sphere center to cube face < radius double dmin = 0; if (Center.X < box.Min.X) { dmin += (Center.X - box.Min.X) * (Center.X - box.Min.X); } else if (Center.X > box.Max.X) { dmin += (Center.X - box.Max.X) * (Center.X - box.Max.X); } if (Center.Y < box.Min.Y) { dmin += (Center.Y - box.Min.Y) * (Center.Y - box.Min.Y); } else if (Center.Y > box.Max.Y) { dmin += (Center.Y - box.Max.Y) * (Center.Y - box.Max.Y); } if (Center.Z < box.Min.Z) { dmin += (Center.Z - box.Min.Z) * (Center.Z - box.Min.Z); } else if (Center.Z > box.Max.Z) { dmin += (Center.Z - box.Max.Z) * (Center.Z - box.Max.Z); } if (dmin <= Radius * Radius) { return(ContainmentType.Intersects); } //else disjoint return(ContainmentType.Disjoint); }