185 lines
6.6 KiB
JavaScript
185 lines
6.6 KiB
JavaScript
import { expect, test } from "bun:test";
|
|
import * as THREE from "three";
|
|
import {
|
|
pickNearestInteractiveHit,
|
|
pickNearestPointCandidate,
|
|
pickVisibleSatellitesInScreenRadius,
|
|
} from "./interactive-picking.js";
|
|
|
|
const hit = (object, distancePxSq, cluster = false) => ({ object, distancePxSq, cluster });
|
|
|
|
test("screen distance wins across icon layers", () => {
|
|
expect(pickNearestInteractiveHit([
|
|
{ type: "bgp_collector", hits: [hit("rrc26", 64)] },
|
|
{ type: "compute_center", hits: [hit("gpu", 9)] },
|
|
]).object).toBe("gpu");
|
|
});
|
|
|
|
test("render priority resolves an exact overlap without letting the lower collector steal it", () => {
|
|
expect(pickNearestInteractiveHit([
|
|
{ type: "bgp_collector", hits: [hit("rrc22", 16)] },
|
|
{ type: "compute_center", hits: [hit("gpu", 16)] },
|
|
]).object).toBe("gpu");
|
|
});
|
|
|
|
test("cluster hits are ignored while pointer eligibility remains true for real markers", () => {
|
|
expect(pickNearestInteractiveHit([
|
|
{ type: "compute_center", hits: [hit("cluster", 1, true)] },
|
|
{ type: "bgp_collector", hits: [hit("rrc22", 9)] },
|
|
]).object).toBe("rrc22");
|
|
});
|
|
|
|
test("nearest point candidate is resolved before the line fallback", () => {
|
|
expect(pickNearestPointCandidate([
|
|
{ type: "compute_center", object: "compute", distancePxSq: 16 },
|
|
{ type: "satellite", object: "satellite", distancePxSq: 0 },
|
|
]).type).toBe("satellite");
|
|
});
|
|
|
|
function projectToScreen(point, localToClip, rect) {
|
|
const projected = point.clone().applyMatrix4(localToClip);
|
|
return {
|
|
x: rect.left + (projected.x * 0.5 + 0.5) * rect.width,
|
|
y: rect.top + (-projected.y * 0.5 + 0.5) * rect.height,
|
|
};
|
|
}
|
|
|
|
function buildPickContext(cameraPos, target = new THREE.Vector3(0, 0, 0), up = new THREE.Vector3(0, 0, -1)) {
|
|
const rect = { left: 10, top: 20, width: 1280, height: 720 };
|
|
const camera = new THREE.PerspectiveCamera(75, rect.width / rect.height, 0.1, 5000);
|
|
camera.up.copy(up);
|
|
camera.position.copy(cameraPos);
|
|
camera.lookAt(target);
|
|
camera.updateMatrixWorld(true);
|
|
const localToClip = new THREE.Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
|
|
return { camera, localToClip, rect };
|
|
}
|
|
|
|
test("high-orbit satellites outside Earth silhouette remain pickable from polar and tilted views", () => {
|
|
const occlusionRadius = 99.8;
|
|
for (const cameraPos of [
|
|
new THREE.Vector3(0, -1200, 0),
|
|
new THREE.Vector3(0, -1200, 150),
|
|
]) {
|
|
const { camera, localToClip, rect } = buildPickContext(cameraPos);
|
|
const geoEquatorial = new THREE.Vector3(0, 0, -660);
|
|
const geoOppositeHemisphere = new THREE.Vector3(0, 40, -660);
|
|
const positions = [
|
|
{ current: geoEquatorial },
|
|
{ current: geoOppositeHemisphere },
|
|
];
|
|
|
|
for (const [expectedIndex, point] of [geoEquatorial, geoOppositeHemisphere].entries()) {
|
|
const screen = projectToScreen(point, localToClip, rect);
|
|
const hits = pickVisibleSatellitesInScreenRadius({
|
|
positions,
|
|
localToClipElements: localToClip.elements,
|
|
cameraLocal: camera.position,
|
|
occlusionRadius,
|
|
rect,
|
|
targetX: screen.x,
|
|
targetY: screen.y,
|
|
radiusPx: 14,
|
|
});
|
|
expect(hits.length).toBeGreaterThanOrEqual(1);
|
|
expect(hits[0].index).toBe(expectedIndex);
|
|
}
|
|
}
|
|
});
|
|
|
|
test("satellites truly occluded behind Earth remain unpickable", () => {
|
|
const occlusionRadius = 99.8;
|
|
const { camera, localToClip, rect } = buildPickContext(new THREE.Vector3(0, -1200, 0));
|
|
const occludedLeo = new THREE.Vector3(0, 108, 0);
|
|
const occludedGeo = new THREE.Vector3(0, 660, 0);
|
|
const positions = [{ current: occludedLeo }, { current: occludedGeo }];
|
|
|
|
for (const point of [occludedLeo, occludedGeo]) {
|
|
const screen = projectToScreen(point, localToClip, rect);
|
|
const hits = pickVisibleSatellitesInScreenRadius({
|
|
positions,
|
|
localToClipElements: localToClip.elements,
|
|
cameraLocal: camera.position,
|
|
occlusionRadius,
|
|
rect,
|
|
targetX: screen.x,
|
|
targetY: screen.y,
|
|
radiusPx: 20,
|
|
});
|
|
expect(hits).toEqual([]);
|
|
}
|
|
});
|
|
|
|
test("overlapping objects preserve closer depth and surface-icon priority", () => {
|
|
const occlusionRadius = 99.8;
|
|
const { camera, localToClip, rect } = buildPickContext(
|
|
new THREE.Vector3(0, 0, 600),
|
|
new THREE.Vector3(0, 0, 0),
|
|
new THREE.Vector3(0, 1, 0),
|
|
);
|
|
const farFrontSat = new THREE.Vector3(0, 0, 150);
|
|
const nearFrontSat = new THREE.Vector3(0, 0, 200);
|
|
const occludedBackSat = new THREE.Vector3(0, 0, -200);
|
|
const positions = [
|
|
{ current: farFrontSat },
|
|
{ current: nearFrontSat },
|
|
{ current: occludedBackSat },
|
|
];
|
|
|
|
const center = projectToScreen(nearFrontSat, localToClip, rect);
|
|
const hits = pickVisibleSatellitesInScreenRadius({
|
|
positions,
|
|
localToClipElements: localToClip.elements,
|
|
cameraLocal: camera.position,
|
|
occlusionRadius,
|
|
rect,
|
|
targetX: center.x,
|
|
targetY: center.y,
|
|
radiusPx: 16,
|
|
});
|
|
expect(hits.map((item) => item.index)).toEqual([1, 0]);
|
|
|
|
// Occluded back-side satellite overlapping a front-side surface marker must not steal picking.
|
|
const backOnlyHits = pickVisibleSatellitesInScreenRadius({
|
|
positions: [{ current: occludedBackSat }],
|
|
localToClipElements: localToClip.elements,
|
|
cameraLocal: camera.position,
|
|
occlusionRadius,
|
|
rect,
|
|
targetX: center.x,
|
|
targetY: center.y,
|
|
radiusPx: 16,
|
|
});
|
|
expect(backOnlyHits).toEqual([]);
|
|
const interactivePick = { type: "compute_center", object: "gpu", distancePxSq: 4 };
|
|
expect(pickNearestPointCandidate([interactivePick, null])).toBe(interactivePick);
|
|
|
|
// Visible satellite at identical screen distance keeps interactivePick priority via stable tie-break.
|
|
const tiedSatelliteCandidate = { type: "satellite", object: "sat", distancePxSq: 4 };
|
|
expect(pickNearestPointCandidate([interactivePick, tiedSatelliteCandidate])).toBe(interactivePick);
|
|
});
|
|
|
|
test("category visibility filter and zero positions are excluded", () => {
|
|
const occlusionRadius = 99.8;
|
|
const { camera, localToClip, rect } = buildPickContext(new THREE.Vector3(0, -1200, 0));
|
|
const geoPoint = new THREE.Vector3(0, 0, -660);
|
|
const positions = [
|
|
{ current: new THREE.Vector3(0, 0, 0) },
|
|
{ current: geoPoint },
|
|
{ current: geoPoint },
|
|
];
|
|
const screen = projectToScreen(geoPoint, localToClip, rect);
|
|
const hits = pickVisibleSatellitesInScreenRadius({
|
|
positions,
|
|
localToClipElements: localToClip.elements,
|
|
cameraLocal: camera.position,
|
|
occlusionRadius,
|
|
rect,
|
|
targetX: screen.x,
|
|
targetY: screen.y,
|
|
radiusPx: 14,
|
|
isCategoryVisible: (index) => index !== 1,
|
|
});
|
|
expect(hits.map((item) => item.index)).toEqual([2]);
|
|
});
|