-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroiReducer.tsx
More file actions
430 lines (380 loc) · 10.9 KB
/
roiReducer.tsx
File metadata and controls
430 lines (380 loc) · 10.9 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
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
import { produce } from 'immer';
import type { PointerEvent as ReactPointerEvent } from 'react';
import type {
CommittedRoi,
PanZoom,
ReactRoiAction,
ResizeStrategy,
RoiMode,
Size,
} from '../index.js';
import type { CommittedRoiProperties } from '../types/CommittedRoi.js';
import type { Roi, XCornerPosition, YCornerPosition } from '../types/Roi.js';
import { assert, assertUnreachable } from '../utilities/assert.js';
import {
changeBoxRotationCenter,
xAxisCornerToCenter,
yAxisCornerToCenter,
} from '../utilities/box.js';
import type { Point } from '../utilities/point.js';
import {
createCommittedRoi,
createRoi,
createRoiFromCommittedRoi,
} from '../utilities/rois.js';
import { cancelAction } from './updaters/cancelAction.js';
import { endAction } from './updaters/endAction.js';
import { updateInitialPanZoom } from './updaters/initialPanZoom.js';
import { pointerMove } from './updaters/pointerMove.js';
import { sanitizeRois } from './updaters/sanitizeRois.js';
import { selectBoxAndStartAction } from './updaters/selectBoxAndStartAction.js';
import { startDraw } from './updaters/startDraw.js';
import { startPan } from './updaters/startPan.js';
import { resetZoomAction, zoomAction, zoomIntoROI } from './updaters/zoom.js';
interface ZoomDomain {
min: number;
max: number;
spaceAroundTarget: number;
}
export type CommitBoxStrategy = 'exact' | 'round';
export interface ReactRoiState<TData = unknown> {
/**
* Current mode
*/
mode: RoiMode;
/**
* Resize strategy
*/
resizeStrategy: ResizeStrategy;
/**
* How box properties of the roi should be updated when committed.
*/
commitRoiBoxStrategy: CommitBoxStrategy;
/**
* When in select_rotate 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;
/**
* Identification of the selected object
*/
selectedRoi?: string;
/**
* Current action being performed
*/
action: ReactRoiAction;
/**
* Regions of interest
*/
rois: Array<Roi<TData>>;
/**
* Committed regions of interest
*/
committedRois: Array<CommittedRoiProperties<TData>>;
/**
* Size of the target on which the rois are drawn
*/
targetSize: Size;
/**
* Size of the container (used to normalize initial panzoom transform)
*/
containerSize: Size;
/**
* Defines the current affine transformation being applied to the container
*/
panZoom: PanZoom;
/**
* Defines the inital panZoom transform so that the image is appropriately scaled and centered in the container
*/
initialPanZoom: PanZoom;
/**
* Zoom level min and max
*/
zoomDomain: ZoomDomain;
}
export interface UpdateRoiOptions {
/**
* Whether the update should be committed immediately.
* In both cases, the ROI will move immediately on screen.
* If set to false, the ROI will enter a mode where it can't be modified through user interactions until committed.
* Setting it to false also prevents having frequent updates to committed ROIs when those can trigger expensive operations.
*/
commit: boolean;
}
export type UpdateRoiPayload = Partial<CommittedRoiProperties> & {
id: string;
options?: UpdateRoiOptions;
};
export type CreateRoiPayload = Partial<CommittedRoiProperties> & {
id: string;
};
export interface ZoomPayload {
scale: number;
containerBoundingRect: DOMRect;
clientX: number;
clientY: number;
}
export interface ZoomIntoROIOptions {
/**
* A margin relative to the viewport to leave around the zoomed in ROI
* 0 means no margin. A value of 0.5 means 50% of the viewport is for the margin and the remaining 50% for the ROI.
*/
margin: number;
}
export interface ZoomIntoROIPayload {
roiOrPoints: CommittedRoi | Point[];
options: ZoomIntoROIOptions;
}
export interface StartDrawPayload {
event: PointerEvent | ReactPointerEvent;
containerBoundingRect: DOMRect;
isPanZooming: boolean;
noUnselection?: boolean;
lockPan: boolean;
data?: unknown;
}
export interface PointerMovePayload {
event: PointerEvent;
containerBoundingRect: DOMRect;
}
export interface StartResizePayload {
id: string;
xAxisCorner: XCornerPosition;
yAxisCorner: YCornerPosition;
}
export interface EndActionPayload {
noUnselection: boolean;
minNewRoiSize: number;
}
export interface CancelActionPayload {
noUnselection: boolean;
}
export interface SelectBoxAndStartMovePayload {
id: string;
}
export interface SelectBoxAndStartRotatePayload {
id: string;
}
export interface SelectBoxAndStartRotateFreePayload {
id: string;
rotationResolution: number;
}
export type RoiReducerAction =
| { type: 'SET_MODE'; payload: RoiMode }
| {
type: 'CREATE_ROI';
payload: CreateRoiPayload;
}
| {
type: 'SET_SIZE';
payload: Size;
}
| {
type: 'SET_CONTAINER_SIZE';
payload: Size;
}
| {
type: 'UPDATE_ROI';
payload: UpdateRoiPayload;
}
| { type: 'REMOVE_ROI'; payload?: string }
| {
type: 'START_RESIZE';
payload: StartResizePayload;
}
| {
type: 'START_DRAW';
payload: StartDrawPayload;
}
| {
type: 'POINTER_MOVE';
payload: PointerMovePayload;
}
| {
type: 'END_ACTION';
payload: EndActionPayload;
}
| { type: 'CANCEL_ACTION'; payload: CancelActionPayload }
| { type: 'START_PAN' }
| {
type: 'SELECT_BOX_AND_START_MOVE';
payload: SelectBoxAndStartMovePayload;
}
| {
type: 'SELECT_BOX_AND_START_ROTATE';
payload: SelectBoxAndStartRotatePayload;
}
| {
type: 'ZOOM';
payload: ZoomPayload;
}
| { type: 'ZOOM_INTO_ROI'; payload: ZoomIntoROIPayload }
| {
type: 'RESET_ZOOM';
}
| { type: 'UNSELECT_ROI'; payload: string }
| { type: 'SELECT_ROI'; payload: string | null };
export function roiReducer(
state: ReactRoiState,
action: RoiReducerAction,
): ReactRoiState {
return produce(state, (draft) => {
const type = action.type;
switch (type) {
case 'SET_MODE':
draft.mode = action.payload;
break;
case 'SET_SIZE': {
// Ignore if size is 0
if (action.payload.width === 0 || action.payload.height === 0) return;
draft.targetSize = action.payload;
updateInitialPanZoom(draft);
sanitizeRois(draft);
break;
}
case 'SET_CONTAINER_SIZE': {
draft.containerSize = action.payload;
updateInitialPanZoom(draft);
break;
}
case 'REMOVE_ROI': {
cancelAction(draft, { noUnselection: false });
const id = action.payload ?? draft.selectedRoi;
const index = draft.rois.findIndex((roi) => roi.id === id);
if (index === -1) return;
draft.rois.splice(index, 1);
draft.committedRois.splice(index, 1);
draft.selectedRoi = undefined;
return;
}
case 'SELECT_ROI': {
const id = action.payload;
if (id) {
const roi = draft.rois.find((roi) => roi.id === id);
if (roi) {
cancelAction(draft, { noUnselection: false });
draft.selectedRoi = id;
}
} else if (draft.selectedRoi) {
cancelAction(draft, { noUnselection: false });
draft.selectedRoi = undefined;
}
break;
}
case 'UNSELECT_ROI': {
const id = action.payload;
if (draft.selectedRoi === id) {
cancelAction(draft, { noUnselection: false });
draft.selectedRoi = undefined;
}
return;
}
case 'CREATE_ROI': {
const { id, ...otherRoiProps } = action.payload;
if (draft.committedRois.some((d) => d.id === id)) {
throw new Error('This id already exists on the draft.');
}
const committedRoi = createCommittedRoi(id, otherRoiProps);
draft.committedRois.push(committedRoi);
draft.rois.push(createRoi(id, otherRoiProps));
draft.selectedRoi = committedRoi.id;
break;
}
case 'UPDATE_ROI': {
const {
id,
options = { commit: true, boundingStrategy: 'none' },
...updatedData
} = action.payload;
if (!id) return;
const index = draft.rois.findIndex((roi) => roi.id === id);
assert(index !== -1, 'ROI not found');
if (options.commit) {
const committedRoi = draft.committedRois[index];
assert(committedRoi, 'Committed ROI not found');
Object.assign<
CommittedRoiProperties,
Partial<CommittedRoiProperties>
>(committedRoi, updatedData);
draft.rois[index] = createRoiFromCommittedRoi(committedRoi);
} else {
const roi = draft.rois[index];
const { x, y, width, height, angle, ...otherData } = updatedData;
Object.assign<Roi, Partial<Roi>>(roi, {
...otherData,
action: { type: 'external' },
});
roi.box.x = x ?? roi.box.x;
roi.box.y = y ?? roi.box.y;
roi.box.width = width ?? roi.box.width;
roi.box.height = height ?? roi.box.height;
roi.box.angle = angle ?? roi.box.angle;
}
break;
}
case 'START_RESIZE': {
const { id, xAxisCorner, yAxisCorner } = action.payload;
const { rois } = draft;
const roi = rois.find((roi) => roi.id === id);
assert(roi, 'ROI not found');
draft.action = 'resizing';
roi.action = {
type: 'resizing',
xAxisCorner,
yAxisCorner,
remainder: {
x: 0,
y: 0,
},
};
roi.box = changeBoxRotationCenter(roi.box, {
xRotationCenter: xAxisCornerToCenter[xAxisCorner],
yRotationCenter: yAxisCornerToCenter[yAxisCorner],
});
break;
}
case 'START_PAN': {
startPan(draft);
break;
}
case 'SELECT_BOX_AND_START_MOVE': {
selectBoxAndStartAction(draft, action.payload.id, {
type: draft.mode === 'select_rotate' ? 'rotating_free' : 'moving',
});
break;
}
case 'SELECT_BOX_AND_START_ROTATE': {
selectBoxAndStartAction(draft, action.payload.id, { type: 'rotating' });
break;
}
case 'START_DRAW': {
startDraw(draft, action.payload);
break;
}
case 'POINTER_MOVE': {
pointerMove(draft, action.payload);
break;
}
case 'END_ACTION': {
endAction(draft, action.payload);
break;
}
case 'CANCEL_ACTION': {
cancelAction(draft, action.payload);
break;
}
case 'ZOOM':
zoomAction(draft, action.payload);
break;
case 'ZOOM_INTO_ROI':
zoomIntoROI(draft, action.payload);
break;
case 'RESET_ZOOM':
resetZoomAction(draft);
break;
default:
assertUnreachable(type);
}
});
}