@cornerstonejs/adapters 5.12.0 → 5.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/adapters/Cornerstone/Segmentation_4X.js +87 -23
- package/dist/esm/adapters/Cornerstone3D/Segmentation/generateSegmentation.js +214 -49
- package/dist/esm/adapters/Cornerstone3D/Segmentation/perFrameFunctionalGroups.d.ts +7 -0
- package/dist/esm/adapters/Cornerstone3D/Segmentation/perFrameFunctionalGroups.js +64 -3
- package/dist/esm/adapters/Cornerstone3D/Segmentation/sourceFrame.d.ts +14 -0
- package/dist/esm/adapters/Cornerstone3D/Segmentation/sourceFrame.js +60 -0
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +7 -7
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
1
2
|
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
2
3
|
import { data, normalizers, derivations, utilities as utilities$1, log } from 'dcmjs';
|
|
3
4
|
import ndarray from 'ndarray';
|
|
@@ -6,10 +7,13 @@ import getDatasetsFromImages from '../helpers/getDatasetsFromImages.js';
|
|
|
6
7
|
import checkOrientation from '../helpers/checkOrientation.js';
|
|
7
8
|
import { utilities } from '@cornerstonejs/core';
|
|
8
9
|
import { utilities as utilities$2 } from '@cornerstonejs/metadata';
|
|
9
|
-
import {
|
|
10
|
+
import { applyPerFrameFunctionalGroups, setPlaneOrientations, getReferencedSourceImageSequenceItem } from '../Cornerstone3D/Segmentation/perFrameFunctionalGroups.js';
|
|
11
|
+
import { getSourceFrame } from '../Cornerstone3D/Segmentation/sourceFrame.js';
|
|
10
12
|
import { getBitmapFramesFromDataset, encodeFramesToTransferSyntax, RLE_LOSSLESS_TRANSFER_SYNTAX_UID as RLE_LOSSLESS_TRANSFER_SYNTAX_UID$1 } from '../Cornerstone3D/encodePixelData.js';
|
|
11
13
|
import { Events } from '../enums/Events.js';
|
|
12
14
|
|
|
15
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
16
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
13
17
|
const cs3dLogger = utilities.logger.adaptersLog.getLogger('Cornerstone.Segmentation_4X');
|
|
14
18
|
const BitArray = data.BitArray,
|
|
15
19
|
DicomMessage = data.DicomMessage,
|
|
@@ -51,6 +55,38 @@ function generateSegmentation(images, inputLabelmaps3D) {
|
|
|
51
55
|
return fillSegmentation(segmentation, inputLabelmaps3D, userOptions);
|
|
52
56
|
}
|
|
53
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Logs one warning for the SEG frames whose source image has no position or
|
|
60
|
+
* no orientation. Such a frame keeps the plane value that dcmjs gave it.
|
|
61
|
+
*/
|
|
62
|
+
function warnMissingPlanes(frameDescriptors, getSource) {
|
|
63
|
+
const missing = {
|
|
64
|
+
position: [],
|
|
65
|
+
orientation: []
|
|
66
|
+
};
|
|
67
|
+
frameDescriptors.forEach((desc, frameIndex) => {
|
|
68
|
+
const source = getSource(desc.sourceFrameIndex);
|
|
69
|
+
Object.keys(missing).forEach(key => {
|
|
70
|
+
if (!source[key]) {
|
|
71
|
+
missing[key].push(frameIndex + 1);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
const details = [['ImagePositionPatient', missing.position], ['ImageOrientationPatient', missing.orientation]].filter(_ref => {
|
|
76
|
+
let _ref2 = _slicedToArray(_ref, 2),
|
|
77
|
+
frames = _ref2[1];
|
|
78
|
+
return frames.length;
|
|
79
|
+
}).map(_ref3 => {
|
|
80
|
+
let _ref4 = _slicedToArray(_ref3, 2),
|
|
81
|
+
name = _ref4[0],
|
|
82
|
+
frames = _ref4[1];
|
|
83
|
+
return "no ".concat(name, " for SEG frames ").concat(frames.join(', '));
|
|
84
|
+
});
|
|
85
|
+
if (details.length) {
|
|
86
|
+
cs3dLogger.warn("The source images have ".concat(details.join('; '), ". These frames keep the ") + "values that dcmjs gave them, which can be those of other source images.");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
54
90
|
/**
|
|
55
91
|
* The segment index of a voxel value of one labelmap3D. A value that is not in
|
|
56
92
|
* `labelmap3D.labelToSegmentIndex` is its own segment index.
|
|
@@ -100,11 +136,23 @@ function segmentsPresentInFrame(pixelData) {
|
|
|
100
136
|
* valid RLE-encoded SEG), so it has been dropped rather than fixed. RLE
|
|
101
137
|
* output is now selected via `transferSyntaxUid` (RLE Lossless is the
|
|
102
138
|
* default), and encoding is handled by `encodeFramesToTransferSyntax`.
|
|
139
|
+
* @param images - The source images, in the order of the derived frames.
|
|
140
|
+
* @param metadata - A metadata provider for the `imageId` of each image.
|
|
141
|
+
*
|
|
142
|
+
* With `images` and `metadata`, each SEG frame gets the source reference, the
|
|
143
|
+
* position and the orientation of its own source image. dcmjs gives a frame
|
|
144
|
+
* the position of the referenced multiframe frame with the same index, but it
|
|
145
|
+
* sorts that multiframe by position, so for a stack in the other scan
|
|
146
|
+
* direction the frame gets the position of the mirrored slice. dcmjs also
|
|
147
|
+
* writes one shared orientation, from one source image. A frame without a
|
|
148
|
+
* source SOP Instance UID throws, so that the per-frame groups always match
|
|
149
|
+
* the PixelData frames. Without `images` or `metadata`, as from the legacy
|
|
150
|
+
* `generateSegmentation`, the frames keep the planes of dcmjs.
|
|
103
151
|
*
|
|
104
152
|
* @returns {object} The filled segmentation object.
|
|
105
153
|
*/
|
|
106
154
|
function fillSegmentation(segmentation, inputLabelmaps3D) {
|
|
107
|
-
var _segmentation$dataset, _segmentation$dataset2,
|
|
155
|
+
var _segmentation$dataset, _segmentation$dataset2, _ref5, _options$transferSynt;
|
|
108
156
|
let userOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
109
157
|
let images = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
|
|
110
158
|
let metadata = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null;
|
|
@@ -175,23 +223,39 @@ function fillSegmentation(segmentation, inputLabelmaps3D) {
|
|
|
175
223
|
});
|
|
176
224
|
});
|
|
177
225
|
if (frameDescriptors.length && images !== null && images !== void 0 && images.length && metadata) {
|
|
178
|
-
//
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
226
|
+
// Read each source image one time, also when it holds many segments.
|
|
227
|
+
const sources = new Map();
|
|
228
|
+
const getSource = sourceFrameIndex => {
|
|
229
|
+
if (!sources.has(sourceFrameIndex)) {
|
|
230
|
+
const image = images[sourceFrameIndex];
|
|
231
|
+
sources.set(sourceFrameIndex, _objectSpread(_objectSpread({}, getSourceFrame(image, metadata)), {}, {
|
|
232
|
+
sourceImageSequenceItem: getReferencedSourceImageSequenceItem(image, metadata)
|
|
233
|
+
}));
|
|
234
|
+
}
|
|
235
|
+
return sources.get(sourceFrameIndex);
|
|
236
|
+
};
|
|
182
237
|
const perFrameInputs = frameDescriptors.map(desc => {
|
|
183
|
-
const
|
|
238
|
+
const _getSource = getSource(desc.sourceFrameIndex),
|
|
239
|
+
position = _getSource.position,
|
|
240
|
+
sourceImageSequenceItem = _getSource.sourceImageSequenceItem;
|
|
241
|
+
|
|
242
|
+
// Throw, so that the per-frame groups always match the PixelData frames.
|
|
184
243
|
if (!(sourceImageSequenceItem !== null && sourceImageSequenceItem !== void 0 && sourceImageSequenceItem.ReferencedSOPInstanceUID)) {
|
|
185
244
|
throw new Error("Cannot resolve a source ReferencedSOPInstanceUID for SEG frame " + "(sourceFrameIndex ".concat(desc.sourceFrameIndex, ", segment ") + "".concat(desc.referencedSegmentNumber, "). Refusing to write a SEG with ") + "unreliable source image references.");
|
|
186
245
|
}
|
|
187
246
|
return {
|
|
188
247
|
referencedSegmentNumber: desc.referencedSegmentNumber,
|
|
189
|
-
sourceImageSequenceItem
|
|
248
|
+
sourceImageSequenceItem,
|
|
249
|
+
planePositionSequence: position ? {
|
|
250
|
+
ImagePositionPatient: position
|
|
251
|
+
} : undefined
|
|
190
252
|
};
|
|
191
253
|
});
|
|
192
254
|
applyPerFrameFunctionalGroups(segmentation.dataset, perFrameInputs);
|
|
255
|
+
setPlaneOrientations(segmentation.dataset, frameDescriptors.map(desc => getSource(desc.sourceFrameIndex).orientation));
|
|
256
|
+
warnMissingPlanes(frameDescriptors, getSource);
|
|
193
257
|
}
|
|
194
|
-
const transferSyntaxUid = (
|
|
258
|
+
const transferSyntaxUid = (_ref5 = (_options$transferSynt = options.transferSyntaxUid) !== null && _options$transferSynt !== void 0 ? _options$transferSynt : options.transferSyntaxUID) !== null && _ref5 !== void 0 ? _ref5 : RLE_LOSSLESS_TRANSFER_SYNTAX_UID;
|
|
195
259
|
if (transferSyntaxUid === RLE_LOSSLESS_TRANSFER_SYNTAX_UID) {
|
|
196
260
|
const isBinaryBitmap = segmentation.dataset.SegmentationType === 'BINARY' && Number(segmentation.dataset.BitsAllocated) === 1;
|
|
197
261
|
if (isBinaryBitmap) {
|
|
@@ -272,10 +336,10 @@ function _getSegmentLabelmaps(sources, referencedFrameIndicies) {
|
|
|
272
336
|
}
|
|
273
337
|
const labelmaps = referencedFrameIndicies.map(frameIndex => {
|
|
274
338
|
let merged;
|
|
275
|
-
sources.forEach(
|
|
339
|
+
sources.forEach(_ref6 => {
|
|
276
340
|
var _labelmap3D$labelmaps;
|
|
277
|
-
let labelmap3D =
|
|
278
|
-
labelValue =
|
|
341
|
+
let labelmap3D = _ref6.labelmap3D,
|
|
342
|
+
labelValue = _ref6.labelValue;
|
|
279
343
|
const pixelData = (_labelmap3D$labelmaps = labelmap3D.labelmaps2D[frameIndex]) === null || _labelmap3D$labelmaps === void 0 ? void 0 : _labelmap3D$labelmaps.pixelData;
|
|
280
344
|
if (!pixelData) {
|
|
281
345
|
return;
|
|
@@ -733,9 +797,9 @@ function checkSEGsOverlapping(pixelData, multiframe, imageIds, validOrientations
|
|
|
733
797
|
frameSegmentsMapping.set(imageIdIndex, [frameSegment]);
|
|
734
798
|
}
|
|
735
799
|
}
|
|
736
|
-
for (let
|
|
737
|
-
var
|
|
738
|
-
let role =
|
|
800
|
+
for (let _ref7 of frameSegmentsMapping.entries()) {
|
|
801
|
+
var _ref8 = _slicedToArray(_ref7, 2);
|
|
802
|
+
let role = _ref8[1];
|
|
739
803
|
let temp2DArray = new TypedArrayConstructor(sliceLength).fill(0);
|
|
740
804
|
for (let i = 0; i < role.length; ++i) {
|
|
741
805
|
const frameSegment = role[i];
|
|
@@ -1139,10 +1203,10 @@ function getImageIdOfSourceImagebyGeometry(ReferencedSeriesInstanceUID, FrameOfR
|
|
|
1139
1203
|
imagePlane
|
|
1140
1204
|
});
|
|
1141
1205
|
}
|
|
1142
|
-
for (const
|
|
1143
|
-
const imageId =
|
|
1144
|
-
const position =
|
|
1145
|
-
const imagePlane =
|
|
1206
|
+
for (const _ref9 of candidates) {
|
|
1207
|
+
const imageId = _ref9.imageId;
|
|
1208
|
+
const position = _ref9.position;
|
|
1209
|
+
const imagePlane = _ref9.imagePlane;
|
|
1146
1210
|
const rowCosines = utilities$2.toNumber(imagePlane === null || imagePlane === void 0 ? void 0 : imagePlane.rowCosines);
|
|
1147
1211
|
const columnCosines = utilities$2.toNumber(imagePlane === null || imagePlane === void 0 ? void 0 : imagePlane.columnCosines);
|
|
1148
1212
|
if (!rowCosines || !columnCosines) {
|
|
@@ -1380,10 +1444,10 @@ function calculateCentroid(imageIdIndexBufferIndex, multiframe, metadataProvider
|
|
|
1380
1444
|
let worldYAcc = 0;
|
|
1381
1445
|
let worldZAcc = 0;
|
|
1382
1446
|
let count = 0;
|
|
1383
|
-
for (const
|
|
1384
|
-
var
|
|
1385
|
-
const imageIdIndex =
|
|
1386
|
-
const bufferIndices =
|
|
1447
|
+
for (const _ref0 of Object.entries(imageIdIndexBufferIndex)) {
|
|
1448
|
+
var _ref1 = _slicedToArray(_ref0, 2);
|
|
1449
|
+
const imageIdIndex = _ref1[0];
|
|
1450
|
+
const bufferIndices = _ref1[1];
|
|
1387
1451
|
const z = Number(imageIdIndex);
|
|
1388
1452
|
if (!bufferIndices || bufferIndices.length === 0) {
|
|
1389
1453
|
continue;
|
|
@@ -1,19 +1,31 @@
|
|
|
1
|
+
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
|
|
1
2
|
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
2
3
|
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
3
|
-
import { normalizers, derivations } from 'dcmjs';
|
|
4
|
-
import { utilities, Enums } from '@cornerstonejs/core';
|
|
4
|
+
import { normalizers, derivations, data } from 'dcmjs';
|
|
5
|
+
import { utilities as utilities$1, Enums } from '@cornerstonejs/core';
|
|
5
6
|
import { fillSegmentation, getSegmentIndexForLabel } from '../../Cornerstone/Segmentation_4X.js';
|
|
6
7
|
import { getBitmapFramesFromDataset, encodeFramesToTransferSyntax, EXPLICIT_VR_LITTLE_ENDIAN_TRANSFER_SYNTAX_UID } from '../encodePixelData.js';
|
|
7
|
-
import { normalizeSharedFunctionalGroupsSequence, getReferencedSourceImageSequenceItem, applyPerFrameFunctionalGroups } from './perFrameFunctionalGroups.js';
|
|
8
|
+
import { normalizeSharedFunctionalGroupsSequence, getReferencedSourceImageSequenceItem, applyPerFrameFunctionalGroups, setPlaneOrientations } from './perFrameFunctionalGroups.js';
|
|
9
|
+
import { utilities } from '@cornerstonejs/metadata';
|
|
10
|
+
import { vec3 } from 'gl-matrix';
|
|
11
|
+
import { toArray } from '../../helpers/toArray.js';
|
|
12
|
+
import { getSourceFrame, sliceNormal, GEOMETRY_TOLERANCE, toFiniteVector } from './sourceFrame.js';
|
|
8
13
|
|
|
14
|
+
const _excluded = ["SliceThickness", "SpacingBetweenSlices"];
|
|
9
15
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
10
16
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
11
|
-
const
|
|
17
|
+
const isEqual = utilities.isEqual;
|
|
18
|
+
const cs3dLogger = utilities$1.logger.adaptersLog.getLogger('Cornerstone3D.Segmentation.generateSegmentation');
|
|
12
19
|
const MetadataModules = Enums.MetadataModules;
|
|
13
20
|
const SEGImageNormalizer = normalizers.SEGImageNormalizer;
|
|
14
21
|
const SegmentationDerivation = derivations.Segmentation;
|
|
15
22
|
const LABELMAP_SEG_SOP_CLASS_UID = '1.2.840.10008.5.1.4.1.1.66.7';
|
|
16
23
|
const BITMAP_SEG_SOP_CLASS_UID = '1.2.840.10008.5.1.4.1.1.66.4';
|
|
24
|
+
const IMAGE_POSITION_PATIENT_TAG = 0x00200032;
|
|
25
|
+
const PLANE_POSITION_SEQUENCE_TAG = 0x00209113;
|
|
26
|
+
const STACK_ID_TAG = 0x00209056;
|
|
27
|
+
const IN_STACK_POSITION_NUMBER_TAG = 0x00209057;
|
|
28
|
+
const FRAME_CONTENT_SEQUENCE_TAG = 0x00209111;
|
|
17
29
|
function resolveTransferSyntaxUid() {
|
|
18
30
|
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
19
31
|
const transferSyntaxUid = options.transferSyntaxUid || options.transferSyntaxUID;
|
|
@@ -114,35 +126,157 @@ function collectSegmentSequence(labelmap3DArray) {
|
|
|
114
126
|
return segment;
|
|
115
127
|
});
|
|
116
128
|
}
|
|
117
|
-
function
|
|
118
|
-
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
function getOrientationIndex(orientations, orientation) {
|
|
130
|
+
let add = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
131
|
+
const index = orientations.findIndex(other => isEqual(other, orientation, GEOMETRY_TOLERANCE));
|
|
132
|
+
return index === -1 && add ? orientations.push(orientation) - 1 : index;
|
|
133
|
+
}
|
|
134
|
+
const POSITION_DIMENSION = {
|
|
135
|
+
DimensionIndexPointer: IMAGE_POSITION_PATIENT_TAG,
|
|
136
|
+
FunctionalGroupPointer: PLANE_POSITION_SEQUENCE_TAG,
|
|
137
|
+
DimensionDescriptionLabel: 'ImagePositionPatient'
|
|
138
|
+
};
|
|
139
|
+
const STACK_ID_DIMENSION = {
|
|
140
|
+
DimensionIndexPointer: STACK_ID_TAG,
|
|
141
|
+
FunctionalGroupPointer: FRAME_CONTENT_SEQUENCE_TAG,
|
|
142
|
+
DimensionDescriptionLabel: 'StackID'
|
|
143
|
+
};
|
|
144
|
+
const IN_STACK_POSITION_DIMENSION = {
|
|
145
|
+
DimensionIndexPointer: IN_STACK_POSITION_NUMBER_TAG,
|
|
146
|
+
FunctionalGroupPointer: FRAME_CONTENT_SEQUENCE_TAG,
|
|
147
|
+
DimensionDescriptionLabel: 'InStackPositionNumber'
|
|
148
|
+
};
|
|
149
|
+
function getLabelmapDimensions(frames) {
|
|
150
|
+
const missing = frames.findIndex(_ref3 => {
|
|
151
|
+
let position = _ref3.position,
|
|
152
|
+
orientation = _ref3.orientation;
|
|
153
|
+
return !position || !orientation;
|
|
154
|
+
});
|
|
155
|
+
if (missing !== -1) {
|
|
156
|
+
throw new Error("Cannot write a LABELMAP SEG: the source image of frame ".concat(missing + 1, " ") + "has no ImagePositionPatient or ImageOrientationPatient.");
|
|
132
157
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
orientation =
|
|
158
|
+
const orientations = [];
|
|
159
|
+
const frameOrientations = frames.map(_ref4 => {
|
|
160
|
+
let orientation = _ref4.orientation;
|
|
161
|
+
return getOrientationIndex(orientations, orientation);
|
|
162
|
+
});
|
|
163
|
+
const normals = orientations.map(sliceNormal);
|
|
164
|
+
const distances = frames.map((_ref5, index) => {
|
|
165
|
+
let position = _ref5.position;
|
|
166
|
+
return vec3.dot(normals[frameOrientations[index]], position);
|
|
167
|
+
});
|
|
168
|
+
const planes = [];
|
|
169
|
+
let planeStart = 0;
|
|
170
|
+
frames.map((_, index) => index).sort((a, b) => frameOrientations[a] - frameOrientations[b] || distances[a] - distances[b]).forEach((index, i, sorted) => {
|
|
171
|
+
var _planes$previous;
|
|
172
|
+
const previous = sorted[i - 1];
|
|
173
|
+
const samePlane = i > 0 && frameOrientations[index] === frameOrientations[previous] && distances[index] - planeStart <= GEOMETRY_TOLERANCE;
|
|
174
|
+
if (!samePlane) {
|
|
175
|
+
planeStart = distances[index];
|
|
176
|
+
}
|
|
177
|
+
planes[index] = samePlane ? planes[previous] : ((_planes$previous = planes[previous]) !== null && _planes$previous !== void 0 ? _planes$previous : -1) + 1;
|
|
178
|
+
});
|
|
179
|
+
const planeCounts = [];
|
|
180
|
+
const orientationStacks = orientations.map(() => []);
|
|
181
|
+
const stacks = [];
|
|
182
|
+
frames.forEach((_, index) => {
|
|
183
|
+
var _planeCounts$planes$i, _stackIndexes$repeat;
|
|
184
|
+
const repeat = (_planeCounts$planes$i = planeCounts[planes[index]]) !== null && _planeCounts$planes$i !== void 0 ? _planeCounts$planes$i : 0;
|
|
185
|
+
planeCounts[planes[index]] = repeat + 1;
|
|
186
|
+
const stackIndexes = orientationStacks[frameOrientations[index]];
|
|
187
|
+
(_stackIndexes$repeat = stackIndexes[repeat]) !== null && _stackIndexes$repeat !== void 0 ? _stackIndexes$repeat : stackIndexes[repeat] = stacks.push([]) - 1;
|
|
188
|
+
stacks[stackIndexes[repeat]].push(index);
|
|
189
|
+
});
|
|
190
|
+
const frameContents = [];
|
|
191
|
+
stacks.forEach((stack, stackIndex) => {
|
|
192
|
+
[...stack].sort((a, b) => planes[a] - planes[b]).forEach((frameIndex, rank) => {
|
|
193
|
+
frameContents[frameIndex] = stacks.length === 1 ? {
|
|
194
|
+
DimensionIndexValues: [rank + 1]
|
|
195
|
+
} : {
|
|
196
|
+
StackID: String(stackIndex + 1),
|
|
197
|
+
InStackPositionNumber: rank + 1,
|
|
198
|
+
DimensionIndexValues: [stackIndex + 1, rank + 1]
|
|
199
|
+
};
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
return {
|
|
203
|
+
dimensions: stacks.length === 1 ? [POSITION_DIMENSION] : [STACK_ID_DIMENSION, IN_STACK_POSITION_DIMENSION],
|
|
204
|
+
frameContents,
|
|
205
|
+
orientations,
|
|
206
|
+
normals,
|
|
207
|
+
frameOrientations
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function getSourceSliceSpacings(sourceImages, metadata, orientations, normals) {
|
|
211
|
+
const distances = orientations.map(() => []);
|
|
212
|
+
sourceImages.forEach(image => {
|
|
213
|
+
const _getSourceFrame = getSourceFrame(image, metadata),
|
|
214
|
+
position = _getSourceFrame.position,
|
|
215
|
+
orientation = _getSourceFrame.orientation;
|
|
216
|
+
const index = position && orientation ? getOrientationIndex(orientations, orientation, false) : -1;
|
|
217
|
+
if (index !== -1) {
|
|
218
|
+
distances[index].push(vec3.dot(normals[index], position));
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
return distances.map(planes => {
|
|
222
|
+
planes.sort((a, b) => a - b);
|
|
223
|
+
const gaps = planes.slice(1).map((distance, index) => distance - planes[index]).filter(gap => gap > GEOMETRY_TOLERANCE);
|
|
224
|
+
if (!gaps.length) {
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
const mean = gaps.reduce((sum, gap) => sum + gap, 0) / gaps.length;
|
|
228
|
+
return gaps.every(gap => Math.abs(gap - mean) <= mean * 0.01) ? mean : undefined;
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
const PIXEL_MEASURE_KEYS = ['PixelSpacing', 'SliceThickness', 'SpacingBetweenSlices'];
|
|
232
|
+
const isSameMeasure = (a, b) => a === b || isEqual(a, b, GEOMETRY_TOLERANCE);
|
|
233
|
+
function applyLabelmapPixelMeasures(dataset, frames, frameOrientations, sliceSpacings, includeSliceSpacing) {
|
|
234
|
+
var _toArray$;
|
|
235
|
+
const shared = dataset.SharedFunctionalGroupsSequence;
|
|
236
|
+
const _ref6 = (_toArray$ = toArray(shared.PixelMeasuresSequence)[0]) !== null && _toArray$ !== void 0 ? _toArray$ : {};
|
|
237
|
+
_ref6.SliceThickness;
|
|
238
|
+
_ref6.SpacingBetweenSlices;
|
|
239
|
+
const derived = _objectWithoutProperties(_ref6, _excluded);
|
|
240
|
+
const derivedPixelSpacing = toFiniteVector(derived.PixelSpacing, 2);
|
|
241
|
+
let spacingMissing = false;
|
|
242
|
+
const pixelMeasures = frames.map((frame, index) => {
|
|
243
|
+
var _frame$pixelSpacing, _frame$sliceThickness;
|
|
244
|
+
const sliceSpacing = sliceSpacings[frameOrientations[index]];
|
|
245
|
+
const measures = _objectSpread({}, derived);
|
|
246
|
+
const pixelSpacing = (_frame$pixelSpacing = frame.pixelSpacing) !== null && _frame$pixelSpacing !== void 0 ? _frame$pixelSpacing : derivedPixelSpacing;
|
|
247
|
+
if (pixelSpacing) {
|
|
248
|
+
measures.PixelSpacing = pixelSpacing;
|
|
249
|
+
}
|
|
250
|
+
const sliceThickness = (_frame$sliceThickness = frame.sliceThickness) !== null && _frame$sliceThickness !== void 0 ? _frame$sliceThickness : sliceSpacing;
|
|
251
|
+
if (sliceThickness !== undefined) {
|
|
252
|
+
measures.SliceThickness = sliceThickness;
|
|
253
|
+
}
|
|
254
|
+
if (includeSliceSpacing) {
|
|
255
|
+
if (sliceSpacing === undefined) {
|
|
256
|
+
spacingMissing = true;
|
|
257
|
+
} else {
|
|
258
|
+
measures.SpacingBetweenSlices = sliceSpacing;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return measures;
|
|
262
|
+
});
|
|
263
|
+
if (spacingMissing) {
|
|
264
|
+
cs3dLogger.warn('generateSegmentation: includeSliceSpacing is set, but the source ' + 'slices are not evenly spaced, there is only one slice, or not all ' + 'source images are loaded; the LABELMAP SEG omits Spacing Between ' + 'Slices.');
|
|
136
265
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
266
|
+
const _pixelMeasures = _slicedToArray(pixelMeasures, 1),
|
|
267
|
+
first = _pixelMeasures[0];
|
|
268
|
+
const same = pixelMeasures.every(measures => PIXEL_MEASURE_KEYS.every(key => isSameMeasure(measures[key], first[key])));
|
|
269
|
+
if (same) {
|
|
270
|
+
shared.PixelMeasuresSequence = first;
|
|
271
|
+
return undefined;
|
|
141
272
|
}
|
|
142
|
-
|
|
273
|
+
delete shared.PixelMeasuresSequence;
|
|
274
|
+
return pixelMeasures;
|
|
143
275
|
}
|
|
144
276
|
function fillLabelmapSegmentation(segmentation, inputLabelmaps3D, metadata, images) {
|
|
277
|
+
var _toArray$0$DimensionO, _toArray$2;
|
|
145
278
|
let options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
279
|
+
let sourceImages = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : images;
|
|
146
280
|
const labelmap3DArray = toLabelmap3DArray(inputLabelmaps3D);
|
|
147
281
|
const segmentSequence = collectSegmentSequence(labelmap3DArray);
|
|
148
282
|
const validFrameIndices = collectNonEmptyFrameIndices(labelmap3DArray);
|
|
@@ -185,6 +319,21 @@ function fillLabelmapSegmentation(segmentation, inputLabelmaps3D, metadata, imag
|
|
|
185
319
|
});
|
|
186
320
|
return frame;
|
|
187
321
|
};
|
|
322
|
+
const frames = images.map((image, outputIndex) => {
|
|
323
|
+
const sourceImageSequenceItem = getReferencedSourceImageSequenceItem(image, metadata);
|
|
324
|
+
if (!(sourceImageSequenceItem !== null && sourceImageSequenceItem !== void 0 && sourceImageSequenceItem.ReferencedSOPInstanceUID)) {
|
|
325
|
+
throw new Error("Cannot resolve a source ReferencedSOPInstanceUID for labelmap SEG " + "frame ".concat(outputIndex + 1, ". Refusing to write a SEG with unreliable ") + "source image references.");
|
|
326
|
+
}
|
|
327
|
+
return _objectSpread(_objectSpread({}, getSourceFrame(image, metadata, true)), {}, {
|
|
328
|
+
sourceImageSequenceItem
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
const _getLabelmapDimension = getLabelmapDimensions(frames),
|
|
332
|
+
dimensions = _getLabelmapDimension.dimensions,
|
|
333
|
+
frameContents = _getLabelmapDimension.frameContents,
|
|
334
|
+
orientations = _getLabelmapDimension.orientations,
|
|
335
|
+
normals = _getLabelmapDimension.normals,
|
|
336
|
+
frameOrientations = _getLabelmapDimension.frameOrientations;
|
|
188
337
|
const dataset = segmentation.dataset;
|
|
189
338
|
dataset.NumberOfFrames = numberOfFrames;
|
|
190
339
|
dataset.Rows = rows;
|
|
@@ -218,30 +367,46 @@ function fillLabelmapSegmentation(segmentation, inputLabelmaps3D, metadata, imag
|
|
|
218
367
|
Value: [LABELMAP_SEG_SOP_CLASS_UID],
|
|
219
368
|
vr: 'UI'
|
|
220
369
|
};
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
370
|
+
dataset.SourceImageSequence = frames.map(_ref7 => {
|
|
371
|
+
let sourceImageSequenceItem = _ref7.sourceImageSequenceItem;
|
|
372
|
+
return sourceImageSequenceItem;
|
|
373
|
+
});
|
|
374
|
+
const dimensionOrganizationUID = (_toArray$0$DimensionO = (_toArray$2 = toArray(dataset.DimensionOrganizationSequence)[0]) === null || _toArray$2 === void 0 ? void 0 : _toArray$2.DimensionOrganizationUID) !== null && _toArray$0$DimensionO !== void 0 ? _toArray$0$DimensionO : data.DicomMetaDictionary.uid();
|
|
375
|
+
dataset.DimensionOrganizationSequence = {
|
|
376
|
+
DimensionOrganizationUID: dimensionOrganizationUID
|
|
377
|
+
};
|
|
378
|
+
dataset.DimensionIndexSequence = dimensions.map(dimension => _objectSpread({
|
|
379
|
+
DimensionOrganizationUID: dimensionOrganizationUID
|
|
380
|
+
}, dimension));
|
|
381
|
+
normalizeSharedFunctionalGroupsSequence(dataset);
|
|
382
|
+
delete dataset.SharedFunctionalGroupsSequence.PlanePositionSequence;
|
|
383
|
+
const includeSliceSpacing = Boolean(options.includeSliceSpacing);
|
|
384
|
+
const sliceSpacings = includeSliceSpacing || frames.some(_ref8 => {
|
|
385
|
+
let sliceThickness = _ref8.sliceThickness;
|
|
386
|
+
return sliceThickness === undefined;
|
|
387
|
+
}) ? getSourceSliceSpacings(sourceImages, metadata, orientations, normals) : [];
|
|
388
|
+
const perFramePixelMeasures = applyLabelmapPixelMeasures(dataset, frames, frameOrientations, sliceSpacings, includeSliceSpacing);
|
|
389
|
+
dataset.PerFrameFunctionalGroupsSequence = [];
|
|
390
|
+
applyPerFrameFunctionalGroups(dataset, frames.map((_ref9, index) => {
|
|
391
|
+
let position = _ref9.position,
|
|
392
|
+
sourceImageSequenceItem = _ref9.sourceImageSequenceItem;
|
|
234
393
|
return {
|
|
235
394
|
sourceImageSequenceItem,
|
|
236
|
-
|
|
237
|
-
|
|
395
|
+
planePositionSequence: {
|
|
396
|
+
ImagePositionPatient: position
|
|
397
|
+
},
|
|
398
|
+
pixelMeasuresSequence: perFramePixelMeasures === null || perFramePixelMeasures === void 0 ? void 0 : perFramePixelMeasures[index],
|
|
399
|
+
frameContent: frameContents[index]
|
|
238
400
|
};
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
401
|
+
}));
|
|
402
|
+
setPlaneOrientations(dataset, frames.map(_ref0 => {
|
|
403
|
+
let orientation = _ref0.orientation;
|
|
404
|
+
return orientation;
|
|
405
|
+
}));
|
|
406
|
+
const sopInstanceUIDs = new Set(frames.map(_ref1 => {
|
|
407
|
+
let sourceImageSequenceItem = _ref1.sourceImageSequenceItem;
|
|
408
|
+
return sourceImageSequenceItem.ReferencedSOPInstanceUID;
|
|
409
|
+
}));
|
|
245
410
|
const referencedSeriesSequence = dataset.ReferencedSeriesSequence;
|
|
246
411
|
const referencedSeries = Array.isArray(referencedSeriesSequence) ? referencedSeriesSequence[0] : referencedSeriesSequence;
|
|
247
412
|
if (referencedSeries !== null && referencedSeries !== void 0 && referencedSeries.ReferencedInstanceSequence && sopInstanceUIDs.size) {
|
|
@@ -290,7 +455,7 @@ function generateSegmentation(images, labelmaps, metadata) {
|
|
|
290
455
|
const nonEmptyFrameIndices = collectNonEmptyFrameIndices(toLabelmap3DArray(labelmaps));
|
|
291
456
|
const filteredImages = resolveReferencedImagesForExport(images, nonEmptyFrameIndices);
|
|
292
457
|
const segmentation = _createMultiframeSegmentationFromReferencedImages(filteredImages, metadata, options);
|
|
293
|
-
const segmentationResult = fillLabelmapSegmentation(segmentation, labelmaps, metadata, filteredImages, options);
|
|
458
|
+
const segmentationResult = fillLabelmapSegmentation(segmentation, labelmaps, metadata, filteredImages, options, images.every(image => image === null || image === void 0 ? void 0 : image.imageId) ? images : []);
|
|
294
459
|
const predecessorImageId = options === null || options === void 0 ? void 0 : options.predecessorImageId;
|
|
295
460
|
if (predecessorImageId) {
|
|
296
461
|
const predecessor = metadata.get(MetadataModules.PREDECESSOR_SEQUENCE, predecessorImageId);
|
|
@@ -14,4 +14,11 @@ export function applyPerFrameFunctionalGroups(dataset: object, frames: Array<{
|
|
|
14
14
|
};
|
|
15
15
|
planeOrientationSequence?: object;
|
|
16
16
|
planePositionSequence?: object;
|
|
17
|
+
frameContent?: {
|
|
18
|
+
DimensionIndexValues: number[];
|
|
19
|
+
StackID?: string;
|
|
20
|
+
InStackPositionNumber?: number;
|
|
21
|
+
};
|
|
22
|
+
pixelMeasuresSequence?: object;
|
|
17
23
|
}>): void;
|
|
24
|
+
export function setPlaneOrientations(dataset: object, frameOrientations: Array<number[] | undefined>): void;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
1
2
|
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
2
3
|
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
|
|
3
|
-
import { Enums, utilities } from '@cornerstonejs/core';
|
|
4
|
+
import { Enums, utilities as utilities$1 } from '@cornerstonejs/core';
|
|
5
|
+
import { utilities } from '@cornerstonejs/metadata';
|
|
6
|
+
import { GEOMETRY_TOLERANCE } from './sourceFrame.js';
|
|
4
7
|
|
|
5
8
|
const _excluded = ["SegmentIdentificationSequence"];
|
|
6
9
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
@@ -27,7 +30,7 @@ function getReferencedFrameNumber(imageId, metadata) {
|
|
|
27
30
|
return undefined;
|
|
28
31
|
}
|
|
29
32
|
const providerFrame = metadata === null || metadata === void 0 || (_metadata$getFrameInf = metadata.getFrameInformationFromURL) === null || _metadata$getFrameInf === void 0 ? void 0 : _metadata$getFrameInf.call(metadata, imageId);
|
|
30
|
-
const frameNumber = providerFrame != null ? Number(providerFrame) : utilities.FrameRange.imageIdToFrameStart(imageId);
|
|
33
|
+
const frameNumber = providerFrame != null ? Number(providerFrame) : utilities$1.FrameRange.imageIdToFrameStart(imageId);
|
|
31
34
|
return Number.isFinite(frameNumber) ? frameNumber : undefined;
|
|
32
35
|
}
|
|
33
36
|
|
|
@@ -68,6 +71,8 @@ function normalizeSharedFunctionalGroupsSequence(dataset) {
|
|
|
68
71
|
* sourceImageSequenceItem: { ReferencedSOPInstanceUID: string, ReferencedFrameNumber?: number },
|
|
69
72
|
* planeOrientationSequence?: object,
|
|
70
73
|
* planePositionSequence?: object,
|
|
74
|
+
* frameContent?: { DimensionIndexValues: number[], StackID?: string, InStackPositionNumber?: number },
|
|
75
|
+
* pixelMeasuresSequence?: object,
|
|
71
76
|
* }>} frames
|
|
72
77
|
*
|
|
73
78
|
* `referencedSegmentNumber` drives the per-frame `SegmentIdentificationSequence`
|
|
@@ -75,6 +80,10 @@ function normalizeSharedFunctionalGroupsSequence(dataset) {
|
|
|
75
80
|
* single frame carries many segment labels as pixel values, so the standard
|
|
76
81
|
* forbids the macro and a fixed `ReferencedSegmentNumber` would be wrong for any
|
|
77
82
|
* label other than the one hard-coded.
|
|
83
|
+
*
|
|
84
|
+
* `frameContent` replaces the frame's `FrameContentSequence`. Callers that
|
|
85
|
+
* build the per-frame groups from scratch (LABELMAP SEGs) must pass it, since
|
|
86
|
+
* the Frame Content macro is mandatory for every Segmentation frame.
|
|
78
87
|
*/
|
|
79
88
|
function applyPerFrameFunctionalGroups(dataset, frames) {
|
|
80
89
|
normalizeSharedFunctionalGroupsSequence(dataset);
|
|
@@ -107,10 +116,62 @@ function applyPerFrameFunctionalGroups(dataset, frames) {
|
|
|
107
116
|
if (frame.planePositionSequence) {
|
|
108
117
|
group.PlanePositionSequence = frame.planePositionSequence;
|
|
109
118
|
}
|
|
119
|
+
if (frame.pixelMeasuresSequence) {
|
|
120
|
+
group.PixelMeasuresSequence = frame.pixelMeasuresSequence;
|
|
121
|
+
}
|
|
122
|
+
if (frame.frameContent) {
|
|
123
|
+
group.FrameContentSequence = frame.frameContent;
|
|
124
|
+
}
|
|
110
125
|
return group;
|
|
111
126
|
});
|
|
112
127
|
dataset.PerFrameFunctionalGroupsSequence = nextSequence;
|
|
113
128
|
dataset.NumberOfFrames = nextSequence.length;
|
|
114
129
|
}
|
|
115
130
|
|
|
116
|
-
|
|
131
|
+
/**
|
|
132
|
+
* Writes the PlaneOrientationSequence of each frame in the shared group or in
|
|
133
|
+
* the per-frame groups, never in both (PS3.3 C.7.6.16). When all frames have
|
|
134
|
+
* one orientation, within GEOMETRY_TOLERANCE, it goes in the shared group
|
|
135
|
+
* only. Otherwise each frame gets its own orientation in its per-frame group,
|
|
136
|
+
* and a frame without one keeps its current per-frame orientation, or else the
|
|
137
|
+
* current shared orientation. When no frame has a known orientation, the
|
|
138
|
+
* dataset does not change.
|
|
139
|
+
*
|
|
140
|
+
* Call it after the per-frame groups are built.
|
|
141
|
+
*
|
|
142
|
+
* @param {object} dataset - SEG dataset
|
|
143
|
+
* @param {Array<number[]|undefined>} frameOrientations - the
|
|
144
|
+
* ImageOrientationPatient of each frame, or undefined when it is not known
|
|
145
|
+
*/
|
|
146
|
+
function setPlaneOrientations(dataset, frameOrientations) {
|
|
147
|
+
var _dataset$PerFrameFunc;
|
|
148
|
+
normalizeSharedFunctionalGroupsSequence(dataset);
|
|
149
|
+
const shared = dataset.SharedFunctionalGroupsSequence;
|
|
150
|
+
const perFrame = (_dataset$PerFrameFunc = dataset.PerFrameFunctionalGroupsSequence) !== null && _dataset$PerFrameFunc !== void 0 ? _dataset$PerFrameFunc : [];
|
|
151
|
+
const _frameOrientations = _slicedToArray(frameOrientations, 1),
|
|
152
|
+
first = _frameOrientations[0];
|
|
153
|
+
if (!frameOrientations.some(Boolean)) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (first && frameOrientations.every(orientation => utilities.isEqual(orientation, first, GEOMETRY_TOLERANCE))) {
|
|
157
|
+
shared.PlaneOrientationSequence = {
|
|
158
|
+
ImageOrientationPatient: first
|
|
159
|
+
};
|
|
160
|
+
perFrame.forEach(group => delete group.PlaneOrientationSequence);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const sharedOrientation = shared.PlaneOrientationSequence;
|
|
164
|
+
delete shared.PlaneOrientationSequence;
|
|
165
|
+
perFrame.forEach((group, index) => {
|
|
166
|
+
const orientation = frameOrientations[index];
|
|
167
|
+
if (orientation) {
|
|
168
|
+
group.PlaneOrientationSequence = {
|
|
169
|
+
ImageOrientationPatient: orientation
|
|
170
|
+
};
|
|
171
|
+
} else if (!group.PlaneOrientationSequence && sharedOrientation) {
|
|
172
|
+
group.PlaneOrientationSequence = sharedOrientation;
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export { applyPerFrameFunctionalGroups, getReferencedFrameNumber, getReferencedSourceImageSequenceItem, normalizeSharedFunctionalGroupsSequence, setPlaneOrientations };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
declare const GEOMETRY_TOLERANCE = 0.001;
|
|
2
|
+
declare function toFiniteVector(values: any, length: number): number[] | undefined;
|
|
3
|
+
interface SourceFrame {
|
|
4
|
+
position?: number[];
|
|
5
|
+
orientation?: number[];
|
|
6
|
+
sliceThickness?: number;
|
|
7
|
+
pixelSpacing?: number[];
|
|
8
|
+
}
|
|
9
|
+
declare function getSourceFrame(image: {
|
|
10
|
+
imageId?: string;
|
|
11
|
+
} | undefined, metadata: any, withPixelMeasures?: boolean): SourceFrame;
|
|
12
|
+
declare const sliceNormal: (orientation: number[]) => number[];
|
|
13
|
+
export { GEOMETRY_TOLERANCE, getSourceFrame, sliceNormal, toFiniteVector };
|
|
14
|
+
export type { SourceFrame };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Enums } from '@cornerstonejs/core';
|
|
2
|
+
import { utilities } from '@cornerstonejs/metadata';
|
|
3
|
+
import { vec3 } from 'gl-matrix';
|
|
4
|
+
|
|
5
|
+
const isEqual = utilities.isEqual,
|
|
6
|
+
toFiniteNumber = utilities.toFiniteNumber;
|
|
7
|
+
const MetadataModules = Enums.MetadataModules;
|
|
8
|
+
const GEOMETRY_TOLERANCE = 1e-3;
|
|
9
|
+
const DEFAULT_POSITION = [0, 0, 0];
|
|
10
|
+
const DEFAULT_ORIENTATION = [1, 0, 0, 0, 1, 0];
|
|
11
|
+
const DEFAULT_PIXEL_SPACING = [1, 1];
|
|
12
|
+
function toFiniteVector(values, length) {
|
|
13
|
+
const numbers = toFiniteNumber(values);
|
|
14
|
+
return Array.isArray(numbers) && numbers.length === length && numbers.every(Number.isFinite) ? numbers : undefined;
|
|
15
|
+
}
|
|
16
|
+
function getSourceFrame(image, metadata) {
|
|
17
|
+
var _metadata$get, _metadata$get2, _toOrientation, _metadata$get3, _metadata$get4, _toFiniteVector;
|
|
18
|
+
let withPixelMeasures = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
19
|
+
const imageId = image === null || image === void 0 ? void 0 : image.imageId;
|
|
20
|
+
const plane = (_metadata$get = metadata === null || metadata === void 0 || (_metadata$get2 = metadata.get) === null || _metadata$get2 === void 0 ? void 0 : _metadata$get2.call(metadata, MetadataModules.IMAGE_PLANE, imageId)) !== null && _metadata$get !== void 0 ? _metadata$get : {};
|
|
21
|
+
const rowCosines = plane.rowCosines,
|
|
22
|
+
columnCosines = plane.columnCosines,
|
|
23
|
+
usingDefaultValues = plane.usingDefaultValues;
|
|
24
|
+
const notDefault = (value, defaultValue) => usingDefaultValues && isEqual(value, defaultValue) ? undefined : value;
|
|
25
|
+
const cosineFlags = [plane.isDefaultValueSetForRowCosine, plane.isDefaultValueSetForColumnCosine];
|
|
26
|
+
const planeOrientation = (_toOrientation = toOrientation(plane.imageOrientationPatient)) !== null && _toOrientation !== void 0 ? _toOrientation : rowCosines && columnCosines ? toOrientation([...rowCosines, ...columnCosines]) : undefined;
|
|
27
|
+
let position = notDefault(toFiniteVector(plane.imagePositionPatient, 3), DEFAULT_POSITION);
|
|
28
|
+
let orientation = cosineFlags.some(flag => flag !== undefined) ? cosineFlags.some(Boolean) ? undefined : planeOrientation : notDefault(planeOrientation, DEFAULT_ORIENTATION);
|
|
29
|
+
if (position && orientation && !withPixelMeasures) {
|
|
30
|
+
return {
|
|
31
|
+
position,
|
|
32
|
+
orientation
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const instance = (_metadata$get3 = metadata === null || metadata === void 0 || (_metadata$get4 = metadata.get) === null || _metadata$get4 === void 0 ? void 0 : _metadata$get4.call(metadata, MetadataModules.IMAGE_DATA, imageId)) !== null && _metadata$get3 !== void 0 ? _metadata$get3 : {};
|
|
36
|
+
position !== null && position !== void 0 ? position : position = toFiniteVector(instance.ImagePositionPatient, 3);
|
|
37
|
+
orientation !== null && orientation !== void 0 ? orientation : orientation = toOrientation(instance.ImageOrientationPatient);
|
|
38
|
+
if (!withPixelMeasures) {
|
|
39
|
+
return {
|
|
40
|
+
position,
|
|
41
|
+
orientation
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
position,
|
|
46
|
+
orientation,
|
|
47
|
+
sliceThickness: [plane.sliceThickness, instance.SliceThickness].map(Number).find(thickness => thickness > 0),
|
|
48
|
+
pixelSpacing: (_toFiniteVector = toFiniteVector(instance.PixelSpacing, 2)) !== null && _toFiniteVector !== void 0 ? _toFiniteVector : notDefault(toFiniteVector(plane.pixelSpacing, 2), DEFAULT_PIXEL_SPACING)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const sliceNormal = orientation => {
|
|
52
|
+
const normal = vec3.cross([0, 0, 0], orientation.slice(0, 3), orientation.slice(3, 6));
|
|
53
|
+
return vec3.normalize(normal, normal);
|
|
54
|
+
};
|
|
55
|
+
function toOrientation(values) {
|
|
56
|
+
const orientation = toFiniteVector(values, 6);
|
|
57
|
+
return orientation && vec3.length(sliceNormal(orientation)) > GEOMETRY_TOLERANCE ? orientation : undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { GEOMETRY_TOLERANCE, getSourceFrame, sliceNormal, toFiniteVector };
|
package/dist/esm/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "5.12.
|
|
1
|
+
export declare const version = "5.12.1";
|
package/dist/esm/version.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cornerstonejs/adapters",
|
|
3
|
-
"version": "5.12.
|
|
3
|
+
"version": "5.12.1",
|
|
4
4
|
"description": "Adapters for Cornerstone3D to/from formats including DICOM SR and others",
|
|
5
5
|
"module": "./dist/esm/index.js",
|
|
6
6
|
"types": "./dist/esm/index.d.ts",
|
|
@@ -99,13 +99,13 @@
|
|
|
99
99
|
"ndarray": "1.0.19"
|
|
100
100
|
},
|
|
101
101
|
"devDependencies": {
|
|
102
|
-
"@cornerstonejs/core": "5.12.
|
|
103
|
-
"@cornerstonejs/metadata": "5.12.
|
|
104
|
-
"@cornerstonejs/tools": "5.12.
|
|
102
|
+
"@cornerstonejs/core": "5.12.1",
|
|
103
|
+
"@cornerstonejs/metadata": "5.12.1",
|
|
104
|
+
"@cornerstonejs/tools": "5.12.1"
|
|
105
105
|
},
|
|
106
106
|
"peerDependencies": {
|
|
107
|
-
"@cornerstonejs/core": "5.12.
|
|
108
|
-
"@cornerstonejs/metadata": "5.12.
|
|
109
|
-
"@cornerstonejs/tools": "5.12.
|
|
107
|
+
"@cornerstonejs/core": "5.12.1",
|
|
108
|
+
"@cornerstonejs/metadata": "5.12.1",
|
|
109
|
+
"@cornerstonejs/tools": "5.12.1"
|
|
110
110
|
}
|
|
111
111
|
}
|