Mesh
Triangle mesh geometry for RayD scenes
Overview#
Mesh is the basic geometry unit in RayD. It holds triangle vertex positions and face indices, optional UV coordinates, and a transform. Meshes are added to a Scene and cannot be shared across scenes.
import rayd as rd
import drjit as dr
rd.set_device(0)
mesh = rd.Mesh(
dr.cuda.Array3f(
[-1.0, 1.0, 1.0, -1.0],
[-1.0, -1.0, 1.0, 1.0],
[0.0, 0.0, 0.0, 0.0],
),
dr.cuda.Array3i([0, 0], [1, 2], [2, 3]),
)Constructor#
rd.Mesh(v, f, uv=None, f_uv=None, verbose=False)| Parameter | Type | Description |
|---|---|---|
v | Array3f or Array3fAD | Vertex positions. Passing Array3fAD attaches gradients automatically |
f | Array3i | Face indices. Each column is one triangle (v0, v1, v2) |
uv | Array2f | None | Optional UV coordinates |
f_uv | Array3i | None | Optional UV face indices |
verbose | bool | Enable verbose build logging |
Properties#
| Property | Description |
|---|---|
mesh.vertex_positions | Get or set vertex positions. Accepts Array3f or Array3fAD |
mesh.vertex_positions_world | Vertex positions after applying the to_world transform |
mesh.vertex_normals | Per-vertex normals |
mesh.vertex_uv | Get or set UV coordinates |
mesh.face_indices | Get or set face indices |
mesh.face_uv_indices | Get or set UV face indices |
mesh.to_world | Get or set the full world transform matrix |
mesh.to_world_left | Left factor of the transform |
mesh.to_world_right | Right factor of the transform |
mesh.use_face_normals | Get or set whether to use face normals instead of vertex normals |
mesh.edges_enabled | Get or set whether edge queries are enabled for this mesh |
mesh.num_vertices | Number of vertices |
mesh.num_faces | Number of faces |
Methods#
mesh.build() — Build the mesh's internal acceleration structure. Call after construction before adding to a scene.
mesh.set_transform(mat, set_left=True) — Set the mesh transform matrix.
mesh.append_transform(mat, append_left=True) — Multiply the current transform by mat.
mesh.secondary_edges() — Return a SecondaryEdgeInfo object containing the mesh's secondary edges for edge sampling and nearest-edge queries.
mesh.edge_indices() — Return a tuple of five arrays containing edge index data.
Differentiable Vertices#
To differentiate through mesh geometry, pass an Array3fAD as vertex positions:
verts = dr.cuda.ad.Array3f(
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 0.0],
)
dr.enable_grad(verts)
mesh = rd.Mesh(
dr.cuda.Array3f([0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 0.0]),
dr.cuda.Array3i([0], [1], [2]),
)
mesh.vertex_positions = vertsAfter scene.intersect(), call dr.backward(loss) to propagate gradients back to the vertex positions.
SecondaryEdgeInfo#
mesh.secondary_edges() returns a SecondaryEdgeInfo with:
| Field | Description |
|---|---|
start | Edge start positions |
edge | Edge direction vectors |
normal0, normal1 | Adjacent face normals |
opposite | Opposite vertex positions |
is_boundary | Boolean mask — True for boundary edges |
edges = mesh.secondary_edges()
print("edge count =", edges.size())
boundary_count = sum(bool(v) for v in list(edges.is_boundary))
print("boundary edges =", boundary_count)Next Steps#
- Scene API — add meshes to a scene
- Edge queries — use edge data for nearest-edge queries