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 | 128x 2519336x 2519336x 60x 60x 60x 60x 38x 60x 60x 60x 22x 60x 2519216x 2519216x 2519156x 60x 60x 60x 2519216x 2519216x 2519216x 2519216x 2519216x 2519216x 2519216x 2519216x 2519216x 2519216x 2519216x 2519216x 2519216x 2519216x 2519216x 2519216x 2519216x | import type { Segmentation } from '../../../types/SegmentationStateTypes';
import type {
LabelmapSegmentationData,
LabelmapLayer,
SegmentLabelmapBindingState,
} from '../../../types/LabelmapTypes';
const SOURCE_REPRESENTATION_NAME = 'binaryLabelmap';
type LabelmapSegmentationWithState = LabelmapSegmentationData & {
labelmaps: {
[labelmapId: string]: LabelmapLayer;
};
segmentBindings: {
[segmentIndex: number]: SegmentLabelmapBindingState;
};
primaryLabelmapId: string;
};
function getSegmentOrder(segmentation: Segmentation): number[] {
Eif (segmentation.segmentOrder?.length) {
return [...segmentation.segmentOrder];
}
return Object.keys(segmentation.segments)
.map(Number)
.sort((a, b) => a - b);
}
function getPrimaryLabelmapId(segmentationId: string): string {
return `${segmentationId}-storage-0`;
}
function getPrimaryLabelmapType(
labelmapData: LabelmapSegmentationData
): 'volume' | 'stack' {
return labelmapData.volumeId ? 'volume' : 'stack';
}
function createPrimaryLabelmapLayer(
segmentation: Segmentation,
labelmapData: LabelmapSegmentationData,
labelmapId = getPrimaryLabelmapId(segmentation.segmentationId)
): LabelmapLayer {
const layer: LabelmapLayer = {
labelmapId,
storageKind: getPrimaryLabelmapType(labelmapData),
labelToSegmentIndex: {},
};
if (labelmapData.volumeId != null) {
layer.volumeId = labelmapData.volumeId;
}
Iif (labelmapData.referencedVolumeId != null) {
layer.referencedVolumeId = labelmapData.referencedVolumeId;
}
Iif (labelmapData.referencedImageIds != null) {
layer.referencedImageIds = labelmapData.referencedImageIds;
}
if (labelmapData.imageIds != null) {
layer.imageIds = labelmapData.imageIds;
}
return layer;
}
function resolvePrimaryLabelmapId(
segmentation: Segmentation,
labelmapData: LabelmapSegmentationData
): string {
const storedLabelmapId = labelmapData.primaryLabelmapId;
if (storedLabelmapId && labelmapData.labelmaps?.[storedLabelmapId]) {
return storedLabelmapId;
}
const fallbackLabelmapId =
Object.keys(labelmapData.labelmaps ?? {})[0] ??
getPrimaryLabelmapId(segmentation.segmentationId);
labelmapData.primaryLabelmapId = fallbackLabelmapId;
return fallbackLabelmapId;
}
/**
* Normalizes sparse or legacy labelmap representation data into the current
* internal state shape. This mutates the segmentation so later render paths and
* state helpers can use stable labelmap and segment binding maps without
* rebuilding the default layer identity on every call.
*/
function ensureLabelmapState(
segmentation: Segmentation
): LabelmapSegmentationWithState | undefined {
const labelmapData = segmentation.representationData.Labelmap;
Iif (!labelmapData) {
return;
}
labelmapData.labelmaps ||= {};
const primaryLabelmapId = resolvePrimaryLabelmapId(
segmentation,
labelmapData
);
labelmapData.labelmaps[primaryLabelmapId] ||= createPrimaryLabelmapLayer(
segmentation,
labelmapData,
primaryLabelmapId
);
labelmapData.segmentBindings ||= {};
labelmapData.sourceRepresentationName ||= SOURCE_REPRESENTATION_NAME;
getSegmentOrder(segmentation).forEach((segmentIndex) => {
labelmapData.segmentBindings[segmentIndex] ||= {
labelmapId: primaryLabelmapId,
labelValue: segmentIndex,
};
});
Object.values(labelmapData.labelmaps).forEach((layer) => {
layer.labelToSegmentIndex = {};
});
Object.entries(labelmapData.segmentBindings).forEach(
([segmentIndex, binding]) => {
const layer = labelmapData.labelmaps[binding.labelmapId];
Iif (!layer) {
return;
}
layer.labelToSegmentIndex ||= {};
layer.labelToSegmentIndex[binding.labelValue] = Number(segmentIndex);
}
);
return labelmapData as LabelmapSegmentationWithState;
}
export {
SOURCE_REPRESENTATION_NAME,
ensureLabelmapState,
getPrimaryLabelmapId,
getSegmentOrder,
};
export type { LabelmapSegmentationWithState };
|