SegmentCollection
Source: segmentCollection.js:84
Collection of mask segments: stores per-segment, per-slice pixel data and segment metadata. Builds the combined label map on demand. TODO: check if mergeable with MaskSegmentHelper.
Constructor
new SegmentCollection(geometry: Geometry): SegmentCollectionParameters
geometry(Geometry) — The mask geometry, used to size the label map.
Instance Methods
getAll(): Map.<number, Map.<number, Uint8Array>>
Get all the segments.
Returns
Map.<number, Map.<number, Uint8Array>>— The segment buffers, indexed by segment number and slice index.
setLabelMap(buffer: Uint8Array)
Set the label map directly. Used when the buffer already exists, for brush-painted masks.
Parameters
buffer(Uint8Array) — The label map buffer.
addFrame(
segNumber: number,
pixelBuffer: Uint8Array,
frameOffset: number,
sliceIndex: number,
sliceSize: number,
value: number,
)
Add pixel data for one DICOM frame to the appropriate segment and slice. Allocates a Uint8Array(sliceSize) on first use for each (segNumber, sliceIndex) pair; merges if the same pair appears again.
Parameters
segNumber(number) — The segment number.pixelBuffer(Uint8Array) — The raw DICOM pixel buffer.frameOffset(number) — Offset of this frame within pixelBuffer.sliceIndex(number) — The slice index in the full label map geometry.sliceSize(number) — Number of pixels per slice (ncols * nrows).value(number) — The value to write for non-zero pixels.
getLabelMap(): Uint8Array
Build and return the combined label map (lazy cached). Iterates segments in insertion order so lower segment numbers win at overlapping positions. Sets the hasOverlap flag if any voxel already carries a different segment's value.
Returns
Uint8Array— The full-volume label map.
updateAtOffset(
offset: number,
previousValue: number,
newValue: number,
)
Update the per-segment, per-slice buffers after a direct edit to the shared label map (for example brush painting on a mask loaded from DICOM, where the label map and the per-segment buffers are otherwise two separate structures). No-op when this collection has no per-segment data (brush-created masks), since the label map is already the source of truth in that case.
Parameters
offset(number) — The offset in the label map that changed.previousValue(number) — The segment number previously at offset.newValue(number) — The segment number now at offset.
shiftSlices(sliceIndexThreshold: number, numberOfSlices: number)
Re-index the per-segment, per-slice buffers after a slice insertion (from Image#appendSlice) that shifted existing slice content to make room for new slices. Unlike the shared label map (which aliases the image buffer, so the raw byte-level shift already covers it for free), #segments is keyed by slice index rather than buffer offset, so an insertion before an existing slice leaves its entries silently pointing at the wrong (pre-shift) slice index unless they are explicitly re-keyed here.
Parameters
sliceIndexThreshold(number) — Slices at or after this index (numbering as it was before the insertion) are shifted.numberOfSlices(number) — Number of slices inserted.
getOtherValueAtOffset(
offset: number,
excludeSegmentNumber: number,
): number
Get the value that a different, non-excluded segment holds at a given label map offset. Used when deleting a segment to restore a voxel that also belonged to another (overlapping) segment instead of losing that other segment's presence there. Segments are checked in insertion order, consistent with the first-wins policy used by SegmentCollection#getLabelMap.
Parameters
offset(number) — The offset in the label map.excludeSegmentNumber(number) — The segment number to ignore.
Returns
number— The other segment's stored value at that offset, or 0 if no other segment covers it.
clone(): SegmentCollection
Create a deep copy of this collection: per-segment/per-slice buffers and the overlap flag are copied, the lazily-built label map and union contour caches are left unset (they rebuild on demand from the copied per-segment data, same as a freshly constructed collection).
Returns
SegmentCollection— The copy.
getHasOverlap(): boolean
Check whether any two segments share at least one voxel.
Returns
boolean— True if overlap was detected.
getSegmentBuffers(
segments: Array.<MaskSegment>,
): Record.<string, Record.<number, Uint8Array>>
Get the segment buffers. Using input image and segments since they could have been modified (for example by brush).
When per-segment data is available (MaskFactory path), builds ROI buffers directly from #segments so that overlap pixels for every segment are included. Falls back to label-map reconstruction for masks created via setupSegmentCollection (brush path), where #segments is empty and the label map is the only source of truth.
Parameters
segments(Array.<MaskSegment>) — The mask segments.
Returns
Record.<string, Record.<number, Uint8Array>>— The segment buffers, indexed by segment index (segment.number - 1) and slice index.
getOrBuildUnionContour(
segmentViewHelper: MaskSegmentViewHelper | undefined,
imageSize: Size,
): ImageContour
Get the union ImageContour for the current visibility state, building it if the set of hidden segments has changed.
Parameters
segmentViewHelper(MaskSegmentViewHelper | undefined) — Hidden-segment tracker.imageSize(Size) — The image size.
Returns
ImageContour— The union ImageContour.