Skip to content

Commit

Permalink
feat(brush size): enable specifying brush size for specific tool (#780)
Browse files Browse the repository at this point in the history
* feat(brush size): enable specifying brush size for specific tool

* address review comments

* typo;

* update api
  • Loading branch information
sedghi authored Sep 12, 2023
1 parent 39fc0a4 commit e933b2f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
4 changes: 2 additions & 2 deletions common/reviews/api/tools.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2076,7 +2076,7 @@ function getBoundingBoxAroundShape(points: Types_2.Point3[], dimensions?: Types_
function getBoundsIJKFromRectangleAnnotations(annotations: any, referenceVolume: any, options?: Options): any;

// @public (undocumented)
function getBrushSizeForToolGroup(toolGroupId: string): void;
function getBrushSizeForToolGroup(toolGroupId: string, toolName?: string): void;

// @public (undocumented)
function getBrushThresholdForToolGroup(toolGroupId: string): any;
Expand Down Expand Up @@ -4677,7 +4677,7 @@ function setAnnotationVisibility(annotationUID: string, visible?: boolean): void
function setAttributesIfNecessary(attributes: any, svgNode: any): void;

// @public (undocumented)
function setBrushSizeForToolGroup(toolGroupId: string, brushSize: number): void;
function setBrushSizeForToolGroup(toolGroupId: string, brushSize: number, toolName?: string): void;

// @public (undocumented)
function setBrushThresholdForToolGroup(toolGroupId: string, threshold: Types_2.Point2): void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ function _setLabelmapColorAndOpacity(
isActiveLabelmap
);

// Todo: the below loop probably can be optimized so that we don't hit it
// unless a config has changed. Right now we get into the following loop
// even for brush drawing which does not makes sense
for (let i = 0; i < numColors; i++) {
const segmentIndex = i;
const segmentColor = colorLUT[segmentIndex];
Expand Down
30 changes: 12 additions & 18 deletions packages/tools/src/tools/displayTools/SegmentationDisplayTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,26 +143,20 @@ class SegmentationDisplayTool extends BaseTool {
const config = this._getMergedRepresentationsConfig(toolGroupId);

const viewportsRenderList = [];
const display =
representation.type === Representations.Labelmap
? labelmapDisplay
: contourDisplay;

for (const viewport of toolGroupViewports) {
if (representation.type == Representations.Labelmap) {
viewportsRenderList.push(
labelmapDisplay.render(
viewport as Types.IVolumeViewport,
representation,
config
)
);
} else if (representation.type == Representations.Contour) {
viewportsRenderList.push(
contourDisplay.render(
viewport as Types.IVolumeViewport,
representation,
config
)
);
}
}
const renderedViewport = display.render(
viewport as Types.IVolumeViewport,
representation,
config
);

viewportsRenderList.push(renderedViewport);
}
return viewportsRenderList;
}
);
Expand Down
26 changes: 22 additions & 4 deletions packages/tools/src/utilities/segmentation/brushSizeForToolGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ import triggerAnnotationRenderForViewportIds from '../triggerAnnotationRenderFor
import { getRenderingEngine } from '@cornerstonejs/core';
import getBrushToolInstances from './utilities';

/**
* Sets the brush size for all brush-based tools in a given tool group.
* @param toolGroupId - The ID of the tool group to set the brush size for.
* @param brushSize - The new brush size to set.
* @param toolName - The name of the specific tool to set the brush size for (optional)
* If not provided, all brush-based tools in the tool group will be affected.
*/
export function setBrushSizeForToolGroup(
toolGroupId: string,
brushSize: number
brushSize: number,
toolName?: string
): void {
const toolGroup = getToolGroup(toolGroupId);

if (toolGroup === undefined) {
return;
}

const brushBasedToolInstances = getBrushToolInstances(toolGroupId);
const brushBasedToolInstances = getBrushToolInstances(toolGroupId, toolName);

brushBasedToolInstances.forEach((tool: BrushTool) => {
tool.configuration.brushSize = brushSize;
Expand Down Expand Up @@ -45,7 +53,17 @@ export function setBrushSizeForToolGroup(
triggerAnnotationRenderForViewportIds(renderingEngine, viewportIds);
}

export function getBrushSizeForToolGroup(toolGroupId: string): void {
/**
* Gets the brush size for the first brush-based tool instance in a given tool group.
* @param toolGroupId - The ID of the tool group to get the brush size for.
* @param toolName - The name of the specific tool to get the brush size for (Optional)
* If not provided, the first brush-based tool instance in the tool group will be used.
* @returns The brush size of the selected tool instance, or undefined if no brush-based tool instance is found.
*/
export function getBrushSizeForToolGroup(
toolGroupId: string,
toolName?: string
): void {
const toolGroup = getToolGroup(toolGroupId);

if (toolGroup === undefined) {
Expand All @@ -58,7 +76,7 @@ export function getBrushSizeForToolGroup(toolGroupId: string): void {
return;
}

const brushBasedToolInstances = getBrushToolInstances(toolGroupId);
const brushBasedToolInstances = getBrushToolInstances(toolGroupId, toolName);

// one is enough as they share the same brush size
const brushToolInstance = brushBasedToolInstances[0];
Expand Down
9 changes: 8 additions & 1 deletion packages/tools/src/utilities/segmentation/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export type ThresholdInformation = {
upper: number;
};

export default function getBrushToolInstances(toolGroupId) {
export default function getBrushToolInstances(
toolGroupId: string,
toolName?: string
) {
const toolGroup = getToolGroup(toolGroupId);

if (toolGroup === undefined) {
Expand All @@ -23,6 +26,10 @@ export default function getBrushToolInstances(toolGroupId) {
return;
}

if (toolName && toolInstances[toolName]) {
return [toolInstances[toolName]];
}

// For each tool that has BrushTool as base class, set the brush size.
const brushBasedToolInstances = Object.values(toolInstances).filter(
(toolInstance) => toolInstance instanceof BrushTool
Expand Down

0 comments on commit e933b2f

Please sign in to comment.