mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
fix(ghostmaps): a double-click follows a pin or zooms to a line or area again
Each edition was drawn as entities, which the default renderer's double-click followed (a pin) or zoomed to (a line or polygon); an expired report or a replaced edition took the follow with it. drawnItemTarget reads that rule off a draw item, for every layer that drew entities before.
This commit is contained in:
1 parent
766cf68272
commit
26e8e26ba8
4 files changed
+202
-6
No files matched your search
@@ -10,6 +10,61 @@
|
||||
import { followOffsetFitting } from '../geo/frames.js';
|
||||
import { findCamera, findQuery } from '../render/rendererOf.js';
|
||||
import { entityFollowTarget } from './followed.js';
|
||||
import { positionToEcef } from '../geo/ellipsoid.js';
|
||||
import { sphereFromPoints } from '../geo/sphere.js';
|
||||
|
||||
/** A line's or shape's positions as ECEF points. */
|
||||
function pointsOf(positions) {
|
||||
if (!positions) return [];
|
||||
if (ArrayBuffer.isView(positions)) {
|
||||
const out = [];
|
||||
for (let i = 0; i + 2 < positions.length; i += 3)
|
||||
out.push({ x: positions[i], y: positions[i + 1], z: positions[i + 2] });
|
||||
return out;
|
||||
}
|
||||
return Array.from(positions, positionToEcef);
|
||||
}
|
||||
|
||||
/**
|
||||
* What a double-click on a drawn item does, as the default renderer did
|
||||
* for the entity a layer drew it as before: a point, an icon or a disc
|
||||
* (an entity with a position) is followed, framed to fit the disc; a line
|
||||
* or another shape (an entity without one) is zoomed to. For a layer's
|
||||
* `targetFor`, given the hit's set id and the item drawn there.
|
||||
* @param {string} set The hit's set id.
|
||||
* @param {object|null|undefined} item The draw item (draw.d.ts) the hit names.
|
||||
*/
|
||||
export function drawnItemTarget(set, item) {
|
||||
if (!item) return null;
|
||||
const key = `${set}:${item.id}`;
|
||||
if (item.pos) {
|
||||
const at = positionToEcef(item.pos);
|
||||
return { key, position: () => ({ ...at }) };
|
||||
}
|
||||
const geometry = item.geometry;
|
||||
if (geometry?.kind === 'ellipse') {
|
||||
const at = positionToEcef(geometry.center);
|
||||
const radius = Math.max(geometry.semiMajor, geometry.semiMinor);
|
||||
return {
|
||||
key,
|
||||
position: () => ({ ...at }),
|
||||
sphere: { center: at, radius: Number.isFinite(radius) ? radius : 0 },
|
||||
};
|
||||
}
|
||||
let points = pointsOf(item.positions);
|
||||
if (geometry?.kind === 'polygon') points = pointsOf(geometry.rings?.[0]);
|
||||
else if (geometry?.kind === 'wall') points = pointsOf(geometry.positions);
|
||||
else if (geometry?.kind === 'rectangle') {
|
||||
const { west, south, east, north } = geometry.rect;
|
||||
points = pointsOf([
|
||||
{ lon: west, lat: south },
|
||||
{ lon: east, lat: south },
|
||||
{ lon: east, lat: north },
|
||||
{ lon: west, lat: north },
|
||||
]);
|
||||
}
|
||||
return points.length ? { zoomTo: sphereFromPoints(points) } : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Where the camera frames a part it starts following: `followOffsetFitting`
|
||||
|
||||
@@ -117,3 +117,52 @@ test('the frame view fits a sphere and steepens far from the ground', () => {
|
||||
assert.ok(Math.abs(elevation - (Math.PI / 4) * 1.5) < 1e-9);
|
||||
assert.ok(far.y < 0);
|
||||
});
|
||||
|
||||
test('a drawn item is followed as an entity with a position was, or zoomed to as one without', async () => {
|
||||
const { drawnItemTarget } = await import('./stockFollow.js');
|
||||
const near = (a, b) => Math.hypot(a.x - b.x, a.y - b.y, a.z - b.z) < 1e-6;
|
||||
const { fromDegrees } = await import('../geo/ellipsoid.js');
|
||||
const point = drawnItemTarget('s', { id: 'p', pos: { lon: 10, lat: 20 } });
|
||||
assert.equal(point.key, 's:p');
|
||||
assert.ok(near(point.position(), fromDegrees(10, 20, 0)));
|
||||
const disc = drawnItemTarget('s', {
|
||||
id: 'd',
|
||||
geometry: {
|
||||
kind: 'ellipse',
|
||||
center: { lon: 10, lat: 20 },
|
||||
semiMajor: 4000,
|
||||
semiMinor: 3000,
|
||||
},
|
||||
});
|
||||
assert.equal(disc.sphere.radius, 4000);
|
||||
assert.ok(near(disc.position(), fromDegrees(10, 20, 0)));
|
||||
const flat = new Float64Array([
|
||||
...Object.values(fromDegrees(0, 0, 0)),
|
||||
...Object.values(fromDegrees(2, 0, 0)),
|
||||
]);
|
||||
const line = drawnItemTarget('s', { id: 'l', positions: flat });
|
||||
assert.ok(line.zoomTo.radius > 100_000 && !line.position);
|
||||
const polygon = drawnItemTarget('s', {
|
||||
id: 'g',
|
||||
geometry: {
|
||||
kind: 'polygon',
|
||||
rings: [
|
||||
[
|
||||
{ lon: 0, lat: 0 },
|
||||
{ lon: 1, lat: 0 },
|
||||
{ lon: 1, lat: 1 },
|
||||
],
|
||||
],
|
||||
},
|
||||
});
|
||||
assert.ok(polygon.zoomTo.radius > 50_000);
|
||||
const rect = drawnItemTarget('s', {
|
||||
id: 'r',
|
||||
geometry: {
|
||||
kind: 'rectangle',
|
||||
rect: { west: 0, south: 0, east: 1, north: 1 },
|
||||
},
|
||||
});
|
||||
assert.ok(rect.zoomTo.radius > 50_000);
|
||||
assert.equal(drawnItemTarget('s', null), null);
|
||||
});
|
||||
Reference in new issue
Block a user