Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 53 additions & 24 deletions src/components/VolumeRendering.vue
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ export default defineComponent({
);
};

const resetColoring = () => {
if (selectedPreset.value) selectPreset(selectedPreset.value);
};

// --- mapping range editing --- //

onKeyDown('Control', () => pwfWidget.setShiftOpacityValues(true));
Expand Down Expand Up @@ -352,39 +356,64 @@ export default defineComponent({
request3DAnimation,
cancel3DAnimation,
selectPreset,
resetColoring,
};
},
});
</script>

<template>
<div class="overflow-hidden">
<div class="mt-4 pwf-editor" ref="editorContainerRef">
<div
class="mt-4 pwf-editor"
ref="editorContainerRef"
data-testid="volume-opacity-editor"
>
<div ref="pwfEditorRef" />
</div>
<div class="mapping-range-editor">
<v-slider
v-model="rangeShift"
density="compact"
hide-details
label="Shift"
:min="rangeShiftMin"
:max="rangeShiftMax"
:step="colorSliderStep"
@pointerdown="request3DAnimation"
@pointerup="cancel3DAnimation"
/>
<v-slider
v-model="rangeWidth"
density="compact"
hide-details
label="Width"
min="1"
:max="rangeWidthMax"
:step="colorSliderStep"
@pointerdown="request3DAnimation"
@pointerup="cancel3DAnimation"
/>
<div class="mapping-range-editor d-flex align-center ga-2">
<div class="flex-grow-1">
<v-slider
v-model="rangeShift"
data-testid="volume-color-shift"
density="compact"
hide-details
label="Shift"
:min="rangeShiftMin"
:max="rangeShiftMax"
:step="colorSliderStep"
@pointerdown="request3DAnimation"
@pointerup="cancel3DAnimation"
/>
<v-slider
v-model="rangeWidth"
data-testid="volume-color-width"
density="compact"
hide-details
label="Width"
min="1"
:max="rangeWidthMax"
:step="colorSliderStep"
@pointerdown="request3DAnimation"
@pointerup="cancel3DAnimation"
/>
</div>
<v-tooltip
text="Restore the opacity curve, Shift, and Width for the selected preset while keeping its palette and lighting"
>
<template #activator="{ props: tooltipProps }">
<v-btn
v-bind="tooltipProps"
data-testid="volume-reset-color-opacity"
aria-label="Reset color and opacity"
icon="mdi-restore"
size="small"
density="compact"
variant="text"
@click="resetColoring"
/>
</template>
</v-tooltip>
</div>
</div>
</template>
Expand Down
148 changes: 148 additions & 0 deletions tests/specs/volume-coloring-reset.e2e.ts
Original file line number Diff line number Diff line change
@@ -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);
});
79 changes: 11 additions & 68 deletions tests/specs/volume-modality-presets.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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) {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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 () =>
Expand Down
Loading
Loading