Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: Add image (ijk) coordinates to stackCanvasToWorld example #1292

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/core/examples/stackCanvasToWorld/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RenderingEngine, Types, Enums } from '@cornerstonejs/core';
import { RenderingEngine, Types, Enums, utilities } from '@cornerstonejs/core';
import {
initDemo,
createImageIdsAndCacheMetaData,
Expand All @@ -12,11 +12,12 @@ console.warn(
);

const { ViewportType } = Enums;
const { worldToImageCoords } = utilities;

// ======== Set up page ======== //
setTitleAndDescription(
'Stack CanvasToWorld',
'Displays the world coordinate when the mouse is moved over the canvas.'
'Displays the world and image (ijk) coordinates when the mouse is moved over the canvas.'
);

const content = document.getElementById('content');
Expand All @@ -31,14 +32,17 @@ const mousePosDiv = document.createElement('div');

const canvasPosElement = document.createElement('p');
const worldPosElement = document.createElement('p');
const imagePosElement = document.createElement('p');

canvasPosElement.innerText = 'canvas:';
worldPosElement.innerText = 'world:';
imagePosElement.innerText = 'image (ijk):';

content.appendChild(mousePosDiv);

mousePosDiv.appendChild(canvasPosElement);
mousePosDiv.appendChild(worldPosElement);
mousePosDiv.appendChild(imagePosElement);

// ============================= //

Expand Down Expand Up @@ -101,11 +105,16 @@ async function run() {
];
// Convert canvas coordinates to world coordinates
const worldPos = viewport.canvasToWorld(canvasPos);
// Convert world coordinates to image (ijk) coordinates
const imagePos = worldToImageCoords(viewport.getCurrentImageId(), worldPos);

canvasPosElement.innerText = `canvas: (${canvasPos[0]}, ${canvasPos[1]})`;
worldPosElement.innerText = `world: (${worldPos[0].toFixed(
2
)}, ${worldPos[1].toFixed(2)}, ${worldPos[2].toFixed(2)})`;

const [i, j, k] = imagePos.map((item) => item.toFixed(2));
imagePosElement.innerText = `image (ij): (${i}, ${j})`;
});
}

Expand Down