@Override
    @SuppressWarnings("unchecked")
    public int getTerrainIntersection(Ray worldPick, CollisionResults results) {
        int numCollisions = 0;
        worldPickRay.set(worldPick);
        List<TerrainPickData> pickData = new ArrayList<>();
        root.findPick(worldPick.clone(), pickData);
        Collections.sort(pickData);

        if (pickData.isEmpty()) {
            return 0;
        }

        workRay.set(worldPick);

        for (TerrainPickData pd : pickData) {
            TerrainPatch patch = pd.targetPatch;

            tracer.getGridSpacing().set(patch.getWorldScale());
            tracer.setGridOrigin(patch.getWorldTranslation());

            workRay.getOrigin().set(worldPick.getDirection()).multLocal(pd.cr.getDistance() - .1f).addLocal(worldPick.getOrigin());

            tracer.startWalk(workRay);

            final Vector3f intersection = new Vector3f();
            final Vector2f loc = tracer.getGridLocation();

            if (tracer.isRayPerpendicularToGrid()) {
                Triangle hit = new Triangle();
                if (checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit)) {
                    float distance = worldPickRay.origin.distance(intersection);

                    //@TODO: Verify if it's even possible to have a ray hit multiple "PickData"s when being perpendicular at all.
                    // because otherwise, we could always return 1 here.
                    if (worldPick.getLimit() < Float.POSITIVE_INFINITY) {
                        if (distance <= worldPick.getLimit()) {
                            if (addCollision(results, patch, intersection, hit, distance)) {
                                if (!multipleCollisions) {
                                    return 1;
                                } else {
                                    numCollisions++;
                                }
                            }
                        } // else return 0; // < this is the old behavior, since the code checked for the range afterwards.
                    } else { // unlimited range
                        if (addCollision(results, patch, intersection, hit, distance)) {
                            if (!multipleCollisions) {
                                return 1;
                            } else {
                                numCollisions++;
                            }
                        }
                    }
                } // else no collision
            } else { // If the ray is perpendicular, tracer.next() would never advance loc, leading to an infinite loop.
                while (loc.x >= -1 && loc.x <= patch.getSize() &&
                        loc.y >= -1 && loc.y <= patch.getSize()) {

                    //System.out.print(loc.x + "," + loc.y + " : ");
                    // check the triangles of main square for intersection.
                    Triangle hit = new Triangle();
                    if (checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit)) {
                        // we found an intersection, so return that!
                        float distance = worldPickRay.origin.distance(intersection);

                        if (worldPick.getLimit() < Float.POSITIVE_INFINITY) {
                            if (distance <= worldPick.getLimit()) {
                                if (addCollision(results, patch, intersection, hit, distance)) {
                                    if (!multipleCollisions) {
                                        return 1;
                                    } else {
                                        numCollisions++;
                                    }
                                }
                            }//  else return 0; // < this is the old behavior, since the code checked for the range afterwards.
                        } else { // unlimited range
                            if (addCollision(results, patch, intersection, hit, distance)) {
                                if (!multipleCollisions) {
                                    return 1;
                                } else {
                                    numCollisions++;
                                }
                            }
                        }
                    }
                    
                    // because of how we get our height coords, we will
                    // sometimes be off by a grid spot, so we check the next
                    // grid space up.
                    int dx = 0, dz = 0;
                    Direction d = tracer.getLastStepDirection();
                    switch (d) {
                        case PositiveX:
                        case NegativeX:
                            dx = 0;
                            dz = 1;
                            break;
                        case PositiveZ:
                        case NegativeZ:
                            dx = 1;
                            dz = 0;
                            break;
                    }

                    if (checkTriangles(loc.x + dx, loc.y + dz, workRay, intersection, patch, hit)) {
                        // we found an intersection, so return that!
                        float distance = worldPickRay.origin.distance(intersection);

                        if (worldPick.getLimit() < Float.POSITIVE_INFINITY) {
                            if (distance <= worldPick.getLimit()) {
                                if (addCollision(results, patch, intersection, hit, distance)) {
                                    if (!multipleCollisions) {
                                        return 1;
                                    } else {
                                        numCollisions++;
                                    }
                                }
                            } // else return null; // < this is the old behavior, since the code checked for the range afterwards.
                        } else { // unlimited range
                            if (addCollision(results, patch, intersection, hit, distance)) {
                                if (!multipleCollisions) {
                                    return 1;
                                } else {
                                    numCollisions++;
                                }
                            }
                        }
                    }
                
                    tracer.next();
                }
            }
        }

        return numCollisions;
    }
It basically works by casting a pick ray
against the bounding volumes of the TerrainQuad and its children, gathering
all of the TerrainPatches hit (in distance order.) The triangles of each patch
are then tested using the BresenhamYUpGridTracer to determine which triangles
to test and in what order. When a hit is found, it is guaranteed to be the
first such hit and can immediately be returned.

@author Joshua Slack
@author Brent Owens
jme3-terrain/src/main/java/com/jme3/terrain/geomipmap/picking/BresenhamTerrainPicker.java