*/
public boolean visit(Ray3 ray, Interval I, Cell cell) {
/* Get the points at which the ray enters and exits the cell.
*/
Point3 p0 = ray.pointAt(I.minimum());
Point3 p1 = ray.pointAt(I.maximum());
/* Get the range of y-values for the ray within the cell, and
* get the portion of the height matrix within the cell.
*/
Interval h = Interval.between(p0.y(), p1.y());
Matrix slice = height.slice(cell.getX(), cell.getZ(), 2, 2);
boolean hit = false;
/* If the range of y-values intersect, then there may be an
* intersection.
*/
if (h.intersects(slice.range())) {
Box3 bounds = cell.getBoundingBox();
/* Get the points on the height field. */
Point3 p00 = new Point3(bounds.minimumX(), slice.at(0, 0), bounds.minimumZ());
Point3 p01 = new Point3(bounds.minimumX(), slice.at(0, 1), bounds.maximumZ());
Point3 p10 = new Point3(bounds.maximumX(), slice.at(1, 0), bounds.minimumZ());
Point3 p11 = new Point3(bounds.maximumX(), slice.at(1, 1), bounds.maximumZ());
Plane3 plane;
double t;
/* Divide the four points into two triangles and check for
* an intersection with each triangle.
*/
plane = Plane3.throughPoints(p00, p10, p11);
t = plane.intersect(ray);
if (I.contains(t)) {
Point3 p = ray.pointAt(t);
/* Get the normalized (x,z) coordinates, (cx, cz),
* within the bounds of the cell. If cx > cz, then
* the intersection hit the triangle, otherwise, it hit
* the plane, but on the other half of the cell.
*/
double cx = (p.x() - p00.x()) / (p10.x() - p00.x());
double cz = (p.z() - p10.z()) / (p11.z() - p10.z());
if (cx > cz) {
Intersection x = newIntersection(ray, t, ray.direction().dot(plane.normal()) < 0.0)
.setBasis(Basis3.fromW(plane.normal(), Basis3.Orientation.RIGHT_HANDED))
.setLocation(p);
recorder.record(x);
hit = true;
}
}
plane = Plane3.throughPoints(p00, p11, p01);
t = plane.intersect(ray);
if (I.contains(t)) {
Point3 p = ray.pointAt(t);
/* Get the normalized (x,z) coordinates, (cx, cz),
* within the bounds of the cell. If cx < cz, then
* the intersection hit the triangle, otherwise, it hit
* the plane, but on the other half of the cell.
*/
double cx = (p.x() - p00.x()) / (p10.x() - p00.x());
double cz = (p.z() - p10.z()) / (p11.z() - p10.z());
if (cx < cz) {
Intersection x = newIntersection(ray, t, ray.direction().dot(plane.normal()) < 0.0)
.setBasis(Basis3.fromW(plane.normal(), Basis3.Orientation.RIGHT_HANDED))
.setLocation(p);