@cornerstonejs/tools 3.5.3 → 3.6.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.
Files changed (45) hide show
  1. package/dist/esm/config.d.ts +7 -0
  2. package/dist/esm/enums/WorkerTypes.d.ts +2 -1
  3. package/dist/esm/enums/WorkerTypes.js +1 -0
  4. package/dist/esm/index.d.ts +2 -2
  5. package/dist/esm/index.js +2 -2
  6. package/dist/esm/tools/annotation/BidirectionalTool.d.ts +3 -0
  7. package/dist/esm/tools/annotation/BidirectionalTool.js +39 -1
  8. package/dist/esm/tools/index.d.ts +2 -1
  9. package/dist/esm/tools/index.js +2 -1
  10. package/dist/esm/tools/segmentation/SegmentBidirectionalTool.d.ts +27 -0
  11. package/dist/esm/tools/segmentation/SegmentBidirectionalTool.js +253 -0
  12. package/dist/esm/tools/segmentation/strategies/compositions/ensureImageVolume.js +5 -8
  13. package/dist/esm/tools/segmentation/strategies/utils/getStrategyData.js +5 -10
  14. package/dist/esm/types/CalculatorTypes.d.ts +21 -3
  15. package/dist/esm/types/ToolSpecificAnnotationTypes.d.ts +34 -0
  16. package/dist/esm/utilities/contours/generateContourSetsFromLabelmap.js +1 -0
  17. package/dist/esm/utilities/math/basic/BasicStatsCalculator.js +88 -13
  18. package/dist/esm/utilities/registerComputeWorker.js +4 -1
  19. package/dist/esm/utilities/segmentation/VolumetricCalculator.js +12 -2
  20. package/dist/esm/utilities/segmentation/computeMetabolicStats.d.ts +8 -0
  21. package/dist/esm/utilities/segmentation/computeMetabolicStats.js +58 -0
  22. package/dist/esm/utilities/segmentation/contourAndFindLargestBidirectional.js +7 -5
  23. package/dist/esm/utilities/segmentation/createMergedLabelmapForIndex.js +10 -2
  24. package/dist/esm/utilities/segmentation/findLargestBidirectional.d.ts +5 -0
  25. package/dist/esm/utilities/segmentation/findLargestBidirectional.js +1 -1
  26. package/dist/esm/utilities/segmentation/getOrCreateImageVolume.d.ts +3 -0
  27. package/dist/esm/utilities/segmentation/getOrCreateImageVolume.js +18 -0
  28. package/dist/esm/utilities/segmentation/getOrCreateSegmentationVolume.d.ts +2 -1
  29. package/dist/esm/utilities/segmentation/getOrCreateSegmentationVolume.js +5 -1
  30. package/dist/esm/utilities/segmentation/getReferenceVolumeForSegmentation.d.ts +1 -0
  31. package/dist/esm/utilities/segmentation/getReferenceVolumeForSegmentation.js +34 -0
  32. package/dist/esm/utilities/segmentation/getReferenceVolumeForSegmentationVolume.d.ts +1 -0
  33. package/dist/esm/utilities/segmentation/getReferenceVolumeForSegmentationVolume.js +20 -0
  34. package/dist/esm/utilities/segmentation/getSegmentLargestBidirectional.d.ts +5 -0
  35. package/dist/esm/utilities/segmentation/getSegmentLargestBidirectional.js +54 -0
  36. package/dist/esm/utilities/segmentation/getStatistics.js +50 -108
  37. package/dist/esm/utilities/segmentation/index.d.ts +5 -1
  38. package/dist/esm/utilities/segmentation/index.js +5 -1
  39. package/dist/esm/utilities/segmentation/isLineInSegment.d.ts +13 -1
  40. package/dist/esm/utilities/segmentation/isLineInSegment.js +20 -12
  41. package/dist/esm/utilities/segmentation/segmentContourAction.js +1 -0
  42. package/dist/esm/utilities/segmentation/utilsForWorker.d.ts +38 -0
  43. package/dist/esm/utilities/segmentation/utilsForWorker.js +125 -0
  44. package/dist/esm/workers/computeWorker.js +336 -38
  45. package/package.json +4 -4
@@ -1,27 +1,53 @@
1
1
  import { expose } from 'comlink';
2
2
  import { utilities } from '@cornerstonejs/core';
3
3
  import SegmentStatsCalculator from '../utilities/segmentation/SegmentStatsCalculator';
4
+ import { getSegmentLargestBidirectional } from '../utilities/segmentation';
5
+ import vtkImageMarchingSquares from '@kitware/vtk.js/Filters/General/ImageMarchingSquares';
6
+ import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
7
+ import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
8
+ import { getDeduplicatedVTKPolyDataPoints } from '../utilities/contours/getDeduplicatedVTKPolyDataPoints';
9
+ import { findContoursFromReducedSet } from '../utilities/contours/contourFinder';
10
+ import { createBidirectionalForSlice } from '../utilities/segmentation/findLargestBidirectional';
11
+ import { createIsInSegmentMetadata } from '../utilities/segmentation/isLineInSegment';
4
12
  const { VoxelManager } = utilities;
5
13
  const computeWorker = {
6
- calculateSegmentsStatisticsVolume: (args) => {
7
- const { segmentationInfo, imageInfo, indices, mode } = args;
8
- const { scalarData: segmentationScalarData, dimensions: segmentationDimensions, spacing: segmentationSpacing, } = segmentationInfo;
9
- const { scalarData: imageScalarData, dimensions: imageDimensions } = imageInfo;
10
- if (segmentationDimensions[0] !== imageDimensions[0] ||
11
- segmentationDimensions[1] !== imageDimensions[1] ||
12
- segmentationDimensions[2] !== imageDimensions[2]) {
13
- throw new Error('Dimensions do not match to calculate statistics, different dimensions not supported yet');
14
- }
15
- const segVoxelManager = VoxelManager.createScalarVolumeVoxelManager({
16
- dimensions: segmentationDimensions,
17
- scalarData: segmentationScalarData,
14
+ createVoxelManager: (dimensions, scalarData) => {
15
+ return VoxelManager.createScalarVolumeVoxelManager({
16
+ dimensions,
17
+ scalarData,
18
18
  });
19
- const imageVoxelManager = VoxelManager.createScalarVolumeVoxelManager({
20
- dimensions: imageDimensions,
21
- scalarData: imageScalarData,
19
+ },
20
+ createDataStructure: (info) => {
21
+ const { scalarData, dimensions, spacing, origin, direction } = info;
22
+ const voxelManager = computeWorker.createVoxelManager(dimensions, scalarData);
23
+ return {
24
+ voxelManager,
25
+ dimensions,
26
+ spacing,
27
+ origin,
28
+ direction,
29
+ scalarData,
30
+ };
31
+ },
32
+ createVTKImageData: (dimensions, origin, direction, spacing, scalarData) => {
33
+ const imageData = vtkImageData.newInstance();
34
+ imageData.setDimensions(dimensions);
35
+ imageData.setOrigin(origin);
36
+ imageData.setDirection(direction);
37
+ imageData.setSpacing(spacing);
38
+ if (!scalarData) {
39
+ return imageData;
40
+ }
41
+ const scalarArray = vtkDataArray.newInstance({
42
+ name: 'Scalars',
43
+ numberOfComponents: 1,
44
+ values: scalarData,
22
45
  });
23
- SegmentStatsCalculator.statsInit({ storePointData: false, indices, mode });
24
- segVoxelManager.forEach(({ value, pointIJK, index }) => {
46
+ imageData.getPointData().setScalars(scalarArray);
47
+ return imageData;
48
+ },
49
+ processSegmentStatistics: ({ segVoxelManager, imageVoxelManager, indices, bounds, imageData, }) => {
50
+ segVoxelManager.forEach(({ value, pointIJK, pointLPS, index }) => {
25
51
  if (indices.indexOf(value) === -1) {
26
52
  return;
27
53
  }
@@ -30,9 +56,66 @@ const computeWorker = {
30
56
  segmentIndex: value,
31
57
  value: imageValue,
32
58
  pointIJK,
59
+ pointLPS,
33
60
  });
34
61
  }, {
35
- boundsIJK: imageVoxelManager.getDefaultBounds(),
62
+ boundsIJK: bounds || imageVoxelManager.getDefaultBounds(),
63
+ imageData,
64
+ });
65
+ },
66
+ performMarchingSquares: (imageData, sliceIndex = null, slicingMode = null) => {
67
+ const options = {};
68
+ if (sliceIndex !== null) {
69
+ options.slice = sliceIndex;
70
+ }
71
+ if (slicingMode !== null) {
72
+ options.slicingMode = slicingMode;
73
+ }
74
+ const mSquares = vtkImageMarchingSquares.newInstance(options);
75
+ mSquares.setInputData(imageData);
76
+ mSquares.setContourValues([1]);
77
+ mSquares.setMergePoints(false);
78
+ return mSquares.getOutputData();
79
+ },
80
+ createContoursFromPolyData: (msOutput, sliceIndex = null) => {
81
+ const reducedSet = getDeduplicatedVTKPolyDataPoints(msOutput);
82
+ if (reducedSet.points?.length) {
83
+ const contours = findContoursFromReducedSet(reducedSet.lines);
84
+ return {
85
+ contours,
86
+ polyData: reducedSet,
87
+ };
88
+ }
89
+ return null;
90
+ },
91
+ createSegmentsFromIndices: (indices) => {
92
+ return [null, ...indices.map((index) => ({ segmentIndex: index }))];
93
+ },
94
+ getArgsFromInfo: (args) => {
95
+ const { segmentationInfo, imageInfo } = args;
96
+ const getSegmentationData = () => {
97
+ return computeWorker.createDataStructure(segmentationInfo);
98
+ };
99
+ const getImageData = () => {
100
+ return computeWorker.createDataStructure(imageInfo);
101
+ };
102
+ return {
103
+ segmentation: segmentationInfo && getSegmentationData(),
104
+ image: imageInfo && getImageData(),
105
+ };
106
+ },
107
+ calculateSegmentsStatisticsVolume: (args) => {
108
+ const { mode, indices } = args;
109
+ const { segmentation, image } = computeWorker.getArgsFromInfo(args);
110
+ const { voxelManager: segVoxelManager, spacing: segmentationSpacing } = segmentation;
111
+ const { voxelManager: imageVoxelManager } = image;
112
+ const imageData = computeWorker.createVTKImageData(segmentation.dimensions, segmentation.origin, segmentation.direction, segmentation.spacing);
113
+ SegmentStatsCalculator.statsInit({ storePointData: false, indices, mode });
114
+ computeWorker.processSegmentStatistics({
115
+ segVoxelManager,
116
+ imageVoxelManager,
117
+ indices,
118
+ imageData,
36
119
  });
37
120
  const stats = SegmentStatsCalculator.getStatistics({
38
121
  spacing: segmentationSpacing,
@@ -41,6 +124,33 @@ const computeWorker = {
41
124
  });
42
125
  return stats;
43
126
  },
127
+ computeMetabolicStats({ segmentationInfo, imageInfo }) {
128
+ const { scalarData, dimensions, spacing, origin, direction } = segmentationInfo;
129
+ const { spacing: imageSpacing, dimensions: imageDimensions, direction: imageDirection, origin: imageOrigin, scalarData: imageScalarData, } = imageInfo;
130
+ const segVoxelManager = computeWorker.createVoxelManager(segmentationInfo.dimensions, segmentationInfo.scalarData);
131
+ const refVoxelManager = computeWorker.createVoxelManager(imageDimensions, imageScalarData);
132
+ let suv = 0;
133
+ let numVoxels = 0;
134
+ const scalarDataLength = segVoxelManager.getScalarDataLength();
135
+ for (let i = 0; i < scalarDataLength; i++) {
136
+ if (segVoxelManager.getAtIndex(i) !== 0) {
137
+ suv += refVoxelManager.getAtIndex(i);
138
+ numVoxels++;
139
+ }
140
+ }
141
+ const tmtv = 1e-3 * numVoxels * spacing[0] * spacing[1] * spacing[2];
142
+ const averageSuv = numVoxels > 0 ? suv / numVoxels : 0;
143
+ const tlg = averageSuv *
144
+ numVoxels *
145
+ imageSpacing[0] *
146
+ imageSpacing[1] *
147
+ imageSpacing[2] *
148
+ 1e-3;
149
+ return {
150
+ tmtv,
151
+ tlg,
152
+ };
153
+ },
44
154
  calculateSegmentsStatisticsStack: (args) => {
45
155
  const { segmentationInfo, imageInfo, indices, mode } = args;
46
156
  SegmentStatsCalculator.statsInit({ storePointData: true, indices, mode });
@@ -52,26 +162,14 @@ const computeWorker = {
52
162
  segInfo.dimensions[1],
53
163
  1,
54
164
  ];
55
- const segVoxelManager = VoxelManager.createScalarVolumeVoxelManager({
56
- dimensions: segDimensions,
57
- scalarData: segInfo.scalarData,
58
- });
59
- const imageVoxelManager = VoxelManager.createScalarVolumeVoxelManager({
60
- dimensions: segDimensions,
61
- scalarData: imgInfo.scalarData,
62
- });
63
- segVoxelManager.forEach(({ value, pointIJK, index }) => {
64
- if (indices.indexOf(value) === -1) {
65
- return;
66
- }
67
- const imageValue = imageVoxelManager.getAtIndex(index);
68
- SegmentStatsCalculator.statsCallback({
69
- segmentIndex: value,
70
- value: imageValue,
71
- pointIJK,
72
- });
73
- }, {
74
- boundsIJK: imageVoxelManager.getDefaultBounds(),
165
+ const segVoxelManager = computeWorker.createVoxelManager(segDimensions, segInfo.scalarData);
166
+ const imageVoxelManager = computeWorker.createVoxelManager(segDimensions, imgInfo.scalarData);
167
+ const imageData = computeWorker.createVTKImageData(segDimensions, segInfo.origin, segInfo.direction, segInfo.spacing);
168
+ computeWorker.processSegmentStatistics({
169
+ segVoxelManager,
170
+ imageVoxelManager,
171
+ indices,
172
+ imageData,
75
173
  });
76
174
  }
77
175
  const spacing = segmentationInfo[0].spacing;
@@ -81,5 +179,205 @@ const computeWorker = {
81
179
  });
82
180
  return stats;
83
181
  },
182
+ getSegmentLargestBidirectionalInternal: (args) => {
183
+ const { segmentationInfo, imageInfo, indices, mode, isStack } = args;
184
+ let segmentation;
185
+ if (!isStack) {
186
+ ({ segmentation } = computeWorker.getArgsFromInfo(args));
187
+ }
188
+ else {
189
+ ({ segmentation } = computeWorker.getArgsFromInfo({
190
+ segmentationInfo: segmentationInfo[0],
191
+ }));
192
+ }
193
+ return isStack
194
+ ? computeWorker.calculateBidirectionalStack({
195
+ segmentationInfo,
196
+ indices,
197
+ mode,
198
+ })
199
+ : computeWorker.calculateVolumetricBidirectional({
200
+ segmentation,
201
+ indices,
202
+ mode,
203
+ });
204
+ },
205
+ findLargestBidirectionalFromContours: (contours, isInSegment, segmentIndex) => {
206
+ let maxBidirectional;
207
+ for (const sliceContour of contours) {
208
+ const bidirectional = createBidirectionalForSlice(sliceContour, isInSegment, maxBidirectional);
209
+ if (!bidirectional) {
210
+ continue;
211
+ }
212
+ maxBidirectional = bidirectional;
213
+ }
214
+ if (maxBidirectional) {
215
+ return {
216
+ segmentIndex,
217
+ majorAxis: maxBidirectional.majorAxis,
218
+ minorAxis: maxBidirectional.minorAxis,
219
+ maxMajor: maxBidirectional.maxMajor,
220
+ maxMinor: maxBidirectional.maxMinor,
221
+ };
222
+ }
223
+ return null;
224
+ },
225
+ calculateBidirectionalStack: ({ segmentationInfo, indices, mode }) => {
226
+ const segments = computeWorker.createSegmentsFromIndices(indices);
227
+ let bidirectionalResults = [];
228
+ for (let i = 0; i < segmentationInfo.length; i++) {
229
+ const segInfo = segmentationInfo[i];
230
+ const dimensions = segInfo.dimensions;
231
+ const segScalarData = segInfo.scalarData;
232
+ const { spacing, direction, origin } = segInfo;
233
+ const voxelManager = computeWorker.createVoxelManager(dimensions, segScalarData);
234
+ const pixelsPerSlice = dimensions[0] * dimensions[1];
235
+ for (let segIndex = 1; segIndex < segments.length; segIndex++) {
236
+ const segment = segments[segIndex];
237
+ if (!segment) {
238
+ continue;
239
+ }
240
+ const segmentIndex = segment.segmentIndex;
241
+ if (computeWorker.isSliceEmptyForSegmentVolume(0, segScalarData, pixelsPerSlice, segmentIndex)) {
242
+ continue;
243
+ }
244
+ const sliceContours = [];
245
+ const filteredData = new Uint8Array(segScalarData.length);
246
+ for (let i = 0; i < segScalarData.length; i++) {
247
+ filteredData[i] = segScalarData[i] === segmentIndex ? 1 : 0;
248
+ }
249
+ const scalarArray = vtkDataArray.newInstance({
250
+ name: 'Pixels',
251
+ numberOfComponents: 1,
252
+ values: filteredData,
253
+ });
254
+ const imageData = computeWorker.createVTKImageData(dimensions, origin, direction, [spacing[0], spacing[1], 1]);
255
+ imageData.getPointData().setScalars(scalarArray);
256
+ try {
257
+ const msOutput = computeWorker.performMarchingSquares(imageData, null, 2);
258
+ const contourData = computeWorker.createContoursFromPolyData(msOutput);
259
+ if (contourData) {
260
+ sliceContours.push(contourData);
261
+ }
262
+ }
263
+ catch (e) {
264
+ console.warn(e);
265
+ }
266
+ const isInSegment = createIsInSegmentMetadata({
267
+ dimensions,
268
+ imageData,
269
+ voxelManager,
270
+ segmentIndex,
271
+ });
272
+ const bidirectionalResult = computeWorker.findLargestBidirectionalFromContours(sliceContours, isInSegment, segmentIndex);
273
+ if (bidirectionalResult) {
274
+ bidirectionalResults.push(bidirectionalResult);
275
+ }
276
+ }
277
+ }
278
+ return bidirectionalResults;
279
+ },
280
+ calculateVolumetricBidirectional: ({ segmentation, indices, mode }) => {
281
+ const { voxelManager, dimensions, origin, direction, spacing } = segmentation;
282
+ const imageData = computeWorker.createVTKImageData(dimensions, origin, direction, spacing);
283
+ const contourSets = computeWorker.generateContourSetsFromLabelmapVolume({
284
+ segmentation,
285
+ indices,
286
+ imageData,
287
+ mode,
288
+ });
289
+ const bidirectionalResults = [];
290
+ for (let i = 0; i < contourSets.length; i++) {
291
+ const contourSet = contourSets[i];
292
+ const { segmentIndex } = contourSet.segment;
293
+ const contours = contourSet.sliceContours;
294
+ const isInSegment = createIsInSegmentMetadata({
295
+ dimensions,
296
+ imageData,
297
+ voxelManager,
298
+ segmentIndex,
299
+ });
300
+ const bidirectionalResult = computeWorker.findLargestBidirectionalFromContours(contours, isInSegment, segmentIndex);
301
+ if (bidirectionalResult) {
302
+ bidirectionalResults.push(bidirectionalResult);
303
+ }
304
+ }
305
+ return bidirectionalResults;
306
+ },
307
+ generateContourSetsFromLabelmapVolume: (args) => {
308
+ const { segmentation, indices, imageData } = args;
309
+ const { voxelManager, dimensions, scalarData, origin, direction, spacing } = segmentation;
310
+ const numSlices = dimensions[2];
311
+ const pixelsPerSlice = dimensions[0] * dimensions[1];
312
+ const segments = computeWorker.createSegmentsFromIndices(indices);
313
+ for (let z = 0; z < numSlices; z++) {
314
+ for (let y = 0; y < dimensions[1]; y++) {
315
+ const index = y * dimensions[0] + z * pixelsPerSlice;
316
+ scalarData[index] = 0;
317
+ scalarData[index + dimensions[0] - 1] = 0;
318
+ }
319
+ }
320
+ const ContourSets = [];
321
+ const numSegments = segments.length;
322
+ for (let segIndex = 0; segIndex < numSegments; segIndex++) {
323
+ const segment = segments[segIndex];
324
+ if (!segment) {
325
+ continue;
326
+ }
327
+ const segmentIndex = segment.segmentIndex;
328
+ const sliceContours = [];
329
+ const scalars = vtkDataArray.newInstance({
330
+ name: 'Scalars',
331
+ numberOfComponents: 1,
332
+ size: pixelsPerSlice * numSlices,
333
+ dataType: 'Uint8Array',
334
+ });
335
+ for (let sliceIndex = 0; sliceIndex < numSlices; sliceIndex++) {
336
+ if (computeWorker.isSliceEmptyForSegmentVolume(sliceIndex, scalarData, pixelsPerSlice, segmentIndex)) {
337
+ continue;
338
+ }
339
+ const frameStart = sliceIndex * pixelsPerSlice;
340
+ try {
341
+ for (let i = 0; i < pixelsPerSlice; i++) {
342
+ const value = scalarData[i + frameStart];
343
+ if (value === segmentIndex) {
344
+ scalars.setValue(i + frameStart, 1);
345
+ }
346
+ else {
347
+ scalars.setValue(i, 0);
348
+ }
349
+ }
350
+ const imageDataCopy = vtkImageData.newInstance();
351
+ imageDataCopy.shallowCopy(imageData);
352
+ imageDataCopy.getPointData().setScalars(scalars);
353
+ const msOutput = computeWorker.performMarchingSquares(imageDataCopy, sliceIndex);
354
+ const contourData = computeWorker.createContoursFromPolyData(msOutput, sliceIndex);
355
+ if (contourData) {
356
+ sliceContours.push(contourData);
357
+ }
358
+ }
359
+ catch (e) {
360
+ console.warn(sliceIndex);
361
+ console.warn(e);
362
+ }
363
+ }
364
+ const ContourSet = {
365
+ sliceContours,
366
+ segment,
367
+ };
368
+ ContourSets.push(ContourSet);
369
+ }
370
+ return ContourSets;
371
+ },
372
+ isSliceEmptyForSegmentVolume: (sliceIndex, segData, pixelsPerSlice, segIndex) => {
373
+ const startIdx = sliceIndex * pixelsPerSlice;
374
+ const endIdx = startIdx + pixelsPerSlice;
375
+ for (let i = startIdx; i < endIdx; i++) {
376
+ if (segData[i] === segIndex) {
377
+ return false;
378
+ }
379
+ }
380
+ return true;
381
+ },
84
382
  };
85
383
  expose(computeWorker);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cornerstonejs/tools",
3
- "version": "3.5.3",
3
+ "version": "3.6.1",
4
4
  "description": "Cornerstone3D Tools",
5
5
  "types": "./dist/esm/index.d.ts",
6
6
  "module": "./dist/esm/index.js",
@@ -103,8 +103,8 @@
103
103
  "canvas": "^2.11.2"
104
104
  },
105
105
  "peerDependencies": {
106
- "@cornerstonejs/core": "^3.5.3",
107
- "@kitware/vtk.js": "32.12.0",
106
+ "@cornerstonejs/core": "^3.6.1",
107
+ "@kitware/vtk.js": "32.12.1",
108
108
  "@types/d3-array": "^3.0.4",
109
109
  "@types/d3-interpolate": "^3.0.1",
110
110
  "d3-array": "^3.2.3",
@@ -122,5 +122,5 @@
122
122
  "type": "individual",
123
123
  "url": "https://ohif.org/donate"
124
124
  },
125
- "gitHead": "42ffdeabf1ed0bd2871db808bf198d5e3f4f4c94"
125
+ "gitHead": "a262fd519f1ceabe2e1d1d01a382087920657f65"
126
126
  }