Skip to content

IS-R1: WebGL2 brain mesh and contact renderer - #547

Merged
neuromechanist merged 11 commits into
feature/issue-537-epic-spatialfrom
feat/is-r1-renderer
Nov 18, 2026
Merged

neuromechanist merged 11 commits into
feature/issue-537-epic-spatialfrom
feat/is-r1-renderer

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Card IS-R1 of the iEEG spatial view plan (the intracranial track of epic #537).

What changed

Three new modules under src/lib/ieeg-spatial/, each with its tests. Nothing imports them yet; the panel that mounts the renderer is card IS-V1.

  • camera.ts: turntable orbit camera in right-anterior-superior (RAS) millimeters with +z up; six view presets (left, right, front, back, top, bottom); view and projection matrices; project and screenRay; fit, orbit and zoom with bounds; rescaleForAspect, which keeps a fit valid when the canvas changes shape.
  • mesh-format.ts: the brain-mesh v1 reader, a matching encoder for tests and tools, vertexNormals, and icosphere. The reader refuses any file whose bytes it cannot account for.
  • brain-gl.ts: createBrainRenderer(canvas, themeColors), returning null when WebGL2 or shader compilation is missing. Methods: setMesh, setContacts, setColors, setSelected, setView, pick, render, resize, snapshot, dispose, plus setTheme. Translucent Lambert plus rim mesh (alpha 0.25, depth writes off, drawn after the opaque contacts), one instanced icosphere for up to 2,000 contacts (radius sqrt(size / pi) clamped to 0.5 to 3 mm, 1.5 mm when size is n/a), a selection ring drawn on top, CPU ray picking (never a second render pass), drawing on demand only (no loop when idle), context loss and restore rebuilt from the last inputs, device pixel ratio capped at 2.

Additions beyond the card's method list (accepted in review round 1): setTheme(colors) with BrainTheme = background, outline, mesh, contact; setMesh(mesh, alpha = 0.25); a pick floor of 4 CSS pixels; swallowing the click that ends a drag; size missing, null, NaN, zero or negative counts as n/a (1.5 mm); more than 2,000 contacts throws RangeError.

Review round 1 (this push)

  • Camera fit after a layout change: resize() now rescales distance, minDistance and maxDistance by the ratio of the fit's half-angle sines when min(1, aspect) changed since the last fit (a 0 x 0 canvas counts as aspect 1), so a brain loaded into a hidden or landscape canvas still fits when the canvas turns portrait, and the user's zoom is kept. Measured in headless Chromium: before, the brain filled the whole 300 px width (x 2 to 297); after, x 74 to 225.
  • Picking: exact sphere hits win, nearest to the eye first; only when none is under the pointer does the pick floor apply, and then the contact whose center is closest in pixels wins. pick(x, y, minPx = MIN_PICK_PX) takes a larger floor for coarse pointers; MIN_PICK_PX is exported for that.
  • Translucent contacts (alpha strictly between 0 and 1) are drawn in a second pass with depth writes off, farthest first, from their own instance buffers refilled in that order. The opaque pass skips them. Measured in headless Chromium: a half-transparent contact in front of an opaque one gave different pixels depending on index order before, and the same pixel in either order after.
  • packContacts and BrainContact document that the caller filters contacts without a position (IS-C1's Contact.position is Vec3 | null) and keeps its own index map; a null or non-finite position throws RangeError, tested.
  • travelled is now traveled; docblocks say that after dispose() or a failed createBrainRenderer the canvas holds a deliberately lost context, so each renderer needs a fresh canvas element.

Notes for IS-T1 and IS-V1

  • brain-mesh v1 header, little-endian, as implemented and pinned by a hand-built known-answer test: bytes 0 to 3 magic NMBM; byte 4 u16 version (1); byte 6 u16 index width in bits (16 or 32); byte 8 u32 vertex count; byte 12 u32 index count; byte 16 f32 scale x, y, z; byte 28 f32 offset x, y, z; byte 40 i16 positions (x, y, z per vertex, position = q * scale + offset, q within -32767 to 32767), then the indices. No padding, no normals.
  • IS-T1 ships two hemispheres but setMesh takes one mesh, so IS-V1 merges them (concatenate positions, offset the second index list) before the call.

Gates (head be9b1f3, the epic tip 366e4d4 merged in)

  • bun install --frozen-lockfile: Checked 416 installs across 568 packages (no changes)
  • bun run typecheck: 0 errors, 0 warnings, 19 hints (494 files)
  • bun run lint: Checked 427 files in 200ms. No fixes applied.
  • bun run test src/lib/ieeg-spatial: Test Files 4 passed (4), Tests 85 passed (85) (the 64 of this card plus 21 of IS-M1's activity.test.ts)
  • bun run test: Test Files 179 passed | 3 skipped (182), Tests 5050 passed | 14 skipped (5064)
  • Bundle, measured: astro build of the epic tip (517bfee) and of this branch with a temporary importer of createBrainRenderer and decodeBrainMesh in viewer.ts (not committed). The lazy chunk viewer.*.js goes from 161,956 to 179,085 bytes raw and from 56,848 to 62,747 bytes with gzip -9c <chunk> | wc -c, a gain of 5,899 bytes gzipped against the 14 KB budget. The three generate:* Open Graph image steps of bun run build were skipped (they render thousands of cards from the live catalog and do not touch JavaScript chunks).
  • Leak check on all six files: all three commands print nothing.

Mutation checks

Each test was shown to fail under a one-line mutation, reverted afterwards (git status --porcelain empty after each). The card's three: swapping the left and right preset vectors fails 5 tests; returning the first exact hit instead of the nearest fails 2; dropping the offset in the decoder fails 5. Round 1 adds 13 more, one or more per new test: floor hit beating an exact hit, first floor hit instead of the closest, minPx ignored, translucent order ascending, opaque or hidden contacts in the translucent list, translucent depth sign flipped, null position not refused, rescale not moving the distance, inverted ratio, bounds left unscaled, aspect clamp dropped. Round 2 strengthened the closest-floor-hit test to run in both array orders, so neither "first floor hit wins" nor "last wins" (the comparison replaced by true) passes. In all, 63 of 64 mutations are caught. The survivor is equivalent: removing the flat-axis guard in the encoder still writes 0, because DataView.setInt16(NaN) stores 0.

Real-browser evidence (scratch, not committed)

Not the IS-QA1 harness. A headless Chromium run on software GL against a bundle of these modules, outside the repository:

  • Mesh and 2,000 contacts draw with no GL error; orbit by pointer drag, wheel and two-pointer pinch change the image; the page does not scroll on wheel; a drag does not click for the panel, a tap does.
  • Idle: 0 frames in 500 ms; one setSelected draws exactly 1 frame.
  • Cleanup: after 20 rounds of replacing mesh, contacts and colors and a double dispose(), buffers, vertex arrays, programs and shaders created equal those deleted.
  • Context loss: loseContext() then restoreContext() gives a pixel-identical frame with no console warnings; snapshot() while lost throws.
  • Device pixel ratio 3 gives a 2x drawing buffer. With WebGL disabled, createBrainRenderer returns null.

Not run, would check

  • Every CI job green on the final head, because the manager reads gh pr checks.
  • A mesh and contacts drawing and orbiting in the card's own browser harness, because that is IS-QA1.
  • Picking against the first 20 rows of the IS-C1 fixture: that card is not on the epic branch yet, so the picking test uses the first 20 vertices of a 70 mm icosphere, as the card allows.
  • Safari and Firefox: only Chromium was available.

Card IS-R1. Part of #537.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Deploying nemar-website with  Cloudflare Pages  Cloudflare Pages

Latest commit: be9b1f3
Status: ✅  Deploy successful!
Preview URL: https://05b3a154.nemar-website.pages.dev
Branch Preview URL: https://feat-is-r1-renderer.nemar-website.pages.dev

View logs

@neuromechanist
neuromechanist merged commit 055e1c7 into feature/issue-537-epic-spatial Nov 18, 2026
5 checks passed
@neuromechanist
neuromechanist deleted the feat/is-r1-renderer branch November 18, 2026 00:04
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