Skip to content

Commit 5589385

Browse files
committed
fix(volume-rendering): fit Shift and Width slider steps to the color range
The Shift and Width sliders took their step from the image data range and Width had a fixed minimum of 1. For data narrower than 1, such as normalized floats, Width was pinned and could not narrow the range, and Shift showed a nonzero value when unshifted. Step and bounds now come from the preset's color range: the step is at most 1/256 of it, Width starts at one step, and Shift bounds are whole steps either side of zero. Ranges 256 or wider keep a step of 1.
1 parent 6fb65e3 commit 5589385

4 files changed

Lines changed: 121 additions & 23 deletions

File tree

‎src/components/VolumeRendering.vue‎

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { useVolumeColoringInitializer } from '@/src/composables/useVolumeColorin
2020
import { useResizeObserver } from '@/src/composables/useResizeObserver';
2121
import { useCurrentImage } from '@/src/composables/useCurrentImage';
2222
import useVolumeColoringStore from '@/src/store/view-configs/volume-coloring';
23+
import { mappingRangeSliders } from '@/src/utils/mappingRangeSliders';
2324
import { getShiftedOpacityFromPreset } from '@/src/utils/vtk-helpers';
2425
2526
const WIDGET_WIDTH = 250;
@@ -248,14 +249,6 @@ export default defineComponent({
248249
);
249250
const hasCurrentImage = computed(() => !!currentImageData.value);
250251
251-
const imageDataRange = computed((): [number, number] => {
252-
const image = currentImageData.value;
253-
if (image) {
254-
return image.getPointData().getScalars().getRange();
255-
}
256-
return [0, 1];
257-
});
258-
259252
const fullMappingRange = computed(() =>
260253
currentImageID.value && selectedPreset.value
261254
? volumeColoringStore.getPresetColorRange(
@@ -345,17 +338,9 @@ export default defineComponent({
345338
mappingRange: computed(
346339
() => colorTransferFunctionRef.value!.mappingRange
347340
),
348-
colorSliderStep: computed(() => {
349-
const [low, high] = imageDataRange.value;
350-
const width = high - low;
351-
const step = Math.min(1, width / 256);
352-
return step > 1 ? Math.round(step) : step;
353-
}),
354-
rangeShiftMin: computed(() => -fullMappingRangeWidth.value / 2),
355-
rangeShiftMax: computed(() => fullMappingRangeWidth.value / 2),
341+
sliders: computed(() => mappingRangeSliders(fullMappingRangeWidth.value)),
356342
rangeShift,
357343
rangeWidth,
358-
rangeWidthMax: computed(() => fullMappingRangeWidth.value * 2),
359344
request3DAnimation,
360345
cancel3DAnimation,
361346
selectPreset,
@@ -382,9 +367,9 @@ export default defineComponent({
382367
density="compact"
383368
hide-details
384369
label="Shift"
385-
:min="rangeShiftMin"
386-
:max="rangeShiftMax"
387-
:step="colorSliderStep"
370+
:min="sliders.shift[0]"
371+
:max="sliders.shift[1]"
372+
:step="sliders.step"
388373
@pointerdown="request3DAnimation"
389374
@pointerup="cancel3DAnimation"
390375
/>
@@ -394,9 +379,9 @@ export default defineComponent({
394379
density="compact"
395380
hide-details
396381
label="Width"
397-
min="1"
398-
:max="rangeWidthMax"
399-
:step="colorSliderStep"
382+
:min="sliders.width[0]"
383+
:max="sliders.width[1]"
384+
:step="sliders.step"
400385
@pointerdown="request3DAnimation"
401386
@pointerup="cancel3DAnimation"
402387
/>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { mappingRangeSliders } from '@/src/utils/mappingRangeSliders';
3+
4+
const onGrid = (value: number, origin: number, step: number) =>
5+
Number.isInteger((value - origin) / step);
6+
7+
describe('mappingRangeSliders', () => {
8+
it.each([0.01, 0.78, 6095, 6095.5])(
9+
'lets Width shrink below the full width %s and keeps zero shift on the grid',
10+
(fullWidth) => {
11+
const { step, shift, width } = mappingRangeSliders(fullWidth);
12+
expect(step).toBeGreaterThan(0);
13+
expect(step).toBeLessThanOrEqual(Math.min(1, fullWidth / 256));
14+
expect(width[0]).toBeLessThan(fullWidth);
15+
expect(width[1]).toBe(2 * fullWidth);
16+
expect(shift[0]).toBe(-shift[1]);
17+
expect(shift[1]).toBeGreaterThanOrEqual(fullWidth / 2);
18+
expect(onGrid(0, shift[0], step)).toBe(true);
19+
}
20+
);
21+
22+
it('steps by whole units over large ranges', () => {
23+
expect(mappingRangeSliders(6095)).toEqual({
24+
step: 1,
25+
shift: [-3048, 3048],
26+
width: [1, 12190],
27+
});
28+
});
29+
});

‎src/utils/mappingRangeSliders.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Step and bounds for the Shift and Width sliders that edit a mapping range
3+
* relative to a full range of the given width.
4+
*/
5+
export function mappingRangeSliders(fullWidth: number) {
6+
const step = Math.min(1, fullWidth / 256);
7+
// Whole steps either side of zero keep an unshifted range on the slider grid.
8+
const halfShift = step > 0 ? Math.ceil(fullWidth / 2 / step) * step : 0;
9+
return {
10+
step,
11+
shift: [-halfShift, halfShift] as const,
12+
width: [step, 2 * fullWidth] as const,
13+
};
14+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { volViewPage } from '../pageobjects/volview.page';
2+
import { waitForModuleTransitions } from './utils';
3+
import {
4+
differingFraction,
5+
floatVoxels,
6+
nrrd,
7+
openWith3DLayout,
8+
pressOn,
9+
radialRamp,
10+
sliderThumb,
11+
stablePixels,
12+
VIEW,
13+
writeNrrd,
14+
} from './volumeTestUtils';
15+
16+
const SIZE = 16;
17+
18+
const ramp = radialRamp(SIZE);
19+
const VOLUMES: [string, (index: number) => number][] = [
20+
['normalized with a far outlier', (i) => (i === 0 ? 1000 : ramp(i) / 1000)],
21+
['spanning 0 to 0.01', (i) => ramp(i) / 1e5],
22+
['in Hounsfield units', (i) => (ramp(i) > 300 ? 700 : ramp(i) ? 40 : -1024)],
23+
];
24+
25+
async function slider(testId: string) {
26+
const thumb = sliderThumb(testId);
27+
const [min, max, now] = await Promise.all(
28+
['aria-valuemin', 'aria-valuemax', 'aria-valuenow'].map(async (name) =>
29+
Number(await thumb.getAttribute(name))
30+
)
31+
);
32+
return { thumb, min, max, now };
33+
}
34+
35+
describe('3D rendering Shift and Width sliders', () => {
36+
VOLUMES.forEach(([label, valueAt], index) => {
37+
it(`edit the color range of a volume ${label}`, async () => {
38+
const name = `volume-mapping-range-${index}`;
39+
await openWith3DLayout(
40+
writeNrrd(name, nrrd([], 'float', SIZE), floatVoxels(SIZE, valueAt)),
41+
name
42+
);
43+
await volViewPage.renderingModuleTab.waitForClickable();
44+
await volViewPage.renderingModuleTab.click();
45+
await waitForModuleTransitions();
46+
47+
const shift = await slider('volume-color-shift');
48+
const width = await slider('volume-color-width');
49+
expect(shift.now).toBe(0);
50+
expect(width.min).toBeLessThan(width.now);
51+
expect(width.max / width.now).toBeCloseTo(2, 6);
52+
53+
const before = await stablePixels(VIEW, 40);
54+
await pressOn(width.thumb, 'Home');
55+
const narrowed = await slider('volume-color-width');
56+
expect(narrowed.now).toBe(narrowed.min);
57+
expect(narrowed.now).toBeLessThan(width.now / 100);
58+
await browser.waitUntil(
59+
async () =>
60+
differingFraction(before, await stablePixels(VIEW, 40)) > 0.05,
61+
{ timeoutMsg: 'Expected Home on Width to narrow the 3D render' }
62+
);
63+
64+
await pressOn(shift.thumb, 'ArrowRight');
65+
const shifted = await slider('volume-color-shift');
66+
expect(shifted.now).toBeGreaterThan(0);
67+
expect(shifted.now).toBeLessThanOrEqual(width.now / 128);
68+
});
69+
});
70+
});

0 commit comments

Comments
 (0)