Skip to content

pycvc/pycvc_gl: bind the LOD family (simplify, pyramids, select, tiles, store) + lod_city example - #511

Merged
transfix merged 3 commits into
masterfrom
feat/pycvc-lod-bindings
Oct 1, 2026
Merged

transfix merged 3 commits into
masterfrom
feat/pycvc-lod-bindings

Conversation

@transfix

@transfix transfix commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Python access to the LOD surface, so apps and demos can be written in Python.

pycvc

  • simplify / simplify_progressive, with out-params returned as values.
  • Pyramids: build_mesh_pyramid, build_image_pyramid and build_volume_pyramid, plus pyramid_params.
  • Selection math: view_params, presets, select_rung, switch_radius_m and related helpers.
  • Tiles: partition_parts, partition_model, partition_components, and build_tiled_pyramids with an optional Python on_tile callback. The GIL is released during the build and reacquired for the callback.
  • Store: scene_writer (file or memory), with to_blob() returning bytes. scene_reader opens a path or (trusted) bytes, and scene_reader.open_verified(bytes, sha256) is the authenticated path. Also read/write, index, has and bake_mesh_asset.
  • Long calls release the GIL, and failures map to Python exceptions.

pycvc_gl: LodGraphicsNode (setPyramid, setBase/appendRungs, setRungStyle with a Python callable, select), SceneGraph.selectLOD / setLODEnabled / lod_stats, and make_view_params.

Example: lod_city builds a synthetic tiled city, builds pyramids on the pool, bakes them to a blob, reloads it with open_verified, attaches LOD nodes and flies a camera path, printing lod_stats. It runs headless as a test and asserts drawn triangles drop at least 3× far vs near.

Review: one major was fixed. tile.geom/tile.cell were non-owning pointers into the tile (a use-after-free that valgrind flagged); they are now owned copies, with a regression test, and valgrind is clean. GIL tests now time one C++ call per assertion. Numpy integers are accepted for simplify_progressive targets.

Known follow-ups (nits):

  • setRungStyle holds a strong reference to the callable, so a style closing over its own node forms a cycle; documented rather than solved.
  • Concurrent mutation of an input geometry from another Python thread during a GIL-released call is unsupported.
  • pycvc_gl's add_child_lod maps a missing parent to RuntimeError, where the pycvc calls raise ValueError.

Tests: pycvc_lod and pycvc_gl_lod pass.

The LOD family (#483-#505) was C++-only, so Python apps and demos could
not build, bake or reload pyramids. Wrap the real headers directly, as
the rest of pycvc does: simplify / simplify_progressive /
sampled_hausdorff, pyramid_params + the three pyramid builders, the
selection math (view_params, quality presets, select_rung, the
switch-radius crossovers, bound distance), tile partitioning +
build_tiled_pyramids, and the scene.cvch5 store (scene_writer to bytes,
scene_reader from a path, trusted bytes or open_verified, index / has /
read / write / bake_mesh_asset), plus cvc::thread_pool and
app.compute_pool() for the `pool` arguments.

The long calls release the GIL with an RAII guard (exception-safe, unlike
Py_BEGIN_ALLOW_THREADS), so a loader thread can bake a city while the
main thread keeps drawing. Python callbacks (on_tile, group_key)
reacquire it on whichever thread C++ runs them, and an exception they
raise is carried through the pool and re-raised from the originating
call with its type intact. Failures map to ValueError / IndexError /
OSError (HDF5) / RuntimeError (SHA-256 mismatch) instead of a blanket
RuntimeError. Out-parameters come back as return values and bytes cross
as bytes. The store is wrapped only when config.h says HDF5 was built,
so a no-HDF5 libcvc still binds (HAVE_LOD_STORE tells Python).

The default %exception becomes a macro so this file can install its own
mapping and restore the original afterwards. The budget solver is left
out: a candidate points at caller-owned per-rung arrays a Python list
cannot back safely.
Python could build pyramids but not draw them: wrap the real
LodGraphicsNode (setPyramid, progressive setBase + appendRungs, select,
setRung, the ladder accessors), SceneGraph.selectLOD / setLODEnabled /
lod_stats, and cvc::gl::make_view_params, plus SceneGraph add_lod /
add_child_lod / lod_node factories and SceneRenderer.make_view_params,
which reads the live camera without a VTK object crossing into Python.

setRungStyle takes a Python callable. Its exceptions are deferred and
re-raised once the C++ call returns rather than thrown through
LodGraphicsNode::addRung, which would orphan a half-built rung child.
drawn_rungs() reports the rung ACTORS drawing, since a switch flips
vtkProp visibility and never the rung nodes' own flags.

examples/lod_city.py runs the whole pipeline on a synthetic city:
partition, setBase at once, pyramids built on a loader thread with its
own pool while the frame loop keeps selecting (appendRungs keeps rung
0), bake to bytes, open_verified reload, and a camera fly-out printing
lod_stats per pose; --render draws each pose offscreen with rung tints.
test_pycvc_gl_lod.py runs it headless and requires drawn triangles to
drop at least 3x from the near pose to the far one.
…a time

Review fixes for the LOD bindings:
- tile.geom and tile.cell were %naturalvar members, whose getters return a non-owning
  pointer INTO the tile -- a use-after-free as soon as the tile proxy is collected
  (`partition_parts(...)[i].geom`, or keeping `t.geom` from an on_tile callback; valgrind
  reported invalid reads). They are now properties returning copies (cvc::geometry is
  copy-on-write, so the copy shares the tile's buffers). Regression test included.
- simplify_progressive targets accept numpy integers (PyNumber_Index before
  PyLong_AsUnsignedLongLong), like the neighbouring typemaps.
- to_blob copies into the new bytes object with the GIL released.
- The GIL-release tests now time ONE C++ call per assertion, sized to take tens of ms,
  across the long calls (including the store readers, open_verified, partition_parts,
  bake_mesh_asset); the old multi-call closure could not fail.
@transfix
transfix merged commit 318bf2f into master Oct 1, 2026
13 checks passed
@transfix
transfix deleted the feat/pycvc-lod-bindings branch October 29, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant