diff --git a/src/components/VolumeRendering.vue b/src/components/VolumeRendering.vue index 4771ca312..76b33c615 100644 --- a/src/components/VolumeRendering.vue +++ b/src/components/VolumeRendering.vue @@ -275,6 +275,10 @@ export default defineComponent({ ); }; + const resetColoring = () => { + if (selectedPreset.value) selectPreset(selectedPreset.value); + }; + // --- mapping range editing --- // onKeyDown('Control', () => pwfWidget.setShiftOpacityValues(true)); @@ -352,6 +356,7 @@ export default defineComponent({ request3DAnimation, cancel3DAnimation, selectPreset, + resetColoring, }; }, }); @@ -359,32 +364,56 @@ export default defineComponent({ diff --git a/tests/specs/volume-coloring-reset.e2e.ts b/tests/specs/volume-coloring-reset.e2e.ts new file mode 100644 index 000000000..0860aec7a --- /dev/null +++ b/tests/specs/volume-coloring-reset.e2e.ts @@ -0,0 +1,148 @@ +import { volViewPage } from '../pageobjects/volview.page'; +import { captureViewPixels, waitForModuleTransitions } from './utils'; +import { + differingFraction, + floatVoxels, + nrrd, + showColorPresets, + openWith3DLayout, + selectedPreset, + writeNrrd, +} from './volumeTestUtils'; + +const SIZE = 16; +const EDITOR = '[data-testid="volume-opacity-editor"]'; +const RESET = '[data-testid="volume-reset-color-opacity"]'; + +const thumb = (slider: string) => $(slider).$('[role="slider"]'); +const shiftThumb = () => thumb('[data-testid="volume-color-shift"]'); +const widthThumb = () => thumb('[data-testid="volume-color-width"]'); +const ambientThumb = () => $('.v-slider*=Ambient').$('[role="slider"]'); +const sliderValue = async (slider: ChainablePromiseElement) => + String(await slider.getAttribute('aria-valuenow')); + +async function pixels(selector: string, inset: number, file: string) { + const [{ data }] = await captureViewPixels(selector, inset, file); + return data; +} + +async function waitForPixels( + selector: string, + inset: number, + expected: Buffer, + matches: boolean, + timeoutMsg: string +) { + await browser.waitUntil( + async () => { + const fraction = differingFraction( + await pixels(selector, inset, 'volume-coloring-reset.png'), + expected + ); + return matches ? fraction < 0.01 : fraction > 0.05; + }, + { timeoutMsg } + ); +} + +async function stablePixels(selector: string, inset: number) { + let previous = await pixels(selector, inset, 'volume-coloring-stable.png'); + await browser.waitUntil(async () => { + const next = await pixels(selector, inset, 'volume-coloring-stable.png'); + const same = next.equals(previous); + previous = next; + return same; + }); + return previous; +} + +async function pressOn(slider: ChainablePromiseElement, key: string) { + await slider.scrollIntoView({ block: 'center', behavior: 'instant' }); + await slider.click(); + await browser.keys(key); +} + +async function dragOpacityCurve(steps: number) { + const editor = $(EDITOR).$('canvas'); + await editor.scrollIntoView({ block: 'center', behavior: 'instant' }); + const drag = browser + .action('pointer') + .move({ origin: editor, x: -60, y: 0 }) + .down(); + for (let step = 0; step < steps; step++) + drag.move({ origin: 'pointer', x: 30, y: 0, duration: 50 }); + await drag.up().perform(); +} + +describe('3D rendering color and opacity reset', () => { + it('restores Shift, Width, and the opacity curve while keeping lighting', async () => { + const name = 'volume-coloring-reset'; + const voxels = floatVoxels(SIZE, (index) => { + const offsets = [ + index % SIZE, + Math.floor(index / SIZE) % SIZE, + Math.floor(index / SIZE ** 2), + ].map((c) => c - SIZE / 2); + return Math.max(0, 1000 * (1 - Math.hypot(...offsets) / (SIZE / 2))); + }); + await openWith3DLayout( + writeNrrd(name, nrrd(['modality:=MR'], 'float', SIZE), voxels), + name + ); + await volViewPage.renderingModuleTab.waitForClickable(); + await volViewPage.renderingModuleTab.click(); + await waitForModuleTransitions(); + const reset = $(RESET); + await expect(reset).toHaveAttribute( + 'aria-label', + 'Reset color and opacity' + ); + + // Reset must keep a preset chosen over the MR default. + await showColorPresets(); + await $('.thumbnail-container*=Grayscale').click(); + await expect(selectedPreset()).toHaveText('Grayscale'); + // The open preset grid pushes the opacity editor out of the capture area. + await showColorPresets(false); + + // A gaussian stays highlighted once clicked, so select it before the baseline. + await dragOpacityCurve(0); + await pressOn(ambientThumb(), 'ArrowRight'); + await expect(ambientThumb()).toHaveAttribute('aria-valuenow', '0.3'); + + const shift = await sliderValue(shiftThumb()); + const width = await sliderValue(widthThumb()); + // The editor draws both the opacity curve and the color bar for Shift and Width. + const editor = await stablePixels(EDITOR, 0); + + await dragOpacityCurve(3); + await waitForPixels( + EDITOR, + 0, + editor, + false, + 'Expected dragging to change the opacity curve' + ); + await pressOn(shiftThumb(), 'End'); + await pressOn(widthThumb(), 'Home'); + await expect(shiftThumb()).not.toHaveAttribute('aria-valuenow', shift); + await expect(widthThumb()).not.toHaveAttribute('aria-valuenow', width); + + await browser.keys('Tab'); + await expect(reset).toBeFocused(); + await browser.keys('Enter'); + + await expect(shiftThumb()).toHaveAttribute('aria-valuenow', shift); + await expect(widthThumb()).toHaveAttribute('aria-valuenow', width); + await waitForPixels( + EDITOR, + 0, + editor, + true, + 'Expected reset to restore the opacity curve and color bar' + ); + await expect(ambientThumb()).toHaveAttribute('aria-valuenow', '0.3'); + await showColorPresets(); + await expect(selectedPreset()).toHaveText('Grayscale'); + }).timeout(180_000); +}); diff --git a/tests/specs/volume-modality-presets.e2e.ts b/tests/specs/volume-modality-presets.e2e.ts index c3e2d0985..015ba3b0c 100644 --- a/tests/specs/volume-modality-presets.e2e.ts +++ b/tests/specs/volume-modality-presets.e2e.ts @@ -8,36 +8,20 @@ import { waitForModuleTransitions, writeManifestToFile, } from './utils'; - -const LAYOUT = { - layouts: { - default: { - direction: 'row', - items: [{ type: '3D', viewDirection: 'Superior', viewUp: 'Anterior' }], - }, - }, -}; +import { + differingFraction, + floatVoxels, + nrrd, + showColorPresets, + openWith3DLayout, + selectedPreset, + writeNrrd, +} from './volumeTestUtils'; const VOXELS = Buffer.from( Array.from({ length: 512 }, (_, index) => index % 256) ); -const nrrd = (fields: string[], type = 'uchar', size = 8) => - [ - 'NRRD0005', - `type: ${type}`, - 'dimension: 3', - `sizes: ${size} ${size} ${size}`, - 'endian: little', - 'space: left-posterior-superior', - 'space directions: (1,0,0) (0,1,0) (0,0,1)', - 'space origin: (0,0,0)', - 'encoding: raw', - ...fields, - '', - '', - ].join('\n'); - const mha = (fields: string[]) => [ 'ObjectType = Image', @@ -49,22 +33,6 @@ const mha = (fields: string[]) => '', ].join('\n'); -function floatVoxels(size: number, valueAt: (index: number) => number) { - const buffer = Buffer.alloc(size ** 3 * 4); - for (let index = 0; index < size ** 3; index++) - buffer.writeFloatLE(valueAt(index), index * 4); - return buffer; -} - -function writeNrrd(name: string, header: string, voxels: Buffer) { - const directory = makeTempDir(name); - fs.writeFileSync( - path.join(directory, `${name}.nrrd`), - Buffer.concat([Buffer.from(header), voxels]) - ); - return `tmp/${name}/${name}.nrrd`; -} - const sliceValue = (index: number) => 100 * (index + 1); function writeDicomSeries(directory: string, modality: string, slices = 4) { @@ -91,23 +59,12 @@ function writeDicomSeries(directory: string, modality: string, slices = 4) { }); } -async function openWith3DLayout(dataUrl: string, name: string) { - const config = `${name}-layout.json`; - await writeManifestToFile(LAYOUT, config); - await volViewPage.open(`?urls=[tmp/${config},${dataUrl}]`); - await volViewPage.waitForViewCounts(0, true); -} - async function expectSelectedPreset(label: string) { await volViewPage.renderingModuleTab.waitForClickable(); await volViewPage.renderingModuleTab.click(); await waitForModuleTransitions(); - const title = $('.v-expansion-panel-title*=Color Presets'); - await title.scrollIntoView({ block: 'center', behavior: 'instant' }); - if ((await title.getAttribute('aria-expanded')) !== 'true') { - await title.click(); - } - await expect($('.thumbnail-container.bg-blue')).toHaveText(label); + await showColorPresets(); + await expect(selectedPreset()).toHaveText(label); } describe('volume preset from declared modality', () => { @@ -175,20 +132,6 @@ async function brightCenterPixels(name: string, inset: number, level = 40) { return { bright, fraction: bright / (data.length / 4) }; } -/** Fraction of RGBA pixels whose largest channel difference exceeds 8. */ -function differingFraction(a: Buffer, b: Buffer) { - let differing = 0; - for (let index = 0; index < a.length; index += 4) { - const delta = Math.max( - Math.abs(a[index] - b[index]), - Math.abs(a[index + 1] - b[index + 1]), - Math.abs(a[index + 2] - b[index + 2]) - ); - if (delta > 8) differing++; - } - return differing / (a.length / 4); -} - /** Captures the 3D view until two consecutive frames match. */ async function stableViewPixels() { const capture = async () => diff --git a/tests/specs/volumeTestUtils.ts b/tests/specs/volumeTestUtils.ts new file mode 100644 index 000000000..554d391fc --- /dev/null +++ b/tests/specs/volumeTestUtils.ts @@ -0,0 +1,80 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { volViewPage } from '../pageobjects/volview.page'; +import { makeTempDir, writeManifestToFile } from './utils'; + +const LAYOUT = { + layouts: { + default: { + direction: 'row', + items: [{ type: '3D', viewDirection: 'Superior', viewUp: 'Anterior' }], + }, + }, +}; + +export const nrrd = (fields: string[], type = 'uchar', size = 8) => + [ + 'NRRD0005', + `type: ${type}`, + 'dimension: 3', + `sizes: ${size} ${size} ${size}`, + 'endian: little', + 'space: left-posterior-superior', + 'space directions: (1,0,0) (0,1,0) (0,0,1)', + 'space origin: (0,0,0)', + 'encoding: raw', + ...fields, + '', + '', + ].join('\n'); + +export function floatVoxels(size: number, valueAt: (index: number) => number) { + const buffer = Buffer.alloc(size ** 3 * 4); + for (let index = 0; index < size ** 3; index++) + buffer.writeFloatLE(valueAt(index), index * 4); + return buffer; +} + +export function writeNrrd(name: string, header: string, voxels: Buffer) { + const directory = makeTempDir(name); + fs.writeFileSync( + path.join(directory, `${name}.nrrd`), + Buffer.concat([Buffer.from(header), voxels]) + ); + return `tmp/${name}/${name}.nrrd`; +} + +export async function showColorPresets(show = true) { + const title = $('.v-expansion-panel-title*=Color Presets'); + await title.scrollIntoView({ block: 'center', behavior: 'instant' }); + if (((await title.getAttribute('aria-expanded')) === 'true') !== show) { + await title.click(); + } +} + +export const selectedPreset = () => $('.thumbnail-container.bg-blue'); + +export async function openWith3DLayout(dataUrl: string, name: string) { + const config = `${name}-layout.json`; + await writeManifestToFile(LAYOUT, config); + await volViewPage.open(`?urls=[tmp/${config},${dataUrl}]`); + await volViewPage.waitForViewCounts(0, true); + // The load-complete toast covers the lower panel controls for a few seconds. + await browser.waitUntil( + async () => (await $$('.Vue-Toastification__toast').length) === 0 + ); +} + +/** Fraction of RGBA pixels whose largest channel difference exceeds 8. */ +export function differingFraction(a: Buffer, b: Buffer) { + let differing = 0; + for (let index = 0; index < a.length; index += 4) { + const delta = Math.max( + Math.abs(a[index] - b[index]), + Math.abs(a[index + 1] - b[index + 1]), + Math.abs(a[index + 2] - b[index + 2]) + ); + if (delta > 8) differing++; + } + return differing / (a.length / 4); +}