private static Point2D intersect(KDNode node, Segment seg) { if (node.isLeaf()) { // long begin = System.nanoTime(); Intersection min = null; for (TriangleRef r : node.refs) { // inters++; Intersection i = seg.intersectTriangle(r.triangle); if (i != null) { if (min == null || min.getDistance() > i.getDistance()) { min = i; } } } // interTime += System.nanoTime()-begin; return min == null ? null : min.getIntersection(); } else { // recursion++; boolean splitAtX = node.splittingPlane.dimension == Dimension.X; double splitValue = node.splittingPlane.splitValue; return seg.splitAtXorY(splitAtX, splitValue) .map( (startOnLeftSide, start, end) -> { if (startOnLeftSide) { Point2D p = intersect(node.left, start); return p != null || end == null ? p : intersect(node.right, end); } else { Point2D p = intersect(node.right, start); return p != null || end == null ? p : intersect(node.left, end); } }); } }
public void visitHalfPlanes(BiConsumer<Segment, Integer> visitor, KDNode node, int depth) { if (node.isLeaf()) { // || depth > 8) { return; } else { if (node.splittingPlane.dimension == Dimension.X) { Point2D start = new Point2D(node.splittingPlane.splitValue, node.bounds.min.getY()); Point2D end = new Point2D(node.splittingPlane.splitValue, node.bounds.max.getY()); visitor.accept(new Segment(start, end), depth); visitHalfPlanes(visitor, node.left, depth + 1); // visitHalfPlanes(visitor, node.right, depth+1); } else { Point2D start = new Point2D(node.bounds.min.getX(), node.splittingPlane.splitValue); Point2D end = new Point2D(node.bounds.max.getX(), node.splittingPlane.splitValue); visitor.accept(new Segment(start, end), depth); visitHalfPlanes(visitor, node.left, depth + 1); // visitHalfPlanes(visitor, node.right, depth+1); } } }