From 2f7fdb5b339ee412330f9c9d614a5582176a9987 Mon Sep 17 00:00:00 2001 From: Afonso Pinto Date: Thu, 5 Nov 2026 00:54:33 +0000 Subject: [PATCH 1/2] Fix spatial skeleton overview selection and inactive projection counts Use projected source coverage for the spacing node budget and render inactive histograms without clearing shared bins or subtracting indicator-only counts. Add regression coverage for overview selection and histogram visibility. --- src/skeleton/spatial/base.spec.ts | 114 +++++++++++++++++++++++++ src/skeleton/spatial/base.ts | 62 ++++++++++++-- src/widget/render_scale_widget.spec.ts | 88 +++++++++++++++++++ src/widget/render_scale_widget.ts | 17 ++-- 4 files changed, 264 insertions(+), 17 deletions(-) create mode 100644 src/widget/render_scale_widget.spec.ts diff --git a/src/skeleton/spatial/base.spec.ts b/src/skeleton/spatial/base.spec.ts index a052727236..e9d77318e7 100644 --- a/src/skeleton/spatial/base.spec.ts +++ b/src/skeleton/spatial/base.spec.ts @@ -22,6 +22,7 @@ import { RenderScaleHistogram } from "#src/render_scale_statistics.js"; import { forEachSpatialSkeletonSourceScale, forEachVisibleSpatialSkeletonChunk, + getSpatialSkeletonSourceDensityContext, getSpatialSkeletonLevelSpecs, getSpatialSkeletonSourceScalesByLimit, selectSpatialSkeletonSourceByLimit, @@ -149,6 +150,119 @@ describe("forEachVisibleSpatialSkeletonChunk", () => { { label: "fine", selected: true }, ]); }); + it.each([ + { + projection: "orthographic", + initialZoom: 2, + mediumZoom: 0.75, + fineZoom: 0.25, + }, + { + projection: "perspective", + initialZoom: 1, + mediumZoom: 0.35, + fineZoom: 0.15, + }, + ])( + "selects the 8 mm overview when zooming out past the dataset ($projection)", + ({ projection, initialZoom, mediumZoom, fineZoom }) => { + const parameters = makeProjectionParameters(); + if (projection === "orthographic") { + mat4.ortho(parameters.projectionMat, -1, 1, -1, 1, 0.5, 5); + } else { + mat4.perspective(parameters.projectionMat, Math.PI / 2, 1, 0.5, 5); + } + const sources = [ + makeTransformedSource("8 mm", 8, 2000), + makeTransformedSource("4 mm", 4, 45000), + makeTransformedSource("2 mm", 2, 110000), + makeTransformedSource("1 mm", 1, 0), + ]; + for (const source of sources) { + source.upperClipDisplayBound = vec3.fromValues(16, 16, 8); + } + function selectedAtZoom(zoom: number) { + mat4.fromTranslation(parameters.viewMatrix, vec3.fromValues(0, 0, -3)); + mat4.scale( + parameters.viewMatrix, + parameters.viewMatrix, + vec3.fromValues(1 / zoom, 1 / zoom, 1 / zoom), + ); + mat4.translate( + parameters.viewMatrix, + parameters.viewMatrix, + vec3.fromValues(-8, -8, -4), + ); + let selected: string | undefined; + forEachSpatialSkeletonSourceScale( + parameters, + 8, + sources, + (source, _index, _physicalSpacing, _pixelSpacing, isSelected) => { + if (isSelected) selected = (source as any).label; + }, + ); + return selected; + } + expect(selectedAtZoom(initialZoom)).toBe("4 mm"); + expect(selectedAtZoom(mediumZoom)).toBe("2 mm"); + expect(selectedAtZoom(fineZoom)).toBe("1 mm"); + expect(selectedAtZoom(1024)).toBe("8 mm"); + const context = getSpatialSkeletonSourceDensityContext( + parameters, + 8, + sources, + )!; + expect(context.viewportArea).toBeLessThan( + parameters.width * parameters.height, + ); + expect(selectedAtZoom(2048)).toBe("8 mm"); + const furtherOut = getSpatialSkeletonSourceDensityContext( + parameters, + 8, + sources, + )!; + expect(furtherOut.physicalDensityTarget).toBeLessThan( + context.physicalDensityTarget / 3.5, + ); + expect(selectedAtZoom(initialZoom)).toBe("4 mm"); + }, + ); + + it("clips projected data coverage to the viewport and applies source transforms", () => { + const parameters = makeProjectionParameters(); + const source = makeTransformedSource("source", 1, 1000); + const context = getSpatialSkeletonSourceDensityContext(parameters, 8, [ + source, + ])!; + expect(context.viewportArea).toBe(250000); + mat4.fromTranslation( + source.chunkLayout.transform, + vec3.fromValues(2, 0, 0), + ); + const offscreen = getSpatialSkeletonSourceDensityContext(parameters, 8, [ + source, + ])!; + expect(offscreen.viewportArea).toBe(0); + expect(offscreen.physicalDensityTarget).toBe(0); + }); + + it("retains the full budget for camera-plane crossings and unbounded sources", () => { + const parameters = makeProjectionParameters(); + mat4.perspective(parameters.projectionMat, Math.PI / 2, 1, 0.5, 5); + const source = makeTransformedSource("source", 8, 1000); + source.upperClipDisplayBound = vec3.fromValues(8, 8, 8); + mat4.fromTranslation(parameters.viewMatrix, vec3.fromValues(-4, -4, -4)); + const crossing = getSpatialSkeletonSourceDensityContext(parameters, 8, [ + source, + ])!; + expect(crossing.viewportArea).toBe(1000000); + source.upperClipDisplayBound[0] = Number.POSITIVE_INFINITY; + const unbounded = getSpatialSkeletonSourceDensityContext(parameters, 8, [ + source, + ])!; + expect(unbounded.viewportArea).toBe(1000000); + }); }); describe("skeleton/source_selection", () => { diff --git a/src/skeleton/spatial/base.ts b/src/skeleton/spatial/base.ts index e00240a22a..838497ed31 100644 --- a/src/skeleton/spatial/base.ts +++ b/src/skeleton/spatial/base.ts @@ -57,6 +57,7 @@ export interface SpatiallyIndexedSkeletonChunkSpecification } const tempMat3 = mat3.create(); +const tempSourceToClip = mat4.create(); export interface SpatialSkeletonSourceDensityContext< Transformed extends TransformedSource, @@ -93,6 +94,51 @@ function getSpatialSkeletonChunkPhysicalVolume( ); } +function getSpatialSkeletonProjectedViewportArea( + { width, height, projectionMat, viewMatrix }: ProjectionParameters, + { + chunkLayout, + lowerClipDisplayBound: lower, + upperClipDisplayBound: upper, + }: TransformedSource, +) { + const fullViewportArea = width * height; + for (let i = 0; i < 3; ++i) { + if (!Number.isFinite(lower[i]) || !Number.isFinite(upper[i])) { + return fullViewportArea; + } + } + const m = tempSourceToClip; + mat4.multiply(m, projectionMat, viewMatrix); + mat4.multiply(m, m, chunkLayout.transform); + let minX = Number.POSITIVE_INFINITY; + let minY = Number.POSITIVE_INFINITY; + let maxX = Number.NEGATIVE_INFINITY; + let maxY = Number.NEGATIVE_INFINITY; + for (let corner = 0; corner < 8; ++corner) { + const x = corner & 1 ? upper[0] : lower[0]; + const y = corner & 2 ? upper[1] : lower[1]; + const z = corner & 4 ? upper[2] : lower[2]; + const w = m[3] * x + m[7] * y + m[11] * z + m[15]; + // Bounds crossing the camera plane cannot be projected from just their + // corners. Keep the full budget rather than underestimating their coverage. + if (w <= 0) return fullViewportArea; + const clipX = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w; + const clipY = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w; + if (!Number.isFinite(clipX) || !Number.isFinite(clipY)) { + return fullViewportArea; + } + minX = Math.min(minX, clipX); + minY = Math.min(minY, clipY); + maxX = Math.max(maxX, clipX); + maxY = Math.max(maxY, clipY); + } + const widthFraction = Math.max(0, Math.min(1, maxX) - Math.max(-1, minX)) / 2; + const heightFraction = + Math.max(0, Math.min(1, maxY) - Math.max(-1, minY)) / 2; + return fullViewportArea * widthFraction * heightFraction; +} + export function getSpatialSkeletonSourceDensityContext< Transformed extends TransformedSource, >( @@ -102,13 +148,8 @@ export function getSpatialSkeletonSourceDensityContext< ): SpatialSkeletonSourceDensityContext | undefined { if (transformedSources.length === 0) return undefined; - const { - displayDimensionRenderInfo, - viewMatrix, - projectionMat, - width, - height, - } = projectionParameters; + const { displayDimensionRenderInfo, viewMatrix, projectionMat } = + projectionParameters; const { voxelPhysicalScales } = displayDimensionRenderInfo; const viewDet = Math.abs( mat3.determinant(mat3FromMat4(tempMat3, viewMatrix)), @@ -140,7 +181,12 @@ export function getSpatialSkeletonSourceDensityContext< } const effectiveVolume = Math.min(sourceVolume, viewFrustumVolume); - const viewportArea = width * height; + // Once the source volume is the limiting volume, empty screen margins must + // not keep the node budget fixed as the dataset becomes smaller on screen. + const viewportArea = getSpatialSkeletonProjectedViewportArea( + projectionParameters, + baseSource, + ); const targetNumNodes = viewportArea / spacingTarget ** 2; const physicalDensityTarget = targetNumNodes / effectiveVolume; return { diff --git a/src/widget/render_scale_widget.spec.ts b/src/widget/render_scale_widget.spec.ts new file mode 100644 index 0000000000..323d293275 --- /dev/null +++ b/src/widget/render_scale_widget.spec.ts @@ -0,0 +1,88 @@ +/** + * @license + * Copyright 2026 Google Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { + RenderScaleHistogram, + trackableRenderScaleTarget, +} from "#src/render_scale_statistics.js"; +import { WatchableVisibilityPriority } from "#src/visibility_priority/frontend.js"; +import { RenderScaleWidget } from "#src/widget/render_scale_widget.js"; + +let widget: RenderScaleWidget | undefined; +beforeEach(() => { + vi.useFakeTimers(); + vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue({ + clearRect: vi.fn(), + fillRect: vi.fn(), + } as unknown as CanvasRenderingContext2D); + vi.stubGlobal( + "ResizeObserver", + class { + observe() {} + disconnect() {} + }, + ); +}); +afterEach(() => { + widget?.dispose(); + widget = undefined; + document.body.replaceChildren(); + vi.restoreAllMocks(); + vi.unstubAllGlobals(); + vi.useRealTimers(); +}); + +describe("RenderScaleWidget", () => { + it("shows 0/0 when projection is inactive without subtracting indicator bars", () => { + const histogram = new RenderScaleHistogram(); + const visibility = new WatchableVisibilityPriority( + Number.POSITIVE_INFINITY, + ); + const removeVisibility = histogram.visibility.add(visibility); + histogram.begin(1); + histogram.add(100, 8, 3, 2); + for (const scale of [200, 300, Number.POSITIVE_INFINITY]) { + histogram.add(scale, 16, 0, 10, true); + } + const target = trackableRenderScaleTarget(8); + widget = new RenderScaleWidget(histogram, target); + expect(widget.legendChunks.textContent).toBe("3/5"); + removeVisibility(); + widget.updateView(); + expect(widget.legendChunks.textContent).toBe("0/0"); + expect(target.value).toBe(8); + }); + + it("preserves shared histogram data across hiding and reactivation", () => { + const histogram = new RenderScaleHistogram(); + const visibility = new WatchableVisibilityPriority( + Number.POSITIVE_INFINITY, + ); + histogram.visibility.add(visibility); + histogram.begin(7); + histogram.add(100, 8, 18, 0); + histogram.add(200, 16, 0, 10, true); + widget = new RenderScaleWidget(histogram, trackableRenderScaleTarget(8)); + const originalBins = histogram.value.slice(); + visibility.value = Number.NEGATIVE_INFINITY; + widget.updateView(); + expect(histogram.value).toEqual(originalBins); + visibility.value = Number.POSITIVE_INFINITY; + widget.updateView(); + expect(widget.legendChunks.textContent).toBe("18/18"); + }); +}); diff --git a/src/widget/render_scale_widget.ts b/src/widget/render_scale_widget.ts index 4ec850d390..ee2db7103e 100644 --- a/src/widget/render_scale_widget.ts +++ b/src/widget/render_scale_widget.ts @@ -223,20 +223,19 @@ export class RenderScaleWidget extends RefCounted { ctx.clearRect(0, 0, width, height); const { histogram } = this; - // histogram.begin(this.frameNumberCounter.frameNumber); const { value: histogramData, spatialScales } = histogram; - - if (!histogram.visibility.visible) { - histogramData.fill(0); - } - - const sortedSpatialScales = Array.from(spatialScales.keys()); + // An inactive view has no displayed chunks. Do not clear shared bins while + // retaining their indicator count, which would produce a negative total. + const histogramVisible = histogram.visibility.visible; + const sortedSpatialScales = histogramVisible + ? Array.from(spatialScales.keys()) + : []; sortedSpatialScales.sort(); const tempColor = vec3.create(); let maxCount = 1; - const numRows = spatialScales.size; + const numRows = sortedSpatialScales.length; let totalPresent = 0; let totalNotPresent = 0; for (let bin = 0; bin < numBins; ++bin) { @@ -251,7 +250,7 @@ export class RenderScaleWidget extends RefCounted { } maxCount = Math.max(count, maxCount); } - totalNotPresent -= histogram.fakeChunkCount; + if (histogramVisible) totalNotPresent -= histogram.fakeChunkCount; const maxBarHeight = height; From 81d646b8e6c41c49283931cbfab5eb819dc7b771 Mon Sep 17 00:00:00 2001 From: Afonso Pinto Date: Thu, 5 Nov 2026 01:30:13 +0000 Subject: [PATCH 2/2] Remove projected-area skeleton selection change Keep the inactive histogram counter fix and its regression tests. Restore skeleton density selection and tests to feature/edit-mode behavior. --- src/skeleton/spatial/base.spec.ts | 114 ------------------------------ src/skeleton/spatial/base.ts | 62 +++------------- 2 files changed, 8 insertions(+), 168 deletions(-) diff --git a/src/skeleton/spatial/base.spec.ts b/src/skeleton/spatial/base.spec.ts index e9d77318e7..a052727236 100644 --- a/src/skeleton/spatial/base.spec.ts +++ b/src/skeleton/spatial/base.spec.ts @@ -22,7 +22,6 @@ import { RenderScaleHistogram } from "#src/render_scale_statistics.js"; import { forEachSpatialSkeletonSourceScale, forEachVisibleSpatialSkeletonChunk, - getSpatialSkeletonSourceDensityContext, getSpatialSkeletonLevelSpecs, getSpatialSkeletonSourceScalesByLimit, selectSpatialSkeletonSourceByLimit, @@ -150,119 +149,6 @@ describe("forEachVisibleSpatialSkeletonChunk", () => { { label: "fine", selected: true }, ]); }); - it.each([ - { - projection: "orthographic", - initialZoom: 2, - mediumZoom: 0.75, - fineZoom: 0.25, - }, - { - projection: "perspective", - initialZoom: 1, - mediumZoom: 0.35, - fineZoom: 0.15, - }, - ])( - "selects the 8 mm overview when zooming out past the dataset ($projection)", - ({ projection, initialZoom, mediumZoom, fineZoom }) => { - const parameters = makeProjectionParameters(); - if (projection === "orthographic") { - mat4.ortho(parameters.projectionMat, -1, 1, -1, 1, 0.5, 5); - } else { - mat4.perspective(parameters.projectionMat, Math.PI / 2, 1, 0.5, 5); - } - const sources = [ - makeTransformedSource("8 mm", 8, 2000), - makeTransformedSource("4 mm", 4, 45000), - makeTransformedSource("2 mm", 2, 110000), - makeTransformedSource("1 mm", 1, 0), - ]; - for (const source of sources) { - source.upperClipDisplayBound = vec3.fromValues(16, 16, 8); - } - function selectedAtZoom(zoom: number) { - mat4.fromTranslation(parameters.viewMatrix, vec3.fromValues(0, 0, -3)); - mat4.scale( - parameters.viewMatrix, - parameters.viewMatrix, - vec3.fromValues(1 / zoom, 1 / zoom, 1 / zoom), - ); - mat4.translate( - parameters.viewMatrix, - parameters.viewMatrix, - vec3.fromValues(-8, -8, -4), - ); - let selected: string | undefined; - forEachSpatialSkeletonSourceScale( - parameters, - 8, - sources, - (source, _index, _physicalSpacing, _pixelSpacing, isSelected) => { - if (isSelected) selected = (source as any).label; - }, - ); - return selected; - } - expect(selectedAtZoom(initialZoom)).toBe("4 mm"); - expect(selectedAtZoom(mediumZoom)).toBe("2 mm"); - expect(selectedAtZoom(fineZoom)).toBe("1 mm"); - expect(selectedAtZoom(1024)).toBe("8 mm"); - const context = getSpatialSkeletonSourceDensityContext( - parameters, - 8, - sources, - )!; - expect(context.viewportArea).toBeLessThan( - parameters.width * parameters.height, - ); - expect(selectedAtZoom(2048)).toBe("8 mm"); - const furtherOut = getSpatialSkeletonSourceDensityContext( - parameters, - 8, - sources, - )!; - expect(furtherOut.physicalDensityTarget).toBeLessThan( - context.physicalDensityTarget / 3.5, - ); - expect(selectedAtZoom(initialZoom)).toBe("4 mm"); - }, - ); - - it("clips projected data coverage to the viewport and applies source transforms", () => { - const parameters = makeProjectionParameters(); - const source = makeTransformedSource("source", 1, 1000); - const context = getSpatialSkeletonSourceDensityContext(parameters, 8, [ - source, - ])!; - expect(context.viewportArea).toBe(250000); - mat4.fromTranslation( - source.chunkLayout.transform, - vec3.fromValues(2, 0, 0), - ); - const offscreen = getSpatialSkeletonSourceDensityContext(parameters, 8, [ - source, - ])!; - expect(offscreen.viewportArea).toBe(0); - expect(offscreen.physicalDensityTarget).toBe(0); - }); - - it("retains the full budget for camera-plane crossings and unbounded sources", () => { - const parameters = makeProjectionParameters(); - mat4.perspective(parameters.projectionMat, Math.PI / 2, 1, 0.5, 5); - const source = makeTransformedSource("source", 8, 1000); - source.upperClipDisplayBound = vec3.fromValues(8, 8, 8); - mat4.fromTranslation(parameters.viewMatrix, vec3.fromValues(-4, -4, -4)); - const crossing = getSpatialSkeletonSourceDensityContext(parameters, 8, [ - source, - ])!; - expect(crossing.viewportArea).toBe(1000000); - source.upperClipDisplayBound[0] = Number.POSITIVE_INFINITY; - const unbounded = getSpatialSkeletonSourceDensityContext(parameters, 8, [ - source, - ])!; - expect(unbounded.viewportArea).toBe(1000000); - }); }); describe("skeleton/source_selection", () => { diff --git a/src/skeleton/spatial/base.ts b/src/skeleton/spatial/base.ts index 838497ed31..e00240a22a 100644 --- a/src/skeleton/spatial/base.ts +++ b/src/skeleton/spatial/base.ts @@ -57,7 +57,6 @@ export interface SpatiallyIndexedSkeletonChunkSpecification } const tempMat3 = mat3.create(); -const tempSourceToClip = mat4.create(); export interface SpatialSkeletonSourceDensityContext< Transformed extends TransformedSource, @@ -94,51 +93,6 @@ function getSpatialSkeletonChunkPhysicalVolume( ); } -function getSpatialSkeletonProjectedViewportArea( - { width, height, projectionMat, viewMatrix }: ProjectionParameters, - { - chunkLayout, - lowerClipDisplayBound: lower, - upperClipDisplayBound: upper, - }: TransformedSource, -) { - const fullViewportArea = width * height; - for (let i = 0; i < 3; ++i) { - if (!Number.isFinite(lower[i]) || !Number.isFinite(upper[i])) { - return fullViewportArea; - } - } - const m = tempSourceToClip; - mat4.multiply(m, projectionMat, viewMatrix); - mat4.multiply(m, m, chunkLayout.transform); - let minX = Number.POSITIVE_INFINITY; - let minY = Number.POSITIVE_INFINITY; - let maxX = Number.NEGATIVE_INFINITY; - let maxY = Number.NEGATIVE_INFINITY; - for (let corner = 0; corner < 8; ++corner) { - const x = corner & 1 ? upper[0] : lower[0]; - const y = corner & 2 ? upper[1] : lower[1]; - const z = corner & 4 ? upper[2] : lower[2]; - const w = m[3] * x + m[7] * y + m[11] * z + m[15]; - // Bounds crossing the camera plane cannot be projected from just their - // corners. Keep the full budget rather than underestimating their coverage. - if (w <= 0) return fullViewportArea; - const clipX = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w; - const clipY = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w; - if (!Number.isFinite(clipX) || !Number.isFinite(clipY)) { - return fullViewportArea; - } - minX = Math.min(minX, clipX); - minY = Math.min(minY, clipY); - maxX = Math.max(maxX, clipX); - maxY = Math.max(maxY, clipY); - } - const widthFraction = Math.max(0, Math.min(1, maxX) - Math.max(-1, minX)) / 2; - const heightFraction = - Math.max(0, Math.min(1, maxY) - Math.max(-1, minY)) / 2; - return fullViewportArea * widthFraction * heightFraction; -} - export function getSpatialSkeletonSourceDensityContext< Transformed extends TransformedSource, >( @@ -148,8 +102,13 @@ export function getSpatialSkeletonSourceDensityContext< ): SpatialSkeletonSourceDensityContext | undefined { if (transformedSources.length === 0) return undefined; - const { displayDimensionRenderInfo, viewMatrix, projectionMat } = - projectionParameters; + const { + displayDimensionRenderInfo, + viewMatrix, + projectionMat, + width, + height, + } = projectionParameters; const { voxelPhysicalScales } = displayDimensionRenderInfo; const viewDet = Math.abs( mat3.determinant(mat3FromMat4(tempMat3, viewMatrix)), @@ -181,12 +140,7 @@ export function getSpatialSkeletonSourceDensityContext< } const effectiveVolume = Math.min(sourceVolume, viewFrustumVolume); - // Once the source volume is the limiting volume, empty screen margins must - // not keep the node budget fixed as the dataset becomes smaller on screen. - const viewportArea = getSpatialSkeletonProjectedViewportArea( - projectionParameters, - baseSource, - ); + const viewportArea = width * height; const targetNumNodes = viewportArea / spacingTarget ** 2; const physicalDensityTarget = targetNumNodes / effectiveVolume; return {