Skip to content

Instantly share code, notes, and snippets.

@carlok carlok/mbroi.js
Last active Sep 28, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.