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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | 430x 430x 234x 234x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x | import { eventTarget, type Types } from '@cornerstonejs/core';
import { Events, SegmentationRepresentations } from '../../enums';
import { triggerSegmentationModified } from './triggerSegmentationEvents';
import debounce from '../../utilities/debounce';
import surfaceDisplay from '../../tools/displayTools/Surface/surfaceDisplay';
import contourDisplay from '../../tools/displayTools/Contour/contourDisplay';
import labelmapDisplay from '../../tools/displayTools/Labelmap/labelmapDisplay';
import { getSegmentation } from './getSegmentation';
const renderers = {
[SegmentationRepresentations.Labelmap]: labelmapDisplay,
[SegmentationRepresentations.Contour]: contourDisplay,
[SegmentationRepresentations.Surface]: surfaceDisplay,
};
/**
* Tracks event listeners for each segmentation and representation type.
*
* Structure:
* Map<segmentationId, Map<representationType, EventListener>>
*/
const segmentationListeners = new Map<string, Map<string, EventListener>>();
/**
* Add the default listener of the given SegmentationRepresentation type.
* @param viewport - viewport to which the representation was added
* @param segmentationId - ID of the segmentation
* @param representationType - representation type
*/
export function addDefaultSegmentationListener(
viewport: Types.IVolumeViewport | Types.IStackViewport,
segmentationId: string,
representationType: SegmentationRepresentations
) {
const updateFunction =
renderers[representationType].getUpdateFunction(viewport);
if (updateFunction) {
addSegmentationListener(segmentationId, representationType, updateFunction);
}
}
/**
* Subscribes to segmentation update events for a given segmentation and calls the given updateFunction for the given representation.
*
* @param segmentationId - ID of the segmentation.
* @param representationType - Representation type (e.g., Labelmap, Surface, etc.).
* @param updateFunction - Function to handle segmentation updates.
*/
function addSegmentationListener(
segmentationId: string,
representationType: string,
updateFunction: (segmentationId: string) => Promise<void>
): void {
// Ensure segmentation entry exists in the map
Eif (!segmentationListeners.has(segmentationId)) {
segmentationListeners.set(segmentationId, new Map());
}
const listenerMap = segmentationListeners.get(segmentationId);
// If existing listener, remove it before adding the new one
Iif (listenerMap.has(representationType)) {
removeSegmentationListener(segmentationId, representationType);
}
// Create and register a new debounced listener
const listener = createDebouncedSegmentationListener(
segmentationId,
representationType,
updateFunction
);
eventTarget.addEventListener(Events.SEGMENTATION_DATA_MODIFIED, listener);
// Store the listener for future cleanup
listenerMap.set(representationType, listener);
}
/**
* Unsubscribes from segmentation update events for a given segmentation and representation type.
*
* @param segmentationId - ID of the segmentation.
* @param representationType - Representation type to unsubscribe from.
*/
function removeSegmentationListener(
segmentationId: string,
representationType: string
): void {
const listenerMap = segmentationListeners.get(segmentationId);
Eif (!listenerMap) {
return;
}
const listener = listenerMap.get(representationType);
if (!listener) {
return;
}
// Remove the event listener
eventTarget.removeEventListener(Events.SEGMENTATION_DATA_MODIFIED, listener);
listenerMap.delete(representationType);
}
/**
* Unsubscribes and removes all listeners for a given segmentation.
*
* @param segmentationId - ID of the segmentation to completely remove.
*/
function removeAllSegmentationListeners(segmentationId: string): void {
const listenerMap = segmentationListeners.get(segmentationId);
if (!listenerMap) {
return;
}
// Remove all event listeners for this segmentation
for (const listener of listenerMap.values()) {
eventTarget.removeEventListener(
Events.SEGMENTATION_DATA_MODIFIED,
listener
);
}
// Remove the segmentation entry from the map
segmentationListeners.delete(segmentationId);
}
/**
* Helper function to create a debounced segmentation event handler.
*
* @param segmentationId - ID of the segmentation to listen for.
* @param updateFunction - Callback to execute when segmentation data changes.
* @returns An event listener compatible with the Cornerstone event system.
*/
function createDebouncedSegmentationListener(
segmentationId: string,
representationType: string,
updateFunction: (segmentationId: string) => Promise<void>
): EventListener {
// Debounced function ensures that frequent segmentation updates
// trigger the update function at most once every 300ms.
const debouncedHandler = debounce((event: CustomEvent) => {
const eventSegmentationId = event.detail?.segmentationId;
const segmentation = getSegmentation(eventSegmentationId);
// Execute only if the event is relevant to this segmentation and
// it has the representation type.
Eif (
eventSegmentationId === segmentationId &&
!!segmentation?.representationData?.[representationType]
) {
updateFunction(segmentationId);
triggerSegmentationModified(segmentationId);
}
}, 300);
// Return the debounced handler as a standard EventListener
return ((event: Event) => {
debouncedHandler(event as CustomEvent);
}) as EventListener;
}
export {
addSegmentationListener,
removeSegmentationListener,
removeAllSegmentationListeners,
};
|