@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
|
@@ -26,7 +26,7 @@ import {
|
|
|
26
26
|
SHAPE_ROTATE_RADIUS_PX,
|
|
27
27
|
type RotatedShape,
|
|
28
28
|
} from "./shapeEdit";
|
|
29
|
-
import { POINTER_RADIUS_PX, TOUCH_RADIUS_PX, hitRadiusPx } from "./stage/gesture";
|
|
29
|
+
import { CLICK_SLOP, POINTER_RADIUS_PX, TOUCH_RADIUS_PX, hitRadiusPx } from "./stage/gesture";
|
|
30
30
|
import { useStage } from "./stage/ImageStage";
|
|
31
31
|
import { useCoarsePointer } from "./stage/useCoarsePointer";
|
|
32
32
|
import { useScreenPx } from "./stage/useScreenPx";
|
|
@@ -34,7 +34,7 @@ import { imageViewBox } from "./stage/view";
|
|
|
34
34
|
|
|
35
35
|
/** Outline width, in screen pixels. */
|
|
36
36
|
const STROKE_PX = 1.5;
|
|
37
|
-
/** The halo under the outline
|
|
37
|
+
/** The dark halo under the outline, in screen pixels on each side. */
|
|
38
38
|
const HALO_PX = 1;
|
|
39
39
|
/** Shift while rotating rounds to this step, and `[` / `]` turn by 1° (15° with Shift). */
|
|
40
40
|
const SNAP = Math.PI / 12;
|
|
@@ -67,11 +67,13 @@ export interface ShapeEditorProps {
|
|
|
67
67
|
stroke?: string | undefined;
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
type Drag = (
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
type Drag = ({ kind: "move"; from: Point } | { kind: "resize"; handle: RoiHandle } | { kind: "rotate" }) & {
|
|
71
|
+
start: RotatedShape;
|
|
72
|
+
/** Where the press landed, in client coordinates: a move counts once it is past the click slop. */
|
|
73
|
+
client: Point;
|
|
74
|
+
moved: boolean;
|
|
75
|
+
pointerId: number;
|
|
76
|
+
};
|
|
75
77
|
|
|
76
78
|
/**
|
|
77
79
|
* An editable rotated rectangle or ellipse inside an `ImageStage`.
|
|
@@ -87,6 +89,8 @@ type Drag = (
|
|
|
87
89
|
* then the interior or outline band (move), then nothing. A small shape's interior does not
|
|
88
90
|
* steal a press meant for a handle, and a press that hits none of them is not claimed, so
|
|
89
91
|
* it reaches whatever is below, or the stage.
|
|
92
|
+
* - **Click slop.** A press becomes an edit only once the pointer has moved 3 screen pixels
|
|
93
|
+
* from where it landed, so a jittery click changes nothing and commits nothing.
|
|
90
94
|
* - **Touch.** A press that grabs the shape is claimed; a second finger landing during an edit
|
|
91
95
|
* cancels it (the shape reverts, nothing is committed) and is left to the stage.
|
|
92
96
|
* - **Rotation.** The handle past the top side turns the shape about its centre; hold Shift to
|
|
@@ -163,7 +167,7 @@ export function ShapeEditor({
|
|
|
163
167
|
} catch {
|
|
164
168
|
// The pointer ended before this handler ran; the edit still follows the events the editor receives.
|
|
165
169
|
}
|
|
166
|
-
const base = { start: value, moved: false, pointerId: event.pointerId };
|
|
170
|
+
const base = { start: value, client: { x: event.clientX, y: event.clientY }, moved: false, pointerId: event.pointerId };
|
|
167
171
|
dragRef.current =
|
|
168
172
|
decision.kind === "move"
|
|
169
173
|
? { ...base, kind: "move", from: at }
|
|
@@ -185,6 +189,8 @@ export function ShapeEditor({
|
|
|
185
189
|
const drag = dragRef.current;
|
|
186
190
|
if (!drag || drag.pointerId !== event.pointerId) return;
|
|
187
191
|
event.stopPropagation();
|
|
192
|
+
// A jittery click is not an edit: nothing changes until the pointer leaves the click slop.
|
|
193
|
+
if (!drag.moved && Math.hypot(event.clientX - drag.client.x, event.clientY - drag.client.y) <= CLICK_SLOP) return;
|
|
188
194
|
const p = stage.toImage({ x: event.clientX, y: event.clientY });
|
|
189
195
|
drag.moved = true;
|
|
190
196
|
if (drag.kind === "move") report(moveShape(drag.start, p.x - drag.from.x, p.y - drag.from.y));
|
|
@@ -255,7 +261,9 @@ export function ShapeEditor({
|
|
|
255
261
|
data-editable={editable ? "" : undefined}
|
|
256
262
|
>
|
|
257
263
|
{value && <Outline kind={kind} shape={value} fill="none" stroke={HALO} strokeWidth={px(STROKE_PX + 2 * HALO_PX)} />}
|
|
258
|
-
{value &&
|
|
264
|
+
{value && (
|
|
265
|
+
<Outline kind={kind} shape={value} data-shape={kind} fill={stroke} fillOpacity={0.12} stroke={stroke} strokeWidth={px(STROKE_PX)} />
|
|
266
|
+
)}
|
|
259
267
|
{kind === "ellipse" && value && (
|
|
260
268
|
<rect
|
|
261
269
|
x={value.cx - value.width / 2}
|
|
@@ -354,7 +362,14 @@ type OutlineProps = Omit<SVGProps<SVGElement>, "ref"> & Record<`data-${string}`,
|
|
|
354
362
|
function Outline({ kind, shape, ...rest }: OutlineProps & { kind: "rect" | "ellipse"; shape: RotatedShape }) {
|
|
355
363
|
const transform = turned(shape);
|
|
356
364
|
return kind === "rect" ? (
|
|
357
|
-
<rect
|
|
365
|
+
<rect
|
|
366
|
+
x={shape.cx - shape.width / 2}
|
|
367
|
+
y={shape.cy - shape.height / 2}
|
|
368
|
+
width={shape.width}
|
|
369
|
+
height={shape.height}
|
|
370
|
+
transform={transform}
|
|
371
|
+
{...rest}
|
|
372
|
+
/>
|
|
358
373
|
) : (
|
|
359
374
|
<ellipse cx={shape.cx} cy={shape.cy} rx={shape.width / 2} ry={shape.height / 2} transform={transform} {...rest} />
|
|
360
375
|
);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* How `AreaSet` groups its regions into batched paths: by appearance (state, role, colours),
|
|
3
3
|
* either one batch per appearance or consecutive runs in item order. Pure, so the keys are
|
|
4
|
-
* unit-tested without a DOM
|
|
4
|
+
* unit-tested without a DOM.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { areaPath, type Area, type AreaId } from "./areaIndex";
|
|
@@ -81,7 +81,17 @@ export function batchAreas(items: readonly BatchableArea[], options: AreaBatchOp
|
|
|
81
81
|
const separate = fillRule === "evenodd" || keepSelected;
|
|
82
82
|
const byKey = new Map<string, AreaBatch>();
|
|
83
83
|
const runs: AreaBatch[] = [];
|
|
84
|
-
const picked: AreaBatch = {
|
|
84
|
+
const picked: AreaBatch = {
|
|
85
|
+
key: "selected",
|
|
86
|
+
id: "selected",
|
|
87
|
+
role: "selection",
|
|
88
|
+
stroke: undefined,
|
|
89
|
+
fill: undefined,
|
|
90
|
+
dim: false,
|
|
91
|
+
indices: [],
|
|
92
|
+
d: "",
|
|
93
|
+
fills: fillRule === "evenodd" && !keepSelected ? [] : null,
|
|
94
|
+
};
|
|
85
95
|
|
|
86
96
|
items.forEach((item, i) => {
|
|
87
97
|
const isSelected = selected.has(item.id);
|
|
@@ -98,7 +108,17 @@ export function batchAreas(items: readonly BatchableArea[], options: AreaBatchOp
|
|
|
98
108
|
const key = areaBatchKey(dim, itemRole, itemStroke, item.fill);
|
|
99
109
|
let batch = paintOrder === "items" ? runs[runs.length - 1] : byKey.get(key);
|
|
100
110
|
if (!batch || batch.key !== key) {
|
|
101
|
-
batch = {
|
|
111
|
+
batch = {
|
|
112
|
+
key,
|
|
113
|
+
id: paintOrder === "items" ? `${key}#${runs.length}` : key,
|
|
114
|
+
role: itemRole,
|
|
115
|
+
stroke: itemStroke,
|
|
116
|
+
fill: item.fill,
|
|
117
|
+
dim,
|
|
118
|
+
indices: [],
|
|
119
|
+
d: "",
|
|
120
|
+
fills: separate ? [] : null,
|
|
121
|
+
};
|
|
102
122
|
byKey.set(key, batch);
|
|
103
123
|
runs.push(batch);
|
|
104
124
|
}
|
|
@@ -232,7 +232,8 @@ export function buildAreaIndex(items: readonly Area[], cell?: number): AreaIndex
|
|
|
232
232
|
let total = 0;
|
|
233
233
|
for (let a = 0; a < count; a++) {
|
|
234
234
|
if (Number.isNaN(bounds[4 * a]!)) continue;
|
|
235
|
-
total +=
|
|
235
|
+
total +=
|
|
236
|
+
(Math.floor((bounds[4 * a + 2]! - bounds[4 * a]!) / s) + 1) * (Math.floor((bounds[4 * a + 3]! - bounds[4 * a + 1]!) / s) + 1);
|
|
236
237
|
}
|
|
237
238
|
return total;
|
|
238
239
|
};
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A contour as a path measured along its length: where a distance along it lands, the
|
|
3
|
+
* nearest place on it to a point, a stretch of it, and the two edits a brush makes (push a
|
|
4
|
+
* part of it, erase a stretch of it).
|
|
5
|
+
*
|
|
6
|
+
* An **arc length** `s` is a distance along the path from its first vertex, in image pixels.
|
|
7
|
+
* An open path runs from the first vertex to the last; a closed one goes on from the last
|
|
8
|
+
* vertex back to the first, so its length includes that closing segment. Every function
|
|
9
|
+
* takes `closed` (default `false`, an open path) and treats the points the same way.
|
|
10
|
+
*
|
|
11
|
+
* Kept pure, so the laws that matter are property-tested: arc length only grows, every vertex
|
|
12
|
+
* sits at its own arc length, a projection is never farther than the nearest vertex, and an
|
|
13
|
+
* erase leaves exactly the length it did not remove.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { Point } from "./measureGeometry";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The cumulative arc length at each vertex.
|
|
20
|
+
*
|
|
21
|
+
* @param points - The path's vertices.
|
|
22
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`.
|
|
23
|
+
* @returns `s[i]`, the length of the path from the first vertex to vertex `i`, so `s[0]` is 0.
|
|
24
|
+
* A closed path has one more entry: its whole length, back at the first vertex. The last
|
|
25
|
+
* entry is always the path's length. An empty path gives `[]`.
|
|
26
|
+
*/
|
|
27
|
+
export function arcLengths(points: readonly Point[], closed = false): number[] {
|
|
28
|
+
if (points.length === 0) return [];
|
|
29
|
+
const out = [0];
|
|
30
|
+
let total = 0;
|
|
31
|
+
const ring = ringOf(points, closed);
|
|
32
|
+
for (let i = 1; i < ring.length; i++) {
|
|
33
|
+
total += distance(ring[i - 1]!, ring[i]!);
|
|
34
|
+
out.push(total);
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The point at arc length `s`.
|
|
41
|
+
*
|
|
42
|
+
* @param points - The path's vertices.
|
|
43
|
+
* @param s - The distance along the path, in image pixels. On an open path it is clamped to
|
|
44
|
+
* `[0, length]`; on a closed path it wraps around.
|
|
45
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`.
|
|
46
|
+
* @returns The point. An empty path gives `NaN` coordinates.
|
|
47
|
+
*/
|
|
48
|
+
export function pointAtArc(points: readonly Point[], s: number, closed = false): Point {
|
|
49
|
+
const ring = ringOf(points, closed);
|
|
50
|
+
const cum = arcLengths(points, closed);
|
|
51
|
+
return pointOn(ring, cum, arcPosition(cum, s, closed));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The nearest place on the path to a point.
|
|
56
|
+
*
|
|
57
|
+
* @param points - The path's vertices.
|
|
58
|
+
* @param p - The point, in image coordinates.
|
|
59
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`.
|
|
60
|
+
* @returns `s`, the arc length of the nearest place (the first one, on a tie); `point`, the
|
|
61
|
+
* place itself; and `distance`, how far `p` is from it. An empty path gives `s` 0, a `NaN`
|
|
62
|
+
* point and an infinite distance.
|
|
63
|
+
*/
|
|
64
|
+
export function projectToArc(points: readonly Point[], p: Point, closed = false): { s: number; point: Point; distance: number } {
|
|
65
|
+
const ring = ringOf(points, closed);
|
|
66
|
+
const cum = arcLengths(points, closed);
|
|
67
|
+
if (ring.length === 0) return { s: 0, point: { x: NaN, y: NaN }, distance: Infinity };
|
|
68
|
+
let best = { s: 0, point: { x: ring[0]!.x, y: ring[0]!.y }, distance: distance(ring[0]!, p) };
|
|
69
|
+
for (let i = 0; i + 1 < ring.length; i++) {
|
|
70
|
+
const a = ring[i]!;
|
|
71
|
+
const b = ring[i + 1]!;
|
|
72
|
+
const t = segmentParameter(a, b, p);
|
|
73
|
+
const on = lerp(a, b, t);
|
|
74
|
+
const d = distance(on, p);
|
|
75
|
+
if (d < best.distance) best = { s: cum[i]! + t * (cum[i + 1]! - cum[i]!), point: on, distance: d };
|
|
76
|
+
}
|
|
77
|
+
return best;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The stretch of the path between two arc lengths, as a polyline: the place at `s0`, every
|
|
82
|
+
* vertex strictly between, and the place at `s1`.
|
|
83
|
+
*
|
|
84
|
+
* @param points - The path's vertices.
|
|
85
|
+
* @param s0 - Where the stretch starts.
|
|
86
|
+
* @param s1 - Where it ends.
|
|
87
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`. On an open
|
|
88
|
+
* path both ends are clamped to it and the stretch always runs forward (`s0` and `s1` are
|
|
89
|
+
* swapped when `s0 > s1`). On a closed path an end outside `[0, length]` wraps, and with
|
|
90
|
+
* `s0 > s1` the stretch runs forward past the first vertex and on to `s1`.
|
|
91
|
+
* @returns The stretch, at least its two ends (the same point twice for an empty stretch).
|
|
92
|
+
* An empty path gives `[]`.
|
|
93
|
+
*/
|
|
94
|
+
export function subPath(points: readonly Point[], s0: number, s1: number, closed = false): Point[] {
|
|
95
|
+
if (points.length === 0) return [];
|
|
96
|
+
const ring = ringOf(points, closed);
|
|
97
|
+
const cum = arcLengths(points, closed);
|
|
98
|
+
const total = cum[cum.length - 1]!;
|
|
99
|
+
let a = arcPosition(cum, s0, closed);
|
|
100
|
+
let b = arcPosition(cum, s1, closed);
|
|
101
|
+
if (!closed && a > b) [a, b] = [b, a];
|
|
102
|
+
if (a <= b) return stretch(ring, cum, a, b);
|
|
103
|
+
return [...stretch(ring, cum, a, total), ...stretch(ring, cum, 0, b).slice(1)];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The unit normal at arc length `s`: the tangent of the segment there, turned a quarter turn
|
|
108
|
+
* by `(tx, ty) → (-ty, tx)`. With image `y` pointing down that turn is **clockwise on
|
|
109
|
+
* screen**: on a path running left to right the normal points down, and on a closed contour
|
|
110
|
+
* whose vertices run clockwise on screen it points inward.
|
|
111
|
+
*
|
|
112
|
+
* @param points - The path's vertices.
|
|
113
|
+
* @param s - The distance along the path, clamped or wrapped as in `pointAtArc`. At a vertex
|
|
114
|
+
* the segment that starts there is used (at the end of an open path, the last one).
|
|
115
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`.
|
|
116
|
+
* @returns A unit vector, or `{ x: 0, y: 0 }` for a path with no length.
|
|
117
|
+
*/
|
|
118
|
+
export function normalAtArc(points: readonly Point[], s: number, closed = false): Point {
|
|
119
|
+
const ring = ringOf(points, closed);
|
|
120
|
+
const cum = arcLengths(points, closed);
|
|
121
|
+
if (ring.length < 2) return { x: 0, y: 0 };
|
|
122
|
+
const start = segmentAt(cum, arcPosition(cum, s, closed));
|
|
123
|
+
// The segment there, or the nearest one with a length: forward first, then back.
|
|
124
|
+
const order = [...range(start, ring.length - 1), ...range(0, start).reverse()];
|
|
125
|
+
for (const i of order) {
|
|
126
|
+
const a = ring[i]!;
|
|
127
|
+
const b = ring[i + 1]!;
|
|
128
|
+
const length = distance(a, b);
|
|
129
|
+
if (length > 0) return { x: -(b.y - a.y) / length, y: (b.x - a.x) / length };
|
|
130
|
+
}
|
|
131
|
+
return { x: 0, y: 0 };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Push part of a path with a soft round brush: every vertex within `radius` of `centre`
|
|
136
|
+
* moves by `delta` scaled by a falloff of `0.5 · (1 + cos(π · d / radius))`, `d` being its
|
|
137
|
+
* distance from `centre` — the whole `delta` at the centre, nothing at the rim, smooth in
|
|
138
|
+
* between. Each segment that passes within `radius` of `centre` is first divided so its
|
|
139
|
+
* vertices are at most `radius / 4` apart, so the brush bends the path rather than dragging
|
|
140
|
+
* a long segment's two ends.
|
|
141
|
+
*
|
|
142
|
+
* @param points - The path's vertices.
|
|
143
|
+
* @param centre - The brush's centre, in image coordinates.
|
|
144
|
+
* @param delta - How far its centre pushes, in image pixels.
|
|
145
|
+
* @param radius - The brush's radius, in image pixels. Zero or less leaves the path as it is.
|
|
146
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`; a closed
|
|
147
|
+
* path's closing segment is divided like the others, its new vertices appended at the end.
|
|
148
|
+
* @returns A new list of vertices.
|
|
149
|
+
*/
|
|
150
|
+
export function deformContour(points: readonly Point[], centre: Point, delta: Point, radius: number, closed = false): Point[] {
|
|
151
|
+
if (!(radius > 0)) return points.map((p) => ({ x: p.x, y: p.y }));
|
|
152
|
+
const step = radius / 4;
|
|
153
|
+
const dense: Point[] = [];
|
|
154
|
+
const n = points.length;
|
|
155
|
+
const segments = closed ? n : n - 1;
|
|
156
|
+
for (let i = 0; i < n; i++) {
|
|
157
|
+
const a = points[i]!;
|
|
158
|
+
dense.push(a);
|
|
159
|
+
if (i >= segments) continue;
|
|
160
|
+
const b = points[(i + 1) % n]!;
|
|
161
|
+
if (distance(lerp(a, b, segmentParameter(a, b, centre)), centre) >= radius) continue;
|
|
162
|
+
const pieces = Math.ceil(distance(a, b) / step);
|
|
163
|
+
for (let k = 1; k < pieces; k++) dense.push(lerp(a, b, k / pieces));
|
|
164
|
+
}
|
|
165
|
+
return dense.map((p) => {
|
|
166
|
+
const d = distance(p, centre);
|
|
167
|
+
if (d >= radius) return { x: p.x, y: p.y };
|
|
168
|
+
const weight = 0.5 * (1 + Math.cos((Math.PI * d) / radius));
|
|
169
|
+
return { x: p.x + delta.x * weight, y: p.y + delta.y * weight };
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The pieces of a path left after erasing the stretch between two arc lengths.
|
|
175
|
+
*
|
|
176
|
+
* @param points - The path's vertices.
|
|
177
|
+
* @param s0 - Where the erased stretch starts.
|
|
178
|
+
* @param s1 - Where it ends.
|
|
179
|
+
* @param closed - Whether the last vertex joins the first. Defaults to `false`. On an open
|
|
180
|
+
* path the ends are clamped and ordered as in `subPath`, and up to two pieces remain: before
|
|
181
|
+
* the stretch and after it. On a closed path the stretch runs forward from `s0` to `s1`
|
|
182
|
+
* (past the first vertex when `s0 > s1`), and what remains is **one open piece**, from `s1`
|
|
183
|
+
* on around to `s0`; erasing an empty stretch (`s0` equal to `s1`) cuts the contour open there.
|
|
184
|
+
* @returns The pieces, each an open polyline of at least two points. A piece with no length
|
|
185
|
+
* (the erased stretch reached an open path's end) is left out.
|
|
186
|
+
*/
|
|
187
|
+
export function eraseArc(points: readonly Point[], s0: number, s1: number, closed = false): Point[][] {
|
|
188
|
+
if (points.length === 0) return [];
|
|
189
|
+
const ring = ringOf(points, closed);
|
|
190
|
+
const cum = arcLengths(points, closed);
|
|
191
|
+
const total = cum[cum.length - 1]!;
|
|
192
|
+
let a = arcPosition(cum, s0, closed);
|
|
193
|
+
let b = arcPosition(cum, s1, closed);
|
|
194
|
+
if (closed) {
|
|
195
|
+
if (a === b) return [[...stretch(ring, cum, a, total), ...stretch(ring, cum, 0, a).slice(1)]];
|
|
196
|
+
return [subPath(points, b, a, true)];
|
|
197
|
+
}
|
|
198
|
+
if (a > b) [a, b] = [b, a];
|
|
199
|
+
const pieces: Point[][] = [];
|
|
200
|
+
if (a > 0) pieces.push(stretch(ring, cum, 0, a));
|
|
201
|
+
if (b < total) pieces.push(stretch(ring, cum, b, total));
|
|
202
|
+
return pieces;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** The vertices walked in order: a closed path ends back at its first vertex. */
|
|
206
|
+
function ringOf(points: readonly Point[], closed: boolean): readonly Point[] {
|
|
207
|
+
return closed && points.length > 0 ? [...points, points[0]!] : points;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** `s` as a position on the path: clamped to an open one, wrapped around a closed one. */
|
|
211
|
+
function arcPosition(cum: readonly number[], s: number, closed: boolean): number {
|
|
212
|
+
const total = cum[cum.length - 1] ?? 0;
|
|
213
|
+
if (!(total > 0)) return 0;
|
|
214
|
+
if (closed) return s >= 0 && s <= total ? s : ((s % total) + total) % total;
|
|
215
|
+
return Math.min(total, Math.max(0, s));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** The segment `s` lies on: the last `i` with `cum[i] <= s`, held to the last segment. */
|
|
219
|
+
function segmentAt(cum: readonly number[], s: number): number {
|
|
220
|
+
let low = 0;
|
|
221
|
+
let high = Math.max(0, cum.length - 2);
|
|
222
|
+
while (low < high) {
|
|
223
|
+
const mid = (low + high + 1) >> 1;
|
|
224
|
+
if (cum[mid]! <= s) low = mid;
|
|
225
|
+
else high = mid - 1;
|
|
226
|
+
}
|
|
227
|
+
return low;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** The point at position `s` (already clamped or wrapped) of a walked path. */
|
|
231
|
+
function pointOn(ring: readonly Point[], cum: readonly number[], s: number): Point {
|
|
232
|
+
if (ring.length === 0) return { x: NaN, y: NaN };
|
|
233
|
+
if (ring.length === 1) return { x: ring[0]!.x, y: ring[0]!.y };
|
|
234
|
+
const i = segmentAt(cum, s);
|
|
235
|
+
const length = cum[i + 1]! - cum[i]!;
|
|
236
|
+
return lerp(ring[i]!, ring[i + 1]!, length > 0 ? Math.min(1, Math.max(0, (s - cum[i]!) / length)) : 0);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** The place at `a`, every vertex strictly between, and the place at `b`; `a <= b`. */
|
|
240
|
+
function stretch(ring: readonly Point[], cum: readonly number[], a: number, b: number): Point[] {
|
|
241
|
+
const out = [pointOn(ring, cum, a)];
|
|
242
|
+
for (let i = 0; i < ring.length; i++) if (cum[i]! > a && cum[i]! < b) out.push({ x: ring[i]!.x, y: ring[i]!.y });
|
|
243
|
+
out.push(pointOn(ring, cum, b));
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** Where on segment `ab` (as `t` in `[0, 1]`) the point nearest `p` is. */
|
|
248
|
+
function segmentParameter(a: Point, b: Point, p: Point): number {
|
|
249
|
+
const dx = b.x - a.x;
|
|
250
|
+
const dy = b.y - a.y;
|
|
251
|
+
const length2 = dx * dx + dy * dy;
|
|
252
|
+
if (length2 === 0) return 0;
|
|
253
|
+
return Math.min(1, Math.max(0, ((p.x - a.x) * dx + (p.y - a.y) * dy) / length2));
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function lerp(a: Point, b: Point, t: number): Point {
|
|
257
|
+
return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function distance(a: Point, b: Point): number {
|
|
261
|
+
return Math.hypot(b.x - a.x, b.y - a.y);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function range(from: number, to: number): number[] {
|
|
265
|
+
const out: number[] = [];
|
|
266
|
+
for (let i = from; i < to; i++) out.push(i);
|
|
267
|
+
return out;
|
|
268
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The arithmetic of editing a datum: a frame on the image, given as an origin and the angle of
|
|
3
|
+
* its i axis. Kept pure, so the cases that bite (a snapped angle, a grid, a press between the
|
|
4
|
+
* origin and the arm) are tested without a pointer.
|
|
5
|
+
*
|
|
6
|
+
* Everything is in the stage's image coordinates. **Angles are radians, positive clockwise on
|
|
7
|
+
* screen** (image `y` points down) from the image's `+x`, as `RotatedShape.rotation` is. The j
|
|
8
|
+
* axis is a quarter turn clockwise from the i axis, along `angle + π/2`.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Point } from "./measureGeometry";
|
|
12
|
+
import { closestOnSegment } from "./polylineIndex";
|
|
13
|
+
import { normalizeAngle, snapAngle } from "./shapeEdit";
|
|
14
|
+
import type { Rect } from "./stage/view";
|
|
15
|
+
|
|
16
|
+
/** A frame on the image: where it starts, and which way its i axis points. */
|
|
17
|
+
export interface Datum {
|
|
18
|
+
/** The origin, in image coordinates. */
|
|
19
|
+
origin: Point;
|
|
20
|
+
/** The i axis's direction: radians, clockwise on screen from the image's `+x`. */
|
|
21
|
+
angle: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The origin ring's radius, in screen pixels. */
|
|
25
|
+
export const DATUM_RING_PX = 7;
|
|
26
|
+
/** The i axis's length when nothing else is given, in screen pixels; the j axis is half of it. */
|
|
27
|
+
export const DATUM_ARM_PX = 40;
|
|
28
|
+
/** The rotation handle's radius at the end of the i axis, in screen pixels. */
|
|
29
|
+
export const DATUM_HANDLE_PX = 5;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The ends of a datum's axes.
|
|
33
|
+
*
|
|
34
|
+
* @param d - The datum.
|
|
35
|
+
* @param armLength - The i axis's length, in image pixels; the j axis is half of it. For a
|
|
36
|
+
* screen-constant glyph pass `useScreenPx()(40)`.
|
|
37
|
+
* @returns The end of the i axis and of the j axis, in image coordinates.
|
|
38
|
+
*/
|
|
39
|
+
export function datumAxes(d: Datum, armLength: number): { i: Point; j: Point } {
|
|
40
|
+
const c = Math.cos(d.angle);
|
|
41
|
+
const s = Math.sin(d.angle);
|
|
42
|
+
const half = armLength / 2;
|
|
43
|
+
// The j axis is the i axis turned a quarter clockwise: (cos(a + π/2), sin(a + π/2)) = (−sin a, cos a).
|
|
44
|
+
return {
|
|
45
|
+
i: { x: d.origin.x + armLength * c, y: d.origin.y + armLength * s },
|
|
46
|
+
j: { x: d.origin.x - half * s, y: d.origin.y + half * c },
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** How `moveDatum` places the origin. */
|
|
51
|
+
export interface MoveDatumOptions {
|
|
52
|
+
/** Round the origin to a grid of this many image pixels; 0 or omitted for none. */
|
|
53
|
+
grid?: number | undefined;
|
|
54
|
+
/** Keep the origin inside this rectangle (edges included), on the last grid line inside it when there is a grid. */
|
|
55
|
+
bounds?: Rect | undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Move a datum's origin to `to`, keeping its angle.
|
|
60
|
+
*
|
|
61
|
+
* @param d - The datum.
|
|
62
|
+
* @param to - The new origin, in image coordinates.
|
|
63
|
+
* @param options - A grid to round to, then bounds to stay inside.
|
|
64
|
+
* @returns The moved datum.
|
|
65
|
+
*/
|
|
66
|
+
export function moveDatum(d: Datum, to: Point, options: MoveDatumOptions = {}): Datum {
|
|
67
|
+
const grid = options.grid ?? 0;
|
|
68
|
+
const b = options.bounds;
|
|
69
|
+
return {
|
|
70
|
+
origin: { x: place(to.x, grid, b?.x, b ? b.x + b.width : undefined), y: place(to.y, grid, b?.y, b ? b.y + b.height : undefined) },
|
|
71
|
+
angle: d.angle,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* One coordinate rounded to the grid and kept in `[low, high]`: past an edge it goes to the last
|
|
77
|
+
* grid line inside. With no grid line inside, it is only kept in.
|
|
78
|
+
*/
|
|
79
|
+
function place(value: number, grid: number, low = -Infinity, high = Infinity): number {
|
|
80
|
+
if (grid > 0) {
|
|
81
|
+
const first = Math.ceil(low / grid) * grid;
|
|
82
|
+
const last = Math.floor(high / grid) * grid;
|
|
83
|
+
if (first <= last) return Math.min(last, Math.max(first, Math.round(value / grid) * grid));
|
|
84
|
+
}
|
|
85
|
+
return Math.min(high, Math.max(low, value));
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Turn a datum about its origin so that its i axis points at `to`.
|
|
90
|
+
*
|
|
91
|
+
* @param d - The datum.
|
|
92
|
+
* @param to - The pointer, in image coordinates. At the origin itself the angle is kept.
|
|
93
|
+
* @param snap - Round the angle to a multiple of this many radians; 0 for none.
|
|
94
|
+
* @returns The turned datum, its angle in `(-π, π]`.
|
|
95
|
+
*/
|
|
96
|
+
export function rotateDatum(d: Datum, to: Point, snap = 0): Datum {
|
|
97
|
+
const dx = to.x - d.origin.x;
|
|
98
|
+
const dy = to.y - d.origin.y;
|
|
99
|
+
const raw = dx === 0 && dy === 0 ? d.angle : Math.atan2(dy, dx);
|
|
100
|
+
return { origin: d.origin, angle: normalizeAngle(snapAngle(raw, snap)) };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** What `datumPress` reads besides the press. */
|
|
104
|
+
export interface DatumPressOptions {
|
|
105
|
+
/** The i axis's length, in screen pixels. Defaults to 40. */
|
|
106
|
+
armLength?: number | undefined;
|
|
107
|
+
/** Whether the arm can be grabbed to turn the datum. Defaults to `true`. */
|
|
108
|
+
rotatable?: boolean | undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* What a press on a datum grabs, decided once for the whole glyph.
|
|
113
|
+
*
|
|
114
|
+
* The origin is its ring (7 screen px); the arm is the i axis from the ring out to its handle.
|
|
115
|
+
* Each is within reach when the press is within `radius` screen pixels of it, and the nearer
|
|
116
|
+
* one wins; a tie goes to the origin.
|
|
117
|
+
*
|
|
118
|
+
* @param d - The datum.
|
|
119
|
+
* @param point - The press, in image coordinates.
|
|
120
|
+
* @param scale - The view scale: screen pixels per image pixel.
|
|
121
|
+
* @param radius - The pointer's tolerance in screen pixels: 12 for a touch, 6 for a mouse.
|
|
122
|
+
* @param options - The arm's length, and whether it can be grabbed.
|
|
123
|
+
* @returns `"origin"` (move), `"arm"` (turn), or `null`: the press belongs to whatever is below.
|
|
124
|
+
*/
|
|
125
|
+
export function datumPress(
|
|
126
|
+
d: Datum,
|
|
127
|
+
point: Point,
|
|
128
|
+
scale: number,
|
|
129
|
+
radius: number,
|
|
130
|
+
options: DatumPressOptions = {},
|
|
131
|
+
): "origin" | "arm" | null {
|
|
132
|
+
const armPx = options.armLength ?? DATUM_ARM_PX;
|
|
133
|
+
// Measured in screen pixels, about the origin.
|
|
134
|
+
const px = (point.x - d.origin.x) * scale;
|
|
135
|
+
const py = (point.y - d.origin.y) * scale;
|
|
136
|
+
const origin = Math.max(0, Math.hypot(px, py) - DATUM_RING_PX);
|
|
137
|
+
let arm = Infinity;
|
|
138
|
+
if (options.rotatable !== false) {
|
|
139
|
+
const c = Math.cos(d.angle);
|
|
140
|
+
const s = Math.sin(d.angle);
|
|
141
|
+
const start = Math.min(DATUM_RING_PX, armPx);
|
|
142
|
+
const q = closestOnSegment({ x: px, y: py }, start * c, start * s, armPx * c, armPx * s);
|
|
143
|
+
const handle = Math.max(0, Math.hypot(px - armPx * c, py - armPx * s) - DATUM_HANDLE_PX);
|
|
144
|
+
arm = Math.min(Math.hypot(q.x - px, q.y - py), handle);
|
|
145
|
+
}
|
|
146
|
+
if (origin <= radius && origin <= arm) return "origin";
|
|
147
|
+
if (arm <= radius) return "arm";
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Which labels a layer draws: the level-of-detail rule of the overlay grammar
|
|
3
|
-
* (`docs/visual-language.md` §5), shared by `PointSet`, `AreaSet` and `GridLayer`.
|
|
2
|
+
* Which labels a layer draws: the level-of-detail rule of the overlay grammar, shared by `PointSet`, `AreaSet` and `GridLayer`.
|
|
4
3
|
*
|
|
5
4
|
* Labels are 11 px mono. They are shown only where their anchors are at least 24 screen
|
|
6
5
|
* pixels apart (so a zoomed-out scene shows none and a zoomed-in one shows all), at most 200 at
|
|
@@ -14,7 +13,7 @@ import { useStage } from "./stage/ImageStage";
|
|
|
14
13
|
import { useScreenPx } from "./stage/useScreenPx";
|
|
15
14
|
import type { Rect } from "./stage/view";
|
|
16
15
|
|
|
17
|
-
/** Labels need this much room between anchors, in screen pixels (overlay grammar
|
|
16
|
+
/** Labels need this much room between anchors, in screen pixels (overlay grammar). */
|
|
18
17
|
export const LABEL_SPACING = 24;
|
|
19
18
|
/** The most label elements drawn at once. */
|
|
20
19
|
export const MAX_LABELS = 200;
|
|
@@ -72,10 +71,7 @@ export function useLabelIndices({ enabled, xy, labelled, forced }: LabelAnchors)
|
|
|
72
71
|
const stage = useStage();
|
|
73
72
|
const unit = useScreenPx()(1);
|
|
74
73
|
const spacing = LABEL_SPACING * unit;
|
|
75
|
-
const spaced = useMemo(
|
|
76
|
-
() => (enabled && labelled.length > 0 ? thinPoints(xy, spacing, labelled) : []),
|
|
77
|
-
[enabled, labelled, xy, spacing],
|
|
78
|
-
);
|
|
74
|
+
const spaced = useMemo(() => (enabled && labelled.length > 0 ? thinPoints(xy, spacing, labelled) : []), [enabled, labelled, xy, spacing]);
|
|
79
75
|
return useMemo(() => {
|
|
80
76
|
if (!enabled || (spaced.length === 0 && forced.length === 0)) return [];
|
|
81
77
|
const view = visibleRect(stage.view, stage.box);
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Marker glyphs for `PointSet`: one path generator per feature kind (overlay grammar
|
|
3
|
-
* `docs/visual-language.md` §5).
|
|
2
|
+
* Marker glyphs for `PointSet`: one path generator per feature kind (the overlay grammar).
|
|
4
3
|
*
|
|
5
4
|
* A glyph is a function from a position to SVG path data, so a layer can append thousands of
|
|
6
5
|
* them into one `<path>` per state and style. Sizes are screen pixels: a generator receives
|
|
@@ -113,9 +112,7 @@ export const MARKER_SHAPES: Readonly<Record<BuiltinMarkerKind, MarkerShape>> = {
|
|
|
113
112
|
path: (x, y, u, angle) => {
|
|
114
113
|
const c = Math.cos(angle);
|
|
115
114
|
const s = Math.sin(angle);
|
|
116
|
-
return (
|
|
117
|
-
`${circlePath(x, y, 2 * u)}M${n(x + 2 * u * c)} ${n(y + 2 * u * s)}L${n(x + 8 * u * c)} ${n(y + 8 * u * s)}`
|
|
118
|
-
);
|
|
115
|
+
return `${circlePath(x, y, 2 * u)}M${n(x + 2 * u * c)} ${n(y + 2 * u * s)}L${n(x + 8 * u * c)} ${n(y + 8 * u * s)}`;
|
|
119
116
|
},
|
|
120
117
|
},
|
|
121
118
|
};
|