inlaytrace 0.1.2 → 0.1.3
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/CHANGELOG.md +7 -0
- package/README.md +8 -4
- package/dist/index.d.ts +55 -1
- package/dist/index.js +479 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# inlaytrace
|
|
2
2
|
|
|
3
|
+
## 0.1.3
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- f71beed: New `shapes` option: a closed outline that is a circle, an ellipse, a rectangle, a rounded rectangle or a regular polygon is drawn as that exact shape instead of fitted curves (on the sample badge, 0.70% → 0.54% wrong pixels). Off by default.
|
|
8
|
+
- 074cc75: `detectSymmetry(rgba, width, height, options?)` finds the likeliest mirror axes (vertical and horizontal) and rotational orders (2–8 by default) in an image, each scored by the share of the drawing that matches its reflection or turn.
|
|
9
|
+
|
|
3
10
|
## 0.1.2
|
|
4
11
|
|
|
5
12
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -9,8 +9,8 @@ curves. Pixel art keeps its exact staircase.
|
|
|
9
9
|
Pure TypeScript with no DOM and no WebAssembly: it runs in browsers, Web
|
|
10
10
|
Workers and Node. MIT licensed.
|
|
11
11
|
|
|
12
|
-
It is the tracer behind the [Inlay app](https://inlaytrace.com/app/)
|
|
13
|
-
|
|
12
|
+
It is the tracer behind every preset of the [Inlay app](https://inlaytrace.com/app/),
|
|
13
|
+
from logos and pixel art to photos.
|
|
14
14
|
|
|
15
15
|
## Install
|
|
16
16
|
|
|
@@ -63,6 +63,9 @@ toSvg(result) // '<svg …>…</svg>'
|
|
|
63
63
|
Béziers between them).
|
|
64
64
|
- `parsePath(d)` / `serializePath(subpaths)`: path data as editable nodes with
|
|
65
65
|
absolute handles, and back. Round trips are idempotent after the first pass.
|
|
66
|
+
- `detectSymmetry(rgba, width, height)`: the likeliest mirror axes and
|
|
67
|
+
rotational orders in an image, each scored by how much of the drawing
|
|
68
|
+
matches (the app offers to enforce one at 95% or more).
|
|
66
69
|
- `oklab(rgb)`: the perceptual colour space the palette and labelling use.
|
|
67
70
|
|
|
68
71
|
## How it works
|
|
@@ -92,8 +95,9 @@ every export.
|
|
|
92
95
|
## Measured
|
|
93
96
|
|
|
94
97
|
On a 640 px JPEG of a five-colour badge, scored by rendering at 4× against the
|
|
95
|
-
clean source art: 0.
|
|
96
|
-
|
|
98
|
+
clean source art: 0.54% wrong pixels with 175 nodes with `shapes` on (0.70%
|
|
99
|
+
and 177 without), against 0.85% with 271 nodes for VTracer tracing each colour
|
|
100
|
+
on its own. At 2048 px it traces in about
|
|
97
101
|
1.3 s (VTracer: 17.5 s, timed by hand). On pixel art both draw every pixel exactly; Inlay uses
|
|
98
102
|
about 14% fewer nodes and is about 4× faster on a 1024 px sprite.
|
|
99
103
|
|
package/dist/index.d.ts
CHANGED
|
@@ -42,6 +42,11 @@ type FitOptions = {
|
|
|
42
42
|
cornerAngle: number;
|
|
43
43
|
/** Half-window in px of the smoothing over the pixel staircase (default 12). */
|
|
44
44
|
smoothing?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Draw a closed outline that is a circle, an ellipse, a (rounded) rectangle
|
|
47
|
+
* or a regular polygon as that exact shape (see shapes.ts). Default false.
|
|
48
|
+
*/
|
|
49
|
+
shapes?: boolean;
|
|
45
50
|
};
|
|
46
51
|
/** A cubic Bézier: start, two handles, end. A straight line has handles on the chord. */
|
|
47
52
|
type Cubic = [Pt, Pt, Pt, Pt];
|
|
@@ -180,4 +185,53 @@ export declare function quantizePalette(rgba: ArrayLike<number>, maxColors: numb
|
|
|
180
185
|
*/
|
|
181
186
|
export declare function exactPalette(rgba: ArrayLike<number>, maxColors: number): string[];
|
|
182
187
|
//#endregion
|
|
183
|
-
|
|
188
|
+
//#region src/symmetry.d.ts
|
|
189
|
+
/**
|
|
190
|
+
* Symmetry: find a likely mirror axis or rotational order in a raster.
|
|
191
|
+
*
|
|
192
|
+
* Works on the image, not the trace, so the answer doesn't depend on the trace
|
|
193
|
+
* settings. The foreground is every opaque pixel that differs from the
|
|
194
|
+
* background (the most common colour around the border, or transparency). A
|
|
195
|
+
* symmetric drawing's foreground centroid lies on its mirror axis or at its
|
|
196
|
+
* centre of rotation, so only candidates near the centroid are scored, each by
|
|
197
|
+
* the share of pixels (foreground on either side) whose reflection or rotation
|
|
198
|
+
* has the same colour. A coarse pass over a sample of pixels picks the
|
|
199
|
+
* candidates; a finer one settles them.
|
|
200
|
+
*/
|
|
201
|
+
type MirrorSymmetry = {
|
|
202
|
+
kind: 'mirror';
|
|
203
|
+
/** A vertical axis splits left from right; a horizontal one top from bottom. */
|
|
204
|
+
orientation: 'vertical' | 'horizontal';
|
|
205
|
+
/** x of a vertical axis or y of a horizontal one, in pixels, to the nearest 0.5. */
|
|
206
|
+
position: number;
|
|
207
|
+
/** The share of the drawing that matches its reflection, 0–1. */
|
|
208
|
+
score: number;
|
|
209
|
+
};
|
|
210
|
+
type RotationalSymmetry = {
|
|
211
|
+
kind: 'rotation';
|
|
212
|
+
/** How many times the drawing repeats in a full turn (2 is a half turn). */
|
|
213
|
+
order: number;
|
|
214
|
+
/** The centre of rotation, in pixels. */
|
|
215
|
+
cx: number;
|
|
216
|
+
cy: number;
|
|
217
|
+
/** The share of the drawing that matches itself turned by 1/order, 0–1. */
|
|
218
|
+
score: number;
|
|
219
|
+
};
|
|
220
|
+
type Symmetry = MirrorSymmetry | RotationalSymmetry;
|
|
221
|
+
type SymmetryOptions = {
|
|
222
|
+
/** RGB distance (0–441) under which two pixels count as the same colour. Default 60. */
|
|
223
|
+
tolerance?: number;
|
|
224
|
+
/** Rotational orders to try. Default 2–8. */
|
|
225
|
+
orders?: number[];
|
|
226
|
+
};
|
|
227
|
+
/**
|
|
228
|
+
* Score every candidate symmetry: the best vertical and horizontal mirror axes
|
|
229
|
+
* and the best centre for each rotational order, highest score first. A clean
|
|
230
|
+
* symmetric drawing scores close to 1; around 0.95 is a reasonable bar for
|
|
231
|
+
* "likely", since JPEG noise and anti-aliasing never match exactly. A round
|
|
232
|
+
* drawing scores well for every order. Returns `[]` for an image with almost
|
|
233
|
+
* no foreground.
|
|
234
|
+
*/
|
|
235
|
+
export declare function detectSymmetry(rgba: ArrayLike<number>, w: number, h: number, options?: SymmetryOptions): Symmetry[];
|
|
236
|
+
//#endregion
|
|
237
|
+
export type { Cubic, FitOptions, InlayOptions, InlayPath, InlayResult, MirrorSymmetry, PathNode, Pt, Rgb, RotationalSymmetry, Subpath, Symmetry, SymmetryOptions };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,294 @@
|
|
|
1
|
+
//#region src/shapes.ts
|
|
2
|
+
/** How closely an outline must follow a shape: RMS and worst distance, in px, of its pixel-edge midpoints. */
|
|
3
|
+
const MAX_RMS = .3;
|
|
4
|
+
const MAX_OFF = .85;
|
|
5
|
+
/** An ellipse whose axes differ by less than this is drawn as a circle. */
|
|
6
|
+
const ROUND = .015;
|
|
7
|
+
const KAPPA = .5522847498307936;
|
|
8
|
+
/** Area, centroid and central second moments (per unit area) of a closed polygon. */
|
|
9
|
+
function moments(pts) {
|
|
10
|
+
let a = 0;
|
|
11
|
+
let sx = 0;
|
|
12
|
+
let sy = 0;
|
|
13
|
+
let sxx = 0;
|
|
14
|
+
let syy = 0;
|
|
15
|
+
let sxy = 0;
|
|
16
|
+
for (let i = 0; i < pts.length; i++) {
|
|
17
|
+
const p = pts[i];
|
|
18
|
+
const q = pts[(i + 1) % pts.length];
|
|
19
|
+
const c = p.x * q.y - q.x * p.y;
|
|
20
|
+
a += c;
|
|
21
|
+
sx += (p.x + q.x) * c;
|
|
22
|
+
sy += (p.y + q.y) * c;
|
|
23
|
+
sxx += (p.x * p.x + p.x * q.x + q.x * q.x) * c;
|
|
24
|
+
syy += (p.y * p.y + p.y * q.y + q.y * q.y) * c;
|
|
25
|
+
sxy += (p.x * q.y + 2 * p.x * p.y + 2 * q.x * q.y + q.x * p.y) * c;
|
|
26
|
+
}
|
|
27
|
+
a /= 2;
|
|
28
|
+
if (Math.abs(a) < 1) return null;
|
|
29
|
+
const cx = sx / (6 * a);
|
|
30
|
+
const cy = sy / (6 * a);
|
|
31
|
+
return {
|
|
32
|
+
cx,
|
|
33
|
+
cy,
|
|
34
|
+
area: Math.abs(a),
|
|
35
|
+
m20: sxx / (12 * a) - cx * cx,
|
|
36
|
+
m02: syy / (12 * a) - cy * cy,
|
|
37
|
+
m11: sxy / (24 * a) - cx * cy
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** The principal axes: the larger and smaller variance and the angle of the larger. */
|
|
41
|
+
function principal(f) {
|
|
42
|
+
const d = Math.hypot((f.m20 - f.m02) / 2, f.m11);
|
|
43
|
+
const mid = (f.m20 + f.m02) / 2;
|
|
44
|
+
return {
|
|
45
|
+
big: mid + d,
|
|
46
|
+
small: mid - d,
|
|
47
|
+
angle: .5 * Math.atan2(2 * f.m11, f.m20 - f.m02)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** The midpoint of every unit pixel edge of a crack outline: within ~0.35 px of the true edge. */
|
|
51
|
+
function midpoints(pts) {
|
|
52
|
+
const out = [];
|
|
53
|
+
for (let i = 0; i < pts.length; i++) {
|
|
54
|
+
const a = pts[i];
|
|
55
|
+
const b = pts[(i + 1) % pts.length];
|
|
56
|
+
const n = Math.max(1, Math.round(Math.hypot(b.x - a.x, b.y - a.y)));
|
|
57
|
+
for (let s = 0; s < n; s++) out.push({
|
|
58
|
+
x: a.x + (b.x - a.x) * (s + .5) / n,
|
|
59
|
+
y: a.y + (b.y - a.y) * (s + .5) / n
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
/** Whether every point is close to a shape, given its distance function. */
|
|
65
|
+
function fits(points, distance) {
|
|
66
|
+
let sum = 0;
|
|
67
|
+
for (const p of points) {
|
|
68
|
+
const d = Math.abs(distance(p));
|
|
69
|
+
if (d > MAX_OFF) return false;
|
|
70
|
+
sum += d * d;
|
|
71
|
+
}
|
|
72
|
+
return Math.sqrt(sum / points.length) <= MAX_RMS;
|
|
73
|
+
}
|
|
74
|
+
/** Into a frame centred on (cx, cy) and turned by `angle`. */
|
|
75
|
+
const local = (p, cx, cy, cos, sin) => {
|
|
76
|
+
const dx = p.x - cx;
|
|
77
|
+
const dy = p.y - cy;
|
|
78
|
+
return {
|
|
79
|
+
u: dx * cos + dy * sin,
|
|
80
|
+
v: -dx * sin + dy * cos
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
const world = (u, v, cx, cy, cos, sin) => ({
|
|
84
|
+
x: cx + u * cos - v * sin,
|
|
85
|
+
y: cy + u * sin + v * cos
|
|
86
|
+
});
|
|
87
|
+
const lineTo = (a, b) => [
|
|
88
|
+
a,
|
|
89
|
+
{
|
|
90
|
+
x: a.x + (b.x - a.x) / 3,
|
|
91
|
+
y: a.y + (b.y - a.y) / 3
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
x: a.x + (b.x - a.x) * 2 / 3,
|
|
95
|
+
y: a.y + (b.y - a.y) * 2 / 3
|
|
96
|
+
},
|
|
97
|
+
b
|
|
98
|
+
];
|
|
99
|
+
/** A quarter arc of an axis-aligned ellipse in the local frame, from angle t to t + 90°. */
|
|
100
|
+
function quarter(cx, cy, cos, sin, a, b, ox, oy, t) {
|
|
101
|
+
const c0 = Math.cos(t);
|
|
102
|
+
const s0 = Math.sin(t);
|
|
103
|
+
const c1 = Math.cos(t + Math.PI / 2);
|
|
104
|
+
const s1 = Math.sin(t + Math.PI / 2);
|
|
105
|
+
const p = (u, v) => world(ox + u, oy + v, cx, cy, cos, sin);
|
|
106
|
+
return [
|
|
107
|
+
p(a * c0, b * s0),
|
|
108
|
+
p(a * c0 - KAPPA * a * s0, b * s0 + KAPPA * b * c0),
|
|
109
|
+
p(a * c1 + KAPPA * a * s1, b * s1 - KAPPA * b * c1),
|
|
110
|
+
p(a * c1, b * s1)
|
|
111
|
+
];
|
|
112
|
+
}
|
|
113
|
+
const signedArea = (cubics) => cubics.reduce((s, [a, , , d]) => s + a.x * d.y - d.x * a.y, 0);
|
|
114
|
+
const reversed = (cubics) => cubics.map(([a, b, c, d]) => [
|
|
115
|
+
d,
|
|
116
|
+
c,
|
|
117
|
+
b,
|
|
118
|
+
a
|
|
119
|
+
]).reverse();
|
|
120
|
+
/**
|
|
121
|
+
* The exact shape a closed crack outline is, if it is one, as cubics running
|
|
122
|
+
* the same way round as the outline; otherwise null. `corners` are the corner
|
|
123
|
+
* points the fitter found on it (a polygon's vertices; usually none for a round shape).
|
|
124
|
+
*/
|
|
125
|
+
function fitShape(pts, corners) {
|
|
126
|
+
const f = moments(pts);
|
|
127
|
+
if (!f) return null;
|
|
128
|
+
const mids = midpoints(pts);
|
|
129
|
+
const p = principal(f);
|
|
130
|
+
const shape = (corners.length >= 3 ? withCorners(f, corners, mids) : null) ?? roundShape(f, p, mids);
|
|
131
|
+
if (!shape) return null;
|
|
132
|
+
let area = 0;
|
|
133
|
+
for (let i = 0; i < pts.length; i++) area += pts[i].x * pts[(i + 1) % pts.length].y - pts[(i + 1) % pts.length].x * pts[i].y;
|
|
134
|
+
if (Math.sign(signedArea(shape.cubics)) !== Math.sign(area)) shape.cubics = reversed(shape.cubics);
|
|
135
|
+
return shape;
|
|
136
|
+
}
|
|
137
|
+
function roundShape(f, p, mids) {
|
|
138
|
+
const cos = Math.cos(p.angle);
|
|
139
|
+
const sin = Math.sin(p.angle);
|
|
140
|
+
let a = 2 * Math.sqrt(Math.max(0, p.big));
|
|
141
|
+
let b = 2 * Math.sqrt(Math.max(0, p.small));
|
|
142
|
+
if (b >= 2.5) {
|
|
143
|
+
const circle = (a - b) / a < ROUND;
|
|
144
|
+
if (circle) a = b = Math.sqrt(f.area / Math.PI);
|
|
145
|
+
const off = (q) => {
|
|
146
|
+
const { u, v } = local(q, f.cx, f.cy, cos, sin);
|
|
147
|
+
const F = u * u / (a * a) + v * v / (b * b) - 1;
|
|
148
|
+
const g = 2 * Math.hypot(u / (a * a), v / (b * b));
|
|
149
|
+
return g > 1e-12 ? F / g : Infinity;
|
|
150
|
+
};
|
|
151
|
+
if (fits(mids, off)) {
|
|
152
|
+
const cubics = [
|
|
153
|
+
0,
|
|
154
|
+
1,
|
|
155
|
+
2,
|
|
156
|
+
3
|
|
157
|
+
].map((k) => quarter(f.cx, f.cy, cos, sin, a, b, 0, 0, k * Math.PI / 2));
|
|
158
|
+
return {
|
|
159
|
+
kind: circle ? "circle" : "ellipse",
|
|
160
|
+
cubics
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
let hw = 0;
|
|
165
|
+
let hh = 0;
|
|
166
|
+
for (const q of mids) {
|
|
167
|
+
const { u, v } = local(q, f.cx, f.cy, cos, sin);
|
|
168
|
+
hw = Math.max(hw, Math.abs(u));
|
|
169
|
+
hh = Math.max(hh, Math.abs(v));
|
|
170
|
+
}
|
|
171
|
+
const lost = 4 * hw * hh - f.area;
|
|
172
|
+
if (lost <= 0) return null;
|
|
173
|
+
const r = Math.sqrt(lost / (4 - Math.PI));
|
|
174
|
+
if (r < 1.5 || hw - r < 1.5 || hh - r < 1.5) return null;
|
|
175
|
+
const off = (q) => {
|
|
176
|
+
const { u, v } = local(q, f.cx, f.cy, cos, sin);
|
|
177
|
+
const qx = Math.abs(u) - (hw - r);
|
|
178
|
+
const qy = Math.abs(v) - (hh - r);
|
|
179
|
+
return Math.hypot(Math.max(qx, 0), Math.max(qy, 0)) + Math.min(Math.max(qx, qy), 0) - r;
|
|
180
|
+
};
|
|
181
|
+
if (!fits(mids, off)) return null;
|
|
182
|
+
const at = (u, v) => world(u, v, f.cx, f.cy, cos, sin);
|
|
183
|
+
const iw = hw - r;
|
|
184
|
+
const ih = hh - r;
|
|
185
|
+
const cubics = [];
|
|
186
|
+
[
|
|
187
|
+
[
|
|
188
|
+
iw,
|
|
189
|
+
ih,
|
|
190
|
+
0
|
|
191
|
+
],
|
|
192
|
+
[
|
|
193
|
+
-iw,
|
|
194
|
+
ih,
|
|
195
|
+
Math.PI / 2
|
|
196
|
+
],
|
|
197
|
+
[
|
|
198
|
+
-iw,
|
|
199
|
+
-ih,
|
|
200
|
+
Math.PI
|
|
201
|
+
],
|
|
202
|
+
[
|
|
203
|
+
iw,
|
|
204
|
+
-ih,
|
|
205
|
+
3 * Math.PI / 2
|
|
206
|
+
]
|
|
207
|
+
].forEach(([ox, oy, t], k) => {
|
|
208
|
+
const arc = quarter(f.cx, f.cy, cos, sin, r, r, ox, oy, t);
|
|
209
|
+
if (k === 0) cubics.push(lineTo(at(hw, -ih), arc[0]));
|
|
210
|
+
else cubics.push(lineTo(cubics[cubics.length - 1][3], arc[0]));
|
|
211
|
+
cubics.push(arc);
|
|
212
|
+
});
|
|
213
|
+
return {
|
|
214
|
+
kind: "rounded-rectangle",
|
|
215
|
+
cubics
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
function withCorners(f, corners, mids) {
|
|
219
|
+
const k = corners.length;
|
|
220
|
+
if (k < 3 || k > 12) return null;
|
|
221
|
+
const angles = corners.map((c) => Math.atan2(c.y - f.cy, c.x - f.cx));
|
|
222
|
+
if (k === 4) {
|
|
223
|
+
let sx = 0;
|
|
224
|
+
let sy = 0;
|
|
225
|
+
for (let i = 0; i < 4; i++) {
|
|
226
|
+
const a = corners[i];
|
|
227
|
+
const b = corners[(i + 1) % 4];
|
|
228
|
+
const t = 4 * Math.atan2(b.y - a.y, b.x - a.x);
|
|
229
|
+
sx += Math.cos(t);
|
|
230
|
+
sy += Math.sin(t);
|
|
231
|
+
}
|
|
232
|
+
const angle = Math.atan2(sy, sx) / 4;
|
|
233
|
+
const cos = Math.cos(angle);
|
|
234
|
+
const sin = Math.sin(angle);
|
|
235
|
+
const vu = f.m20 * cos * cos + 2 * f.m11 * cos * sin + f.m02 * sin * sin;
|
|
236
|
+
const vv = f.m20 * sin * sin - 2 * f.m11 * cos * sin + f.m02 * cos * cos;
|
|
237
|
+
const hw = Math.sqrt(3 * vu);
|
|
238
|
+
const hh = Math.sqrt(3 * vv);
|
|
239
|
+
if (hw < 1.5 || hh < 1.5) return null;
|
|
240
|
+
const off = (q) => {
|
|
241
|
+
const { u, v } = local(q, f.cx, f.cy, cos, sin);
|
|
242
|
+
const qx = Math.abs(u) - hw;
|
|
243
|
+
const qy = Math.abs(v) - hh;
|
|
244
|
+
return Math.hypot(Math.max(qx, 0), Math.max(qy, 0)) + Math.min(Math.max(qx, qy), 0);
|
|
245
|
+
};
|
|
246
|
+
if (!fits(mids, off)) return null;
|
|
247
|
+
const c = [
|
|
248
|
+
world(hw, -hh, f.cx, f.cy, cos, sin),
|
|
249
|
+
world(hw, hh, f.cx, f.cy, cos, sin),
|
|
250
|
+
world(-hw, hh, f.cx, f.cy, cos, sin),
|
|
251
|
+
world(-hw, -hh, f.cx, f.cy, cos, sin)
|
|
252
|
+
];
|
|
253
|
+
return {
|
|
254
|
+
kind: "rectangle",
|
|
255
|
+
cubics: c.map((a, i) => lineTo(a, c[(i + 1) % 4]))
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
const step = 2 * Math.PI / k;
|
|
259
|
+
const R = Math.sqrt(2 * f.area / (k * Math.sin(step)));
|
|
260
|
+
if (R < 4) return null;
|
|
261
|
+
for (const c of corners) if (Math.abs(Math.hypot(c.x - f.cx, c.y - f.cy) - R) > Math.max(1, .04 * R)) return null;
|
|
262
|
+
let sx = 0;
|
|
263
|
+
let sy = 0;
|
|
264
|
+
for (const a of angles) {
|
|
265
|
+
sx += Math.cos(a * k);
|
|
266
|
+
sy += Math.sin(a * k);
|
|
267
|
+
}
|
|
268
|
+
const phase = Math.atan2(sy, sx) / k;
|
|
269
|
+
const vertices = Array.from({ length: k }, (_, i) => ({
|
|
270
|
+
x: f.cx + R * Math.cos(phase + i * step),
|
|
271
|
+
y: f.cy + R * Math.sin(phase + i * step)
|
|
272
|
+
}));
|
|
273
|
+
const off = (q) => {
|
|
274
|
+
let best = Infinity;
|
|
275
|
+
for (let i = 0; i < k; i++) {
|
|
276
|
+
const a = vertices[i];
|
|
277
|
+
const b = vertices[(i + 1) % k];
|
|
278
|
+
const dx = b.x - a.x;
|
|
279
|
+
const dy = b.y - a.y;
|
|
280
|
+
const t = Math.max(0, Math.min(1, ((q.x - a.x) * dx + (q.y - a.y) * dy) / (dx * dx + dy * dy)));
|
|
281
|
+
best = Math.min(best, Math.hypot(q.x - a.x - t * dx, q.y - a.y - t * dy));
|
|
282
|
+
}
|
|
283
|
+
return best;
|
|
284
|
+
};
|
|
285
|
+
if (!fits(mids, off)) return null;
|
|
286
|
+
return {
|
|
287
|
+
kind: "polygon",
|
|
288
|
+
cubics: vertices.map((a, i) => lineTo(a, vertices[(i + 1) % k]))
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
//#endregion
|
|
1
292
|
//#region src/fit.ts
|
|
2
293
|
const R = 6;
|
|
3
294
|
const END_TRIM = 3;
|
|
@@ -59,6 +350,10 @@ function fitChain(points, closed, o) {
|
|
|
59
350
|
if (n < 2) return [];
|
|
60
351
|
if (!closed && n === 2) return [line(pts[0], pts[1])];
|
|
61
352
|
const corners = findCorners(pts, closed, o.cornerAngle).filter((c) => closed || dist(c.at, pts[0]) >= MIN_GAP && dist(c.at, pts[n - 1]) >= MIN_GAP);
|
|
353
|
+
if (closed && o.shapes) {
|
|
354
|
+
const shape = fitShape(points, corners.map((c) => c.at));
|
|
355
|
+
if (shape) return shape.cubics;
|
|
356
|
+
}
|
|
62
357
|
if (closed && corners.length === 0) {
|
|
63
358
|
if (n < 4) return polygon(pts, true);
|
|
64
359
|
let smoothed = smooth([...pts, pts[0]], true, o.smoothing ?? SMOOTH);
|
|
@@ -1745,4 +2040,187 @@ function toSubpath(cubics) {
|
|
|
1745
2040
|
};
|
|
1746
2041
|
}
|
|
1747
2042
|
//#endregion
|
|
1748
|
-
|
|
2043
|
+
//#region src/symmetry.ts
|
|
2044
|
+
/**
|
|
2045
|
+
* Score every candidate symmetry: the best vertical and horizontal mirror axes
|
|
2046
|
+
* and the best centre for each rotational order, highest score first. A clean
|
|
2047
|
+
* symmetric drawing scores close to 1; around 0.95 is a reasonable bar for
|
|
2048
|
+
* "likely", since JPEG noise and anti-aliasing never match exactly. A round
|
|
2049
|
+
* drawing scores well for every order. Returns `[]` for an image with almost
|
|
2050
|
+
* no foreground.
|
|
2051
|
+
*/
|
|
2052
|
+
function detectSymmetry(rgba, w, h, options = {}) {
|
|
2053
|
+
const tol2 = (options.tolerance ?? 60) ** 2;
|
|
2054
|
+
const orders = options.orders ?? [
|
|
2055
|
+
2,
|
|
2056
|
+
3,
|
|
2057
|
+
4,
|
|
2058
|
+
5,
|
|
2059
|
+
6,
|
|
2060
|
+
7,
|
|
2061
|
+
8
|
|
2062
|
+
];
|
|
2063
|
+
const n = w * h;
|
|
2064
|
+
if (!n) return [];
|
|
2065
|
+
const counts = /* @__PURE__ */ new Map();
|
|
2066
|
+
const border = (i) => {
|
|
2067
|
+
const o = i * 4;
|
|
2068
|
+
const key = rgba[o + 3] < 128 ? -1 : rgba[o] >> 3 << 10 | rgba[o + 1] >> 3 << 5 | rgba[o + 2] >> 3;
|
|
2069
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
2070
|
+
};
|
|
2071
|
+
for (let x = 0; x < w; x++) {
|
|
2072
|
+
border(x);
|
|
2073
|
+
border((h - 1) * w + x);
|
|
2074
|
+
}
|
|
2075
|
+
for (let y = 0; y < h; y++) {
|
|
2076
|
+
border(y * w);
|
|
2077
|
+
border(y * w + w - 1);
|
|
2078
|
+
}
|
|
2079
|
+
let bgKey = -1;
|
|
2080
|
+
let most = -1;
|
|
2081
|
+
for (const [k, c] of counts) if (c > most) {
|
|
2082
|
+
most = c;
|
|
2083
|
+
bgKey = k;
|
|
2084
|
+
}
|
|
2085
|
+
const bg = bgKey < 0 ? null : [
|
|
2086
|
+
(bgKey >> 10 & 31) * 8 + 4,
|
|
2087
|
+
(bgKey >> 5 & 31) * 8 + 4,
|
|
2088
|
+
(bgKey & 31) * 8 + 4
|
|
2089
|
+
];
|
|
2090
|
+
const fg = new Uint8Array(n);
|
|
2091
|
+
let sx = 0;
|
|
2092
|
+
let sy = 0;
|
|
2093
|
+
let count = 0;
|
|
2094
|
+
for (let i = 0; i < n; i++) {
|
|
2095
|
+
const o = i * 4;
|
|
2096
|
+
if (rgba[o + 3] < 128) continue;
|
|
2097
|
+
if (bg) {
|
|
2098
|
+
const dr = rgba[o] - bg[0];
|
|
2099
|
+
const dg = rgba[o + 1] - bg[1];
|
|
2100
|
+
const db = rgba[o + 2] - bg[2];
|
|
2101
|
+
if (dr * dr + dg * dg + db * db < tol2) continue;
|
|
2102
|
+
}
|
|
2103
|
+
fg[i] = 1;
|
|
2104
|
+
sx += i % w;
|
|
2105
|
+
sy += i / w | 0;
|
|
2106
|
+
count++;
|
|
2107
|
+
}
|
|
2108
|
+
if (count < 16) return [];
|
|
2109
|
+
const cx = sx / count + .5;
|
|
2110
|
+
const cy = sy / count + .5;
|
|
2111
|
+
const same = (i, j) => {
|
|
2112
|
+
if (fg[i] !== fg[j]) return false;
|
|
2113
|
+
if (!fg[i]) return true;
|
|
2114
|
+
const a = i * 4;
|
|
2115
|
+
const b = j * 4;
|
|
2116
|
+
const dr = rgba[a] - rgba[b];
|
|
2117
|
+
const dg = rgba[a + 1] - rgba[b + 1];
|
|
2118
|
+
const db = rgba[a + 2] - rgba[b + 2];
|
|
2119
|
+
return dr * dr + dg * dg + db * db < tol2;
|
|
2120
|
+
};
|
|
2121
|
+
/** The share of sampled pixels that match their image under `map` (-1: off the picture). */
|
|
2122
|
+
const score = (map, step) => {
|
|
2123
|
+
let match = 0;
|
|
2124
|
+
let total = 0;
|
|
2125
|
+
for (let y = 0; y < h; y += step) for (let x = 0; x < w; x += step) {
|
|
2126
|
+
const i = y * w + x;
|
|
2127
|
+
const j = map(x, y);
|
|
2128
|
+
if (j < 0) {
|
|
2129
|
+
if (fg[i]) total++;
|
|
2130
|
+
continue;
|
|
2131
|
+
}
|
|
2132
|
+
if (!fg[i] && !fg[j]) continue;
|
|
2133
|
+
total++;
|
|
2134
|
+
if (same(i, j)) match++;
|
|
2135
|
+
}
|
|
2136
|
+
return total ? match / total : 0;
|
|
2137
|
+
};
|
|
2138
|
+
const coarse = Math.max(1, Math.round(Math.sqrt(n / 4e4)));
|
|
2139
|
+
const fine = Math.max(1, Math.round(Math.sqrt(n / 1e5)));
|
|
2140
|
+
const mirror = (orientation, a) => orientation === "vertical" ? (x, y) => {
|
|
2141
|
+
const m = 2 * a - x - 1;
|
|
2142
|
+
return m >= 0 && m < w ? y * w + m : -1;
|
|
2143
|
+
} : (x, y) => {
|
|
2144
|
+
const m = 2 * a - y - 1;
|
|
2145
|
+
return m >= 0 && m < h ? m * w + x : -1;
|
|
2146
|
+
};
|
|
2147
|
+
const rotation = (order, ox, oy) => {
|
|
2148
|
+
const t = 2 * Math.PI / order;
|
|
2149
|
+
const cos = Math.cos(t);
|
|
2150
|
+
const sin = Math.sin(t);
|
|
2151
|
+
return (x, y) => {
|
|
2152
|
+
const px = x + .5 - ox;
|
|
2153
|
+
const py = y + .5 - oy;
|
|
2154
|
+
const rx = Math.floor(ox + px * cos - py * sin);
|
|
2155
|
+
const ry = Math.floor(oy + px * sin + py * cos);
|
|
2156
|
+
return rx >= 0 && rx < w && ry >= 0 && ry < h ? ry * w + rx : -1;
|
|
2157
|
+
};
|
|
2158
|
+
};
|
|
2159
|
+
/** Coarse-score the candidates, then re-score the best few finely. */
|
|
2160
|
+
const best = (candidates, map) => {
|
|
2161
|
+
for (const c of candidates) c.score = score(map(c), coarse);
|
|
2162
|
+
const top = [...candidates].sort((a, b) => b.score - a.score).slice(0, 3);
|
|
2163
|
+
if (fine !== coarse) for (const c of top) c.score = score(map(c), fine);
|
|
2164
|
+
return top.sort((a, b) => b.score - a.score)[0];
|
|
2165
|
+
};
|
|
2166
|
+
const out = [];
|
|
2167
|
+
const reach = Math.max(4, Math.round(Math.min(w, h) / 100));
|
|
2168
|
+
for (const orientation of ["vertical", "horizontal"]) {
|
|
2169
|
+
const centre = Math.round((orientation === "vertical" ? cx : cy) * 2) / 2;
|
|
2170
|
+
const candidates = [];
|
|
2171
|
+
for (let d = -reach; d <= reach; d += .5) candidates.push({
|
|
2172
|
+
kind: "mirror",
|
|
2173
|
+
orientation,
|
|
2174
|
+
position: centre + d,
|
|
2175
|
+
score: 0
|
|
2176
|
+
});
|
|
2177
|
+
out.push(best(candidates, (c) => mirror(c.orientation, c.position)));
|
|
2178
|
+
}
|
|
2179
|
+
const near = Math.max(2, Math.round(reach / 2));
|
|
2180
|
+
const rotations = orders.map((order) => {
|
|
2181
|
+
let top = {
|
|
2182
|
+
kind: "rotation",
|
|
2183
|
+
order,
|
|
2184
|
+
cx,
|
|
2185
|
+
cy,
|
|
2186
|
+
score: -1
|
|
2187
|
+
};
|
|
2188
|
+
for (let dy = -near; dy <= near; dy++) for (let dx = -near; dx <= near; dx++) {
|
|
2189
|
+
const s = score(rotation(order, cx + dx, cy + dy), coarse);
|
|
2190
|
+
if (s > top.score) top = {
|
|
2191
|
+
kind: "rotation",
|
|
2192
|
+
order,
|
|
2193
|
+
cx: cx + dx,
|
|
2194
|
+
cy: cy + dy,
|
|
2195
|
+
score: s
|
|
2196
|
+
};
|
|
2197
|
+
}
|
|
2198
|
+
return top;
|
|
2199
|
+
});
|
|
2200
|
+
rotations.sort((a, b) => b.score - a.score);
|
|
2201
|
+
rotations.forEach((g, k) => {
|
|
2202
|
+
if (k > 1) return out.push(g);
|
|
2203
|
+
let top = g;
|
|
2204
|
+
for (const dy of [
|
|
2205
|
+
-.5,
|
|
2206
|
+
0,
|
|
2207
|
+
.5
|
|
2208
|
+
]) for (const dx of [
|
|
2209
|
+
-.5,
|
|
2210
|
+
0,
|
|
2211
|
+
.5
|
|
2212
|
+
]) {
|
|
2213
|
+
const c = {
|
|
2214
|
+
...g,
|
|
2215
|
+
cx: g.cx + dx,
|
|
2216
|
+
cy: g.cy + dy
|
|
2217
|
+
};
|
|
2218
|
+
c.score = score(rotation(c.order, c.cx, c.cy), fine);
|
|
2219
|
+
if (c.score > top.score || top === g) top = c;
|
|
2220
|
+
}
|
|
2221
|
+
out.push(top);
|
|
2222
|
+
});
|
|
2223
|
+
return out.sort((a, b) => b.score - a.score);
|
|
2224
|
+
}
|
|
2225
|
+
//#endregion
|
|
2226
|
+
export { detectSymmetry, exactPalette, fitChain, isFlat, mapPoints, mergeLabels, oklab, otsu, parsePath, peakPalette, pointAt, quantizePalette, serializePath, staircase, toSvg, trace, traceInlay };
|
package/package.json
CHANGED