mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
9018ee8980
SonarCloud measured 16.9% coverage on this PR's new code. Tests now cover the WebGL globe engine (boot, caching, input, camera, regions, prefetch) and renderer (against a fake WebGL2 context that reads the real shaders), the paint, labels, sky and pins, the central useAtlasGlobe hook and the hub hooks, the snapshot, account queue and Atlas repos, every desktop and phone hub view, the server's people, flights and mesh routes, the shared badge rules, and the new view transition, warm bags and kept planner maps.
215 lines
6.9 KiB
TypeScript
215 lines
6.9 KiB
TypeScript
// FE-ATLAS2-PAINT-501 to FE-ATLAS2-PAINT-510: the branches of the mesh builder the main
|
|
// globeMesh tests leave out (each split direction, holes, multipolygons, growth).
|
|
import type { Topology } from 'topojson-specification';
|
|
import { describe, expect, it } from 'vitest';
|
|
import { buildLayer, densify, tessellate } from './globeMesh';
|
|
|
|
const RAD = Math.PI / 180;
|
|
|
|
/** A square with a hole, a two-part country, a sliver too thin to fill and a feature without geometry. */
|
|
const SHAPES: Topology = {
|
|
type: 'Topology',
|
|
arcs: [
|
|
[
|
|
[0, 0],
|
|
[10, 0],
|
|
[10, 10],
|
|
[0, 10],
|
|
[0, 0],
|
|
],
|
|
[
|
|
[3, 3],
|
|
[3, 7],
|
|
[7, 7],
|
|
[7, 3],
|
|
[3, 3],
|
|
],
|
|
[
|
|
[20, 0],
|
|
[22, 0],
|
|
[22, 2],
|
|
[20, 2],
|
|
[20, 0],
|
|
],
|
|
[
|
|
[30, 0],
|
|
[32, 0],
|
|
[32, 2],
|
|
[30, 2],
|
|
[30, 0],
|
|
],
|
|
[
|
|
[40, 0],
|
|
[41, 1],
|
|
[40, 0],
|
|
],
|
|
],
|
|
objects: {
|
|
countries: {
|
|
type: 'GeometryCollection',
|
|
geometries: [
|
|
{ type: 'Polygon', id: 0, arcs: [[0], [1]] },
|
|
{ type: 'MultiPolygon', id: 1, arcs: [[[2]], [[3]]] },
|
|
{ type: 'Polygon', id: 2, arcs: [[4]] },
|
|
{ type: null, id: 3 },
|
|
],
|
|
},
|
|
},
|
|
};
|
|
|
|
/** A long, thin strip: cut fine it needs more vertices than the builders reserve up front. */
|
|
const STRIP: Topology = {
|
|
type: 'Topology',
|
|
arcs: [
|
|
[
|
|
[0, 0],
|
|
[350, 0],
|
|
[350, 0.01],
|
|
[0, 0.01],
|
|
[0, 0],
|
|
],
|
|
],
|
|
objects: { strip: { type: 'GeometryCollection', geometries: [{ type: 'Polygon', id: 0, arcs: [[0]] }] } },
|
|
};
|
|
|
|
function edgeLengths(coords: number[], tris: number[]): number[] {
|
|
const out: number[] = [];
|
|
for (let i = 0; i < tris.length; i += 3) {
|
|
const t = [tris[i], tris[i + 1], tris[i + 2]];
|
|
for (const [p, q] of [
|
|
[t[0], t[1]],
|
|
[t[1], t[2]],
|
|
[t[2], t[0]],
|
|
]) {
|
|
out.push(Math.hypot(coords[p * 2] - coords[q * 2], coords[p * 2 + 1] - coords[q * 2 + 1]));
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
describe('tessellate', () => {
|
|
it('FE-ATLAS2-PAINT-501: keeps a triangle that is already small enough as it is', () => {
|
|
const coords = [0, 0, 1, 0, 0, 1];
|
|
expect(tessellate(coords, [0, 1, 2], 5)).toEqual([0, 1, 2]);
|
|
expect(coords).toHaveLength(6);
|
|
});
|
|
|
|
it('FE-ATLAS2-PAINT-502: splits the first edge when it is the longest', () => {
|
|
const coords = [0, 0, 10, 0, 5, 1];
|
|
expect(tessellate(coords, [0, 1, 2], 9)).toEqual([3, 1, 2, 0, 3, 2]);
|
|
expect(coords.slice(6)).toEqual([5, 0]);
|
|
});
|
|
|
|
it('FE-ATLAS2-PAINT-503: splits the second edge when it is the longest', () => {
|
|
const coords = [0, 0, 1, 0, 0, 10];
|
|
expect(tessellate(coords, [0, 1, 2], 10.01)).toEqual([0, 3, 2, 0, 1, 3]);
|
|
expect(coords.slice(6)).toEqual([0.5, 5]);
|
|
});
|
|
|
|
it('FE-ATLAS2-PAINT-504: splits the closing edge when it is the longest', () => {
|
|
const coords = [0, 0, 5, 1, 10, 0];
|
|
expect(tessellate(coords, [0, 1, 2], 9)).toEqual([3, 1, 2, 0, 1, 3]);
|
|
expect(coords.slice(6)).toEqual([5, 0]);
|
|
});
|
|
|
|
it('FE-ATLAS2-PAINT-505: two triangles sharing a long edge share its midpoint', () => {
|
|
const coords = [0, 0, 10, 0, 10, 10, 0, 10];
|
|
const tris = tessellate(coords, [0, 1, 2, 0, 2, 3], 12);
|
|
expect(tris).toHaveLength(12);
|
|
// One new vertex for the shared diagonal, not one per triangle.
|
|
expect(coords).toHaveLength(10);
|
|
expect(coords.slice(8)).toEqual([5, 5]);
|
|
expect(Math.max(...edgeLengths(coords, tris))).toBeLessThanOrEqual(12);
|
|
});
|
|
});
|
|
|
|
describe('densify', () => {
|
|
it('FE-ATLAS2-PAINT-506: leaves a short line and a single point alone and cuts a diagonal evenly', () => {
|
|
expect(
|
|
densify(
|
|
[
|
|
[0, 0],
|
|
[1, 1],
|
|
],
|
|
5
|
|
)
|
|
).toEqual([
|
|
[0, 0],
|
|
[1, 1],
|
|
]);
|
|
expect(densify([[3, 4]], 1)).toEqual([[3, 4]]);
|
|
const d = densify(
|
|
[
|
|
[0, 0],
|
|
[3, 4],
|
|
],
|
|
2
|
|
);
|
|
expect(d).toEqual([
|
|
[0, 0],
|
|
[1, 4 / 3],
|
|
[2, 8 / 3],
|
|
[3, 4],
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('buildLayer', () => {
|
|
const layer = buildLayer(SHAPES, 'countries', 100, (n) => 10 + n);
|
|
|
|
it('FE-ATLAS2-PAINT-507: fills a polygon with its hole, both parts of a multipolygon, and skips slivers and empty features', () => {
|
|
const fid = Array.from(layer.fill.fid);
|
|
// Square with hole: 4 + 4 vertices; the two parts: 4 each; the sliver and the empty feature: none.
|
|
expect(fid).toEqual([10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11]);
|
|
expect(layer.fill.ll).toHaveLength(32);
|
|
expect(layer.fill.ref).toHaveLength(32);
|
|
// A square with a hole takes eight triangles, each plain square two.
|
|
expect(layer.fill.index).toHaveLength((8 + 2 + 2) * 3);
|
|
expect(Math.min(...layer.fill.index)).toBe(0);
|
|
expect(Math.max(...layer.fill.index)).toBe(15);
|
|
});
|
|
|
|
it('FE-ATLAS2-PAINT-508: the hole stays empty and every vertex refers to the centre of its own outline', () => {
|
|
const idx = Array.from(layer.fill.index.slice(0, 24));
|
|
// The hole's own vertices (4 to 7) never make up one triangle together.
|
|
for (let i = 0; i < idx.length; i += 3) {
|
|
expect(idx.slice(i, i + 3).every((v) => v >= 4)).toBe(false);
|
|
}
|
|
expect(layer.fill.ref[0]).toBeCloseTo(5 * RAD);
|
|
expect(layer.fill.ref[1]).toBeCloseTo(5 * RAD);
|
|
expect(layer.fill.ref[8 * 2]).toBeCloseTo(21 * RAD);
|
|
expect(layer.fill.ref[12 * 2]).toBeCloseTo(31 * RAD);
|
|
expect(layer.fill.ll[8 * 2]).toBeCloseTo(20 * RAD);
|
|
});
|
|
|
|
it('FE-ATLAS2-PAINT-509: draws every outline as coast when no border is shared', () => {
|
|
expect(layer.borders.count).toBe(0);
|
|
expect(layer.borders.seg).toHaveLength(0);
|
|
// Outer square, hole, two parts (four sides each) and the sliver's two sides.
|
|
expect(layer.coast.count).toBe(18);
|
|
expect(layer.coast.seg).toHaveLength(18 * 4);
|
|
});
|
|
|
|
it('FE-ATLAS2-PAINT-510: grows its buffers for a fine cut of a long strip and keeps every edge short', () => {
|
|
const strip = buildLayer(STRIP, 'strip', 0.02, () => 7);
|
|
const vertices = strip.fill.fid.length;
|
|
expect(vertices).toBeGreaterThan(1 << 15);
|
|
expect(strip.fill.ll).toHaveLength(vertices * 2);
|
|
expect(strip.fill.ref).toHaveLength(vertices * 2);
|
|
expect(strip.fill.fid.every((v) => v === 7)).toBe(true);
|
|
// Spread would overflow the argument list for an index this long.
|
|
expect(strip.fill.index.reduce((m, v) => Math.max(m, v), 0)).toBe(vertices - 1);
|
|
expect(strip.fill.index.length % 3).toBe(0);
|
|
// The outline: 700 degrees cut into steps of at most 0.02.
|
|
expect(strip.coast.count).toBeGreaterThanOrEqual(35000);
|
|
expect(strip.coast.seg).toHaveLength(strip.coast.count * 4);
|
|
let longest = 0;
|
|
for (let i = 0; i < strip.coast.seg.length; i += 4) {
|
|
const s = strip.coast.seg;
|
|
longest = Math.max(longest, Math.hypot(s[i + 2] - s[i], s[i + 3] - s[i + 1]));
|
|
}
|
|
// Float32 storage: a few millionths of slack on values up to six radians.
|
|
expect(longest).toBeLessThanOrEqual(0.02 * RAD + 1e-5);
|
|
});
|
|
});
|