Restructuring bidirectionalTool to improve maintainability

This commit is contained in:
Bruno Alves de Faria committed 2017-12-28 08:59:25 -02:00
1 parent 4aa0204119
commit 0acdff0bc5
22 files changed
+1290 -1

No files matched your search

@@ -0,0 +1,22 @@
import { cornerstoneTools } from 'meteor/ohif:cornerstone';
// Add a proxy to cornerstoneTools.drawHandles function to change the handles' active state base on
// the hover, moving and selected states
export default function(context, eventData, handles, color, options) {
Object.keys(handles).forEach(handleKey => {
if (handleKey === 'textBox') return;
const handle = handles[handleKey];
handle.drawnIndependently = handle.moving;
if (handle.selected) {
handle.active = handle.hover;
} else {
if (handle.hover) {
handle.active = true;
} else {
handle.active = false;
}
}
});
cornerstoneTools.drawHandles(context, eventData, handles, color, options);
}
@@ -0,0 +1,54 @@
import { cornerstone } from 'meteor/ohif:cornerstone';
// draw perpendicular line
export default function(context, eventData, element, data, color, lineWidth) {
let startX, startY, endX, endY;
if (data.handles.start.x === data.handles.end.x &&
data.handles.start.y === data.handles.end.y) {
startX = data.handles.start.x;
startY = data.handles.start.y;
endX = data.handles.end.x;
endY = data.handles.end.y;
} else {
// mid point of long-axis line
const mid = {
x: (data.handles.start.x + data.handles.end.x) / 2,
y: (data.handles.start.y + data.handles.end.y) / 2
};
// Length of long-axis
const dx = (data.handles.start.x - data.handles.end.x) * (eventData.image.columnPixelSpacing || 1);
const dy = (data.handles.start.y - data.handles.end.y) * (eventData.image.rowPixelSpacing || 1);
const length = Math.sqrt(dx * dx + dy * dy);
const vectorX = (data.handles.start.x - data.handles.end.x) / length;
const vectorY = (data.handles.start.y - data.handles.end.y) / length;
const perpendicularLineLength = length / 2;
startX = mid.x + (perpendicularLineLength / 2) * vectorY;
startY = mid.y - (perpendicularLineLength / 2) * vectorX;
endX = mid.x - (perpendicularLineLength / 2) * vectorY;
endY = mid.y + (perpendicularLineLength / 2) * vectorX;
}
if (data.handles.perpendicularStart.locked) {
data.handles.perpendicularStart.x = startX;
data.handles.perpendicularStart.y = startY;
data.handles.perpendicularEnd.x = endX;
data.handles.perpendicularEnd.y = endY;
}
// Draw perpendicular line
const perpendicularStartCanvas = cornerstone.pixelToCanvas(element, data.handles.perpendicularStart);
const perpendicularEndCanvas = cornerstone.pixelToCanvas(element, data.handles.perpendicularEnd);
context.beginPath();
context.strokeStyle = color;
context.lineWidth = lineWidth;
context.moveTo(perpendicularStartCanvas.x, perpendicularStartCanvas.y);
context.lineTo(perpendicularEndCanvas.x, perpendicularEndCanvas.y);
context.stroke();
}
@@ -0,0 +1,39 @@
import { _ } from 'meteor/underscore';
import { OHIF } from 'meteor/ohif:core';
import { cornerstone, cornerstoneTools } from 'meteor/ohif:cornerstone';
// Draw a line marker over the selected arm
export default function(eventData, handles, color) {
const lib = OHIF.lesiontracker.bidirectional;
const { canvasContext, element } = eventData;
const handleKey = lib.getSelectedHandleKey(handles);
if (!handleKey) return;
const handle = handles[handleKey];
// Used a big distance (1km) to fill the entire line
const mmStep = -1000000;
// Get the line's start and end points
const fakeImage = {
columnPixelSpacing: eventData.viewport.scale,
rowPixelSpacing: eventData.viewport.scale
};
const pointA = lib.repositionBidirectionalArmHandle(fakeImage, handles, handleKey, mmStep, 0);
const pointB = _.pick(handle, ['x', 'y']);
// Stop here if pointA is not present
if (!pointA) return;
// Get the canvas coordinates for the line var perpendicularStartCanvas = cornerstone.pixelToCanvas(element, data.handles.perpendicularStart);
const canvasPointA = cornerstone.pixelToCanvas(element, pointA);
const canvasPointB = cornerstone.pixelToCanvas(element, pointB);
// Draw the line marker
canvasContext.beginPath();
canvasContext.strokeStyle = color;
canvasContext.lineWidth = cornerstoneTools.toolStyle.getToolWidth();
canvasContext.moveTo(canvasPointA.x, canvasPointA.y);
canvasContext.lineTo(canvasPointB.x, canvasPointB.y);
canvasContext.stroke();
}
@@ -0,0 +1,2 @@
import onImageRendered from './onImageRendered';
export default onImageRendered;
@@ -0,0 +1,170 @@
import { cornerstone, cornerstoneMath, cornerstoneTools } from 'meteor/ohif:cornerstone';
import { OHIF } from 'meteor/ohif:core';
import { toolType } from '../definitions';
import drawHandles from './drawHandles';
import drawPerpendicularLine from './drawPerpendicularLine';
import drawSelectedMarker from './drawSelectedMarker';
export default function(event, eventData) {
const { element, canvasContext } = eventData;
// if we have no toolData for this element, return immediately as there is nothing to do
const toolData = cornerstoneTools.getToolState(event.currentTarget, toolType);
if (!toolData) return;
// LT-29 Disable Target Measurements when pixel spacing is not available
const { rowPixelSpacing, columnPixelSpacing } = eventData.image;
if (!rowPixelSpacing || !columnPixelSpacing) return;
// we have tool data for this element - iterate over each one and draw it
const context = canvasContext.canvas.getContext('2d');
context.setTransform(1, 0, 0, 1, 0, 0);
let color;
const lineWidth = cornerstoneTools.toolStyle.getToolWidth();
const config = cornerstoneTools[toolType].getConfiguration();
for (let i = 0; i < toolData.data.length; i++) {
const data = toolData.data[i];
const { start, end, perpendicularStart, perpendicularEnd, textBox } = data.handles;
const strokeWidth = lineWidth;
context.save();
// configurable shadow from CornerstoneTools
const { shadow } = config;
if (shadow && shadow.shadow) {
context.shadowColor = shadow.shadowColor || '#000000';
context.shadowOffsetX = shadow.shadowOffsetX || 1;
context.shadowOffsetY = shadow.shadowOffsetY || 1;
}
const activeColor = cornerstoneTools.toolColors.getActiveColor();
if (data.active) {
color = activeColor;
} else {
color = cornerstoneTools.toolColors.getToolColor();
}
// draw the line
const handleStartCanvas = cornerstone.pixelToCanvas(element, start);
const handleEndCanvas = cornerstone.pixelToCanvas(element, end);
const canvasTextLocation = cornerstone.pixelToCanvas(element, textBox);
context.beginPath();
context.strokeStyle = color;
context.lineWidth = strokeWidth;
context.moveTo(handleStartCanvas.x, handleStartCanvas.y);
context.lineTo(handleEndCanvas.x, handleEndCanvas.y);
context.stroke();
// Draw perpendicular line
drawPerpendicularLine(context, eventData, element, data, color, strokeWidth);
// Draw the handles
const handlesColor = color;
drawHandles(context, eventData, data.handles, handlesColor, { drawHandlesIfActive: true });
// Draw the selected marker
drawSelectedMarker(eventData, data.handles, '#FF9999');
// Calculate the long axis length
const dx = (start.x - end.x) * (columnPixelSpacing || 1);
const dy = (start.y - end.y) * (rowPixelSpacing || 1);
let length = Math.sqrt(dx * dx + dy * dy);
// Calculate the short axis length
const wx = (perpendicularStart.x - perpendicularEnd.x) * (columnPixelSpacing || 1);
const wy = (perpendicularStart.y - perpendicularEnd.y) * (rowPixelSpacing || 1);
let width = Math.sqrt(wx * wx + wy * wy);
if (!width) {
width = 0;
}
// Length is always longer than width
if (width > length) {
const tempW = width;
const tempL = length;
length = tempW;
width = tempL;
}
if (data.measurementNumber) {
// Draw the textbox
let suffix = ' mm';
if (!rowPixelSpacing || !columnPixelSpacing) {
suffix = ' pixels';
}
const lengthText = ' L ' + length.toFixed(1) + suffix;
const widthText = ' W ' + width.toFixed(1) + suffix;
const textLines = [`Target ${data.measurementNumber}`, lengthText, widthText];
const boundingBox = cornerstoneTools.drawTextBox(
context,
textLines,
canvasTextLocation.x,
canvasTextLocation.y,
color,
config.textBox
);
textBox.boundingBox = boundingBox;
OHIF.cornerstone.repositionTextBox(eventData, data, config.textBox);
// Draw linked line as dashed
const link = {
start: {},
end: {}
};
const midpointCanvas = {
x: (handleStartCanvas.x + handleEndCanvas.x) / 2,
y: (handleStartCanvas.y + handleEndCanvas.y) / 2,
};
const points = [ handleStartCanvas, handleEndCanvas, midpointCanvas ];
link.end.x = canvasTextLocation.x;
link.end.y = canvasTextLocation.y;
link.start = cornerstoneMath.point.findClosestPoint(points, link.end);
const boundingBoxPoints = [ {
// Top middle point of bounding box
x: boundingBox.left + boundingBox.width / 2,
y: boundingBox.top
}, {
// Left middle point of bounding box
x: boundingBox.left,
y: boundingBox.top + boundingBox.height / 2
}, {
// Bottom middle point of bounding box
x: boundingBox.left + boundingBox.width / 2,
y: boundingBox.top + boundingBox.height
}, {
// Right middle point of bounding box
x: boundingBox.left + boundingBox.width,
y: boundingBox.top + boundingBox.height / 2
},
];
link.end = cornerstoneMath.point.findClosestPoint(boundingBoxPoints, link.start);
context.beginPath();
context.strokeStyle = color;
context.lineWidth = lineWidth;
context.setLineDash([ 2, 3 ]);
context.moveTo(link.start.x, link.start.y);
context.lineTo(link.end.x, link.end.y);
context.stroke();
}
// Set measurement text to show lesion table
data.longestDiameter = length.toFixed(1);
data.shortestDiameter = width.toFixed(1);
context.restore();
}
}