-
Notifications
You must be signed in to change notification settings - Fork 248
/
FrameOfReferenceSpecificAnnotationManager.ts
405 lines (333 loc) · 13.4 KB
/
FrameOfReferenceSpecificAnnotationManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import {
Annotation,
Annotations,
FrameOfReferenceSpecificAnnotations,
AnnotationState,
} from '../../types/AnnotationTypes';
import cloneDeep from 'lodash.clonedeep';
import { Enums, eventTarget, Types, utilities } from '@cornerstonejs/core';
import { checkAndDefineIsLockedProperty } from './annotationLocking';
interface FilterInterface {
FrameOfReferenceUID?: string;
toolName?: string;
}
/**
* This class stores annotations in per FrameOfReference. Tool coordinates are
* in the world coordinates for the viewports, which is the patient coordinate system for DICOM.
*
* Each FrameOfReferenceSpecificAnnotationManager is separate, so it is be possible
* to render different annotations of the same tool on different viewports that share
* the same FrameOfReferenceUID, however no core tool in this library currently does this.
* This could be useful for e.g. viewing two different reads of the same data side-by-side.
*
* Note that this class is a singleton and should not be instantiated directly.
* To get the stored annotations information you can use ToolState helpers.
*
*/
export default class FrameOfReferenceSpecificAnnotationManager {
private annotations: AnnotationState;
public readonly uid: string;
/**
* @param uid - The uid of the state manager. If omitted it is autogenerated.
*/
constructor(uid?: string) {
if (!uid) {
uid = utilities.uuidv4();
}
this.annotations = {};
this.uid = uid;
// Listen to the IMAGE_VOLUME_MODIFIED event to invalidate data.
eventTarget.addEventListener(
Enums.Events.IMAGE_VOLUME_MODIFIED,
this._imageVolumeModifiedHandler
);
}
/**
* When a volume is modified we invalidate all of the `annotations` on the
* volume's `FrameOfReferenceUID`. This is mainly to update statistics calculations
* when an annotation is drawn whilst data is still loading.
*
* @param evt - The IMAGE_VOLUME_MODIFIED rendering event.
*/
_imageVolumeModifiedHandler = (
evt: Types.EventTypes.ImageVolumeModifiedEvent
) => {
const eventDetail = evt.detail;
const { FrameOfReferenceUID } = eventDetail;
const annotations = this.annotations;
const frameOfReferenceSpecificAnnotations =
annotations[FrameOfReferenceUID];
if (!frameOfReferenceSpecificAnnotations) {
return;
}
Object.keys(frameOfReferenceSpecificAnnotations).forEach((toolName) => {
const toolSpecificAnnotations =
frameOfReferenceSpecificAnnotations[toolName];
toolSpecificAnnotations.forEach((annotation) => {
const invalidated = annotation.invalidated;
if (invalidated !== undefined) {
annotation.invalidated = true;
}
});
});
};
/**
* Returns all the available frameOfReferences inside the state manager
* @returns - All the registered frame of references inside the manager
*/
getFramesOfReference = (): Array<string> => {
return Object.keys(this.annotations);
};
/**
* get all tools `Annotations` for the provided FrameOfReference
*
* @param FrameOfReferenceUID - The UID of the FrameOfReference to retrieve data for.
* @returns FrameOfReferenceSpecificAnnotations
*/
getFrameOfReferenceAnnotations = (
FrameOfReferenceUID: string
): FrameOfReferenceSpecificAnnotations => {
return this.annotations[FrameOfReferenceUID];
};
/**
* Get `Annotations` from the the manager given the `FrameOfReferenceUID` and `toolName`.
*
* @param FrameOfReferenceUID - The UID of the FrameOfReference to retrieve data for.
* @param toolName - The name of the tool to retrieve data for.
*/
get = (
FrameOfReferenceUID: string,
toolName: string
): Annotations | undefined => {
const frameOfReferenceSpecificAnnotations =
this.annotations[FrameOfReferenceUID];
if (!frameOfReferenceSpecificAnnotations) {
return;
}
return frameOfReferenceSpecificAnnotations[toolName];
};
/**
* Given the unique identified for the some `annotation`, returns the `annotation`
* from the `annotations`. Searches are more efficient if either/both of
* the `FrameOfReferenceUID` and the `toolName` are given by the `filter`.
*
* @param annotationUID - The unique identifier of the `annotation`.
* @param filter - A `filter` which reduces the scope of the search.
*
* @returns The retrieved `annotation`.
*/
getAnnotation = (
annotationUID: string,
filter: FilterInterface = {}
): Annotation | undefined => {
const toolSpecificAnnotationsAndIndex =
this._getToolSpecificAnnotationsAndIndex(annotationUID, filter);
if (!toolSpecificAnnotationsAndIndex) {
return;
}
const { toolSpecificAnnotations, index } = toolSpecificAnnotationsAndIndex;
return toolSpecificAnnotations[index];
};
/**
* Adds an instance of `Annotation` to the `annotations`.
*
* @param annotation - The annotation to add.
*/
addAnnotation = (annotation: Annotation): void => {
const { metadata } = annotation;
const { FrameOfReferenceUID, toolName } = metadata;
const annotations = this.annotations;
let frameOfReferenceSpecificAnnotations = annotations[FrameOfReferenceUID];
if (!frameOfReferenceSpecificAnnotations) {
annotations[FrameOfReferenceUID] = {};
frameOfReferenceSpecificAnnotations = annotations[FrameOfReferenceUID];
}
let toolSpecificAnnotations = frameOfReferenceSpecificAnnotations[toolName];
if (!toolSpecificAnnotations) {
frameOfReferenceSpecificAnnotations[toolName] = [];
toolSpecificAnnotations = frameOfReferenceSpecificAnnotations[toolName];
}
toolSpecificAnnotations.push(annotation);
checkAndDefineIsLockedProperty(annotation);
};
/**
* Removes an instance of `Annotation` from the `annotations`.
*
* @param annotation - The annotation to remove.
*/
// removeAnnotation = (annotation: Annotation): void => {
// const { metadata } = annotation
// const { FrameOfReferenceUID, toolName, annotationUID } = metadata
// const annotations = this.annotations
// const frameOfReferenceSpecificAnnotations = annotations[FrameOfReferenceUID]
// if (!frameOfReferenceSpecificAnnotations) {
// throw new Error(
// `frameOfReferenceSpecificAnnotations with FrameOfReferenceUID ${FrameOfReferenceUID} does not exist.`
// )
// }
// const toolSpecificAnnotations = frameOfReferenceSpecificAnnotations[toolName]
// if (!toolSpecificAnnotations) {
// throw new Error(
// `toolSpecificAnnotations for toolName ${toolName} on FrameOfReferenceUID ${FrameOfReferenceUID} does not exist.`
// )
// }
// const index = toolSpecificAnnotations.findIndex(
// (annotation) => annotation.metadata.annotationUID === annotationUID
// )
// toolSpecificAnnotations.splice(index, 1)
// // remove tool specific annotations if no annotation is left
// if (!toolSpecificAnnotations.length) {
// delete frameOfReferenceSpecificAnnotations[toolName]
// }
// // Make sure it is not held in the global set of locked instances
// setAnnotationLocked(annotation, false)
// }
/**
* Given the unique identified for the some `annotation`, removes the `annotation`
* from the `annotations`. Searches are more efficient if either/both of
* the `FrameOfReferenceUID` and the `toolName` are given by the `filter`.
*
* @param annotationUID - The unique identifier of the `annotation` to remove.
* @param filter - A `filter` which reduces the scope of the search.
*/
removeAnnotation = (annotationUID: string, filter: FilterInterface = {}) => {
const toolSpecificAnnotationsAndIndex =
this._getToolSpecificAnnotationsAndIndex(annotationUID, filter);
if (!toolSpecificAnnotationsAndIndex) {
return;
}
const { toolSpecificAnnotations, index } = toolSpecificAnnotationsAndIndex;
const { metadata } = toolSpecificAnnotations[0];
toolSpecificAnnotations.splice(index, 1);
// remove tool specific annotations if no annotation is left
if (!toolSpecificAnnotations.length) {
const { toolName } = metadata;
delete this.annotations[metadata.FrameOfReferenceUID][toolName];
}
};
/**
* Returns a section of the annotations. Useful for serialization.
*
* - If no arguments are given, the entire `AnnotationState` instance is returned.
* - If the `FrameOfReferenceUID` is given, the corresponding
* `FrameOfReferenceSpecificAnnotations` instance is returned.
* - If both the `FrameOfReferenceUID` and the `toolName` are are given, the
* corresponding `Annotations` instance is returned.
*
* @param FrameOfReferenceUID - A filter string for returning the `annotations` of a specific frame of reference.
* @param toolName - A filter string for returning `annotations` for a specific tool on a specific frame of reference.
*
* @returns The retrieved `annotation`.
*/
saveAnnotations = (
FrameOfReferenceUID?: string,
toolName?: string
): AnnotationState | FrameOfReferenceSpecificAnnotations | Annotations => {
const annotations = this.annotations;
if (FrameOfReferenceUID && toolName) {
const frameOfReferenceSpecificAnnotations =
annotations[FrameOfReferenceUID];
if (!frameOfReferenceSpecificAnnotations) {
return;
}
const toolSpecificAnnotations =
frameOfReferenceSpecificAnnotations[toolName];
return cloneDeep(toolSpecificAnnotations);
} else if (FrameOfReferenceUID) {
const frameOfReferenceSpecificAnnotations =
annotations[FrameOfReferenceUID];
return cloneDeep(frameOfReferenceSpecificAnnotations);
}
return cloneDeep(annotations);
};
/**
* Restores a section of the `annotations`. Useful for loading in serialized data.
*
* - If no arguments are given, the entire `AnnotationState` instance is restored.
* - If the `FrameOfReferenceUID` is given, the corresponding
* `FrameOfReferenceSpecificAnnotations` instance is restored.
* - If both the `FrameOfReferenceUID` and the `toolName` are are given, the
* corresponding `Annotations` instance is restored.
*
* @param FrameOfReferenceUID - A filter string for restoring only the `annotations` of a specific frame of reference.
* @param toolName - A filter string for restoring `annotation` for a specific tool on a specific frame of reference.
*/
restoreAnnotations = (
state: AnnotationState | FrameOfReferenceSpecificAnnotations | Annotations,
FrameOfReferenceUID?: string,
toolName?: string
): void => {
const annotations = this.annotations;
if (FrameOfReferenceUID && toolName) {
// Set Annotations for FrameOfReferenceUID and toolName.
let frameOfReferenceSpecificAnnotations =
annotations[FrameOfReferenceUID];
if (!frameOfReferenceSpecificAnnotations) {
annotations[FrameOfReferenceUID] = {};
frameOfReferenceSpecificAnnotations = annotations[FrameOfReferenceUID];
}
frameOfReferenceSpecificAnnotations[toolName] = <Annotations>state;
} else if (FrameOfReferenceUID) {
// Set FrameOfReferenceSpecificAnnotations for FrameOfReferenceUID.
annotations[FrameOfReferenceUID] = <FrameOfReferenceSpecificAnnotations>(
state
);
} else {
// Set entire annotations
this.annotations = <AnnotationState>cloneDeep(state);
}
};
/**
* Given the unique identifier for a tool, returns the `Annotations`
* it belongs to, and the `index` of its position in that array.
*
* @param annotationUID - The unique identifier of the `annotation`.
* @param filter - A `filter` which reduces the scope of the search.
*
* @returns {object}
* @returns {object.toolSpecificAnnotations} The `Annotations` instance containing the `annotation`.
* @returns {object.index} The `index` of the `annotation` in the `toolSpecificAnnotations` array.
*
* @internal
*/
private _getToolSpecificAnnotationsAndIndex(
annotationUID: string,
filter: FilterInterface
): { toolSpecificAnnotations: Annotations; index: number } {
const { toolName, FrameOfReferenceUID } = filter;
const annotations = this.annotations;
let frameOfReferenceUIDKeys;
if (FrameOfReferenceUID) {
frameOfReferenceUIDKeys = [FrameOfReferenceUID];
} else {
frameOfReferenceUIDKeys = Object.keys(annotations);
}
const numFrameOfReferenceUIDKeys = frameOfReferenceUIDKeys.length;
for (let i = 0; i < numFrameOfReferenceUIDKeys; i++) {
const frameOfReferenceUID = frameOfReferenceUIDKeys[i];
const frameOfReferenceSpecificAnnotations =
annotations[frameOfReferenceUID];
let toolNameKeys;
if (toolName) {
toolNameKeys = [toolName];
} else {
toolNameKeys = Object.keys(frameOfReferenceSpecificAnnotations);
}
const numToolNameKeys = toolNameKeys.length;
for (let j = 0; j < numToolNameKeys; j++) {
const toolName = toolNameKeys[j];
const toolSpecificAnnotations =
frameOfReferenceSpecificAnnotations[toolName];
const index = toolSpecificAnnotations.findIndex(
(annotation) => annotation.annotationUID === annotationUID
);
if (index !== -1) {
return { toolSpecificAnnotations, index };
}
}
}
}
}
const defaultFrameOfReferenceSpecificAnnotationManager =
new FrameOfReferenceSpecificAnnotationManager('DEFAULT');
export { defaultFrameOfReferenceSpecificAnnotationManager };