From 73735b080a0eac88f9f300f5047c278810e58962 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Tue, 10 Nov 2026 15:12:23 -0800 Subject: [PATCH] Write the range ring's band a word a pixel and place fronts without angles The band is copied out of its radius table as whole 32-bit pixels where the image's bytes line up with words, a byte at a time where they do not. Each front's positions come from the unit vector on the sphere straight to the ellipsoid, without going through longitude and latitude and back; they move by nanometres. --- src/layers/awareness/rangeRing.js | 50 ++++++++++++++++++++----- src/layers/awareness/rangeRing.test.mjs | 8 ++++ 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/src/layers/awareness/rangeRing.js b/src/layers/awareness/rangeRing.js index 2c7216699..e02556085 100644 --- a/src/layers/awareness/rangeRing.js +++ b/src/layers/awareness/rangeRing.js @@ -47,20 +47,32 @@ export function awarenessRangeRingPositions( segments = AWARENESS_RANGE_RING_STYLE.segments, ) { const { up, bearings } = ringFrame(groundCenter, segments); + const r2 = WGS84.radiiSquared; const positions = new Array(bearings.length); for (let i = 0; i < bearings.length; i++) { const { per, along } = bearings[i]; const angular = radiusM * per; const cosA = Math.cos(angular); const sinA = Math.sin(angular); - const x = up[0] * cosA + along[0] * sinA; - const y = up[1] * cosA + along[1] * sinA; - const z = up[2] * cosA + along[2] * sinA; - positions[i] = fromRadians( - Math.atan2(y, x), - Math.atan2(z, Math.hypot(x, y)), - heightM, - ); + // The unit vector there on the sphere, taken as the ellipsoid's + // surface normal (its latitude read as geodetic) and raised to + // `heightM` along it, as `fromRadians` places a longitude and latitude. + let nx = up[0] * cosA + along[0] * sinA; + let ny = up[1] * cosA + along[1] * sinA; + let nz = up[2] * cosA + along[2] * sinA; + const length = Math.hypot(nx, ny, nz); + nx /= length; + ny /= length; + nz /= length; + const kx = r2.x * nx; + const ky = r2.y * ny; + const kz = r2.z * nz; + const gamma = Math.sqrt(nx * kx + ny * ky + nz * kz); + positions[i] = { + x: kx / gamma + nx * heightM, + y: ky / gamma + ny * heightM, + z: kz / gamma + nz * heightM, + }; } return positions; } @@ -320,7 +332,24 @@ export function paintAwarenessBand(image, band, memo = {}) { } } const bin = memo.bin; - const [r, g, b] = rgb.map((c) => Math.round(c * 255)); + // Clamped as the image's bytes would clamp them. + const [r, g, b] = rgb.map((c) => + Math.min(255, Math.max(0, Math.round(c * 255))), + ); + // Whole pixels at a time where the bytes line up: each table entry as + // the four bytes it writes, in the platform's byte order. + if (data.byteOffset % 4 === 0 && data.length === bin.length * 4) { + if (memo.pixels?.length !== table.length) + memo.pixels = new Uint32Array(table.length); + const pixels = memo.pixels; + for (let i = 0; i < table.length; i++) + pixels[i] = LITTLE_ENDIAN + ? (r | (g << 8) | (b << 16) | (table[i] << 24)) >>> 0 + : ((r << 24) | (g << 16) | (b << 8) | table[i]) >>> 0; + const words = new Uint32Array(data.buffer, data.byteOffset, bin.length); + for (let p = 0; p < bin.length; p++) words[p] = pixels[bin[p]]; + return; + } for (let p = 0, k = 0; p < bin.length; p++, k += 4) { data[k] = r; data[k + 1] = g; @@ -329,6 +358,9 @@ export function paintAwarenessBand(image, band, memo = {}) { } } +/** Whether this platform stores the low byte of a word first. */ +const LITTLE_ENDIAN = new Uint8Array(new Uint32Array([1]).buffer)[0] === 1; + /** `p` in metres east and north of `origin`, in `origin`'s surface frame. */ function eastNorth(origin, p) { const { lon, lat } = toLonLatH(origin); diff --git a/src/layers/awareness/rangeRing.test.mjs b/src/layers/awareness/rangeRing.test.mjs index 2b036b381..8761d8ad3 100644 --- a/src/layers/awareness/rangeRing.test.mjs +++ b/src/layers/awareness/rangeRing.test.mjs @@ -1120,6 +1120,14 @@ test('a band painted with a kept memo is the band painted afresh, step after ste const expected = fresh(); paintAwarenessBand(expected, { ...band, rgb: [1, 0, 0] }); assert.equal(firstMismatch(image.data, expected.data), -1, 'repainted'); + // Bytes that do not line up with whole words are painted a byte at a + // time, the same. + const odd = { + width: size, + data: new Uint8ClampedArray(new ArrayBuffer(size * size * 4 + 1), 1), + }; + paintAwarenessBand(odd, { ...band, rgb: [1, 0, 0] }, memo); + assert.equal(firstMismatch(odd.data, expected.data), -1, 'unaligned'); }); test('the band is one flat unpickable ellipse at the rim, drawn anew each step about the subject', (t) => {