Skip to content

Commit e0e2455

Browse files
committed
Fix test setup and improve coordinate mapping
1 parent b4a162b commit e0e2455

File tree

10 files changed

+4677
-53
lines changed

10 files changed

+4677
-53
lines changed

package-lock.json

Lines changed: 56 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dicom-microscopy-viewer",
3-
"version": "0.10.1",
3+
"version": "0.10.2",
44
"description": "Interactive web-based viewer for DICOM Microscopy Images",
55
"main": "build/dicom-microscopy-viewer.js",
66
"scripts": {
@@ -41,6 +41,7 @@
4141
},
4242
"dependencies": {
4343
"dicomweb-client": "^0.5.2",
44+
"mathjs": "^7.1.0",
4445
"ol": "^6.3.1"
4546
}
4647
}

src/api.js

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ import { toStringXY, rotate } from 'ol/coordinate';
3131

3232
import { formatImageMetadata, getFrameMapping } from './metadata.js';
3333
import { ROI } from './roi.js';
34-
import { generateUID } from './utils.js';
34+
import {
35+
generateUID,
36+
mapPixelCoord2SlideCoord,
37+
mapSlideCoord2PixelCoord
38+
} from './utils.js';
3539
import {
3640
Point,
3741
Multipoint,
@@ -77,6 +81,7 @@ function _getRotation(metadata) {
7781
* slide coordinate system (x, y, z), i.e. it express in which direction one
7882
* is moving in the slide coordinate system when the ROW index changes.
7983
*/
84+
8085
var degrees;
8186
if (
8287
(metadata.ImageOrientationSlide[0] === 0) &&
@@ -149,7 +154,7 @@ function _getRotation(metadata) {
149154

150155

151156
function _geometry2Scoord3d(geometry, pyramid) {
152-
console.info('map coordinates from image to slide coordinate system')
157+
console.info('map coordinates from pixel matrix to slide coordinate system')
153158
const frameOfReferenceUID = pyramid[pyramid.length-1].FrameOfReferenceUID;
154159
const type = geometry.getType();
155160
if (type === 'Point') {
@@ -207,7 +212,7 @@ function _geometry2Scoord3d(geometry, pyramid) {
207212
}
208213

209214
function _scoord3d2Geometry(scoord3d, pyramid) {
210-
console.info('map coordinates from slide to image coordinate system')
215+
console.info('map coordinates from slide coordinate system to pixel matrix')
211216
const type = scoord3d.graphicType;
212217
const data = scoord3d.graphicData;
213218

@@ -269,24 +274,28 @@ function _scoord3dCoordinates2geometryCoordinates(coordinates, pyramid) {
269274
*/
270275
function _coordinateFormatGeometry2Scoord3d(coordinates, pyramid) {
271276
let transform = false;
272-
if(!(coordinates[0] instanceof Array)) {
277+
if(!Array.isArray(coordinates[0])) {
273278
coordinates = [coordinates];
274279
transform = true;
275280
}
276281
const metadata = pyramid[pyramid.length-1];
277282
const origin = metadata.TotalPixelMatrixOriginSequence[0];
278-
const xOffset = Number(origin.XOffsetInSlideCoordinateSystem);
279-
const yOffset = Number(origin.YOffsetInSlideCoordinateSystem);
280-
const pixelSpacing = _getPixelSpacing(metadata);
281-
282-
const rotation = _getRotation(metadata);
283+
const orientation = metadata.ImageOrientationSlide;
284+
const spacing = _getPixelSpacing(metadata);
285+
const offset = [
286+
Number(origin.XOffsetInSlideCoordinateSystem),
287+
Number(origin.YOffsetInSlideCoordinateSystem),
288+
];
283289

284290
coordinates = coordinates.map(c => {
285-
const position = rotate(c, rotation);
286-
const x = Number((xOffset - ((position[1] - 1) * pixelSpacing[1])).toFixed(4));
287-
const y = Number((yOffset + (position[0] * pixelSpacing[0])).toFixed(4));
288-
const z = Number((0).toFixed(4));
289-
return [x, y, z];
291+
const pixelCoord = [c[0], -(c[1] + 1)];
292+
const slideCoord = mapPixelCoord2SlideCoord({
293+
orientation,
294+
spacing,
295+
offset,
296+
point: pixelCoord
297+
});
298+
return [slideCoord[0], slideCoord[1], 0];
290299
});
291300
if (transform) {
292301
return coordinates[0];
@@ -300,23 +309,28 @@ function _coordinateFormatGeometry2Scoord3d(coordinates, pyramid) {
300309
*/
301310
function _coordinateFormatScoord3d2Geometry(coordinates, pyramid) {
302311
let transform = false;
303-
if(!(coordinates[0] instanceof Array)) {
312+
if(!Array.isArray(coordinates[0])) {
304313
coordinates = [coordinates];
305314
transform = true;
306315
}
307316
const metadata = pyramid[pyramid.length-1];
317+
const orientation = metadata.ImageOrientationSlide;
318+
const spacing = _getPixelSpacing(metadata);
308319
const origin = metadata.TotalPixelMatrixOriginSequence[0];
309-
const xOffset = Number(origin.XOffsetInSlideCoordinateSystem);
310-
const yOffset = Number(origin.YOffsetInSlideCoordinateSystem);
311-
const pixelSpacing = _getPixelSpacing(metadata);
320+
const offset = [
321+
Number(origin.XOffsetInSlideCoordinateSystem),
322+
Number(origin.YOffsetInSlideCoordinateSystem),
323+
];
312324

313-
let rotation = _getRotation(metadata);
314-
rotation = (360 - rotation * (180 / Math.PI)) * (Math.PI / 180)
315325
coordinates = coordinates.map(c =>{
316-
const x = (c[1] - yOffset) / pixelSpacing[0];
317-
const y = -((c[0] - xOffset) / pixelSpacing[1] - 1);
318-
const z = c[2];
319-
return rotate([x, y, z], rotation);
326+
const slideCoord = [c[0], c[1]];
327+
const pixelCoord = mapSlideCoord2PixelCoord({
328+
offset,
329+
orientation,
330+
spacing,
331+
point: slideCoord
332+
});
333+
return [pixelCoord[0], -(pixelCoord[1] + 1), 0];
320334
});
321335
if (transform) {
322336
return coordinates[0];

src/metadata.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function formatImageMetadata(metadata) {
8181
if (vr === 'SQ') {
8282
dataset[keyword] = [];
8383
} else {
84-
dataset[keyword] = ""; // TODO: should rather be null?
84+
dataset[keyword] = null;
8585
}
8686
}
8787
});

0 commit comments

Comments
 (0)