public bool Intersects(BoundingBox box) { if (box.Min.X >= Max.X || box.Max.X <= Min.X) { return false; } if (box.Min.Y >= Max.Y || box.Max.Y <= Min.Y) { return false; } if (box.Min.Z >= Max.Z || box.Max.Z <= Min.Z) { return false; } return true; }
public double? Intersects(BoundingBox boundingBox) { double tNear = int.MinValue; double tFar = int.MaxValue; #region - X - if (Direction.X == 0 && Origin.X < boundingBox.Min.X && Origin.X > boundingBox.Max.X) { // Ray is paralell and outside planes return null; } else { double t1 = (boundingBox.Min.X - Origin.X) / Direction.X; double t2 = (boundingBox.Max.X - Origin.X) / Direction.X; if (t1 > t2) { MathHelper.Swap(ref t1, ref t2); } if (t1 > tNear) { tNear = t1; } if (t2 < tFar) { tFar = t2; } if (tNear > tFar || tFar < 0) { return null; } } #endregion #region - Y - if (Direction.Y == 0 && Origin.Y < boundingBox.Min.Y && Origin.Y > boundingBox.Max.Y) { // Ray is paralell and outside planes return null; } else { double t1 = (boundingBox.Min.Y - Origin.Y) / Direction.Y; double t2 = (boundingBox.Max.Y - Origin.Y) / Direction.Y; if (t1 > t2) { MathHelper.Swap(ref t1, ref t2); } if (t1 > tNear) { tNear = t1; } if (t2 < tFar) { tFar = t2; } if (tNear > tFar || tFar < 0) { return null; } } #endregion #region - Z - if (Direction.Z == 0 && Origin.Z < boundingBox.Min.Z && Origin.Z > boundingBox.Max.Z) { // Ray is paralell and outside planes return null; } else { double t1 = (boundingBox.Min.Z - Origin.Z) / Direction.Z; double t2 = (boundingBox.Max.Z - Origin.Z) / Direction.Z; if (t1 > t2) { MathHelper.Swap(ref t1, ref t2); } if (t1 > tNear) { tNear = t1; } if (t2 < tFar) { tFar = t2; } if (tNear > tFar || tFar < 0) { return null; } } #endregion return tNear; }
public double IntersectionVolume(BoundingBox box) { double intersectionVolume = 1; if (!Intersects(box)) { return 0; } List<double> points = new List<double>(); points.Add(Min.X); points.Add(box.Min.X); points.Add(Max.X); points.Add(box.Max.X); points.Sort(); intersectionVolume *= points[2] - points[1]; points.Clear(); points.Add(Min.Y); points.Add(box.Min.Y); points.Add(Max.Y); points.Add(box.Max.Y); points.Sort(); intersectionVolume *= points[2] - points[1]; points.Clear(); points.Add(Min.Z); points.Add(box.Min.Z); points.Add(Max.Z); points.Add(box.Max.Z); points.Sort(); intersectionVolume *= points[2] - points[1]; return intersectionVolume; }