Files
gods-eye-view/src/globe/navara/surfaceRay.test.mjs
T
German Khokhlov 7abf74518f Cut duplicated and dead code across the engines, the layers and the QA scripts
- src/geo drops the ported debug argument checks and the tests that
  bitExact already covers; package boundaries accept a directory entry,
  so src/geo/ is listed once per group.
- Cesium no longer carries movers and lines nobody draws with: they are
  optional engine capabilities. The fake engine and the Cesium camera use
  the shared cameraMath; unused contract tables, clock.ticked, the
  'finest' surface detail and Navara click statistics are gone.
- ALPR keeps one overlay core and one presentation model for both
  engines, bikeshare one station bookkeeping, the Navara ground effects
  one definition helper; image atlases, view centres, mover banks and
  colours are shared.
- The legacy switch route, the clock offset and rate controls and other
  unused exports are removed.
- The acceptance runs the Navara draw, geo import and scope checks;
  scenarios use the harness switch and page helpers; the main chunk
  check keeps the size budget only and runs once in CI.
2026-11-05 13:32:39 +04:00

135 lines
4.4 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
BackSide,
DoubleSide,
FrontSide,
Group,
Mesh,
MeshBasicMaterial,
PlaneGeometry,
} from 'three';
import { createNavaraSurface } from './surface.js';
function fixture(t, { distances = [5], sizes = [20], supported = true } = {}) {
const tiles = distances.map((distance, i) => {
const root = new Group();
const geometry = new PlaneGeometry(sizes[i] ?? 20, sizes[i] ?? 20);
const material = new MeshBasicMaterial({ side: DoubleSide });
const mesh = new Mesh(geometry, material);
root.position.set(6378137, 0, distance);
root.add(mesh);
root.updateMatrixWorld(true);
t.after(() => {
geometry.dispose();
material.dispose();
});
return root;
});
let terrainQueries = 0;
const { surface } = createNavaraSurface(
{
sampleTerrainHeight: () => {
terrainQueries++;
return 123;
},
},
{
tileRoots: () => tiles,
isIdle: () => true,
retainTileGeometry() {
if (!supported) throw new Error('CPU geometry unavailable');
},
},
);
const ray = {
origin: { x: 6378137, y: 0, z: 0 },
direction: { x: 0, y: 0, z: 1 },
far: 10,
};
return { surface, tiles, ray, terrainQueries: () => terrainQueries };
}
test('surface ray returns the nearest real triangle in world metres', (t) => {
const h = fixture(t, { distances: [8, 5] });
const hit = h.surface.raycast({
...h.ray,
direction: { x: 0, y: 0, z: 7 },
});
assert.equal(hit.distance, 5);
assert.deepEqual(hit.position, { x: 6378137, y: 0, z: 5 });
assert.equal(hit.object, h.tiles[1]);
assert.equal(h.terrainQueries(), 0);
});
test('a ray starting inside a broad bound still finds its nearer triangle', (t) => {
const h = fixture(t, { distances: [5, 2], sizes: [1, 200] });
assert.equal(h.surface.raycast(h.ray).distance, 2);
});
test('surface rays respect finite range, tile and child exclusions and visibility', (t) => {
const h = fixture(t, { distances: [5, 8] });
assert.equal(h.surface.raycast({ ...h.ray, far: 4 }), undefined);
assert.equal(h.surface.raycast({ ...h.ray, far: 5 }).distance, 5);
for (const excluded of [h.tiles[0], h.tiles[0].children[0]])
assert.equal(
h.surface.raycast({ ...h.ray, exclude: [excluded] }).distance,
8,
);
h.tiles[0].visible = false;
assert.equal(h.surface.raycast(h.ray).distance, 8);
h.tiles[1].children[0].visible = false;
assert.equal(h.surface.raycast(h.ray), undefined);
assert.equal(h.terrainQueries(), 0);
});
test('invalid and unsupported surface rays never invent terrain intersections', (t) => {
const h = fixture(t);
for (const far of [0, -1, NaN, Infinity])
assert.equal(h.surface.raycast({ ...h.ray, far }), undefined);
for (const direction of [
{ x: 0, y: 0, z: 0 },
{ x: NaN, y: 0, z: 1 },
])
assert.equal(h.surface.raycast({ ...h.ray, direction }), undefined);
assert.equal(
h.surface.raycast({ ...h.ray, origin: { x: Infinity, y: 0, z: 0 } }),
undefined,
);
const missing = fixture(t, { supported: false });
assert.equal(missing.surface.raycast(missing.ray), undefined);
assert.equal(missing.terrainQueries(), 0);
});
test('surface rays work after UV/normal release and honor sides and draw ranges', (t) => {
const h = fixture(t);
const mesh = h.tiles[0].children[0];
mesh.geometry.attributes.normal.array = null;
mesh.geometry.attributes.uv.array = null;
mesh.material.side = FrontSide;
assert.equal(h.surface.raycast(h.ray), undefined);
mesh.material.side = BackSide;
assert.equal(h.surface.raycast(h.ray).distance, 5);
mesh.geometry.setDrawRange(0, 0);
assert.equal(h.surface.raycast(h.ray), undefined);
mesh.geometry.setDrawRange(0, 6);
const hit = h.surface.raycast({
...h.ray,
direction: { x: 1, y: 0, z: 1 },
});
assert.ok(Math.abs(hit.distance - Math.sqrt(50)) < 1e-9);
assert.ok(Math.abs(hit.position.x - 6378142) < 1e-9);
});
test('surface ray distances stay in world metres under mesh transforms', (t) => {
const h = fixture(t);
h.tiles[0].scale.set(2, 3, 2);
h.tiles[0].children[0].position.z = 1;
h.tiles[0].updateMatrixWorld(true);
assert.equal(h.surface.raycast({ ...h.ray, far: 6 }), undefined);
const first = h.surface.raycast(h.ray);
assert.equal(first.distance, 7);
h.surface.raycast({ ...h.ray, direction: { x: 1, y: 0, z: 1 } });
assert.deepEqual(first.position, { x: 6378137, y: 0, z: 7 });
});