Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,31 @@ export class DefaultPlanarDataProvider implements PlanarDataProvider {
? imageVolume.imageIds
: dataSet.imageIds;

// The volume sorts its imageIds by position along the scan axis, which can
// reorder (commonly reverse) them relative to the registered dataSet order
// the caller computed initialImageIdIndex against. Remap the index through
// the imageId so the payload index addresses the slice the caller asked
// for in the payload's (volume) ordering.
let volumeInitialImageIdIndex = initialImageIdIndex;
if (initialImageIdIndex !== undefined && imageIds !== dataSet.imageIds) {
const requestedImageId = dataSet.imageIds[initialImageIdIndex];
const remappedIndex = imageIds.indexOf(requestedImageId);
if (remappedIndex >= 0) {
volumeInitialImageIdIndex = remappedIndex;
} else {
console.warn(
`[PlanarViewport] initialImageIdIndex remap failed: imageId ` +
`"${requestedImageId}" not found in the volume imageIds; ` +
`using the original index ${initialImageIdIndex}`
);
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

return {
id: dataId,
type: 'image',
imageIds,
initialImageIdIndex,
initialImageIdIndex: volumeInitialImageIdIndex,
acquisitionOrientation: options.acquisitionOrientation,
imageData: dataSet.imageData,
imageVolume,
Expand Down
141 changes: 141 additions & 0 deletions packages/core/test/defaultPlanarDataProvider.jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
jest.mock('../src/loaders/volumeLoader', () => ({
__esModule: true,
createAndCacheVolume: jest.fn(),
}));

jest.mock('../src/loaders/imageLoader', () => ({
__esModule: true,
loadAndCacheImage: jest.fn(async (imageId) => ({ imageId })),
}));

jest.mock(
'../src/RenderingEngine/GenericViewport/genericViewportDisplaySetAccess',
() => ({
__esModule: true,
getGenericViewportPlanarDisplaySet: jest.fn(),
})
);

import { ActorRenderMode } from '../src/types';
import { createAndCacheVolume } from '../src/loaders/volumeLoader';
import { getGenericViewportPlanarDisplaySet } from '../src/RenderingEngine/GenericViewport/genericViewportDisplaySetAccess';
import { DefaultPlanarDataProvider } from '../src/RenderingEngine/GenericViewport/Planar/DefaultPlanarDataProvider';

const DATA_ID = 'display-set-1';

function makeImageIds(count) {
return Array.from({ length: count }, (_, i) => `wadors:image-${i}`);
}

function registerDataSet(dataSet) {
getGenericViewportPlanarDisplaySet.mockReturnValue(dataSet);
}

function mockVolume(imageIds) {
createAndCacheVolume.mockResolvedValue({
imageIds,
load: jest.fn(),
});
}

describe('DefaultPlanarDataProvider', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('volume-slice payloads', () => {
it('remaps initialImageIdIndex when the volume reorders the imageIds', async () => {
// The registered dataSet carries the caller's ordering; the cached volume
// sorts by position along the scan axis, here the exact reverse.
const dataSetImageIds = makeImageIds(30);
registerDataSet({ imageIds: dataSetImageIds, initialImageIdIndex: 12 });
mockVolume([...dataSetImageIds].reverse());

const provider = new DefaultPlanarDataProvider();
const payload = await provider.load(DATA_ID, {
orientation: 'acquisition',
renderMode: ActorRenderMode.VTK_VOLUME_SLICE,
volumeId: 'volume-1',
});

// Index 12 in dataSet order is imageId image-12; in the reversed volume
// ordering that image sits at index 17 — the payload must address it there,
// not land on the mirrored slice.
expect(payload.imageIds[payload.initialImageIdIndex]).toBe(
'wadors:image-12'
);
expect(payload.initialImageIdIndex).toBe(17);
});

it('keeps the index unchanged when the volume preserves the ordering', async () => {
const dataSetImageIds = makeImageIds(10);
registerDataSet({ imageIds: dataSetImageIds, initialImageIdIndex: 3 });
mockVolume([...dataSetImageIds]);

const provider = new DefaultPlanarDataProvider();
const payload = await provider.load(DATA_ID, {
orientation: 'acquisition',
renderMode: ActorRenderMode.VTK_VOLUME_SLICE,
volumeId: 'volume-1',
});

expect(payload.initialImageIdIndex).toBe(3);
});

it('warns and keeps the original index when the imageId is not in the volume', async () => {
const dataSetImageIds = makeImageIds(10);
registerDataSet({ imageIds: dataSetImageIds, initialImageIdIndex: 3 });
mockVolume(makeImageIds(10).map((id) => `${id}-other`));
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});

try {
const provider = new DefaultPlanarDataProvider();
const payload = await provider.load(DATA_ID, {
orientation: 'acquisition',
renderMode: ActorRenderMode.VTK_VOLUME_SLICE,
volumeId: 'volume-1',
});

expect(payload.initialImageIdIndex).toBe(3);
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('initialImageIdIndex remap failed')
);
} finally {
warnSpy.mockRestore();
}
});

it('preserves "no slice requested" (undefined) so the view can center', async () => {
const dataSetImageIds = makeImageIds(10);
registerDataSet({ imageIds: dataSetImageIds });
mockVolume([...dataSetImageIds].reverse());

const provider = new DefaultPlanarDataProvider();
const payload = await provider.load(DATA_ID, {
orientation: 'acquisition',
renderMode: ActorRenderMode.VTK_VOLUME_SLICE,
volumeId: 'volume-1',
});

expect(payload.initialImageIdIndex).toBeUndefined();
});
});

describe('image payloads', () => {
it('keeps the dataSet ordering and index on the image branch', async () => {
const dataSetImageIds = makeImageIds(5);
registerDataSet({ imageIds: dataSetImageIds, initialImageIdIndex: 2 });

const provider = new DefaultPlanarDataProvider();
const payload = await provider.load(DATA_ID, {
orientation: 'acquisition',
renderMode: ActorRenderMode.VTK_IMAGE,
volumeId: undefined,
});

expect(payload.imageIds).toEqual(dataSetImageIds);
expect(payload.initialImageIdIndex).toBe(2);
expect(payload.image.imageId).toBe('wadors:image-2');
});
});
});
Loading