From b8d619390afe4fe2dd41bf666782493a5e7711e0 Mon Sep 17 00:00:00 2001 From: Bruno Alves de Faria Date: Tue, 29 May 2018 21:15:24 -0300 Subject: [PATCH] feat(bidirectional-tool): Reset rotation behaviour when long/short axes invert --- .../bidirectionalTool/invertHandles.js | 42 +++++++++++++++++++ .../bidirectionalTool/mouseDownCallback.js | 9 +++- 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 Packages/ohif-lesiontracker/client/compatibility/bidirectionalTool/invertHandles.js diff --git a/Packages/ohif-lesiontracker/client/compatibility/bidirectionalTool/invertHandles.js b/Packages/ohif-lesiontracker/client/compatibility/bidirectionalTool/invertHandles.js new file mode 100644 index 000000000..8405a9c09 --- /dev/null +++ b/Packages/ohif-lesiontracker/client/compatibility/bidirectionalTool/invertHandles.js @@ -0,0 +1,42 @@ +const swapAttribute = (a, b, attribute) => { + const originalA = a[attribute]; + const originalB = b[attribute]; + a[attribute] = originalB; + b[attribute] = originalA; +}; + +const swapHandles = (a, b) => { + swapAttribute(a, b, 'x'); + swapAttribute(a, b, 'y'); + swapAttribute(a, b, 'moving'); + swapAttribute(a, b, 'hover'); + swapAttribute(a, b, 'active'); + swapAttribute(a, b, 'selected'); +}; + +function invertHandles(eventData, measurementData, handle) { + const { rowPixelSpacing, columnPixelSpacing } = eventData.image; + const { handles } = measurementData; + const { start, end, perpendicularStart, perpendicularEnd } = handles; + + // Calculate the long axis length + const dx = (start.x - end.x) * (columnPixelSpacing || 1); + const dy = (start.y - end.y) * (rowPixelSpacing || 1); + const 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); + const width = Math.sqrt(wx * wx + wy * wy) || 0; + + if (width > length) { + swapHandles(start, end); + swapHandles(start, perpendicularStart); + swapHandles(end, perpendicularEnd); + return Object.values(handles).find(h => h.moving === true); + } + + return handle; +} + +export default invertHandles; diff --git a/Packages/ohif-lesiontracker/client/compatibility/bidirectionalTool/mouseDownCallback.js b/Packages/ohif-lesiontracker/client/compatibility/bidirectionalTool/mouseDownCallback.js index 6bff924d0..f762cb54c 100644 --- a/Packages/ohif-lesiontracker/client/compatibility/bidirectionalTool/mouseDownCallback.js +++ b/Packages/ohif-lesiontracker/client/compatibility/bidirectionalTool/mouseDownCallback.js @@ -5,6 +5,7 @@ import { toolType, distanceThreshold } from './definitions'; import mouseMoveCallback from './mouseMoveCallback'; import pointNearTool from './pointNearTool'; import moveHandle from './moveHandle'; +import invertHandles from './invertHandles'; // Clear the selected state for the given handles object const unselectAllHandles = handles => { @@ -88,9 +89,14 @@ export default function(event) { for (let i = 0; i < toolData.data.length; i++) { data = toolData.data[i]; const handleParams = [element, data.handles, coords, distanceThreshold]; - const handle = cornerstoneTools.getHandleNearImagePoint(...handleParams); + let handle = cornerstoneTools.getHandleNearImagePoint(...handleParams); if (handle) { + handle.moving = true; + + // Invert handles if needed + handle = invertHandles(eventData, data, handle); + // Hide the cursor to improve precision while resizing the line or set to move // if dragging text box $element.css('cursor', handle.hasBoundingBox ? 'move' : 'none'); @@ -99,7 +105,6 @@ export default function(event) { data.active = true; unselectAllHandles(data.handles); - handle.moving = true; moveHandle(eventData, toolType, data, handle, () => handleDoneMove(handle)); event.stopImmediatePropagation(); event.stopPropagation();