// exports.js — terrain export helpers (pure): sample count, the square on the map, size estimate, status text
import { R_KM } from './geo.js';
export { R_KM };
export function samplesFor(sizeKm, resM) { // the smallest 2^n + 1 covering the size (as export.samples_for)
const need = (sizeKm * 1000) / resM + 1;
let n = 1;
while (2 ** n + 1 < need) n++;
return 2 ** n + 1;
}
function destination(lat, lon, bearing, km, R) {
const r = Math.PI / 180, d = km / R, la = lat * r, b = bearing;
const la2 = Math.asin(Math.sin(la) * Math.cos(d) + Math.cos(la) * Math.sin(d) * Math.cos(b));
const lo2 = lon * r + Math.atan2(Math.sin(b) * Math.sin(d) * Math.cos(la), Math.cos(d) - Math.sin(la) * Math.sin(la2));
return { lat: la2 / r, lon: ((lo2 / r + 540) % 360) - 180 };
}
export function squareOutline(lat, lon, sizeKm, R = R_KM, per = 8) { // the export square (azimuthal equidistant)
const h = sizeKm / 2, pts = [];
const corners = [[-h, h], [h, h], [h, -h], [-h, -h], [-h, h]]; // [x east, y north], from the north-west
for (let k = 0; k < 4; k++) {
for (let i = 0; i < per; i++) {
const t = i / per, x = corners[k][0] + (corners[k + 1][0] - corners[k][0]) * t;
const y = corners[k][1] + (corners[k + 1][1] - corners[k][1]) * t;
pts.push(destination(lat, lon, Math.atan2(x, y), Math.hypot(x, y), R));
}
}
pts.push(pts[0]);
return pts;
}
export const exportSizeMB = n => (n * n * 7.8) / 1e6; // f32 + 16-bit PNG + raw + preview (the island: 128 MB)
export function exportStatusText(s) {
if (!s || s.state === 'idle') return '';
if (s.state === 'running') return `Exporting ${s.name}… ${Math.round((s.fraction ?? 0) * 100)}%`;
if (s.state === 'failed') return `Export ${s.name} failed: ${s.error ?? 'unknown error'}`;
return `Exported ${s.name}`;
}