@cornerstonejs/tools 1.19.0 → 1.19.2

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 (43) hide show
  1. package/dist/cjs/tools/annotation/PlanarFreehandROITool.js +14 -1
  2. package/dist/cjs/tools/annotation/PlanarFreehandROITool.js.map +1 -1
  3. package/dist/cjs/utilities/index.d.ts +2 -2
  4. package/dist/cjs/utilities/index.js +4 -3
  5. package/dist/cjs/utilities/index.js.map +1 -1
  6. package/dist/cjs/utilities/stackPrefetch/index.d.ts +3 -2
  7. package/dist/cjs/utilities/stackPrefetch/index.js +8 -6
  8. package/dist/cjs/utilities/stackPrefetch/index.js.map +1 -1
  9. package/dist/cjs/utilities/stackPrefetch/stackContextPrefetch.d.ts +16 -0
  10. package/dist/cjs/utilities/stackPrefetch/stackContextPrefetch.js +227 -0
  11. package/dist/cjs/utilities/stackPrefetch/stackContextPrefetch.js.map +1 -0
  12. package/dist/cjs/utilities/stackPrefetch/stackPrefetch.d.ts +7 -1
  13. package/dist/cjs/utilities/stackPrefetch/stackPrefetch.js +23 -93
  14. package/dist/cjs/utilities/stackPrefetch/stackPrefetch.js.map +1 -1
  15. package/dist/cjs/utilities/stackPrefetch/stackPrefetchUtils.d.ts +14 -0
  16. package/dist/cjs/utilities/stackPrefetch/stackPrefetchUtils.js +85 -0
  17. package/dist/cjs/utilities/stackPrefetch/stackPrefetchUtils.js.map +1 -0
  18. package/dist/esm/tools/annotation/PlanarFreehandROITool.js +14 -1
  19. package/dist/esm/tools/annotation/PlanarFreehandROITool.js.map +1 -1
  20. package/dist/esm/utilities/index.d.ts +2 -2
  21. package/dist/esm/utilities/index.js +2 -2
  22. package/dist/esm/utilities/index.js.map +1 -1
  23. package/dist/esm/utilities/stackPrefetch/index.d.ts +3 -2
  24. package/dist/esm/utilities/stackPrefetch/index.js +3 -2
  25. package/dist/esm/utilities/stackPrefetch/index.js.map +1 -1
  26. package/dist/esm/utilities/stackPrefetch/stackContextPrefetch.d.ts +16 -0
  27. package/dist/esm/utilities/stackPrefetch/stackContextPrefetch.js +220 -0
  28. package/dist/esm/utilities/stackPrefetch/stackContextPrefetch.js.map +1 -0
  29. package/dist/esm/utilities/stackPrefetch/stackPrefetch.d.ts +7 -1
  30. package/dist/esm/utilities/stackPrefetch/stackPrefetch.js +12 -79
  31. package/dist/esm/utilities/stackPrefetch/stackPrefetch.js.map +1 -1
  32. package/dist/esm/utilities/stackPrefetch/stackPrefetchUtils.d.ts +14 -0
  33. package/dist/esm/utilities/stackPrefetch/stackPrefetchUtils.js +77 -0
  34. package/dist/esm/utilities/stackPrefetch/stackPrefetchUtils.js.map +1 -0
  35. package/dist/umd/index.js +1 -1
  36. package/dist/umd/index.js.map +1 -1
  37. package/package.json +3 -3
  38. package/src/tools/annotation/PlanarFreehandROITool.ts +22 -4
  39. package/src/utilities/index.ts +2 -1
  40. package/src/utilities/stackPrefetch/index.ts +3 -7
  41. package/src/utilities/stackPrefetch/stackContextPrefetch.ts +380 -0
  42. package/src/utilities/stackPrefetch/stackPrefetch.ts +29 -151
  43. package/src/utilities/stackPrefetch/stackPrefetchUtils.ts +114 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cornerstonejs/tools",
3
- "version": "1.19.0",
3
+ "version": "1.19.2",
4
4
  "description": "Cornerstone3D Tools",
5
5
  "main": "dist/umd/index.js",
6
6
  "types": "dist/esm/index.d.ts",
@@ -29,7 +29,7 @@
29
29
  "webpack:watch": "webpack --mode development --progress --watch --config ./.webpack/webpack.dev.js"
30
30
  },
31
31
  "dependencies": {
32
- "@cornerstonejs/core": "^1.19.0",
32
+ "@cornerstonejs/core": "^1.19.2",
33
33
  "lodash.clonedeep": "4.5.0",
34
34
  "lodash.get": "^4.4.2"
35
35
  },
@@ -52,5 +52,5 @@
52
52
  "type": "individual",
53
53
  "url": "https://ohif.org/donate"
54
54
  },
55
- "gitHead": "4caa1d79544a3076d89de80d02fbb9a18c1ad186"
55
+ "gitHead": "b2e964e0191802b363088ad264401e90513d88c3"
56
56
  }
@@ -44,14 +44,13 @@ import {
44
44
  AnnotationStyle,
45
45
  PublicToolProps,
46
46
  ToolProps,
47
- InteractionTypes,
48
47
  SVGDrawingHelper,
49
48
  } from '../../types';
50
- import { drawLine, drawCircle, drawLinkedTextBox } from '../../drawingSvg';
49
+ import { drawLinkedTextBox } from '../../drawingSvg';
51
50
  import { PlanarFreehandROIAnnotation } from '../../types/ToolSpecificAnnotationTypes';
52
51
  import { getTextBoxCoordsCanvas } from '../../utilities/drawing';
53
52
  import { PlanarFreehandROICommonData } from '../../utilities/math/polyline/planarFreehandROIInternalTypes';
54
- import pointInPolyline from '../../utilities/math/polyline/pointInPolyline';
53
+
55
54
  import { getIntersectionCoordinatesWithPolyline } from '../../utilities/math/polyline/getIntersectionWithPolyline';
56
55
  import pointInShapeCallback from '../../utilities/pointInShapeCallback';
57
56
  import { isViewportPreScaled } from '../../utilities/viewport/isViewportPreScaled';
@@ -752,9 +751,28 @@ class PlanarFreehandROITool extends AnnotationTool {
752
751
 
753
752
  const { imageData, metadata } = image;
754
753
  const canvasCoordinates = points.map((p) => viewport.worldToCanvas(p));
754
+
755
+ // Using an arbitrary start point (canvasPoint), calculate the
756
+ // mm spacing for the canvas in the X and Y directions.
757
+ const canvasPoint = canvasCoordinates[0];
758
+ const originalWorldPoint = viewport.canvasToWorld(canvasPoint);
759
+ const deltaXPoint = viewport.canvasToWorld([
760
+ canvasPoint[0] + 1,
761
+ canvasPoint[1],
762
+ ]);
763
+ const deltaYPoint = viewport.canvasToWorld([
764
+ canvasPoint[0],
765
+ canvasPoint[1] + 1,
766
+ ]);
767
+
768
+ const deltaInX = vec3.distance(originalWorldPoint, deltaXPoint);
769
+ const deltaInY = vec3.distance(originalWorldPoint, deltaYPoint);
770
+
755
771
  const scale = getCalibratedScale(image);
756
- const area =
772
+ let area =
757
773
  polyline.calculateAreaOfPoints(canvasCoordinates) / scale / scale;
774
+ // Convert from canvas_pixels ^2 to mm^2
775
+ area *= deltaInX * deltaInY;
758
776
 
759
777
  const worldPosIndex = csUtils.transformWorldToIndex(imageData, points[0]);
760
778
  worldPosIndex[0] = Math.floor(worldPosIndex[0]);
@@ -29,7 +29,7 @@ import * as cine from './cine';
29
29
  import * as boundingBox from './boundingBox';
30
30
  import * as planarFreehandROITool from './planarFreehandROITool';
31
31
  import * as rectangleROITool from './rectangleROITool';
32
- import * as stackPrefetch from './stackPrefetch';
32
+ import { stackPrefetch, stackContextPrefetch } from './stackPrefetch';
33
33
  import * as viewport from './viewport';
34
34
  import * as touch from './touch';
35
35
  import * as dynamicVolume from './dynamicVolume';
@@ -65,6 +65,7 @@ export {
65
65
  rectangleROITool,
66
66
  planarFreehandROITool,
67
67
  stackPrefetch,
68
+ stackContextPrefetch,
68
69
  scroll,
69
70
  roundNumber,
70
71
  };
@@ -1,8 +1,4 @@
1
- import {
2
- enable,
3
- disable,
4
- setConfiguration,
5
- getConfiguration,
6
- } from './stackPrefetch';
1
+ import stackPrefetch from './stackPrefetch';
2
+ import stackContextPrefetch from './stackContextPrefetch';
7
3
 
8
- export { enable, disable, setConfiguration, getConfiguration };
4
+ export { stackPrefetch, stackContextPrefetch };
@@ -0,0 +1,380 @@
1
+ import {
2
+ imageLoader,
3
+ Enums,
4
+ eventTarget,
5
+ imageLoadPoolManager,
6
+ cache,
7
+ getConfiguration as getCoreConfiguration,
8
+ } from '@cornerstonejs/core';
9
+ import { addToolState, getToolState } from './state';
10
+ import {
11
+ getStackData,
12
+ requestType,
13
+ priority,
14
+ clearFromImageIds,
15
+ getPromiseRemovedHandler,
16
+ } from './stackPrefetchUtils';
17
+ import roundNumber from '../roundNumber';
18
+
19
+ let configuration = {
20
+ maxImagesToPrefetch: Infinity,
21
+ // Fetch up to 2 image before and after
22
+ minBefore: 2,
23
+ maxAfter: 2,
24
+ // Increment the cache size by 10 images
25
+ directionExtraImages: 10,
26
+ preserveExistingPool: false,
27
+ };
28
+
29
+ let resetPrefetchTimeout;
30
+ // Starting the prefetch quickly isn't an issue as the main image is already being
31
+ // loaded, so a 5 ms prefetch delay is fine
32
+ const resetPrefetchDelay = 5;
33
+
34
+ /**
35
+ * Call this to enable stack context sensitive prefetch. Should be called
36
+ * before stack data is set in order to start prefetch after load first image.
37
+ * This will add a STACK_NEW_IMAGE to detect when a new image is displayed, and then
38
+ * update the prefetch stack. The context sensitive prefetch reacts to the
39
+ * initial display, or significant moves, the already loaded images, the
40
+ * cache size and the direction of navigation. The behaviour is:
41
+ *
42
+ * 1. On navigating to a new image initially, or one that is at a different position:
43
+ * * Fetch the next/previous 2 images
44
+ * 2. If the user is navigating forward/backward by less than 5 images, then
45
+ * * Prefetch additional images in the direction of navigation, up to 100
46
+ * 3. If all the images in a given prefetch have completed, then:
47
+ * * Use the last prefetched image size as an image size for the stack
48
+ * * Fetch up to 1/4 of the cache size images near the current image
49
+ *
50
+ * This is designed to:
51
+ * * Get nearby images immediately so that they are available for navigation
52
+ * * Under the assumption that users might click and view an image, then
53
+ * navigate to next/previous image to see the exact image they want
54
+ * * Not interfere with loading other viewports if they are still loading
55
+ * * Load priority is prefetch, and minimal images are requested initially
56
+ * * Load an entire series if it will fit in memory
57
+ * * Allows navigating to other parts of the series and display images immediately
58
+ * * Have images available for CINE/navigation in one direction even when
59
+ * there is more image data than will fit in memory.
60
+ * * Up to 100 images in the direction of travel will be prefetched
61
+ *
62
+ * @param element - to prefetch on
63
+ */
64
+ const enable = (element): void => {
65
+ const stack = getStackData(element);
66
+
67
+ if (!stack || !stack.imageIds || stack.imageIds.length === 0) {
68
+ console.warn('CornerstoneTools.stackPrefetch: No images in stack.');
69
+ return;
70
+ }
71
+
72
+ updateToolState(element);
73
+
74
+ prefetch(element);
75
+
76
+ element.removeEventListener(Enums.Events.STACK_NEW_IMAGE, onImageUpdated);
77
+ element.addEventListener(Enums.Events.STACK_NEW_IMAGE, onImageUpdated);
78
+
79
+ const promiseRemovedHandler = getPromiseRemovedHandler(element);
80
+
81
+ eventTarget.removeEventListener(
82
+ Enums.Events.IMAGE_CACHE_IMAGE_REMOVED,
83
+ promiseRemovedHandler
84
+ );
85
+ eventTarget.addEventListener(
86
+ Enums.Events.IMAGE_CACHE_IMAGE_REMOVED,
87
+ promiseRemovedHandler
88
+ );
89
+ };
90
+
91
+ function prefetch(element) {
92
+ const stack = getStackData(element);
93
+ if (!stack?.imageIds?.length) {
94
+ console.warn('CornerstoneTools.stackPrefetch: No images in stack.');
95
+ return;
96
+ }
97
+
98
+ // Get the stackPrefetch tool data
99
+ const stackPrefetchData = getToolState(element);
100
+
101
+ if (!stackPrefetchData) {
102
+ return;
103
+ }
104
+
105
+ const stackPrefetch = stackPrefetchData || {};
106
+
107
+ // If all the requests are complete, disable the stackPrefetch tool
108
+ stackPrefetch.enabled &&= stackPrefetch.indicesToRequest?.length;
109
+
110
+ // Make sure the tool is still enabled
111
+ if (stackPrefetch.enabled === false) {
112
+ return;
113
+ }
114
+
115
+ // Remove an imageIdIndex from the list of indices to request
116
+ // This fires when the individual image loading deferred is resolved
117
+ function removeFromList(imageIdIndex) {
118
+ const index = stackPrefetch.indicesToRequest.indexOf(imageIdIndex);
119
+
120
+ if (index > -1) {
121
+ // Don't remove last element if imageIdIndex not found
122
+ stackPrefetch.indicesToRequest.splice(index, 1);
123
+ }
124
+ }
125
+
126
+ // Remove all already cached images from the
127
+ // IndicesToRequest array.
128
+ const indicesToRequestCopy = stackPrefetch.indicesToRequest.slice();
129
+ const { currentImageIdIndex } = stack;
130
+
131
+ indicesToRequestCopy.forEach((imageIdIndex) => {
132
+ const imageId = stack.imageIds[imageIdIndex];
133
+
134
+ if (!imageId) {
135
+ return;
136
+ }
137
+
138
+ const distance = Math.abs(currentImageIdIndex - imageIdIndex);
139
+ // For nearby objects, ensure the last accessed time is updated
140
+ // by using getImageLoadObject.
141
+ // For more distant objects, just check if available, but dont
142
+ // change the access time.
143
+ // This allows throwing data that hasn't been accessed and is not
144
+ // nearby.
145
+ const imageCached =
146
+ distance < 6
147
+ ? cache.getImageLoadObject(imageId)
148
+ : cache.isLoaded(imageId);
149
+
150
+ if (imageCached) {
151
+ // Already in cache
152
+ removeFromList(imageIdIndex);
153
+ }
154
+ });
155
+
156
+ // Stop here if there are no images left to request
157
+ // After those in the cache have been removed
158
+ if (!stackPrefetch.indicesToRequest.length) {
159
+ return;
160
+ }
161
+
162
+ // Clear the requestPool of prefetch requests, if needed.
163
+ if (!configuration.preserveExistingPool) {
164
+ imageLoadPoolManager.filterRequests(clearFromImageIds(stack));
165
+ }
166
+
167
+ function doneCallback(imageId) {
168
+ const imageIdIndex = stack.imageIds.indexOf(imageId);
169
+
170
+ removeFromList(imageIdIndex);
171
+ const image = cache.getCachedImageBasedOnImageURI(imageId);
172
+ const { stats } = stackPrefetch;
173
+ const decodeTimeInMS = image?.image?.decodeTimeInMS || 0;
174
+ if (decodeTimeInMS) {
175
+ stats.imageIds.set(imageId, decodeTimeInMS);
176
+ stats.decodeTimeInMS += decodeTimeInMS;
177
+ const loadTimeInMS = image?.image?.loadTimeInMS || 0;
178
+ stats.loadTimeInMS += loadTimeInMS;
179
+ }
180
+
181
+ if (!stackPrefetch.indicesToRequest.length) {
182
+ if (image?.sizeInBytes) {
183
+ const { sizeInBytes } = image;
184
+ const usage = cache.getMaxCacheSize() / 4 / sizeInBytes;
185
+ if (!stackPrefetch.cacheFill) {
186
+ stats.initialTime = Date.now() - stats.start;
187
+ stats.initialSize = stats.imageIds.size;
188
+ updateToolState(element, usage);
189
+ prefetch(element);
190
+ } else if (stats.imageIds.size) {
191
+ stats.fillTime = Date.now() - stats.start;
192
+ const { size } = stats.imageIds;
193
+ stats.fillSize = size;
194
+ console.log(
195
+ 'Done cache fill',
196
+ stats.fillTime,
197
+ 'ms',
198
+ size,
199
+ 'items',
200
+ 'average total time',
201
+ roundNumber(stats.fillTime / size),
202
+ 'ms',
203
+ 'average load',
204
+ roundNumber(stats.loadTimeInMS / size),
205
+ 'ms',
206
+ 'average decode',
207
+ roundNumber(stats.decodeTimeInMS / size),
208
+ 'ms'
209
+ );
210
+ }
211
+ }
212
+ }
213
+ }
214
+
215
+ const requestFn = (imageId, options) =>
216
+ imageLoader
217
+ .loadAndCacheImage(imageId, options)
218
+ .then(() => doneCallback(imageId));
219
+
220
+ const { useNorm16Texture } = getCoreConfiguration().rendering;
221
+
222
+ indicesToRequestCopy.forEach((imageIdIndex) => {
223
+ const imageId = stack.imageIds[imageIdIndex];
224
+ // IMPORTANT: Request type should be passed if not the 'interaction'
225
+ // highest priority will be used for the request type in the imageRetrievalPool
226
+ const options = {
227
+ targetBuffer: {
228
+ type: useNorm16Texture ? undefined : 'Float32Array',
229
+ },
230
+ preScale: {
231
+ enabled: true,
232
+ },
233
+ requestType,
234
+ };
235
+
236
+ imageLoadPoolManager.addRequest(
237
+ requestFn.bind(null, imageId, options),
238
+ requestType,
239
+ // Additional details
240
+ {
241
+ imageId,
242
+ },
243
+ priority
244
+ // addToBeginning
245
+ );
246
+ });
247
+ }
248
+
249
+ function onImageUpdated(e) {
250
+ // Start prefetching again (after a delay)
251
+ // When the user has scrolled to a new image
252
+ clearTimeout(resetPrefetchTimeout);
253
+ resetPrefetchTimeout = setTimeout(function () {
254
+ const element = e.target;
255
+
256
+ // If playClip is enabled and the user loads a different series in the viewport
257
+ // An exception will be thrown because the element will not be enabled anymore
258
+ try {
259
+ updateToolState(element);
260
+ prefetch(element);
261
+ } catch (error) {
262
+ return;
263
+ }
264
+ }, resetPrefetchDelay);
265
+ }
266
+
267
+ // Not a full signum, but good enough for direction.
268
+ const signum = (x) => (x < 0 ? -1 : 1);
269
+
270
+ const updateToolState = (element, usage?: number) => {
271
+ const stack = getStackData(element);
272
+ if (!stack || !stack.imageIds || stack.imageIds.length === 0) {
273
+ console.warn('CornerstoneTools.stackPrefetch: No images in stack.');
274
+ return;
275
+ }
276
+
277
+ const { currentImageIdIndex } = stack;
278
+ let { maxAfter = 2, minBefore = 2 } = configuration;
279
+ const { directionExtraImages = 10 } = configuration;
280
+ // Use the currentImageIdIndex from the stack as the initialImageIdIndex
281
+ const stackPrefetchData = getToolState(element) || {
282
+ indicesToRequest: [],
283
+ currentImageIdIndex,
284
+ stackCount: 0,
285
+ enabled: true,
286
+ direction: 1,
287
+ stats: {
288
+ start: Date.now(),
289
+ imageIds: new Map(),
290
+ decodeTimeInMS: 0,
291
+ loadTimeInMS: 0,
292
+ totalBytes: 0,
293
+ },
294
+ };
295
+ const delta = currentImageIdIndex - stackPrefetchData.currentImageIdIndex;
296
+ stackPrefetchData.direction = signum(delta);
297
+ stackPrefetchData.currentImageIdIndex = currentImageIdIndex;
298
+ stackPrefetchData.enabled = true;
299
+
300
+ if (stackPrefetchData.stackCount < 100) {
301
+ stackPrefetchData.stackCount += directionExtraImages;
302
+ }
303
+
304
+ if (Math.abs(delta) > maxAfter || !delta) {
305
+ // Not incrementing by 1, so stop increasing the data size
306
+ // TODO - consider reversing the CINE playback
307
+ stackPrefetchData.stackCount = 0;
308
+ if (usage) {
309
+ // The usage of the cache that this stack can use
310
+ const positionFraction = currentImageIdIndex / stack.imageIds.length;
311
+ minBefore = Math.ceil(usage * positionFraction);
312
+ maxAfter = Math.ceil(usage * (1 - positionFraction));
313
+ stackPrefetchData.cacheFill = true;
314
+ } else {
315
+ stackPrefetchData.cacheFill = false;
316
+ }
317
+ } else if (delta < 0) {
318
+ minBefore += stackPrefetchData.stackCount;
319
+ maxAfter = 0;
320
+ } else {
321
+ maxAfter += stackPrefetchData.stackCount;
322
+ minBefore = 0;
323
+ }
324
+
325
+ const minIndex = Math.max(0, currentImageIdIndex - minBefore);
326
+
327
+ const maxIndex = Math.min(
328
+ stack.imageIds.length - 1,
329
+ currentImageIdIndex + maxAfter
330
+ );
331
+
332
+ // Order these correctly initially
333
+ const indicesToRequest = [];
334
+ for (let i = currentImageIdIndex + 1; i <= maxIndex; i++) {
335
+ indicesToRequest.push(i);
336
+ }
337
+ for (let i = currentImageIdIndex - 1; i >= minIndex; i--) {
338
+ indicesToRequest.push(i);
339
+ }
340
+ stackPrefetchData.indicesToRequest = indicesToRequest;
341
+
342
+ addToolState(element, stackPrefetchData);
343
+ };
344
+
345
+ function disable(element) {
346
+ clearTimeout(resetPrefetchTimeout);
347
+ element.removeEventListener(Enums.Events.STACK_NEW_IMAGE, onImageUpdated);
348
+
349
+ const promiseRemovedHandler = getPromiseRemovedHandler(element);
350
+
351
+ eventTarget.removeEventListener(
352
+ Enums.Events.IMAGE_CACHE_IMAGE_REMOVED,
353
+ promiseRemovedHandler
354
+ );
355
+
356
+ const stackPrefetchData = getToolState(element);
357
+ // If there is actually something to disable, disable it
358
+
359
+ if (stackPrefetchData && stackPrefetchData.data.length) {
360
+ stackPrefetchData.enabled = false;
361
+ // Don't worry about clearing the requests - there aren't that many too be bothersome
362
+ }
363
+ }
364
+
365
+ function getConfiguration() {
366
+ return configuration;
367
+ }
368
+
369
+ function setConfiguration(config) {
370
+ configuration = config;
371
+ }
372
+
373
+ const stackContextPrefetch = {
374
+ enable,
375
+ disable,
376
+ getConfiguration,
377
+ setConfiguration,
378
+ };
379
+
380
+ export default stackContextPrefetch;