@ohif/extension-cornerstone 3.14.0-beta.116 → 3.14.0-beta.118

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ohif/extension-cornerstone",
3
- "version": "3.14.0-beta.116",
3
+ "version": "3.14.0-beta.118",
4
4
  "description": "OHIF extension for Cornerstone",
5
5
  "author": "OHIF",
6
6
  "license": "MIT",
@@ -36,9 +36,9 @@
36
36
  "@cornerstonejs/codec-openjpeg": "1.3.2",
37
37
  "@cornerstonejs/codec-openjph": "2.4.9",
38
38
  "@cornerstonejs/dicom-image-loader": "5.19.7",
39
- "@ohif/core": "3.14.0-beta.116",
40
- "@ohif/extension-default": "3.14.0-beta.116",
41
- "@ohif/ui-next": "3.14.0-beta.116",
39
+ "@ohif/core": "3.14.0-beta.118",
40
+ "@ohif/extension-default": "3.14.0-beta.118",
41
+ "@ohif/ui-next": "3.14.0-beta.118",
42
42
  "dcmjs": "0.52.0",
43
43
  "dicom-parser": "1.8.21",
44
44
  "hammerjs": "2.0.8",
@@ -1346,6 +1346,7 @@ describe('SegmentationService', () => {
1346
1346
  },
1347
1347
  FrameOfReferenceUID: 'frameOfReferenceUID',
1348
1348
  label: 'Segmentation 2',
1349
+ timePointScope: 'each' as const,
1349
1350
  };
1350
1351
 
1351
1352
  const retrievedSegmentationId = await service.createLabelmapForDisplaySet(
@@ -1354,7 +1355,10 @@ describe('SegmentationService', () => {
1354
1355
  );
1355
1356
 
1356
1357
  expect(imageLoader.createAndCacheDerivedLabelmapImages).toHaveBeenCalledTimes(1);
1357
- expect(imageLoader.createAndCacheDerivedLabelmapImages).toHaveBeenCalledWith('timePoint2');
1358
+ // One RLE labelmap image for each frame of every time point.
1359
+ expect(imageLoader.createAndCacheDerivedLabelmapImages).toHaveBeenCalledWith(['imageId'], {
1360
+ voxelRepresentation: csEnums.VoxelManagerEnum.RLE,
1361
+ });
1358
1362
 
1359
1363
  expect(service.addOrUpdateSegmentation).toHaveBeenCalledTimes(1);
1360
1364
  expect(service.addOrUpdateSegmentation).toHaveBeenCalledWith({
@@ -1376,7 +1380,7 @@ describe('SegmentationService', () => {
1376
1380
  representation: {
1377
1381
  data: {
1378
1382
  imageIds: ['imageId'],
1379
- referencedImageIds: 'timePoint2',
1383
+ referencedImageIds: ['imageId'],
1380
1384
  },
1381
1385
  type: 'Labelmap',
1382
1386
  },
@@ -1386,6 +1390,31 @@ describe('SegmentationService', () => {
1386
1390
  expect(retrievedSegmentationId).toEqual(segmentationId);
1387
1391
  });
1388
1392
 
1393
+ it('creates one labelmap of the middle time point for a dynamic volume by default', async () => {
1394
+ const displaySet = {
1395
+ imageIds: ['a1', 'a2', 'b1', 'b2', 'c1', 'c2'],
1396
+ isDynamicVolume: true,
1397
+ dynamicVolumeInfo: {
1398
+ timePoints: [
1399
+ ['a1', 'a2'],
1400
+ ['b1', 'b2'],
1401
+ ['c1', 'c2'],
1402
+ ],
1403
+ },
1404
+ } as unknown as AppTypes.DisplaySet;
1405
+ jest
1406
+ .spyOn(imageLoader, 'createAndCacheDerivedLabelmapImages')
1407
+ .mockReturnValue([] as csTypes.IImage[]);
1408
+ jest.spyOn(cstSegmentation.state, 'getSegmentations').mockReturnValue([]);
1409
+ jest.spyOn(service, 'addOrUpdateSegmentation').mockReturnValue(undefined);
1410
+
1411
+ await service.createLabelmapForDisplaySet(displaySet);
1412
+ expect(imageLoader.createAndCacheDerivedLabelmapImages).toHaveBeenLastCalledWith(['b1', 'b2']);
1413
+
1414
+ await service.createLabelmapForDisplaySet(displaySet, { timePointIndex: 2 });
1415
+ expect(imageLoader.createAndCacheDerivedLabelmapImages).toHaveBeenLastCalledWith(['c1', 'c2']);
1416
+ });
1417
+
1389
1418
  it('should create a dedicated color LUT and remember its index so every viewport representation reuses it', async () => {
1390
1419
  const displaySet = {
1391
1420
  imageIds: ['imageId'],
@@ -447,6 +447,13 @@ class SegmentationService extends PubSubService implements ISegmentationServiceI
447
447
  label?: string;
448
448
  /** The caller invented the label, so the user has not chosen a name. */
449
449
  labelIsGenerated?: boolean;
450
+ /**
451
+ * For a 4D display set. 'all' (the default) makes one 3D labelmap that
452
+ * all time points share; 'each' makes a labelmap for each time point.
453
+ */
454
+ timePointScope?: 'all' | 'each';
455
+ /** For 'all': the 0-based time point of the labelmap. Default: the middle one. */
456
+ timePointIndex?: number;
450
457
  }
451
458
  ): Promise<string> {
452
459
  return this._createSegmentationForDisplaySet(displaySet, LABELMAP, options);
@@ -484,23 +491,30 @@ class SegmentationService extends PubSubService implements ISegmentationServiceI
484
491
  label?: string;
485
492
  /** The caller invented the label, so the user has not chosen a name. */
486
493
  labelIsGenerated?: boolean;
494
+ timePointScope?: 'all' | 'each';
495
+ timePointIndex?: number;
487
496
  }
488
497
  ): Promise<string> {
489
498
  // Todo: random does not makes sense, make this better, like
490
499
  // labelmap 1, 2, 3 etc
491
500
  const segmentationId = options?.segmentationId ?? `${csUtils.uuidv4()}`;
492
501
 
493
- const isDynamicVolume = displaySet.isDynamicVolume;
494
-
495
- let referenceImageIds = displaySet.imageIds;
496
- if (isDynamicVolume) {
497
- // get the middle timepoint for referenceImageIds
498
- const timePoints = displaySet.dynamicVolumeInfo.timePoints;
499
- const middleTimePoint = timePoints[Math.floor(timePoints.length / 2)];
500
- referenceImageIds = middleTimePoint;
501
- }
502
-
503
- const derivedImages = await imageLoader.createAndCacheDerivedLabelmapImages(referenceImageIds);
502
+ // A 4D segmentation is one 3D labelmap of one time point, which shows on
503
+ // every time point. A labelmap with 'each' has one labelmap image for each
504
+ // frame of every time point; Cornerstone tools shows and edits the images
505
+ // of the time point on display, and RLE keeps the many images small.
506
+ const timePoints = displaySet.isDynamicVolume && displaySet.dynamicVolumeInfo?.timePoints;
507
+ const eachTimePoint =
508
+ timePoints && segmentationType === LABELMAP && options?.timePointScope === 'each';
509
+ const referenceImageIds =
510
+ timePoints && !eachTimePoint
511
+ ? timePoints[options?.timePointIndex ?? Math.floor(timePoints.length / 2)]
512
+ : displaySet.imageIds;
513
+ const derivedImages = await (eachTimePoint
514
+ ? imageLoader.createAndCacheDerivedLabelmapImages(referenceImageIds, {
515
+ voxelRepresentation: csEnums.VoxelManagerEnum.RLE,
516
+ })
517
+ : imageLoader.createAndCacheDerivedLabelmapImages(referenceImageIds));
504
518
 
505
519
  const segs = this.getSegmentations();
506
520
  const label = options?.label || `Segmentation ${segs.length + 1}`;
@@ -0,0 +1,93 @@
1
+ import { SegmentationRepresentations } from '@cornerstonejs/tools/enums';
2
+ import { createSegmentationForViewport } from './createSegmentationForViewport';
3
+ import { promptTimePointScope } from './promptTimePointScope';
4
+
5
+ jest.mock('./promptTimePointScope', () => ({
6
+ promptTimePointScope: jest.fn(),
7
+ }));
8
+
9
+ function createServicesManager(isDynamicVolume: boolean) {
10
+ const segmentationService = {
11
+ getSegmentations: jest.fn(() => []),
12
+ createLabelmapForDisplaySet: jest.fn(async (_ds, { segmentationId }) => segmentationId),
13
+ createContourForDisplaySet: jest.fn(async (_ds, { segmentationId }) => segmentationId),
14
+ addSegmentationRepresentation: jest.fn(),
15
+ };
16
+ const viewports = new Map([['vp', { displaySetInstanceUIDs: ['ds'] }]]);
17
+ const servicesManager = {
18
+ services: {
19
+ viewportGridService: {
20
+ getState: () => ({ viewports }),
21
+ },
22
+ displaySetService: {
23
+ getDisplaySetByUID: () => ({ displaySetInstanceUID: 'ds', isDynamicVolume }),
24
+ },
25
+ segmentationService,
26
+ uiDialogService: {},
27
+ cornerstoneViewportService: { getCornerstoneViewport: () => undefined },
28
+ },
29
+ };
30
+ return { servicesManager: servicesManager as never, segmentationService, viewports };
31
+ }
32
+
33
+ describe('createSegmentationForViewport on a 4D display set', () => {
34
+ beforeEach(() => jest.clearAllMocks());
35
+
36
+ it('creates no segmentation when the user cancels the dialog', async () => {
37
+ (promptTimePointScope as jest.Mock).mockResolvedValue(undefined);
38
+ const { servicesManager, segmentationService } = createServicesManager(true);
39
+
40
+ const result = await createSegmentationForViewport(servicesManager, {
41
+ viewportId: 'vp',
42
+ segmentationType: SegmentationRepresentations.Labelmap,
43
+ });
44
+
45
+ expect(result).toBeUndefined();
46
+ expect(segmentationService.createLabelmapForDisplaySet).not.toHaveBeenCalled();
47
+ });
48
+
49
+ it('creates no segmentation when the viewport changes series during the dialog', async () => {
50
+ const { servicesManager, segmentationService, viewports } = createServicesManager(true);
51
+ (promptTimePointScope as jest.Mock).mockImplementation(async () => {
52
+ viewports.set('vp', { displaySetInstanceUIDs: ['other'] });
53
+ return 'all';
54
+ });
55
+
56
+ const result = await createSegmentationForViewport(servicesManager, {
57
+ viewportId: 'vp',
58
+ segmentationType: SegmentationRepresentations.Labelmap,
59
+ });
60
+
61
+ expect(result).toBeUndefined();
62
+ expect(segmentationService.createLabelmapForDisplaySet).not.toHaveBeenCalled();
63
+ });
64
+
65
+ it('skips the dialog when the caller gives timePointScope', async () => {
66
+ const { servicesManager, segmentationService } = createServicesManager(true);
67
+
68
+ await createSegmentationForViewport(servicesManager, {
69
+ viewportId: 'vp',
70
+ options: { timePointScope: 'each', segmentationId: 'seg' },
71
+ segmentationType: SegmentationRepresentations.Labelmap,
72
+ });
73
+
74
+ expect(promptTimePointScope).not.toHaveBeenCalled();
75
+ expect(segmentationService.createLabelmapForDisplaySet).toHaveBeenCalledWith(
76
+ expect.anything(),
77
+ expect.objectContaining({ timePointScope: 'each' })
78
+ );
79
+ });
80
+
81
+ it('shows no dialog for a contour', async () => {
82
+ const { servicesManager, segmentationService } = createServicesManager(true);
83
+
84
+ await createSegmentationForViewport(servicesManager, {
85
+ viewportId: 'vp',
86
+ options: { segmentationId: 'seg' },
87
+ segmentationType: SegmentationRepresentations.Contour,
88
+ });
89
+
90
+ expect(promptTimePointScope).not.toHaveBeenCalled();
91
+ expect(segmentationService.createContourForDisplaySet).toHaveBeenCalled();
92
+ });
93
+ });
@@ -1,4 +1,5 @@
1
- import { utilities as csUtils } from '@cornerstonejs/core';
1
+ import { cache, utilities as csUtils } from '@cornerstonejs/core';
2
+ import { promptTimePointScope, type TimePointScope } from './promptTimePointScope';
2
3
  import { SegmentationRepresentations } from '@cornerstonejs/tools/enums';
3
4
  import i18n from '@ohif/i18n';
4
5
  import { ServicesManager } from '@ohif/core';
@@ -19,6 +20,8 @@ type CreateSegmentationForViewportOptions = {
19
20
  label?: string;
20
21
  segmentationId?: string;
21
22
  createInitialSegment?: boolean;
23
+ /** For a 4D labelmap; when not set, a dialog asks the user. */
24
+ timePointScope?: TimePointScope;
22
25
  };
23
26
 
24
27
  type CreateSegmentationForViewportParams = {
@@ -37,7 +40,13 @@ export async function createSegmentationForViewport(
37
40
  servicesManager: ServicesManager,
38
41
  { viewportId, options = {}, segmentationType }: CreateSegmentationForViewportParams
39
42
  ): Promise<string | undefined> {
40
- const { viewportGridService, displaySetService, segmentationService } = servicesManager.services;
43
+ const {
44
+ viewportGridService,
45
+ displaySetService,
46
+ segmentationService,
47
+ uiDialogService,
48
+ cornerstoneViewportService,
49
+ } = servicesManager.services;
41
50
  const { viewports } = viewportGridService.getState();
42
51
  const targetViewportId = viewportId;
43
52
 
@@ -61,11 +70,42 @@ export async function createSegmentationForViewport(
61
70
 
62
71
  const displaySet = displaySetService.getDisplaySetByUID(displaySetInstanceUID);
63
72
 
73
+ // A 4D labelmap that applies to all time points is one 3D labelmap of the
74
+ // time point on display.
75
+ let timePointIndex: number | undefined;
76
+ let timePointScope: TimePointScope | undefined;
77
+ if (displaySet.isDynamicVolume && segmentationType === SegmentationRepresentations.Labelmap) {
78
+ timePointScope = options.timePointScope;
79
+ if (!timePointScope) {
80
+ timePointScope = await promptTimePointScope(uiDialogService);
81
+ }
82
+ if (!timePointScope) {
83
+ return undefined;
84
+ }
85
+ // The viewport can show a different series after the dialog closes.
86
+ const shownDisplaySetUID = viewportGridService.getState().viewports.get(viewportId)
87
+ ?.displaySetInstanceUIDs?.[0];
88
+ if (!options.displaySetInstanceUID && shownDisplaySetUID !== displaySetInstanceUID) {
89
+ return undefined;
90
+ }
91
+ if (timePointScope === 'all') {
92
+ const csViewport = cornerstoneViewportService.getCornerstoneViewport(viewportId);
93
+ // The volume of this display set, and not the first volume of a fusion.
94
+ const volumeId =
95
+ csViewport?.getAllVolumeIds?.().find(id => id.includes(displaySetInstanceUID)) ??
96
+ csViewport?.getVolumeId?.();
97
+ const groupNumber = volumeId && cache.getVolume(volumeId)?.dimensionGroupNumber;
98
+ timePointIndex = (groupNumber || 1) - 1;
99
+ }
100
+ }
101
+
64
102
  const segmentationCreationOptions = {
65
103
  label,
66
104
  segmentationId,
67
105
  labelIsGenerated: !options.label,
68
106
  segments: _createDefaultSegments(options.createInitialSegment),
107
+ timePointScope,
108
+ timePointIndex,
69
109
  };
70
110
 
71
111
  // This will create the segmentation and register it as a display set
@@ -82,11 +82,21 @@ export function isDisplaySetOverlayable({
82
82
  return false;
83
83
  }
84
84
 
85
- if (!csUtils.isValidVolume(backgroundDisplaySet.imageIds || [])) {
85
+ // A 4D series is a valid volume when one time point is.
86
+ const firstTimePoint = ds => ds.dynamicVolumeInfo?.timePoints?.[0];
87
+
88
+ if (
89
+ !csUtils.isValidVolume(
90
+ firstTimePoint(backgroundDisplaySet) || backgroundDisplaySet.imageIds || []
91
+ )
92
+ ) {
86
93
  return false;
87
94
  }
88
95
 
89
- const imageIds = displaySet.imageIds || displaySet.images?.map(image => image.imageId);
96
+ const imageIds =
97
+ firstTimePoint(displaySet) ||
98
+ displaySet.imageIds ||
99
+ displaySet.images?.map(image => image.imageId);
90
100
 
91
101
  if (!displaySet.isMultiFrame && imageIds?.length > 0 && !csUtils.isValidVolume(imageIds)) {
92
102
  return false;
@@ -0,0 +1,82 @@
1
+ import React from 'react';
2
+ import { Button, Card, CardDescription, CardFooter, CardHeader, CardTitle } from '@ohif/ui-next';
3
+ import i18n from '@ohif/i18n';
4
+
5
+ export type TimePointScope = 'all' | 'each';
6
+
7
+ function TimePointScopeDialog({ hide, onChoose }) {
8
+ const choose = (scope: TimePointScope) => {
9
+ onChoose(scope);
10
+ hide();
11
+ };
12
+ return (
13
+ <div className="text-foreground text-base">
14
+ <p className="mb-3">{i18n.t('Tools:This series has multiple time points.')}</p>
15
+ <div className="grid grid-cols-2 gap-3">
16
+ <Card className="flex flex-col shadow-none">
17
+ <CardHeader className="space-y-1 p-3">
18
+ <CardTitle className="text-base font-normal leading-snug tracking-normal">
19
+ {i18n.t('Tools:Create a segmentation for each time point')}
20
+ </CardTitle>
21
+ <CardDescription className="leading-snug">
22
+ {i18n.t('Tools:Changes apply only to the time point shown.')}
23
+ </CardDescription>
24
+ </CardHeader>
25
+ <CardFooter className="mt-auto p-3 pt-0">
26
+ <Button
27
+ variant="default"
28
+ className="w-full"
29
+ dataCY="time-point-scope-each"
30
+ onClick={() => choose('each')}
31
+ >
32
+ {i18n.t('Tools:Each time point')}
33
+ </Button>
34
+ </CardFooter>
35
+ </Card>
36
+ <Card className="flex flex-col shadow-none">
37
+ <CardHeader className="space-y-1 p-3">
38
+ <CardTitle className="text-base font-normal leading-snug tracking-normal">
39
+ {i18n.t('Tools:Create one segmentation, shown on every time point')}
40
+ </CardTitle>
41
+ <CardDescription className="leading-snug">
42
+ {i18n.t('Tools:Changes apply to every time point.')}
43
+ </CardDescription>
44
+ </CardHeader>
45
+ <CardFooter className="mt-auto p-3 pt-0">
46
+ <Button
47
+ variant="default"
48
+ className="w-full"
49
+ dataCY="time-point-scope-all"
50
+ onClick={() => choose('all')}
51
+ >
52
+ {i18n.t('Tools:All time points')}
53
+ </Button>
54
+ </CardFooter>
55
+ </Card>
56
+ </div>
57
+ </div>
58
+ );
59
+ }
60
+
61
+ /**
62
+ * Asks whether a new segmentation of a 4D series applies to all time points
63
+ * or to each time point separately. Resolves to undefined when the user
64
+ * cancels or closes the dialog.
65
+ */
66
+ export function promptTimePointScope(
67
+ uiDialogService: AppTypes.UIDialogService
68
+ ): Promise<TimePointScope | undefined> {
69
+ return new Promise(resolve => {
70
+ uiDialogService.show({
71
+ id: 'time-point-scope',
72
+ title: i18n.t('Tools:Segmentation time points'),
73
+ content: TimePointScopeDialog,
74
+ shouldCloseOnEsc: true,
75
+ contentProps: { onChoose: resolve },
76
+ onClose: id => {
77
+ uiDialogService.hide(id);
78
+ resolve(undefined);
79
+ },
80
+ });
81
+ });
82
+ }