Skip to content

Instantly share code, notes, and snippets.

@carlok
Last active September 28, 2015 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlok/6e40125d5f312c3c7847 to your computer and use it in GitHub Desktop.
Save carlok/6e40125d5f312c3c7847 to your computer and use it in GitHub Desktop.
custom Basic ROI (work in progress) for cornerstoneTool
(function ($, cornerstone, cornerstoneTools) {
'use strict';
var toolType = 'mBasicRoi';
var configuration = {
mRadius: 15
};
///////// BEGIN ACTIVE TOOL ///////
function createNewMeasurement(mouseEventData) {
var config = cornerstoneTools.mBasicRoi.getConfiguration();
// create the measurement data for this tool with the end handle activated
var measurementData = {
visible: true, active: true, handles: {
end: {
x: mouseEventData.currentPoints.image.x,
y: mouseEventData.currentPoints.image.y,
highlight: true,
active: true,
mRadius: 15
}
}
};
if (config.mRadius) {
configuration = config;
}
return measurementData;
}
///////// END ACTIVE TOOL ///////
///////// BEGIN IMAGE RENDERING ///////
function doubleClickCallback(e, eventData) {
var element = eventData.element;
if (e.data && e.data.mouseButtonMask && !cornerstoneTools.isMouseButtonEnabled(eventData.which, e.data.mouseButtonMask)) {
return false;
}
var toolData = cornerstoneTools.getToolState(element, toolType);
// now check to see if there is a handle we can move
if (!toolData) {
return false;
}
toolData.data.pop();
cornerstone.updateImage(element);
return false; // false = causes jquery to preventDefault() and stopPropagation() this event
}
function pointNearTool(element, data, coords) {
var endCanvas = cornerstone.pixelToCanvas(element, data.handles.end);
return cornerstoneMath.point.distance(endCanvas, coords) < 1.1 * configuration.mRadius; // now parametric
}
function onImageRendered(e, eventData) {
// if we have no toolData for this element, return immediately as there is nothing to do
var toolData = cornerstoneTools.getToolState(e.currentTarget, toolType);
if (toolData === undefined) {
return;
}
// we have tool data for this element - iterate over each one and draw it
var context = eventData.canvasContext.canvas.getContext("2d");
cornerstone.setToPixelCoordinateSystem(eventData.enabledElement, context);
var color = "red";
for (var i = 0; i < toolData.data.length; i++) {
context.save();
var data = toolData.data[i];
context.beginPath();
context.strokeStyle = color;
for (var property in data.handles) {
var handle = data.handles[property];
if (handle.active || handle.highlight) {
context.beginPath();
context.lineWidth = 2 / eventData.viewport.scale;
context.arc(handle.x, handle.y, configuration.mRadius, 0, 2 * Math.PI);
context.moveTo(handle.x, handle.y - (configuration.mRadius / 8));
context.lineTo(handle.x, handle.y + (configuration.mRadius / 8));
context.moveTo(handle.x - (configuration.mRadius / 8), handle.y);
context.lineTo(handle.x + (configuration.mRadius / 8), handle.y);
context.stroke();
}
}
context.stroke();
context.restore();
}
}
///////// END IMAGE RENDERING ///////
// module exports
cornerstoneTools.mBasicRoi = cornerstoneTools.mouseButtonTool({
createNewMeasurement: createNewMeasurement,
mouseDoubleClickCallback: doubleClickCallback,
onImageRendered: onImageRendered,
pointNearTool: pointNearTool,
toolType: toolType
});
cornerstoneTools.mBasicRoiTouch = cornerstoneTools.touchTool({
createNewMeasurement: createNewMeasurement,
mouseDoubleClickCallback: doubleClickCallback,
onImageRendered: onImageRendered,
pointNearTool: pointNearTool,
toolType: toolType
});
})($, cornerstone, cornerstoneTools);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment