@vitavision/stage2d 0.12.0 → 0.13.2
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/api/mapValues.ts +1 -4
- package/src/components/AreaSet.tsx +32 -9
- package/src/components/ContourEditor.tsx +325 -40
- package/src/components/DatumEditor.tsx +261 -0
- package/src/components/DraftShape.tsx +37 -5
- package/src/components/EllipseSet.tsx +3 -3
- package/src/components/GridLayer.tsx +8 -3
- package/src/components/HeatmapLayer.tsx +1 -1
- package/src/components/MaskEditor.tsx +57 -9
- package/src/components/MeasureOverlay.tsx +353 -92
- package/src/components/PointSet.tsx +23 -12
- package/src/components/PolylineSet.tsx +83 -27
- package/src/components/RectRoiEditor.tsx +189 -93
- package/src/components/ShapeEditor.tsx +25 -10
- package/src/components/areaBatching.ts +23 -3
- package/src/components/areaIndex.ts +2 -1
- package/src/components/contourArc.ts +268 -0
- package/src/components/datumEdit.ts +149 -0
- package/src/components/labelLod.ts +3 -7
- package/src/components/markerShapes.ts +2 -5
- package/src/components/measureGeometry.ts +46 -28
- package/src/components/measureHit.ts +383 -0
- package/src/components/overlayRole.ts +1 -1
- package/src/components/pointIndex.ts +14 -5
- package/src/components/polylineIndex.ts +3 -3
- package/src/components/shapeEdit.ts +38 -5
- package/src/components/stage/CompareLayer.tsx +231 -0
- package/src/components/stage/ImageLayer.tsx +56 -24
- package/src/components/stage/ImageStage.tsx +33 -38
- package/src/components/stage/StageSurface.tsx +34 -34
- package/src/components/stage/StageToolbar.tsx +35 -37
- 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 +17 -12
- package/src/components/stage/view.ts +3 -15
- package/src/components/useShapeDrag.ts +2 -4
- package/src/index.ts +34 -65
|
@@ -10,6 +10,15 @@
|
|
|
10
10
|
* that reads as constant on screen.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
/** `MeasureOverlay`'s dot radius when a point names none, in screen pixels. */
|
|
14
|
+
export const MEASURE_POINT_RADIUS_PX = 3;
|
|
15
|
+
/** The arm of a cross mark, from its centre, in screen pixels. */
|
|
16
|
+
export const MEASURE_CROSS_PX = 5;
|
|
17
|
+
/** How far a dimension line sits off its span when it names no offset, in image pixels. */
|
|
18
|
+
export const MEASURE_DIMENSION_OFFSET = 16;
|
|
19
|
+
/** The size of a caliper's arrow head (`arrowHeadPoints`' `size`), in screen pixels. */
|
|
20
|
+
export const MEASURE_ARROW_HEAD_PX = 4;
|
|
21
|
+
|
|
13
22
|
/** A 2D point — in image pixels unless a signature says otherwise. */
|
|
14
23
|
export interface Point {
|
|
15
24
|
/** Horizontal coordinate, increasing rightward. */
|
|
@@ -57,11 +66,41 @@ export function polygonPath(points: readonly Point[]): string {
|
|
|
57
66
|
return commands.join(" ");
|
|
58
67
|
}
|
|
59
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Many unconnected segments as one SVG path: `M x1 y1 L x2 y2` per segment.
|
|
71
|
+
*
|
|
72
|
+
* One path draws thousands of ticks for the cost of one element. A segment with a
|
|
73
|
+
* non-finite coordinate is skipped rather than written, since a single `NaN` in path data
|
|
74
|
+
* stops the browser drawing every segment after it.
|
|
75
|
+
*
|
|
76
|
+
* @param points - `[x1, y1, x2, y2, …]`, four numbers per segment. A trailing partial
|
|
77
|
+
* segment is ignored.
|
|
78
|
+
* @returns The path data; `""` without a whole segment.
|
|
79
|
+
*/
|
|
80
|
+
export function segmentsPath(points: ArrayLike<number>): string {
|
|
81
|
+
let d = "";
|
|
82
|
+
for (let i = 0; i + 3 < points.length; i += 4) {
|
|
83
|
+
const x1 = points[i]!;
|
|
84
|
+
const y1 = points[i + 1]!;
|
|
85
|
+
const x2 = points[i + 2]!;
|
|
86
|
+
const y2 = points[i + 3]!;
|
|
87
|
+
if (!(Number.isFinite(x1) && Number.isFinite(y1) && Number.isFinite(x2) && Number.isFinite(y2))) continue;
|
|
88
|
+
d += `M${x1} ${y1}L${x2} ${y2}`;
|
|
89
|
+
}
|
|
90
|
+
return d;
|
|
91
|
+
}
|
|
92
|
+
|
|
60
93
|
/** The two line segments of a cross mark centred on `(x, y)`, each `2·size` long. */
|
|
61
94
|
export function crossSegments(x: number, y: number, size: number): [[Point, Point], [Point, Point]] {
|
|
62
95
|
return [
|
|
63
|
-
[
|
|
64
|
-
|
|
96
|
+
[
|
|
97
|
+
{ x: x - size, y },
|
|
98
|
+
{ x: x + size, y },
|
|
99
|
+
],
|
|
100
|
+
[
|
|
101
|
+
{ x, y: y - size },
|
|
102
|
+
{ x, y: y + size },
|
|
103
|
+
],
|
|
65
104
|
];
|
|
66
105
|
}
|
|
67
106
|
|
|
@@ -75,7 +114,7 @@ export function crossSegments(x: number, y: number, size: number): [[Point, Poin
|
|
|
75
114
|
* it). So only a raw difference near zero is treated as "nothing to draw"; every other
|
|
76
115
|
* multiple of a full turn normalizes to `2π` itself rather than collapsing to `0`.
|
|
77
116
|
*/
|
|
78
|
-
function sweepAngle(startAngle: number, endAngle: number): number {
|
|
117
|
+
export function sweepAngle(startAngle: number, endAngle: number): number {
|
|
79
118
|
const turn = Math.PI * 2;
|
|
80
119
|
const EPSILON = 1e-9;
|
|
81
120
|
const raw = endAngle - startAngle;
|
|
@@ -126,13 +165,7 @@ export function arcPath(cx: number, cy: number, r: number, startAngle: number, e
|
|
|
126
165
|
* by `height`, centred at `(cx, cy)` and rotated by `angle` radians. Corner order is
|
|
127
166
|
* suitable for `polygonPath` — around the box, not crossed.
|
|
128
167
|
*/
|
|
129
|
-
export function caliperCorners(
|
|
130
|
-
cx: number,
|
|
131
|
-
cy: number,
|
|
132
|
-
width: number,
|
|
133
|
-
height: number,
|
|
134
|
-
angle: number,
|
|
135
|
-
): [Point, Point, Point, Point] {
|
|
168
|
+
export function caliperCorners(cx: number, cy: number, width: number, height: number, angle: number): [Point, Point, Point, Point] {
|
|
136
169
|
const hw = width / 2;
|
|
137
170
|
const hh = height / 2;
|
|
138
171
|
const local: [number, number][] = [
|
|
@@ -153,12 +186,7 @@ export function caliperCorners(
|
|
|
153
186
|
* along the box's own measurement axis. What the arrow points at is the scan direction the
|
|
154
187
|
* caliper was evaluated in, not merely a decoration on the box.
|
|
155
188
|
*/
|
|
156
|
-
export function caliperArrow(
|
|
157
|
-
cx: number,
|
|
158
|
-
cy: number,
|
|
159
|
-
width: number,
|
|
160
|
-
angle: number,
|
|
161
|
-
): { from: Point; to: Point } {
|
|
189
|
+
export function caliperArrow(cx: number, cy: number, width: number, angle: number): { from: Point; to: Point } {
|
|
162
190
|
const reach = width / 2 + Math.max(4, width * 0.2);
|
|
163
191
|
const tip = rotatePoint(reach, 0, angle);
|
|
164
192
|
return { from: { x: cx, y: cy }, to: { x: cx + tip.x, y: cy + tip.y } };
|
|
@@ -175,11 +203,7 @@ export function arrowHeadPoints(tip: Point, angle: number, size: number): [Point
|
|
|
175
203
|
const spread = size;
|
|
176
204
|
const left = rotatePoint(-back, spread, angle);
|
|
177
205
|
const right = rotatePoint(-back, -spread, angle);
|
|
178
|
-
return [
|
|
179
|
-
{ x: tip.x + left.x, y: tip.y + left.y },
|
|
180
|
-
tip,
|
|
181
|
-
{ x: tip.x + right.x, y: tip.y + right.y },
|
|
182
|
-
];
|
|
206
|
+
return [{ x: tip.x + left.x, y: tip.y + left.y }, tip, { x: tip.x + right.x, y: tip.y + right.y }];
|
|
183
207
|
}
|
|
184
208
|
|
|
185
209
|
/** What `dimensionGeometry` computes: the lines and label placement of a dimension annotation. */
|
|
@@ -204,13 +228,7 @@ export interface DimensionGeometry {
|
|
|
204
228
|
* Degenerate input (`x1 === x2 && y1 === y2`) falls back to an arbitrary rightward normal
|
|
205
229
|
* rather than dividing by zero, so a caller need not special-case a zero-length dimension.
|
|
206
230
|
*/
|
|
207
|
-
export function dimensionGeometry(
|
|
208
|
-
x1: number,
|
|
209
|
-
y1: number,
|
|
210
|
-
x2: number,
|
|
211
|
-
y2: number,
|
|
212
|
-
offset: number,
|
|
213
|
-
): DimensionGeometry {
|
|
231
|
+
export function dimensionGeometry(x1: number, y1: number, x2: number, y2: number, offset: number): DimensionGeometry {
|
|
214
232
|
const dx = x2 - x1;
|
|
215
233
|
const dy = y2 - y1;
|
|
216
234
|
const length = Math.hypot(dx, dy);
|
|
@@ -0,0 +1,383 @@
|
|
|
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])
|
|
103
|
+
best = Math.min(best, segmentDistance(p, a.x, a.y, b.x, b.y));
|
|
104
|
+
return best;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
case "polyline": {
|
|
108
|
+
const s = primitive.points;
|
|
109
|
+
const n = Math.floor(s.length / 2);
|
|
110
|
+
if (n === 0) return Infinity;
|
|
111
|
+
if (n === 1) return Math.hypot(p.x - s[0]!, p.y - s[1]!);
|
|
112
|
+
let best = Infinity;
|
|
113
|
+
const count = primitive.closed === true ? n : n - 1;
|
|
114
|
+
for (let i = 0; i < count; i++) {
|
|
115
|
+
const j = (i + 1) % n;
|
|
116
|
+
const d = segmentDistance(p, s[2 * i]!, s[2 * i + 1]!, s[2 * j]!, s[2 * j + 1]!);
|
|
117
|
+
if (d < best) best = d;
|
|
118
|
+
}
|
|
119
|
+
return best;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
default:
|
|
123
|
+
return Infinity;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The distance from `p` to an arc drawn by `arcPath`: its rim within the sweep, else its nearer end. */
|
|
128
|
+
function arcDistance(cx: number, cy: number, r: number, startAngle: number, endAngle: number, p: Point): number {
|
|
129
|
+
if (!(r > 0)) return Infinity;
|
|
130
|
+
const sweep = sweepAngle(startAngle, endAngle);
|
|
131
|
+
if (sweep < 1e-9) return Infinity;
|
|
132
|
+
const d = Math.hypot(p.x - cx, p.y - cy);
|
|
133
|
+
const turn = 2 * Math.PI;
|
|
134
|
+
const along = (((Math.atan2(p.y - cy, p.x - cx) - startAngle) % turn) + turn) % turn;
|
|
135
|
+
if (along <= sweep) return Math.abs(d - r);
|
|
136
|
+
const end = (a: number) => Math.hypot(p.x - cx - r * Math.cos(a), p.y - cy - r * Math.sin(a));
|
|
137
|
+
return Math.min(end(startAngle), end(startAngle + sweep));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The primitives with an `id`, gridded by bounding box: a hover over thousands of them visits
|
|
142
|
+
* a handful of cells, not every primitive.
|
|
143
|
+
*/
|
|
144
|
+
interface MeasureIndex {
|
|
145
|
+
/** Per primitive, five numbers: its box (`minX`, `minY`, `maxX`, `maxY`, image pixels) and how far past it its mark reaches, in screen pixels. */
|
|
146
|
+
readonly boxes: Float64Array;
|
|
147
|
+
/** The largest such reach. */
|
|
148
|
+
readonly maxPad: number;
|
|
149
|
+
readonly cell: number;
|
|
150
|
+
readonly x0: number;
|
|
151
|
+
readonly y0: number;
|
|
152
|
+
readonly cols: number;
|
|
153
|
+
readonly rows: number;
|
|
154
|
+
/** CSR layout: cell `c`'s primitives are `entries[starts[c] .. starts[c + 1]]`. */
|
|
155
|
+
readonly starts: Uint32Array;
|
|
156
|
+
readonly entries: Uint32Array;
|
|
157
|
+
/** Primitives whose box spans too many cells to grid: checked on every query. */
|
|
158
|
+
readonly large: Uint32Array;
|
|
159
|
+
/** The query each primitive was last visited by, so one in several cells is measured once. */
|
|
160
|
+
readonly seen: Uint32Array;
|
|
161
|
+
query: number;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** A box spanning more cells than this is checked on every query rather than gridded. */
|
|
165
|
+
const LARGE_CELLS = 64;
|
|
166
|
+
|
|
167
|
+
const indexCache = new WeakMap<readonly MeasurePrimitive[], MeasureIndex>();
|
|
168
|
+
|
|
169
|
+
function indexOf(primitives: readonly MeasurePrimitive[]): MeasureIndex {
|
|
170
|
+
const cached = indexCache.get(primitives);
|
|
171
|
+
if (cached && cached.seen.length === primitives.length) return cached;
|
|
172
|
+
const n = primitives.length;
|
|
173
|
+
const boxes = new Float64Array(n * 5);
|
|
174
|
+
let maxPad = 0;
|
|
175
|
+
let minX = Infinity;
|
|
176
|
+
let minY = Infinity;
|
|
177
|
+
let maxX = -Infinity;
|
|
178
|
+
let maxY = -Infinity;
|
|
179
|
+
const gridded: number[] = [];
|
|
180
|
+
for (let i = 0; i < n; i++) {
|
|
181
|
+
const primitive = primitives[i]!;
|
|
182
|
+
const box = boxOf(primitive);
|
|
183
|
+
boxes.set(box, i * 5);
|
|
184
|
+
// Only a primitive with an id can be picked, and only a finite box can contain a point.
|
|
185
|
+
if (primitive.id === undefined || !box.every(Number.isFinite)) continue;
|
|
186
|
+
gridded.push(i);
|
|
187
|
+
if (box[4] > maxPad) maxPad = box[4];
|
|
188
|
+
if (box[0] < minX) minX = box[0];
|
|
189
|
+
if (box[1] < minY) minY = box[1];
|
|
190
|
+
if (box[2] > maxX) maxX = box[2];
|
|
191
|
+
if (box[3] > maxY) maxY = box[3];
|
|
192
|
+
}
|
|
193
|
+
// About one primitive per cell over the occupied extent.
|
|
194
|
+
const width = Math.max(1, maxX - minX);
|
|
195
|
+
const height = Math.max(1, maxY - minY);
|
|
196
|
+
const cell = gridded.length > 0 ? Math.max(1, Math.sqrt((width * height) / gridded.length)) : 1;
|
|
197
|
+
const cols = gridded.length > 0 ? Math.min(4096, Math.floor(width / cell) + 1) : 1;
|
|
198
|
+
const rows = gridded.length > 0 ? Math.min(4096, Math.floor(height / cell) + 1) : 1;
|
|
199
|
+
const x0 = gridded.length > 0 ? minX : 0;
|
|
200
|
+
const y0 = gridded.length > 0 ? minY : 0;
|
|
201
|
+
const cellX = (x: number) => Math.min(cols - 1, Math.max(0, Math.floor((x - x0) / cell)));
|
|
202
|
+
const cellY = (y: number) => Math.min(rows - 1, Math.max(0, Math.floor((y - y0) / cell)));
|
|
203
|
+
|
|
204
|
+
const large: number[] = [];
|
|
205
|
+
const counts = new Uint32Array(cols * rows + 1);
|
|
206
|
+
const spans: [number, number, number, number][] = [];
|
|
207
|
+
for (const i of gridded) {
|
|
208
|
+
const span: [number, number, number, number] = [
|
|
209
|
+
cellX(boxes[i * 5]!),
|
|
210
|
+
cellY(boxes[i * 5 + 1]!),
|
|
211
|
+
cellX(boxes[i * 5 + 2]!),
|
|
212
|
+
cellY(boxes[i * 5 + 3]!),
|
|
213
|
+
];
|
|
214
|
+
spans.push(span);
|
|
215
|
+
if ((span[2] - span[0] + 1) * (span[3] - span[1] + 1) > LARGE_CELLS) {
|
|
216
|
+
large.push(i);
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
for (let cy = span[1]; cy <= span[3]; cy++) for (let cx = span[0]; cx <= span[2]; cx++) counts[cy * cols + cx + 1]!++;
|
|
220
|
+
}
|
|
221
|
+
for (let c = 0; c < cols * rows; c++) counts[c + 1]! += counts[c]!;
|
|
222
|
+
const starts = counts;
|
|
223
|
+
const fill = starts.slice(0, cols * rows);
|
|
224
|
+
const entries = new Uint32Array(starts[cols * rows]!);
|
|
225
|
+
gridded.forEach((i, k) => {
|
|
226
|
+
const span = spans[k]!;
|
|
227
|
+
if ((span[2] - span[0] + 1) * (span[3] - span[1] + 1) > LARGE_CELLS) return;
|
|
228
|
+
for (let cy = span[1]; cy <= span[3]; cy++) for (let cx = span[0]; cx <= span[2]; cx++) entries[fill[cy * cols + cx]!++] = i;
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
const index: MeasureIndex = {
|
|
232
|
+
boxes,
|
|
233
|
+
maxPad,
|
|
234
|
+
cell,
|
|
235
|
+
x0,
|
|
236
|
+
y0,
|
|
237
|
+
cols,
|
|
238
|
+
rows,
|
|
239
|
+
starts,
|
|
240
|
+
entries,
|
|
241
|
+
large: Uint32Array.from(large),
|
|
242
|
+
seen: new Uint32Array(n),
|
|
243
|
+
query: 0,
|
|
244
|
+
};
|
|
245
|
+
indexCache.set(primitives, index);
|
|
246
|
+
return index;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** A primitive's box in image pixels, and how far past it its mark reaches in screen pixels. */
|
|
250
|
+
function boxOf(primitive: MeasurePrimitive): [number, number, number, number, number] {
|
|
251
|
+
let minX = Infinity;
|
|
252
|
+
let minY = Infinity;
|
|
253
|
+
let maxX = -Infinity;
|
|
254
|
+
let maxY = -Infinity;
|
|
255
|
+
let pad = 0;
|
|
256
|
+
const add = (x: number, y: number) => {
|
|
257
|
+
// A NaN poisons the box, so that a primitive with one is never picked.
|
|
258
|
+
if (Number.isNaN(x) || Number.isNaN(y)) {
|
|
259
|
+
minX = Number.NaN;
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (x < minX) minX = x;
|
|
263
|
+
if (x > maxX) maxX = x;
|
|
264
|
+
if (y < minY) minY = y;
|
|
265
|
+
if (y > maxY) maxY = y;
|
|
266
|
+
};
|
|
267
|
+
switch (primitive.kind) {
|
|
268
|
+
case "point":
|
|
269
|
+
add(primitive.x, primitive.y);
|
|
270
|
+
pad = primitive.cross ? MEASURE_CROSS_PX : (primitive.radius ?? MEASURE_POINT_RADIUS_PX);
|
|
271
|
+
break;
|
|
272
|
+
case "segment":
|
|
273
|
+
add(primitive.x1, primitive.y1);
|
|
274
|
+
add(primitive.x2, primitive.y2);
|
|
275
|
+
break;
|
|
276
|
+
case "circle":
|
|
277
|
+
case "arc":
|
|
278
|
+
add(primitive.cx - primitive.r, primitive.cy - primitive.r);
|
|
279
|
+
add(primitive.cx + primitive.r, primitive.cy + primitive.r);
|
|
280
|
+
break;
|
|
281
|
+
case "caliper": {
|
|
282
|
+
for (const corner of caliperCorners(primitive.cx, primitive.cy, primitive.width, primitive.height, primitive.angle))
|
|
283
|
+
add(corner.x, corner.y);
|
|
284
|
+
const tip = caliperArrow(primitive.cx, primitive.cy, primitive.width, primitive.angle).to;
|
|
285
|
+
add(tip.x, tip.y);
|
|
286
|
+
// The head's back corners reach 1.6 × its size behind the tip and 1 × to the side.
|
|
287
|
+
pad = MEASURE_ARROW_HEAD_PX * 2;
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
case "dimension": {
|
|
291
|
+
const g = dimensionGeometry(primitive.x1, primitive.y1, primitive.x2, primitive.y2, primitive.offset ?? MEASURE_DIMENSION_OFFSET);
|
|
292
|
+
for (const [a, b] of [g.extensionLine1, g.extensionLine2]) {
|
|
293
|
+
add(a.x, a.y);
|
|
294
|
+
add(b.x, b.y);
|
|
295
|
+
}
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
case "segments": {
|
|
299
|
+
// Only whole, finite segments are drawn; a skipped one does not poison the rest.
|
|
300
|
+
const s = primitive.points;
|
|
301
|
+
for (let k = 0; k + 3 < s.length; k += 4) {
|
|
302
|
+
if (!(Number.isFinite(s[k]) && Number.isFinite(s[k + 1]) && Number.isFinite(s[k + 2]) && Number.isFinite(s[k + 3]))) continue;
|
|
303
|
+
add(s[k]!, s[k + 1]!);
|
|
304
|
+
add(s[k + 2]!, s[k + 3]!);
|
|
305
|
+
}
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
case "polyline": {
|
|
309
|
+
const s = primitive.points;
|
|
310
|
+
for (let k = 0; k + 1 < s.length; k += 2) add(s[k]!, s[k + 1]!);
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return [minX, minY, maxX, maxY, pad];
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* The primitive nearest to `point` within `radius`, among those with an `id`.
|
|
319
|
+
*
|
|
320
|
+
* The primitives are gridded by bounding box once per `primitives` array, and the grid is
|
|
321
|
+
* kept while that array lives, so a hover over thousands of primitives measures only those
|
|
322
|
+
* near the pointer. Pass a new array when the primitives change (as React state does): one
|
|
323
|
+
* edited in place keeps its old grid.
|
|
324
|
+
*
|
|
325
|
+
* @param primitives - The primitives, bottom to top, as `MeasureOverlay` draws them.
|
|
326
|
+
* @param point - The point, in image coordinates.
|
|
327
|
+
* @param radius - The search radius, in image pixels.
|
|
328
|
+
* @param strokeScale - The image-to-screen scale the overlay is drawn at.
|
|
329
|
+
* @returns The nearest primitive's id and distance, or `null`. A tie goes to the later
|
|
330
|
+
* primitive, which is drawn on top. A primitive with a NaN coordinate is never picked,
|
|
331
|
+
* except a `segments` one, whose bad segments are skipped as they are when drawn.
|
|
332
|
+
*/
|
|
333
|
+
export function nearestMeasurePrimitive(
|
|
334
|
+
primitives: readonly MeasurePrimitive[],
|
|
335
|
+
point: Point,
|
|
336
|
+
radius: number,
|
|
337
|
+
strokeScale: number,
|
|
338
|
+
): { id: string; distance: number } | null {
|
|
339
|
+
const index = indexOf(primitives);
|
|
340
|
+
const { boxes, cell, x0, y0, cols, rows, starts, entries, large, seen } = index;
|
|
341
|
+
const screen = strokeWidthFor(strokeScale, 1);
|
|
342
|
+
index.query = (index.query + 1) >>> 0 || 1;
|
|
343
|
+
const query = index.query;
|
|
344
|
+
let best: { id: string; distance: number } | null = null;
|
|
345
|
+
let bestIndex = -1;
|
|
346
|
+
|
|
347
|
+
const consider = (i: number) => {
|
|
348
|
+
if (seen[i] === query) return;
|
|
349
|
+
seen[i] = query;
|
|
350
|
+
const reach = radius + boxes[i * 5 + 4]! * screen;
|
|
351
|
+
if (
|
|
352
|
+
!(
|
|
353
|
+
point.x >= boxes[i * 5]! - reach &&
|
|
354
|
+
point.x <= boxes[i * 5 + 2]! + reach &&
|
|
355
|
+
point.y >= boxes[i * 5 + 1]! - reach &&
|
|
356
|
+
point.y <= boxes[i * 5 + 3]! + reach
|
|
357
|
+
)
|
|
358
|
+
) {
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const primitive = primitives[i]!;
|
|
362
|
+
const distance = measurePrimitiveDistance(primitive, point, strokeScale);
|
|
363
|
+
if (distance > radius) return;
|
|
364
|
+
if (best === null || distance < best.distance || (distance === best.distance && i > bestIndex)) {
|
|
365
|
+
best = { id: primitive.id!, distance };
|
|
366
|
+
bestIndex = i;
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
const reach = radius + index.maxPad * screen;
|
|
371
|
+
const cx0 = Math.max(0, Math.floor((point.x - reach - x0) / cell));
|
|
372
|
+
const cx1 = Math.min(cols - 1, Math.floor((point.x + reach - x0) / cell));
|
|
373
|
+
const cy0 = Math.max(0, Math.floor((point.y - reach - y0) / cell));
|
|
374
|
+
const cy1 = Math.min(rows - 1, Math.floor((point.y + reach - y0) / cell));
|
|
375
|
+
for (let cy = cy0; cy <= cy1; cy++) {
|
|
376
|
+
for (let cx = cx0; cx <= cx1; cx++) {
|
|
377
|
+
const c = cy * cols + cx;
|
|
378
|
+
for (let k = starts[c]!; k < starts[c + 1]!; k++) consider(entries[k]!);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
for (let k = 0; k < large.length; k++) consider(large[k]!);
|
|
382
|
+
return best;
|
|
383
|
+
}
|
|
@@ -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.
|
|
@@ -123,8 +122,18 @@ export function buildPointIndex(xy: ArrayLike<number>, options: PointIndexOption
|
|
|
123
122
|
}
|
|
124
123
|
if (live === 0) {
|
|
125
124
|
return {
|
|
126
|
-
count,
|
|
127
|
-
|
|
125
|
+
count,
|
|
126
|
+
xy,
|
|
127
|
+
ids: ids,
|
|
128
|
+
radii,
|
|
129
|
+
maxRadius,
|
|
130
|
+
cell: 1,
|
|
131
|
+
x0: 0,
|
|
132
|
+
y0: 0,
|
|
133
|
+
cols: 1,
|
|
134
|
+
rows: 1,
|
|
135
|
+
starts: new Uint32Array(2),
|
|
136
|
+
items: new Uint32Array(0),
|
|
128
137
|
};
|
|
129
138
|
}
|
|
130
139
|
|
|
@@ -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
|
}
|