Skip to content

tf overlays: the contour spectrum and contour tree of a field on a mesh - #879

Merged
transfix merged 1 commit into
masterfrom
feat/mesh-contour-overlays
Oct 29, 2026
Merged

transfix merged 1 commit into
masterfrom
feat/mesh-contour-overlays

Conversation

@transfix

@transfix transfix commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Rebased onto master after #878 merged (f715017).

The FEM demos' Colours editors couldn't show the contour spectrum or contour tree. Both overlays were computed from volumes only, so on mesh_lab and fe_lab fields #878 marked them unavailable. With this PR, they're computed on the field's own mesh.

Library (cvc, no GL):

  • cvc::contour_mesh (cvc/volume/contour_mesh.h): a scalar field on a simplicial mesh, one value per point, linear over each tetrahedron (or over each triangle of a surface).
  • mesh_contour_spectrum_builder and compute_contour_spectrum(contour_mesh):
    • On tetrahedra, each tetrahedron uses the grid version's integrals (shared code) with its exact gradient.
    • On a surface, each triangle's integrals are exact: the contours' length (drawn as "area"), the area below and above, and |∇f|² along the contours.
    • It's resumable, and stepping it changes no bit.
  • mesh_contour_tree_builder and compute_contour_tree(contour_mesh):
    • Carr's join / split / merge over the graph of the elements' edges.
    • Resumable in every phase; the sort is a bottom-up merge sort for that reason.
    • On a grid's own tetrahedra (or triangles), it builds exactly the grid builder's tree.
    • NaN points are left out with their edges, and separate pieces get a tree each.
    • On domains whose level sets form loops (an annulus, a torus), it still returns a tree, with approximate component counts across the loop.
  • overlay_job, compute_overlays and schedule_overlays accept a mesh. overlay_options::has_range computes the layers over a given range.

FEM nodes (mesh_nodes.cpp): when an editor asks for these layers (Display ▸ Contour spectrum / Contour tree), the node starts overlay tasks on the app's state_exec scheduler: cooperative slices on single-threaded wasm, the background pool where threads exist. There's one task per layer, computed over the published field_colors.range so the layers line up with the histogram.

  • The tree is built on the values clamped to that range. A piecewise-linear field's level-set components inside the range depend only on which vertices lie above, so clamping keeps them; it drops the outlier extrema of a curvature's 2–98 % range. The tree is capped at 200 nodes.
  • The spectrum uses the values as they are.
  • New values cancel the running tasks and clear the layers. fe_lab's elements view (one value per element) reports them unavailable.

Tests (Windows, MSVC; all pass):

  • mesh_contour_test (13 tests):
    • the mesh tree equals the grid builder's on 24 random grids, plateaus included, stepped or not;
    • f = x on unit-cube tetrahedra gives area 1, volumes w / 1−w, gradient 1;
    • unit-square triangles are exact to 1e-12, including a diagonal field;
    • stepping the spectrum changes no bit;
    • 80 random fields on a torus and an annulus each give one tree;
    • separate pieces, NaN points, bad input;
    • an overlay_job matches the one-shot result however it's stepped;
    • a scheduled task publishes into state.
  • cvcgl_ariadne_realize: both FEM nodes publish a 256-sample spectrum and a tree on request, and keep them across a recolour.
  • Unchanged and still passing: contour_tree_test (40), contour_spectrum_test (25), tf_overlays_test (18), tf_overlay_tasks_test (15), cvcgl_tf_editor, cvcgl_tf_binding, cvcgl_fe_interval.

Visual check: arigl snapshots of mesh_lab.ari with both Colours editors showing every layer:

  • fe_lab's Poisson solution: spectrum curves plus a small tree;
  • mesh_lab's mean curvature: spectrum plus a busy 200-node tree. Curvature on a scanned surface has many extrema.

Not covered by a test: the elements-view note.

Docs: docs/TRANSFER_FUNCTIONS.md §5 ("On a mesh") and §6 (field colours), and docs/ARIADNE.md "Field colours".

Base automatically changed from fix/tf-editor-fem-colours to master October 29, 2026 23:01
The FEM demos' Colours editors could not show the contour spectrum or the
contour tree: both were computed from volumes only, and the mesh_lab /
fe_lab fields reported them unavailable. They now come from the field's
own mesh.

- cvc::contour_mesh (cvc/volume/contour_mesh.h): a scalar field on a
  simplicial mesh, one value per point, linear over each tetrahedron, or
  over each triangle of a surface.
- mesh_contour_spectrum_builder and compute_contour_spectrum(contour_mesh):
  on tetrahedra, the grid version's per-tetrahedron integrals with each
  tetrahedron's exact gradient. On triangles, exact per-triangle
  integrals: the contours' length, the area below and above, and |grad f|^2
  along them. Resumable; stepping changes no bit.
- mesh_contour_tree_builder and compute_contour_tree(contour_mesh): Carr's
  join / split / merge over the graph of the elements' edges, resumable in
  every phase (a bottom-up merge sort). On a grid's own tetrahedra it
  builds exactly the grid builder's tree. NaN points are left out with
  their edges, and pieces get a tree each. A domain whose level sets loop
  still gives a tree.
- overlay_job, compute_overlays and schedule_overlays take a mesh.
  overlay_options::has_range computes the layers over a given range.
- mesh_lab / fe_lab: an editor's request starts overlay tasks on the
  state_exec scheduler (cooperative where there are no threads) over the
  published range, one per layer. The tree is built on the values clamped
  to that range, which keeps its components there and drops the
  far-outlier extrema, and capped at 200 nodes. New values cancel the
  tasks and clear the layers. fe_lab's elements view (one value per
  element) reports them unavailable.

Tests: mesh_contour_test (13: against the grid tree on random data with
plateaus, exact linear-field spectra on cubes and squares, stepping, loopy
and disconnected domains, errors, a job and a scheduled task), and
cvcgl_ariadne_realize (both FEM nodes publish the layers on request).
@transfix
transfix force-pushed the feat/mesh-contour-overlays branch from 79fb85e to d974a66 Compare October 29, 2026 23:02
@transfix
transfix enabled auto-merge (squash) October 29, 2026 23:12
@transfix
transfix merged commit 13d8902 into master Oct 29, 2026
20 of 27 checks passed
@transfix
transfix deleted the feat/mesh-contour-overlays branch October 29, 2026 23:40
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