GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Kuiper, cosmos and drawing engines / kuiper-belt / see-through/202610050356

See3d and 3 more files

A JavaScript file in the see through folder of Kuiper Belt that maps substations footprints odbl and see DEM, and uses data from OpenStreetMap (Overpass) and the GridAtlas repository.

676 lines of JavaScript and CSS and HTML from kuiper-belt, in 4 files. Topic: Kuiper, cosmos and drawing engines. Section 1543 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. Pf geodesy · kuiper-belt / see-through/202610050356/pf.js lines 1–219
  2. Kuiper Belt — See Through Page render · kuiper-belt / see-through/202610050513/index.html lines 1–24
  3. Plain render · kuiper-belt / see-through/202610050609/plain.css lines 1–44
  4. See3d interaction · kuiper-belt / see-through/202610050609/see3d.js lines 1–389

Pf

A JavaScript module in the see through folder of Kuiper Belt that steps lon, converts to local and mercator and calculates great-circle distances and planar distances, using OSTN15 and Helmert.

geodesykuiper-belt / see-through/202610050356/pf.js · lines 1–219 of 219 · version 2026-10-05 03:56 · Entry point · pattern circumferenceAtLatitude (shared by 2 files)

What it does
Defines 31 functions or classes, including ecef, geodetic, axes, dot, frame. Side effects: changes the web page (DOM).
Why it exists in the grid workflow
Domain stage agent_infra (repository name word 'kuiper'): AI-agent memory, coordination and code audit (the multi-agent journey named in Ventus Ltd's brief).
Inputs & outputs
Coordinate systems named: OSGB36 / EPSG:27700 (British National Grid), WGS84 / EPSG:4326, ETRS89 / EPSG:4258.
Downstream impact
Used directly by 2 files: Model (kuiper-belt); Kuiper Belt — See Through Page (kuiper-belt, https://ventusltd.github.io/kuiper-belt/see-through/202610050513/).
What it achieves
It places key (`placeKey`), wires map (`wireToMap`) and maps wire (`mapToWire`). Classic-script copy of the simulator's `place-frame.mjs` (GlobalGrid2050 origin/main, `energy-transition-simulator/202609282123/place-frame.mjs`), made by tools: every line below the wrapper is the original with only the word 'export' removed.
Why it matters
Loaded by the See Through page at https://ventusltd.github.io/kuiper-belt/see-through/202610050513/. Also imported by Model.
Application
kuiper-belt · see-through/202610050356 · JavaScript module
Uses
.../world/bng.mjs
Used by
required by Model (require), loaded by Kuiper Belt — See Through Page (script src)
Produces
ecef, geodetic, axes, dot, frame, stepLon, anchorFromKey, placeKey, enu, fromEnu, toLocal, fromLocal
Defines here
ecef, geodetic, axes, dot, frame, stepLon, anchorFromKey, placeKey, enu, fromEnu, toLocal, fromLocal, mercatorX, mercatorY, lngFromMercatorX, latFromMercatorY, circumferenceAtLatitude, mercatorZ, mercatorScale, meterInMercator, toMercator, fromMercator, wireToMap, mapToWire, wireBuffer, useWorld, needWorld, toBng, fromBng, wireToBng
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
kuiper-belt@26fba12ab1b5bab8e71934ab86c58273e5f32682 see-through/202610050356/pf.js:1-219 · blob 786a9cafaacbcf51eb6ca7e0298d53aa41e63a83 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom · domain stage: agent_infra · users: ai_agent · canonical: duplicate_candidate
// pf.js - classic-script copy of the simulator's place-frame.mjs (globalgrid2050 origin/main c5ac5219,
// energy-transition-simulator/202609282123/place-frame.mjs), made by tools: every line below the wrapper is the
// original with only the word 'export' removed. Owner's code, reused under their permission (see NOTICE.md).
// Exposes window.STPF (browser) or module.exports (node).
(function (root) {
'use strict';
// place-frame.mjs: the one coordinate system between GridAtlas (the MapLibre map) and the 3D wireframe.
// Dependency-free, pure functions, no DOM. Every wireframe object is authored in metres around an anchor
// (a place key on a 100 m lattice); this file turns those metres into the Mercator vertices the overlay's
// custom layer draws, and turns map positions back into metres.
//
// WHAT IS EXACT, AND WHAT IS A CONVENTION
//   - Horizontal: exact WGS84 ellipsoid. A local point (x, y) is a point on the anchor's tangent plane
//     (x east, y north, through ECEF); its latitude and longitude are the foot of the ellipsoid normal
//     through it. So nothing is scaled by a sphere: the old overlay path (metres times
//     meterInMercatorCoordinateUnits) uses MapLibre's sphere of 6,371,008.8 m and ignores the ellipsoid,
//     which puts the far corner of a 200 m block a few decimetres off (the tests print the figure).
//   - Vertical: z is height above the ellipsoid at that point (the plane's curvature bump is dropped, so
//     z = 0 drapes on the map). In Mercator units z follows MapLibre's mercatorZfromAltitude, because the
//     map's own camera and extrusions use that convention; it is the map's rule, not the earth's.
//   - enu()/fromEnu() give the pure 3D ENU frame for anyone who needs it (no draping).
//   - toMercator/fromMercator/mercatorScale/mercatorZ copy MapLibre GL JS 4.7.1 (mercator_coordinate.ts)
//     term for term, so they match what the map does, not what the earth is.
// Agreement between these functions proves they are consistent with each other, never that the map
// imagery or grid layers are right on the ground.

// ---- WGS84 ----
const WGS84 = { a: 6378137.0, f: 1 / 298.257223563 };
const A = WGS84.a, F = WGS84.f, E2 = F * (2 - F), RAD = Math.PI / 180;

/** Geodetic (degrees, metres) -> ECEF [X, Y, Z] in metres. */
function ecef(lat, lon, h = 0) {
  const p = lat * RAD, l = lon * RAD, s = Math.sin(p), c = Math.cos(p);
  const N = A / Math.sqrt(1 - E2 * s * s);
  return [(N + h) * c * Math.cos(l), (N + h) * c * Math.sin(l), (N * (1 - E2) + h) * s];
}

/** ECEF -> { lat, lon, h } (degrees, metres). Fixed-point iteration, converged to 1e-15 rad. */
function geodetic(X, Y, Z) {
  const p = Math.hypot(X, Y);
  let lat = Math.atan2(Z, p * (1 - E2)), h = 0;
  for (let k = 0; k < 20; k++) {
    const s = Math.sin(lat), N = A / Math.sqrt(1 - E2 * s * s);
    h = Math.abs(Math.cos(lat)) > 1e-9 ? p / Math.cos(lat) - N : Math.abs(Z) - N * (1 - E2);
    const next = Math.atan2(Z, p * (1 - E2 * N / (N + h)));
    const done = Math.abs(next - lat) < 1e-15;
    lat = next;
    if (done) break;
  }
  const s = Math.sin(lat), N = A / Math.sqrt(1 - E2 * s * s);
  h = Math.abs(Math.cos(lat)) > 1e-9 ? p / Math.cos(lat) - N : Math.abs(Z) - N * (1 - E2);
  return { lat: lat / RAD, lon: Math.atan2(Y, X) / RAD, h };
}

// Unit vectors of the local frame at (lat, lon): east, north, up (up is the ellipsoid normal).
function axes(lat, lon) {
  const p = lat * RAD, l = lon * RAD, sp = Math.sin(p), cp = Math.cos(p), sl = Math.sin(l), cl = Math.cos(l);
  return { e: [-sl, cl, 0], n: [-sp * cl, -sp * sl, cp], u: [cp * cl, cp * sl, sp] };
}
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];

// An anchor's frame, computed from its lat/lon (never stored in data; cached per object only).
const FRAMES = new WeakMap();
function frame(anchor) {
  let f = FRAMES.get(anchor);
  if (!f) { f = { o: ecef(anchor.lat, anchor.lon, 0), ...axes(anchor.lat, anchor.lon) }; FRAMES.set(anchor, f); }
  return f;
}

// ---- the place key: a 100 m lattice ----
// Rows: latitude = j * STEP_LAT, with STEP_LAT = 100 / 111320 degrees (the overlay's constant, so row j is
// the overlay's own rounding). On the ellipsoid a row step is 99.3 m at the equator to 100.1 m at 61 N.
// Columns: along each row, exactly 100 m of parallel arc on WGS84 at the row's latitude:
// STEP_LON(j) = 100 / (N(lat_j) cos(lat_j)) radians. So a key is two integers; its anchor is computed.
const STEP_LAT = 100 / 111320;
const LAT_LIMIT = 85.0511287798066; // MapLibre's Mercator edge
function stepLon(latRow) {
  const s = Math.sin(latRow * RAD), N = A / Math.sqrt(1 - E2 * s * s);
  return 100 / (N * Math.cos(latRow * RAD)) / RAD;
}

/** The anchor for key 'pf:j:i' (or { j, i }): { key, j, i, lat, lon }. */
function anchorFromKey(key) {
  let j, i;
  if (typeof key === 'string') {
    const m = /^pf:(-?\d+):(-?\d+)$/.exec(key);
    if (!m) throw new Error(`not a place key: ${key}`);
    j = Number(m[1]); i = Number(m[2]);
  } else ({ j, i } = key);
  const lat = j * STEP_LAT, lon = i * stepLon(lat);
  return { key: `pf:${j}:${i}`, j, i, lat, lon };
}

/** The anchor nearest (lat, lon) on the 100 m lattice. */
function placeKey(lat, lon) {
  if (!(Math.abs(lat) < LAT_LIMIT) || !Number.isFinite(lon)) throw new Error(`no place key at ${lat}, ${lon}`);
  const w = ((lon + 180) % 360 + 360) % 360 - 180; // longitude in [-180, 180)
  const j = Math.round(lat / STEP_LAT);
  return anchorFromKey({ j, i: Math.round(w / stepLon(j * STEP_LAT)) });
}

// ---- local metres <-> WGS84 ----

/** Pure ENU: WGS84 (lat, lon, h) -> { x, y, z } in the anchor's east-north-up frame, via ECEF. */
function enu(anchor, lat, lon, h = 0) {
  const f = frame(anchor), g = ecef(lat, lon, h), d = [g[0] - f.o[0], g[1] - f.o[1], g[2] - f.o[2]];
  return { x: dot(f.e, d), y: dot(f.n, d), z: dot(f.u, d) };
}

/** Pure ENU inverse: { lat, lon, h }. */
function fromEnu(anchor, x, y, z = 0) {
  const f = frame(anchor), P = [0, 1, 2].map(k => f.o[k] + x * f.e[k] + y * f.n[k] + z * f.u[k]);
  return geodetic(P[0], P[1], P[2]);
}

/** Wireframe metres from WGS84: (x, y) where the ellipsoid normal through (lat, lon) meets the anchor's
 *  tangent plane; z = h, height above the ellipsoid. Exact inverse of fromLocal. */
function toLocal(anchor, lat, lon, h = 0) {
  const f = frame(anchor), g = ecef(lat, lon, 0), nrm = axes(lat, lon).u;
  const d = [g[0] - f.o[0], g[1] - f.o[1], g[2] - f.o[2]];
  const t = -dot(f.u, d) / dot(f.u, nrm); // along the normal to the plane
  const P = [d[0] + t * nrm[0], d[1] + t * nrm[1], d[2] + t * nrm[2]];
  return { x: dot(f.e, P), y: dot(f.n, P), z: h };
}

/** WGS84 { lat, lon, h } of wireframe metres (x, y on the tangent plane, z above the ellipsoid). */
function fromLocal(anchor, x, y, z = 0) {
  const g = fromEnu(anchor, x, y, 0);
  return { lat: g.lat, lon: g.lon, h: z, planeHeight: g.h };
}

// ---- MapLibre's Mercator (maplibre-gl 4.7.1, copied term for term) ----
const MAPLIBRE_EARTH_RADIUS = 6371008.8;
const CIRC = 2 * Math.PI * MAPLIBRE_EARTH_RADIUS;
const mercatorX = lng => (180 + lng) / 360;
const mercatorY = lat => (180 - (180 / Math.PI * Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360)))) / 360;
const lngFromMercatorX = x => x * 360 - 180;
const latFromMercatorY = y => 360 / Math.PI * Math.atan(Math.exp((180 - y * 360) * Math.PI / 180)) - 90;
const circumferenceAtLatitude = lat => CIRC * Math.cos(lat * Math.PI / 180);
const mercatorZ = (alt, lat) => alt / circumferenceAtLatitude(lat);
/** MapLibre's mercatorScale(lat) = 1 / cos(lat). */
const mercatorScale = lat => 1 / Math.cos(lat * Math.PI / 180);
/** MapLibre's MercatorCoordinate.meterInMercatorCoordinateUnits() at latitude lat (sphere, 6,371,008.8 m). */
const meterInMercator = lat => 1 / CIRC * mercatorScale(latFromMercatorY(mercatorY(lat)));

/** = MercatorCoordinate.fromLngLat([lon, lat], alt): { x, y, z }. */
function toMercator(lat, lon, alt = 0) {
  return { x: mercatorX(lon), y: mercatorY(lat), z: mercatorZ(alt, lat) };
}
/** Inverse: { lat, lon, alt }. */
function fromMercator(x, y, z = 0) {
  const lat = latFromMercatorY(y);
  return { lat, lon: lngFromMercatorX(x), alt: z * circumferenceAtLatitude(lat) };
}

// ---- wireframe <-> map ----

/** The Mercator vertex for wireframe metres (x, y, z) around anchor: { x, y, z } for the custom layer, plus
 *  dx, dy, dz relative to the anchor's own Mercator point. Upload dx/dy/dz as float32 and put the anchor in
 *  the matrix: absolute float32 Mercator near 0.3 has a step of 3e-8, about 1.2 m on the ground. */
function wireToMap(anchor, x, y, z = 0) {
  const g = fromLocal(anchor, x, y, z), m = toMercator(g.lat, g.lon, z), o = toMercator(anchor.lat, anchor.lon, 0);
  return { x: m.x, y: m.y, z: m.z, dx: m.x - o.x, dy: m.y - o.y, dz: m.z };
}

/** Inverse of wireToMap: Mercator (x, y, z) -> wireframe metres { x, y, z } around anchor. */
function mapToWire(anchor, mx, my, mz = 0) {
  const g = fromMercator(mx, my, mz);
  return toLocal(anchor, g.lat, g.lon, g.alt);
}

/** A whole line list [[x0,y0,z0,x1,y1,z1], ...] -> Float32Array of anchor-relative Mercator vertices. */
function wireBuffer(anchor, lines) {
  const out = new Float32Array(lines.length * 6);
  lines.forEach((L, k) => {
    for (let v = 0; v < 2; v++) {
      const w = wireToMap(anchor, L[3 * v], L[3 * v + 1], L[3 * v + 2]);
      out.set([w.dx, w.dy, w.dz], 6 * k + 3 * v);
    }
  });
  return out;
}

// ---- Inside GB: the world's National Grid ----
// Injected, so this file stays dependency-free and the world's own code is the one that runs:
//   useWorld(await import('.../world/bng.mjs'), { blocks, etrsToBng, bngToEtrs })   // second argument optional
// With OSTN15 blocks loaded for the place, toBng/fromBng use OSTN15 (the OS definitive transformation, about
// 0.1 m) and report the Helmert gap; elsewhere they return the Helmert answer, flagged. GPS/WGS84 is taken as
// ETRS89 here: the two part by about 0.9 m by 2026 (plate motion), which is a stated, known error.
let WORLD = null;
function useWorld(bng, ostn = null) {
  if (!bng || typeof bng.wgs84ToBng !== 'function' || typeof bng.bngToWgs84 !== 'function') throw new Error('useWorld needs the world bng.mjs');
  WORLD = { bng, ostn };
}
const needWorld = () => { if (!WORLD) throw new Error('call useWorld(bng) first'); return WORLD; };

/** WGS84 -> National Grid { e, n, engine, helmert, gapToOstn15 } (gap in metres, null if no OSTN15 here). */
function toBng(lat, lon) {
  const W = needWorld(), h = W.bng.wgs84ToBng(lat, lon);
  const o = W.ostn ? W.ostn.etrsToBng(W.ostn.blocks, lat, lon) : null;
  if (o) return { e: o.e, n: o.n, engine: 'OSTN15', helmert: h, gapToOstn15: Math.hypot(h.e - o.e, h.n - o.n) };
  return { e: h.e, n: h.n, engine: 'Helmert', helmert: h, gapToOstn15: null };
}

/** National Grid -> WGS84 { lat, lon, engine }. */
function fromBng(e, n) {
  const W = needWorld(), o = W.ostn ? W.ostn.bngToEtrs(W.ostn.blocks, e, n) : null;
  if (o) return { lat: o.lat, lon: o.lon, engine: 'OSTN15' };
  const h = W.bng.bngToWgs84(e, n);
  return { lat: h.lat, lon: h.lon, engine: 'Helmert' };
}

/** Wireframe metres -> National Grid, and back: the two places tied by the same key must agree. */
const wireToBng = (anchor, x, y) => { const g = fromLocal(anchor, x, y, 0); return toBng(g.lat, g.lon); };
const bngToWire = (anchor, e, n) => { const g = fromBng(e, n); return toLocal(anchor, g.lat, g.lon, 0); };

var API = { WGS84: WGS84, ecef: ecef, geodetic: geodetic, STEP_LAT: STEP_LAT, LAT_LIMIT: LAT_LIMIT, anchorFromKey: anchorFromKey, placeKey: placeKey, enu: enu, fromEnu: fromEnu, toLocal: toLocal, fromLocal: fromLocal, MAPLIBRE_EARTH_RADIUS: MAPLIBRE_EARTH_RADIUS, mercatorX: mercatorX, mercatorY: mercatorY, lngFromMercatorX: lngFromMercatorX, latFromMercatorY: latFromMercatorY, circumferenceAtLatit  … [line shortened here: 733 characters; full line on GitHub]
if (typeof module !== 'undefined' && module.exports) module.exports = API; else root.STPF = API;
})(typeof window !== 'undefined' ? window : globalThis);

Kuiper Belt — See Through Page

A web page. Published at https://ventusltd.github.io/kuiper-belt/see-through/202610050513/.

renderkuiper-belt / see-through/202610050513/index.html · lines 1–24 of 24 · version 2026-10-05 05:13 · Entry point

What it does
It draws its content. Uses libraries vendor/maplibre-gl.css, grid-data.js, vendor/maplibre-gl.js. Side effects: changes the web page (DOM).
Why it exists in the grid workflow
Domain stage agent_infra (repository name word 'kuiper'): AI-agent memory, coordination and code audit (the multi-agent journey named in Ventus Ltd's brief). README: "Download the newest dated folder and open `index.html` in a web browser."
Inputs & outputs
Out: web page.
Downstream impact
It is itself an entry point, published at https://ventusltd.github.io/kuiper-belt/see-through/202610050513/.
What it achieves
The README says: Download the newest dated folder and open `index.html` in a web browser. It responds to error events.
Why it matters
No other file in the estate was found to import it; it is an entry point.
Application
kuiper-belt · see-through/202610050513 · page
Uses
Plain (stylesheet), Kuiper Law (script src), Pf (script src), Model (script src), See3d (script src), Descent (script src), vendor/maplibre-gl.css, grid-data.js, vendor/maplibre-gl.js
Used by
none found
Produces
none declared
Defines here
none
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
kuiper-belt@26fba12ab1b5bab8e71934ab86c58273e5f32682 see-through/202610050513/index.html:1-24 · blob b069ce1d879eec5c8f0c8bc4fe12e10a3dadd1c1 · GitHub at these lines
State
achieves: readme · context: readme_derived · stage: output · effects: dom · domain stage: agent_infra · users: ai_agent · canonical: unknown
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<title>See Through</title>
<link rel="icon" href="data:,">
<link rel="stylesheet" href="vendor/maplibre-gl.css">
<link rel="stylesheet" href="plain.css">
<style>html,body{margin:0;height:100%;background:#000;overflow:hidden}#host{position:fixed;inset:0;background:#000}
</style></head>
<body><div id="host"></div>
<script src="kuiper-law.js"></script>
<script src="grid-data.js"></script>
<script src="vendor/maplibre-gl.js"></script>
<script src="pf.js"></script>
<script src="model.js"></script>
<script src="see3d.js"></script>
<script src="descent.js"></script>
<script>
(function(){
  var m=(window.KGModules||[]).filter(function(x){return x.id==='descent'})[0], host=document.getElementById('host');
  m.standalone = true;
  window.__errors=[]; window.addEventListener('error',function(e){window.__errors.push(String(e.message));});
  m.open(host);
})();
</script></body></html>

Plain

A style sheet in the see through folder of Kuiper Belt.

renderkuiper-belt / see-through/202610050609/plain.css · lines 1–44 of 44 · version 2026-10-05 06:09 · Entry point

What it does
Side effects: changes the web page (DOM).
Why it exists in the grid workflow
Domain stage agent_infra (repository name word 'kuiper'): AI-agent memory, coordination and code audit (the multi-agent journey named in Ventus Ltd's brief).
Downstream impact
Used directly by 1 file: Kuiper Belt — See Through Page (kuiper-belt, https://ventusltd.github.io/kuiper-belt/see-through/202610050513/).
What it achieves
ST1: presentation only. Keep drawing colours, input handlers and audio.
Why it matters
Loaded by the See Through page at https://ventusltd.github.io/kuiper-belt/see-through/202610050513/.
Application
kuiper-belt · see-through/202610050609 · style sheet
Uses
none found
Used by
loaded by Kuiper Belt — See Through Page (stylesheet)
Produces
none declared
Defines here
none
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
kuiper-belt@26fba12ab1b5bab8e71934ab86c58273e5f32682 see-through/202610050609/plain.css:1-44 · blob 28684671eda994fa20745543ad63c6e8c7edf3ab · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom · domain stage: agent_infra · users: ai_agent · canonical: unknown
/* ST1: presentation only. Keep drawing colours, input handlers and audio. */
.st-plain, .st-plain div, .st-plain button, .st-plain input, .st-plain a {
  color: #fff !important;
  font-family: system-ui, sans-serif !important;
  font-weight: 400 !important;
  font-variant-numeric: tabular-nums;
  text-shadow: none !important;
  letter-spacing: normal !important;
}
.st-plain button, .st-plain input {
  border: 1px solid #fff !important;
  border-radius: 0 !important;
  background: #000 !important;
  opacity: 1 !important;
  font-size: 24px !important;
}
.st-plain button[data-on="1"] { text-decoration: underline; text-underline-offset: 6px; }
.st-plain button:focus-visible, .st-plain input:focus-visible { outline: 2px solid #fff !important; outline-offset: 3px; }
/* Transparent full-screen containers stay transparent; textual blocks get black backing. */
.st-plain div[style*="font:"], .st-plain div[style*="font-size:"], .st-plain a { background-color: #000 !important; }
.st-plain { background-color: transparent !important; }
.st-plain div[style*="border-radius:10px"] { border-color: #fff !important; border-radius: 0 !important; }
.st-plain .st-model-note { font-size: 24px !important; border: 0 !important; border-radius: 0 !important; padding: 4px !important; }
.st-plain .st-model-counts { font-size: 24px !important; line-height: 1.25 !important; }

/* ST2 edge layout; the text never occupies the circle. */
.st-plain button { font-size:20px !important; }
.st-plain .st-edge { white-space:nowrap !important; overflow-x:auto !important; overflow-y:hidden !important; max-height:24px !important; background:#000 !important; color:#fff !important; font-weight:400 !important; pointer-events:auto; }
.st-plain .st-model-counts { font-size:14px !important; line-height:22px !important; }
.st-plain .st-network { pointer-events:none; }
.st-plain .st-joystick { border-color:#fff !important; background:rgba(0,0,0,.65) !important; }
.st-plain .st-joystick > div { border-color:#fff !important; }

/* ST3: only the words have backing; preserve every wrapped fact and credit. */
.st-plain .st-edge.st-wrap {
  white-space:normal !important; overflow:visible !important; max-height:none !important;
  width:fit-content; box-sizing:border-box; overflow-wrap:anywhere;
  background:transparent !important; pointer-events:none;
}
.st-plain .st-words {
  color:#fff; background:#000; padding:1px 3px;
  -webkit-box-decoration-break:clone; box-decoration-break:clone;
}
.st-plain .st-attribution { font-size:11px !important; line-height:16px !important; }

See3d

A JavaScript file in the see through folder of Kuiper Belt that maps substations footprints odbl and see DEM, and uses data from OpenStreetMap (Overpass) and the GridAtlas repository.

interactionkuiper-belt / see-through/202610050609/see3d.js · lines 1–389 of 389 · version 2026-10-05 06:09 · Entry point · pattern footprints (shared by 2 files)

What it does
It maps substations footprints odbl and see DEM. Defines 27 functions or classes, including getJSON, footprints, lines, osmDetail, others. Side effects: changes the web page (DOM); makes network requests.
Why it exists in the grid workflow
Domain stage agent_infra (repository name word 'kuiper'): AI-agent memory, coordination and code audit (the multi-agent journey named in Ventus Ltd's brief).
Inputs & outputs
In: data source: gridatlas data; data source: OpenStreetMap (Overpass); JSON file substations-footprints.odbl.json; GeoJSON file kv.geojson; JSON file index.json. Out: interactive map.
Downstream impact
Used directly by 1 file: Kuiper Belt — See Through Page (kuiper-belt, https://ventusltd.github.io/kuiper-belt/see-through/202610050513/).
What it achieves
It gets JSON (`getJSON`), fits zoom (`fitZoom`) and reads grounds (`readGrounds`). HOW IT IS DRAWN (the simulator overlay's method, reused): - The map is the porthole's MapLibre GL JS 4.7.1 map (`descent.js`). It responds to pointerdown, pointermove and pointerup events.
Why it matters
Loaded by the See Through page at https://ventusltd.github.io/kuiper-belt/see-through/202610050513/.
Application
kuiper-belt · see-through/202610050609 · script
Uses
none found
Used by
loaded by Kuiper Belt — See Through Page (script src)
Produces
getJSON, footprints, lines, osmDetail, others, program, sh, ease, pack, groundArray, fadeNow, enter
Defines here
getJSON, footprints, lines, osmDetail, others, program, sh, ease, pack, groundArray, fadeNow, enter, fitZoom, tipTo, build, elevation, readGrounds, tick, move, stopEase, ui, joyAt, jup, up, setChips, phoneLayout, exit
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
kuiper-belt@26fba12ab1b5bab8e71934ab86c58273e5f32682 see-through/202610050609/see3d.js:1-389 · blob 76d49437f49ca4a579cbbf3f49331865f26f8265 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom, network · domain stage: agent_infra · users: ai_agent · canonical: unknown
// see3d.js - SEE THROUGH stage 4: the porthole's own map tips over and one substation rises on the real satellite
// ground, drawn by rule (model.js) with the owner's pylons marching away along the real lines.
//
// HOW IT IS DRAWN (the simulator overlay's method, reused):
//   - The map is the porthole's MapLibre GL JS 4.7.1 map (descent.js). Ground: Esri World Imagery draped on AWS Terrain
//     Tiles (terrarium), exactly the overlay's sources (overlay.html:43, :46, :296).
//   - The drawing is ONE custom WebGL layer inside the map's own camera (overlay.html:79-101), GL_LINES only. Vertices
//     are anchor-relative Mercator from place-frame (pf.js = place-frame.mjs), the anchor folded into the matrix
//     (overlay.html:34-38 withAnchor), each vertex lifted by the terrain height under it (perf.js:106-109 reads it per
//     block; here per ground reference, re-read when terrain tiles arrive, at most 300 queries a frame as perf.js).
//   - The look is wire-look.js: #9cdbff hairlines, premultiplied alpha 0.85 over imagery, fading with view depth past
//     the centre by world/fade.mjs's rule (450 m + 2.5 m per metre of height, to 4 km) (wire-look.js:17-32, :49-57).
//     Conductors keep pylons-real's colours by voltage (pylons-real.js:20-22).
//   - The arrival is morph.js's idea per piece: a height scale 0 to 1 with a cubic in-out (morph.js:26-30), started
//     piece by piece; conductors and busbars "draw themselves" by a reveal parameter along their length.
// Controls: drag to orbit, wheel or pinch to zoom, the overlay's joystick on touch to fly (overlay.html:124-129,
// prospect/snippets/joystick.js), keys W A S D, arrows, Q E, R F, + -. Any tap during the arrival skips to the end.
(function () {
  'use strict';
  var SCRIPT = (document.currentScript && document.currentScript.src) || location.href;
  var BASE = SCRIPT.replace(/[^\/]*$/, '');
  var GA = 'https://ventusltd.github.io/gridatlas/atlas/releases/202608300453-atlas-v9/data/';
  var DEM = { type: 'raster-dem', tiles: ['https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png'], tileSize: 256, encoding: 'terrarium', maxzoom: 15 };  // overlay.html:46
  var OVERPASS = 'https://overpass-api.de/api/interpreter';
  var FONT = '"Segoe UI", Arial, sans-serif';
  var C = 2 * Math.PI * 6371008.8;
  var X = null;                 // live state while in 3D
  var cache = { fp: null, lines: null, osm: {}, linesErr: false };

  // ---------------- data ----------------
  function getJSON(u, ms) {
    var ctl = window.AbortController ? new AbortController() : null, t = ctl && setTimeout(function () { ctl.abort(); }, ms || 20000);
    return fetch(u, ctl ? { signal: ctl.signal } : {}).then(function (r) { clearTimeout(t); if (!r.ok) throw Error(u + ' ' + r.status); return r.json(); });
  }
  function footprints() {
    if (cache.fp) return cache.fp;
    cache.fp = getJSON(BASE + 'data/substations-footprints.odbl.json').then(function (j) {
      var byI = {}, all = [];
      (j.f || []).forEach(function (r) { var ring = r[2], w = 180, s = 90, e = -180, n = -90; ring.forEach(function (p) { if (p[0] < w) w = p[0]; if (p[0] > e) e = p[0]; if (p[1] < s) s = p[1]; if (p[1] > n) n = p[1]; });
        var o = { i: r[0], ring: ring, bbox: [w, s, e, n] }; byI[r[0]] = o; all.push(o); });
      return { byI: byI, all: all };
    }).catch(function () { return { byI: {}, all: [] }; });
    return cache.fp;
  }
  function lines() {   // GridAtlas's own line files (the ones descent.js draws for GRID), with a box per line (pylons-real.js:47-54)
    if (cache.lines) return cache.lines;
    cache.lines = Promise.all(['400', '275', '220', '132', '66'].map(function (kv) {
      return getJSON(GA + 'grid_' + kv + 'kv.geojson', 30000).then(function (j) {
        var L = [];
        (j.features || []).forEach(function (f) { var g = f.geometry; if (!g) return;
          (g.type === 'LineString' ? [g.coordinates] : g.type === 'MultiLineString' ? g.coordinates : []).forEach(function (c) {
            var w = 180, s = 90, e = -180, n = -90; c.forEach(function (p) { if (p[0] < w) w = p[0]; if (p[0] > e) e = p[0]; if (p[1] < s) s = p[1]; if (p[1] > n) n = p[1]; });
            L.push({ bbox: [w, s, e, n], coords: c }); }); });   // names are never read
        return [kv, L];
      }).catch(function () { cache.linesErr = true; return [kv, []]; });
    })).then(function (pairs) { var o = {}; pairs.forEach(function (p) { o[p[0]] = p[1]; }); return o; });
    return cache.lines;
  }
  // OpenStreetMap detail: the saved Overpass answer for this site if the app carries one, else one live query
  function osmDetail(rec, ring) {
    var k = rec.i; if (cache.osm[k]) return cache.osm[k];
    if (!cache.osmIndex) cache.osmIndex = getJSON(BASE + 'data/osm/index.json', 8000).catch(function () { return []; });
    cache.osm[k] = cache.osmIndex.then(function (ix) { if (ix.indexOf(k) < 0) throw Error('not saved'); return getJSON(BASE + 'data/osm/' + k + '.json', 8000); })
      .then(function (j) { return { json: j, how: 'saved' }; })
      .catch(function () {
        var q = window.SeeModel.osmQuery(rec, ring);
        var ctl = window.AbortController ? new AbortController() : null, t = ctl && setTimeout(function () { ctl.abort(); }, 15000);
        return fetch(OVERPASS, { method: 'POST', body: 'data=' + encodeURIComponent(q), signal: ctl ? ctl.signal : undefined })
          .then(function (r) { clearTimeout(t); if (!r.ok) throw Error('overpass ' + r.status); return r.json(); })
          .then(function (j) { return { json: j, how: 'live' }; });
      })
      .catch(function () { return { json: null, how: 'none' }; });
    return cache.osm[k];
  }
  function others(all, lat, lon, km) {
    var dl = km / 110.574, dn = km / (111.32 * Math.cos(lat * Math.PI / 180));
    return all.filter(function (o) { return o.bbox[2] > lon - dn && o.bbox[0] < lon + dn && o.bbox[3] > lat - dl && o.bbox[1] < lat + dl; });
  }

  // ---------------- the wire layer ----------------
  var VS = 'uniform mat4 u; uniform float u_mz; uniform float u_e; uniform float u_rise; uniform float u_near; uniform float u_fade;'
    + 'attribute vec4 p; attribute float g; varying float v_a; varying float v_s;'
    + 'void main(){ gl_Position = u * vec4(p.xy, (g - u_e + p.z * u_rise) * u_mz, 1.0); v_s = p.w;'
    + ' v_a = clamp(1.0 - max(gl_Position.w - u_near, 0.0) / u_fade, 0.0, 1.0); }';
  var FS = 'precision mediump float; uniform vec4 u_c; uniform float u_reveal; varying float v_a; varying float v_s;'
    + 'void main(){ if (v_s > u_reveal) discard; float a = u_c.a * v_a; gl_FragColor = vec4(u_c.rgb * a, a); }';
  function program(gl) {
    var sh = function (t, s) { var o = gl.createShader(t); gl.shaderSource(o, s); gl.compileShader(o); if (!gl.getShaderParameter(o, gl.COMPILE_STATUS)) throw Error(gl.getShaderInfoLog(o)); return o; };
    var pr = gl.createProgram(); gl.attachShader(pr, sh(gl.VERTEX_SHADER, VS)); gl.attachShader(pr, sh(gl.FRAGMENT_SHADER, FS)); gl.linkProgram(pr);
    if (!gl.getProgramParameter(pr, gl.LINK_STATUS)) throw Error(gl.getProgramInfoLog(pr));
    var L = {}; ['u', 'u_mz', 'u_e', 'u_rise', 'u_near', 'u_fade', 'u_c', 'u_reveal'].forEach(function (n) { L[n] = gl.getUniformLocation(pr, n); });
    return { pr: pr, L: L, p: gl.getAttribLocation(pr, 'p'), g: gl.getAttribLocation(pr, 'g') };
  }
  function ease(x) { x = Math.max(0, Math.min(1, x)); return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2; }   // morph.js:30

  // Pack the model: positions (anchor-relative Mercator x, y; height above ground in metres; reveal s) and grounds.
  function pack(m) {
    var PF = window.STPF, a = m.anchor, ax = PF.mercatorX(a.lon), ay = PF.mercatorY(a.lat), v = m.v, n = v.length / 7;
    var pos = new Float32Array(n * 4), memo = new Map();
    for (var k = 0; k < n; k++) {
      var x = v[7 * k], y = v[7 * k + 1], key = x.toFixed(3) + ',' + y.toFixed(3), q = memo.get(key);
      if (!q) { var gg = PF.fromLocal(a, x, y, 0); q = [PF.mercatorX(gg.lon) - ax, PF.mercatorY(gg.lat) - ay]; memo.set(key, q); }
      pos[4 * k] = q[0]; pos[4 * k + 1] = q[1]; pos[4 * k + 2] = v[7 * k + 2]; pos[4 * k + 3] = v[7 * k + 6];
    }
    return { pos: pos, n: n, ax: ax, ay: ay, mz: 1 / (C * Math.cos(a.lat * Math.PI / 180)) };
  }
  function groundArray(m, H) {   // per vertex: (1 - gt) H[ga] + gt H[gb]
    var v = m.v, n = v.length / 7, out = new Float32Array(n);
    for (var k = 0; k < n; k++) { var ga = v[7 * k + 3], gb = v[7 * k + 4], gt = v[7 * k + 5]; out[k] = gt ? (1 - gt) * H[ga] + gt * H[gb] : H[ga]; }
    return out;
  }

  var layer = {
    id: 'see-through-3d', type: 'custom', renderingMode: '3d',
    onAdd: function (map, gl) { this.gl = gl; this.P = null; this.buf = null; this.gbuf = null; this.ver = -1; this.gver = -1; },
    onRemove: function (map, gl) { if (this.buf) gl.deleteBuffer(this.buf); if (this.gbuf) gl.deleteBuffer(this.gbuf); this.buf = this.gbuf = null; },
    render: function (gl, args) {
      if (!X || !X.pk) return;
      var t0 = performance.now();
      try {
        if (!this.P) this.P = program(gl);
        var P = this.P, pk = X.pk, m = X.model;
        if (this.ver !== X.ver) { if (!this.buf) this.buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, this.buf); gl.bufferData(gl.ARRAY_BUFFER, pk.pos, gl.STATIC_DRAW); this.ver = X.ver; }
        if (this.gver !== X.gver) { if (!this.gbuf) this.gbuf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, this.gbuf); gl.bufferData(gl.ARRAY_BUFFER, X.garr, gl.DYNAMIC_DRAW); this.gver = X.gver; }
        var mm = (args && args.defaultProjectionData && args.defaultProjectionData.mainMatrix) || args, u = new Float32Array(16);
        for (var k = 0; k < 4; k++) { u[k] = mm[k]; u[4 + k] = mm[4 + k]; u[8 + k] = mm[8 + k]; u[12 + k] = mm[k] * pk.ax + mm[4 + k] * pk.ay + mm[12 + k]; }   // withAnchor, overlay.html:34-38
        gl.useProgram(P.pr);
        gl.uniformMatrix4fv(P.L.u, false, u); gl.uniform1f(P.L.u_mz, pk.mz);
        // MapLibre 4.7.1 draws the 3D world relative to the camera centre's ground height (queryTerrainElevation subtracts
        // transform.elevation too), so heights read absolute from the terrain are taken back by it here
        var tre = X.map.transform && X.map.transform.elevation; gl.uniform1f(P.L.u_e, isFinite(tre) ? tre : 0);
        var F = fadeNow(); gl.uniform1f(P.L.u_near, F.near); gl.uniform1f(P.L.u_fade, F.fade);
        gl.bindBuffer(gl.ARRAY_BUFFER, this.buf); gl.enableVertexAttribArray(P.p); gl.vertexAttribPointer(P.p, 4, gl.FLOAT, false, 0, 0);
        gl.bindBuffer(gl.ARRAY_BUFFER, this.gbuf); gl.enableVertexAttribArray(P.g); gl.vertexAttribPointer(P.g, 1, gl.FLOAT, false, 0, 0);
        gl.enable(gl.BLEND); gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);   // premultiplied, wire-look.js:68
        var t = X.t, draws = 0, gs = m.groups, glow = X.glow;
        for (var i = 0; i < gs.length; i++) {
          var G = gs[i]; if (!G.count) continue;
          var x = (t - G.t0) / G.dur; if (x <= 0) continue;
          var rise = 1, rev = 1.01, col = G.col, a = 0.85;
          if (G.mode === 'rise') rise = Math.max(0.002, ease(x)); else rev = x >= 1 ? 1.01 : x;
          if (G.kind === 'busbar' || G.kind === 'bay' || G.kind === 'droppers') { a = 0.55 + 0.45 * glow; }
          if (G.kind === 'span') a = 0.9;
          // conductors: the voltage colour lifted toward the wire-look hairline colour, so it reads over imagery as the overlay's does
          if (col !== window.SeeModel.STEEL) col = [col[0] * 0.55 + 0.45 * 0.75, col[1] * 0.55 + 0.45 * 0.9, col[2] * 0.55 + 0.45];
          var gl2 = (G.kind === 'busbar' ? 1 + 0.9 * X.flash : 1);
          gl.uniform1f(P.L.u_rise, rise); gl.uniform1f(P.L.u_reveal, rev);
          gl.uniform4f(P.L.u_c, Math.min(1, col[0] * gl2), Math.min(1, col[1] * gl2), Math.min(1, col[2] * gl2), a);
          gl.drawArrays(gl.LINES, G.start * 2, G.count * 2); draws++;
        }
        X.draws = draws;
      } catch (e) { X.err = String(e && e.message || e); }
      X.renderMs = performance.now() - t0;
    }
  };
  function fadeNow() {   // wire-look.js:49-57 with world/fade.mjs's aerial rule
    var map = X.map, tr = map.transform, lat = map.getCenter().lat;
    var ppm = 512 * Math.pow(2, map.getZoom()) / (C * Math.cos(lat * Math.PI / 180));
    var camPx = tr && tr.cameraToCenterDistance ? tr.cameraToCenterDistance : 0.5 / Math.tan(0.6435 / 2) * map.getCanvas().clientHeight;
    var above = camPx / ppm * Math.cos(map.getPitch() * Math.PI / 180), fadeM = Math.min(4000, 450 + Math.max(0, above) * 2.5);
    return { near: camPx, fade: Math.max(1, fadeM * ppm) };
  }

  // ---------------- enter, build, animate ----------------
  function enter(o) {
    if (X) exit();
    var map = o.map;
    X = { map: map, o: o, rec: o.rec, t: 0, tStart: 0, model: null, pk: null, ver: 0, gver: 0, glow: 0, flash: 0, H: null, gDone: false,
      start: performance.now(), skip: false, orbit: 0, touched: false, keys: {}, joy: { x: 0, y: 0, id: null }, ptr: {}, fiv: [], lastNow: 0, err: '', draws: 0,
      clip0: o.rx, prevCam: { center: map.getCenter(), zoom: map.getZoom(), pitch: map.getPitch(), bearing: map.getBearing() }, phase: 'loading' };
    // ground: the overlay's terrain under the satellite image
    try { if (!map.getSource('see-dem')) map.addSource('see-dem', DEM); map.setTerrain({ source: 'see-dem', exaggeration: 1 }); } catch (e) { X.err = 'terrain: ' + e.message; if (o.networkStatus) o.networkStatus('see-dem', true); }
    // MapLibre 4.7.1 asks the terrain for the camera centre's height at the camera's tile zoom; above the DEM's maxzoom
    // (15, AWS terrarium) that answers 0, so on high ground the camera was placed below the hill behind it (seen at a
    // 138 m site: centre elevation 0, camera at 154 m, ground under the camera 163 m). Ask at 15 at most, then coarser
    // while a tile is missing. Runtime wrap of this map's own terrain object only, as morph.js wraps layers.
    try { var T = map.terrain; if (T && !T.__seeClamp) { var orig = T.getElevationForLngLatZoom.bind(T);
      T.getElevationForLngLatZoom = function (ll, z) { var e = 0; for (var q = Math.min(z, 15); q >= 10 && !e; q--) e = orig(ll, q); return e; }; T.__seeClamp = true; } } catch (e) {}
    try { if (map.setSky) map.setSky({ 'sky-color': '#0b1830', 'horizon-color': '#3a5470', 'fog-color': '#1d2c3c', 'fog-ground-blend': 0.6, 'horizon-fog-blend': 0.7, 'sky-horizon-blend': 0.6, 'atmosphere-blend': 0.6 }); } catch (e) {}
    try { if (!map.getLayer(layer.id)) map.addLayer(layer); } catch (e) { X.err = 'layer: ' + e.message; }
    X.onData = function (e) { if (X && e && e.sourceId === 'see-dem' && e.tile && e.tile.state === 'loaded') { X.demDirty = true; X.demOK = true; if (X.o.networkStatus) X.o.networkStatus('see-dem', false); } }; map.on('sourcedata', X.onData);
    ui(o);
    // the camera tips over at once, toward the point; the frame follows when the model is built
    var lat = o.rec.lat, lon = o.rec.lon;
    tipTo(lon, lat, 60, fitZoom(lat, 140), 2200);
    phoneLayout(o, true);
    footprints().then(function (fp) {
      if (!X || X.rec !== o.rec) return;
      var f = fp.byI[o.rec.i], ring = f ? f.ring : null;
      // build at once with what is local (the outline), then again when the lines and the mapped detail arrive
      Promise.all([lines(), osmDetail(o.rec, ring)]).then(function (res) {
        if (!X || X.rec !== o.rec) return;
        var osm = res[1].json ? window.SeeModel.osmParse(res[1].json, ring) : null;
        build({ footprint: ring, lines: res[0], others: others(fp.all, lat, lon, 6), osm: osm }, res[1].how);
      });
    });
    X.raf = requestAnimationFrame(tick);
    window.__see3d = api;
  }
  function fitZoom(lat, metres) {
    var W = X.map.getCanvas().clientWidth || innerWidth, H = X.map.getCanvas().clientHeight || innerHeight, minWH = Math.min(W, H);
    var z = Math.log(40075016.686 * Math.cos(lat * Math.PI / 180) * minWH / (512 * metres)) / Math.LN2;
    return Math.max(14.2, Math.min(18.6, z));
  }
  function tipTo(lon, lat, pitch, zoom, ms, bearing) {
    try { X.map.easeTo({ center: [lon, lat], pitch: pitch, zoom: zoom, bearing: bearing == null ? -24 : bearing, duration: ms, easing: ease }); } catch (e) {}
  }
  function build(data, how) {
    var t0 = performance.now(), m;
    try { m = window.SeeModel.substation(X.rec, data); } catch (e) { X.err = 'model: ' + e.message; X.phase = 'failed'; setChips(); return; }
    X.model = m; X.buildMs = performance.now() - t0; X.osmHow = how;
    X.pk = pack(m); X.packMs = performance.now() - t0 - X.buildMs;
    X.H = new Float32Array(m.grounds.length); X.Hok = new Uint8Array(m.grounds.length); X.garr = groundArray(m, X.H); X.ver++; X.gver++;
    X.phase = 'arriving';
    // the animation clock starts now (the camera has been tipping since enter)
    X.tStart = performance.now() - Math.min(1800, performance.now() - X.start) ;
    var g0 = m.grounds[0], reach = Math.max(140, Math.min(700, m.extent * 2.2 + 80)) * (X.o.port ? 0.75 : 1);
    if (!X.touched) tipTo(g0.lon, g0.lat, 62, fitZoom(g0.lat, reach), 2000);
    setChips();
  }
  // Terrain height at a place. queryTerrainElevation asks at the camera's tile zoom and answers 0 above the DEM's
  // maxzoom (15) ("cannot calculate elevation if elevation maxzoom > source.maxzoom"), so the terrain is asked at 15.
  function elevation(lon, lat) {
    var map = X.map, tr = map.terrain;
    try { if (tr && tr.getElevationForLngLatZoom) { var ll = new window.maplibregl.LngLat(lon, lat), e = 0;   // a missing tile reads 0: try coarser
      for (var z = 15; z >= 11 && !e; z--) e = tr.getElevationForLngLatZoom(ll, z); return e; } } catch (er) {}
    try { return map.queryTerrainElevation([lon, lat]); } catch (e) { return null; }
  }
  // Heights under the compound and every tower (perf.js:106-109 reads them per block, after terrain tiles arrive):
  // read again every 0.4 s for the first 20 s and whenever a terrain tile lands, at most 300 a frame.
  function readGrounds(now) {
    var m = X.model; if (!m) return;
    if (X.demDirty || (now - (X.gLast || 0) > 400 && (now - X.start) < 20000)) { X.gLast = now; X.demDirty = false; X.gi = 0; }
    if (X.gi == null || X.gi >= m.grounds.length) return;
    var changed = false, end = Math.min(m.grounds.length, X.gi + 300);
    for (var i = X.gi; i < end; i++) {
      var e = elevation(m.grounds[i].lon, m.grounds[i].lat);
      if (e != null && isFinite(e) && Math.abs(e - X.H[i]) > 0.02) { X.H[i] = e; changed = true; }
      if (e != null && isFinite(e)) X.Hok[i] = 1;
    }
    X.gi = end;
    if (changed) { X.garr = groundArray(X.model, X.H); X.gver++; }
  }
  function tick(now) {
    if (!X) return;
    X.raf = requestAnimationFrame(tick);
    if (X.lastNow) { X.fiv.push(now - X.lastNow); if (X.fiv.length > 240) X.fiv.shift(); }
    var dt = Math.min(0.1, X.lastNow ? (now - X.lastNow) / 1000 : 0.016); X.lastNow = now;
    if (!X.demOK && now - X.start > 9000 && X.o.networkStatus) X.o.networkStatus('see-dem', true);
    // the clip of the porthole opens to the whole screen while the camera tips
    var el = X.o.mapEl, k = Math.min(1, (now - X.start) / 1300), Rfull = Math.hypot(window.innerWidth, window.innerHeight);
    if (k < 1) { var r = X.clip0 + (Rfull - X.clip0) * ease(k), cp = 'circle(' + r.toFixed(1) + 'px at ' + X.o.cx.toFixed(1) + 'px ' + X.o.cy.toFixed(1) + 'px)'; el.style.clipPath = cp; el.style.webkitClipPath = cp; }
    else if (el.style.clipPath !== 'none') { el.style.clipPath = 'none'; el.style.webkitClipPath = 'none'; }
    if (X.model) {
      var m = X.model;
      if (X.skip) { X.tStart = now - (m.end + 1.2) * 1000; X.skip = false; }
      X.t = (now - X.tStart) / 1000;
      // energise: after the last span is strung the busbars light (a flash that settles to full colour)
      var tl = m.end - 0.1; X.glow = X.t < tl ? 0 : Math.min(1, (X.t - tl) / 0.5); X.flash = X.t < tl ? 0 : Math.max(0, 1 - Math.abs(X.t - tl - 0.35) / 0.35);
      // the towers march outward: the camera draws back with them (unless the player has taken the camera)
      if (!X.backed && X.t > m.timeline.towers - 0.2 && !X.touched) { X.backed = true; try { X.map.easeTo({ zoom: X.map.getZoom() - (X.o.phone ? 0.4 : 0.6), pitch: 64, duration: 4200, easing: ease }); } catch (e) {} }
      if (X.phase === 'arriving' && X.t > m.end + 0.6) { X.phase = 'orbit'; if (X.o.ping) { X.o.ping(12, 0.03); } }
      readGrounds(now);
    }
    // an ease interrupted by another (the frame follows the model, the camera draws back) can leave MapLibre's
    // elevation freeze on, and the camera then keeps a centre height of 0 under a hill: release it once no ease runs
    try { if (X.map._elevationFreeze && !X.map.isEasing()) X.map._elevationFreeze = false; } catch (e) {}
    move(dt);
    X.map.triggerRepaint();
  }
  function move(dt) {   // orbit after the arrival; keys and joystick fly (overlay.html:131-145 drone speed, 60 m/s)
    var map = X.map, K = X.keys, j = X.joy;
    var f = (K.w || K.arrowup ? 1 : 0) - (K.s || K.arrowdown ? 1 : 0) - j.y;
    var r = (K.d ? 1 : 0) - (K.a ? 1 : 0);
    var turn = (K.e || K.arrowright ? 1 : 0) - (K.q || K.arrowleft ? 1 : 0) + j.x;
    var tilt = (K.r ? 1 : 0) - (K.f ? 1 : 0), zin = (K['+'] || K['='] ? 1 : 0) - (K['-'] || K._ ? 1 : 0);
    if (f || r || turn || tilt || zin) {
      X.touched = true; stopEase();
      var sp = 60 * Math.pow(2, 17 - map.getZoom()) * (K.shift ? 3 : 1), b = map.getBearing() * Math.PI / 180, c = map.getCenter();
      var dn = (f * Math.cos(b) - r * Math.sin(b)) * sp * dt, de = (f * Math.sin(b) + r * Math.cos(b)) * sp * dt;
      map.jumpTo({ center: [c.lng + de / (111320 * Math.cos(c.lat * Math.PI / 180)), c.lat + dn / 111320], bearing: map.getBearing() + turn * 60 * dt,
        pitch: Math.max(0, Math.min(82, map.getPitch() + tilt * 30 * dt)), zoom: Math.max(12, Math.min(19.5, map.getZoom() + zin * dt)) });
    } else if (X.phase === 'orbit' && !X.touched && !X.drag) {
      X.orbit = Math.min(1, X.orbit + dt * 0.5);
      map.jumpTo({ bearing: map.getBearing() + 4.5 * X.orbit * dt });
    }
  }
  function stopEase() { try { if (X.map.isEasing && X.map.isEasing()) X.map.stop(); } catch (e) {} }

  // ---------------- UI: the label, a line of numbers, the joystick, the input pad ----------------
  function ui(o) {
    var host = o.host, pad = document.createElement('div');
    pad.style.cssText = 'position:absolute;inset:0;touch-action:none;background:transparent';
    host.insertBefore(pad, o.ui); X.pad = pad;
    var lab = document.createElement('div');
    lab.style.cssText = 'position:absolute;left:50%;transform:translateX(-50%);bottom:calc(' + (o.phone ? 76 : 96) + 'px + env(safe-area-inset-bottom));font:bold ' + (o.phone ? 13 : 15) + 'px ' + FONT + ';letter-spacing:1.5px;color:rgba(156,219,255,0.95);background:rgba(0,0,0,0.55);border:1px solid rgba(156,219,255,0.35);border-radius:6px;padding:5px 10px;white-space:nowrap;pointer-events:none;tex  … [line shortened here: 416 characters; full line on GitHub]
    lab.className = 'st-model-note'; lab.textContent = 'MAP + ESTIMATE'; o.ui.appendChild(lab); X.lab = lab;
    var chips = document.createElement('div');
    chips.className = 'st-model-counts'; chips.style.cssText = 'position:absolute;left:16px;top:calc(' + (o.phone ? 60 : 64) + 'px + env(safe-area-inset-top));font:bold ' + (o.phone ? 16 : 20) + 'px/1.35 ' + FONT + ';color:#fff;text-shadow:0 0 4px #000,0 0 2px #000;pointer-events:none;white-space:pre';
    o.ui.appendChild(chips); X.chips = chips; setChips();
    // the joystick, touch screens only (menu-style.js:37-43: hidden on fine pointers)
    var coarse = (navigator.maxTouchPoints || 0) > 0 || (window.matchMedia && (matchMedia('(any-pointer: coarse)').matches || matchMedia('(pointer: coarse)').matches));
    var joy = document.createElement('div'), knob = document.createElement('div');
    joy.style.cssText = 'position:absolute;left:calc(16px + env(safe-area-inset-left));bottom:calc(' + (o.phone ? 120 : 150) + 'px + env(safe-area-inset-bottom));width:120px;height:120px;border-radius:50%;background:rgba(156,219,255,0.04);border:1px solid rgba(156,219,255,0.35);touch-action:none;pointer-events:auto;display:' + (coarse ? 'block' : 'none');
    knob.style.cssText = 'position:absolute;left:36px;top:36px;width:48px;height:48px;border-radius:50%;border:1px solid rgba(156,219,255,0.6);background:rgba(156,219,255,0.12);pointer-events:none';
    joy.className = 'st-joystick'; joy.setAttribute('aria-label', 'Fly: push forward or turn');
    joy.appendChild(knob); o.ui.appendChild(joy); X.joyEl = joy;
    var joyAt = function (e) { var r = joy.getBoundingClientRect(), h = r.width / 2, x = (e.clientX - r.left - h) / (h - 10), y = (e.clientY - r.top - h) / (h - 10), d = Math.hypot(x, y);
      if (d > 1) { x /= d; y /= d; } X.joy.x = x; X.joy.y = y; knob.style.left = (36 + x * 40) + 'px'; knob.style.top = (36 + y * 40) + 'px'; };   // walk-fps.js:137-139
    joy.addEventListener('pointerdown', function (e) { e.stopPropagation(); X.joy.id = e.pointerId; try { joy.setPointerCapture(e.pointerId); } catch (x) {} joyAt(e); X.touched = true; });
    joy.addEventListener('pointermove', function (e) { if (e.pointerId === X.joy.id) joyAt(e); });
    var jup = function (e) { if (e.pointerId !== X.joy.id) return; X.joy.id = null; X.joy.x = X.joy.y = 0; knob.style.left = knob.style.top = '36px'; };
    joy.addEventListener('pointerup', jup); joy.addEventListener('pointercancel', jup); joy.addEventListener('lostpointercapture', jup);
    X.resetJoy = function () { X.joy.id = null; X.joy.x = X.joy.y = 0; knob.style.left = knob.style.top = '36px'; };
    // orbit by drag, pinch or wheel to zoom, tap to skip the arrival
    pad.addEventListener('pointerdown', function (e) { X.ptr[e.pointerId] = { x: e.clientX, y: e.clientY, x0: e.clientX, y0: e.clientY, t: performance.now() }; try { pad.setPointerCapture(e.pointerId); } catch (x) {}
      var ids = Object.keys(X.ptr); if (ids.length === 2) { var a = X.ptr[ids[0]], b = X.ptr[ids[1]]; X.pinch = Math.hypot(a.x - b.x, a.y - b.y) || 1; } });
    pad.addEventListener('pointermove', function (e) {
      var p = X.ptr[e.pointerId]; if (!p) return; var dx = e.clientX - p.x, dy = e.clientY - p.y; p.x = e.clientX; p.y = e.clientY;
      var ids = Object.keys(X.ptr), map = X.map;
      if (ids.length >= 2 && X.pinch) { var a = X.ptr[ids[0]], b = X.ptr[ids[1]], d = Math.hypot(a.x - b.x, a.y - b.y) || 1; stopEase(); X.touched = true;
        map.jumpTo({ zoom: Math.max(12, Math.min(19.5, map.getZoom() + Math.log(d / X.pinch) / Math.LN2)) }); X.pinch = d; return; }
      if (Math.hypot(p.x - p.x0, p.y - p.y0) > 8) { X.drag = true; X.touched = true; stopEase();
        map.jumpTo({ bearing: map.getBearing() - dx * 0.35, pitch: Math.max(0, Math.min(82, map.getPitch() + dy * 0.25)) }); }
    });
    var up = function (e) { var p = X.ptr[e.pointerId]; delete X.ptr[e.pointerId]; if (Object.keys(X.ptr).length < 2) X.pinch = 0;
      if (p && e.type === 'pointerup' && Math.hypot(p.x - p.x0, p.y - p.y0) <= 8 && X.model && X.t < X.model.end) X.skip = true;
      if (!Object.keys(X.ptr).length) X.drag = false; };
    pad.addEventListener('pointerup', up); pad.addEventListener('pointercancel', up);
    pad.addEventListener('wheel', function (e) { e.preventDefault(); var dy = +e.deltaY || 0; if (e.deltaMode === 1) dy *= 30; if (!isFinite(dy)) return; stopEase(); X.touched = true;
      var map = X.map; map.jumpTo({ zoom: Math.max(12, Math.min(19.5, map.getZoom() - Math.max(-1, Math.min(1, dy * 0.002)))) }); }, { passive: false });
    X.kd = function (e) { var tg = e.target && e.target.tagName; if (tg === 'INPUT' || tg === 'TEXTAREA') return; var k = e.key.toLowerCase(); if (k === ' ') { X.skip = true; e.preventDefault(); return; }
      if (/^(w|a|s|d|q|e|r|f|arrowup|arrowdown|arrowleft|arrowright|\+|=|-|_|shift)$/.test(k)) { X.keys[k] = true; e.preventDefault(); } };
    X.ku = function (e) { X.keys[e.key.toLowerCase()] = false; };
    window.addEventListener('keydown', X.kd); window.addEventListener('keyup', X.ku);
    X.blur = function () { X.keys = {}; X.resetJoy(); X.ptr = {}; X.pinch = 0; X.drag = false; }; window.addEventListener('blur', X.blur);
  }
  function setChips() {
    if (!X || !X.chips) return;
    var m = X.model, s = m && m.stats, L = [];
    if (!m) L.push(X.phase === 'failed' ? 'MODEL FAILED' : 'LOADING');
    else {
      L.push(s.kvs.length ? s.kvs.slice(0, 3).join(' / ') + ' KV' : 'KV UNKNOWN');
      L.push('BY RULE'); L.push('BAYS ' + s.bays + '   TOWERS ' + s.towers);
      L.push('FENCE ' + (s.fence === 'mapped' ? 'MAPPED' : 'ESTIMATED'));
      if (cache.linesErr) L.push('LINES OFFLINE');
    }
    X.chips.textContent = '';
    var words = document.createElement('span'); words.className = 'st-words'; words.textContent = L.join(' · '); X.chips.appendChild(words); X.chips.title = words.textContent;
  }
  // phones: the attribution goes small along the bottom, the numbers sit under the caption, the joystick clears the label
  function phoneLayout(o, on) {
    if (!on) return;
    // The name is above; facts and credits wrap without full-width black bars.
    X.lab.style.display = 'none';
    X.chips.className = 'st-edge st-model-counts st-wrap';
    X.chips.style.cssText = 'position:absolute;left:8px;right:8px;max-width:720px;bottom:calc(116px + env(safe-area-inset-bottom));font:400 14px/22px ' + FONT;
    X.joyEl.style.bottom = 'calc(174px + env(safe-area-inset-bottom))';
    if (o.land) { X.chips.style.right = '132px'; X.chips.style.bottom = 'calc(52px + env(safe-area-inset-bottom))'; X.joyEl.style.bottom = 'calc(104px + env(safe-area-inset-bottom))'; }
    X.joyEl.style.zIndex = '5';
  }
  function exit() {
    if (!X) return;
    phoneLayout(X.o, false);
    var map = X.map;
    cancelAnimationFrame(X.raf); try { map.off('sourcedata', X.onData); } catch (e) {}
    window.removeEventListener('keydown', X.kd); window.removeEventListener('keyup', X.ku); window.removeEventListener('blur', X.blur);
    [X.pad, X.lab, X.chips, X.joyEl].forEach(function (e) { if (e && e.parentNode) e.parentNode.removeChild(e); });
    try { if (map.getLayer(layer.id)) map.removeLayer(layer.id); } catch (e) {}
    try { map.setTerrain(null); } catch (e) {}
    try { map.stop(); map.jumpTo({ pitch: 0, bearing: 0 }); } catch (e) {}
    var el = X.o.mapEl; el.style.clipPath = 'circle(' + X.o.rx.toFixed(1) + 'px at ' + X.o.cx.toFixed(1) + 'px ' + X.o.cy.toFixed(1) + 'px)'; el.style.webkitClipPath = el.style.clipPath;
    X = null; window.__see3d = null;
  }

  var api = {
    enter: enter, exit: exit,
    state: function () { if (!X) return null; var m = X.model; return { phase: X.phase, t: X.t, end: m && m.end, stats: m && m.stats, groups: m && m.groups.length, segs: m && m.v.length / 14,
      grounds: m && m.grounds.length, groundsRead: X.Hok ? Array.prototype.reduce.call(X.Hok, function (a, b) { return a + b; }, 0) : 0, buildMs: X.buildMs, packMs: X.packMs,
      osm: X.osmHow, err: X.err, draws: X.draws, renderMs: X.renderMs, zoom: X.map.getZoom(), pitch: X.map.getPitch(), bearing: X.map.getBearing(), linesErr: cache.linesErr }; },
    frameIntervals: function () { return X ? X.fiv.slice() : []; },
    skip: function () { if (X) X.skip = true; },
    _map: function () { return X && X.map; },
    relayout: function (o) { if (!X) return; Object.assign(X.o, o); X.attCss = null; phoneLayout(X.o, true); },
    model: function () { return X && X.model; },
    groundAt: function (i) { return X && X.H ? X.H[i] : null; }
  };
  window.SeeThrough3D = api;
})();