Issue: Float32Array Handling in Annotation Points when Switching to cornerstoneWebImageLoader (RGB)

Description:

I’ve encountered an issue where Cornerstone insists on saving annotation points as Float32Array instead of regular arrays of numbers. This issue arose after changing the image loading process from cornerstoneWadoImageLoader to cornerstoneWebImageLoader so the Photometric Interpretation is RGB (I followed this code). Despite attempts to convert these arrays back to regular arrays, it continue to revert to Float32Array.

Code:

static getMeasurementData(
    MeasurementGroup,
    sopInstanceUIDToImageIdMap,
    imageToWorldCoords,
    metadata
  ) {
    const { MeasurementReport } = dcmjs.adapters.Cornerstone3D;

    const { defaultState, NUMGroup, SCOORDGroup, ReferencedFrameNumber } =
      MeasurementReport.getSetupMeasurementData(
        MeasurementGroup,
        sopInstanceUIDToImageIdMap,
        metadata,
        LengthAdapter.toolType
      );

    const referencedImageId =
      defaultState.annotation.metadata.referencedImageId;

    const { GraphicData } = SCOORDGroup;
    const worldCoords = [];
    for (let i = 0; i < GraphicData.length; i += 2) {
      const point = imageToWorldCoords(referencedImageId, [
        GraphicData[i],
        GraphicData[i + 1],
      ]);
      worldCoords.push(point);
    }

    const state = defaultState;

    state.annotation.data = {
      handles: {
        points: [...worldCoords],
        activeHandleIndex: 0,
        textBox: {
          hasMoved: false,
        },
      },
      cachedStats: {
        [`imageId:${referencedImageId}`]: {
          length: NUMGroup
            ? NUMGroup.MeasuredValueSequence?.[0]?.NumericValue
            : 0,
          unit: NUMGroup?.MeasuredValueSequence?.[0]
            ?.MeasurementUnitsCodeSequence?.[0]?.CodeValue,
          color: NUMGroup?.ContentSequence?.[0]?.TextValue.split(":")[1],
        },
      },
      frameNumber: ReferencedFrameNumber,
    };

    return state;
  }

Observed Behavior:

state.annotation.data.handles.points is logged as Float32Array in the console not regular array of numbers as worldCoords

Could you provide guidance on how to prevent Float32Array conversion when switching from MONOCHROME to RGB in DICOM images? & Thank you for your assistance!

If my issue is unclear, I’ve noticed that the points inside the state object are being forced by the library to change, so they don’t match the worldCoords (points: [...worldCoords]).

I discovered that using transformWorldToIndex reverts them to their initial state (same as the worldCoords). This workaround works, but I’m confused about why this happens.

I don’t understand what you mean by regular array of numbers, Float32Array is in fact regular array of numbers, what did you expect?

Maybe a brief intro to coordinate systems would help

https://slicer.readthedocs.io/en/latest/user_guide/coordinate_systems.html

Sorry for my misunderstanding. you’re right Float32Array was not the issue. As mentioned in my comment, the problem is that the points inside the state object are internally changed, so they don’t match the worldCoords values:

const point = imageToWorldCoords(referencedImageId, [
        GraphicData[i],
        GraphicData[i + 1],
      ]);
worldCoords.push(point);

points: [...worldCoords],

I had to add an extra step using transformWorldToIndex to revert the points back.