Skip to content

Commit

Permalink
fix: labelmap only update cfun when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
sedghi authored and swederik committed Mar 22, 2022
1 parent c29e738 commit f0f96de
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { deepMerge } from '../../../utilities'
import { IToolGroup } from '../../../types'

const MAX_NUMBER_COLORS = 255
const labelMapConfigCache = new Map()

/**
* For each viewport, and for each segmentation, set the segmentation for the viewport's enabled element
Expand Down Expand Up @@ -157,20 +158,19 @@ function render(
}

const actor = viewport.getActor(segmentationDataUID)

if (!actor) {
console.warn('No actor found for actorUID: ', segmentationDataUID)
return
}

const { volumeActor } = actor
const { cfun, ofun } = labelmapRepresentation.config

const labelmapConfig = config.representations[Representations.Labelmap]
const renderInactiveSegmentations = config.renderInactiveSegmentations

_setLabelmapColorAndOpacity(
volumeActor,
viewport.uid,
actor,
cfun,
ofun,
colorLUTIndex,
Expand All @@ -182,7 +182,8 @@ function render(
}

function _setLabelmapColorAndOpacity(
volumeActor: Types.VolumeActor,
viewportUID: string,
actor: Types.ActorEntry,
cfun: vtkColorTransferFunction,
ofun: vtkPiecewiseFunction,
colorLUTIndex: number,
Expand All @@ -205,25 +206,36 @@ function _setLabelmapColorAndOpacity(

const colorLUT = SegmentationState.getColorLut(colorLUTIndex)
const numColors = Math.min(256, colorLUT.length)
const { volumeActor, uid } = actor

for (let i = 0; i < numColors; i++) {
const color = colorLUT[i]
cfun.addRGBPoint(
i,
color[0] / MAX_NUMBER_COLORS,
color[1] / MAX_NUMBER_COLORS,
color[2] / MAX_NUMBER_COLORS
)
const needUpdate = _needsTransferFunctionUpdateUpdate(
viewportUID,
actor.uid,
fillAlpha,
colorLUTIndex
)

// Set the opacity per label.
const segmentOpacity = (color[3] / 255) * fillAlpha
ofun.addPoint(i, segmentOpacity)
}
// recent change to ColorTransferFunction has aff

ofun.setClamping(false)
if (needUpdate) {
for (let i = 0; i < numColors; i++) {
const color = colorLUT[i]
cfun.addRGBPoint(
i,
color[0] / MAX_NUMBER_COLORS,
color[1] / MAX_NUMBER_COLORS,
color[2] / MAX_NUMBER_COLORS
)

// Set the opacity per label.
const segmentOpacity = (color[3] / 255) * fillAlpha
ofun.addPoint(i, segmentOpacity)
}
ofun.setClamping(false)
volumeActor.getProperty().setRGBTransferFunction(0, cfun)
volumeActor.getProperty().setScalarOpacity(0, ofun)
}

volumeActor.getProperty().setRGBTransferFunction(0, cfun)
volumeActor.getProperty().setScalarOpacity(0, ofun)
volumeActor.getProperty().setInterpolationTypeToNearest()

volumeActor.getProperty().setUseLabelOutline(labelmapConfig.renderOutline)
Expand All @@ -237,6 +249,31 @@ function _setLabelmapColorAndOpacity(
volumeActor.setVisibility(visible)
}

function _needsTransferFunctionUpdateUpdate(
viewportUID: string,
actorUID: string,
fillAlpha: number,
colorLUTIndex: number
): boolean {
const cacheUID = `${viewportUID}-${actorUID}`
const config = labelMapConfigCache.get(cacheUID)

if (
config &&
config.fillAlpha === fillAlpha &&
config.colorLUTIndex === colorLUTIndex
) {
return false
}

labelMapConfigCache.set(cacheUID, {
fillAlpha,
colorLUTIndex,
})

return true
}

function _removeLabelmapFromToolGroupViewports(
toolGroupUID: string,
segmentationDataUID: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ function fillRectangle(
} = operationData
const { imageData, dimensions, scalarData } = segmentation

const rectangleCornersIJK = points.map((world) => {
let rectangleCornersIJK = points.map((world) => {
return imageData.worldToIndex(world)
})

// math round
rectangleCornersIJK = rectangleCornersIJK.map((point) => {
return point.map((coord) => {
return Math.round(coord)
})
})

const boundsIJK = getBoundingBoxAroundShape(rectangleCornersIJK, dimensions)

if (boundsIJK.every(([min, max]) => min !== max)) {
Expand Down

0 comments on commit f0f96de

Please sign in to comment.