<commit_before>import { OHIF } from 'meteor/ohif:core';
import { _ } from 'meteor/underscore';

OHIF.cornerstone = {};

OHIF.cornerstone.pixelToPage = (element, position) => {
    const enabledElement = cornerstone.getEnabledElement(element);
    const result = {
        x: 0,
        y: 0
    };

    // Stop here if the cornerstone element is not enabled or position is not an object
    if (!enabledElement || typeof position !== 'object') {
        return result;
    }

    const canvas = enabledElement.canvas;

    const canvasOffset = $(canvas).offset();
    result.x += canvasOffset.left;
    result.y += canvasOffset.top;

    const canvasPosition = cornerstone.pixelToCanvas(element, position);
    result.x += canvasPosition.x;
    result.y += canvasPosition.y;

    return result;
};

OHIF.cornerstone.repositionTextBoxWhileDragging = (eventData, measurementData) => {
    const element = eventData.element;
    const enabledElement = cornerstone.getEnabledElement(element);
    const $element = $(element);
    const image = enabledElement.image;

    const modifiedCallback = () => {
        const handles = measurementData.handles;
        const textBox = handles.textBox;

        const bounds = {};
        bounds.x = textBox.boundingBox.width;
        bounds.y = textBox.boundingBox.height;

        const getHandlePosition = key => _.pick(handles[key], ['x', 'y']);
        const start = getHandlePosition('start');
        const end = getHandlePosition('end');

        const calculateAxisCenter = axis => {
            const a = start[axis];
            const b = end[axis];
            const lowest = Math.min(a, b);
            const highest = Math.max(a, b);
            return lowest + ((highest - lowest) / 2);
        };

        const tool = {};
        tool.x = calculateAxisCenter('x');
        tool.y = calculateAxisCenter('y');

        const mid = {};
        mid.x = image.width / 2;
        mid.y = image.height / 2;

        const directions = {};
        directions.x = tool.x < mid.x ? -1 : 1;
        directions.y = tool.y < mid.y ? -1 : 1;

        const points = {};
        points.x = directions.x < 0 ? 0 : image.width;
        points.y = directions.y < 0 ? 0 : image.height;

        const diffX = directions.x < 0 ? tool.x : image.width - tool.x;
        const diffY = directions.y < 0 ? tool.y : image.height - tool.y;

        const cornerAxis = diffY < diffX ? 'y' : 'x';
        const toolAxis = diffY < diffX ? 'x' : 'y';

        textBox[cornerAxis] = points[cornerAxis];
        textBox[toolAxis] = tool[toolAxis];

        if (directions[cornerAxis] < 0) {
            const topLeft = cornerstone.pageToPixel(element, 0, 0);
            const bottomRight = cornerstone.pageToPixel(element, bounds.x, bounds.y);
            const boxSize = bottomRight[cornerAxis] - topLeft[cornerAxis];
            textBox[cornerAxis] -= boxSize;
        }
    };

    const mouseUpCallback = () => {
        $element.off('CornerstoneToolsMeasurementModified', modifiedCallback);
    };

    $element.one('CornerstoneToolsMouseDrag', () => {<commit_msg>LT-40: Placing the text call-out always above the patient<commit_after>import { OHIF } from 'meteor/ohif:core';
import { _ } from 'meteor/underscore';

OHIF.cornerstone = {};

OHIF.cornerstone.pixelToPage = (element, position) => {
    const enabledElement = cornerstone.getEnabledElement(element);
    const result = {
        x: 0,
        y: 0
    };

    // Stop here if the cornerstone element is not enabled or position is not an object
    if (!enabledElement || typeof position !== 'object') {
        return result;
    }

    const canvas = enabledElement.canvas;

    const canvasOffset = $(canvas).offset();
    result.x += canvasOffset.left;
    result.y += canvasOffset.top;

    const canvasPosition = cornerstone.pixelToCanvas(element, position);
    result.x += canvasPosition.x;
    result.y += canvasPosition.y;

    return result;
};

OHIF.cornerstone.repositionTextBoxWhileDragging = (eventData, measurementData) => {
    const element = eventData.element;
    const enabledElement = cornerstone.getEnabledElement(element);
    const $element = $(element);
    const image = enabledElement.image;

    const modifiedCallback = () => {
        const handles = measurementData.handles;
        const textBox = handles.textBox;

        const bounds = {};
        bounds.x = textBox.boundingBox.width;
        bounds.y = textBox.boundingBox.height;

        const getHandlePosition = key => _.pick(handles[key], ['x', 'y']);
        const start = getHandlePosition('start');
        const end = getHandlePosition('end');

        const calculateAxisCenter = axis => {
            const a = start[axis];
            const b = end[axis];
            const lowest = Math.min(a, b);
            const highest = Math.max(a, b);
            return lowest + ((highest - lowest) / 2);
        };

        const tool = {};
        tool.x = calculateAxisCenter('x');
        tool.y = calculateAxisCenter('y');

        const mid = {};
        mid.x = image.width / 2;
        mid.y = image.height / 2;

        const directions = {};
        directions.x = tool.x < mid.x ? -1 : 1;
        directions.y = tool.y < mid.y ? -1 : 1;

        // TODO: REMOVE - Temporary for RSNA (placing always above patient)
        directions.y = -1;

        const points = {};
        points.x = directions.x < 0 ? 0 : image.width;
        points.y = directions.y < 0 ? 0 : image.height;

        const diffX = directions.x < 0 ? tool.x : image.width - tool.x;
        const diffY = directions.y < 0 ? tool.y : image.height - tool.y;

        let cornerAxis = diffY < diffX ? 'y' : 'x';
        let toolAxis = diffY < diffX ? 'x' : 'y';

        // TODO: REMOVE - Temporary for RSNA (placing always above patient)
        cornerAxis = 'y';
        toolAxis = 'x';

        textBox[cornerAxis] = points[cornerAxis];
        textBox[toolAxis] = tool[toolAxis];

        if (directions[cornerAxis] < 0) {
            const topLeft = cornerstone.pageToPixel(element, 0, 0);
            const bottomRight = cornerstone.pageToPixel(element, bounds.x, bounds.y);
            const boxSize = bottomRight[cornerAxis] - topLeft[cornerAxis];
            textBox[cornerAxis] -= boxSize;
        }
    };

    const mouseUpCallback = () => {
        $element.off('CornerstoneToolsMeasurementModified', modifiedCallback);
    };

    $element.one('CornerstoneToolsMouseDrag', () => {
{'fraction_non_alphanumeric': 0.12815744614907795, 'fraction_numerical': 0.00604370060437006, 'mean_word_length': 2.3649635036496353, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1778829', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1842, 'n_words': 466}