@ohif/extension-cornerstone 3.14.0-beta.120 → 3.14.0-beta.121
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/ohif-extension-cornerstone.umd.js +1 -1
- package/package.json +4 -4
- package/src/commandsModule.ts +38 -0
- package/src/components/ViewportOrientationMenu/Viewport3DCompassMenu.tsx +261 -0
- package/src/components/ViewportOrientationMenu/ViewportOrientationMenuWrapper.tsx +11 -0
- package/src/index.tsx +2 -0
- package/src/initCornerstoneTools.js +3 -0
- package/src/utils/volume3dView.test.ts +48 -0
- package/src/utils/volume3dView.ts +113 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohif/extension-cornerstone",
|
|
3
|
-
"version": "3.14.0-beta.
|
|
3
|
+
"version": "3.14.0-beta.121",
|
|
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.
|
|
40
|
-
"@ohif/extension-default": "3.14.0-beta.
|
|
41
|
-
"@ohif/ui-next": "3.14.0-beta.
|
|
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",
|
|
42
42
|
"dcmjs": "0.52.0",
|
|
43
43
|
"dicom-parser": "1.8.21",
|
|
44
44
|
"hammerjs": "2.0.8",
|
package/src/commandsModule.ts
CHANGED
|
@@ -46,6 +46,12 @@ import {
|
|
|
46
46
|
} from './utils/getLegacyViewportType';
|
|
47
47
|
import { viewportOperations as ops } from './services/ViewportService/backends/viewportOperations';
|
|
48
48
|
import { getViewportAdapter } from './services/ViewportService/adapter';
|
|
49
|
+
import {
|
|
50
|
+
rotateVolume3D,
|
|
51
|
+
setVolume3DSpin,
|
|
52
|
+
setVolume3DViewDirection,
|
|
53
|
+
type ViewDirection,
|
|
54
|
+
} from './utils/volume3dView';
|
|
49
55
|
import {
|
|
50
56
|
usePositionPresentationStore,
|
|
51
57
|
useSegmentationPresentationStore,
|
|
@@ -1223,6 +1229,29 @@ function commandsModule({
|
|
|
1223
1229
|
const renderingEngine = cornerstoneViewportService.getRenderingEngine();
|
|
1224
1230
|
renderingEngine.render();
|
|
1225
1231
|
},
|
|
1232
|
+
/** Looks at a volume3d viewport from an anatomical direction (S, I, R, L, A, P). */
|
|
1233
|
+
setViewport3DViewDirection: ({
|
|
1234
|
+
viewportId,
|
|
1235
|
+
direction,
|
|
1236
|
+
}: {
|
|
1237
|
+
viewportId: string;
|
|
1238
|
+
direction: ViewDirection;
|
|
1239
|
+
}) => {
|
|
1240
|
+
setVolume3DViewDirection(
|
|
1241
|
+
cornerstoneViewportService.getCornerstoneViewport(viewportId),
|
|
1242
|
+
direction
|
|
1243
|
+
);
|
|
1244
|
+
},
|
|
1245
|
+
/** Orbits a volume3d viewport camera by `angle` degrees about the patient superior axis. */
|
|
1246
|
+
rotateViewport3DBy: ({ viewportId, angle }: { viewportId: string; angle: number }) => {
|
|
1247
|
+
rotateVolume3D(cornerstoneViewportService.getCornerstoneViewport(viewportId), angle);
|
|
1248
|
+
},
|
|
1249
|
+
/** Spins a volume3d viewport continuously; `speed` 0 stops it, a negative value reverses it. */
|
|
1250
|
+
setViewport3DSpin: ({ viewportId, speed }: { viewportId: string; speed: number }) => {
|
|
1251
|
+
setVolume3DSpin(viewportId, speed, id =>
|
|
1252
|
+
cornerstoneViewportService.getCornerstoneViewport(id)
|
|
1253
|
+
);
|
|
1254
|
+
},
|
|
1226
1255
|
toggleEnabledDisabledToolbar({ value, itemId, toolGroupId }) {
|
|
1227
1256
|
const toolName = itemId || value;
|
|
1228
1257
|
toolGroupId = toolGroupId ?? _getActiveViewportToolGroupId();
|
|
@@ -2819,6 +2848,15 @@ function commandsModule({
|
|
|
2819
2848
|
updateVolumeData: {
|
|
2820
2849
|
commandFn: actions.updateVolumeData,
|
|
2821
2850
|
},
|
|
2851
|
+
setViewport3DViewDirection: {
|
|
2852
|
+
commandFn: actions.setViewport3DViewDirection,
|
|
2853
|
+
},
|
|
2854
|
+
rotateViewport3DBy: {
|
|
2855
|
+
commandFn: actions.rotateViewport3DBy,
|
|
2856
|
+
},
|
|
2857
|
+
setViewport3DSpin: {
|
|
2858
|
+
commandFn: actions.setViewport3DSpin,
|
|
2859
|
+
},
|
|
2822
2860
|
toggleEnabledDisabledToolbar: {
|
|
2823
2861
|
commandFn: actions.toggleEnabledDisabledToolbar,
|
|
2824
2862
|
},
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import React, { useRef, useState } from 'react';
|
|
2
|
+
import { useSystem } from '@ohif/core';
|
|
3
|
+
import {
|
|
4
|
+
Button,
|
|
5
|
+
Icons,
|
|
6
|
+
Popover,
|
|
7
|
+
PopoverContent,
|
|
8
|
+
PopoverTrigger,
|
|
9
|
+
useIconPresentation,
|
|
10
|
+
} from '@ohif/ui-next';
|
|
11
|
+
import { getVolume3DSpin, type ViewDirection } from '../../utils/volume3dView';
|
|
12
|
+
|
|
13
|
+
const CENTER = 50;
|
|
14
|
+
const LINE_RADIUS = 30;
|
|
15
|
+
const LABEL_RADIUS = 42;
|
|
16
|
+
|
|
17
|
+
// Screen angle of each label; P-A is drawn as a diagonal to suggest depth.
|
|
18
|
+
const DIRECTIONS: { direction: ViewDirection; angle: number }[] = [
|
|
19
|
+
{ direction: 'S', angle: -90 },
|
|
20
|
+
{ direction: 'I', angle: 90 },
|
|
21
|
+
{ direction: 'R', angle: 180 },
|
|
22
|
+
{ direction: 'L', angle: 0 },
|
|
23
|
+
{ direction: 'P', angle: 225 },
|
|
24
|
+
{ direction: 'A', angle: 45 },
|
|
25
|
+
];
|
|
26
|
+
const AXIS_ANGLES = [180, -90, 225];
|
|
27
|
+
|
|
28
|
+
const toXY = (angleDegrees: number, radius: number) => {
|
|
29
|
+
const radians = (angleDegrees * Math.PI) / 180;
|
|
30
|
+
return { x: CENTER + radius * Math.cos(radians), y: CENTER + radius * Math.sin(radians) };
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Orientation menu of a volume3d viewport, in the style of the 3D Slicer view controller:
|
|
35
|
+
* click S, I, R, L, A or P to look from that side, and set a continuous spin speed.
|
|
36
|
+
*/
|
|
37
|
+
function Viewport3DCompassMenu({
|
|
38
|
+
location,
|
|
39
|
+
viewportId,
|
|
40
|
+
isOpen = false,
|
|
41
|
+
onOpen,
|
|
42
|
+
onClose,
|
|
43
|
+
disabled,
|
|
44
|
+
...props
|
|
45
|
+
}: withAppTypes<{
|
|
46
|
+
location?: string;
|
|
47
|
+
viewportId: string;
|
|
48
|
+
isOpen?: boolean;
|
|
49
|
+
onOpen?: () => void;
|
|
50
|
+
onClose?: () => void;
|
|
51
|
+
disabled?: boolean;
|
|
52
|
+
}>) {
|
|
53
|
+
const { servicesManager, commandsManager } = useSystem();
|
|
54
|
+
const { toolbarService } = servicesManager.services;
|
|
55
|
+
const { IconContainer, className: iconClassName, containerProps } = useIconPresentation();
|
|
56
|
+
const [spin, setSpin] = useState(0);
|
|
57
|
+
const triggerRef = useRef<HTMLDivElement>(null);
|
|
58
|
+
const interactedOutsideRef = useRef(false);
|
|
59
|
+
|
|
60
|
+
const changeSpin = (speed: number) => {
|
|
61
|
+
commandsManager.runCommand('setViewport3DSpin', { viewportId, speed });
|
|
62
|
+
setSpin(speed);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const lookFrom = (direction: ViewDirection) =>
|
|
66
|
+
commandsManager.runCommand('setViewport3DViewDirection', { viewportId, direction });
|
|
67
|
+
|
|
68
|
+
const handleOpenChange = (openState: boolean) => {
|
|
69
|
+
if (openState) {
|
|
70
|
+
// The spin can stop without this menu, for example when the viewport changes.
|
|
71
|
+
setSpin(getVolume3DSpin(viewportId));
|
|
72
|
+
onOpen?.();
|
|
73
|
+
} else {
|
|
74
|
+
onClose?.();
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const { align, side } = toolbarService.getAlignAndSide(Number(location));
|
|
79
|
+
const Icon = <Icons.OrientationSwitch className={iconClassName} />;
|
|
80
|
+
const gradientId = `compass-sphere-${viewportId}`;
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<Popover
|
|
84
|
+
open={isOpen}
|
|
85
|
+
onOpenChange={handleOpenChange}
|
|
86
|
+
>
|
|
87
|
+
<PopoverTrigger
|
|
88
|
+
asChild
|
|
89
|
+
className="flex items-center justify-center"
|
|
90
|
+
>
|
|
91
|
+
<div ref={triggerRef}>
|
|
92
|
+
{IconContainer ? (
|
|
93
|
+
<IconContainer
|
|
94
|
+
disabled={disabled}
|
|
95
|
+
icon="OrientationSwitch"
|
|
96
|
+
label="3D orientation"
|
|
97
|
+
{...props}
|
|
98
|
+
{...containerProps}
|
|
99
|
+
>
|
|
100
|
+
{Icon}
|
|
101
|
+
</IconContainer>
|
|
102
|
+
) : (
|
|
103
|
+
<Button
|
|
104
|
+
variant="ghost"
|
|
105
|
+
size="icon"
|
|
106
|
+
disabled={disabled}
|
|
107
|
+
aria-label="3D orientation"
|
|
108
|
+
>
|
|
109
|
+
{Icon}
|
|
110
|
+
</Button>
|
|
111
|
+
)}
|
|
112
|
+
</div>
|
|
113
|
+
</PopoverTrigger>
|
|
114
|
+
<PopoverContent
|
|
115
|
+
onInteractOutside={() => {
|
|
116
|
+
interactedOutsideRef.current = true;
|
|
117
|
+
}}
|
|
118
|
+
// The trigger is a wrapper div that cannot take focus; return focus to its button,
|
|
119
|
+
// except after a click outside, where the focus stays where the user put it.
|
|
120
|
+
onCloseAutoFocus={event => {
|
|
121
|
+
event.preventDefault();
|
|
122
|
+
if (!interactedOutsideRef.current) {
|
|
123
|
+
triggerRef.current?.querySelector('button')?.focus();
|
|
124
|
+
}
|
|
125
|
+
interactedOutsideRef.current = false;
|
|
126
|
+
}}
|
|
127
|
+
className="w-auto flex-shrink-0 rounded-lg border-0 bg-neutral-800 p-1 shadow-none"
|
|
128
|
+
align={align}
|
|
129
|
+
side={side}
|
|
130
|
+
style={{ left: 0 }}
|
|
131
|
+
>
|
|
132
|
+
<svg
|
|
133
|
+
viewBox="0 0 100 100"
|
|
134
|
+
className="h-36 w-36"
|
|
135
|
+
role="group"
|
|
136
|
+
aria-label="View direction"
|
|
137
|
+
>
|
|
138
|
+
<defs>
|
|
139
|
+
<radialGradient
|
|
140
|
+
id={gradientId}
|
|
141
|
+
cx="35%"
|
|
142
|
+
cy="35%"
|
|
143
|
+
r="65%"
|
|
144
|
+
fx="35%"
|
|
145
|
+
fy="35%"
|
|
146
|
+
>
|
|
147
|
+
<stop
|
|
148
|
+
offset="0%"
|
|
149
|
+
stopColor="hsl(var(--secondary-foreground))"
|
|
150
|
+
stopOpacity="0.9"
|
|
151
|
+
/>
|
|
152
|
+
<stop
|
|
153
|
+
offset="45%"
|
|
154
|
+
stopColor="hsl(var(--secondary))"
|
|
155
|
+
stopOpacity="1"
|
|
156
|
+
/>
|
|
157
|
+
<stop
|
|
158
|
+
offset="100%"
|
|
159
|
+
stopColor="hsl(var(--secondary))"
|
|
160
|
+
stopOpacity="0.55"
|
|
161
|
+
/>
|
|
162
|
+
</radialGradient>
|
|
163
|
+
</defs>
|
|
164
|
+
{AXIS_ANGLES.map(angle => {
|
|
165
|
+
const start = toXY(angle, LINE_RADIUS);
|
|
166
|
+
const end = toXY(angle + 180, LINE_RADIUS);
|
|
167
|
+
return (
|
|
168
|
+
<line
|
|
169
|
+
key={angle}
|
|
170
|
+
x1={start.x}
|
|
171
|
+
y1={start.y}
|
|
172
|
+
x2={end.x}
|
|
173
|
+
y2={end.y}
|
|
174
|
+
stroke="white"
|
|
175
|
+
strokeWidth="1.2"
|
|
176
|
+
/>
|
|
177
|
+
);
|
|
178
|
+
})}
|
|
179
|
+
<circle
|
|
180
|
+
cx={CENTER}
|
|
181
|
+
cy={CENTER}
|
|
182
|
+
r={5}
|
|
183
|
+
fill={`url(#${gradientId})`}
|
|
184
|
+
className="drop-shadow-[0_0_6px_hsl(var(--secondary)/0.7)]"
|
|
185
|
+
/>
|
|
186
|
+
{DIRECTIONS.map(({ direction, angle }) => {
|
|
187
|
+
const { x, y } = toXY(angle, LABEL_RADIUS);
|
|
188
|
+
return (
|
|
189
|
+
<g
|
|
190
|
+
key={direction}
|
|
191
|
+
className="group cursor-pointer select-none outline-none"
|
|
192
|
+
onClick={() => lookFrom(direction)}
|
|
193
|
+
onKeyDown={event => {
|
|
194
|
+
if (event.key === 'Enter' || event.key === ' ') {
|
|
195
|
+
event.preventDefault();
|
|
196
|
+
lookFrom(direction);
|
|
197
|
+
}
|
|
198
|
+
}}
|
|
199
|
+
role="button"
|
|
200
|
+
tabIndex={0}
|
|
201
|
+
aria-label={`View from ${direction}`}
|
|
202
|
+
>
|
|
203
|
+
<circle
|
|
204
|
+
cx={x}
|
|
205
|
+
cy={y}
|
|
206
|
+
r={10}
|
|
207
|
+
fill="transparent"
|
|
208
|
+
className="group-focus-visible:stroke-white"
|
|
209
|
+
/>
|
|
210
|
+
<text
|
|
211
|
+
x={x}
|
|
212
|
+
y={y}
|
|
213
|
+
textAnchor="middle"
|
|
214
|
+
dominantBaseline="central"
|
|
215
|
+
fill="white"
|
|
216
|
+
fontSize="11"
|
|
217
|
+
fontWeight="600"
|
|
218
|
+
fontFamily="sans-serif"
|
|
219
|
+
className="pointer-events-none"
|
|
220
|
+
>
|
|
221
|
+
{direction}
|
|
222
|
+
</text>
|
|
223
|
+
</g>
|
|
224
|
+
);
|
|
225
|
+
})}
|
|
226
|
+
</svg>
|
|
227
|
+
<div className="mt-2 flex items-center justify-center">
|
|
228
|
+
<button
|
|
229
|
+
type="button"
|
|
230
|
+
className="mr-2 cursor-pointer text-lg text-neutral-400 outline-none hover:text-neutral-200 focus:text-neutral-200"
|
|
231
|
+
onClick={() => changeSpin(spin === 0 ? 1 : 0)}
|
|
232
|
+
aria-label={spin === 0 ? 'Start spin' : 'Stop spin'}
|
|
233
|
+
>
|
|
234
|
+
Spin
|
|
235
|
+
</button>
|
|
236
|
+
<Button
|
|
237
|
+
variant="ghost"
|
|
238
|
+
size="icon"
|
|
239
|
+
className="h-7 w-7"
|
|
240
|
+
onClick={() => changeSpin(spin - 1)}
|
|
241
|
+
aria-label="Decrease spin"
|
|
242
|
+
>
|
|
243
|
+
<Icons.ChevronLeft className="h-4 w-4" />
|
|
244
|
+
</Button>
|
|
245
|
+
<span className="min-w-[1.75rem] text-center font-mono text-sm font-medium">{spin}</span>
|
|
246
|
+
<Button
|
|
247
|
+
variant="ghost"
|
|
248
|
+
size="icon"
|
|
249
|
+
className="h-7 w-7"
|
|
250
|
+
onClick={() => changeSpin(spin + 1)}
|
|
251
|
+
aria-label="Increase spin"
|
|
252
|
+
>
|
|
253
|
+
<Icons.ChevronRight className="h-4 w-4" />
|
|
254
|
+
</Button>
|
|
255
|
+
</div>
|
|
256
|
+
</PopoverContent>
|
|
257
|
+
</Popover>
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
export default Viewport3DCompassMenu;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import React, { ReactNode } from 'react';
|
|
2
|
+
import { useSystem } from '@ohif/core';
|
|
2
3
|
import ViewportOrientationMenu from './ViewportOrientationMenu';
|
|
4
|
+
import Viewport3DCompassMenu from './Viewport3DCompassMenu';
|
|
3
5
|
import { useViewportDisplaySets } from '../../hooks/useViewportDisplaySets';
|
|
6
|
+
import { getViewportAdapter } from '../../services/ViewportService/adapter';
|
|
4
7
|
|
|
5
8
|
export function ViewportOrientationMenuWrapper(
|
|
6
9
|
props: withAppTypes<{
|
|
@@ -15,11 +18,19 @@ export function ViewportOrientationMenuWrapper(
|
|
|
15
18
|
): ReactNode {
|
|
16
19
|
const { viewportId } = props;
|
|
17
20
|
const { viewportDisplaySets } = useViewportDisplaySets(viewportId);
|
|
21
|
+
const { servicesManager } = useSystem();
|
|
18
22
|
|
|
19
23
|
if (!viewportDisplaySets.length) {
|
|
20
24
|
return null;
|
|
21
25
|
}
|
|
22
26
|
|
|
27
|
+
const viewport = servicesManager.services.cornerstoneViewportService.getCornerstoneViewport(
|
|
28
|
+
viewportId
|
|
29
|
+
);
|
|
30
|
+
if (viewport && getViewportAdapter(viewport).getShape() === 'volume3d') {
|
|
31
|
+
return <Viewport3DCompassMenu {...props} />;
|
|
32
|
+
}
|
|
33
|
+
|
|
23
34
|
return (
|
|
24
35
|
<ViewportOrientationMenu
|
|
25
36
|
{...props}
|
package/src/index.tsx
CHANGED
|
@@ -45,6 +45,7 @@ import {
|
|
|
45
45
|
SculptorTool,
|
|
46
46
|
SplineContourSegmentationTool,
|
|
47
47
|
LabelMapEditWithContourTool,
|
|
48
|
+
OrientationControllerTool,
|
|
48
49
|
} from '@cornerstonejs/tools';
|
|
49
50
|
import { LabelmapSlicePropagationTool, MarkerLabelmapTool } from '@cornerstonejs/ai';
|
|
50
51
|
import * as polySeg from '@cornerstonejs/polymorphic-segmentation';
|
|
@@ -108,6 +109,7 @@ export default function initCornerstoneTools(configuration = {}) {
|
|
|
108
109
|
addTool(SplineROITool);
|
|
109
110
|
addTool(LivewireContourTool);
|
|
110
111
|
addTool(OrientationMarkerTool);
|
|
112
|
+
addTool(OrientationControllerTool);
|
|
111
113
|
addTool(WindowLevelRegionTool);
|
|
112
114
|
addTool(PlanarFreehandContourSegmentationTool);
|
|
113
115
|
addTool(SegmentSelectTool);
|
|
@@ -171,6 +173,7 @@ const toolNames = {
|
|
|
171
173
|
LivewireContour: LivewireContourTool.toolName,
|
|
172
174
|
PlanarFreehandROI: PlanarFreehandROITool.toolName,
|
|
173
175
|
OrientationMarker: OrientationMarkerTool.toolName,
|
|
176
|
+
OrientationController: OrientationControllerTool.toolName,
|
|
174
177
|
WindowLevelRegion: WindowLevelRegionTool.toolName,
|
|
175
178
|
PlanarFreehandContourSegmentation: PlanarFreehandContourSegmentationTool.toolName,
|
|
176
179
|
SegmentBidirectional: SegmentBidirectionalTool.toolName,
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Enums } from '@cornerstonejs/core';
|
|
2
|
+
import { rotateVolume3D, setVolume3DViewDirection } from './volume3dView';
|
|
3
|
+
|
|
4
|
+
// Legacy volume3d viewport: the adapter reads getCamera and writes setCamera.
|
|
5
|
+
function makeViewport(type = Enums.ViewportType.VOLUME_3D) {
|
|
6
|
+
let camera = { focalPoint: [0, 0, 0], position: [0, 0, 10], viewUp: [0, 1, 0] };
|
|
7
|
+
return {
|
|
8
|
+
type,
|
|
9
|
+
getCamera: () => camera,
|
|
10
|
+
setCamera: jest.fn(patch => (camera = { ...camera, ...patch })),
|
|
11
|
+
render: jest.fn(),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const rounded = (point: number[]) => point.map(value => Math.round(value * 1e6) / 1e6 + 0);
|
|
16
|
+
|
|
17
|
+
describe('volume3dView', () => {
|
|
18
|
+
it('looks from anterior: camera at -y (LPS), same distance, superior up', () => {
|
|
19
|
+
const viewport = makeViewport();
|
|
20
|
+
setVolume3DViewDirection(viewport as never, 'A');
|
|
21
|
+
const camera = viewport.getCamera();
|
|
22
|
+
expect(rounded(camera.position)).toEqual([0, -10, 0]);
|
|
23
|
+
expect(camera.viewUp).toEqual([0, 0, 1]);
|
|
24
|
+
expect(viewport.render).toHaveBeenCalled();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('looks from inferior: camera at -z, anterior up as the orientation cube', () => {
|
|
28
|
+
const viewport = makeViewport();
|
|
29
|
+
setVolume3DViewDirection(viewport as never, 'I');
|
|
30
|
+
expect(rounded(viewport.getCamera().position)).toEqual([0, 0, -10]);
|
|
31
|
+
expect(viewport.getCamera().viewUp).toEqual([0, -1, 0]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('orbits about the superior axis: 90 degrees takes anterior to left', () => {
|
|
35
|
+
const viewport = makeViewport();
|
|
36
|
+
setVolume3DViewDirection(viewport as never, 'A');
|
|
37
|
+
expect(rotateVolume3D(viewport as never, 90)).toBe(true);
|
|
38
|
+
expect(rounded(viewport.getCamera().position)).toEqual([10, 0, 0]);
|
|
39
|
+
expect(rounded(viewport.getCamera().viewUp)).toEqual([0, 0, 1]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('does nothing on a viewport that is not volume3d', () => {
|
|
43
|
+
const viewport = makeViewport(Enums.ViewportType.ORTHOGRAPHIC);
|
|
44
|
+
expect(rotateVolume3D(viewport as never, 90)).toBe(false);
|
|
45
|
+
setVolume3DViewDirection(viewport as never, 'A');
|
|
46
|
+
expect(viewport.setCamera).not.toHaveBeenCalled();
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { vec3 } from 'gl-matrix';
|
|
2
|
+
import type { Types } from '@cornerstonejs/core';
|
|
3
|
+
import { getViewportAdapter } from '../services/ViewportService/adapter';
|
|
4
|
+
|
|
5
|
+
export type ViewDirection = 'S' | 'I' | 'R' | 'L' | 'A' | 'P';
|
|
6
|
+
|
|
7
|
+
/** Degrees per second for each step of the spin speed. */
|
|
8
|
+
export const SPIN_DEGREES_PER_SECOND = 18;
|
|
9
|
+
|
|
10
|
+
// Patient axes are LPS: +x left, +y posterior, +z superior. `offset` points from the
|
|
11
|
+
// focal point to the camera, so 'A' puts the camera anterior, looking posterior.
|
|
12
|
+
const VIEW_DIRECTIONS: Record<ViewDirection, { offset: vec3; viewUp: Types.Point3 }> = {
|
|
13
|
+
S: { offset: [0, 0, 1], viewUp: [0, 1, 0] },
|
|
14
|
+
I: { offset: [0, 0, -1], viewUp: [0, -1, 0] }, // anterior up, as the CS3D orientation cube
|
|
15
|
+
R: { offset: [-1, 0, 0], viewUp: [0, 0, 1] },
|
|
16
|
+
L: { offset: [1, 0, 0], viewUp: [0, 0, 1] },
|
|
17
|
+
A: { offset: [0, -1, 0], viewUp: [0, 0, 1] },
|
|
18
|
+
P: { offset: [0, 1, 0], viewUp: [0, 0, 1] },
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** The camera fields of a volume3d viewport, legacy or generic; undefined for other viewports. */
|
|
22
|
+
function getVolume3DCamera(viewport: Types.IViewport | undefined) {
|
|
23
|
+
const adapter = viewport && getViewportAdapter(viewport);
|
|
24
|
+
if (adapter?.getShape() !== 'volume3d') {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
return { adapter, camera: adapter.getViewState() as Types.ICamera };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Moves the camera of a volume3d viewport to look from an anatomical direction. */
|
|
31
|
+
export function setVolume3DViewDirection(
|
|
32
|
+
viewport: Types.IViewport | undefined,
|
|
33
|
+
direction: ViewDirection
|
|
34
|
+
): void {
|
|
35
|
+
const found = getVolume3DCamera(viewport);
|
|
36
|
+
const view = VIEW_DIRECTIONS[direction];
|
|
37
|
+
if (!found || !view) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const { focalPoint, position } = found.camera;
|
|
41
|
+
const distance = vec3.distance(position, focalPoint) || 1;
|
|
42
|
+
const newPosition = vec3.scaleAndAdd(vec3.create(), focalPoint, view.offset, distance);
|
|
43
|
+
found.adapter.setViewState({
|
|
44
|
+
focalPoint,
|
|
45
|
+
position: [...newPosition] as Types.Point3,
|
|
46
|
+
viewUp: view.viewUp,
|
|
47
|
+
});
|
|
48
|
+
viewport?.render();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Orbits the camera of a volume3d viewport about the patient superior (z) axis through
|
|
53
|
+
* the focal point. Returns false when the viewport is not a volume3d viewport.
|
|
54
|
+
*/
|
|
55
|
+
export function rotateVolume3D(viewport: Types.IViewport | undefined, degrees: number): boolean {
|
|
56
|
+
const found = getVolume3DCamera(viewport);
|
|
57
|
+
if (!found) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
const { focalPoint, position, viewUp } = found.camera;
|
|
61
|
+
const radians = (degrees * Math.PI) / 180;
|
|
62
|
+
const newPosition = vec3.rotateZ(vec3.create(), position, focalPoint, radians);
|
|
63
|
+
const newViewUp = vec3.rotateZ(vec3.create(), viewUp, [0, 0, 0], radians);
|
|
64
|
+
found.adapter.setViewState({
|
|
65
|
+
focalPoint,
|
|
66
|
+
position: [...newPosition] as Types.Point3,
|
|
67
|
+
viewUp: [...newViewUp] as Types.Point3,
|
|
68
|
+
});
|
|
69
|
+
viewport?.render();
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const spinSpeeds = new Map<string, number>();
|
|
74
|
+
let frameId: number | undefined;
|
|
75
|
+
let lastTime = 0;
|
|
76
|
+
let getViewport: (viewportId: string) => Types.IViewport | undefined = () => undefined;
|
|
77
|
+
|
|
78
|
+
function spinTick(now: number) {
|
|
79
|
+
const seconds = (now - lastTime) / 1000;
|
|
80
|
+
lastTime = now;
|
|
81
|
+
spinSpeeds.forEach((speed, viewportId) => {
|
|
82
|
+
const degrees = -speed * SPIN_DEGREES_PER_SECOND * seconds;
|
|
83
|
+
if (!rotateVolume3D(getViewport(viewportId), degrees)) {
|
|
84
|
+
spinSpeeds.delete(viewportId);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
frameId = spinSpeeds.size ? requestAnimationFrame(spinTick) : undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function getVolume3DSpin(viewportId: string): number {
|
|
91
|
+
return spinSpeeds.get(viewportId) ?? 0;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Sets the spin speed of a volume3d viewport; 0 stops it. The spin stops by itself when
|
|
96
|
+
* the viewport is gone or is no longer a volume3d viewport.
|
|
97
|
+
*/
|
|
98
|
+
export function setVolume3DSpin(
|
|
99
|
+
viewportId: string,
|
|
100
|
+
speed: number,
|
|
101
|
+
viewportLookup: (viewportId: string) => Types.IViewport | undefined
|
|
102
|
+
): void {
|
|
103
|
+
getViewport = viewportLookup;
|
|
104
|
+
if (speed) {
|
|
105
|
+
spinSpeeds.set(viewportId, speed);
|
|
106
|
+
} else {
|
|
107
|
+
spinSpeeds.delete(viewportId);
|
|
108
|
+
}
|
|
109
|
+
if (spinSpeeds.size && frameId === undefined) {
|
|
110
|
+
lastTime = performance.now();
|
|
111
|
+
frameId = requestAnimationFrame(spinTick);
|
|
112
|
+
}
|
|
113
|
+
}
|