intersectBox method

Vector3? intersectBox(
  1. Box3 box,
  2. Vector3 target
)

Implementation

Vector3? intersectBox(Box3 box, Vector3 target) {
  var tmin, tmax, tymin, tymax, tzmin, tzmax;

  var invdirx = 1 / direction.x,
      invdiry = 1 / direction.y,
      invdirz = 1 / direction.z;

  var origin = this.origin;

  if (invdirx >= 0) {
    tmin = (box.min.x - origin.x) * invdirx;
    tmax = (box.max.x - origin.x) * invdirx;
  } else {
    tmin = (box.max.x - origin.x) * invdirx;
    tmax = (box.min.x - origin.x) * invdirx;
  }

  if (invdiry >= 0) {
    tymin = (box.min.y - origin.y) * invdiry;
    tymax = (box.max.y - origin.y) * invdiry;
  } else {
    tymin = (box.max.y - origin.y) * invdiry;
    tymax = (box.min.y - origin.y) * invdiry;
  }

  if ((tmin > tymax) || (tymin > tmax)) return null;

  // These lines also handle the case where tmin or tmax is NaN
  // (result of 0 * Infinity). x !== x returns true if x is NaN

  if (tymin > tmin || tmin != tmin) tmin = tymin;

  if (tymax < tmax || tmax != tmax) tmax = tymax;

  if (invdirz >= 0) {
    tzmin = (box.min.z - origin.z) * invdirz;
    tzmax = (box.max.z - origin.z) * invdirz;
  } else {
    tzmin = (box.max.z - origin.z) * invdirz;
    tzmax = (box.min.z - origin.z) * invdirz;
  }

  if ((tmin > tzmax) || (tzmin > tmax)) return null;

  if (tzmin > tmin || tmin != tmin) tmin = tzmin;

  if (tzmax < tmax || tmax != tmax) tmax = tzmax;

  //return point closest to the ray (positive side)

  if (tmax < 0) return null;

  return at(tmin >= 0 ? tmin : tmax, target);
}