@cornerstonejs/tools 5.18.4 → 5.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. package/dist/esm/eventListeners/segmentation/dynamicVolumeDimensionGroupChangedListener.d.ts +2 -0
  2. package/dist/esm/eventListeners/segmentation/dynamicVolumeDimensionGroupChangedListener.js +35 -0
  3. package/dist/esm/init.js +3 -0
  4. package/dist/esm/stateManagement/segmentation/SegmentationStateManager.js +8 -5
  5. package/dist/esm/stateManagement/segmentation/events/triggerSegmentationDataModified.d.ts +2 -1
  6. package/dist/esm/stateManagement/segmentation/events/triggerSegmentationDataModified.js +3 -1
  7. package/dist/esm/stateManagement/segmentation/helpers/computeStackLabelmapFromVolume.js +7 -2
  8. package/dist/esm/stateManagement/segmentation/labelmapModel/dynamicLabelmapVolume.d.ts +16 -0
  9. package/dist/esm/stateManagement/segmentation/labelmapModel/dynamicLabelmapVolume.js +128 -0
  10. package/dist/esm/stateManagement/segmentation/labelmapModel/index.d.ts +1 -1
  11. package/dist/esm/stateManagement/segmentation/labelmapModel/index.js +1 -1
  12. package/dist/esm/stateManagement/segmentation/labelmapModel/labelmapLayerStore.d.ts +2 -1
  13. package/dist/esm/stateManagement/segmentation/labelmapModel/labelmapLayerStore.js +28 -2
  14. package/dist/esm/synchronizers/callbacks/imageSliceSyncCallback.js +22 -4
  15. package/dist/esm/tools/segmentation/BrushTool.js +1 -1
  16. package/dist/esm/tools/segmentation/strategies/compositions/ensureImageVolume.js +25 -3
  17. package/dist/esm/tools/segmentation/strategies/compositions/ensureSegmentationVolume.js +16 -1
  18. package/dist/esm/tools/segmentation/strategies/utils/getStrategyData.js +11 -1
  19. package/dist/esm/types/EventTypes.d.ts +1 -0
  20. package/dist/esm/utilities/segmentation/createLabelmapMemo.js +10 -1
  21. package/dist/esm/utilities/segmentation/getOrCreateSegmentationVolume.js +7 -1
  22. package/dist/esm/utilities/segmentation/growCut/runGrowCut.js +5 -0
  23. package/dist/esm/version.d.ts +1 -1
  24. package/dist/esm/version.js +1 -1
  25. package/package.json +3 -3
@@ -0,0 +1,2 @@
1
+ declare const dynamicVolumeDimensionGroupChangedListener: (evt: CustomEvent) => void;
2
+ export default dynamicVolumeDimensionGroupChangedListener;
@@ -0,0 +1,35 @@
1
+ import { cache, utilities } from '@cornerstonejs/core';
2
+ import { getSegmentations } from '../../stateManagement/segmentation/getSegmentations.js';
3
+ import { getLabelmaps } from '../../stateManagement/segmentation/labelmapModel/labelmapLayerStore.js';
4
+ import { isDynamicLabelmapVolume, syncDynamicLabelmapVolume, } from '../../stateManagement/segmentation/labelmapModel/dynamicLabelmapVolume.js';
5
+ import { triggerSegmentationDataModified } from '../../stateManagement/segmentation/triggerSegmentationEvents.js';
6
+ const dynamicVolumeDimensionGroupChangedListener = function (evt) {
7
+ const imageVolume = cache.getVolume(evt.detail?.volumeId);
8
+ if (!imageVolume) {
9
+ return;
10
+ }
11
+ const { dimensionGroupNumber } = imageVolume;
12
+ getSegmentations().forEach((segmentation) => {
13
+ let changed = false;
14
+ let followsImageVolume = false;
15
+ getLabelmaps(segmentation).forEach((layer) => {
16
+ const labelmapVolumeId = layer.volumeId ?? layer.geometryVolumeId;
17
+ const labelmapVolume = labelmapVolumeId && cache.getVolume(labelmapVolumeId);
18
+ if (isDynamicLabelmapVolume(labelmapVolume)) {
19
+ changed =
20
+ syncDynamicLabelmapVolume(labelmapVolume, imageVolume) || changed;
21
+ return;
22
+ }
23
+ const firstImageId = layer.imageIds?.[0];
24
+ const referencedImageId = (firstImageId && cache.getImage(firstImageId)?.referencedImageId) ??
25
+ labelmapVolume?.referencedImageIds?.[0];
26
+ followsImageVolume ||=
27
+ !!referencedImageId &&
28
+ imageVolume.getImageURIIndex(utilities.imageIdToURI(referencedImageId)) > -1;
29
+ });
30
+ if (changed || followsImageVolume) {
31
+ triggerSegmentationDataModified(segmentation.segmentationId, undefined, undefined, { voxelsUnchanged: !changed, dimensionGroupNumber });
32
+ }
33
+ });
34
+ };
35
+ export default dynamicVolumeDimensionGroupChangedListener;
package/dist/esm/init.js CHANGED
@@ -11,6 +11,7 @@ import segmentationRepresentationModifiedListener from './eventListeners/segment
11
11
  import { setConfig } from './config.js';
12
12
  import segmentationRemovedListener from './eventListeners/segmentation/segmentationRemovedEventListener.js';
13
13
  import renderingPipelineChangedListener from './eventListeners/segmentation/renderingPipelineChangedListener.js';
14
+ import dynamicVolumeDimensionGroupChangedListener from './eventListeners/segmentation/dynamicVolumeDimensionGroupChangedListener.js';
14
15
  import { registerBuiltInSegmentationRepresentationDisplays } from './tools/displayTools/registerBuiltInSegmentationRepresentationDisplays.js';
15
16
  let csToolsInitialized = false;
16
17
  export function init(defaultConfiguration = {}) {
@@ -41,6 +42,7 @@ function _addCornerstoneEventListeners() {
41
42
  eventTarget.addEventListener(elementEnabledEvent, addEnabledElement);
42
43
  eventTarget.addEventListener(elementDisabledEvent, removeEnabledElement);
43
44
  eventTarget.addEventListener(Enums.Events.RENDERING_PIPELINE_CHANGED, renderingPipelineChangedListener);
45
+ eventTarget.addEventListener(Enums.Events.DYNAMIC_VOLUME_DIMENSION_GROUP_CHANGED, dynamicVolumeDimensionGroupChangedListener);
44
46
  annotationInterpolationEventDispatcher.enable();
45
47
  }
46
48
  function _removeCornerstoneEventListeners() {
@@ -49,6 +51,7 @@ function _removeCornerstoneEventListeners() {
49
51
  eventTarget.removeEventListener(elementEnabledEvent, addEnabledElement);
50
52
  eventTarget.removeEventListener(elementDisabledEvent, removeEnabledElement);
51
53
  eventTarget.removeEventListener(Enums.Events.RENDERING_PIPELINE_CHANGED, renderingPipelineChangedListener);
54
+ eventTarget.removeEventListener(Enums.Events.DYNAMIC_VOLUME_DIMENSION_GROUP_CHANGED, dynamicVolumeDimensionGroupChangedListener);
52
55
  annotationInterpolationEventDispatcher.disable();
53
56
  }
54
57
  function _addCornerstoneToolsEventListeners() {
@@ -6,7 +6,7 @@ import getViewportLabelmapRenderMode from './helpers/getViewportLabelmapRenderMo
6
6
  import { triggerSegmentationModified, triggerSegmentationRemoved, triggerSegmentationRepresentationModified, triggerSegmentationRepresentationRemoved, } from './triggerSegmentationEvents.js';
7
7
  import { segmentationStyle } from './SegmentationStyle.js';
8
8
  import { triggerSegmentationAdded } from './events/triggerSegmentationAdded.js';
9
- import { ensureLabelmapState, LabelmapImageReferenceResolver, syncLegacyLabelmapData, } from './labelmapModel/index.js';
9
+ import { ensureLabelmapState, getOrCreateDynamicLabelmapVolume, LabelmapImageReferenceResolver, syncLegacyLabelmapData, } from './labelmapModel/index.js';
10
10
  import { utilities as cornerstoneUtilities } from '@cornerstonejs/core';
11
11
  const cs3dLogger = cornerstoneUtilities.logger.toolsLog.getLogger('stateManagement.segmentation.SegmentationStateManager');
12
12
  const initialDefaultState = {
@@ -376,10 +376,13 @@ async function internalConvertStackToVolumeLabelmap({ segmentationId, options, }
376
376
  const segmentation = defaultSegmentationStateManager.getSegmentation(segmentationId);
377
377
  const data = segmentation.representationData
378
378
  .Labelmap;
379
- const { volumeId } = await internalComputeVolumeLabelmapFromStack({
380
- imageIds: data.imageIds,
381
- options,
382
- });
379
+ let volumeId = getOrCreateDynamicLabelmapVolume(segmentation)?.volumeId;
380
+ if (!volumeId) {
381
+ ({ volumeId } = await internalComputeVolumeLabelmapFromStack({
382
+ imageIds: data.imageIds,
383
+ options,
384
+ }));
385
+ }
383
386
  segmentation.representationData.Labelmap.volumeId = volumeId;
384
387
  }
385
388
  function getDefaultRenderingConfig(type) {
@@ -1,3 +1,4 @@
1
- export declare function triggerSegmentationDataModified(segmentationId: string, modifiedSlicesToUse?: number[], segmentIndex?: number, { voxelsUnchanged }?: {
1
+ export declare function triggerSegmentationDataModified(segmentationId: string, modifiedSlicesToUse?: number[], segmentIndex?: number, { voxelsUnchanged, dimensionGroupNumber, }?: {
2
2
  voxelsUnchanged?: boolean;
3
+ dimensionGroupNumber?: number;
3
4
  }): void;
@@ -1,12 +1,14 @@
1
1
  import { triggerEvent, eventTarget } from '@cornerstonejs/core';
2
2
  import { Events } from '../../../enums/index.js';
3
3
  import { setSegmentationDirty } from '../../../utilities/segmentation/utilities.js';
4
- export function triggerSegmentationDataModified(segmentationId, modifiedSlicesToUse, segmentIndex, { voxelsUnchanged } = {}) {
4
+ import { getDynamicLabelmapTimePointForSegmentation } from '../labelmapModel/dynamicLabelmapVolume.js';
5
+ export function triggerSegmentationDataModified(segmentationId, modifiedSlicesToUse, segmentIndex, { voxelsUnchanged, dimensionGroupNumber = getDynamicLabelmapTimePointForSegmentation(segmentationId), } = {}) {
5
6
  const eventDetail = {
6
7
  segmentationId,
7
8
  modifiedSlicesToUse,
8
9
  segmentIndex,
9
10
  ...(voxelsUnchanged ? { voxelsUnchanged } : {}),
11
+ ...(dimensionGroupNumber ? { dimensionGroupNumber } : {}),
10
12
  };
11
13
  setSegmentationDirty(segmentationId);
12
14
  triggerEvent(eventTarget, Events.SEGMENTATION_DATA_MODIFIED, eventDetail);
@@ -1,9 +1,14 @@
1
1
  import { cache } from '@cornerstonejs/core';
2
2
  import { getSegmentation } from '../getSegmentation.js';
3
3
  import { updateStackSegmentationState } from '../helpers/updateStackSegmentationState.js';
4
+ import { getDynamicLabelmapImageIds } from '../labelmapModel/dynamicLabelmapVolume.js';
5
+ function getStackImageIds(segmentationVolume) {
6
+ return (getDynamicLabelmapImageIds(segmentationVolume) ??
7
+ segmentationVolume.imageIds);
8
+ }
4
9
  export async function computeStackLabelmapFromVolume({ volumeId, }) {
5
10
  const segmentationVolume = cache.getVolume(volumeId);
6
- return { imageIds: segmentationVolume.imageIds };
11
+ return { imageIds: getStackImageIds(segmentationVolume) };
7
12
  }
8
13
  export function convertVolumeToStackLabelmap({ segmentationId, options, }) {
9
14
  const segmentation = getSegmentation(segmentationId);
@@ -16,7 +21,7 @@ export function convertVolumeToStackLabelmap({ segmentationId, options, }) {
16
21
  return updateStackSegmentationState({
17
22
  segmentationId,
18
23
  viewportId: options.viewportId,
19
- imageIds: segmentationVolume.imageIds,
24
+ imageIds: getStackImageIds(segmentationVolume),
20
25
  options,
21
26
  });
22
27
  }
@@ -0,0 +1,16 @@
1
+ import type { Types } from '@cornerstonejs/core';
2
+ import type { LabelmapLayer } from '../../../types/LabelmapTypes.js';
3
+ declare function createDynamicLabelmapVolume(volumeId: string, layer: LabelmapLayer): Types.IImageVolume | undefined;
4
+ declare function syncDynamicLabelmapVolume(labelmapVolume: Types.IImageVolume, imageVolume: Types.IImageVolume): boolean;
5
+ declare function syncDynamicLabelmapVolumeToImageId(labelmapVolume: Types.IImageVolume, referencedImageId: string): boolean;
6
+ declare function isDynamicLabelmapImageCovered(labelmapVolume: Types.IImageVolume | undefined, referencedImageId: string | undefined): boolean;
7
+ declare function getDynamicLabelmapTimePoint(volume?: Types.IImageVolume): {
8
+ dimensionGroupNumber: number;
9
+ voxelManager: Types.IVoxelManager<number>;
10
+ } | undefined;
11
+ declare function getVoxelManagerTimePoint(voxelManager: unknown): number | undefined;
12
+ declare function setDynamicLabelmapVolumeId(segmentationId: string, volumeId: string): void;
13
+ declare function getDynamicLabelmapTimePointForSegmentation(segmentationId: string): number | undefined;
14
+ declare function getDynamicLabelmapImageIds(volume?: Types.IImageVolume): string[] | undefined;
15
+ declare function isDynamicLabelmapVolume(volume?: Types.IImageVolume): boolean;
16
+ export { createDynamicLabelmapVolume, getDynamicLabelmapImageIds, getDynamicLabelmapTimePoint, getDynamicLabelmapTimePointForSegmentation, getVoxelManagerTimePoint, isDynamicLabelmapImageCovered, isDynamicLabelmapVolume, setDynamicLabelmapVolumeId, syncDynamicLabelmapVolume, syncDynamicLabelmapVolumeToImageId, };
@@ -0,0 +1,128 @@
1
+ import { cache, ImageVolume, utilities } from '@cornerstonejs/core';
2
+ import { forEachLabelmapImageReference } from './labelmapImageIdMapping.js';
3
+ const { VoxelManager, deepClone, generateVolumePropsFromImageIds, sortImageIdsAndGetSpacing, splitImageIdsBy4DTags, } = utilities;
4
+ const dynamicLabelmaps = new WeakMap();
5
+ const timePointByVoxelManager = new WeakMap();
6
+ const volumeIdBySegmentationId = new Map();
7
+ function createDynamicLabelmapVolume(volumeId, layer) {
8
+ const imageIdByReferencedImageId = new Map();
9
+ forEachLabelmapImageReference(layer, (referencedImageId, imageId) => {
10
+ imageIdByReferencedImageId.set(referencedImageId, imageId);
11
+ });
12
+ const { imageIdGroups: unsortedReferencedGroups } = splitImageIdsBy4DTags([
13
+ ...imageIdByReferencedImageId.keys(),
14
+ ]);
15
+ if (unsortedReferencedGroups.length < 2) {
16
+ return;
17
+ }
18
+ const groupSize = unsortedReferencedGroups[0].length;
19
+ if (unsortedReferencedGroups.some((group) => group.length !== groupSize ||
20
+ group.some((id) => !imageIdByReferencedImageId.has(id)))) {
21
+ utilities.logger.toolsLog
22
+ .getLogger('dynamicLabelmapVolume')
23
+ .warn(`Labelmap ${layer.labelmapId}: the 4D split does not match the labelmap images; using a static volume`);
24
+ return;
25
+ }
26
+ const referencedImageIdByImageId = new Map();
27
+ imageIdByReferencedImageId.forEach((imageId, referencedImageId) => referencedImageIdByImageId.set(imageId, referencedImageId));
28
+ const firstGroup = unsortedReferencedGroups[0].map((referencedImageId) => imageIdByReferencedImageId.get(referencedImageId));
29
+ const volumeProps = generateVolumePropsFromImageIds(firstGroup, volumeId);
30
+ const scanAxisNormal = volumeProps.direction.slice(6, 9);
31
+ const imageIdGroups = unsortedReferencedGroups.map((referencedImageIds) => sortImageIdsAndGetSpacing(referencedImageIds.map((referencedImageId) => imageIdByReferencedImageId.get(referencedImageId)), scanAxisNormal).sortedImageIds);
32
+ const referencedImageIdGroups = imageIdGroups.map((imageIds) => imageIds.map((imageId) => referencedImageIdByImageId.get(imageId)));
33
+ const groupIndexByReferencedImageId = new Map();
34
+ referencedImageIdGroups.forEach((referencedImageIds, groupIndex) => referencedImageIds.forEach((referencedImageId) => groupIndexByReferencedImageId.set(referencedImageId, groupIndex)));
35
+ const voxelManager = VoxelManager.createScalarDynamicVolumeVoxelManager({
36
+ imageIdGroups,
37
+ dimensions: volumeProps.dimensions,
38
+ numberOfComponents: 1,
39
+ id: volumeId,
40
+ });
41
+ const volume = new ImageVolume({
42
+ volumeId,
43
+ dataType: volumeProps.dataType,
44
+ metadata: deepClone(volumeProps.metadata),
45
+ dimensions: volumeProps.dimensions,
46
+ spacing: volumeProps.spacing,
47
+ origin: volumeProps.origin,
48
+ direction: volumeProps.direction,
49
+ imageIds: imageIdGroups[0],
50
+ referencedImageIds: referencedImageIdGroups[0],
51
+ voxelManager,
52
+ });
53
+ const dynamicLabelmap = {
54
+ imageIdGroups,
55
+ referencedImageIdGroups,
56
+ groupIndexByReferencedImageId,
57
+ groupIndex: 0,
58
+ setGroupIndex: (groupIndex) => {
59
+ voxelManager.setDimensionGroupNumber(groupIndex + 1);
60
+ volume.imageIds = imageIdGroups[groupIndex];
61
+ volume.referencedImageIds = referencedImageIdGroups[groupIndex];
62
+ dynamicLabelmap.groupIndex = groupIndex;
63
+ },
64
+ groupVoxelManagers: [],
65
+ };
66
+ dynamicLabelmaps.set(volume, dynamicLabelmap);
67
+ cache.putVolumeSync(volumeId, volume);
68
+ for (const imageVolume of cache.getVolumes()) {
69
+ if (syncDynamicLabelmapVolume(volume, imageVolume)) {
70
+ break;
71
+ }
72
+ }
73
+ return volume;
74
+ }
75
+ function syncDynamicLabelmapVolume(labelmapVolume, imageVolume) {
76
+ const currentImageIds = imageVolume.getCurrentDimensionGroupImageIds?.();
77
+ return Boolean(currentImageIds?.length &&
78
+ syncDynamicLabelmapVolumeToImageId(labelmapVolume, currentImageIds[0]));
79
+ }
80
+ function syncDynamicLabelmapVolumeToImageId(labelmapVolume, referencedImageId) {
81
+ const dynamicLabelmap = dynamicLabelmaps.get(labelmapVolume);
82
+ const groupIndex = dynamicLabelmap?.groupIndexByReferencedImageId.get(referencedImageId);
83
+ if (groupIndex === undefined || groupIndex === dynamicLabelmap.groupIndex) {
84
+ return false;
85
+ }
86
+ dynamicLabelmap.setGroupIndex(groupIndex);
87
+ return true;
88
+ }
89
+ function isDynamicLabelmapImageCovered(labelmapVolume, referencedImageId) {
90
+ const dynamicLabelmap = labelmapVolume && dynamicLabelmaps.get(labelmapVolume);
91
+ return (!dynamicLabelmap ||
92
+ dynamicLabelmap.groupIndexByReferencedImageId.has(referencedImageId));
93
+ }
94
+ function getDynamicLabelmapTimePoint(volume) {
95
+ const dynamicLabelmap = volume && dynamicLabelmaps.get(volume);
96
+ if (!dynamicLabelmap) {
97
+ return;
98
+ }
99
+ const { groupIndex, groupVoxelManagers, imageIdGroups } = dynamicLabelmap;
100
+ const dimensionGroupNumber = groupIndex + 1;
101
+ groupVoxelManagers[groupIndex] ??= VoxelManager.createImageVolumeVoxelManager({
102
+ dimensions: volume.dimensions,
103
+ imageIds: imageIdGroups[groupIndex],
104
+ numberOfComponents: 1,
105
+ id: `${volume.volumeId}-${dimensionGroupNumber}`,
106
+ });
107
+ timePointByVoxelManager.set(groupVoxelManagers[groupIndex], dimensionGroupNumber);
108
+ return { dimensionGroupNumber, voxelManager: groupVoxelManagers[groupIndex] };
109
+ }
110
+ function getVoxelManagerTimePoint(voxelManager) {
111
+ return timePointByVoxelManager.get(voxelManager);
112
+ }
113
+ function setDynamicLabelmapVolumeId(segmentationId, volumeId) {
114
+ volumeIdBySegmentationId.set(segmentationId, volumeId);
115
+ }
116
+ function getDynamicLabelmapTimePointForSegmentation(segmentationId) {
117
+ const volumeId = volumeIdBySegmentationId.get(segmentationId);
118
+ const volume = volumeId && cache.getVolume(volumeId);
119
+ const dynamicLabelmap = volume && dynamicLabelmaps.get(volume);
120
+ return dynamicLabelmap && dynamicLabelmap.groupIndex + 1;
121
+ }
122
+ function getDynamicLabelmapImageIds(volume) {
123
+ return volume && dynamicLabelmaps.get(volume)?.imageIdGroups.flat();
124
+ }
125
+ function isDynamicLabelmapVolume(volume) {
126
+ return Boolean(volume && dynamicLabelmaps.has(volume));
127
+ }
128
+ export { createDynamicLabelmapVolume, getDynamicLabelmapImageIds, getDynamicLabelmapTimePoint, getDynamicLabelmapTimePointForSegmentation, getVoxelManagerTimePoint, isDynamicLabelmapImageCovered, isDynamicLabelmapVolume, setDynamicLabelmapVolumeId, syncDynamicLabelmapVolume, syncDynamicLabelmapVolumeToImageId, };
@@ -1,6 +1,6 @@
1
1
  export { SOURCE_REPRESENTATION_NAME, ensureLabelmapState, getPrimaryLabelmapId, getSegmentOrder, } from './normalizeLabelmapSegmentationData.js';
2
2
  export type { LabelmapSegmentationWithState } from './normalizeLabelmapSegmentationData.js';
3
- export { getConstructorNameForLabelmap, getLabelmap, getLabelmapDataById, getLabelmapForImageId, getLabelmapForVolumeId, getLabelmapIds, getLabelmaps, getOrCreateLabelmapVolume, getScalarArrayLengthFromLabelmap, registerLabelmap, removeLabelmap, } from './labelmapLayerStore.js';
3
+ export { getConstructorNameForLabelmap, getLabelmap, getLabelmapDataById, getLabelmapForImageId, getLabelmapForVolumeId, getLabelmapIds, getLabelmaps, getOrCreateDynamicLabelmapVolume, getOrCreateLabelmapVolume, getScalarArrayLengthFromLabelmap, registerLabelmap, removeLabelmap, } from './labelmapLayerStore.js';
4
4
  export { getReferencedImageIdToCurrentImageIdMap, syncLegacyLabelmapData, } from './labelmapLegacyAdapter.js';
5
5
  export { getLabelmapForSegment, getLabelToSegmentIndex, getLabelValueForSegment, getSegmentBinding, getSegmentIndexForLabelValue, getSegmentsOnLabelmap, removeSegmentBinding, resolveLabelmapForSegment, setSegmentBinding, } from './labelmapSegmentBindings.js';
6
6
  export { createPrivateLabelmap, moveSegmentToPrivateLabelmap, } from './privateLabelmap.js';
@@ -1,5 +1,5 @@
1
1
  export { SOURCE_REPRESENTATION_NAME, ensureLabelmapState, getPrimaryLabelmapId, getSegmentOrder, } from './normalizeLabelmapSegmentationData.js';
2
- export { getConstructorNameForLabelmap, getLabelmap, getLabelmapDataById, getLabelmapForImageId, getLabelmapForVolumeId, getLabelmapIds, getLabelmaps, getOrCreateLabelmapVolume, getScalarArrayLengthFromLabelmap, registerLabelmap, removeLabelmap, } from './labelmapLayerStore.js';
2
+ export { getConstructorNameForLabelmap, getLabelmap, getLabelmapDataById, getLabelmapForImageId, getLabelmapForVolumeId, getLabelmapIds, getLabelmaps, getOrCreateDynamicLabelmapVolume, getOrCreateLabelmapVolume, getScalarArrayLengthFromLabelmap, registerLabelmap, removeLabelmap, } from './labelmapLayerStore.js';
3
3
  export { getReferencedImageIdToCurrentImageIdMap, syncLegacyLabelmapData, } from './labelmapLegacyAdapter.js';
4
4
  export { getLabelmapForSegment, getLabelToSegmentIndex, getLabelValueForSegment, getSegmentBinding, getSegmentIndexForLabelValue, getSegmentsOnLabelmap, removeSegmentBinding, resolveLabelmapForSegment, setSegmentBinding, } from './labelmapSegmentBindings.js';
5
5
  export { createPrivateLabelmap, moveSegmentToPrivateLabelmap, } from './privateLabelmap.js';
@@ -7,10 +7,11 @@ declare function getLabelmaps(segmentation: Segmentation): LabelmapLayer[];
7
7
  declare function registerLabelmap(segmentation: Segmentation, layer: LabelmapLayer): void;
8
8
  declare function removeLabelmap(segmentation: Segmentation, labelmapId: string): void;
9
9
  declare function getOrCreateLabelmapVolume(layer: LabelmapLayer): Types.IImageVolume | undefined;
10
+ declare function getOrCreateDynamicLabelmapVolume(segmentation: Segmentation): Types.IImageVolume | undefined;
10
11
  declare function getLabelmapIds(segmentation: Segmentation): string[];
11
12
  declare function getLabelmapDataById(segmentation: Segmentation, labelmapId: string): LabelmapSegmentationData | undefined;
12
13
  declare function getScalarArrayLengthFromLabelmap(layer: LabelmapLayer): number;
13
14
  declare function getConstructorNameForLabelmap(layer: LabelmapLayer): string;
14
15
  declare function getLabelmapForImageId(segmentation: Segmentation, imageId: string): LabelmapLayer | undefined;
15
16
  declare function getLabelmapForVolumeId(segmentation: Segmentation, volumeId: string): LabelmapLayer | undefined;
16
- export { getConstructorNameForLabelmap, getLabelmap, getLabelmapDataById, getLabelmapForImageId, getLabelmapForVolumeId, getLabelmapIds, getLabelmaps, getOrCreateLabelmapVolume, getScalarArrayLengthFromLabelmap, registerLabelmap, removeLabelmap, tagLabelmapVolume, };
17
+ export { getConstructorNameForLabelmap, getLabelmap, getLabelmapDataById, getLabelmapForImageId, getLabelmapForVolumeId, getLabelmapIds, getLabelmaps, getOrCreateDynamicLabelmapVolume, getOrCreateLabelmapVolume, getScalarArrayLengthFromLabelmap, registerLabelmap, removeLabelmap, tagLabelmapVolume, };
@@ -1,6 +1,7 @@
1
1
  import { cache, imageLoader, volumeLoader, Enums } from '@cornerstonejs/core';
2
2
  import { ensureLabelmapState } from './normalizeLabelmapSegmentationData.js';
3
3
  import { forEachLabelmapImageReference, hasMultipleLabelmapImagesPerReferencedImageId, } from './labelmapImageIdMapping.js';
4
+ import { createDynamicLabelmapVolume, isDynamicLabelmapVolume, setDynamicLabelmapVolumeId, } from './dynamicLabelmapVolume.js';
4
5
  function tagLabelmapVolume(volume) {
5
6
  if (volume) {
6
7
  volume.reductionStatistic = Enums.VoxelStatistics.ForegroundMajority;
@@ -59,7 +60,32 @@ function getOrCreateLabelmapVolume(layer) {
59
60
  if (cachedVolume) {
60
61
  return tagLabelmapVolume(cachedVolume);
61
62
  }
62
- return tagLabelmapVolume(volumeLoader.createAndCacheVolumeFromImagesSync(volumeId, imageIds));
63
+ return tagLabelmapVolume(createDynamicLabelmapVolume(volumeId, layer) ??
64
+ volumeLoader.createAndCacheVolumeFromImagesSync(volumeId, imageIds));
65
+ }
66
+ const staticLabelmapLayers = new WeakSet();
67
+ function getOrCreateDynamicLabelmapVolume(segmentation) {
68
+ const layer = getLabelmap(segmentation, ensureLabelmapState(segmentation)?.primaryLabelmapId);
69
+ if (!layer || staticLabelmapLayers.has(layer)) {
70
+ return;
71
+ }
72
+ const volumeId = layer.volumeId ?? layer.geometryVolumeId ?? `${layer.labelmapId}-geometry`;
73
+ const cachedVolume = cache.getVolume(volumeId);
74
+ const volume = cachedVolume
75
+ ? isDynamicLabelmapVolume(cachedVolume)
76
+ ? cachedVolume
77
+ : undefined
78
+ : layer.imageIds?.length &&
79
+ !layer.volumeId &&
80
+ !hasDuplicateReferencedImageIds(layer) &&
81
+ createDynamicLabelmapVolume(volumeId, layer);
82
+ if (!volume) {
83
+ staticLabelmapLayers.add(layer);
84
+ return;
85
+ }
86
+ layer.geometryVolumeId ??= layer.volumeId ? undefined : volumeId;
87
+ setDynamicLabelmapVolumeId(segmentation.segmentationId, volumeId);
88
+ return tagLabelmapVolume(volume);
63
89
  }
64
90
  function getOrCreateMergedStackLabelmapVolume(layer) {
65
91
  if (layer.volumeId || !hasDuplicateReferencedImageIds(layer)) {
@@ -163,4 +189,4 @@ function getLabelmapForImageId(segmentation, imageId) {
163
189
  function getLabelmapForVolumeId(segmentation, volumeId) {
164
190
  return getLabelmaps(segmentation).find((layer) => layer.volumeId === volumeId || layer.geometryVolumeId === volumeId);
165
191
  }
166
- export { getConstructorNameForLabelmap, getLabelmap, getLabelmapDataById, getLabelmapForImageId, getLabelmapForVolumeId, getLabelmapIds, getLabelmaps, getOrCreateLabelmapVolume, getScalarArrayLengthFromLabelmap, registerLabelmap, removeLabelmap, tagLabelmapVolume, };
192
+ export { getConstructorNameForLabelmap, getLabelmap, getLabelmapDataById, getLabelmapForImageId, getLabelmapForVolumeId, getLabelmapIds, getLabelmaps, getOrCreateDynamicLabelmapVolume, getOrCreateLabelmapVolume, getScalarArrayLengthFromLabelmap, registerLabelmap, removeLabelmap, tagLabelmapVolume, };
@@ -2,6 +2,17 @@ import { vec3, mat4 } from 'gl-matrix';
2
2
  import { ActorRenderMode, getRenderingEngine, metaData, utilities, VolumeViewport, } from '@cornerstonejs/core';
3
3
  import areViewportsCoplanar from './areViewportsCoplanar.js';
4
4
  const getSpatialRegistration = (targetId, sourceId) => utilities.spatialRegistrationMetadataProvider.get('spatialRegistrationModule', targetId, sourceId);
5
+ const calculatedRegistrationFrames = new Map();
6
+ function removeStaleRegistration(sourceId, targetId, sourceFOR, targetFOR) {
7
+ const frames = calculatedRegistrationFrames.get(`${sourceId}_${targetId}`);
8
+ if (frames === undefined || frames === `${sourceFOR}_${targetFOR}`) {
9
+ return;
10
+ }
11
+ utilities.spatialRegistrationMetadataProvider.add([sourceId, targetId], undefined);
12
+ utilities.spatialRegistrationMetadataProvider.add([targetId, sourceId], undefined);
13
+ calculatedRegistrationFrames.delete(`${sourceId}_${targetId}`);
14
+ calculatedRegistrationFrames.delete(`${targetId}_${sourceId}`);
15
+ }
5
16
  function isVolumeSliceViewport(viewport) {
6
17
  if (viewport instanceof VolumeViewport) {
7
18
  return true;
@@ -32,17 +43,24 @@ export default async function imageSliceSyncCallback(synchronizerInstance, sourc
32
43
  if (!areViewportsCoplanar(sViewport, tViewport)) {
33
44
  return;
34
45
  }
35
- let registrationMatrixMat4 = getSpatialRegistration(targetViewport.viewportId, sourceViewport.viewportId);
46
+ const sourceId = sourceViewport.viewportId;
47
+ const targetId = targetViewport.viewportId;
48
+ const frameOfReferenceUID1 = sViewport.getFrameOfReferenceUID();
49
+ const frameOfReferenceUID2 = tViewport.getFrameOfReferenceUID();
50
+ removeStaleRegistration(sourceId, targetId, frameOfReferenceUID1, frameOfReferenceUID2);
51
+ let registrationMatrixMat4 = getSpatialRegistration(targetId, sourceId);
36
52
  if (!registrationMatrixMat4) {
37
- const frameOfReferenceUID1 = sViewport.getFrameOfReferenceUID();
38
- const frameOfReferenceUID2 = tViewport.getFrameOfReferenceUID();
39
53
  if (frameOfReferenceUID1 === frameOfReferenceUID2 &&
40
54
  options?.useInitialPosition !== false) {
41
55
  registrationMatrixMat4 = mat4.identity(mat4.create());
42
56
  }
43
57
  else {
44
58
  utilities.calculateViewportsSpatialRegistration(sViewport, tViewport);
45
- registrationMatrixMat4 = getSpatialRegistration(targetViewport.viewportId, sourceViewport.viewportId);
59
+ registrationMatrixMat4 = getSpatialRegistration(targetId, sourceId);
60
+ if (registrationMatrixMat4) {
61
+ calculatedRegistrationFrames.set(`${sourceId}_${targetId}`, `${frameOfReferenceUID1}_${frameOfReferenceUID2}`);
62
+ calculatedRegistrationFrames.set(`${targetId}_${sourceId}`, `${frameOfReferenceUID2}_${frameOfReferenceUID1}`);
63
+ }
46
64
  }
47
65
  if (!registrationMatrixMat4) {
48
66
  return;
@@ -207,7 +207,7 @@ class BrushTool extends LabelmapBaseTool {
207
207
  viewport,
208
208
  strategy: activeStrategy,
209
209
  });
210
- if (!operationData) {
210
+ if (!operationData || !strategyData) {
211
211
  return;
212
212
  }
213
213
  const memo = this.createMemo(operationData.segmentationId, strategyData.segmentationVoxelManager);
@@ -2,11 +2,32 @@ import { cache, utilities as csUtils } from '@cornerstonejs/core';
2
2
  import StrategyCallbacks from '../../../../enums/StrategyCallbacks.js';
3
3
  import { getSegmentation } from '../../../../stateManagement/segmentation/getSegmentation.js';
4
4
  import getOrCreateImageVolume from '../../../../utilities/segmentation/getOrCreateImageVolume.js';
5
+ import { getOrCreateDynamicLabelmapVolume } from '../../../../stateManagement/segmentation/labelmapModel/index.js';
6
+ import { syncDynamicLabelmapVolumeToImageId } from '../../../../stateManagement/segmentation/labelmapModel/dynamicLabelmapVolume.js';
7
+ function getDynamicImageVolume(referencedImageIds, isDynamicLabelmap) {
8
+ const volume = cache.getVolumeContainingImageId(referencedImageIds[0])
9
+ ?.volume;
10
+ if (!volume?.isDynamicVolume?.()) {
11
+ return;
12
+ }
13
+ if (!isDynamicLabelmap ||
14
+ volume.getCurrentDimensionGroupImageIds().includes(referencedImageIds[0])) {
15
+ return volume;
16
+ }
17
+ }
5
18
  export default {
6
19
  [StrategyCallbacks.EnsureImageVolumeFor3DManipulation]: (data) => {
7
20
  const { operationData, viewport } = data;
21
+ const segmentation = getSegmentation(operationData.segmentationId);
22
+ const dynamicLabelmapVolume = getOrCreateDynamicLabelmapVolume(segmentation);
8
23
  let referencedImageIds;
9
- if (viewport) {
24
+ if (dynamicLabelmapVolume) {
25
+ if (viewport) {
26
+ syncDynamicLabelmapVolumeToImageId(dynamicLabelmapVolume, viewport.getCurrentImageId());
27
+ }
28
+ referencedImageIds = dynamicLabelmapVolume.referencedImageIds;
29
+ }
30
+ else if (viewport) {
10
31
  referencedImageIds = viewport.getImageIds();
11
32
  const isValidVolumeForSphere = csUtils.isValidVolume(referencedImageIds);
12
33
  if (!isValidVolumeForSphere) {
@@ -14,7 +35,6 @@ export default {
14
35
  }
15
36
  }
16
37
  else {
17
- const segmentation = getSegmentation(operationData.segmentationId);
18
38
  const imageIds = segmentation.representationData
19
39
  .Labelmap.imageIds;
20
40
  referencedImageIds = imageIds.map((imageId) => {
@@ -22,7 +42,9 @@ export default {
22
42
  return image.referencedImageId;
23
43
  });
24
44
  }
25
- const imageVolume = getOrCreateImageVolume(referencedImageIds);
45
+ const imageVolume = (viewport && !dynamicLabelmapVolume
46
+ ? undefined
47
+ : getDynamicImageVolume(referencedImageIds, Boolean(dynamicLabelmapVolume))) ?? getOrCreateImageVolume(referencedImageIds);
26
48
  if (!imageVolume) {
27
49
  throw new Error('Failed to create or get image volume');
28
50
  }
@@ -1,10 +1,26 @@
1
1
  import { utilities, cache } from '@cornerstonejs/core';
2
2
  import StrategyCallbacks from '../../../../enums/StrategyCallbacks.js';
3
3
  import getOrCreateSegmentationVolume from '../../../../utilities/segmentation/getOrCreateSegmentationVolume.js';
4
+ import { getDynamicLabelmapTimePoint, isDynamicLabelmapImageCovered, isDynamicLabelmapVolume, syncDynamicLabelmapVolumeToImageId, } from '../../../../stateManagement/segmentation/labelmapModel/dynamicLabelmapVolume.js';
4
5
  export default {
5
6
  [StrategyCallbacks.EnsureSegmentationVolumeFor3DManipulation]: (data) => {
6
7
  const { operationData, viewport } = data;
7
8
  const { segmentationId, imageIds: segImageIds } = operationData;
9
+ const segVolume = getOrCreateSegmentationVolume(segmentationId);
10
+ if (isDynamicLabelmapVolume(segVolume)) {
11
+ if (viewport) {
12
+ const imageId = viewport.getCurrentImageId();
13
+ if (!isDynamicLabelmapImageCovered(segVolume, imageId)) {
14
+ operationData.segmentationVoxelManager = undefined;
15
+ return;
16
+ }
17
+ syncDynamicLabelmapVolumeToImageId(segVolume, imageId);
18
+ }
19
+ operationData.segmentationVoxelManager =
20
+ getDynamicLabelmapTimePoint(segVolume).voxelManager;
21
+ operationData.segmentationImageData = segVolume.imageData;
22
+ return;
23
+ }
8
24
  const referencedImageIds = viewport
9
25
  ? viewport.getImageIds()
10
26
  : segImageIds.map((imageId) => cache.getImage(imageId).referencedImageId);
@@ -12,7 +28,6 @@ export default {
12
28
  if (!isValidVolumeForSphere) {
13
29
  throw new Error('Volume is not reconstructable for sphere manipulation');
14
30
  }
15
- const segVolume = getOrCreateSegmentationVolume(segmentationId);
16
31
  if (!segVolume) {
17
32
  return;
18
33
  }
@@ -1,6 +1,7 @@
1
1
  import { cache, Enums, eventTarget } from '@cornerstonejs/core';
2
2
  import { getCurrentLabelmapImageIdForViewport } from '../../../../stateManagement/segmentation/segmentationState.js';
3
3
  import { getLabelmapActorEntry } from '../../../../stateManagement/segmentation/helpers/index.js';
4
+ import { getDynamicLabelmapTimePoint, isDynamicLabelmapImageCovered, } from '../../../../stateManagement/segmentation/labelmapModel/dynamicLabelmapVolume.js';
4
5
  import { getReferenceVolumeForSegmentationVolume } from '../../../../utilities/segmentation/getReferenceVolumeForSegmentationVolume.js';
5
6
  function getStrategyDataForVolumeViewport({ operationData, }) {
6
7
  const { volumeId } = operationData;
@@ -20,8 +21,14 @@ function getStrategyDataForVolumeViewport({ operationData, }) {
20
21
  if (!segmentationVolume || !imageVolume) {
21
22
  return null;
22
23
  }
24
+ const currentImageIds = imageVolume.getCurrentDimensionGroupImageIds?.();
25
+ if (currentImageIds?.length &&
26
+ !isDynamicLabelmapImageCovered(segmentationVolume, currentImageIds[0])) {
27
+ return null;
28
+ }
23
29
  const { imageData: segmentationImageData } = segmentationVolume;
24
- const { voxelManager: segmentationVoxelManager } = segmentationVolume;
30
+ const segmentationVoxelManager = getDynamicLabelmapTimePoint(segmentationVolume)?.voxelManager ??
31
+ segmentationVolume.voxelManager;
25
32
  const { voxelManager: imageVoxelManager, imageData } = imageVolume;
26
33
  return {
27
34
  segmentationImageData,
@@ -46,6 +53,9 @@ function getStrategyDataForStackViewport({ operationData, viewport, strategy, })
46
53
  viewport,
47
54
  });
48
55
  segmentationVoxelManager = operationData.segmentationVoxelManager;
56
+ if (!segmentationVoxelManager) {
57
+ return null;
58
+ }
49
59
  segmentationImageData = operationData.segmentationImageData;
50
60
  getSegmentationScalarData = () => null;
51
61
  }
@@ -110,6 +110,7 @@ type SegmentationDataModifiedEventDetail = {
110
110
  modifiedSlicesToUse?: number[];
111
111
  segmentIndex?: number;
112
112
  voxelsUnchanged?: boolean;
113
+ dimensionGroupNumber?: number;
113
114
  };
114
115
  type SegmentationRenderedEventDetail = {
115
116
  viewportId: string;
@@ -1,5 +1,6 @@
1
1
  import { utilities } from '@cornerstonejs/core';
2
2
  import { triggerSegmentationDataModified } from '../../stateManagement/segmentation/triggerSegmentationEvents.js';
3
+ import { getVoxelManagerTimePoint } from '../../stateManagement/segmentation/labelmapModel/dynamicLabelmapVolume.js';
3
4
  const { VoxelManager, RLEVoxelMap } = utilities;
4
5
  export function createLabelmapMemo(segmentationId, segmentationVoxelManager) {
5
6
  return createRleMemo(segmentationId, segmentationVoxelManager);
@@ -24,7 +25,15 @@ export function restoreMemo(isUndo) {
24
25
  segmentationVoxelManager.setAtIJKPoint(pointIJK, value);
25
26
  });
26
27
  const slices = useVoxelManager.getArrayOfModifiedSlices();
27
- triggerSegmentationDataModified(this.segmentationId, slices);
28
+ const dimensionGroupNumber = getVoxelManagerTimePoint(segmentationVoxelManager);
29
+ if (dimensionGroupNumber) {
30
+ triggerSegmentationDataModified(this.segmentationId, slices, undefined, {
31
+ dimensionGroupNumber,
32
+ });
33
+ }
34
+ else {
35
+ triggerSegmentationDataModified(this.segmentationId, slices);
36
+ }
28
37
  };
29
38
  if (undo) {
30
39
  for (let i = postSteps.length - 1; i >= 0; i--) {
@@ -1,10 +1,16 @@
1
1
  import { cache, volumeLoader, utilities, ImageVolume, } from '@cornerstonejs/core';
2
2
  import { getSegmentation } from '../../stateManagement/segmentation/getSegmentation.js';
3
+ import { getOrCreateDynamicLabelmapVolume } from '../../stateManagement/segmentation/labelmapModel/index.js';
3
4
  function getOrCreateSegmentationVolume(segmentationId) {
4
- const { representationData } = getSegmentation(segmentationId);
5
+ const segmentation = getSegmentation(segmentationId);
6
+ const { representationData } = segmentation;
5
7
  if (!representationData.Labelmap) {
6
8
  return;
7
9
  }
10
+ const dynamicVolume = getOrCreateDynamicLabelmapVolume(segmentation);
11
+ if (dynamicVolume) {
12
+ return dynamicVolume;
13
+ }
8
14
  let { volumeId } = representationData.Labelmap;
9
15
  let segVolume;
10
16
  if (volumeId) {
@@ -28,6 +28,8 @@ async function runGrowCut(referenceVolumeId, labelmapVolumeId, options = DEFAULT
28
28
  let numIterations = Math.floor(Math.sqrt(rows ** 2 + columns ** 2 + numSlices ** 2) / 2);
29
29
  numIterations = Math.min(numIterations, 500);
30
30
  const labelmapData = labelmap.voxelManager.getCompleteScalarDataArray();
31
+ const getTimePoint = () => labelmap.voxelManager.getCurrentDimensionGroupNumber?.();
32
+ const startTimePoint = getTimePoint();
31
33
  let volumePixelData = volume.voxelManager.getCompleteScalarDataArray();
32
34
  if (!(volumePixelData instanceof Float32Array)) {
33
35
  volumePixelData = new Float32Array(volumePixelData);
@@ -307,6 +309,9 @@ async function runGrowCut(referenceVolumeId, labelmapVolumeId, options = DEFAULT
307
309
  const maxX = boundsResult[3];
308
310
  const maxY = boundsResult[4];
309
311
  const maxZ = boundsResult[5];
312
+ if (getTimePoint() !== startTimePoint) {
313
+ return;
314
+ }
310
315
  labelmap.voxelManager.setCompleteScalarDataArray(labelmapData);
311
316
  labelmap.voxelManager.clearBounds();
312
317
  labelmap.voxelManager.setBounds([
@@ -1 +1 @@
1
- export declare const version = "5.18.4";
1
+ export declare const version = "5.19.1";
@@ -1 +1 @@
1
- export const version = '5.18.4';
1
+ export const version = '5.19.1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cornerstonejs/tools",
3
- "version": "5.18.4",
3
+ "version": "5.19.1",
4
4
  "description": "Cornerstone3D Tools",
5
5
  "types": "./dist/esm/index.d.ts",
6
6
  "module": "./dist/esm/index.js",
@@ -126,11 +126,11 @@
126
126
  "lodash.get": "4.4.2"
127
127
  },
128
128
  "devDependencies": {
129
- "@cornerstonejs/core": "5.18.4",
129
+ "@cornerstonejs/core": "5.19.1",
130
130
  "canvas": "3.2.0"
131
131
  },
132
132
  "peerDependencies": {
133
- "@cornerstonejs/core": "5.18.4",
133
+ "@cornerstonejs/core": "5.19.1",
134
134
  "@kitware/vtk.js": "36.4.1",
135
135
  "@types/d3-array": "3.2.1",
136
136
  "@types/d3-interpolate": "3.0.4",