Skip to content

Commit

Permalink
feat: Add segmentIndex-level segmentation editing
Browse files Browse the repository at this point in the history
  • Loading branch information
sedghi authored and swederik committed Mar 21, 2022
1 parent b4eb90c commit 5cd5ccf
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 168 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
import { getEnabledElement } from '@ohif/cornerstone-render'

import state from './state'
import { addNewLabelmap, getNextLabelmapIndex } from './addNewLabelmap'
import { addNewLabelmap } from './addNewLabelmap'

function getNextLabelmapIndex(canvas) {
const enabledElement = getEnabledElement(canvas)

if (!enabledElement) {
return
}

const { viewportUID } = enabledElement

// VolumeViewport Implementation
const viewportSegState = state.volumeViewports[viewportUID]

if (!viewportSegState) {
return 0
}

const numLabelmaps = viewportSegState.labelmaps.filter(
(labelmapUID) => !!labelmapUID
).length

// next labelmap index = current length of labelmaps
return numLabelmaps
}

/**
* Returns the index of the active `Labelmap3D`.
Expand Down Expand Up @@ -69,13 +93,13 @@ async function setActiveLabelmapIndex(
}

// Todo: do we need this? it should be set to the value of the labelmapIndex
let index = labelmapIndex
if (!labelmapIndex) {
index = getNextLabelmapIndex(canvas)
}
const index = labelmapIndex
// if (!labelmapIndex) {
// index = getNextLabelmapIndex(canvas)
// }

const options = {
volumeUID: `labelmap-${index}`,
volumeUID: `${scene.uid}-labelmap-${index}`,
}
// Put the current volume as a reference for the labelmap
const labelmapUID = await addNewLabelmap({
Expand All @@ -87,4 +111,45 @@ async function setActiveLabelmapIndex(
return labelmapUID
}

export { getActiveLabelmapIndex, setActiveLabelmapIndex }
// this method SHOULD not be used to create a new labelmap
function setActiveLabelmapIndexByLabelmapUID(
canvas: HTMLCanvasElement,
labelmapUID: string
): void {
const enabledElement = getEnabledElement(canvas)

if (!enabledElement) {
return
}

const { scene, viewportUID } = enabledElement

// stackViewport
if (!scene) {
throw new Error('Segmentation for StackViewport is not supported yet')
}

// volumeViewport
const viewportSegState = state.volumeViewports[viewportUID]

if (!viewportSegState || viewportSegState.labelmaps.length === 0) {
throw new Error(`No labelmap found for ${viewportUID}`)
}

const labelmapIndex = viewportSegState.labelmaps.findIndex(
({ volumeUID }) => labelmapUID === volumeUID
)

if (labelmapIndex === undefined) {
throw new Error(`No labelmap found with name of ${labelmapUID}`)
}

setActiveLabelmapIndex(canvas, labelmapIndex)
}

export {
getActiveLabelmapIndex,
setActiveLabelmapIndex,
getNextLabelmapIndex,
setActiveLabelmapIndexByLabelmapUID,
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getEnabledElement } from '@ohif/cornerstone-render'

import state from './state'
import { addNewLabelmap, getNextLabelmapIndex } from './addNewLabelmap'
import { getActiveLabelmapIndex } from '.'

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,6 @@ type LabelmapOptions = {
}
}

function getNextLabelmapIndex(canvas) {
const enabledElement = getEnabledElement(canvas)

if (!enabledElement) {
return
}

const { viewportUID } = enabledElement

// VolumeViewport Implementation
const viewportSegState = state.volumeViewports[viewportUID]

if (!viewportSegState) {
return 0
}

const numLabelmaps = viewportSegState.labelmaps.filter(
(labelmapUID) => !!labelmapUID
).length

// next labelmap index = current length of labelmaps
return numLabelmaps
}

/**
* addNewLabelmap - Adds a `Labelmap3D` object to the `BrushStackState` object.
*
Expand Down Expand Up @@ -117,7 +93,7 @@ async function addNewLabelmap({
if (!labelmapState) {
labelmapState = {
volumeUID: labelmapUID,
activeSegmentIndex: 0,
activeSegmentIndex: 1,
segmentsHidden: [],
cfun: vtkColorTransferFunction.newInstance(),
ofun: vtkPiecewiseFunction.newInstance(),
Expand All @@ -138,4 +114,4 @@ async function addNewLabelmap({
return labelmapUID
}

export { addNewLabelmap, getNextLabelmapIndex }
export { addNewLabelmap }
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getEnabledElement } from '@ohif/cornerstone-render'
import state from './state'

function getLabelmapUIDsForElement(canvas: HTMLCanvasElement): string[] {
const enabledElement = getEnabledElement(canvas)

if (!enabledElement) {
return
}

const { scene, viewportUID } = enabledElement

// stackViewport
if (!scene) {
throw new Error('Segmentation for StackViewport is not supported yet')
}

const viewportState = state.volumeViewports[viewportUID]

if (!viewportState) {
return []
}

return viewportState.labelmaps.map(({ volumeUID }) => volumeUID)
}

function getLabelmapUIDsForViewportUID(viewportUID: string): string[] {
const viewportState = state.volumeViewports[viewportUID]

if (!viewportState) {
return []
}

return viewportState.labelmaps.map(({ volumeUID }) => volumeUID)
}

export { getLabelmapUIDsForElement, getLabelmapUIDsForViewportUID }
13 changes: 12 additions & 1 deletion packages/cornerstone-tools/src/store/SegmentationModule/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import setLabelmapForElement, {
} from './setLabelmapForElement'
import {
setActiveLabelmapIndex,
setActiveLabelmapIndexByLabelmapUID,
getActiveLabelmapIndex,
getNextLabelmapIndex,
} from './activeLabelmapIndex'
import {
setActiveSegmentIndex,
Expand All @@ -14,7 +16,11 @@ import {
getSegmentationConfig,
setSegmentationConfig,
} from './segmentationConfig'
import { addNewLabelmap, getNextLabelmapIndex } from './addNewLabelmap'
import { addNewLabelmap } from './addNewLabelmap'
import {
getLabelmapUIDsForElement,
getLabelmapUIDsForViewportUID,
} from './getLabelmapUIDsForElement'

export {
setLabelmapForElement,
Expand All @@ -23,8 +29,10 @@ export {
getSegmentationConfig,
setSegmentationConfig,
setActiveLabelmapIndex,
setActiveLabelmapIndexByLabelmapUID,
getActiveLabelmapIndex,
addNewLabelmap,
getActiveSegmentIndex,
getNextLabelmapIndex,
}

Expand All @@ -35,9 +43,12 @@ export default {

// Set/Get Labelmap
getLabelmapForElement,
getLabelmapUIDsForElement,
getLabelmapUIDsForViewportUID,

// Set/Get Active labelmap/Index
getActiveLabelmapForElement,
setActiveLabelmapIndexByLabelmapUID,
setActiveLabelmapIndex,
getActiveLabelmapIndex,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ISegmentationConfig {

const defaultSegmentationConfig: ISegmentationConfig = {
renderOutline: true,
outlineWidth: 3,
outlineWidth: 2,
renderFill: true,
renderInactiveLabelmaps: false,
// radius: 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function updateStateForVolumeViewports(
// Overwriting the new labelmap state
viewportState.labelmaps[labelmapIndex] = {
volumeUID: labelmapUID,
activeSegmentIndex: 0,
activeSegmentIndex: 1,
segmentsHidden: [],
cfun: vtkColorTransferFunction.newInstance(),
ofun: vtkPiecewiseFunction.newInstance(),
Expand Down
18 changes: 9 additions & 9 deletions packages/cornerstone-tools/src/store/SegmentationModule/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const state: SegmentationState = {
// labelmaps: [
// {
// volumeUID: 'seg1',
// activeSegmentIndex: 0,
// activeSegmentIndex: 1,
// colorLUTIndex: 0,
// cfun: new cfun
// ofun: new ofun
Expand All @@ -44,15 +44,15 @@ const state: SegmentationState = {
// labelmaps: [
// {
// volumeUID: 'seg1',
// activeSegmentIndex: 0,
// activeSegmentIndex: 1,
// colorLUTIndex: 0,
// cfun: new cfun
// ofun: new ofun
// segmentsHidden: [],
// },
// {
// volumeUID: 'seg2',
// activeSegmentIndex: 0,
// activeSegmentIndex: 1,
// colorLUTIndex: 0,
// cfun: new cfun
// ofun: new ofun
Expand All @@ -65,15 +65,15 @@ const state: SegmentationState = {
// labelmaps: [
// {
// volumeUID: 'seg1',
// activeSegmentIndex: 0,
// activeSegmentIndex: 1,
// colorLUTIndex: 0,
// cfun: new cfun
// ofun: new ofun
// segmentsHidden: [],
// },
// {
// volumeUID: 'seg2',
// activeSegmentIndex: 0,
// activeSegmentIndex: 1,
// colorLUTIndex: 0,
// cfun: new cfun
// ofun: new ofun
Expand All @@ -89,15 +89,15 @@ const state: SegmentationState = {
// labelmaps: [
// {
// volumeUID: 'seg1',
// activeSegmentIndex: 0,
// activeSegmentIndex: 1,
// colorLUTIndex: 0,
// cfun: new cfun
// ofun: new ofun
// segmentsHidden: [],
// },
// {
// volumeUID: 'seg2',
// activeSegmentIndex: 0,
// activeSegmentIndex: 1,
// colorLUTIndex: 0,
// cfun: new cfun
// ofun: new ofun
Expand All @@ -110,15 +110,15 @@ const state: SegmentationState = {
// labelmaps: [
// {
// volumeUID: 'seg1',
// activeSegmentIndex: 0,
// activeSegmentIndex: 1,
// colorLUTIndex: 0,
// cfun: new cfun
// ofun: new ofun
// segmentsHidden: [],
// },
// {
// volumeUID: 'seg2',
// activeSegmentIndex: 0,
// activeSegmentIndex: 1,
// colorLUTIndex: 0,
// cfun: new cfun
// ofun: new ofun
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ import { getViewportUIDsWithLabelmapToRender } from '../../util/viewportFilters'

import { CornerstoneTools3DEvents as EVENTS } from '../../enums'
import RectangleRoiTool from '../annotation/RectangleRoiTool'
import {
drawHandles as drawHandlesSvg,
drawLinkedTextBox as drawLinkedTextBoxSvg,
drawRect as drawRectSvg,
} from '../../drawingSvg'
import { drawRect as drawRectSvg } from '../../drawingSvg'
import {
resetElementCursor,
hideElementCursor,
Expand All @@ -26,7 +22,7 @@ import triggerAnnotationRenderForViewportUIDs from '../../util/triggerAnnotation
import {
setActiveLabelmapIndex,
getActiveLabelmapIndex,
setLabelmapForElement,
getActiveSegmentIndex,
} from '../../store/SegmentationModule'

// import {
Expand Down Expand Up @@ -91,6 +87,8 @@ export default class RectangleScissorsTool extends BaseTool {
)
}
const labelmapUID = await setActiveLabelmapIndex(element, labelmapIndex)
const segmentIndex = getActiveSegmentIndex(element)

const labelmap = cache.getVolume(labelmapUID)

const toolData = {
Expand Down Expand Up @@ -130,6 +128,7 @@ export default class RectangleScissorsTool extends BaseTool {
this.editData = {
toolData,
labelmap,
segmentIndex,
viewportUIDsToRender,
handleIndex: 3,
movingTextBox: false,
Expand Down
Loading

0 comments on commit 5cd5ccf

Please sign in to comment.