Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | import { loadAndCacheImage } from '../../../loaders/imageLoader';
import { createAndCacheVolume } from '../../../loaders/volumeLoader';
import { ActorRenderMode } from '../../../types';
import resolveViewportVolumeId from '../../helpers/resolveViewportVolumeId';
import type { LoadedData } from '../ViewportArchitectureTypes';
import { getGenericViewportPlanarDisplaySet } from '../genericViewportDisplaySetAccess';
import type {
PlanarDataProvider,
PlanarDataLoadOptions,
PlanarPayload,
PlanarRegisteredDataSet,
} from './PlanarViewportTypes';
export class DefaultPlanarDataProvider implements PlanarDataProvider {
async load(
dataId: string,
options?: PlanarDataLoadOptions
): Promise<LoadedData<PlanarPayload>> {
const dataSet = this.getDataSet(dataId);
if (!dataSet) {
throw new Error(
`[PlanarViewport] No registered planar dataset for ${dataId}`
);
}
if (!options) {
throw new Error(
`[PlanarViewport] No load options were provided for ${dataId}`
);
}
if (!dataSet.imageIds.length) {
throw new Error('[PlanarViewport] Cannot load an empty planar dataset');
}
// A concrete, clamped index is always needed to load a single image below.
const clampedImageIdIndex = Math.min(
Math.max(0, dataSet.initialImageIdIndex ?? 0),
dataSet.imageIds.length - 1
);
// But preserve "no slice requested" (undefined) in the payload so the volume
// acquisition view can center instead of pinning to slice 0; an explicit
// index (including 0) is clamped and honored downstream.
const initialImageIdIndex =
dataSet.initialImageIdIndex === undefined
? undefined
: clampedImageIdIndex;
if (
options.renderMode === ActorRenderMode.VTK_VOLUME_SLICE ||
options.renderMode === ActorRenderMode.CPU_VOLUME
) {
const volumeId = resolveViewportVolumeId(options.volumeId);
const imageVolume = await createAndCacheVolume(volumeId, {
imageIds: dataSet.imageIds,
});
imageVolume.load();
const imageIds = imageVolume.imageIds
? 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}`
);
}
}
return {
id: dataId,
type: 'image',
imageIds,
initialImageIdIndex: volumeInitialImageIdIndex,
acquisitionOrientation: options.acquisitionOrientation,
imageData: dataSet.imageData,
imageVolume,
reference: dataSet.reference,
renderMode: options.renderMode,
useWorldCoordinateImageData: dataSet.useWorldCoordinateImageData,
volumeId,
};
}
const image =
dataSet.image &&
dataSet.image.imageId === dataSet.imageIds[clampedImageIdIndex]
? dataSet.image
: await loadAndCacheImage(dataSet.imageIds[clampedImageIdIndex]);
return {
id: dataId,
type: 'image',
imageIds: dataSet.imageIds,
image,
imageData: dataSet.imageData,
initialImageIdIndex,
acquisitionOrientation: options.acquisitionOrientation,
reference: dataSet.reference,
renderMode: options.renderMode,
useWorldCoordinateImageData: dataSet.useWorldCoordinateImageData,
volumeId: options.volumeId,
};
}
private getDataSet(dataId: string): PlanarRegisteredDataSet | undefined {
const dataSet = getGenericViewportPlanarDisplaySet(dataId);
if (!isPlanarRegisteredDataSet(dataSet)) {
return;
}
return dataSet;
}
}
function isPlanarRegisteredDataSet(
value: unknown
): value is PlanarRegisteredDataSet {
if (
!value ||
typeof value !== 'object' ||
!Array.isArray((value as PlanarRegisteredDataSet).imageIds) ||
(value as PlanarRegisteredDataSet).imageIds.length === 0
) {
return false;
}
const dataSet = value as PlanarRegisteredDataSet;
return (
(dataSet.initialImageIdIndex === undefined ||
typeof dataSet.initialImageIdIndex === 'number') &&
(dataSet.volumeId === undefined || typeof dataSet.volumeId === 'string')
);
}
|