Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-usd

Source of release 0.9.4

0.9.4 / tests / checks / geom_checks.lucb

## UsdGeom reading: xform op stacks (every op kind, inversion, resetting),
## the walk's visibility, purpose, specifier and instancer-prototype rules,
## and inherited constant primvars.
from luce_std import math
import memory
from luce_geocore.geocore import Mesh, GeometrySet
from luce_geocore.core.growing import Growing
from luce_geocore.kernel import dispose_set, set_mesh, group_bits, curves_type, points_component, Curves, PointCloud, AttributeStore, Domain, cloud_store, curves_knot_offsets
from luce_usd.kernel import Composer, anonymous_session, flatten_stage, absolute_path, ImportOptions, ExportOptions, Warnings, import_layer, export_set, write_usda, Names, LayerData, read_usda, read_file, Stage, earliest, gather, Gathered, GprimKind, Matrix4d, local_transform, absolute_root, purpose_render
import strings

func near(a: f64, b: f64) -> bool:
    return math.abs(a - b) < 1e-5

func check_point(matrix: const Matrix4d*, x: f64, y: f64, z: f64, ex: f64, ey: f64, ez: f64):
    let placed = matrix.point(x, y, z)
    if not (near(placed.0, ex) and near(placed.1, ey) and near(placed.2, ez)):
        print(f"placed ({placed.0}, {placed.1}, {placed.2}), expected ({ex}, {ey}, {ez})")
    assert(near(placed.0, ex) and near(placed.1, ey) and near(placed.2, ez))

pub func transforms() -> !:
    var buffer: u8[256]
    let text = try read_file(try strings.format(buffer, f"tests/fixtures/geom/xforms.usda"))
    defer free(text) in memory.heap
    var names = try Names()
    defer names.close()
    var layer = try LayerData(&names)
    defer layer.close()
    try read_usda(text, &layer)
    var stage = try Stage(&layer, earliest)
    var found = Gathered()
    defer found.close()
    let identity = Matrix4d.identity()
    try gather(&stage, absolute_root, 1 | purpose_render, &identity, &found)
    assert(found.gprims.count == 4)
    let gprims = found.gprims.view()
    assert(names.name_text(gprims[0].path) == "Quad" and gprims[0].kind == GprimKind.mesh)
    check_point(&gprims[0].world, 1.0, 0.0, 0.0, 1.0, 4.0, 3.0)
    check_point(&gprims[0].world, 0.0, 1.0, 0.0, -1.0, 2.0, 3.0)
    assert(gprims[0].end - gprims[0].first == 1)
    assert(names.name_text(gprims[1].path) == "Tri")
    check_point(&gprims[1].world, 1.0, 0.0, 0.0, 6.0, 0.0, 0.0)
    assert(names.name_text(gprims[2].path) == "Inner")
    # Local (0, 0, 11), then World: scaled to 22, raised by 3.
    check_point(&gprims[2].world, 1.0, 0.0, 0.0, 1.0, 2.0, 25.0)
    assert(names.name_text(gprims[3].path) == "Scatter" and gprims[3].kind == GprimKind.instancer)
    assert(found.skipped_types.count == 1 and names.text_of(found.skipped_types.items[0]) == "Camera")

## The xform fixture imported: two meshes placed, the empty mesh prim and
## the instancer left out, the inherited displayColor on the detail.
pub func imported() -> !:
    var buffer: u8[256]
    let text = try read_file(try strings.format(buffer, f"tests/fixtures/geom/xforms.usda"))
    defer free(text) in memory.heap
    var names = try Names()
    defer names.close()
    var layer = try LayerData(&names)
    defer layer.close()
    try read_usda(text, &layer)
    var warnings = Warnings()
    defer warnings.close()
    let options = ImportOptions(time = math.nan, realize = false, purposes = 0, convert_units = false, subdivide_unauthored = true)
    let set = try import_layer(&layer, &options, &warnings)
    defer dispose_set(set)
    let mesh = set_mesh(set) else trap("no mesh")
    assert(mesh.face_count() == 2 and mesh.point_count() == 7)
    let second = try mesh.point(1)
    assert(near(second.x, 1.0) and near(second.y, 4.0) and near(second.z, 3.0))
    let tri = try mesh.point(5)
    assert(near(tri.x, 6.0) and near(tri.y, 0.0) and near(tri.z, 0.0))
    let path = mesh.find_attribute("path", 2)
    assert(path >= 0)
    let entry = try mesh.attribute_entry(path)
    assert(entry.text_at(0) == "/World/Quad" and entry.text_at(1) == "/World/Reset/Tri")
    let color = mesh.find_attribute("Cd", 3)
    assert(color >= 0 and near(try mesh.attribute_value(color, 0, 0), 1.0))
    # The quad says "none"; the triangle's unauthored scheme is Catmull-Clark,
    # but the quad has more faces... one face each: the first scheme found wins.
    assert(mesh.find_attribute("subd.scheme", 3) < 0 or near(try mesh.attribute_value(mesh.find_attribute("subd.scheme", 3), 0), 1.0))

## The mesh fixture read at `time` (NaN: startTimeCode).
func import_meshes_at(at_time: f64, convert_units: bool, check: func(const Mesh*, const GeometrySet*, const Names*) -> unit!) -> !:
    var buffer: u8[256]
    let text = try read_file(try strings.format(buffer, f"tests/fixtures/geom/meshes.usda"))
    defer free(text) in memory.heap
    var names = try Names()
    defer names.close()
    var layer = try LayerData(&names)
    defer layer.close()
    try read_usda(text, &layer)
    var warnings = Warnings()
    defer warnings.close()
    let options = ImportOptions(time = at_time, realize = false, purposes = 0, convert_units = convert_units, subdivide_unauthored = true)
    let set = try import_layer(&layer, &options, &warnings)
    defer dispose_set(set)
    let mesh = set_mesh(set) else trap("no mesh")
    try check(mesh, set, &names)
    assert(contains((str)warnings.text.view(), "1 faces with fewer than 3 corners were dropped"))
    assert(contains((str)warnings.text.view(), "different subdivision schemes"))

func contains(text: str, part: str) -> bool:
    if part.length > text.length: return false
    for at in 0..<text.length - part.length + 1:
        if (str)text.bytes[at..<at + part.length] == part: return true
    return false

func number(mesh: const Mesh*, name: str, domain: i64, element: i64, component: i64) -> f64!:
    let index = mesh.find_attribute(name, domain)
    if index < 0:
        print(f"no attribute {name}")
        trap("missing attribute")
    return try mesh.attribute_value(index, element, component)

func text_at(mesh: const Mesh*, name: str, domain: i64, element: usize) -> str!:
    let index = mesh.find_attribute(name, domain)
    assert(index >= 0)
    return (try mesh.attribute_entry(index)).text_at(element)

func in_group(mesh: const Mesh*, name: str, domain: i64, element: usize) -> bool!:
    let index = mesh.find_attribute(name, domain)
    assert(index >= 0)
    return (try group_bits(mesh, index)).get(element)

## Primvars, winding, subsets, subdivision, bindings and paths, at the
## start time.
pub func mesh_features() -> !:
    try import_meshes_at(math.nan, false, check_features)

func check_features(mesh: const Mesh*, set: const GeometrySet*, names: const Names*) -> !:
    # Left keeps two of its faces; Moving adds one.
    assert(mesh.face_count() == 3 and mesh.point_count() == 8)
    let corners = mesh.corner_span()
    # The left-handed quad reversed after its first corner.
    assert(corners[0] == 0 and corners[1] == 3 and corners[2] == 2 and corners[3] == 1)
    assert(corners[7] == 5 and corners[8] == 6 and corners[9] == 7)
    # faceVarying st follows the reversal.
    let uv: f64[8] = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0]
    for corner in 0..<(usize)4:
        assert(near(try number(mesh, "uv", 1, (i64)corner, 0), uv[corner * 2]) and near(try number(mesh, "uv", 1, (i64)corner, 1), uv[corner * 2 + 1]))
    # Indexed vertex colors; Moving's points hold zeros.
    assert(near(try number(mesh, "Cd", 0, 1, 1), 1.0) and near(try number(mesh, "Cd", 0, 4, 0), 1.0) and near(try number(mesh, "Cd", 0, 6, 0), 0.0))
    assert(near(try number(mesh, "tag", 2, 0, 0), 7.0) and near(try number(mesh, "tag", 2, 1, 0), 8.0) and near(try number(mesh, "tag", 2, 2, 0), 5.0))
    assert((try text_at(mesh, "path", 2, 0)) == "/Root/Left" and (try text_at(mesh, "path", 2, 2)) == "/Root/Moving")
    assert((try text_at(mesh, "material", 2, 0)) == "/Root/Looks/Red" and (try text_at(mesh, "material", 2, 1)) == "/Root/Looks/Blue" and (try text_at(mesh, "material", 2, 2)) == "")
    assert(try in_group(mesh, "top", 2, 1) and not try in_group(mesh, "top", 2, 0))
    assert(try in_group(mesh, "hole", 2, 1))
    let rim = try mesh.find_edge(0, 1)
    assert(rim >= 0 and try in_group(mesh, "rim", 4, (usize)rim))
    assert(near(try number(mesh, "crease", 4, try mesh.find_edge(1, 2), 0), 2.0) and near(try number(mesh, "crease", 4, try mesh.find_edge(2, 3), 0), 0.0))
    assert(near(try number(mesh, "corner_sharpness", 0, 3, 0), 10.0))
    assert(near(try number(mesh, "subd.scheme", 3, 0, 0), 1.0))
    # Moving at time 1.
    let moving = try mesh.point(6)
    assert(near(moving.x, 1.0) and near(moving.z, 0.0))
    assert(near(try set.detail_value("usd.metersPerUnit"), 0.01) and near(try set.detail_value("usd.time"), 1.0))

## Samples interpolate between times.
pub func mesh_at_time() -> !:
    try import_meshes_at(2.0, false, check_time)

func check_time(mesh: const Mesh*, set: const GeometrySet*, names: const Names*) -> !:
    let moving = try mesh.point(6)
    assert(near(moving.x, 2.0) and near(moving.y, 0.0) and near(moving.z, 2.0))

## Z-up centimeters converted to Y-up meters.
pub func mesh_units() -> !:
    try import_meshes_at(math.nan, true, check_units)

func check_units(mesh: const Mesh*, set: const GeometrySet*, names: const Names*) -> !:
    let corner = try mesh.point(3)
    assert(near(corner.x, 0.0) and near(corner.y, 0.0) and near(corner.z, -0.01))
    assert(near(try set.detail_value("usd.metersPerUnit"), 1.0))

## Intrinsic shapes tessellated, sized and turned onto their axes.
pub func shapes() -> !:
    var buffer: u8[256]
    let text = try read_file(try strings.format(buffer, f"tests/fixtures/geom/shapes.usda"))
    defer free(text) in memory.heap
    var names = try Names()
    defer names.close()
    var layer = try LayerData(&names)
    defer layer.close()
    try read_usda(text, &layer)
    var warnings = Warnings()
    defer warnings.close()
    let options = ImportOptions(time = math.nan, realize = false, purposes = 0, convert_units = false, subdivide_unauthored = false)
    let set = try import_layer(&layer, &options, &warnings)
    defer dispose_set(set)
    let mesh = set_mesh(set) else trap("no mesh")
    if warnings.text.count > 0: print((str)warnings.text.view())
    assert(warnings.text.count == 0)
    # The cube's 8 corners at ±2; its faces face out.
    let corner = try mesh.point(7)
    assert(near(corner.x, 2.0) and near(corner.y, 2.0) and near(corner.z, 2.0))
    let normal = try mesh.face_normal(1)
    assert(near(normal.z, 1.0))
    # Every face of every shape faces away from its shape's center.
    let bounds = try set.bounds()
    assert(near(bounds.maximum.x, 12.0) and near(bounds.minimum.x, -5.0))
    assert((try text_at(mesh, "path", 2, 0)) == "/Box")
    let color = mesh.find_attribute("Cd", 2)
    assert(color >= 0 and near(try mesh.attribute_value(color, 0, 2), 1.0) and near(try mesh.attribute_value(color, 6, 2), 0.0))

## Curves and points: types, handles, knots, weights, widths and paths.
pub func curves_and_points() -> !:
    var buffer: u8[256]
    let text = try read_file(try strings.format(buffer, f"tests/fixtures/geom/curves.usda"))
    defer free(text) in memory.heap
    var names = try Names()
    defer names.close()
    var layer = try LayerData(&names)
    defer layer.close()
    try read_usda(text, &layer)
    var warnings = Warnings()
    defer warnings.close()
    let options = ImportOptions(time = math.nan, realize = false, purposes = 0, convert_units = false, subdivide_unauthored = false)
    let set = try import_layer(&layer, &options, &warnings)
    defer dispose_set(set)
    if warnings.text.count > 0: print((str)warnings.text.view())
    assert(warnings.text.count == 0 and set_mesh(set) == none)
    let curves = (const Curves*)(set.find(curves_type()) else trap("no curves"))
    # 2 lines, 1 Bezier (2 control points), 1 spline, 1 NURBS arc.
    assert(curves.curve_count() == 5 and curves.point_count() == 5 + 2 + 4 + 3)
    let lifted = curves.point(0)
    assert(near(lifted.z, 1.0))
    # Knot offsets cover every curve, rising: only the arc has knots.
    let knot_offsets = curves_knot_offsets(curves)
    assert(knot_offsets.length == 6 and knot_offsets[0] == 0 and knot_offsets[4] == 0 and knot_offsets[5] == 6)
    let store = curves.store()
    assert(near(store_number(store, "curve_type", 2, 2, 0), 1.0) and near(store_number(store, "curve_type", 2, 3, 0), 2.0) and near(store_number(store, "curve_type", 2, 4, 0), 2.0))
    assert(near(store_number(store, "knot_mode", 2, 4, 0), 2.0) and near(store_number(store, "order", 2, 4, 0), 3.0))
    # The Bezier's first handles: the vertex after it, and mirrored before.
    assert(near(store_number(store, "handle_r", 0, 5, 0), 1.0) and near(store_number(store, "handle_l", 0, 5, 0), -1.0) and near(store_number(store, "handle_l", 0, 5, 1), -1.0))
    assert(near(store_number(store, "w", 0, 12, 0), 0.70710678) and near(store_number(store, "w", 0, 0, 0), 1.0))
    assert(near(store_number(store, "width", 2, 1, 0), 0.5) and near(store_number(store, "Cd", 2, 1, 1), 1.0))
    let cloud = (const PointCloud*)(set.find(points_component()) else trap("no points"))
    assert(cloud.count() == 2)
    let points_store = cloud_store(cloud)
    assert(near(store_number(points_store, "width", 0, 1, 0), 0.2) and near(store_number(points_store, "id", 0, 1, 0), 11.0))

func store_number(store: const AttributeStore*, name: str, domain: i64, element: usize, component: usize) -> f64:
    let index = store.find(name, Domain((u8)domain))
    if index < 0:
        print(f"no attribute {name}")
        trap("missing attribute")
    return store.entry((usize)index).number(element, component)

## The instancer fixture: instances, or placed gprims when flattening.
func import_forest(realize: bool) -> GeometrySet*!:
    var buffer: u8[256]
    let text = try read_file(try strings.format(buffer, f"tests/fixtures/geom/instancer.usda"))
    defer free(text) in memory.heap
    var names = try Names()
    defer names.close()
    var layer = try LayerData(&names)
    defer layer.close()
    try read_usda(text, &layer)
    var warnings = Warnings()
    defer warnings.close()
    let options = ImportOptions(time = math.nan, realize = realize, purposes = 0, convert_units = false, subdivide_unauthored = false)
    let set = try import_layer(&layer, &options, &warnings)
    if warnings.text.count > 0: print((str)warnings.text.view())
    return set

pub func instancer() -> !:
    let set = try import_forest(false)
    defer dispose_set(set)
    assert(set_mesh(set) == none and set.instance_count() == 3)
    let moved = try set.instance_translation(1)
    assert(near(moved.x, 2.0) and near(moved.z, 10.0))
    let turned = try set.instance_rotation(1)
    assert(near(turned.x, 0.0) and near(turned.y, 0.0) and math.abs(turned.z - math.pi * 0.5) < 1e-3)
    let scaled = try set.instance_scale(1)
    assert(near(scaled.x, 2.0) and near(scaled.z, 2.0))
    assert(try set.instance_visible(0) and not try set.instance_visible(2))
    let flat = try import_forest(true)
    defer dispose_set(flat)
    let mesh = set_mesh(flat) else trap("no mesh")
    assert(mesh.face_count() == 7)
    # The tree's corner (1, 0, 0) placed at (2, 0, 10).
    let tip = try mesh.point(1)
    assert(near(tip.x, 2.0) and near(tip.z, 10.0))

## A fixture imported, exported, written as text, read and imported again.
func round_trip(file_name: str, check: func(const GeometrySet*, const GeometrySet*) -> unit!) -> !:
    var buffer: u8[256]
    let text = try read_file(try strings.format(buffer, f"tests/fixtures/geom/{file_name}"))
    defer free(text) in memory.heap
    var names = try Names()
    defer names.close()
    var layer = try LayerData(&names)
    defer layer.close()
    try read_usda(text, &layer)
    var warnings = Warnings()
    defer warnings.close()
    let options = ImportOptions(time = math.nan, realize = false, purposes = 0, convert_units = false, subdivide_unauthored = false)
    let first = try import_layer(&layer, &options, &warnings)
    defer dispose_set(first)
    var exported = try LayerData(&names)
    defer exported.close()
    let export_options = ExportOptions(split = true, precise = false)
    try export_set(first, &exported, "geo", &export_options, &warnings)
    var written = Growing[u8]()
    defer written.close()
    try write_usda(&exported, &written)
    var again_names = try Names()
    defer again_names.close()
    var again = try LayerData(&again_names)
    defer again.close()
    try read_usda(written.view(), &again)
    let second = try import_layer(&again, &options, &warnings)
    defer dispose_set(second)
    try check(first, second)

## Every face, point, corner and element attribute the same after a trip.
pub func mesh_round_trip() -> !:
    try round_trip("meshes.usda", same_meshes)

func same_meshes(first: const GeometrySet*, second: const GeometrySet*) -> !:
    let a = set_mesh(first) else trap("no mesh")
    let b = set_mesh(second) else trap("no mesh")
    assert(a.face_count() == b.face_count() and a.point_count() == b.point_count())
    for face in 0..<(usize)a.face_count():
        assert((try text_at(a, "path", 2, face)) == (try text_at(b, "path", 2, face)))
        assert((try text_at(a, "material", 2, face)) == (try text_at(b, "material", 2, face)))
        assert(near(try number(a, "tag", 2, (i64)face, 0), try number(b, "tag", 2, (i64)face, 0)))
        assert((try in_group(a, "top", 2, face)) == (try in_group(b, "top", 2, face)))
        assert((try in_group(a, "hole", 2, face)) == (try in_group(b, "hole", 2, face)))
    let ca = a.corner_span()
    let cb = b.corner_span()
    for corner in 0..<ca.length:
        let pa = try a.point((i64)ca[corner])
        let pb = try b.point((i64)cb[corner])
        assert(near(pa.x, pb.x) and near(pa.y, pb.y) and near(pa.z, pb.z))
        for k in 0..<(i64)2: assert(near(try number(a, "uv", 1, (i64)corner, k), try number(b, "uv", 1, (i64)corner, k)))
        assert(near(try number(a, "Cd", 0, (i64)ca[corner], 1), try number(b, "Cd", 0, (i64)cb[corner], 1)))
    let crease_a = a.find_attribute("crease", 4)
    let crease_b = b.find_attribute("crease", 4)
    assert(crease_a >= 0 and crease_b >= 0)
    let creases = try sum_of(a, crease_a)
    assert(near(creases, try sum_of(b, crease_b)) and near(creases, 4.0))
    assert(near(try sum_of(a, a.find_attribute("corner_sharpness", 0)), try sum_of(b, b.find_attribute("corner_sharpness", 0))))

func sum_of(mesh: const Mesh*, index: i64) -> f64!:
    let entry = try mesh.attribute_entry(index)
    var total: f64 = 0.0
    for element in 0..<entry.elements: total += entry.number(element, 0)
    return total

## Curves, points and instances the same after a trip.
pub func elements_round_trip() -> !:
    try round_trip("curves.usda", same_curves)
    try round_trip("instancer.usda", same_instances)

func same_curves(first: const GeometrySet*, second: const GeometrySet*) -> !:
    let a = (const Curves*)(first.find(curves_type()) else trap("no curves"))
    let b = (const Curves*)(second.find(curves_type()) else trap("no curves"))
    assert(a.curve_count() == b.curve_count() and a.point_count() == b.point_count())
    for point in 0..<a.point_count():
        let pa = a.point(point)
        let pb = b.point(point)
        assert(near(pa.x, pb.x) and near(pa.y, pb.y) and near(pa.z, pb.z))
    for name in ["curve_type", "order", "knot_mode"]:
        for curve in 0..<a.curve_count(): assert(near(store_number(a.store(), name, 2, curve, 0), store_number(b.store(), name, 2, curve, 0)))
    assert(near(store_number(b.store(), "w", 0, 12, 0), 0.70710678))
    let pa = cloud_store((const PointCloud*)(first.find(points_component()) else trap("no points")))
    let pb = cloud_store((const PointCloud*)(second.find(points_component()) else trap("no points")))
    assert(near(store_number(pa, "id", 0, 1, 0), store_number(pb, "id", 0, 1, 0)) and near(store_number(pb, "width", 0, 1, 0), 0.2))

func same_instances(first: const GeometrySet*, second: const GeometrySet*) -> !:
    assert(second.instance_count() == 3)
    for at in 0..<(i64)3:
        let ta = try first.instance_translation(at)
        let tb = try second.instance_translation(at)
        let ra = try first.instance_rotation(at)
        let rb = try second.instance_rotation(at)
        assert(near(ta.x, tb.x) and near(ta.z, tb.z) and math.abs(ra.z - rb.z) < 2e-3)
        assert((try first.instance_visible(at)) == (try second.instance_visible(at)))

## Faces of any size: big_face.usda holds a 1000-corner star (alternating
## radii 1 and 0.8, so every other corner is concave), once right-handed and
## once left-handed. Each stays one face, both clip into 998 triangles
## covering the star's area and facing its way, and they survive a trip.
pub func big_face() -> !:
    try round_trip("big_face.usda", same_big_faces)

func same_big_faces(first: const GeometrySet*, second: const GeometrySet*) -> !:
    try check_big_faces(set_mesh(first) else trap("no mesh"))
    try check_big_faces(set_mesh(second) else trap("no mesh"))

func check_big_faces(mesh: const Mesh*) -> !:
    assert(mesh.face_count() == 2 and (try mesh.face_size(0)) == 1000 and (try mesh.face_size(1)) == 1000)
    let triangles = mesh.triangle_span()
    let corners = mesh.corner_span()
    assert(triangles.length == 2 * 998 * 3)
    for face in 0..<(usize)2:
        # The left-handed copy is reversed, so it faces -z.
        let sign = 1.0 if face == 0 else -1.0
        var area: f64 = 0.0
        for at in face * 998..<face * 998 + 998:
            let a = try mesh.point((i64)corners[(usize)triangles[at * 3]])
            let b = try mesh.point((i64)corners[(usize)triangles[at * 3 + 1]])
            let c = try mesh.point((i64)corners[(usize)triangles[at * 3 + 2]])
            let twice = ((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)) * sign
            assert(twice >= 0.0)
            area += twice / 2.0
        assert(math.abs(area - 2.5132576) < 1e-4)

## N-gons stay n-gons: a concave face is ear clipped (no display triangle
## covers its notch), a self-intersecting one displays as a fan, and a face
## without an area is kept with a zero normal and a warning.
pub func folded_faces() -> !:
    let text = "#usda 1.0\ndef Mesh \"Folded\"\n{\n    int[] faceVertexCounts = [6, 5, 3]\n    int[] faceVertexIndices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]\n    point3f[] points = [(0, 0, 0), (2, 0, 0), (2, 1, 0), (1, 1, 0), (1, 2, 0), (0, 2, 0), (0, 0, 1), (2, 0, 1), (0, 1, 1), (2, 1, 1), (1, -1, 1), (0, 0, 2), (1, 0, 2), (2, 0, 2)]\n}\n"
    var names = try Names()
    defer names.close()
    var layer = try LayerData(&names)
    defer layer.close()
    try read_usda(text.bytes, &layer)
    var warnings = Warnings()
    defer warnings.close()
    let options = ImportOptions(time = math.nan, realize = false, purposes = 0, convert_units = false, subdivide_unauthored = false)
    let set = try import_layer(&layer, &options, &warnings)
    defer dispose_set(set)
    let mesh = set_mesh(set) else trap("no mesh")
    assert(mesh.face_count() == 3 and (try mesh.face_size(0)) == 6 and (try mesh.face_size(1)) == 5)
    let flat = try mesh.face_normal(2)
    assert(flat.x == 0.0 and flat.y == 0.0 and flat.z == 0.0)
    assert(contains((str)warnings.text.view(), "1 faces have no area"))
    let triangles = mesh.triangle_span()
    let corners = mesh.corner_span()
    assert(triangles.length == 8 * 3)
    var area: f64 = 0.0
    for at in 0..<(usize)4:
        let a = try mesh.point((i64)corners[(usize)triangles[at * 3]])
        let b = try mesh.point((i64)corners[(usize)triangles[at * 3 + 1]])
        let c = try mesh.point((i64)corners[(usize)triangles[at * 3 + 2]])
        let twice = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)
        assert(twice > 0.0)
        area += twice / 2.0
        let x = (a.x + b.x + c.x) / 3.0
        let y = (a.y + b.y + c.y) / 3.0
        assert(not (x > 1.0 and y > 1.0))
    assert(near(area, 3.0))

## A composed stage with scene-graph instances: flattened with prototypes,
## each prototype imports once and each instance becomes a row; flattening
## everything places each instance's gprims instead.
func import_scene(realize: bool) -> GeometrySet*!:
    var names = try Names()
    defer names.close()
    var identifier = Growing[u8]()
    defer identifier.close()
    try absolute_path("tests/fixtures/geom/scene_instances.usda", &identifier)
    var composer = try Composer(&names, (str)identifier.view(), anonymous_session)
    defer composer.close()
    var flat = try LayerData(&names)
    defer flat.close()
    try flatten_stage(&composer, &flat, false, not realize)
    var warnings = Warnings()
    defer warnings.close()
    let options = ImportOptions(time = math.nan, realize = realize, purposes = 0, convert_units = false, subdivide_unauthored = false)
    return try import_layer(&flat, &options, &warnings)

pub func scene_instances() -> !:
    let set = try import_scene(false)
    defer dispose_set(set)
    # A and B share a prototype; C's variant makes its own.
    assert(set_mesh(set) == none and set.instance_count() == 3)
    let a = try set.instance_translation(0)
    let b = try set.instance_translation(1)
    let c = try set.instance_translation(2)
    assert(near(a.x, 5.0) and near(b.y, 5.0) and near(c.z, 5.0))
    # Each row keeps its instance prim's path, through a usda trip too.
    assert((try set.instance_path(0)) == "/A" and (try set.instance_path(2)) == "/C")
    var names = try Names()
    defer names.close()
    var exported = try LayerData(&names)
    defer exported.close()
    var warnings = Warnings()
    defer warnings.close()
    let export_options = ExportOptions(split = true, precise = false)
    try export_set(set, &exported, "geo", &export_options, &warnings)
    var written = Growing[u8]()
    defer written.close()
    try write_usda(&exported, &written)
    var again_names = try Names()
    defer again_names.close()
    var again = try LayerData(&again_names)
    defer again.close()
    try read_usda(written.view(), &again)
    let options = ImportOptions(time = math.nan, realize = false, purposes = 0, convert_units = false, subdivide_unauthored = false)
    let back = try import_layer(&again, &options, &warnings)
    defer dispose_set(back)
    assert(back.instance_count() == 3 and (try back.instance_path(1)) == "/B")
    let flat = try import_scene(true)
    defer dispose_set(flat)
    let mesh = set_mesh(flat) else trap("no mesh")
    assert(mesh.face_count() == 3)
    # C's big triangle, placed at z = 5.
    var found = false
    for at in 0..<mesh.point_count():
        let p = try mesh.point(at)
        found = found or (near(p.x, 2.0) and near(p.z, 5.0))
    assert(found)