Nearest edge query

Query the scene edge BVH for nearest edges and boundary detection

Overview#

This tutorial walks through building a scene with edge support, querying nearest edges for a batch of points, and reading edge metadata including boundary status.

Build the Scene#

import rayd as rd
import drjit as dr
 
rd.set_device(0)
 
mesh = rd.Mesh(
    dr.cuda.Array3f(
        [0.0, 1.0, 1.0, 0.0],
        [0.0, 0.0, 1.0, 1.0],
        [0.0, 0.0, 0.0, 0.0],
    ),
    dr.cuda.Array3i([0, 0], [1, 2], [2, 3]),
)
mesh.build()
 
scene = rd.Scene()
scene.add_mesh(mesh)
scene.build()

Inspect Edge Info#

secondary_edges = mesh.secondary_edges()
boundary_count = sum(bool(v) for v in list(secondary_edges.is_boundary))
 
print("secondary_edge_count =", secondary_edges.size())
print("boundary_edge_count =", boundary_count)

Query Nearest Edges#

queries = dr.cuda.Array3f(
    [0.25, 1.20, -0.15, 0.50],
    [0.25, 0.50, 0.70, 1.20],
    [0.10, 0.00, 0.00, 0.00],
)
 
result = scene.nearest_edge(queries)
 
for i, valid in enumerate(list(result.is_valid())):
    if valid:
        edge_point = [float(result.edge_point[axis][i]) for axis in range(3)]
        print(
            f"query[{i}]: distance={float(result.distance[i]):.4f}, "
            f"shape_id={int(result.shape_id[i])}, "
            f"edge_id={int(result.edge_id[i])}, "
            f"boundary={bool(result.is_boundary[i])}, "
            f"edge_point={edge_point}"
        )
    else:
        print(f"query[{i}] invalid")

Filter Edges with a Mask#

To restrict queries to a subset of edges:

edge_count = secondary_edges.size()
mask = dr.cuda.Bool([True] * edge_count)
scene.set_edge_mask(mask)

Top-K Nearest Edges#

To find multiple nearest edges per query point:

result = scene.nearest_edges(queries, k=3)
 
print("query_count =", result.query_count)
print("k =", result.k)

Next Steps#