@ohif/extension-cornerstone 3.14.0-beta.121 → 3.14.0-beta.123

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.121",
3
+ "version": "3.14.0-beta.123",
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.24.0",
39
- "@ohif/core": "3.14.0-beta.121",
40
- "@ohif/extension-default": "3.14.0-beta.121",
41
- "@ohif/ui-next": "3.14.0-beta.121",
39
+ "@ohif/core": "3.14.0-beta.123",
40
+ "@ohif/extension-default": "3.14.0-beta.123",
41
+ "@ohif/ui-next": "3.14.0-beta.123",
42
42
  "dcmjs": "0.52.0",
43
43
  "dicom-parser": "1.8.21",
44
44
  "hammerjs": "2.0.8",
@@ -58,6 +58,10 @@ import {
58
58
  useSelectedSegmentationsForViewportStore,
59
59
  } from './stores';
60
60
  import { toolNames } from './initCornerstoneTools';
61
+ import getVolumeCroppingToolGroup, {
62
+ getVolumeCroppingState,
63
+ setDisjointSegmentationMode,
64
+ } from './utils/getVolumeCroppingToolGroup';
61
65
  import CornerstoneViewportDownloadForm from './utils/CornerstoneViewportDownloadForm';
62
66
  import { updateSegmentBidirectionalStats } from './utils/updateSegmentationStats';
63
67
  import { generateSegmentationCSVReport } from './utils/generateSegmentationCSVReport';
@@ -190,6 +194,10 @@ function commandsModule({
190
194
  return toolGroup?.id;
191
195
  }
192
196
 
197
+ function _getVolumeCroppingToolGroup(viewportId?: string) {
198
+ return getVolumeCroppingToolGroup(toolGroupService, viewportId);
199
+ }
200
+
193
201
  function _usesPrimaryActivation(bindings) {
194
202
  if (!bindings?.length) {
195
203
  return true;
@@ -1229,6 +1237,96 @@ function commandsModule({
1229
1237
  const renderingEngine = cornerstoneViewportService.getRenderingEngine();
1230
1238
  renderingEngine.render();
1231
1239
  },
1240
+ /**
1241
+ * Steps the crop tool through its three states: off -> Active -> Passive -> off, or sets
1242
+ * `mode` directly. Active: VolumeCropping owns the primary button (drag a handle to crop,
1243
+ * drag elsewhere to rotate). Passive: the crop box stays, and the previous primary tool
1244
+ * comes back. VolumeCroppingControl shows the same box in the MPR viewports.
1245
+ */
1246
+ cycleVolumeCropping: ({ mode, viewportId }: { mode?: string; viewportId?: string } = {}) => {
1247
+ const toolGroup = _getVolumeCroppingToolGroup(
1248
+ viewportId ?? viewportGridService.getActiveViewportId()
1249
+ );
1250
+ if (!toolGroup) {
1251
+ return;
1252
+ }
1253
+ const { Active, Passive, Disabled } = Enums.ToolModes;
1254
+ const current = getVolumeCroppingState(toolGroup);
1255
+ const next = mode ?? (current === Active ? Passive : current === Passive ? Disabled : Active);
1256
+ if (next === current) {
1257
+ // A second activation would record VolumeCropping as its own previous primary tool.
1258
+ return;
1259
+ }
1260
+ const tool = toolGroup.getToolInstance(toolNames.VolumeCropping);
1261
+
1262
+ // The crop box is built in onSetToolActive, so Disabled -> Passive activates first.
1263
+ if (next === Active || current === Disabled) {
1264
+ actions.setToolActive({
1265
+ toolName: toolNames.VolumeCropping,
1266
+ toolGroupId: toolGroup.id,
1267
+ bindings: [
1268
+ { mouseButton: Enums.MouseBindings.Primary },
1269
+ {
1270
+ mouseButton: Enums.MouseBindings.Primary,
1271
+ modifierKey: Enums.KeyboardBindings.Shift,
1272
+ },
1273
+ ],
1274
+ });
1275
+ // onSetToolActive hides the box, so show it after activation.
1276
+ tool.setClippingPlanesVisible(true);
1277
+ }
1278
+ if (next !== Active && current !== Passive) {
1279
+ actions.setToolActive({
1280
+ toolName: toolGroup.getPrevActivePrimaryToolName(),
1281
+ toolGroupId: toolGroup.id,
1282
+ });
1283
+ // The SHIFT binding would otherwise keep VolumeCropping in Active mode.
1284
+ toolGroup.setToolPassive(toolNames.VolumeCropping, { removeAllBindings: true });
1285
+ }
1286
+ if (next === Disabled) {
1287
+ tool.setClippingPlanesVisible(false);
1288
+ toolGroup.setToolDisabled(toolNames.VolumeCropping);
1289
+ }
1290
+
1291
+ toolGroupService.getToolGroupIds().forEach(toolGroupId => {
1292
+ const controlGroup = toolGroupService.getToolGroup(toolGroupId);
1293
+ if (!controlGroup?.hasTool(toolNames.VolumeCroppingControl)) {
1294
+ return;
1295
+ }
1296
+ if (next === Disabled) {
1297
+ controlGroup.setToolDisabled(toolNames.VolumeCroppingControl);
1298
+ } else if (current === Disabled) {
1299
+ // Enabled adds the listener that follows the 3D handles; Active adds the box.
1300
+ // No binding: handles of an Active tool drag anyway, and a Primary binding would
1301
+ // overwrite the MPR group's previous primary tool.
1302
+ controlGroup.setToolEnabled(toolNames.VolumeCroppingControl);
1303
+ controlGroup.setToolActive(toolNames.VolumeCroppingControl);
1304
+ }
1305
+ });
1306
+ },
1307
+ /** Shows or hides the drag handles of the crop box. */
1308
+ toggle3Dhandles: ({ viewportId, visible }: { viewportId?: string; visible?: boolean } = {}) => {
1309
+ const tool = _getVolumeCroppingToolGroup(viewportId)?.getToolInstance(
1310
+ toolNames.VolumeCropping
1311
+ );
1312
+ tool?.setHandlesVisible(visible ?? !tool.getHandlesVisible());
1313
+ },
1314
+ /** Makes a plain drag rotate the crop planes, not the camera (SHIFT-drag always does). */
1315
+ togglePlaneRotation: ({
1316
+ viewportId,
1317
+ enabled,
1318
+ }: { viewportId?: string; enabled?: boolean } = {}) => {
1319
+ const tool = _getVolumeCroppingToolGroup(viewportId)?.getToolInstance(
1320
+ toolNames.VolumeCropping
1321
+ );
1322
+ tool?.setRotatePlanesOnDrag(enabled ?? !tool.getRotatePlanesOnDrag());
1323
+ },
1324
+ /** Shows the segmentation only outside the crop box (toggles when `enabled` is undefined). */
1325
+ setVolumeCroppingDisjointMode: ({
1326
+ viewportId,
1327
+ enabled,
1328
+ }: { viewportId?: string; enabled?: boolean } = {}) =>
1329
+ setDisjointSegmentationMode(_getVolumeCroppingToolGroup(viewportId), enabled),
1232
1330
  /** Looks at a volume3d viewport from an anatomical direction (S, I, R, L, A, P). */
1233
1331
  setViewport3DViewDirection: ({
1234
1332
  viewportId,
@@ -2848,6 +2946,18 @@ function commandsModule({
2848
2946
  updateVolumeData: {
2849
2947
  commandFn: actions.updateVolumeData,
2850
2948
  },
2949
+ cycleVolumeCropping: {
2950
+ commandFn: actions.cycleVolumeCropping,
2951
+ },
2952
+ toggle3Dhandles: {
2953
+ commandFn: actions.toggle3Dhandles,
2954
+ },
2955
+ togglePlaneRotation: {
2956
+ commandFn: actions.togglePlaneRotation,
2957
+ },
2958
+ setVolumeCroppingDisjointMode: {
2959
+ commandFn: actions.setVolumeCroppingDisjointMode,
2960
+ },
2851
2961
  setViewport3DViewDirection: {
2852
2962
  commandFn: actions.setViewport3DViewDirection,
2853
2963
  },
@@ -0,0 +1,169 @@
1
+ import React, { useState } from 'react';
2
+ import { useSystem } from '@ohif/core';
3
+ import {
4
+ Button,
5
+ Icons,
6
+ Popover,
7
+ PopoverContent,
8
+ PopoverTrigger,
9
+ Switch,
10
+ useIconPresentation,
11
+ } from '@ohif/ui-next';
12
+ import i18n from 'i18next';
13
+ import { Enums } from '@cornerstonejs/tools';
14
+ import { toolNames } from '../../initCornerstoneTools';
15
+ import { getVolumeCroppingState } from '../../utils/getVolumeCroppingToolGroup';
16
+
17
+ type CroppingState = { handles: boolean; rotatePlanes: boolean; disjoint: boolean };
18
+
19
+ /**
20
+ * Viewport corner menu for the VolumeCropping tool of a volume3d viewport, shown while
21
+ * the crop box is on: the drag handles, whether a plain drag rotates the crop planes, and
22
+ * whether the segmentation shows only outside the crop box.
23
+ * The VolumeCropping toolbar button turns the crop box on and off.
24
+ */
25
+ function VolumeCroppingMenu({
26
+ viewportId,
27
+ location,
28
+ isOpen = false,
29
+ onOpen,
30
+ onClose,
31
+ disabled,
32
+ ...props
33
+ }: withAppTypes<{
34
+ viewportId: string;
35
+ location?: string;
36
+ isOpen?: boolean;
37
+ onOpen?: () => void;
38
+ onClose?: () => void;
39
+ disabled?: boolean;
40
+ }>) {
41
+ const { servicesManager, commandsManager } = useSystem();
42
+ const { toolGroupService, toolbarService } = servicesManager.services;
43
+ const { IconContainer, className: iconClassName, containerProps } = useIconPresentation();
44
+ const [state, setState] = useState<CroppingState>({
45
+ handles: false,
46
+ rotatePlanes: false,
47
+ disjoint: false,
48
+ });
49
+
50
+ // The tool owns this state, and hotkeys or other code can change it, so read it on open.
51
+ const readState = () => {
52
+ const tool = toolGroupService
53
+ .getToolGroupForViewport(viewportId)
54
+ ?.getToolInstance(toolNames.VolumeCropping);
55
+ setState({
56
+ handles: !!tool?.getHandlesVisible(),
57
+ rotatePlanes: !!tool?.getRotatePlanesOnDrag(),
58
+ disjoint: !!tool?.getDisjointSegmentation?.(),
59
+ });
60
+ };
61
+
62
+ const run = (commandName: string, options: Record<string, unknown>) => {
63
+ commandsManager.runCommand(commandName, { viewportId, ...options });
64
+ readState();
65
+ };
66
+
67
+ const handleOpenChange = (openState: boolean) => {
68
+ if (openState) {
69
+ readState();
70
+ onOpen?.();
71
+ } else {
72
+ onClose?.();
73
+ }
74
+ };
75
+
76
+ // Toolbar state is evaluated for the active viewport only, so check this viewport too.
77
+ const toolGroup = toolGroupService.getToolGroupForViewport(viewportId);
78
+ if (
79
+ !toolGroup?.hasTool(toolNames.VolumeCropping) ||
80
+ getVolumeCroppingState(toolGroup) === Enums.ToolModes.Disabled
81
+ ) {
82
+ return null;
83
+ }
84
+
85
+ const { align, side } = toolbarService.getAlignAndSide(Number(location));
86
+ const Icon = <Icons.Crop className={iconClassName} />;
87
+ const ariaLabel = i18n.t('Buttons:Volume Cropping');
88
+
89
+ const rows = [
90
+ {
91
+ label: i18n.t('Buttons:Show handles'),
92
+ tooltip: i18n.t('Buttons:Show the drag handles of the crop box'),
93
+ checked: state.handles,
94
+ onChange: (visible: boolean) => run('toggle3Dhandles', { visible }),
95
+ },
96
+ {
97
+ label: i18n.t('Buttons:Rotate planes on drag'),
98
+ tooltip: i18n.t(
99
+ 'Buttons:While the crop tool is active, a drag rotates the crop planes, not the camera'
100
+ ),
101
+ checked: state.rotatePlanes,
102
+ onChange: (enabled: boolean) => run('togglePlaneRotation', { enabled }),
103
+ },
104
+ {
105
+ label: i18n.t('Buttons:Segmentation outside the crop box'),
106
+ tooltip: i18n.t(
107
+ 'Buttons:Show the segmentation only outside the crop box. When off, the crop box also crops the segmentation'
108
+ ),
109
+ checked: state.disjoint,
110
+ onChange: (enabled: boolean) => run('setVolumeCroppingDisjointMode', { enabled }),
111
+ },
112
+ ];
113
+
114
+ return (
115
+ <Popover
116
+ open={isOpen}
117
+ onOpenChange={handleOpenChange}
118
+ >
119
+ <PopoverTrigger
120
+ asChild
121
+ className="flex items-center justify-center"
122
+ >
123
+ <div>
124
+ {IconContainer ? (
125
+ <IconContainer
126
+ disabled={disabled}
127
+ icon="Crop"
128
+ aria-label={ariaLabel}
129
+ {...props}
130
+ {...containerProps}
131
+ >
132
+ {Icon}
133
+ </IconContainer>
134
+ ) : (
135
+ <Button
136
+ variant="ghost"
137
+ size="icon"
138
+ disabled={disabled}
139
+ aria-label={ariaLabel}
140
+ >
141
+ {Icon}
142
+ </Button>
143
+ )}
144
+ </div>
145
+ </PopoverTrigger>
146
+ <PopoverContent
147
+ className="flex w-[240px] flex-col gap-2 rounded p-2"
148
+ align={align}
149
+ side={side}
150
+ >
151
+ {rows.map(row => (
152
+ <label
153
+ key={row.label}
154
+ title={row.tooltip}
155
+ className="flex items-center justify-between text-sm"
156
+ >
157
+ {row.label}
158
+ <Switch
159
+ checked={row.checked}
160
+ onCheckedChange={row.onChange}
161
+ />
162
+ </label>
163
+ ))}
164
+ </PopoverContent>
165
+ </Popover>
166
+ );
167
+ }
168
+
169
+ export default VolumeCroppingMenu;
@@ -113,6 +113,19 @@ const toolbarButtons: Button[] = [
113
113
  },
114
114
  },
115
115
  },
116
+ {
117
+ id: 'volumeCroppingMenu',
118
+ uiType: 'ohif.volumeCroppingMenu',
119
+ props: {
120
+ icon: 'Crop',
121
+ label: i18n.t('Buttons:Volume Cropping'),
122
+ tooltip: i18n.t('Buttons:Crop the 3D volume'),
123
+ evaluate: {
124
+ name: 'evaluate.volumeCroppingMenu',
125
+ hideWhenDisabled: true,
126
+ },
127
+ },
128
+ },
116
129
  {
117
130
  id: 'windowLevelMenuEmbedded',
118
131
  uiType: 'ohif.windowLevelMenuEmbedded',
@@ -276,6 +289,22 @@ const toolbarButtons: Button[] = [
276
289
  ],
277
290
  },
278
291
  },
292
+ {
293
+ id: 'VolumeCropping',
294
+ uiType: 'ohif.toolButton',
295
+ props: {
296
+ icon: 'Crop',
297
+ label: i18n.t('Buttons:Volume Cropping'),
298
+ tooltip: i18n.t(
299
+ 'Buttons:Crop the 3D volume. Click to activate, again to keep the crop box only, again to remove it'
300
+ ),
301
+ commands: 'cycleVolumeCropping',
302
+ evaluate: {
303
+ name: 'evaluate.volumeCropping',
304
+ disabledText: i18n.t('Buttons:Select a 3D viewport to enable this tool'),
305
+ },
306
+ },
307
+ },
279
308
  {
280
309
  id: 'ImageOverlayViewer',
281
310
  uiType: 'ohif.toolButton',
@@ -745,6 +774,7 @@ export const toolbarSections = {
745
774
  'modalityLoadBadge',
746
775
  'trackingStatus',
747
776
  'navigationComponent',
777
+ 'volumeCroppingMenu',
748
778
  ],
749
779
 
750
780
  [TOOLBAR_SECTIONS.viewportActionMenu.bottomLeft]: ['windowLevelMenu'],
@@ -781,6 +811,7 @@ export const toolbarSections = {
781
811
  'UltrasoundDirectionalTool',
782
812
  'WindowLevelRegion',
783
813
  'SegmentLabelTool',
814
+ 'VolumeCropping',
784
815
  ],
785
816
  };
786
817
 
@@ -4,6 +4,11 @@ import { getViewportAdapter, isVolumeRenderingViewport } from './services/Viewpo
4
4
  import { utils } from '@ohif/ui-next';
5
5
  import { ViewportDataOverlayMenuWrapper } from './components/ViewportDataOverlaySettingMenu/ViewportDataOverlayMenuWrapper';
6
6
  import { ViewportOrientationMenuWrapper } from './components/ViewportOrientationMenu/ViewportOrientationMenuWrapper';
7
+ import VolumeCroppingMenu from './components/VolumeCroppingMenu/VolumeCroppingMenu';
8
+ import { toolNames } from './initCornerstoneTools';
9
+ import getVolumeCroppingToolGroup, {
10
+ getVolumeCroppingState,
11
+ } from './utils/getVolumeCroppingToolGroup';
7
12
  import { WindowLevelActionMenuWrapper } from './components/WindowLevelActionMenu/WindowLevelActionMenuWrapper';
8
13
  import { VOIManualControlMenuWrapper } from './components/VOIManualControlMenu';
9
14
  import { ThresholdMenuWrapper } from './components/ThresholdMenu/ThresholdMenuWrapper';
@@ -226,6 +231,42 @@ export default function getToolbarModule({ servicesManager, extensionManager }:
226
231
  };
227
232
  },
228
233
  },
234
+ {
235
+ name: 'ohif.volumeCroppingMenu',
236
+ defaultComponent: VolumeCroppingMenu,
237
+ },
238
+ {
239
+ // The corner menu shows only on a volume3d viewport while the crop box is on.
240
+ name: 'evaluate.volumeCroppingMenu',
241
+ evaluate: ({ viewportId }) => {
242
+ const viewport = cornerstoneViewportService.getCornerstoneViewport(viewportId);
243
+ const toolGroup = toolGroupService.getToolGroupForViewport(viewportId);
244
+ const isOn =
245
+ toolGroup?.hasTool(toolNames.VolumeCropping) &&
246
+ getVolumeCroppingState(toolGroup) !== Enums.ToolModes.Disabled;
247
+ return {
248
+ disabled: !viewport || !isOn || getViewportAdapter(viewport).getShape() !== 'volume3d',
249
+ };
250
+ },
251
+ },
252
+ {
253
+ // Three states of the crop button: Active (highlighted), Passive (toggled), off.
254
+ name: 'evaluate.volumeCropping',
255
+ evaluate: ({ viewportId, disabledText }) => {
256
+ const toolGroup = getVolumeCroppingToolGroup(toolGroupService, viewportId);
257
+ if (!toolGroup) {
258
+ return getDisabledState(disabledText);
259
+ }
260
+ const mode = getVolumeCroppingState(toolGroup);
261
+ return mode === Enums.ToolModes.Active
262
+ ? { disabled: false, isActive: true }
263
+ : {
264
+ disabled: false,
265
+ isActive: false,
266
+ className: utils.getToggledClassName(mode === Enums.ToolModes.Passive),
267
+ };
268
+ },
269
+ },
229
270
  {
230
271
  name: 'ohif.windowLevelMenu',
231
272
  defaultComponent: WindowLevelActionMenuWrapper,
@@ -45,6 +45,8 @@ import {
45
45
  SculptorTool,
46
46
  SplineContourSegmentationTool,
47
47
  LabelMapEditWithContourTool,
48
+ VolumeCroppingTool,
49
+ VolumeCroppingControlTool,
48
50
  OrientationControllerTool,
49
51
  } from '@cornerstonejs/tools';
50
52
  import { LabelmapSlicePropagationTool, MarkerLabelmapTool } from '@cornerstonejs/ai';
@@ -121,6 +123,8 @@ export default function initCornerstoneTools(configuration = {}) {
121
123
  addTool(SculptorTool);
122
124
  addTool(SplineContourSegmentationTool);
123
125
  addTool(LabelMapEditWithContourTool);
126
+ addTool(VolumeCroppingTool);
127
+ addTool(VolumeCroppingControlTool);
124
128
  // Modify annotation tools to use dashed lines on SR
125
129
  const annotationStyle = {
126
130
  textBoxFontSize: '15px',
@@ -186,6 +190,8 @@ const toolNames = {
186
190
  SculptorTool: SculptorTool.toolName,
187
191
  SplineContourSegmentation: SplineContourSegmentationTool.toolName,
188
192
  LabelMapEditWithContourTool: LabelMapEditWithContourTool.toolName,
193
+ VolumeCropping: VolumeCroppingTool.toolName,
194
+ VolumeCroppingControl: VolumeCroppingControlTool.toolName,
189
195
  };
190
196
 
191
197
  export { toolNames };
@@ -0,0 +1,63 @@
1
+ import getVolumeCroppingToolGroup, {
2
+ getVolumeCroppingState,
3
+ setDisjointSegmentationMode,
4
+ } from './getVolumeCroppingToolGroup';
5
+
6
+ jest.mock('@cornerstonejs/tools', () => ({
7
+ Enums: { ToolModes: { Active: 'Active', Passive: 'Passive', Disabled: 'Disabled' } },
8
+ }));
9
+ jest.mock('../initCornerstoneTools', () => ({
10
+ toolNames: { VolumeCropping: 'VolumeCropping' },
11
+ }));
12
+
13
+ const makeGroup = ({
14
+ mode = undefined,
15
+ primary = 'TrackballRotate',
16
+ viewportIds = ['v3d'],
17
+ } = {}) => ({
18
+ hasTool: name => name === 'VolumeCropping',
19
+ getToolOptions: () => (mode ? { mode } : undefined),
20
+ getActivePrimaryMouseButtonTool: () => primary,
21
+ getViewportIds: () => viewportIds,
22
+ });
23
+
24
+ describe('getVolumeCroppingState', () => {
25
+ it('reports the state that the user sees', () => {
26
+ expect(getVolumeCroppingState(makeGroup())).toBe('Disabled');
27
+ expect(getVolumeCroppingState(makeGroup({ mode: 'Disabled' }))).toBe('Disabled');
28
+ expect(getVolumeCroppingState(makeGroup({ mode: 'Active', primary: 'VolumeCropping' }))).toBe(
29
+ 'Active'
30
+ );
31
+ // CS3D keeps Active mode while a SHIFT binding remains; another primary tool means Passive.
32
+ expect(getVolumeCroppingState(makeGroup({ mode: 'Active' }))).toBe('Passive');
33
+ });
34
+ });
35
+
36
+ describe('getVolumeCroppingToolGroup', () => {
37
+ it('uses the viewport group, else the first populated group with the tool', () => {
38
+ const empty = makeGroup({ viewportIds: [] });
39
+ const populated = makeGroup();
40
+ const service = {
41
+ getToolGroupForViewport: id => (id === 'v3d' ? populated : undefined),
42
+ getToolGroupIds: () => ['empty', 'populated'],
43
+ getToolGroup: id => (id === 'empty' ? empty : populated),
44
+ };
45
+ expect(getVolumeCroppingToolGroup(service as never, 'v3d')).toBe(populated);
46
+ expect(getVolumeCroppingToolGroup(service as never, 'mpr')).toBe(populated);
47
+ });
48
+ });
49
+
50
+ describe('setDisjointSegmentationMode', () => {
51
+ it('sets the mode on the crop tool of the tool group, and toggles it without a value', () => {
52
+ let disjoint = false;
53
+ const tool = {
54
+ getDisjointSegmentation: () => disjoint,
55
+ setDisjointSegmentation: jest.fn(value => (disjoint = value)),
56
+ };
57
+ const toolGroup = { getToolInstance: name => (name === 'VolumeCropping' ? tool : undefined) };
58
+ expect(setDisjointSegmentationMode(toolGroup, true)).toBe(true);
59
+ expect(tool.setDisjointSegmentation).toHaveBeenLastCalledWith(true);
60
+ expect(setDisjointSegmentationMode(toolGroup)).toBe(false);
61
+ expect(disjoint).toBe(false);
62
+ });
63
+ });
@@ -0,0 +1,53 @@
1
+ import { Enums } from '@cornerstonejs/tools';
2
+ import { toolNames } from '../initCornerstoneTools';
3
+
4
+ /**
5
+ * The state of the crop tool as the user sees it: Active when it is the primary tool,
6
+ * Passive when the crop box shows but another tool is primary, Disabled when it is off.
7
+ * The CS3D mode alone is not enough: a tool keeps Active mode while its SHIFT binding
8
+ * remains, after another tool takes the primary button.
9
+ */
10
+ export function getVolumeCroppingState(toolGroup): Enums.ToolModes {
11
+ const mode = toolGroup?.getToolOptions(toolNames.VolumeCropping)?.mode;
12
+ if (!mode || mode === Enums.ToolModes.Disabled) {
13
+ return Enums.ToolModes.Disabled;
14
+ }
15
+ return toolGroup.getActivePrimaryMouseButtonTool() === toolNames.VolumeCropping
16
+ ? Enums.ToolModes.Active
17
+ : Enums.ToolModes.Passive;
18
+ }
19
+
20
+ /**
21
+ * Sets the disjoint segmentation mode of the crop tool of `toolGroup`: the segmentation
22
+ * shows only outside the crop box. Toggles the mode when `enabled` is undefined. Returns
23
+ * the new state, or undefined when the tool group or the CS3D tool has no such mode.
24
+ */
25
+ export function setDisjointSegmentationMode(toolGroup, enabled?: boolean): boolean | undefined {
26
+ const tool = toolGroup?.getToolInstance(toolNames.VolumeCropping);
27
+ if (!tool?.setDisjointSegmentation) {
28
+ return undefined;
29
+ }
30
+ const next = enabled ?? !tool.getDisjointSegmentation();
31
+ tool.setDisjointSegmentation(next);
32
+ return next;
33
+ }
34
+
35
+ /**
36
+ * The tool group that holds VolumeCropping: the group of `viewportId` when it has the
37
+ * tool, otherwise the first group with the tool that has viewports. The second case
38
+ * lets the main toolbar work while an MPR viewport is active.
39
+ */
40
+ export default function getVolumeCroppingToolGroup(
41
+ toolGroupService: AppTypes.Services['toolGroupService'],
42
+ viewportId?: string
43
+ ) {
44
+ const hasCropping = toolGroup =>
45
+ toolGroup?.hasTool(toolNames.VolumeCropping) ? toolGroup : undefined;
46
+ return (
47
+ (viewportId && hasCropping(toolGroupService.getToolGroupForViewport(viewportId))) ||
48
+ toolGroupService
49
+ .getToolGroupIds()
50
+ .map(id => hasCropping(toolGroupService.getToolGroup(id)))
51
+ .find(toolGroup => toolGroup?.getViewportIds().length)
52
+ );
53
+ }