if (I.isEmpty()) { // missed the grid entirely
return false;
}
Interval cellI = new Interval(I.minimum(), I.minimum());
Vector3 d = ray.direction();
Point3 p = ray.pointAt(I.minimum());
Cell cell;
Cell nextCell = this.nearestCell(p);
double rx = p.x() - (bound.minimumX() + (double) nextCell.cx * dx);
double ry = p.y() - (bound.minimumY() + (double) nextCell.cy * dy);
double rz = p.z() - (bound.minimumZ() + (double) nextCell.cz * dz);
do {
cell = nextCell;
double tx, ty, tz, t;
tx = (d.x() > 0.0) ? (dx - rx) / d.x() : -rx / d.x();
ty = (d.y() > 0.0) ? (dy - ry) / d.y() : -ry / d.y();
tz = (d.z() > 0.0) ? (dz - rz) / d.z() : -rz / d.z();
if (tx < ty && tx < tz) {
t = tx;
rx = (d.x() > 0.0) ? 0.0 : dx;
ry += t * d.y();
rz += t * d.z();
nextCell = new Cell(cell.cx + (d.x() > 0.0 ? 1 : -1), cell.cy, cell.cz);
} else if (ty < tz) {
t = ty;
rx += t * d.x();
ry = (d.y() > 0.0) ? 0.0 : dy;
rz += t * d.z();
nextCell = new Cell(cell.cx, cell.cy + (d.y() > 0.0 ? 1 : -1), cell.cz);
} else { // tz <= tx && tz <= ty
t = tz;
rx += t * d.x();
ry += t * d.y();
rz = (d.z() > 0.0) ? 0.0 : dz;
nextCell = new Cell(cell.cx, cell.cy, cell.cz + (d.z() > 0.0 ? 1 : -1));
}
cellI = new Interval(cellI.maximum(), cellI.maximum() + t);
if (cellI.maximum() > I.maximum()) {
cellI = cellI.intersect(I);
visitor.visit(ray, cellI, cell);
break;
}