@cornerstonejs/tools 1.19.1 → 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.
- package/dist/cjs/utilities/index.d.ts +2 -2
- package/dist/cjs/utilities/index.js +4 -3
- package/dist/cjs/utilities/index.js.map +1 -1
- package/dist/cjs/utilities/stackPrefetch/index.d.ts +3 -2
- package/dist/cjs/utilities/stackPrefetch/index.js +8 -6
- package/dist/cjs/utilities/stackPrefetch/index.js.map +1 -1
- package/dist/cjs/utilities/stackPrefetch/stackContextPrefetch.d.ts +16 -0
- package/dist/cjs/utilities/stackPrefetch/stackContextPrefetch.js +227 -0
- package/dist/cjs/utilities/stackPrefetch/stackContextPrefetch.js.map +1 -0
- package/dist/cjs/utilities/stackPrefetch/stackPrefetch.d.ts +7 -1
- package/dist/cjs/utilities/stackPrefetch/stackPrefetch.js +23 -93
- package/dist/cjs/utilities/stackPrefetch/stackPrefetch.js.map +1 -1
- package/dist/cjs/utilities/stackPrefetch/stackPrefetchUtils.d.ts +14 -0
- package/dist/cjs/utilities/stackPrefetch/stackPrefetchUtils.js +85 -0
- package/dist/cjs/utilities/stackPrefetch/stackPrefetchUtils.js.map +1 -0
- package/dist/esm/utilities/index.d.ts +2 -2
- package/dist/esm/utilities/index.js +2 -2
- package/dist/esm/utilities/index.js.map +1 -1
- package/dist/esm/utilities/stackPrefetch/index.d.ts +3 -2
- package/dist/esm/utilities/stackPrefetch/index.js +3 -2
- package/dist/esm/utilities/stackPrefetch/index.js.map +1 -1
- package/dist/esm/utilities/stackPrefetch/stackContextPrefetch.d.ts +16 -0
- package/dist/esm/utilities/stackPrefetch/stackContextPrefetch.js +220 -0
- package/dist/esm/utilities/stackPrefetch/stackContextPrefetch.js.map +1 -0
- package/dist/esm/utilities/stackPrefetch/stackPrefetch.d.ts +7 -1
- package/dist/esm/utilities/stackPrefetch/stackPrefetch.js +12 -79
- package/dist/esm/utilities/stackPrefetch/stackPrefetch.js.map +1 -1
- package/dist/esm/utilities/stackPrefetch/stackPrefetchUtils.d.ts +14 -0
- package/dist/esm/utilities/stackPrefetch/stackPrefetchUtils.js +77 -0
- package/dist/esm/utilities/stackPrefetch/stackPrefetchUtils.js.map +1 -0
- package/dist/umd/index.js +1 -1
- package/dist/umd/index.js.map +1 -1
- package/package.json +3 -3
- package/src/utilities/index.ts +2 -1
- package/src/utilities/stackPrefetch/index.ts +3 -7
- package/src/utilities/stackPrefetch/stackContextPrefetch.ts +380 -0
- package/src/utilities/stackPrefetch/stackPrefetch.ts +29 -151
- 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.
|
|
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.
|
|
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": "
|
|
55
|
+
"gitHead": "b2e964e0191802b363088ad264401e90513d88c3"
|
|
56
56
|
}
|
package/src/utilities/index.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
3
|
-
disable,
|
|
4
|
-
setConfiguration,
|
|
5
|
-
getConfiguration,
|
|
6
|
-
} from './stackPrefetch';
|
|
1
|
+
import stackPrefetch from './stackPrefetch';
|
|
2
|
+
import stackContextPrefetch from './stackContextPrefetch';
|
|
7
3
|
|
|
8
|
-
export {
|
|
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;
|
|
@@ -9,10 +9,14 @@ import {
|
|
|
9
9
|
getConfiguration as getCoreConfiguration,
|
|
10
10
|
} from '@cornerstonejs/core';
|
|
11
11
|
import { addToolState, getToolState } from './state';
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
import {
|
|
13
|
+
getStackData,
|
|
14
|
+
requestType,
|
|
15
|
+
priority,
|
|
16
|
+
getPromiseRemovedHandler,
|
|
17
|
+
nearestIndex,
|
|
18
|
+
range,
|
|
19
|
+
} from './stackPrefetchUtils';
|
|
16
20
|
|
|
17
21
|
let configuration = {
|
|
18
22
|
maxImagesToPrefetch: Infinity,
|
|
@@ -28,66 +32,6 @@ let configuration = {
|
|
|
28
32
|
let resetPrefetchTimeout;
|
|
29
33
|
const resetPrefetchDelay = 10;
|
|
30
34
|
|
|
31
|
-
function range(lowEnd, highEnd) {
|
|
32
|
-
// Javascript version of Python's range function
|
|
33
|
-
// http://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-an-array-based-on-suppl
|
|
34
|
-
lowEnd = Math.round(lowEnd) || 0;
|
|
35
|
-
highEnd = Math.round(highEnd) || 0;
|
|
36
|
-
|
|
37
|
-
const arr = [];
|
|
38
|
-
let c = highEnd - lowEnd + 1;
|
|
39
|
-
|
|
40
|
-
if (c <= 0) {
|
|
41
|
-
return arr;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
while (c--) {
|
|
45
|
-
arr[c] = highEnd--;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return arr;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function nearestIndex(arr, x) {
|
|
52
|
-
// Return index of nearest values in array
|
|
53
|
-
// http://stackoverflow.com/questions/25854212/return-index-of-nearest-values-in-an-array
|
|
54
|
-
let low = 0;
|
|
55
|
-
let high = arr.length - 1;
|
|
56
|
-
|
|
57
|
-
arr.forEach((v, idx) => {
|
|
58
|
-
if (v < x) {
|
|
59
|
-
low = Math.max(idx, low);
|
|
60
|
-
} else if (v > x) {
|
|
61
|
-
high = Math.min(idx, high);
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
return { low, high };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function getStackData(element) {
|
|
69
|
-
const enabledElement = getEnabledElement(element);
|
|
70
|
-
|
|
71
|
-
if (!enabledElement) {
|
|
72
|
-
throw new Error(
|
|
73
|
-
'stackPrefetch: element must be a valid Cornerstone enabled element'
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const { viewport } = enabledElement;
|
|
78
|
-
|
|
79
|
-
if (!(viewport instanceof StackViewport)) {
|
|
80
|
-
throw new Error(
|
|
81
|
-
'stackPrefetch: element must be a StackViewport, VolumeViewport stackPrefetch not yet implemented'
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return {
|
|
86
|
-
currentImageIdIndex: viewport.getCurrentImageIdIndex(),
|
|
87
|
-
imageIds: viewport.getImageIds(),
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
35
|
function prefetch(element) {
|
|
92
36
|
// Get the stackPrefetch tool data
|
|
93
37
|
const stackPrefetchData = getToolState(element);
|
|
@@ -99,18 +43,15 @@ function prefetch(element) {
|
|
|
99
43
|
const stackPrefetch = stackPrefetchData || {};
|
|
100
44
|
const stack = getStackData(element);
|
|
101
45
|
|
|
102
|
-
if (!stack
|
|
46
|
+
if (!stack?.imageIds?.length) {
|
|
103
47
|
console.warn('CornerstoneTools.stackPrefetch: No images in stack.');
|
|
104
48
|
return;
|
|
105
49
|
}
|
|
106
50
|
|
|
51
|
+
const { currentImageIdIndex } = stack;
|
|
52
|
+
|
|
107
53
|
// If all the requests are complete, disable the stackPrefetch tool
|
|
108
|
-
|
|
109
|
-
!stackPrefetch.indicesToRequest ||
|
|
110
|
-
!stackPrefetch.indicesToRequest.length
|
|
111
|
-
) {
|
|
112
|
-
stackPrefetch.enabled = false;
|
|
113
|
-
}
|
|
54
|
+
stackPrefetch.enabled &&= stackPrefetch.indicesToRequest?.length;
|
|
114
55
|
|
|
115
56
|
// Make sure the tool is still enabled
|
|
116
57
|
if (stackPrefetch.enabled === false) {
|
|
@@ -140,9 +81,20 @@ function prefetch(element) {
|
|
|
140
81
|
return;
|
|
141
82
|
}
|
|
142
83
|
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
84
|
+
const distance = Math.abs(currentImageIdIndex - imageIdIndex);
|
|
85
|
+
// For nearby objects, ensure the last accessed time is updated
|
|
86
|
+
// by using getImageLoadObject.
|
|
87
|
+
// For more distant objects, just check if available, but dont
|
|
88
|
+
// change the access time.
|
|
89
|
+
// This allows throwing data that hasn't been accessed and is not
|
|
90
|
+
// nearby.
|
|
91
|
+
const imageCached =
|
|
92
|
+
distance < 6
|
|
93
|
+
? cache.getImageLoadObject(imageId)
|
|
94
|
+
: cache.isLoaded(imageId);
|
|
95
|
+
|
|
96
|
+
if (imageCached) {
|
|
97
|
+
// Already in cache
|
|
146
98
|
removeFromList(imageIdIndex);
|
|
147
99
|
}
|
|
148
100
|
});
|
|
@@ -173,41 +125,8 @@ function prefetch(element) {
|
|
|
173
125
|
const imageIdIndex = stack.imageIds.indexOf(image.imageId);
|
|
174
126
|
|
|
175
127
|
removeFromList(imageIdIndex);
|
|
176
|
-
|
|
177
|
-
// triggerEvent(element, EVENTS.STACK_PREFETCH_IMAGE_LOADED, {
|
|
178
|
-
// element,
|
|
179
|
-
// imageId: image.imageId,
|
|
180
|
-
// imageIndex: imageIdIndex,
|
|
181
|
-
// stackPrefetch,
|
|
182
|
-
// stack,
|
|
183
|
-
// });
|
|
184
|
-
|
|
185
|
-
// If there are no more images to fetch
|
|
186
|
-
// if (
|
|
187
|
-
// !(
|
|
188
|
-
// stackPrefetch.indicesToRequest &&
|
|
189
|
-
// stackPrefetch.indicesToRequest.length > 0
|
|
190
|
-
// )
|
|
191
|
-
// ) {
|
|
192
|
-
// triggerEvent(element, EVENTS.STACK_PREFETCH_DONE, {
|
|
193
|
-
// element,
|
|
194
|
-
// stackPrefetch,
|
|
195
|
-
// stack,
|
|
196
|
-
// });
|
|
197
|
-
// }
|
|
198
128
|
}
|
|
199
129
|
|
|
200
|
-
// Retrieve the errorLoadingHandler if one exists
|
|
201
|
-
// const errorLoadingHandler =
|
|
202
|
-
// loadHandlerManager.getErrorLoadingHandler(element);
|
|
203
|
-
|
|
204
|
-
// function failCallback(error) {
|
|
205
|
-
// logger.log('prefetch errored: %o', error);
|
|
206
|
-
// if (errorLoadingHandler) {
|
|
207
|
-
// errorLoadingHandler(element, imageId, error, 'stackPrefetch');
|
|
208
|
-
// }
|
|
209
|
-
// }
|
|
210
|
-
|
|
211
130
|
// Prefetch images around the current image (before and after)
|
|
212
131
|
let lowerIndex = nearest.low;
|
|
213
132
|
let higherIndex = nearest.high;
|
|
@@ -277,49 +196,6 @@ function prefetch(element) {
|
|
|
277
196
|
});
|
|
278
197
|
}
|
|
279
198
|
|
|
280
|
-
function getPromiseRemovedHandler(element) {
|
|
281
|
-
return function (e) {
|
|
282
|
-
const eventData = e.detail;
|
|
283
|
-
|
|
284
|
-
// When an imagePromise has been pushed out of the cache, re-add its index
|
|
285
|
-
// It to the indicesToRequest list so that it will be retrieved later if the
|
|
286
|
-
// CurrentImageIdIndex is changed to an image nearby
|
|
287
|
-
let stackData;
|
|
288
|
-
|
|
289
|
-
try {
|
|
290
|
-
// It will throw an exception in some cases (eg: thumbnails)
|
|
291
|
-
stackData = getStackData(element);
|
|
292
|
-
} catch (error) {
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
if (!stackData || !stackData.imageIds || stackData.imageIds.length === 0) {
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
const stack = stackData;
|
|
301
|
-
const imageIdIndex = stack.imageIds.indexOf(eventData.imageId);
|
|
302
|
-
|
|
303
|
-
// Make sure the image that was removed is actually in this stack
|
|
304
|
-
// Before adding it to the indicesToRequest array
|
|
305
|
-
if (imageIdIndex < 0) {
|
|
306
|
-
return;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
const stackPrefetchData = getToolState(element);
|
|
310
|
-
|
|
311
|
-
if (
|
|
312
|
-
!stackPrefetchData ||
|
|
313
|
-
!stackPrefetchData.indicesToRequest ||
|
|
314
|
-
!stackPrefetchData.indicesToRequest.length
|
|
315
|
-
) {
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
stackPrefetchData.indicesToRequest.push(imageIdIndex);
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
|
|
323
199
|
function onImageUpdated(e) {
|
|
324
200
|
// Start prefetching again (after a delay)
|
|
325
201
|
// When the user has scrolled to a new image
|
|
@@ -408,4 +284,6 @@ function setConfiguration(config) {
|
|
|
408
284
|
configuration = config;
|
|
409
285
|
}
|
|
410
286
|
|
|
411
|
-
|
|
287
|
+
const stackPrefetch = { enable, disable, getConfiguration, setConfiguration };
|
|
288
|
+
|
|
289
|
+
export default stackPrefetch;
|