Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fractional segmentation #161

Merged
merged 10 commits into from
Nov 28, 2024
88 changes: 58 additions & 30 deletions src/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ function disposeOverviewMapLayers (map) {
*/
export function disposeLayer (layer, disposeSource = false) {
console.info('dispose layer:', layer)
if (typeof layer?.getSource !== 'function') {
return
}
const source = layer.getSource()
if (disposeSource === true && source && source.clear) {
source.clear()
Expand Down Expand Up @@ -4329,7 +4332,8 @@ class VolumeImageViewer {
client: _getClient(this[_clients], Enums.SOPClassUIDs.SEGMENTATION),
channel: segmentNumber
},
hasLoader: false
hasLoader: false,
segmentationType: refSegmentation.SegmentationType
}

const source = new DataTileSource({
Expand Down Expand Up @@ -4359,7 +4363,7 @@ class VolumeImageViewer {
windowWidth,
colormap: [
[...segment.style.paletteColorLookupTable.data.at(0), defaultSegmentStyle.backgroundOpacity],
[...segment.style.paletteColorLookupTable.data.at(-1)]
...segment.style.paletteColorLookupTable.data.slice(1)
pedrokohler marked this conversation as resolved.
Show resolved Hide resolved
]
}),
useInterimTilesOnError: false,
Expand Down Expand Up @@ -4416,7 +4420,7 @@ class VolumeImageViewer {
* @param {Object} [styleOptions]
* @param {number} [styleOptions.opacity] - Opacity
*/
showSegment (segmentUID, styleOptions = {}) {
showSegment (segmentUID, styleOptions = {}, shouldZoomIn = false) {
if (!(segmentUID in this[_segments])) {
const error = new CustomError(
errorTypes.VISUALIZATION,
Expand All @@ -4438,13 +4442,16 @@ class VolumeImageViewer {
source.setLoader(loader)
}

const view = this[_map].getView()
const currentZoomLevel = view.getZoom()
pedrokohler marked this conversation as resolved.
Show resolved Hide resolved
if (
currentZoomLevel < segment.minZoomLevel ||
currentZoomLevel > segment.maxZoomLevel
) {
view.animate({ zoom: segment.minZoomLevel })
if (shouldZoomIn) {
const view = this[_map].getView()
const currentZoomLevel = view.getZoom()

if (
currentZoomLevel < segment.minZoomLevel ||
currentZoomLevel > segment.maxZoomLevel
) {
view.animate({ zoom: segment.minZoomLevel })
}
}

segment.layer.setVisible(true)
Expand Down Expand Up @@ -4490,32 +4497,16 @@ class VolumeImageViewer {
}

/**
* Set the style of a segment.
* Add segment overlay. The overlay shows the color palette of the segment.
*
* @param {string} segmentUID - Unique tracking identifier of segment
* @param {Object} styleOptions - Style options
* @param {number} [styleOptions.opacity] - Opacity
* @param {Object} segment - The segment for which to show the overlay
*/
setSegmentStyle (segmentUID, styleOptions = {}) {
if (!(segmentUID in this[_segments])) {
const error = new CustomError(
errorTypes.VISUALIZATION,
'Cannot set style of segment. ' +
`Could not find segment "${segmentUID}".`
)
throw this[_options].errorInterceptor(error) || error
}
const segment = this[_segments][segmentUID]

if (styleOptions.opacity != null) {
segment.style.opacity = styleOptions.opacity
segment.layer.setOpacity(styleOptions.opacity)
}

addSegmentOverlay (segment) {
let title = segment.segment.propertyType.CodeMeaning
const padding = Math.round((16 - title.length) / 2)
title = title.padStart(title.length + padding)
title = title.padEnd(title.length + 2 * padding)

const overlayElement = segment.overlay.getElement()
overlayElement.innerHTML = title
overlayElement.style = {}
Expand Down Expand Up @@ -4547,14 +4538,51 @@ class VolumeImageViewer {
context.fillStyle = `rgb(${r}, ${g}, ${b})`
context.fillRect(0, height / colors.length * j, width, 1)
}

const upperBound = document.createElement('span')
upperBound.innerHTML = '255'

const lowerBound = document.createElement('span')
lowerBound.innerHTML = '0'

overlayElement.appendChild(upperBound)
overlayElement.appendChild(canvas)
overlayElement.appendChild(lowerBound)

const parentElement = overlayElement.parentNode
parentElement.style.display = 'inline'

this[_map].addOverlay(segment.overlay)
}

/**
* Set the style of a segment.
*
* @param {string} segmentUID - Unique tracking identifier of segment
* @param {Object} styleOptions - Style options
* @param {number} [styleOptions.opacity] - Opacity
*/
setSegmentStyle (segmentUID, styleOptions = {}) {
if (!(segmentUID in this[_segments])) {
const error = new CustomError(
errorTypes.VISUALIZATION,
'Cannot set style of segment. ' +
`Could not find segment "${segmentUID}".`
)
throw this[_options].errorInterceptor(error) || error
}
const segment = this[_segments][segmentUID]

if (styleOptions.opacity != null) {
segment.style.opacity = styleOptions.opacity
segment.layer.setOpacity(styleOptions.opacity)
}

if (segment.segmentationType === 'FRACTIONAL') {
this.addSegmentOverlay(segment)
}
}

/**
* Get the default style of a segment.
*
Expand Down