Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 | import ViewerManager, { EVENTS as ViewerEvents } from '../tools/viewerManager'; import RoiAnnotation, { EVENTS as AnnotationEvents } from '../utils/RoiAnnotation'; import styles from '../utils/styles'; import { DicomMetadataStore, PubSubService } from '@ohif/core'; const EVENTS = { ANNOTATION_UPDATED: 'annotationUpdated', ANNOTATION_SELECTED: 'annotationSelected', ANNOTATION_REMOVED: 'annotationRemoved', RELABEL: 'relabel', DELETE: 'delete', }; /** * MicroscopyService is responsible to manage multiple third-party API's * microscopy viewers expose methods to manage the interaction with these * viewers and handle their ROI graphics to create, remove and modify the * ROI annotations relevant to the application */ export default class MicroscopyService extends PubSubService { public static REGISTRATION = servicesManager => { return { name: 'microscopyService', altName: 'MicroscopyService', create: (props) => { return new MicroscopyService(props); }, }; }; servicesManager: any; managedViewers = new Set(); roiUids = new Set(); annotations = {}; selectedAnnotation = null; pendingFocus = false; constructor({ servicesManager, extensionManager }) { super(EVENTS); this.servicesManager = servicesManager; this.peerImport = extensionManager.appConfig.peerImport; this._onRoiAdded = this._onRoiAdded.bind(this); this._onRoiModified = this._onRoiModified.bind(this); this._onRoiRemoved = this._onRoiRemoved.bind(this); this._onRoiUpdated = this._onRoiUpdated.bind(this); this._onRoiSelected = this._onRoiSelected.bind(this); this.isROIsVisible = true; } /** * Clears all the annotations and managed viewers, setting the manager state * to its initial state */ clear() { this.managedViewers.forEach(managedViewer => managedViewer.destroy()); this.managedViewers.clear(); for (const key in this.annotations) { delete this.annotations[key]; } this.roiUids.clear(); this.selectedAnnotation = null; this.pendingFocus = false; } clearAnnotations() { Object.keys(this.annotations).forEach(uid => { this.removeAnnotation(this.annotations[uid]); }); } public importDicomMicroscopyViewer(): Promise<any> { return this.peerImport("dicom-microscopy-viewer"); } /** * Observes when a ROI graphic is added, creating the correspondent annotation * with the current graphic and view state. * Creates a subscription for label updating for the created annotation and * publishes an ANNOTATION_UPDATED event when it happens. * Also triggers the relabel process after the graphic is placed. * * @param {Object} data The published data * @param {Object} data.roiGraphic The added ROI graphic object * @param {ViewerManager} data.managedViewer The origin viewer for the event */ _onRoiAdded(data) { const { roiGraphic, managedViewer, label } = data; const { studyInstanceUID, seriesInstanceUID } = managedViewer; const viewState = managedViewer.getViewState(); const roiAnnotation = new RoiAnnotation( roiGraphic, studyInstanceUID, seriesInstanceUID, '', viewState ); this.roiUids.add(roiGraphic.uid); this.annotations[roiGraphic.uid] = roiAnnotation; roiAnnotation.subscribe(AnnotationEvents.LABEL_UPDATED, () => { this._broadcastEvent(EVENTS.ANNOTATION_UPDATED, roiAnnotation); }); if (label !== undefined) { roiAnnotation.setLabel(label); } else { const onRelabel = item => managedViewer.updateROIProperties({ uid: roiGraphic.uid, properties: { label: item.label, finding: item.finding }, }); this.triggerRelabel(roiAnnotation, true, onRelabel); } } /** * Observes when a ROI graphic is modified, updating the correspondent * annotation with the current graphic and view state. * * @param {Object} data The published data * @param {Object} data.roiGraphic The modified ROI graphic object */ _onRoiModified(data) { const { roiGraphic, managedViewer } = data; const roiAnnotation = this.getAnnotation(roiGraphic.uid); if (!roiAnnotation) { return; } roiAnnotation.setRoiGraphic(roiGraphic); roiAnnotation.setViewState(managedViewer.getViewState()); } /** * Observes when a ROI graphic is removed, reflecting the removal in the * annotations' state. * * @param {Object} data The published data * @param {Object} data.roiGraphic The removed ROI graphic object */ _onRoiRemoved(data) { const { roiGraphic } = data; this.roiUids.delete(roiGraphic.uid); this.annotations[roiGraphic.uid].destroy(); delete this.annotations[roiGraphic.uid]; this._broadcastEvent(EVENTS.ANNOTATION_REMOVED, roiGraphic); } /** * Observes any changes on ROI graphics and synchronize all the managed * viewers to reflect those changes. * Also publishes an ANNOTATION_UPDATED event to notify the subscribers. * * @param {Object} data The published data * @param {Object} data.roiGraphic The added ROI graphic object * @param {ViewerManager} data.managedViewer The origin viewer for the event */ _onRoiUpdated(data) { const { roiGraphic, managedViewer } = data; this.synchronizeViewers(managedViewer); this._broadcastEvent(EVENTS.ANNOTATION_UPDATED, this.getAnnotation(roiGraphic.uid)); } /** * Observes when an ROI is selected. * Also publishes an ANNOTATION_SELECTED event to notify the subscribers. * * @param {Object} data The published data * @param {Object} data.roiGraphic The added ROI graphic object * @param {ViewerManager} data.managedViewer The origin viewer for the event */ _onRoiSelected(data) { const { roiGraphic } = data; const selectedAnnotation = this.getAnnotation(roiGraphic.uid); if (selectedAnnotation && selectedAnnotation !== this.getSelectedAnnotation()) { if (this.selectedAnnotation) { this.clearSelection(); } this.selectedAnnotation = selectedAnnotation; this._broadcastEvent(EVENTS.ANNOTATION_SELECTED, selectedAnnotation); } } /** * Creates the subscriptions for the managed viewer being added * * @param {ViewerManager} managedViewer The viewer being added */ _addManagedViewerSubscriptions(managedViewer) { managedViewer._roiAddedSubscription = managedViewer.subscribe( ViewerEvents.ADDED, this._onRoiAdded ); managedViewer._roiModifiedSubscription = managedViewer.subscribe( ViewerEvents.MODIFIED, this._onRoiModified ); managedViewer._roiRemovedSubscription = managedViewer.subscribe( ViewerEvents.REMOVED, this._onRoiRemoved ); managedViewer._roiUpdatedSubscription = managedViewer.subscribe( ViewerEvents.UPDATED, this._onRoiUpdated ); managedViewer._roiSelectedSubscription = managedViewer.subscribe( ViewerEvents.UPDATED, this._onRoiSelected ); } /** * Removes the subscriptions for the managed viewer being removed * * @param {ViewerManager} managedViewer The viewer being removed */ _removeManagedViewerSubscriptions(managedViewer) { managedViewer._roiAddedSubscription && managedViewer._roiAddedSubscription.unsubscribe(); managedViewer._roiModifiedSubscription && managedViewer._roiModifiedSubscription.unsubscribe(); managedViewer._roiRemovedSubscription && managedViewer._roiRemovedSubscription.unsubscribe(); managedViewer._roiUpdatedSubscription && managedViewer._roiUpdatedSubscription.unsubscribe(); managedViewer._roiSelectedSubscription && managedViewer._roiSelectedSubscription.unsubscribe(); managedViewer._roiAddedSubscription = null; managedViewer._roiModifiedSubscription = null; managedViewer._roiRemovedSubscription = null; managedViewer._roiUpdatedSubscription = null; managedViewer._roiSelectedSubscription = null; } /** * Returns the managed viewers that are displaying the image with the given * study and series UIDs * * @param {String} studyInstanceUID UID for the study * @param {String} seriesInstanceUID UID for the series * * @returns {Array} The managed viewers for the given series UID */ _getManagedViewersForSeries(studyInstanceUID, seriesInstanceUID) { const filter = managedViewer => managedViewer.studyInstanceUID === studyInstanceUID && managedViewer.seriesInstanceUID === seriesInstanceUID; return Array.from(this.managedViewers).filter(filter); } /** * Returns the managed viewers that are displaying the image with the given * study UID * * @param {String} studyInstanceUID UID for the study * * @returns {Array} The managed viewers for the given series UID */ getManagedViewersForStudy(studyInstanceUID) { const filter = managedViewer => managedViewer.studyInstanceUID === studyInstanceUID; return Array.from(this.managedViewers).filter(filter); } /** * Restores the created annotations for the viewer being added * * @param {ViewerManager} managedViewer The viewer being added */ _restoreAnnotations(managedViewer) { const { studyInstanceUID, seriesInstanceUID } = managedViewer; const annotations = this.getAnnotationsForSeries(studyInstanceUID, seriesInstanceUID); annotations.forEach(roiAnnotation => { managedViewer.addRoiGraphic(roiAnnotation.roiGraphic); }); } /** * Creates a managed viewer instance for the given third-party API's viewer. * Restores existing annotations for the given study/series. * Adds event subscriptions for the viewer being added. * Focuses the selected annotation when the viewer is being loaded into the * active viewport. * * @param viewer - Third-party viewer API's object to be managed * @param viewportId - The viewport Id where the viewer will be loaded * @param container - The DOM element where it will be rendered * @param studyInstanceUID - The study UID of the loaded image * @param seriesInstanceUID - The series UID of the loaded image * @param displaySets - All displaySets related to the same StudyInstanceUID * * @returns {ViewerManager} managed viewer */ addViewer(viewer, viewportId, container, studyInstanceUID, seriesInstanceUID) { const managedViewer = new ViewerManager( viewer, viewportId, container, studyInstanceUID, seriesInstanceUID ); this._restoreAnnotations(managedViewer); viewer._manager = managedViewer; this.managedViewers.add(managedViewer); // this._potentiallyLoadSR(studyInstanceUID, displaySets); this._addManagedViewerSubscriptions(managedViewer); if (this.pendingFocus) { this.pendingFocus = false; this.focusAnnotation(this.selectedAnnotation, viewportId); } return managedViewer; } _potentiallyLoadSR(StudyInstanceUID, displaySets) { const studyMetadata = DicomMetadataStore.getStudy(StudyInstanceUID); const smDisplaySet = displaySets.find(ds => ds.Modality === 'SM'); const { FrameOfReferenceUID, othersFrameOfReferenceUID } = smDisplaySet; if (!studyMetadata) { return; } let derivedDisplaySets = FrameOfReferenceUID ? displaySets.filter( ds => ds.ReferencedFrameOfReferenceUID === FrameOfReferenceUID || // sometimes each depth instance has the different FrameOfReferenceID othersFrameOfReferenceUID.includes(ds.ReferencedFrameOfReferenceUID) ) : []; if (!derivedDisplaySets.length) { return; } derivedDisplaySets = derivedDisplaySets.filter(ds => ds.Modality === 'SR'); if (derivedDisplaySets.some(ds => ds.isLoaded === true)) { // Don't auto load return; } // find most recent and load it. let recentDateTime = 0; let recentDisplaySet = derivedDisplaySets[0]; derivedDisplaySets.forEach(ds => { const dateTime = Number(`${ds.SeriesDate}${ds.SeriesTime}`); if (dateTime > recentDateTime) { recentDateTime = dateTime; recentDisplaySet = ds; } }); recentDisplaySet.isLoading = true; recentDisplaySet.load(smDisplaySet); } /** * Removes the given third-party viewer API's object from the managed viewers * and clears all its event subscriptions * * @param {Object} viewer Third-party viewer API's object to be removed */ removeViewer(viewer) { const managedViewer = viewer._manager; this._removeManagedViewerSubscriptions(managedViewer); managedViewer.destroy(); this.managedViewers.delete(managedViewer); } /** * Toggle ROIs visibility */ toggleROIsVisibility() { this.isROIsVisible ? this.hideROIs() : this.showROIs; this.isROIsVisible = !this.isROIsVisible; } /** * Hide all ROIs */ hideROIs() { this.managedViewers.forEach(mv => mv.hideROIs()); } /** Show all ROIs */ showROIs() { this.managedViewers.forEach(mv => mv.showROIs()); } /** * Returns a RoiAnnotation instance for the given ROI UID * * @param {String} uid UID of the annotation * * @returns {RoiAnnotation} The RoiAnnotation instance found for the given UID */ getAnnotation(uid) { return this.annotations[uid]; } /** * Returns all the RoiAnnotation instances being managed * * @returns {Array} All RoiAnnotation instances */ getAnnotations() { const annotations = []; Object.keys(this.annotations).forEach(uid => { annotations.push(this.getAnnotation(uid)); }); return annotations; } /** * Returns the RoiAnnotation instances registered with the given study UID * * @param {String} studyInstanceUID UID for the study */ getAnnotationsForStudy(studyInstanceUID) { const filter = a => a.studyInstanceUID === studyInstanceUID; return this.getAnnotations().filter(filter); } /** * Returns the RoiAnnotation instances registered with the given study and * series UIDs * * @param {String} studyInstanceUID UID for the study * @param {String} seriesInstanceUID UID for the series */ getAnnotationsForSeries(studyInstanceUID, seriesInstanceUID) { const filter = annotation => annotation.studyInstanceUID === studyInstanceUID && annotation.seriesInstanceUID === seriesInstanceUID; return this.getAnnotations().filter(filter); } /** * Returns the selected RoiAnnotation instance or null if none is selected * * @returns {RoiAnnotation} The selected RoiAnnotation instance */ getSelectedAnnotation() { return this.selectedAnnotation; } /** * Clear current RoiAnnotation selection */ clearSelection() { if (this.selectedAnnotation) { this.setROIStyle(this.selectedAnnotation.uid, { stroke: { color: '#00ff00', }, }); } this.selectedAnnotation = null; } /** * Selects the given RoiAnnotation instance, publishing an ANNOTATION_SELECTED * event to notify all the subscribers * * @param {RoiAnnotation} roiAnnotation The instance to be selected */ selectAnnotation(roiAnnotation) { if (this.selectedAnnotation) { this.clearSelection(); } this.selectedAnnotation = roiAnnotation; this._broadcastEvent(EVENTS.ANNOTATION_SELECTED, roiAnnotation); this.setROIStyle(roiAnnotation.uid, styles.active); } /** * Toggles overview map * * @param viewportId The active viewport index * @returns {void} */ toggleOverviewMap(viewportId) { const managedViewers = Array.from(this.managedViewers); const managedViewer = managedViewers.find(mv => mv.viewportId === viewportId); if (managedViewer) { managedViewer.toggleOverviewMap(); } } /** * Removes a RoiAnnotation instance from the managed annotations and reflects * its removal on all third-party viewers being managed * * @param {RoiAnnotation} roiAnnotation The instance to be removed */ removeAnnotation(roiAnnotation) { const { uid, studyInstanceUID, seriesInstanceUID } = roiAnnotation; const filter = managedViewer => managedViewer.studyInstanceUID === studyInstanceUID && managedViewer.seriesInstanceUID === seriesInstanceUID; const managedViewers = Array.from(this.managedViewers).filter(filter); managedViewers.forEach(managedViewer => managedViewer.removeRoiGraphic(uid)); if (this.annotations[uid]) { this.roiUids.delete(uid); this.annotations[uid].destroy(); delete this.annotations[uid]; this._broadcastEvent(EVENTS.ANNOTATION_REMOVED, roiAnnotation); } } /** * Focus the given RoiAnnotation instance by changing the OpenLayers' Map view * state of the managed viewer with the given viewport index. * If the image for the given annotation is not yet loaded into the viewport, * it will set a pendingFocus flag to true in order to perform the focus when * the managed viewer instance is created. * * @param {RoiAnnotation} roiAnnotation RoiAnnotation instance to be focused * @param {string} viewportId Index of the viewport to focus */ focusAnnotation(roiAnnotation, viewportId) { const filter = mv => mv.viewportId === viewportId; const managedViewer = Array.from(this.managedViewers).find(filter); if (managedViewer) { managedViewer.setViewStateByExtent(roiAnnotation); } else { this.pendingFocus = true; } } /** * Synchronize the ROI graphics for all the managed viewers that has the same * series UID of the given managed viewer * * @param {ViewerManager} baseManagedViewer Reference managed viewer */ synchronizeViewers(baseManagedViewer) { const { studyInstanceUID, seriesInstanceUID } = baseManagedViewer; const managedViewers = this._getManagedViewersForSeries(studyInstanceUID, seriesInstanceUID); // Prevent infinite loops arrising from updates. managedViewers.forEach(managedViewer => this._removeManagedViewerSubscriptions(managedViewer)); managedViewers.forEach(managedViewer => { if (managedViewer === baseManagedViewer) { return; } const annotations = this.getAnnotationsForSeries(studyInstanceUID, seriesInstanceUID); managedViewer.clearRoiGraphics(); annotations.forEach(roiAnnotation => { managedViewer.addRoiGraphic(roiAnnotation.roiGraphic); }); }); managedViewers.forEach(managedViewer => this._addManagedViewerSubscriptions(managedViewer)); } /** * Activates interactions across all the viewers being managed * * @param {Array} interactions interactions */ activateInteractions(interactions) { this.managedViewers.forEach(mv => mv.activateInteractions(interactions)); this.activeInteractions = interactions; } getActiveInteractions() { return this.activeInteractions; } /** * Triggers the relabelling process for the given RoiAnnotation instance, by * publishing the RELABEL event to notify the subscribers * * @param {RoiAnnotation} roiAnnotation The instance to be relabelled * @param {boolean} newAnnotation Whether the annotation is newly drawn (so it deletes on cancel). */ triggerRelabel(roiAnnotation, newAnnotation = false, onRelabel) { if (!onRelabel) { onRelabel = ({ label }) => this.managedViewers.forEach(mv => mv.updateROIProperties({ uid: roiAnnotation.uid, properties: { label }, }) ); } this._broadcastEvent(EVENTS.RELABEL, { roiAnnotation, deleteCallback: () => this.removeAnnotation(roiAnnotation), successCallback: onRelabel, newAnnotation, }); } /** * Triggers the deletion process for the given RoiAnnotation instance, by * publishing the DELETE event to notify the subscribers * * @param {RoiAnnotation} roiAnnotation The instance to be deleted */ triggerDelete(roiAnnotation) { this._broadcastEvent(EVENTS.DELETE, roiAnnotation); } /** * Set ROI style for all managed viewers * * @param {string} uid The ROI uid that will be styled * @param {object} styleOptions - Style options * @param {object*} styleOptions.stroke - Style options for the outline of the geometry * @param {number[]} styleOptions.stroke.color - RGBA color of the outline * @param {number} styleOptions.stroke.width - Width of the outline * @param {object*} styleOptions.fill - Style options for body the geometry * @param {number[]} styleOptions.fill.color - RGBA color of the body * @param {object*} styleOptions.image - Style options for image */ setROIStyle(uid, styleOptions) { this.managedViewers.forEach(mv => mv.setROIStyle(uid, styleOptions)); } /** * Get all managed viewers * * @returns {Array} managedViewers */ getAllManagedViewers() { return Array.from(this.managedViewers); } } export { EVENTS }; |