diff --git a/src/render/cesium/query.js b/src/render/cesium/query.js index 8bbb23510..b8241ffb0 100644 --- a/src/render/cesium/query.js +++ b/src/render/cesium/query.js @@ -108,8 +108,10 @@ export function createCesiumQuery({ // What height queries see through, by their items' pick ids: the sets not // drawn as ground, and the sets a caller excludes. Built once per change - // to the sets, not per query. + // to the sets, not per query. Each list's sets are kept beside it, with + // the change they were read at, for `withoutSets` to hide. const seeThroughCache = new Map(); + const seeThroughSets = new WeakMap(); let seeThroughVersion = -1; function seeThrough(exclude, { allSets = false } = {}) { if (seeThroughVersion !== drawPart.version()) { @@ -120,14 +122,18 @@ export function createCesiumQuery({ let out = seeThroughCache.get(key); if (!out) { out = []; + const records = []; for (const record of drawPart.sets.values()) if ( allSets || !record.spec.includeInHeightQueries || exclude?.includes(record.spec.id) - ) + ) { + records.push(record); for (const id of record.pickIds) out.push(id); + } seeThroughCache.set(key, out); + seeThroughSets.set(out, { records, version: seeThroughVersion }); } return out; } @@ -159,11 +165,60 @@ export function createCesiumQuery({ } } + /** + * The hosts `withoutSets` hid, each followed by its set, shown again once its + * sample is done. + */ + let hiddenHosts = []; + const hide = (host, record) => { + if (!host || host.show === false || host.isDestroyed?.()) return; + host.show = false; + hiddenHosts.push(host, record); + }; + /** Show again what `hidden` hid, as each set now asks. */ + const showAgain = (hidden) => { + for (let i = 0; i < hidden.length; i += 2) + hidden[i].show = hidden[i + 1].spec.show !== false; + }; + + /** + * `fn` with the sets seen through by `excluded` hidden whole, so a sample + * renders past them in one pass. Seen through item by item instead, each + * item hit costs another pass, and a line hit rebuilds every line of its + * collection, as the scene hides a hit line alone. Synchronous: they are + * shown again, as their sets now ask, before anything draws. A list read + * before the sets last changed is seen through item by item, as its items + * may no longer be all its sets draw; a sample inside another sees what + * the outer one hid, unless it sees through it too. + */ + function withoutSets(excluded, fn) { + const seen = excluded && seeThroughSets.get(excluded); + const current = + seen?.records.length > 0 && seen.version === drawPart.version(); + const outer = hiddenHosts; + if (!current && !outer.length) return fn(); + showAgain(outer); + hiddenHosts = []; + try { + if (current) + for (const record of seen.records) { + hide(record.host, record); + hide(record.retiring, record); + } + return fn(); + } finally { + showAgain(hiddenHosts); + hiddenHosts = outer; + for (let i = 0; i < outer.length; i += 2) outer[i].show = false; + } + } + /** A height now from what is loaded, against the surface asked for. */ function heightNow(point, against, excluded) { if (against === 'terrain') return heights().now(point, { against: 'terrain' }); - const sample = () => heights().now(point, { exclude: excluded }); + const sample = () => + withoutSets(excluded, () => heights().now(point, { exclude: excluded })); return against === 'tiles' ? withoutGlobe(sample) : sample(); } diff --git a/src/render/cesium/query.test.mjs b/src/render/cesium/query.test.mjs index b3cbe2ab6..a1d098a59 100644 --- a/src/render/cesium/query.test.mjs +++ b/src/render/cesium/query.test.mjs @@ -152,3 +152,130 @@ test('a query port built before its viewer can draw sees the sets drawn later, a viewer.isDestroyed = () => true; assert.equal(findQuery(viewer), null, 'gone with its viewer'); }); + +test('a height sample hides the sets it sees through for that sample alone', () => { + const viewer = fakeViewer(); + const { scene } = viewer; + const shownWhileSampling = []; + scene.sampleHeight = (carto, exclude) => { + scene.sampled.push(exclude); + shownWhileSampling.push(scene.added.map((host) => host.show)); + return 12; + }; + const query = queryOf(viewer); + const ground = drawOf(viewer).points({ + id: 'ground', + includeInHeightQueries: true, + }); + ground.set([{ id: 'a', pos: { lon: 0, lat: 0 }, pixelSize: 4 }]); + const markers = drawOf(viewer).points({ id: 'markers' }); + markers.set([{ id: 'b', pos: { lon: 1, lat: 0 }, pixelSize: 4 }]); + const hidden = drawOf(viewer).points({ id: 'hidden' }); + hidden.set([{ id: 'c', pos: { lon: 2, lat: 0 }, pixelSize: 4 }]); + hidden.show = false; + const [groundHost, markersHost, hiddenHost] = scene.added; + + assert.equal(query.sampleHeightNow({ lon: 0, lat: 0 }), 12); + assert.deepEqual( + shownWhileSampling.at(-1), + [true, false, false], + 'the markers are hidden while it samples, the ground is not', + ); + assert.deepEqual( + scene.sampled + .at(-1) + .map((id) => id.set) + .sort(), + ['hidden', 'markers'], + 'and their items are still seen through', + ); + assert.equal(groundHost.show, true); + assert.equal(markersHost.show, true, 'shown again after'); + assert.equal(hiddenHost.show, false, 'a hidden set stays hidden'); + + query.sampleHeightNow({ lon: 0, lat: 0 }, { exclude: ['ground'] }); + assert.deepEqual(shownWhileSampling.at(-1), [false, false, false]); + assert.deepEqual( + scene.added.map((host) => host.show), + [true, true, false], + ); + + const out = new Float64Array(2); + query.sampleHeightNowMany(new Float64Array([0, 0, 1, 0]), out); + assert.deepEqual([...out], [12, 12]); + assert.deepEqual(shownWhileSampling.slice(-2), [ + [true, false, false], + [true, false, false], + ]); + assert.deepEqual( + scene.added.map((host) => host.show), + [true, true, false], + ); + + // A sample that throws is a miss, and shows them again. + scene.sampleHeight = () => { + throw new Error('lost context'); + }; + assert.equal(query.sampleHeightNow({ lon: 0, lat: 0 }), null); + assert.deepEqual( + scene.added.map((host) => host.show), + [true, true, false], + ); + + // Terrain samples render nothing, so they hide nothing. + const before = shownWhileSampling.length; + scene.globe = { getHeight: () => 5 }; + query.sampleHeightNow({ lon: 0, lat: 0 }, { against: 'terrain' }); + assert.equal(shownWhileSampling.length, before); + ground.dispose(); + markers.dispose(); + hidden.dispose(); +}); + +test('hiding the sets seen through answers as seeing through their items did, nested samples too', () => { + const viewer = fakeViewer(); + const { scene } = viewer; + const query = queryOf(viewer); + const ground = drawOf(viewer).points({ + id: 'ground', + includeInHeightQueries: true, + }); + ground.set([{ id: 'a', pos: { lon: 0, lat: 0 }, pixelSize: 4 }]); + const markers = drawOf(viewer).points({ id: 'markers' }); + markers.set([{ id: 'b', pos: { lon: 0, lat: 0 }, pixelSize: 4 }]); + const [groundHost, markersHost] = scene.added; + // The first thing drawn and not seen through answers: the markers at + // 100 m, the ground at 50 m, else the surface at 12 m. + const sees = (host, set, exclude) => + host.show && !exclude.some((id) => id.set === set); + let nested = null; + scene.sampleHeight = (carto, exclude) => { + const inner = nested?.(); + nested = null; + if (inner !== undefined) scene.inner = inner; + if (sees(markersHost, 'markers', exclude)) return 100; + if (sees(groundHost, 'ground', exclude)) return 50; + return 12; + }; + + assert.equal(query.sampleHeightNow({ lon: 0, lat: 0 }), 50); + assert.equal( + query.sampleHeightNow({ lon: 0, lat: 0 }, { exclude: ['ground'] }), + 12, + ); + // A sample inside one that hid the ground sees it, and leaves it hidden + // for the outer one. + nested = () => query.sampleHeightNow({ lon: 0, lat: 0 }); + assert.equal( + query.sampleHeightNow({ lon: 0, lat: 0 }, { exclude: ['ground'] }), + 12, + ); + assert.equal(scene.inner, 50); + assert.deepEqual( + [groundHost.show, markersHost.show], + [true, true], + 'all shown after', + ); + ground.dispose(); + markers.dispose(); +});