cornerstone3D: [Bug] imageToWorldCoords does not work correctly

Describe the Bug

First of all, I need to mention that my original goal is to get transformation matrix/object for displayed image in canvas coordinate system. Other words, I need to get image position relatively to top left canvas corner in pixels, image scale (image width in pixels on canvas divide by original image width in pixels for scaleX and same logic with height for scaleY) and rotation. To calculate image position and scale I tried to use imageToWorldCoords method from utilities with worldToCanvas from my instance of StackViewport. It looks something like this:

function computePosition(viewport: Types.IStackViewport): Types.Point2 {
    const imageId = viewport.getCurrentImageId();

    const positionInImageCoordinates = [0, 0] as Types.Point2;
    const positionInWorldCoordinates = utilities.imageToWorldCoords(imageId, positionInImageCoordinates) as Types.Point3;

    return viewport.worldToCanvas(positionInWorldCoordinates);
}

function computeScale(viewport: Types.IStackViewport): { x: number, y: number } {
    const image = viewport.getCornerstoneImage();

    const topLeftImageCornerInImageCoordinates = [0, 0] as Types.Point2;
    const bottomRightImageCornerInImageCoordinates = [image.width, image.height] as Types.Point2;

    const topLeftImageCornerInWorldCoordinates = utilities.imageToWorldCoords(image.imageId, topLeftImageCornerInImageCoordinates) as Types.Point3;
    const bottomRightImageCornerInWorldCoordinates = utilities.imageToWorldCoords(image.imageId, bottomRightImageCornerInImageCoordinates) as Types.Point3;

    const [leftImageCoordinateInCanvas, topImageCoordinateInCanvas] = viewport.worldToCanvas(topLeftImageCornerInWorldCoordinates);
    const [rightImageCoordinateInCanvas, bottomImageCoordinateInCanvas] = viewport.worldToCanvas(bottomRightImageCornerInWorldCoordinates);

    const imageWidthInCanvas = rightImageCoordinateInCanvas - leftImageCoordinateInCanvas;
    const imageHeightInCanvas = bottomImageCoordinateInCanvas - topImageCoordinateInCanvas;

    const scaleX = imageWidthInCanvas / image.width;
    const scaleY = imageHeightInCanvas / image.height;

    return { x: scaleX, y: scaleY };
}

// `viewport` id defined here
viewportElement.addEventListener(Enums.Events.CAMERA_MODIFIED, () => {
    const position = computePosition(viewport);
    const scale = computeScale(viewport);
    const rotation = viewport.getRotation();

    // other code...
});

Unfortunately, this approach does not work because method imageToWordCoords from utilities throws this error:

bundle.js:40443 Uncaught TypeError: Cannot read properties of undefined (reading '0')
    at Object.scaleAndAdd (bundle.js:40443:16)
    at Object.imageToWorldCoords (bundle.js:69289:18)
    at computePosition (bundle.js:108323:60)
    at HTMLDivElement.<anonymous> (bundle.js:108314:26)
    at triggerEvent (bundle.js:38048:15)
    at StackViewport.triggerCameraModifiedEventIfNecessary (bundle.js:85586:9)
    at StackViewport.setCamera (bundle.js:85574:12)
    at PanTool._dragCallback (bundle.js:100019:31)
    at PanTool.mouseDragCallback (bundle.js:100001:12)
    at HTMLDivElement.mouseDrag (bundle.js:106045:16)

I investigated original method and tried to make it work by myself, but this did not give me any result. To make long story short, i tried different series and it works on CT and MR modalities, but does not work on DX because column and row cosines are null and origin is undefined. I tried to make it default with something like [0, 0, 0] but it works wrong for now. Hope you can help me with this issue.

Steps to Reproduce

  1. Open DX/RG modality with cornerstone viewer and stack viewport (you can use my example file located here https://filetransfer.io/data-package/lxlqzF3M#link)
  2. Run code above in your environment
  3. See errors in console

The current behavior

Code throws errors in console and you cannot get calculated values

The expected behavior

It works fine like with CT/MR modalities

OS

MacOS

Node version

18.17.0

Browser

Chrome version 116.0.5845.179 (Official Build) (arm64)

About this issue

  • Original URL
  • State: open
  • Created 10 months ago
  • Reactions: 7
  • Comments: 16 (11 by maintainers)

Most upvoted comments

Have same problem.