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

feat(LabelTool): add label tool #1725

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/utilities/windowLevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function toWindowLevel(
*
* LINEAR (default):
* - Uses the DICOM standard formula from C.11.2.1.2.1:
* if x <= c - 0.5 - (w-1)/2 => lower bound
* if x > c - 0.5 + (w-1)/2 => upper bound
* if x {'<='} c - 0.5 - (w-1)/2 {'=>'} lower bound
* if x {'>'} c - 0.5 + (w-1)/2 {'=>'} upper bound
*
* LINEAR_EXACT (C.11.2.1.3.2):
* - Uses:
Expand Down
2 changes: 2 additions & 0 deletions packages/tools/examples/dynamicallyAddAnnotations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ArrowAnnotateTool,
CircleROITool,
EllipticalROITool,
LabelTool,
LengthTool,
ProbeTool,
RectangleROITool,
Expand All @@ -32,6 +33,7 @@ console.debug(
);

const tools = [
LabelTool,
AngleTool,
ArrowAnnotateTool,
EllipticalROITool,
Expand Down
79 changes: 79 additions & 0 deletions packages/tools/examples/dynamicallyAddAnnotations/labelToolUI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { getEnabledElementByViewportId, utilities } from '@cornerstonejs/core';
import type { Point2 } from '@cornerstonejs/core/types';
import { LabelTool } from '@cornerstonejs/tools';
import { typeToIdMap, typeToStartIdMap, typeToEndIdMap } from './constants';

function getInputValue(form: HTMLFormElement, inputId: string): number {
return Number((form.querySelector(`#${inputId}`) as HTMLInputElement).value);
}

function getCoordinates(
form: HTMLFormElement,
type: 'canvas' | 'image'
): Point2 {
const position: Point2 = [
getInputValue(form, `${typeToStartIdMap[type]}-1`),
getInputValue(form, `${typeToStartIdMap[type]}-2`),
];
return position;
}

function createFormElement(): HTMLFormElement {
const form = document.createElement('form');
form.style.marginBottom = '10px';

['canvas', 'image'].forEach((coordType) => {
form.innerHTML += `
<label style="margin-right: 20px;">${
coordType.charAt(0).toUpperCase() + coordType.slice(1)
} Coords: Start [${coordType === 'canvas' ? 'x, y' : 'i, j'}]:</label>
<input style="width:40px" type="number" id="${coordType}-start-1" placeholder="${
coordType === 'canvas' ? 'x' : 'i'
}" value="10">
<input style="width:40px" type="number" id="${coordType}-start-2" placeholder="${
coordType === 'canvas' ? 'y' : 'j'
}" value="10">
<label style="margin-left: 52px; margin-right: 21px;">Text:</label>
<input style="width:100px" type="text" id="${coordType}-text" placeholder="My Annotation" value="">
<br>
<button style="margin-left: 52px;" type="button" id="${coordType}-stack">Add Stack</button>
<button type="button" id="${coordType}-volume">Add Volume</button>
<br><br>
`;
});

return form;
}

function addButtonListeners(form: HTMLFormElement): void {
const buttons = form.querySelectorAll('button');
buttons.forEach((button) => {
button.addEventListener('click', () => {
const [type, viewportType] = button.id.split('-') as [
'canvas' | 'image',
keyof typeof typeToIdMap
];
const enabledElement = getEnabledElementByViewportId(
typeToIdMap[viewportType]
);
const viewport = enabledElement.viewport;
const coords = getCoordinates(form, type);
const textInput = form.querySelector(`#${type}-text`) as HTMLInputElement;
const text = textInput ? textInput.value : '';
const currentImageId = viewport.getCurrentImageId() as string;

const position =
type === 'image'
? utilities.imageToWorldCoords(currentImageId, coords)
: viewport.canvasToWorld(coords);

LabelTool.hydrate(viewport.id, position, text);
});
});
}

export function createLabelToolUI(): HTMLFormElement {
const form = createFormElement();
addButtonListeners(form);
return form;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createProbeToolUI } from './probeToolUI';
import { createRectangleROIToolUI } from './rectangleROIToolUI';
import { createCircleROIToolUI } from './circleROIToolUI';
import { createSplineROIToolUI } from './splineROIToolUI';
import { createLabelToolUI } from './labelToolUI';

interface ToolUIConfig {
toolName: string;
Expand All @@ -31,6 +32,9 @@ function createToolUI(toolName: string, config: ToolUIConfig): ToolUI | null {
case 'EllipticalROI':
forms = [createEllipseROIToolUI()];
break;
case 'Label':
forms = [createLabelToolUI()];
break;
case 'Length':
forms = [createLengthToolUI()];
break;
Expand Down
2 changes: 2 additions & 0 deletions packages/tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
StackScrollTool,
PlanarRotateTool,
MIPJumpToClickTool,
LabelTool,
LengthTool,
HeightTool,
ProbeTool,
Expand Down Expand Up @@ -106,6 +107,7 @@ export {
PlanarRotateTool,
MIPJumpToClickTool,
// Annotation Tools
LabelTool,
LengthTool,
HeightTool,
CrosshairsTool,
Expand Down
Loading
Loading