@vitavision/stage2d 0.12.0 → 0.13.0
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/README.md +3 -6
- package/dist/index.d.ts +669 -57
- package/dist/index.js +5455 -3621
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/components/AreaSet.tsx +4 -4
- package/src/components/ContourEditor.tsx +286 -37
- package/src/components/DatumEditor.tsx +261 -0
- package/src/components/EllipseSet.tsx +3 -3
- package/src/components/GridLayer.tsx +1 -2
- package/src/components/HeatmapLayer.tsx +1 -1
- package/src/components/MeasureOverlay.tsx +320 -89
- package/src/components/PointSet.tsx +4 -4
- package/src/components/PolylineSet.tsx +81 -17
- package/src/components/RectRoiEditor.tsx +188 -84
- package/src/components/ShapeEditor.tsx +14 -4
- package/src/components/areaBatching.ts +1 -1
- package/src/components/contourArc.ts +268 -0
- package/src/components/datumEdit.ts +149 -0
- package/src/components/labelLod.ts +2 -3
- package/src/components/markerShapes.ts +1 -2
- package/src/components/measureGeometry.ts +34 -1
- package/src/components/measureHit.ts +371 -0
- package/src/components/overlayRole.ts +1 -1
- package/src/components/pointIndex.ts +2 -3
- package/src/components/polylineIndex.ts +3 -3
- package/src/components/shapeEdit.ts +38 -5
- package/src/components/stage/CompareLayer.tsx +228 -0
- package/src/components/stage/ImageLayer.tsx +56 -15
- package/src/components/stage/ImageStage.tsx +29 -8
- package/src/components/stage/StageSurface.tsx +1 -4
- package/src/components/stage/StageToolbar.tsx +26 -8
- package/src/components/stage/compareStyle.ts +155 -0
- package/src/components/stage/gesture.ts +5 -0
- package/src/components/stage/hitTest.ts +19 -0
- package/src/components/stage/useStageHitTest.ts +11 -0
- package/src/components/useShapeDrag.ts +2 -4
- package/src/index.ts +32 -2
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which measurement primitive is under the pointer: the distance from a point to each kind as
|
|
3
|
+
* `MeasureOverlay` draws it, and the nearest primitive that has an `id`.
|
|
4
|
+
*
|
|
5
|
+
* Pure, so every kind's geometry is tested without a DOM. Distances are in image pixels.
|
|
6
|
+
* Marks that `MeasureOverlay` sizes in screen pixels (a dot, a cross, a caliper's arrow head)
|
|
7
|
+
* are measured at the same size, through `strokeScale`.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { MeasurePrimitive } from "./MeasureOverlay";
|
|
11
|
+
import {
|
|
12
|
+
arrowHeadPoints,
|
|
13
|
+
caliperArrow,
|
|
14
|
+
caliperCorners,
|
|
15
|
+
dimensionGeometry,
|
|
16
|
+
MEASURE_ARROW_HEAD_PX,
|
|
17
|
+
MEASURE_CROSS_PX,
|
|
18
|
+
MEASURE_DIMENSION_OFFSET,
|
|
19
|
+
MEASURE_POINT_RADIUS_PX,
|
|
20
|
+
strokeWidthFor,
|
|
21
|
+
sweepAngle,
|
|
22
|
+
type Point,
|
|
23
|
+
} from "./measureGeometry";
|
|
24
|
+
import { closestOnSegment } from "./polylineIndex";
|
|
25
|
+
import { toShapeFrame } from "./shapeEdit";
|
|
26
|
+
|
|
27
|
+
/** The distance from `p` to segment ab. */
|
|
28
|
+
function segmentDistance(p: Point, ax: number, ay: number, bx: number, by: number): number {
|
|
29
|
+
const q = closestOnSegment(p, ax, ay, bx, by);
|
|
30
|
+
return Math.hypot(q.x - p.x, q.y - p.y);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* How far a point is from a primitive as drawn, in image pixels.
|
|
35
|
+
*
|
|
36
|
+
* - A dot, a filled circle and a caliper box are areas: the distance is 0 anywhere inside.
|
|
37
|
+
* - Every other kind is a line: the distance to its nearest stroke (a cross's arms, a
|
|
38
|
+
* circle's or an arc's rim, a dimension's three lines, a caliper's direction arrow).
|
|
39
|
+
* - Labels are not part of a primitive's shape.
|
|
40
|
+
*
|
|
41
|
+
* @param primitive - The primitive.
|
|
42
|
+
* @param point - The point, in image coordinates.
|
|
43
|
+
* @param strokeScale - The image-to-screen scale the overlay is drawn at, which sizes a dot,
|
|
44
|
+
* a cross and an arrow head.
|
|
45
|
+
* @returns The distance; `Infinity` for a primitive that draws nothing (a zero sweep, an empty
|
|
46
|
+
* polyline, a kind this version does not know).
|
|
47
|
+
*/
|
|
48
|
+
export function measurePrimitiveDistance(primitive: MeasurePrimitive, point: Point, strokeScale: number): number {
|
|
49
|
+
const p = point;
|
|
50
|
+
switch (primitive.kind) {
|
|
51
|
+
case "point": {
|
|
52
|
+
if (primitive.cross) {
|
|
53
|
+
const size = strokeWidthFor(strokeScale, MEASURE_CROSS_PX);
|
|
54
|
+
const { x, y } = primitive;
|
|
55
|
+
return Math.min(segmentDistance(p, x - size, y, x + size, y), segmentDistance(p, x, y - size, x, y + size));
|
|
56
|
+
}
|
|
57
|
+
const radius = strokeWidthFor(strokeScale, primitive.radius ?? MEASURE_POINT_RADIUS_PX);
|
|
58
|
+
return Math.max(0, Math.hypot(p.x - primitive.x, p.y - primitive.y) - radius);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
case "segment":
|
|
62
|
+
return segmentDistance(p, primitive.x1, primitive.y1, primitive.x2, primitive.y2);
|
|
63
|
+
|
|
64
|
+
case "segments": {
|
|
65
|
+
const s = primitive.points;
|
|
66
|
+
let best = Infinity;
|
|
67
|
+
for (let i = 0; i + 3 < s.length; i += 4) {
|
|
68
|
+
const d = segmentDistance(p, s[i]!, s[i + 1]!, s[i + 2]!, s[i + 3]!);
|
|
69
|
+
if (d < best) best = d;
|
|
70
|
+
}
|
|
71
|
+
return best;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
case "circle": {
|
|
75
|
+
const d = Math.hypot(p.x - primitive.cx, p.y - primitive.cy);
|
|
76
|
+
return primitive.filled ? Math.max(0, d - primitive.r) : Math.abs(d - primitive.r);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
case "arc":
|
|
80
|
+
return arcDistance(primitive.cx, primitive.cy, primitive.r, primitive.startAngle, primitive.endAngle, p);
|
|
81
|
+
|
|
82
|
+
case "caliper": {
|
|
83
|
+
const { cx, cy, width, height, angle } = primitive;
|
|
84
|
+
const u = toShapeFrame({ cx, cy, width, height, rotation: angle }, p);
|
|
85
|
+
let best = Math.hypot(Math.max(Math.abs(u.x) - width / 2, 0), Math.max(Math.abs(u.y) - height / 2, 0));
|
|
86
|
+
if (best > 0 && primitive.showDirection !== false) {
|
|
87
|
+
const arrow = caliperArrow(cx, cy, width, angle);
|
|
88
|
+
const [left, tip, right] = arrowHeadPoints(arrow.to, angle, strokeWidthFor(strokeScale, MEASURE_ARROW_HEAD_PX));
|
|
89
|
+
best = Math.min(
|
|
90
|
+
best,
|
|
91
|
+
segmentDistance(p, arrow.from.x, arrow.from.y, arrow.to.x, arrow.to.y),
|
|
92
|
+
segmentDistance(p, left.x, left.y, tip.x, tip.y),
|
|
93
|
+
segmentDistance(p, right.x, right.y, tip.x, tip.y),
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
return best;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
case "dimension": {
|
|
100
|
+
const g = dimensionGeometry(primitive.x1, primitive.y1, primitive.x2, primitive.y2, primitive.offset ?? MEASURE_DIMENSION_OFFSET);
|
|
101
|
+
let best = Infinity;
|
|
102
|
+
for (const [a, b] of [g.extensionLine1, g.extensionLine2, g.dimensionLine]) best = Math.min(best, segmentDistance(p, a.x, a.y, b.x, b.y));
|
|
103
|
+
return best;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
case "polyline": {
|
|
107
|
+
const s = primitive.points;
|
|
108
|
+
const n = Math.floor(s.length / 2);
|
|
109
|
+
if (n === 0) return Infinity;
|
|
110
|
+
if (n === 1) return Math.hypot(p.x - s[0]!, p.y - s[1]!);
|
|
111
|
+
let best = Infinity;
|
|
112
|
+
const count = primitive.closed === true ? n : n - 1;
|
|
113
|
+
for (let i = 0; i < count; i++) {
|
|
114
|
+
const j = (i + 1) % n;
|
|
115
|
+
const d = segmentDistance(p, s[2 * i]!, s[2 * i + 1]!, s[2 * j]!, s[2 * j + 1]!);
|
|
116
|
+
if (d < best) best = d;
|
|
117
|
+
}
|
|
118
|
+
return best;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
default:
|
|
122
|
+
return Infinity;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The distance from `p` to an arc drawn by `arcPath`: its rim within the sweep, else its nearer end. */
|
|
127
|
+
function arcDistance(cx: number, cy: number, r: number, startAngle: number, endAngle: number, p: Point): number {
|
|
128
|
+
if (!(r > 0)) return Infinity;
|
|
129
|
+
const sweep = sweepAngle(startAngle, endAngle);
|
|
130
|
+
if (sweep < 1e-9) return Infinity;
|
|
131
|
+
const d = Math.hypot(p.x - cx, p.y - cy);
|
|
132
|
+
const turn = 2 * Math.PI;
|
|
133
|
+
const along = (((Math.atan2(p.y - cy, p.x - cx) - startAngle) % turn) + turn) % turn;
|
|
134
|
+
if (along <= sweep) return Math.abs(d - r);
|
|
135
|
+
const end = (a: number) => Math.hypot(p.x - cx - r * Math.cos(a), p.y - cy - r * Math.sin(a));
|
|
136
|
+
return Math.min(end(startAngle), end(startAngle + sweep));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The primitives with an `id`, gridded by bounding box: a hover over thousands of them visits
|
|
141
|
+
* a handful of cells, not every primitive.
|
|
142
|
+
*/
|
|
143
|
+
interface MeasureIndex {
|
|
144
|
+
/** Per primitive, five numbers: its box (`minX`, `minY`, `maxX`, `maxY`, image pixels) and how far past it its mark reaches, in screen pixels. */
|
|
145
|
+
readonly boxes: Float64Array;
|
|
146
|
+
/** The largest such reach. */
|
|
147
|
+
readonly maxPad: number;
|
|
148
|
+
readonly cell: number;
|
|
149
|
+
readonly x0: number;
|
|
150
|
+
readonly y0: number;
|
|
151
|
+
readonly cols: number;
|
|
152
|
+
readonly rows: number;
|
|
153
|
+
/** CSR layout: cell `c`'s primitives are `entries[starts[c] .. starts[c + 1]]`. */
|
|
154
|
+
readonly starts: Uint32Array;
|
|
155
|
+
readonly entries: Uint32Array;
|
|
156
|
+
/** Primitives whose box spans too many cells to grid: checked on every query. */
|
|
157
|
+
readonly large: Uint32Array;
|
|
158
|
+
/** The query each primitive was last visited by, so one in several cells is measured once. */
|
|
159
|
+
readonly seen: Uint32Array;
|
|
160
|
+
query: number;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** A box spanning more cells than this is checked on every query rather than gridded. */
|
|
164
|
+
const LARGE_CELLS = 64;
|
|
165
|
+
|
|
166
|
+
const indexCache = new WeakMap<readonly MeasurePrimitive[], MeasureIndex>();
|
|
167
|
+
|
|
168
|
+
function indexOf(primitives: readonly MeasurePrimitive[]): MeasureIndex {
|
|
169
|
+
const cached = indexCache.get(primitives);
|
|
170
|
+
if (cached && cached.seen.length === primitives.length) return cached;
|
|
171
|
+
const n = primitives.length;
|
|
172
|
+
const boxes = new Float64Array(n * 5);
|
|
173
|
+
let maxPad = 0;
|
|
174
|
+
let minX = Infinity;
|
|
175
|
+
let minY = Infinity;
|
|
176
|
+
let maxX = -Infinity;
|
|
177
|
+
let maxY = -Infinity;
|
|
178
|
+
const gridded: number[] = [];
|
|
179
|
+
for (let i = 0; i < n; i++) {
|
|
180
|
+
const primitive = primitives[i]!;
|
|
181
|
+
const box = boxOf(primitive);
|
|
182
|
+
boxes.set(box, i * 5);
|
|
183
|
+
// Only a primitive with an id can be picked, and only a finite box can contain a point.
|
|
184
|
+
if (primitive.id === undefined || !box.every(Number.isFinite)) continue;
|
|
185
|
+
gridded.push(i);
|
|
186
|
+
if (box[4] > maxPad) maxPad = box[4];
|
|
187
|
+
if (box[0] < minX) minX = box[0];
|
|
188
|
+
if (box[1] < minY) minY = box[1];
|
|
189
|
+
if (box[2] > maxX) maxX = box[2];
|
|
190
|
+
if (box[3] > maxY) maxY = box[3];
|
|
191
|
+
}
|
|
192
|
+
// About one primitive per cell over the occupied extent.
|
|
193
|
+
const width = Math.max(1, maxX - minX);
|
|
194
|
+
const height = Math.max(1, maxY - minY);
|
|
195
|
+
const cell = gridded.length > 0 ? Math.max(1, Math.sqrt((width * height) / gridded.length)) : 1;
|
|
196
|
+
const cols = gridded.length > 0 ? Math.min(4096, Math.floor(width / cell) + 1) : 1;
|
|
197
|
+
const rows = gridded.length > 0 ? Math.min(4096, Math.floor(height / cell) + 1) : 1;
|
|
198
|
+
const x0 = gridded.length > 0 ? minX : 0;
|
|
199
|
+
const y0 = gridded.length > 0 ? minY : 0;
|
|
200
|
+
const cellX = (x: number) => Math.min(cols - 1, Math.max(0, Math.floor((x - x0) / cell)));
|
|
201
|
+
const cellY = (y: number) => Math.min(rows - 1, Math.max(0, Math.floor((y - y0) / cell)));
|
|
202
|
+
|
|
203
|
+
const large: number[] = [];
|
|
204
|
+
const counts = new Uint32Array(cols * rows + 1);
|
|
205
|
+
const spans: [number, number, number, number][] = [];
|
|
206
|
+
for (const i of gridded) {
|
|
207
|
+
const span: [number, number, number, number] = [cellX(boxes[i * 5]!), cellY(boxes[i * 5 + 1]!), cellX(boxes[i * 5 + 2]!), cellY(boxes[i * 5 + 3]!)];
|
|
208
|
+
spans.push(span);
|
|
209
|
+
if ((span[2] - span[0] + 1) * (span[3] - span[1] + 1) > LARGE_CELLS) {
|
|
210
|
+
large.push(i);
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
for (let cy = span[1]; cy <= span[3]; cy++) for (let cx = span[0]; cx <= span[2]; cx++) counts[cy * cols + cx + 1]!++;
|
|
214
|
+
}
|
|
215
|
+
for (let c = 0; c < cols * rows; c++) counts[c + 1]! += counts[c]!;
|
|
216
|
+
const starts = counts;
|
|
217
|
+
const fill = starts.slice(0, cols * rows);
|
|
218
|
+
const entries = new Uint32Array(starts[cols * rows]!);
|
|
219
|
+
gridded.forEach((i, k) => {
|
|
220
|
+
const span = spans[k]!;
|
|
221
|
+
if ((span[2] - span[0] + 1) * (span[3] - span[1] + 1) > LARGE_CELLS) return;
|
|
222
|
+
for (let cy = span[1]; cy <= span[3]; cy++) for (let cx = span[0]; cx <= span[2]; cx++) entries[fill[cy * cols + cx]!++] = i;
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
const index: MeasureIndex = {
|
|
226
|
+
boxes,
|
|
227
|
+
maxPad,
|
|
228
|
+
cell,
|
|
229
|
+
x0,
|
|
230
|
+
y0,
|
|
231
|
+
cols,
|
|
232
|
+
rows,
|
|
233
|
+
starts,
|
|
234
|
+
entries,
|
|
235
|
+
large: Uint32Array.from(large),
|
|
236
|
+
seen: new Uint32Array(n),
|
|
237
|
+
query: 0,
|
|
238
|
+
};
|
|
239
|
+
indexCache.set(primitives, index);
|
|
240
|
+
return index;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** A primitive's box in image pixels, and how far past it its mark reaches in screen pixels. */
|
|
244
|
+
function boxOf(primitive: MeasurePrimitive): [number, number, number, number, number] {
|
|
245
|
+
let minX = Infinity;
|
|
246
|
+
let minY = Infinity;
|
|
247
|
+
let maxX = -Infinity;
|
|
248
|
+
let maxY = -Infinity;
|
|
249
|
+
let pad = 0;
|
|
250
|
+
const add = (x: number, y: number) => {
|
|
251
|
+
// A NaN poisons the box, so that a primitive with one is never picked.
|
|
252
|
+
if (Number.isNaN(x) || Number.isNaN(y)) {
|
|
253
|
+
minX = Number.NaN;
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (x < minX) minX = x;
|
|
257
|
+
if (x > maxX) maxX = x;
|
|
258
|
+
if (y < minY) minY = y;
|
|
259
|
+
if (y > maxY) maxY = y;
|
|
260
|
+
};
|
|
261
|
+
switch (primitive.kind) {
|
|
262
|
+
case "point":
|
|
263
|
+
add(primitive.x, primitive.y);
|
|
264
|
+
pad = primitive.cross ? MEASURE_CROSS_PX : (primitive.radius ?? MEASURE_POINT_RADIUS_PX);
|
|
265
|
+
break;
|
|
266
|
+
case "segment":
|
|
267
|
+
add(primitive.x1, primitive.y1);
|
|
268
|
+
add(primitive.x2, primitive.y2);
|
|
269
|
+
break;
|
|
270
|
+
case "circle":
|
|
271
|
+
case "arc":
|
|
272
|
+
add(primitive.cx - primitive.r, primitive.cy - primitive.r);
|
|
273
|
+
add(primitive.cx + primitive.r, primitive.cy + primitive.r);
|
|
274
|
+
break;
|
|
275
|
+
case "caliper": {
|
|
276
|
+
for (const corner of caliperCorners(primitive.cx, primitive.cy, primitive.width, primitive.height, primitive.angle)) add(corner.x, corner.y);
|
|
277
|
+
const tip = caliperArrow(primitive.cx, primitive.cy, primitive.width, primitive.angle).to;
|
|
278
|
+
add(tip.x, tip.y);
|
|
279
|
+
// The head's back corners reach 1.6 × its size behind the tip and 1 × to the side.
|
|
280
|
+
pad = MEASURE_ARROW_HEAD_PX * 2;
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
case "dimension": {
|
|
284
|
+
const g = dimensionGeometry(primitive.x1, primitive.y1, primitive.x2, primitive.y2, primitive.offset ?? MEASURE_DIMENSION_OFFSET);
|
|
285
|
+
for (const [a, b] of [g.extensionLine1, g.extensionLine2]) {
|
|
286
|
+
add(a.x, a.y);
|
|
287
|
+
add(b.x, b.y);
|
|
288
|
+
}
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
case "segments": {
|
|
292
|
+
// Only whole, finite segments are drawn; a skipped one does not poison the rest.
|
|
293
|
+
const s = primitive.points;
|
|
294
|
+
for (let k = 0; k + 3 < s.length; k += 4) {
|
|
295
|
+
if (!(Number.isFinite(s[k]) && Number.isFinite(s[k + 1]) && Number.isFinite(s[k + 2]) && Number.isFinite(s[k + 3]))) continue;
|
|
296
|
+
add(s[k]!, s[k + 1]!);
|
|
297
|
+
add(s[k + 2]!, s[k + 3]!);
|
|
298
|
+
}
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
case "polyline": {
|
|
302
|
+
const s = primitive.points;
|
|
303
|
+
for (let k = 0; k + 1 < s.length; k += 2) add(s[k]!, s[k + 1]!);
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
return [minX, minY, maxX, maxY, pad];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* The primitive nearest to `point` within `radius`, among those with an `id`.
|
|
312
|
+
*
|
|
313
|
+
* The primitives are gridded by bounding box once per `primitives` array, and the grid is
|
|
314
|
+
* kept while that array lives, so a hover over thousands of primitives measures only those
|
|
315
|
+
* near the pointer. Pass a new array when the primitives change (as React state does): one
|
|
316
|
+
* edited in place keeps its old grid.
|
|
317
|
+
*
|
|
318
|
+
* @param primitives - The primitives, bottom to top, as `MeasureOverlay` draws them.
|
|
319
|
+
* @param point - The point, in image coordinates.
|
|
320
|
+
* @param radius - The search radius, in image pixels.
|
|
321
|
+
* @param strokeScale - The image-to-screen scale the overlay is drawn at.
|
|
322
|
+
* @returns The nearest primitive's id and distance, or `null`. A tie goes to the later
|
|
323
|
+
* primitive, which is drawn on top. A primitive with a NaN coordinate is never picked,
|
|
324
|
+
* except a `segments` one, whose bad segments are skipped as they are when drawn.
|
|
325
|
+
*/
|
|
326
|
+
export function nearestMeasurePrimitive(
|
|
327
|
+
primitives: readonly MeasurePrimitive[],
|
|
328
|
+
point: Point,
|
|
329
|
+
radius: number,
|
|
330
|
+
strokeScale: number,
|
|
331
|
+
): { id: string; distance: number } | null {
|
|
332
|
+
const index = indexOf(primitives);
|
|
333
|
+
const { boxes, cell, x0, y0, cols, rows, starts, entries, large, seen } = index;
|
|
334
|
+
const screen = strokeWidthFor(strokeScale, 1);
|
|
335
|
+
index.query = (index.query + 1) >>> 0 || 1;
|
|
336
|
+
const query = index.query;
|
|
337
|
+
let best: { id: string; distance: number } | null = null;
|
|
338
|
+
let bestIndex = -1;
|
|
339
|
+
|
|
340
|
+
const consider = (i: number) => {
|
|
341
|
+
if (seen[i] === query) return;
|
|
342
|
+
seen[i] = query;
|
|
343
|
+
const reach = radius + boxes[i * 5 + 4]! * screen;
|
|
344
|
+
if (
|
|
345
|
+
!(point.x >= boxes[i * 5]! - reach && point.x <= boxes[i * 5 + 2]! + reach && point.y >= boxes[i * 5 + 1]! - reach && point.y <= boxes[i * 5 + 3]! + reach)
|
|
346
|
+
) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const primitive = primitives[i]!;
|
|
350
|
+
const distance = measurePrimitiveDistance(primitive, point, strokeScale);
|
|
351
|
+
if (distance > radius) return;
|
|
352
|
+
if (best === null || distance < best.distance || (distance === best.distance && i > bestIndex)) {
|
|
353
|
+
best = { id: primitive.id!, distance };
|
|
354
|
+
bestIndex = i;
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const reach = radius + index.maxPad * screen;
|
|
359
|
+
const cx0 = Math.max(0, Math.floor((point.x - reach - x0) / cell));
|
|
360
|
+
const cx1 = Math.min(cols - 1, Math.floor((point.x + reach - x0) / cell));
|
|
361
|
+
const cy0 = Math.max(0, Math.floor((point.y - reach - y0) / cell));
|
|
362
|
+
const cy1 = Math.min(rows - 1, Math.floor((point.y + reach - y0) / cell));
|
|
363
|
+
for (let cy = cy0; cy <= cy1; cy++) {
|
|
364
|
+
for (let cx = cx0; cx <= cx1; cx++) {
|
|
365
|
+
const c = cy * cols + cx;
|
|
366
|
+
for (let k = starts[c]!; k < starts[c + 1]!; k++) consider(entries[k]!);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
for (let k = 0; k < large.length; k++) consider(large[k]!);
|
|
370
|
+
return best;
|
|
371
|
+
}
|
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
* caught, and which ones are far enough apart to carry a label.
|
|
4
4
|
*
|
|
5
5
|
* The same uniform grid as `polylineIndex.ts`, in CSR layout (cell `c`'s entries are
|
|
6
|
-
* `starts[c] .. starts[c + 1]`, no per-cell arrays)
|
|
7
|
-
*
|
|
8
|
-
* (`docs/measurements/stage2d-bench-analysis.md`), and it imports neither React nor the DOM.
|
|
6
|
+
* `starts[c] .. starts[c + 1]`, no per-cell arrays). It answers in microseconds whichever engine
|
|
7
|
+
* draws the markers, and it imports neither React nor the DOM.
|
|
9
8
|
*
|
|
10
9
|
* Points are flat `[x0, y0, x1, y1, …]` arrays in image coordinates, the form a detector
|
|
11
10
|
* returns and the cheapest to keep. A point with a non-finite coordinate is not indexed.
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* A spatial index over many polylines: which one is under the pointer, and which ones a
|
|
3
3
|
* rubber band caught.
|
|
4
4
|
*
|
|
5
|
-
* Asking the DOM is too slow at scale.
|
|
6
|
-
* 20k elements
|
|
7
|
-
* microseconds whichever engine draws the lines
|
|
5
|
+
* Asking the DOM is too slow at scale. Measured, `elementFromPoint` over
|
|
6
|
+
* 20k elements takes about 2.1 ms at p95, over a 2 ms budget. A uniform grid of segments answers in
|
|
7
|
+
* microseconds whichever engine draws the lines.
|
|
8
8
|
*
|
|
9
9
|
* Points are flat `[x0, y0, x1, y1, …]` arrays in image coordinates, the form a backend
|
|
10
10
|
* returns and the cheapest to keep.
|
|
@@ -139,9 +139,19 @@ export function resizeShape(shape: RotatedShape, handle: RoiHandle, to: Point, m
|
|
|
139
139
|
*/
|
|
140
140
|
export function rotateShape(shape: RotatedShape, to: Point, snap = 0): RotatedShape {
|
|
141
141
|
// The handle sits along the shape's own up direction, which is at angle `rotation - π/2`.
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
142
|
+
const rotation = Math.atan2(to.y - shape.cy, to.x - shape.cx) + Math.PI / 2;
|
|
143
|
+
return { ...shape, rotation: normalizeAngle(snapAngle(rotation, snap)) };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* An angle rounded to the nearest multiple of a step: what Shift does to a rotation.
|
|
148
|
+
*
|
|
149
|
+
* @param angle - Radians.
|
|
150
|
+
* @param step - The step, in radians (`Math.PI / 12` for 15°). 0 or less leaves the angle as it is.
|
|
151
|
+
* @returns The rounded angle. It is not brought into `(-π, π]`; pass it through `normalizeAngle` for that.
|
|
152
|
+
*/
|
|
153
|
+
export function snapAngle(angle: number, step: number): number {
|
|
154
|
+
return step > 0 ? Math.round(angle / step) * step : angle;
|
|
145
155
|
}
|
|
146
156
|
|
|
147
157
|
/**
|
|
@@ -255,6 +265,22 @@ function distanceOutside(shape: RotatedShape, kind: "rect" | "ellipse", p: Point
|
|
|
255
265
|
return (f - Math.sqrt(f)) / g;
|
|
256
266
|
}
|
|
257
267
|
|
|
268
|
+
/**
|
|
269
|
+
* How far a point inside a shape is from its outline, in image pixels; zero outside it. For an
|
|
270
|
+
* ellipse the same first-order estimate as `distanceOutside`, which grows without bound towards
|
|
271
|
+
* the centre.
|
|
272
|
+
*/
|
|
273
|
+
function distanceInside(shape: RotatedShape, kind: "rect" | "ellipse", p: Point): number {
|
|
274
|
+
const u = toShapeFrame(shape, p);
|
|
275
|
+
const a = Math.max(shape.width / 2, 1e-9);
|
|
276
|
+
const b = Math.max(shape.height / 2, 1e-9);
|
|
277
|
+
if (kind === "rect") return Math.max(0, Math.min(a - Math.abs(u.x), b - Math.abs(u.y)));
|
|
278
|
+
const f = (u.x / a) ** 2 + (u.y / b) ** 2;
|
|
279
|
+
if (f >= 1) return 0;
|
|
280
|
+
const g = Math.hypot(u.x / (a * a), u.y / (b * b));
|
|
281
|
+
return g > 0 ? (Math.sqrt(f) - f) / g : Infinity;
|
|
282
|
+
}
|
|
283
|
+
|
|
258
284
|
/**
|
|
259
285
|
* What a press on an editable shape means, decided once for the whole editor so that a small
|
|
260
286
|
* shape's interior never steals a press meant for its handle.
|
|
@@ -262,7 +288,8 @@ function distanceOutside(shape: RotatedShape, kind: "rect" | "ellipse", p: Point
|
|
|
262
288
|
* In order:
|
|
263
289
|
* 1. The nearest handle (the eight, and the rotation handle when `rotatable`) whose screen
|
|
264
290
|
* distance from the press is within `radius` plus the handle's half size: resize or rotate.
|
|
265
|
-
* 2. Inside the shape, or within `radius` screen pixels of its outline
|
|
291
|
+
* 2. Inside the shape (when `interior`), or within `radius` screen pixels of its outline on
|
|
292
|
+
* either side: move.
|
|
266
293
|
* 3. Otherwise `null`: the press is declined and belongs to whatever is below.
|
|
267
294
|
*
|
|
268
295
|
* @param shape - The shape.
|
|
@@ -271,6 +298,9 @@ function distanceOutside(shape: RotatedShape, kind: "rect" | "ellipse", p: Point
|
|
|
271
298
|
* @param point - The press, in image coordinates.
|
|
272
299
|
* @param radius - The pointer's tolerance in screen pixels: 12 for a touch, 6 for a mouse.
|
|
273
300
|
* @param rotatable - Whether the rotation handle exists. Defaults to `true`.
|
|
301
|
+
* @param interior - Whether a press inside the shape, away from its outline, moves it.
|
|
302
|
+
* Defaults to `true`; `false` leaves the inside to whatever is below, so only the handles
|
|
303
|
+
* and the band along the outline grab the shape.
|
|
274
304
|
* @returns The decision, or `null`.
|
|
275
305
|
*/
|
|
276
306
|
export function shapePress(
|
|
@@ -280,6 +310,7 @@ export function shapePress(
|
|
|
280
310
|
point: Point,
|
|
281
311
|
radius: number,
|
|
282
312
|
rotatable = true,
|
|
313
|
+
interior = true,
|
|
283
314
|
): ShapePress | null {
|
|
284
315
|
const best: { press: ShapePress | null; distance: number } = { press: null, distance: Infinity };
|
|
285
316
|
const consider = (candidate: ShapePress, at: Point, half: number) => {
|
|
@@ -294,5 +325,7 @@ export function shapePress(
|
|
|
294
325
|
consider({ kind: "rotate" }, rotationHandlePoint(shape, SHAPE_ROTATE_OFFSET_PX / scale), SHAPE_ROTATE_RADIUS_PX);
|
|
295
326
|
}
|
|
296
327
|
if (best.press) return best.press;
|
|
297
|
-
|
|
328
|
+
const outside = distanceOutside(shape, kind, point);
|
|
329
|
+
if (outside > 0) return outside * scale <= radius ? { kind: "move" } : null;
|
|
330
|
+
return interior || distanceInside(shape, kind, point) * scale <= radius ? { kind: "move" } : null;
|
|
298
331
|
}
|