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

feature(brush): add configuration option to skip paint if the segmentation is not visible (skips event calls) #1033

Merged
merged 1 commit into from
Jul 28, 2019
Merged
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
72 changes: 52 additions & 20 deletions src/tools/base/BaseBrushTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import isToolActive from './../../store/isToolActive.js';
import store from './../../store/index.js';
import triggerEvent from './../../util/triggerEvent.js';

const { state, setters } = store.modules.brush;
const { state, getters, setters } = store.modules.brush;

/**
* @abstract
Expand All @@ -19,7 +19,9 @@ const { state, setters } = store.modules.brush;
class BaseBrushTool extends BaseTool {
constructor(props, defaultProps = {}) {
if (!defaultProps.configuration) {
defaultProps.configuration = {};
defaultProps.configuration = {
skipPaintForInvisibleSegmentations: false,
};
}
defaultProps.configuration.referencedToolData = 'brush';
super(props, defaultProps);
Expand Down Expand Up @@ -189,29 +191,36 @@ class BaseBrushTool extends BaseTool {
* @returns {void}
*/
_drawingMouseUpCallback(evt) {
const element = evt.detail.element;

this._drawing = false;
this._mouseUpRender = true;

let measurementData = null;
const brushModule = store.modules.brush;
const toolState =
getToolState(element, this.name) || getToolState(element, 'brush');
const currentSegmentationIndex = brushModule.state.drawColorId;

if (toolState.data && toolState.data.length > currentSegmentationIndex) {
measurementData = toolState.data[currentSegmentationIndex];
}
const element = evt.detail.element;
const toolData = (
getToolState(element, this.name) ||
getToolState(element, 'brush') ||
{}
).data;
const segmentationIndex = state.drawColorId;

if (
!this.configuration.skipPaintForInvisibleSegmentations ||
_isSegmentationVisibleForElement(element, segmentationIndex, toolData)
) {
let measurementData = null;

if (toolData && toolData.length > segmentationIndex) {
measurementData = toolData[segmentationIndex];
}

const eventData = {
toolName: this.name,
element,
measurementData,
evtDetail: evt.detail,
};
const eventData = {
toolName: this.name,
element,
measurementData,
evtDetail: evt.detail,
};

triggerEvent(element, EVENTS.MEASUREMENT_COMPLETED, eventData);
triggerEvent(element, EVENTS.MEASUREMENT_COMPLETED, eventData);
}

this._stopListeningForMouseUp(element);
}
Expand Down Expand Up @@ -530,4 +539,27 @@ class BaseBrushTool extends BaseTool {
}
}

function _isSegmentationVisibleForElement(
element,
segmentationIndex,
toolData
) {
const enabledElement = external.cornerstone.getEnabledElement(element);
const visibleSegmentationsForElement = getters.visibleSegmentationsForElement(
enabledElement.uuid
);

return (
// Global alpha for active segmentation
state.alpha > 0.001 &&
// Master isVisible toggle per seg + element
// TODO: If false, should we check the secondary alpha that's applied to segmentions that aren't visible?
visibleSegmentationsForElement[segmentationIndex] === true &&
// Does not have alpha, or alpha is > 1 (0 to 255)
(toolData[segmentationIndex] === undefined ||
toolData[segmentationIndex].alpha === undefined ||
toolData[segmentationIndex].alpha > 0.001)
);
}

export default BaseBrushTool;
93 changes: 64 additions & 29 deletions src/tools/brush/BrushTool.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export default class BrushTool extends BaseBrushTool {
nonOverlapping: _nonOverlappingStrategy,
},
defaultStrategy: 'overlapping',
configuration: { alwaysEraseOnClick: false },
configuration: {
alwaysEraseOnClick: false,
skipPaintForInvisibleSegmentations: false,
},
};

super(props, defaultProps);
Expand Down Expand Up @@ -106,6 +109,21 @@ export default class BrushTool extends BaseBrushTool {
* @returns {void}
*/
_paint(evt) {
const element = evt.detail.element;
const toolData = _getBaseBrushToolStateForElement(element).data;
const segmentationIndex = brushModule.state.drawColorId;
const shouldErase =
this.configuration.alwaysEraseOnClick || _isCtrlDown(evt.detail);
const isErasingNothing = shouldErase && !toolData[segmentationIndex];

if (
isErasingNothing ||
(this.configuration.skipPaintForInvisibleSegmentations &&
!_isSegmentationVisibleForElement(element, segmentationIndex, toolData))
) {
return;
}

this.applyActiveStrategy(evt, this.configuration);

triggerEvent(evt.detail.element, EVENTS.MEASUREMENT_MODIFIED, evt.detail);
Expand All @@ -119,19 +137,10 @@ function _overlappingStrategy(evt, configuration) {
const element = eventData.element;
const { rows, columns } = eventData.image;
const { x, y } = eventData.currentPoints.image;
let toolState = getToolState(
const toolState = getToolState(
element,
BaseBrushTool.getReferencedToolDataName()
);

if (!toolState) {
addToolState(element, BaseBrushTool.getReferencedToolDataName(), {});
toolState = getToolState(
element,
BaseBrushTool.getReferencedToolDataName()
);
}

const toolData = toolState.data;

if (x < 0 || x > columns || y < 0 || y > rows) {
Expand All @@ -149,20 +158,10 @@ function _nonOverlappingStrategy(evt, configuration) {
const element = eventData.element;
const { rows, columns } = eventData.image;
const { x, y } = eventData.currentPoints.image;

let toolState = getToolState(
const toolState = getToolState(
element,
BaseBrushTool.getReferencedToolDataName()
);

if (!toolState) {
addToolState(element, BaseBrushTool.getReferencedToolDataName(), {});
toolState = getToolState(
element,
BaseBrushTool.getReferencedToolDataName()
);
}

const toolData = toolState.data;
const segmentationIndex = brushModule.state.drawColorId;

Expand Down Expand Up @@ -193,15 +192,10 @@ function _nonOverlappingStrategy(evt, configuration) {
function _drawMainColor(eventData, toolData, pointerArray, configuration) {
const shouldErase =
configuration.alwaysEraseOnClick || _isCtrlDown(eventData);
const columns = eventData.image.columns;
const segmentationIndex = brushModule.state.drawColorId;
const hasNoDataForSegmentation = !toolData[segmentationIndex];

if (shouldErase && !toolData[segmentationIndex]) {
// Erase command, yet no data yet, just return.
return;
}

if (!toolData[segmentationIndex]) {
if (hasNoDataForSegmentation) {
toolData[segmentationIndex] = {};
}

Expand All @@ -227,6 +221,7 @@ function _drawMainColor(eventData, toolData, pointerArray, configuration) {
}

const toolDataI = toolData[segmentationIndex];
const columns = eventData.image.columns;

// Draw / Erase the active color.
drawBrushPixels(pointerArray, toolDataI, columns, shouldErase);
Expand All @@ -237,3 +232,43 @@ function _drawMainColor(eventData, toolData, pointerArray, configuration) {
function _isCtrlDown(eventData) {
return (eventData.event && eventData.event.ctrlKey) || eventData.ctrlKey;
}

function _getBaseBrushToolStateForElement(element) {
let toolState = getToolState(
element,
BaseBrushTool.getReferencedToolDataName()
);

if (!toolState) {
addToolState(element, BaseBrushTool.getReferencedToolDataName(), {});
toolState = getToolState(
element,
BaseBrushTool.getReferencedToolDataName()
);
}

return toolState;
}

function _isSegmentationVisibleForElement(
element,
segmentationIndex,
toolData
) {
const enabledElement = external.cornerstone.getEnabledElement(element);
const visibleSegmentationsForElement = brushModule.getters.visibleSegmentationsForElement(
enabledElement.uuid
);

return (
// Global alpha for active segmentation
brushModule.state.alpha > 0.001 &&
// Master isVisible toggle per seg + element
// TODO: If false, should we check the secondary alpha that's applied to segmentions that aren't visible?
visibleSegmentationsForElement[segmentationIndex] === true &&
// Does not have alpha, or alpha is > 1 (0 to 255)
(toolData[segmentationIndex] === undefined ||
toolData[segmentationIndex].alpha === undefined ||
toolData[segmentationIndex].alpha > 0.001)
);
}