-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoiProvider.tsx
More file actions
279 lines (264 loc) · 9.32 KB
/
RoiProvider.tsx
File metadata and controls
279 lines (264 loc) · 9.32 KB
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
import type { ReactNode } from 'react';
import { useEffect, useMemo, useReducer, useRef } from 'react';
import type {
BoundaryStrategy,
OnChangeCallback,
OnCommitCallback,
PanZoom,
PanZoomWithOrigin,
ResizeStrategy,
RoiMode,
RoiState,
} from '../index.js';
import type { CommittedRoiProperties } from '../types/CommittedRoi.js';
import { getIdentityPanzoom } from '../utilities/panZoom.ts';
import { createRoiFromCommittedRoi } from '../utilities/rois.js';
import { normalizeAngle } from '../utilities/rotate.js';
import type { ActionCallbacks, PanZoomContext } from './contexts.js';
import {
callbacksRefContext,
committedRoisContext,
panZoomContext,
roiContainerRefContext,
roiDispatchContext,
roiStateContext,
roiStateRefContext,
roisContext,
} from './contexts.js';
import type { CommitBoxStrategy, ReactRoiState } from './roiReducer.js';
import { roiReducer } from './roiReducer.js';
import { initialSize } from './updaters/initialPanZoom.js';
export interface RoiProviderInitialConfig<TData> {
mode?: RoiMode;
zoom?: {
/**
* The initial zoom level.
* The scaling and translation apply on the coordinate system of the container.
* Therefore, translations are in device pixels.
* The origin is the fixed point of the scaling transformation, before the translation applies:
* - 'top-left': the top left corner of the container
* - 'center': the center of the container
* @default { scale: 1, translation: [0, 0]}
*/
initial?: Partial<PanZoomWithOrigin>;
/**
* The minimum zoom level allowed.
* @default 1
*/
min?: number;
/**
* The maximum zoom level allowed.
* @default 10
*/
max?: number;
/**
* When zooming and panning the target, some of the empty space around the target can become visible within the container.
* This option controls how much of that space is allowed to be visible, expressed as a number between 0 and 1
* which represents the percentage of the container's available space.
* @default 0.5
*/
spaceAroundTarget?: number;
};
/**
* On commit, ROI's position will be updated such as to satisfy the given boundary strategy:
* - `inside_auto`: the whole ROI must be inside the target. If not, it will be moved or resized accordingly, or reverted.
* - `inside`: the whole ROI must be inside the target. If not, it will be reverted.
* - `partially_inside`: at least part of the ROI must be inside the target. If not, it will be reverted.
* - `none`: no boundary check is performed. The ROI can be anywhere.
* @default 'inside_auto'
*/
commitRoiBoundaryStrategy?: BoundaryStrategy;
/**
* Defines how the target should fit within the container at initialization.
* - `none`: The target is not transformed relative to the container. The top-left corner of the target matches the
* top-left corner of the container.
* - `contain`: The target is transformed such as the full target is visible in the container. The center of the
* target matches the center of the container.
* - `cover`: The target is scaled down such that one of the dimensions is fitting the container exactly, or not
* scaled at all if the target is smaller than the container. The center of the target matches the center
* of the container.
* - `center`: The center of the target matches the center of the container, without scaling the target.
* @default 'contain'
*/
resizeStrategy?: ResizeStrategy;
/**
* How should the roi be updated before committing it when created / moved / resized / rotated.
* It affects the final values of `x`, `y`, `width`, and `height` box properties in the committed ROI.
* 'exact' will keep the exact, non-rounded values. This results in floating point numbers in the box properties.
* 'round' will try as much as possible to keep integers:
* - If the angle of rotation is 0, then x, y, width and height will be integers
* - If the angle of rotation is not 0, then only width and height will be integers
* @default 'exact'
*
*/
commitRoiBoxStrategy?: CommitBoxStrategy;
rois?: Array<CommittedRoiProperties<TData>>;
selectedRoiId?: string;
/**
* When in rotate_selected mode, this defines how much the mouse movement transnlates to a rotation angle.
* It is expressed in number of pixels for a full 360 degree rotation.
* @default 1200
*/
rotationResolution?: number;
}
interface RoiProviderProps<TData> {
children: ReactNode;
initialConfig?: RoiProviderInitialConfig<TData>;
onCommit?: OnCommitCallback<TData>;
onChange?: OnChangeCallback<TData>;
/**
* Called after zoom or pan actions
* @param zoom The new pan and zoom state.
*/
onAfterZoomChange?: (zoom: PanZoom) => void;
}
type CreateInitialConfigParam<T> = Required<
Omit<RoiProviderInitialConfig<T>, 'selectedRoiId'>
> & {
zoom: Required<Required<RoiProviderInitialConfig<T>['zoom']>>;
selectedRoiId?: string;
};
function createInitialState<T>(
initialConfig: CreateInitialConfigParam<T>,
): ReactRoiState<T> {
return {
mode: initialConfig.mode,
action: 'idle',
isContainerInitialized: false,
isTargetInitialized: false,
resizeStrategy: initialConfig.resizeStrategy,
commitRoiBoxStrategy: initialConfig.commitRoiBoxStrategy,
commitRoiBoundaryStrategy: initialConfig.commitRoiBoundaryStrategy,
targetSize: initialSize,
containerSize: initialSize,
selectedRoi: initialConfig.selectedRoiId,
committedRois: initialConfig.rois,
rois: initialConfig.rois
.map((committedRoi) => ({
...committedRoi,
angle: normalizeAngle(committedRoi.angle),
}))
.map((committedRoi) => createRoiFromCommittedRoi(committedRoi)),
panZoom: getIdentityPanzoom(),
basePanZoom: getIdentityPanzoom(),
startPanZoom: {
scale: initialConfig.zoom.initial?.scale ?? 1,
translation: initialConfig.zoom.initial?.translation ?? [0, 0],
origin: initialConfig.zoom.initial?.origin ?? 'top-left',
},
zoomDomain: initialConfig.zoom,
rotationResolution: initialConfig.rotationResolution,
};
}
export function RoiProvider<TData>(props: RoiProviderProps<TData>) {
const {
children,
initialConfig = {},
onAfterZoomChange,
onCommit,
onChange,
} = props;
const {
rois: initialRois = [],
mode: initialMode = 'hybrid',
zoom = {},
selectedRoiId,
commitRoiBoundaryStrategy = 'inside_auto',
resizeStrategy = 'contain',
commitRoiBoxStrategy = 'round',
rotationResolution = 1200,
} = initialConfig;
const { spaceAroundTarget = 0.5 } = zoom;
// Choose the most appropriate min / max based on different parameters
const min = Math.min(zoom.min ?? 1, zoom.initial?.scale ?? 1);
const max = Math.max(zoom.max ?? 200, zoom.initial?.scale ?? 1);
const [state, dispatch] = useReducer(roiReducer, null, () =>
createInitialState<TData>({
mode: initialMode,
rois: initialRois,
zoom: {
min,
max,
spaceAroundTarget,
initial: initialConfig.zoom?.initial ?? {
...getIdentityPanzoom(),
origin: 'top-left',
},
},
commitRoiBoundaryStrategy,
resizeStrategy,
commitRoiBoxStrategy,
selectedRoiId,
rotationResolution,
}),
);
const stateRef = useRef(state);
const containerRef = useRef<HTMLDivElement>(null);
const callbacksRef = useRef<ActionCallbacks<TData>>({
onZoom: onAfterZoomChange,
onAfterChange: onCommit,
onChange,
});
useEffect(() => {
stateRef.current = state;
}, [state]);
useEffect(() => {
callbacksRef.current = {
onZoom: onAfterZoomChange,
onAfterChange: onCommit,
onChange,
};
}, [onAfterZoomChange, onCommit, onChange]);
const {
rois,
committedRois,
mode,
selectedRoi,
panZoom,
basePanZoom,
startPanZoom,
targetSize,
containerSize,
isTargetInitialized,
isContainerInitialized,
action,
} = state;
const roiState: RoiState = useMemo(() => {
return {
mode,
selectedRoi,
action,
};
}, [mode, selectedRoi, action]);
const isReady = isTargetInitialized && isContainerInitialized;
const panzoomContextValue: PanZoomContext = useMemo(() => {
return {
targetSize,
containerSize,
panZoom,
basePanZoom,
startPanZoom,
// We memoize this here to minimize the number of times we need to render the container
isReady,
};
}, [panZoom, basePanZoom, startPanZoom, isReady, targetSize, containerSize]);
return (
<callbacksRefContext.Provider value={callbacksRef}>
<roiStateRefContext.Provider value={stateRef}>
<roiContainerRefContext.Provider value={containerRef}>
<panZoomContext.Provider value={panzoomContextValue}>
<roiDispatchContext.Provider value={dispatch}>
<roisContext.Provider value={rois}>
<committedRoisContext.Provider value={committedRois}>
<roiStateContext.Provider value={roiState}>
{children}
</roiStateContext.Provider>
</committedRoisContext.Provider>
</roisContext.Provider>
</roiDispatchContext.Provider>
</panZoomContext.Provider>
</roiContainerRefContext.Provider>
</roiStateRefContext.Provider>
</callbacksRefContext.Provider>
);
}