dsh-quay-bot 1.0.2 → 1.0.6
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 +28 -105
- package/botscreen/requirements.txt +1 -1
- package/client.js +123 -53
- package/cordis.patch.yml +1 -1
- package/host/api.js +4 -0
- package/host/remote.js +285 -8
- package/host/screen.js +22 -86
- package/package.json +34 -32
- package/remote/bin/dsh-remote.mjs +435 -0
- package/remote/package.json +27 -0
- package/remote/src/config.mjs +183 -0
- package/remote/src/crypto.mjs +220 -0
- package/remote/src/keys.mjs +226 -0
- package/remote/src/proxy.mjs +1028 -0
- package/remote/src/sessions.mjs +163 -0
- package/remote/src/shim.js +838 -0
- package/remote/src/supervisor.mjs +207 -0
- package/remote/src/tunnel.mjs +533 -0
- package/remote/src/upstream.mjs +403 -0
- package/remote/src/util.mjs +149 -0
- package/remote/src/ws.mjs +313 -0
- package/LICENSE +0 -21
- package/avatar/build.mjs +0 -81
- package/avatar/gallery/droid.json +0 -14
- package/avatar/gallery/ghost.json +0 -14
- package/avatar/gallery/grok-pod.json +0 -14
- package/avatar/gallery/guardian.json +0 -14
- package/avatar/gallery/mecha.json +0 -14
- package/avatar/gallery/mochi.json +0 -14
- package/avatar/gallery/neko.json +0 -14
- package/avatar/gallery/orbiter.json +0 -14
- package/avatar/gallery/relay.json +0 -14
- package/avatar/gallery/spark.json +0 -14
- package/avatar/src/bot/clay.ts +0 -135
- package/avatar/src/bot/effects.ts +0 -100
- package/avatar/src/bot/grok-bot.ts +0 -626
- package/avatar/src/bot/index.ts +0 -15
- package/avatar/src/bot/palettes.ts +0 -62
- package/avatar/src/bot/ring.ts +0 -219
- package/avatar/src/bot/scheduler.ts +0 -239
- package/avatar/src/bot/shapes.ts +0 -277
- package/avatar/src/bot/spring.ts +0 -196
- package/avatar/src/core/color.ts +0 -92
- package/avatar/src/core/eyes.ts +0 -297
- package/avatar/src/core/field.ts +0 -482
- package/avatar/src/core/frame.ts +0 -655
- package/avatar/src/core/fx.ts +0 -375
- package/avatar/src/core/index.ts +0 -49
- package/avatar/src/core/paint.ts +0 -331
- package/avatar/src/core/path.ts +0 -495
- package/avatar/src/core/pose.ts +0 -581
- package/avatar/src/core/svg.ts +0 -98
- package/avatar/src/core/types.ts +0 -153
- package/avatar/src/core/validate.ts +0 -296
- package/avatar/src/core/volume.ts +0 -681
- package/avatar/src/entry.ts +0 -260
- package/bin/doctor.mjs +0 -148
- package/botscreen/requirements-offline.txt +0 -11
- package/market/index.json +0 -41
- package/vendor/pip/pip-25.1.1-py3-none-any.whl +0 -0
- package/vendor/pip/setuptools-78.1.1-py3-none-any.whl +0 -0
package/avatar/src/core/path.ts
DELETED
|
@@ -1,495 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 受限 SVG 路径几何:解析、归一化、展平。
|
|
3
|
-
*
|
|
4
|
-
* 只接受 M L H V C S Q T A Z(绝对与相对)。解析结果由数字重新序列化,
|
|
5
|
-
* 因此模型写的文本永远不会被原样回显进标记。
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export type Segment =
|
|
9
|
-
| { c: 'M'; x: number; y: number }
|
|
10
|
-
| { c: 'L'; x: number; y: number }
|
|
11
|
-
| { c: 'C'; x1: number; y1: number; x2: number; y2: number; x: number; y: number }
|
|
12
|
-
| { c: 'Q'; x1: number; y1: number; x: number; y: number }
|
|
13
|
-
| { c: 'A'; rx: number; ry: number; rot: number; large: 0 | 1; sweep: 0 | 1; x: number; y: number }
|
|
14
|
-
| { c: 'Z' };
|
|
15
|
-
|
|
16
|
-
export type Point = [number, number];
|
|
17
|
-
|
|
18
|
-
export interface Bounds {
|
|
19
|
-
minX: number;
|
|
20
|
-
minY: number;
|
|
21
|
-
maxX: number;
|
|
22
|
-
maxY: number;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export const PATH_LIMITS = {
|
|
26
|
-
maxChars: 6000,
|
|
27
|
-
maxSegments: 400,
|
|
28
|
-
maxSubpaths: 16,
|
|
29
|
-
coordMin: -20,
|
|
30
|
-
coordMax: 120,
|
|
31
|
-
} as const;
|
|
32
|
-
|
|
33
|
-
/** 曲线与其折线代理之间允许的最大偏差(viewBox 单位)。 */
|
|
34
|
-
export const FLATNESS = 0.03;
|
|
35
|
-
|
|
36
|
-
const ARG_COUNT: Record<string, number> = { M: 2, L: 2, H: 1, V: 1, C: 6, S: 4, Q: 4, T: 2, A: 7, Z: 0 };
|
|
37
|
-
|
|
38
|
-
export class PathError extends Error {}
|
|
39
|
-
|
|
40
|
-
function tokenize(d: string): (string | number)[] {
|
|
41
|
-
if (/[^MmLlHhVvCcSsQqTtAaZz0-9.,eE+\-\s]/.test(d)) {
|
|
42
|
-
const bad = /[^MmLlHhVvCcSsQqTtAaZz0-9.,eE+\-\s]/.exec(d)![0];
|
|
43
|
-
throw new PathError(`unsupported character ${JSON.stringify(bad)} (allowed commands: M L H V C S Q T A Z)`);
|
|
44
|
-
}
|
|
45
|
-
const tokens: (string | number)[] = [];
|
|
46
|
-
const re = /([MmLlHhVvCcSsQqTtAaZz])|([+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)|([\s,]+)/y;
|
|
47
|
-
let i = 0;
|
|
48
|
-
while (i < d.length) {
|
|
49
|
-
re.lastIndex = i;
|
|
50
|
-
const m = re.exec(d);
|
|
51
|
-
if (!m) throw new PathError(`cannot parse path near ${JSON.stringify(d.slice(i, i + 12))}`);
|
|
52
|
-
if (m[1]) tokens.push(m[1]);
|
|
53
|
-
else if (m[2]) tokens.push(Number(m[2]));
|
|
54
|
-
i = re.lastIndex;
|
|
55
|
-
}
|
|
56
|
-
return tokens;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** 解析并转成绝对的 M/L/C/Q/A/Z 段。失败抛 PathError。 */
|
|
60
|
-
export function parsePath(d: string): Segment[] {
|
|
61
|
-
if (typeof d !== 'string' || d.trim() === '') throw new PathError('path is empty');
|
|
62
|
-
if (d.length > PATH_LIMITS.maxChars) throw new PathError(`path longer than ${PATH_LIMITS.maxChars} characters`);
|
|
63
|
-
const tokens = tokenize(d);
|
|
64
|
-
const out: Segment[] = [];
|
|
65
|
-
let i = 0;
|
|
66
|
-
let cx = 0;
|
|
67
|
-
let cy = 0;
|
|
68
|
-
let sx = 0;
|
|
69
|
-
let sy = 0;
|
|
70
|
-
let prevC: Point | null = null;
|
|
71
|
-
let prevQ: Point | null = null;
|
|
72
|
-
let cmd = '';
|
|
73
|
-
let subpaths = 0;
|
|
74
|
-
let open = false;
|
|
75
|
-
|
|
76
|
-
while (i < tokens.length) {
|
|
77
|
-
const t = tokens[i];
|
|
78
|
-
if (typeof t === 'string') {
|
|
79
|
-
cmd = t;
|
|
80
|
-
i++;
|
|
81
|
-
if (cmd === 'Z' || cmd === 'z') {
|
|
82
|
-
if (!open) throw new PathError('Z without an open subpath');
|
|
83
|
-
out.push({ c: 'Z' });
|
|
84
|
-
cx = sx;
|
|
85
|
-
cy = sy;
|
|
86
|
-
prevC = prevQ = null;
|
|
87
|
-
open = false;
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
90
|
-
} else if (!cmd) {
|
|
91
|
-
throw new PathError('path must start with M');
|
|
92
|
-
} else if (cmd === 'Z' || cmd === 'z') {
|
|
93
|
-
throw new PathError('numbers after Z');
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const up = cmd.toUpperCase();
|
|
97
|
-
const rel = cmd !== up;
|
|
98
|
-
if (out.length === 0 && up !== 'M') throw new PathError('path must start with M');
|
|
99
|
-
const n = ARG_COUNT[up];
|
|
100
|
-
const args: number[] = [];
|
|
101
|
-
for (let k = 0; k < n; k++) {
|
|
102
|
-
const v = tokens[i + k];
|
|
103
|
-
if (typeof v !== 'number') throw new PathError(`command ${cmd} expects ${n} numbers`);
|
|
104
|
-
if (!Number.isFinite(v)) throw new PathError('non-finite number');
|
|
105
|
-
args.push(v);
|
|
106
|
-
}
|
|
107
|
-
i += n;
|
|
108
|
-
const ox = rel ? cx : 0;
|
|
109
|
-
const oy = rel ? cy : 0;
|
|
110
|
-
let nextC: Point | null = null;
|
|
111
|
-
let nextQ: Point | null = null;
|
|
112
|
-
|
|
113
|
-
switch (up) {
|
|
114
|
-
case 'M': {
|
|
115
|
-
if (open) out.push({ c: 'Z' });
|
|
116
|
-
cx = args[0] + ox;
|
|
117
|
-
cy = args[1] + oy;
|
|
118
|
-
sx = cx;
|
|
119
|
-
sy = cy;
|
|
120
|
-
out.push({ c: 'M', x: cx, y: cy });
|
|
121
|
-
subpaths++;
|
|
122
|
-
open = true;
|
|
123
|
-
cmd = rel ? 'l' : 'L';
|
|
124
|
-
break;
|
|
125
|
-
}
|
|
126
|
-
case 'L':
|
|
127
|
-
cx = args[0] + ox;
|
|
128
|
-
cy = args[1] + oy;
|
|
129
|
-
out.push({ c: 'L', x: cx, y: cy });
|
|
130
|
-
break;
|
|
131
|
-
case 'H':
|
|
132
|
-
cx = args[0] + ox;
|
|
133
|
-
out.push({ c: 'L', x: cx, y: cy });
|
|
134
|
-
break;
|
|
135
|
-
case 'V':
|
|
136
|
-
cy = args[0] + oy;
|
|
137
|
-
out.push({ c: 'L', x: cx, y: cy });
|
|
138
|
-
break;
|
|
139
|
-
case 'C': {
|
|
140
|
-
const s: Segment = { c: 'C', x1: args[0] + ox, y1: args[1] + oy, x2: args[2] + ox, y2: args[3] + oy, x: args[4] + ox, y: args[5] + oy };
|
|
141
|
-
out.push(s);
|
|
142
|
-
nextC = [s.x2, s.y2];
|
|
143
|
-
cx = s.x;
|
|
144
|
-
cy = s.y;
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
case 'S': {
|
|
148
|
-
const x1: number = prevC ? 2 * cx - prevC[0] : cx;
|
|
149
|
-
const y1: number = prevC ? 2 * cy - prevC[1] : cy;
|
|
150
|
-
const s: Segment = { c: 'C', x1, y1, x2: args[0] + ox, y2: args[1] + oy, x: args[2] + ox, y: args[3] + oy };
|
|
151
|
-
out.push(s);
|
|
152
|
-
nextC = [s.x2, s.y2];
|
|
153
|
-
cx = s.x;
|
|
154
|
-
cy = s.y;
|
|
155
|
-
break;
|
|
156
|
-
}
|
|
157
|
-
case 'Q': {
|
|
158
|
-
const s: Segment = { c: 'Q', x1: args[0] + ox, y1: args[1] + oy, x: args[2] + ox, y: args[3] + oy };
|
|
159
|
-
out.push(s);
|
|
160
|
-
nextQ = [s.x1, s.y1];
|
|
161
|
-
cx = s.x;
|
|
162
|
-
cy = s.y;
|
|
163
|
-
break;
|
|
164
|
-
}
|
|
165
|
-
case 'T': {
|
|
166
|
-
const x1: number = prevQ ? 2 * cx - prevQ[0] : cx;
|
|
167
|
-
const y1: number = prevQ ? 2 * cy - prevQ[1] : cy;
|
|
168
|
-
const s: Segment = { c: 'Q', x1, y1, x: args[0] + ox, y: args[1] + oy };
|
|
169
|
-
out.push(s);
|
|
170
|
-
nextQ = [x1, y1];
|
|
171
|
-
cx = s.x;
|
|
172
|
-
cy = s.y;
|
|
173
|
-
break;
|
|
174
|
-
}
|
|
175
|
-
case 'A': {
|
|
176
|
-
const [rx, ry, rot, la, sw] = args;
|
|
177
|
-
if (rx < 0 || ry < 0) throw new PathError('arc radii must be non-negative');
|
|
178
|
-
if ((la !== 0 && la !== 1) || (sw !== 0 && sw !== 1)) throw new PathError('arc flags must be 0 or 1, written as separate numbers');
|
|
179
|
-
cx = args[5] + ox;
|
|
180
|
-
cy = args[6] + oy;
|
|
181
|
-
out.push({ c: 'A', rx, ry, rot, large: la as 0 | 1, sweep: sw as 0 | 1, x: cx, y: cy });
|
|
182
|
-
break;
|
|
183
|
-
}
|
|
184
|
-
default:
|
|
185
|
-
throw new PathError(`unsupported command ${cmd}`);
|
|
186
|
-
}
|
|
187
|
-
prevC = nextC;
|
|
188
|
-
prevQ = nextQ;
|
|
189
|
-
if (out.length > PATH_LIMITS.maxSegments) throw new PathError(`more than ${PATH_LIMITS.maxSegments} segments`);
|
|
190
|
-
}
|
|
191
|
-
if (open) out.push({ c: 'Z' });
|
|
192
|
-
if (subpaths > PATH_LIMITS.maxSubpaths) throw new PathError(`more than ${PATH_LIMITS.maxSubpaths} subpaths`);
|
|
193
|
-
for (const s of out) {
|
|
194
|
-
for (const [k, v] of Object.entries(s)) {
|
|
195
|
-
if (k === 'c' || typeof v !== 'number') continue;
|
|
196
|
-
if (k === 'rot' || k === 'large' || k === 'sweep') continue;
|
|
197
|
-
if (v < PATH_LIMITS.coordMin || v > PATH_LIMITS.coordMax) throw new PathError(`coordinate ${v} far outside the 0..100 viewBox`);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
return out;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
const num = (n: number): string => {
|
|
204
|
-
const r = Math.round(n * 100) / 100;
|
|
205
|
-
return (Object.is(r, -0) ? 0 : r).toString();
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
/** 只由解析出的数字构成的紧凑绝对序列化。 */
|
|
209
|
-
export function serializePath(segs: Segment[]): string {
|
|
210
|
-
const parts: string[] = [];
|
|
211
|
-
for (const s of segs) {
|
|
212
|
-
switch (s.c) {
|
|
213
|
-
case 'M':
|
|
214
|
-
parts.push(`M${num(s.x)} ${num(s.y)}`);
|
|
215
|
-
break;
|
|
216
|
-
case 'L':
|
|
217
|
-
parts.push(`L${num(s.x)} ${num(s.y)}`);
|
|
218
|
-
break;
|
|
219
|
-
case 'C':
|
|
220
|
-
parts.push(`C${num(s.x1)} ${num(s.y1)} ${num(s.x2)} ${num(s.y2)} ${num(s.x)} ${num(s.y)}`);
|
|
221
|
-
break;
|
|
222
|
-
case 'Q':
|
|
223
|
-
parts.push(`Q${num(s.x1)} ${num(s.y1)} ${num(s.x)} ${num(s.y)}`);
|
|
224
|
-
break;
|
|
225
|
-
case 'A':
|
|
226
|
-
parts.push(`A${num(s.rx)} ${num(s.ry)} ${num(s.rot)} ${s.large} ${s.sweep} ${num(s.x)} ${num(s.y)}`);
|
|
227
|
-
break;
|
|
228
|
-
case 'Z':
|
|
229
|
-
parts.push('Z');
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
return parts.join('');
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/** 端点式圆弧 → 采样点(SVG 规范 F.6.5)。 */
|
|
237
|
-
function sampleArc(x0: number, y0: number, s: Extract<Segment, { c: 'A' }>, out: Point[]): void {
|
|
238
|
-
let { rx, ry } = s;
|
|
239
|
-
const { x, y } = s;
|
|
240
|
-
if (rx === 0 || ry === 0 || (x0 === x && y0 === y)) {
|
|
241
|
-
out.push([x, y]);
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
const phi = (s.rot * Math.PI) / 180;
|
|
245
|
-
const cos = Math.cos(phi);
|
|
246
|
-
const sin = Math.sin(phi);
|
|
247
|
-
const dx = (x0 - x) / 2;
|
|
248
|
-
const dy = (y0 - y) / 2;
|
|
249
|
-
const x1p = cos * dx + sin * dy;
|
|
250
|
-
const y1p = -sin * dx + cos * dy;
|
|
251
|
-
rx = Math.abs(rx);
|
|
252
|
-
ry = Math.abs(ry);
|
|
253
|
-
const lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
|
|
254
|
-
if (lambda > 1) {
|
|
255
|
-
const k = Math.sqrt(lambda);
|
|
256
|
-
rx *= k;
|
|
257
|
-
ry *= k;
|
|
258
|
-
}
|
|
259
|
-
const numerator = rx * rx * ry * ry - rx * rx * y1p * y1p - ry * ry * x1p * x1p;
|
|
260
|
-
const denominator = rx * rx * y1p * y1p + ry * ry * x1p * x1p;
|
|
261
|
-
let coef = Math.sqrt(Math.max(0, numerator / denominator));
|
|
262
|
-
if (s.large === s.sweep) coef = -coef;
|
|
263
|
-
const cxp = (coef * rx * y1p) / ry;
|
|
264
|
-
const cyp = (-coef * ry * x1p) / rx;
|
|
265
|
-
const cx = cos * cxp - sin * cyp + (x0 + x) / 2;
|
|
266
|
-
const cy = sin * cxp + cos * cyp + (y0 + y) / 2;
|
|
267
|
-
const angle = (ux: number, uy: number, vx: number, vy: number) => Math.atan2(ux * vy - uy * vx, ux * vx + uy * vy);
|
|
268
|
-
const t1 = angle(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry);
|
|
269
|
-
let delta = angle((x1p - cxp) / rx, (y1p - cyp) / ry, (-x1p - cxp) / rx, (-y1p - cyp) / ry);
|
|
270
|
-
if (!s.sweep && delta > 0) delta -= 2 * Math.PI;
|
|
271
|
-
if (s.sweep && delta < 0) delta += 2 * Math.PI;
|
|
272
|
-
// 弦与弧的偏差不超过 FLATNESS:弦在圆心角 θ 处下陷 r(1 - cos(θ/2))。
|
|
273
|
-
const stepAngle = 2 * Math.acos(Math.max(-1, 1 - FLATNESS / Math.max(rx, ry)));
|
|
274
|
-
const steps = Math.min(256, Math.max(4, Math.ceil(Math.abs(delta) / stepAngle)));
|
|
275
|
-
for (let k = 1; k <= steps; k++) {
|
|
276
|
-
const t = t1 + (delta * k) / steps;
|
|
277
|
-
const ex = rx * Math.cos(t);
|
|
278
|
-
const ey = ry * Math.sin(t);
|
|
279
|
-
out.push([cos * ex - sin * ey + cx, sin * ex + cos * ey + cy]);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/** 展平为闭合多边形,每个子路径一个。 */
|
|
284
|
-
export function flattenPath(segs: Segment[]): Point[][] {
|
|
285
|
-
const polys: Point[][] = [];
|
|
286
|
-
let cur: Point[] = [];
|
|
287
|
-
let px = 0;
|
|
288
|
-
let py = 0;
|
|
289
|
-
for (const s of segs) {
|
|
290
|
-
switch (s.c) {
|
|
291
|
-
case 'M':
|
|
292
|
-
if (cur.length) polys.push(cur);
|
|
293
|
-
cur = [[s.x, s.y]];
|
|
294
|
-
px = s.x;
|
|
295
|
-
py = s.y;
|
|
296
|
-
break;
|
|
297
|
-
case 'L':
|
|
298
|
-
cur.push([s.x, s.y]);
|
|
299
|
-
px = s.x;
|
|
300
|
-
py = s.y;
|
|
301
|
-
break;
|
|
302
|
-
case 'C': {
|
|
303
|
-
// Wang 上界:这么多弦就能把误差控制在 FLATNESS 内。
|
|
304
|
-
const m = Math.max(
|
|
305
|
-
Math.hypot(px - 2 * s.x1 + s.x2, py - 2 * s.y1 + s.y2),
|
|
306
|
-
Math.hypot(s.x1 - 2 * s.x2 + s.x, s.y1 - 2 * s.y2 + s.y),
|
|
307
|
-
);
|
|
308
|
-
const n = Math.min(128, Math.max(2, Math.ceil(Math.sqrt((0.75 * m) / FLATNESS))));
|
|
309
|
-
for (let k = 1; k <= n; k++) {
|
|
310
|
-
const t = k / n;
|
|
311
|
-
const u = 1 - t;
|
|
312
|
-
cur.push([
|
|
313
|
-
u * u * u * px + 3 * u * u * t * s.x1 + 3 * u * t * t * s.x2 + t * t * t * s.x,
|
|
314
|
-
u * u * u * py + 3 * u * u * t * s.y1 + 3 * u * t * t * s.y2 + t * t * t * s.y,
|
|
315
|
-
]);
|
|
316
|
-
}
|
|
317
|
-
px = s.x;
|
|
318
|
-
py = s.y;
|
|
319
|
-
break;
|
|
320
|
-
}
|
|
321
|
-
case 'Q': {
|
|
322
|
-
const n = Math.min(128, Math.max(2, Math.ceil(Math.sqrt((0.25 * Math.hypot(px - 2 * s.x1 + s.x, py - 2 * s.y1 + s.y)) / FLATNESS))));
|
|
323
|
-
for (let k = 1; k <= n; k++) {
|
|
324
|
-
const t = k / n;
|
|
325
|
-
const u = 1 - t;
|
|
326
|
-
cur.push([u * u * px + 2 * u * t * s.x1 + t * t * s.x, u * u * py + 2 * u * t * s.y1 + t * t * s.y]);
|
|
327
|
-
}
|
|
328
|
-
px = s.x;
|
|
329
|
-
py = s.y;
|
|
330
|
-
break;
|
|
331
|
-
}
|
|
332
|
-
case 'A':
|
|
333
|
-
sampleArc(px, py, s, cur);
|
|
334
|
-
px = s.x;
|
|
335
|
-
py = s.y;
|
|
336
|
-
break;
|
|
337
|
-
case 'Z':
|
|
338
|
-
if (cur.length) {
|
|
339
|
-
polys.push(cur);
|
|
340
|
-
px = cur[0][0];
|
|
341
|
-
py = cur[0][1];
|
|
342
|
-
}
|
|
343
|
-
cur = [];
|
|
344
|
-
break;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
if (cur.length) polys.push(cur);
|
|
348
|
-
return polys.filter((p) => p.length >= 3);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
/** 点对所有子路径的 nonzero 环绕数(SVG 默认填充规则)。 */
|
|
352
|
-
export function winding(polys: Point[][], x: number, y: number): number {
|
|
353
|
-
let w = 0;
|
|
354
|
-
for (const poly of polys) {
|
|
355
|
-
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
|
|
356
|
-
const [xi, yi] = poly[i];
|
|
357
|
-
const [xj, yj] = poly[j];
|
|
358
|
-
if (yj <= y) {
|
|
359
|
-
if (yi > y && (xi - xj) * (y - yj) - (x - xj) * (yi - yj) > 0) w++;
|
|
360
|
-
} else if (yi <= y && (xi - xj) * (y - yj) - (x - xj) * (yi - yj) < 0) w--;
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
return w;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
export function isInside(polys: Point[][], x: number, y: number): boolean {
|
|
367
|
-
return winding(polys, x, y) !== 0;
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
/** 一行上多点(xs 升序)的 nonzero 内部标记,一次扫描所有边。 */
|
|
371
|
-
export function insideRow(polys: Point[][], y: number, xs: ArrayLike<number>, out: Uint8Array): void {
|
|
372
|
-
const hits: [number, number][] = [];
|
|
373
|
-
for (const poly of polys) {
|
|
374
|
-
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
|
|
375
|
-
const [xj, yj] = poly[j];
|
|
376
|
-
const [xi, yi] = poly[i];
|
|
377
|
-
if (yj <= y && yi > y) hits.push([xj + ((y - yj) * (xi - xj)) / (yi - yj), 1]);
|
|
378
|
-
else if (yi <= y && yj > y) hits.push([xj + ((y - yj) * (xi - xj)) / (yi - yj), -1]);
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
hits.sort((a, b) => a[0] - b[0]);
|
|
382
|
-
let w = 0;
|
|
383
|
-
let h = 0;
|
|
384
|
-
for (let k = 0; k < xs.length; k++) {
|
|
385
|
-
while (h < hits.length && hits[h][0] < xs[k]) w += hits[h++][1];
|
|
386
|
-
out[k] = w !== 0 ? 1 : 0;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* 一组多边形的边表,供同一个轮廓上的大量点/行测试使用。
|
|
392
|
-
* 查询只做一遍算术,不建格子索引:一个定义里这类查询最多几十万次,够用且好懂。
|
|
393
|
-
*/
|
|
394
|
-
export class EdgeBands {
|
|
395
|
-
constructor(private readonly polys: Point[][]) {}
|
|
396
|
-
|
|
397
|
-
private crossings(y: number): { xs: Float64Array; dirs: Int8Array; n: number } {
|
|
398
|
-
const xs = new Float64Array(64);
|
|
399
|
-
const dirs = new Int8Array(64);
|
|
400
|
-
let n = 0;
|
|
401
|
-
for (const poly of this.polys) {
|
|
402
|
-
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
|
|
403
|
-
const [xj, yj] = poly[j];
|
|
404
|
-
const [xi, yi] = poly[i];
|
|
405
|
-
let dir = 0;
|
|
406
|
-
if (yj <= y && yi > y) dir = 1;
|
|
407
|
-
else if (yi <= y && yj > y) dir = -1;
|
|
408
|
-
else continue;
|
|
409
|
-
const x = xj + ((y - yj) * (xi - xj)) / (yi - yj);
|
|
410
|
-
let m = n++;
|
|
411
|
-
for (; m > 0 && xs[m - 1] > x; m--) {
|
|
412
|
-
xs[m] = xs[m - 1];
|
|
413
|
-
dirs[m] = dirs[m - 1];
|
|
414
|
-
}
|
|
415
|
-
xs[m] = x;
|
|
416
|
-
dirs[m] = dir;
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
return { xs, dirs, n };
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/** 同 `winding`。 */
|
|
423
|
-
winding(x: number, y: number): number {
|
|
424
|
-
const { xs, dirs, n } = this.crossings(y);
|
|
425
|
-
let w = 0;
|
|
426
|
-
for (let k = 0; k < n && xs[k] < x; k++) w += dirs[k];
|
|
427
|
-
return w;
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
/** 同 `insideRow`。 */
|
|
431
|
-
insideRow(y: number, points: ArrayLike<number>, out: Uint8Array): void {
|
|
432
|
-
const { xs, dirs, n } = this.crossings(y);
|
|
433
|
-
let w = 0;
|
|
434
|
-
let h = 0;
|
|
435
|
-
for (let k = 0; k < points.length; k++) {
|
|
436
|
-
while (h < n && xs[h] < points[k]) w += dirs[h++];
|
|
437
|
-
out[k] = w !== 0 ? 1 : 0;
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
/** 单点版本。 */
|
|
442
|
-
insideRowAt(x: number, y: number): boolean {
|
|
443
|
-
return this.winding(x, y) !== 0;
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
const bandCache = new WeakMap<Point[][], EdgeBands>();
|
|
448
|
-
|
|
449
|
-
/** 这些多边形的边表,每个数组只建一次;建好后数组不可再改。 */
|
|
450
|
-
export function edgeBands(polys: Point[][]): EdgeBands {
|
|
451
|
-
let index = bandCache.get(polys);
|
|
452
|
-
if (!index) {
|
|
453
|
-
index = new EdgeBands(polys);
|
|
454
|
-
bandCache.set(polys, index);
|
|
455
|
-
}
|
|
456
|
-
return index;
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
export function bounds(polys: Point[][]): Bounds {
|
|
460
|
-
const b: Bounds = { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity };
|
|
461
|
-
for (const p of polys) {
|
|
462
|
-
for (let k = 0; k < p.length; k++) {
|
|
463
|
-
const x = p[k][0];
|
|
464
|
-
const y = p[k][1];
|
|
465
|
-
if (x < b.minX) b.minX = x;
|
|
466
|
-
if (y < b.minY) b.minY = y;
|
|
467
|
-
if (x > b.maxX) b.maxX = x;
|
|
468
|
-
if (y > b.maxY) b.maxY = y;
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
return b;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
/** 在 1 单位采样网格上估算填充面积(nonzero 规则)。 */
|
|
475
|
-
export function filledArea(polys: Point[][], b: Bounds = bounds(polys)): number {
|
|
476
|
-
let n = 0;
|
|
477
|
-
const xs: number[] = [];
|
|
478
|
-
for (let x = Math.floor(b.minX) + 0.5; x < b.maxX; x += 1) xs.push(x);
|
|
479
|
-
const row = new Uint8Array(xs.length);
|
|
480
|
-
for (let y = Math.floor(b.minY) + 0.5; y < b.maxY; y += 1) {
|
|
481
|
-
insideRow(polys, y, xs, row);
|
|
482
|
-
for (let k = 0; k < row.length; k++) n += row[k];
|
|
483
|
-
}
|
|
484
|
-
return n;
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
/** 把归一化段按子路径切开。 */
|
|
488
|
-
export function splitSubpaths(segs: Segment[]): Segment[][] {
|
|
489
|
-
const out: Segment[][] = [];
|
|
490
|
-
for (const s of segs) {
|
|
491
|
-
if (s.c === 'M') out.push([]);
|
|
492
|
-
out[out.length - 1]?.push(s);
|
|
493
|
-
}
|
|
494
|
-
return out;
|
|
495
|
-
}
|