ultra-text 1.0.1 → 1.0.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 +224 -1002
- package/dist/assets/{cpuSdf.worker-xS57yQbP.js → cpuSdf.worker--X1Qpmdp.js} +136 -47
- package/dist/assets/cpuSdf.worker--X1Qpmdp.js.map +1 -0
- package/dist/atlas/appendAtlas.js +67 -25
- package/dist/atlas/appendAtlas.js.map +1 -1
- package/dist/atlas/buildAtlas.js +3 -2
- package/dist/atlas/buildAtlas.js.map +1 -1
- package/dist/atlas/cpuAtlas.d.ts +4 -1
- package/dist/atlas/cpuAtlas.js +8 -6
- package/dist/atlas/cpuAtlas.js.map +1 -1
- package/dist/atlas/cpuSdf.js +152 -40
- package/dist/atlas/cpuSdf.js.map +1 -1
- package/dist/atlas/cpuWorker.d.ts +5 -3
- package/dist/atlas/cpuWorker.js +119 -12
- package/dist/atlas/cpuWorker.js.map +1 -1
- package/dist/atlas/gpuAtlas.js +100 -276
- package/dist/atlas/gpuAtlas.js.map +1 -1
- package/dist/atlas/gpuCommon.d.ts +135 -0
- package/dist/atlas/gpuCommon.js +223 -0
- package/dist/atlas/gpuCommon.js.map +1 -0
- package/dist/atlas/jfaWgsl.js +47 -241
- package/dist/atlas/jfaWgsl.js.map +1 -1
- package/dist/atlas/rasterize.js +103 -58
- package/dist/atlas/rasterize.js.map +1 -1
- package/dist/atlas/shapeRaster.js +2 -1
- package/dist/atlas/shapeRaster.js.map +1 -1
- package/dist/atlas/vanillaBounds.d.ts +64 -0
- package/dist/atlas/vanillaBounds.js +42 -0
- package/dist/atlas/vanillaBounds.js.map +1 -0
- package/dist/atlas/vanillaCore.d.ts +100 -0
- package/dist/atlas/vanillaCore.js +1012 -0
- package/dist/atlas/vanillaCore.js.map +1 -0
- package/dist/atlas/vanillaCpuAtlas.d.ts +9 -0
- package/dist/atlas/vanillaCpuAtlas.js +87 -0
- package/dist/atlas/vanillaCpuAtlas.js.map +1 -0
- package/dist/atlas/vanillaExtras.d.ts +8 -0
- package/dist/atlas/vanillaExtras.js +166 -0
- package/dist/atlas/vanillaExtras.js.map +1 -0
- package/dist/atlas/vanillaGpuHelpers.d.ts +232 -0
- package/dist/atlas/vanillaGpuHelpers.js +119 -0
- package/dist/atlas/vanillaGpuHelpers.js.map +1 -0
- package/dist/dom/DomSdf.js +224 -284
- package/dist/dom/DomSdf.js.map +1 -1
- package/dist/dom/DomSlug.js +75 -151
- package/dist/dom/DomSlug.js.map +1 -1
- package/dist/dom/domAnalytic.d.ts +54 -0
- package/dist/dom/domAnalytic.js +282 -0
- package/dist/dom/domAnalytic.js.map +1 -0
- package/dist/dom/domExtras.d.ts +27 -0
- package/dist/dom/domExtras.js +137 -0
- package/dist/dom/domExtras.js.map +1 -0
- package/dist/dom/fontCanvas.js +27 -9
- package/dist/dom/fontCanvas.js.map +1 -1
- package/dist/dom/layoutSignature.js +150 -48
- package/dist/dom/layoutSignature.js.map +1 -1
- package/dist/dom/pretextLayout.js +55 -13
- package/dist/dom/pretextLayout.js.map +1 -1
- package/dist/dom/snapshot.js +369 -204
- package/dist/dom/snapshot.js.map +1 -1
- package/dist/dom/svgCapture.d.ts +8 -1
- package/dist/dom/svgCapture.js +75 -1
- package/dist/dom/svgCapture.js.map +1 -1
- package/dist/dom/tone.js +76 -36
- package/dist/dom/tone.js.map +1 -1
- package/dist/index.js +4 -3
- package/dist/slug/SlugMaterial.d.ts +11 -3
- package/dist/slug/SlugMaterial.js +54 -10
- package/dist/slug/SlugMaterial.js.map +1 -1
- package/dist/slug/coverage.d.ts +16 -12
- package/dist/slug/coverage.js +194 -221
- package/dist/slug/coverage.js.map +1 -1
- package/dist/slug/preprocessCore.d.ts +5 -1
- package/dist/slug/preprocessCore.js +118 -87
- package/dist/slug/preprocessCore.js.map +1 -1
- package/dist/slug/runtime.d.ts +1 -0
- package/dist/slug/runtime.js +90 -44
- package/dist/slug/runtime.js.map +1 -1
- package/dist/slug/standaloneRuntime.d.ts +3 -2
- package/dist/slug/standaloneRuntime.js +75 -40
- package/dist/slug/standaloneRuntime.js.map +1 -1
- package/dist/slug/truetype.js +166 -193
- package/dist/slug/truetype.js.map +1 -1
- package/dist/slug/vanillaCore.d.ts +340 -0
- package/dist/slug/vanillaCore.js +1621 -0
- package/dist/slug/vanillaCore.js.map +1 -0
- package/dist/slug-vanilla.d.ts +13 -364
- package/dist/slug-vanilla.js +5 -1453
- package/dist/slug.d.ts +1 -1
- package/dist/slug.js +6 -6
- package/dist/text/GlyphTextMaterial.d.ts +1 -1
- package/dist/text/GlyphTextMaterial.js +5 -5
- package/dist/text/GlyphTextMaterial.js.map +1 -1
- package/dist/text/SdfTextMaterial.js +7 -5
- package/dist/text/SdfTextMaterial.js.map +1 -1
- package/dist/text/decorations.js +36 -19
- package/dist/text/decorations.js.map +1 -1
- package/dist/text/geometry.d.ts +3 -22
- package/dist/text/geometryUtils.d.ts +11 -1
- package/dist/text/geometryUtils.js +22 -1
- package/dist/text/geometryUtils.js.map +1 -1
- package/dist/text/glyphGeometry.d.ts +9 -0
- package/dist/text/glyphGeometry.js +353 -0
- package/dist/text/glyphGeometry.js.map +1 -0
- package/dist/text/index.d.ts +2 -1
- package/dist/text/rectGeometry.d.ts +13 -0
- package/dist/text/rectGeometry.js +97 -0
- package/dist/text/rectGeometry.js.map +1 -0
- package/dist/text/reflow.d.ts +18 -1
- package/dist/text/reflow.js +108 -38
- package/dist/text/reflow.js.map +1 -1
- package/dist/text/snap.d.ts +1 -0
- package/dist/text/snap.js +26 -0
- package/dist/text/snap.js.map +1 -0
- package/dist/vanilla.d.ts +13 -363
- package/dist/vanilla.js +6 -1473
- package/dist/windfoil/geometry.d.ts +2 -2
- package/dist/windfoil/geometry.js +113 -40
- package/dist/windfoil/geometry.js.map +1 -1
- package/dist/windfoil/mixed.d.ts +1 -0
- package/dist/windfoil/mixed.js +44 -25
- package/dist/windfoil/mixed.js.map +1 -1
- package/dist/windfoil/preprocess.js +2 -2
- package/dist/windfoil/preprocessCore.d.ts +1 -0
- package/dist/windfoil/preprocessCore.js +9 -4
- package/dist/windfoil/preprocessCore.js.map +1 -1
- package/dist/windfoil/runtime.d.ts +3 -1
- package/dist/windfoil/runtime.js +62 -40
- package/dist/windfoil/runtime.js.map +1 -1
- package/dist/windfoil/storage.d.ts +60 -2
- package/dist/windfoil/storage.js +142 -6
- package/dist/windfoil/storage.js.map +1 -1
- package/dist/windfoil/trace/contours.d.ts +5 -0
- package/dist/windfoil/trace/contours.js +167 -43
- package/dist/windfoil/trace/contours.js.map +1 -1
- package/dist/windfoil/trace/fit.d.ts +3 -1
- package/dist/windfoil/trace/fit.js +471 -175
- package/dist/windfoil/trace/fit.js.map +1 -1
- package/dist/windfoil/trace/outline.js +55 -30
- package/dist/windfoil/trace/outline.js.map +1 -1
- package/dist/windfoil/trace/types.d.ts +10 -0
- package/dist/windfoil/tracedOutlines.js +59 -34
- package/dist/windfoil/tracedOutlines.js.map +1 -1
- package/package.json +11 -11
- package/skills/ultra-text/references/three-tsl.md +2 -2
- package/skills/ultra-text/references/vanilla-webgpu.md +3 -2
- package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +0 -1
- package/dist/slug-vanilla.js.map +0 -1
- package/dist/text/geometry.js +0 -348
- package/dist/text/geometry.js.map +0 -1
- package/dist/vanilla.js.map +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/windfoil/trace/fit.ts
|
|
2
|
-
var
|
|
3
|
-
var CORNER_ANGLE =
|
|
2
|
+
var MERGE_STRAIGHT = Math.sin(5 * Math.PI / 180);
|
|
3
|
+
var CORNER_ANGLE = 45 * Math.PI / 180;
|
|
4
4
|
var CORNER_WINDOW = 3;
|
|
5
5
|
var CORNER_SHARPNESS = .72;
|
|
6
6
|
var CORNER_TRIM = 1;
|
|
@@ -9,16 +9,26 @@ var STEP = .5;
|
|
|
9
9
|
var INITIAL_PIECE = 32;
|
|
10
10
|
var MAX_PIECES = 512;
|
|
11
11
|
var SMOOTHING = 1e-4;
|
|
12
|
+
var MERGE_STRAIGHT_SQ = MERGE_STRAIGHT * MERGE_STRAIGHT;
|
|
13
|
+
var CORNER_REACH_SQ = CORNER_REACH * CORNER_REACH;
|
|
12
14
|
var TAN_CORNER_SQ = Math.tan(CORNER_ANGLE - 1e-7) ** 2;
|
|
13
15
|
var mod = (a, n) => (a % n + n) % n;
|
|
14
16
|
var grow = (buf, need) => buf.length < need ? new Float64Array(Math.max(buf.length * 2, need)) : buf;
|
|
17
|
+
var growPreserve = (buf, len, need) => {
|
|
18
|
+
if (buf.length >= need) return buf;
|
|
19
|
+
const grown = new Float64Array(Math.max(buf.length * 2, need, 256));
|
|
20
|
+
if (len > 0) grown.set(buf.subarray(0, len));
|
|
21
|
+
return grown;
|
|
22
|
+
};
|
|
15
23
|
function resampleClosed(points, scratch) {
|
|
16
24
|
const n = points.length >>> 1;
|
|
17
25
|
const cumulative = scratch.matrix = grow(scratch.matrix, n + 1);
|
|
18
26
|
cumulative[0] = 0;
|
|
19
27
|
for (let i = 0; i < n; i++) {
|
|
20
28
|
const j = i + 1 < n ? i + 1 : 0;
|
|
21
|
-
|
|
29
|
+
const dx = points[2 * j] - points[2 * i];
|
|
30
|
+
const dy = points[2 * j + 1] - points[2 * i + 1];
|
|
31
|
+
cumulative[i + 1] = cumulative[i] + Math.sqrt(dx * dx + dy * dy);
|
|
22
32
|
}
|
|
23
33
|
const length = cumulative[n];
|
|
24
34
|
const count = Math.max(8, Math.round(length / STEP));
|
|
@@ -82,7 +92,7 @@ function fitLine(q, count, start, steps, dir) {
|
|
|
82
92
|
function findCorners(q, count, spacing, scratch) {
|
|
83
93
|
const window = Math.min(CORNER_WINDOW, count * spacing / 8);
|
|
84
94
|
const reach = Math.max(2, Math.round(window / spacing));
|
|
85
|
-
const half = Math.max(1, Math.round(reach / 2));
|
|
95
|
+
const half = Math.min(reach - 1, Math.max(1, Math.round(reach / 2)) + 1);
|
|
86
96
|
if (count < 4 * reach) return [];
|
|
87
97
|
const turn = scratch.work.subarray(2 * count, 3 * count);
|
|
88
98
|
for (let i = 0; i < count; i++) turn[i] = turnAt(q, count, i, reach, TAN_CORNER_SQ);
|
|
@@ -115,7 +125,9 @@ function findCorners(q, count, spacing, scratch) {
|
|
|
115
125
|
const t = ((b[0] - a[0]) * b[3] - (b[1] - a[1]) * b[2]) / det;
|
|
116
126
|
const px = a[0] + a[2] * t;
|
|
117
127
|
const py = a[1] + a[3] * t;
|
|
118
|
-
|
|
128
|
+
const dx = px - x;
|
|
129
|
+
const dy = py - y;
|
|
130
|
+
if (dx * dx + dy * dy <= CORNER_REACH_SQ) {
|
|
119
131
|
x = px;
|
|
120
132
|
y = py;
|
|
121
133
|
}
|
|
@@ -140,19 +152,8 @@ function toPoly(q, qo, p, po) {
|
|
|
140
152
|
p[po + 4] = q[qo + 4] - x0 - bx;
|
|
141
153
|
p[po + 5] = q[qo + 5] - y0 - by;
|
|
142
154
|
}
|
|
143
|
-
function
|
|
144
|
-
const dx0 = poly[o] - px;
|
|
145
|
-
const dy0 = poly[o + 1] - py;
|
|
146
|
-
const bx = poly[o + 2];
|
|
147
|
-
const by = poly[o + 3];
|
|
148
|
-
const ax = poly[o + 4];
|
|
149
|
-
const ay = poly[o + 5];
|
|
150
|
-
let t = guess < 0 ? 0 : guess > 1 ? 1 : guess;
|
|
155
|
+
function refineNearQuadraticSq(dx0, dy0, bx, by, ax, ay, t, ex, ey, dSq, cutoffSq) {
|
|
151
156
|
for (let k = 0; k < 3; k++) {
|
|
152
|
-
const ex = (ax * t + bx) * t + dx0;
|
|
153
|
-
const ey = (ay * t + by) * t + dy0;
|
|
154
|
-
const dSq = ex * ex + ey * ey;
|
|
155
|
-
if (dSq <= cutoffSq) return dSq;
|
|
156
157
|
const dx = 2 * ax * t + bx;
|
|
157
158
|
const dy = 2 * ay * t + by;
|
|
158
159
|
const slope = dx * dx + dy * dy + 2 * (ex * ax + ey * ay);
|
|
@@ -161,10 +162,22 @@ function nearQuadraticSq(px, py, poly, o, guess, cutoffSq) {
|
|
|
161
162
|
next = next < 0 ? 0 : next > 1 ? 1 : next;
|
|
162
163
|
if (next === t) return dSq;
|
|
163
164
|
t = next;
|
|
165
|
+
ex = (ax * t + bx) * t + dx0;
|
|
166
|
+
ey = (ay * t + by) * t + dy0;
|
|
167
|
+
dSq = ex * ex + ey * ey;
|
|
168
|
+
if (dSq <= cutoffSq) return dSq;
|
|
164
169
|
}
|
|
170
|
+
return dSq;
|
|
171
|
+
}
|
|
172
|
+
function nearQuadraticSq(px, py, x0, y0, bx, by, ax, ay, guess, cutoffSq) {
|
|
173
|
+
const dx0 = x0 - px;
|
|
174
|
+
const dy0 = y0 - py;
|
|
175
|
+
const t = guess < 0 ? 0 : guess > 1 ? 1 : guess;
|
|
165
176
|
const ex = (ax * t + bx) * t + dx0;
|
|
166
177
|
const ey = (ay * t + by) * t + dy0;
|
|
167
|
-
|
|
178
|
+
const dSq = ex * ex + ey * ey;
|
|
179
|
+
if (dSq <= cutoffSq) return dSq;
|
|
180
|
+
return refineNearQuadraticSq(dx0, dy0, bx, by, ax, ay, t, ex, ey, dSq, cutoffSq);
|
|
168
181
|
}
|
|
169
182
|
function addEntry(matrix, stride, n, i, j, v) {
|
|
170
183
|
const a = i < j ? i : j;
|
|
@@ -185,11 +198,43 @@ function addTriad(matrix, stride, n, i0, i1, i2, w0, w1, w2, scale) {
|
|
|
185
198
|
addEntry(matrix, stride, n, i1, i2, scale * w1 * w2);
|
|
186
199
|
}
|
|
187
200
|
}
|
|
188
|
-
function fitSpline(data, u, uLen, knots, first, last, scratch) {
|
|
189
|
-
|
|
201
|
+
function fitSpline(data, u, uLen, knots, m, first, last, scratch) {
|
|
202
|
+
if (m === 1 && first !== null && last !== null) {
|
|
203
|
+
const fx = first[0];
|
|
204
|
+
const fy = first[1];
|
|
205
|
+
const lx = last[0];
|
|
206
|
+
const ly = last[1];
|
|
207
|
+
const k0 = knots[0];
|
|
208
|
+
const invSpan = 1 / (knots[1] - k0);
|
|
209
|
+
let mCC = 0;
|
|
210
|
+
let rxC = 0;
|
|
211
|
+
let ryC = 0;
|
|
212
|
+
for (let i = 0; i < uLen; i++) {
|
|
213
|
+
let s = (u[i] - k0) * invSpan;
|
|
214
|
+
s = s < 0 ? 0 : s > 1 ? 1 : s;
|
|
215
|
+
const u0 = 1 - s;
|
|
216
|
+
const b0 = u0 * u0;
|
|
217
|
+
const b1 = 2 * s * u0;
|
|
218
|
+
const b2 = s * s;
|
|
219
|
+
rxC += b1 * (data[2 * i] - b0 * fx - b2 * lx);
|
|
220
|
+
ryC += b1 * (data[2 * i + 1] - b0 * fy - b2 * ly);
|
|
221
|
+
mCC += b1 * b1;
|
|
222
|
+
}
|
|
223
|
+
const quads = scratch.quads = grow(scratch.quads, 6);
|
|
224
|
+
const denom = mCC + 1e-9;
|
|
225
|
+
quads[0] = fx;
|
|
226
|
+
quads[1] = fy;
|
|
227
|
+
quads[2] = mCC > 1e-12 ? rxC / denom : .5 * (fx + lx);
|
|
228
|
+
quads[3] = mCC > 1e-12 ? ryC / denom : .5 * (fy + ly);
|
|
229
|
+
quads[4] = lx;
|
|
230
|
+
quads[5] = ly;
|
|
231
|
+
return quads.subarray(0, 6);
|
|
232
|
+
}
|
|
190
233
|
const periodic = first === null;
|
|
191
234
|
const n = periodic ? m - 2 : m;
|
|
192
235
|
const stride = m + 2;
|
|
236
|
+
const d1 = stride;
|
|
237
|
+
const d2 = 2 * stride;
|
|
193
238
|
const bOff = 5 * stride;
|
|
194
239
|
const matLen = periodic ? bOff + 3 : 3 * stride;
|
|
195
240
|
const rhsLen = (periodic ? 4 : 2) * stride;
|
|
@@ -205,64 +250,111 @@ function fitSpline(data, u, uLen, knots, first, last, scratch) {
|
|
|
205
250
|
const after = k < m ? here : periodic ? knots[1] - knots[0] : 0;
|
|
206
251
|
ratio[k] = after / (before + after || 1);
|
|
207
252
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
invSpan = 1 / (knots[piece + 1] - k0);
|
|
217
|
-
}
|
|
218
|
-
let s = (ui - k0) * invSpan;
|
|
219
|
-
s = s < 0 ? 0 : s > 1 ? 1 : s;
|
|
220
|
-
const u0 = 1 - s;
|
|
221
|
-
const b0 = u0 * u0;
|
|
222
|
-
const b1 = 2 * s * u0;
|
|
223
|
-
const b2 = s * s;
|
|
224
|
-
let x = data[2 * i];
|
|
225
|
-
let y = data[2 * i + 1];
|
|
226
|
-
const r0 = ratio[piece];
|
|
227
|
-
const r1 = ratio[piece + 1];
|
|
253
|
+
const fxEnd = first ? first[0] : 0;
|
|
254
|
+
const fyEnd = first ? first[1] : 0;
|
|
255
|
+
const lxEnd = last ? last[0] : 0;
|
|
256
|
+
const lyEnd = last ? last[1] : 0;
|
|
257
|
+
for (let piece = 0, i = 0; piece < m; piece++) {
|
|
258
|
+
const k0 = knots[piece];
|
|
259
|
+
const k1 = knots[piece + 1];
|
|
260
|
+
const invSpan = 1 / (k1 - k0);
|
|
228
261
|
const hasPrev = periodic || piece > 0;
|
|
229
262
|
const hasNext = periodic || piece < m - 1;
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
263
|
+
const r0 = hasPrev ? ratio[piece] : 0;
|
|
264
|
+
const omR0 = hasPrev ? 1 - r0 : 0;
|
|
265
|
+
const r1 = hasNext ? ratio[piece + 1] : 0;
|
|
266
|
+
const omR1 = hasNext ? 1 - r1 : 0;
|
|
267
|
+
const fxW = hasPrev ? 0 : fxEnd;
|
|
268
|
+
const fyW = hasPrev ? 0 : fyEnd;
|
|
269
|
+
const lxW = hasNext ? 0 : lxEnd;
|
|
270
|
+
const lyW = hasNext ? 0 : lyEnd;
|
|
271
|
+
let rxP = 0;
|
|
272
|
+
let ryP = 0;
|
|
273
|
+
let rxC = 0;
|
|
274
|
+
let ryC = 0;
|
|
275
|
+
let rxN = 0;
|
|
276
|
+
let ryN = 0;
|
|
277
|
+
let mPP = 0;
|
|
278
|
+
let mPC = 0;
|
|
279
|
+
let mPN = 0;
|
|
280
|
+
let mCC = 0;
|
|
281
|
+
let mCN = 0;
|
|
282
|
+
let mNN = 0;
|
|
283
|
+
const start = i;
|
|
284
|
+
for (; i < uLen; i++) {
|
|
285
|
+
const ui = u[i];
|
|
286
|
+
if (piece < m - 1 && ui >= k1) break;
|
|
287
|
+
let s = (ui - k0) * invSpan;
|
|
288
|
+
s = s < 0 ? 0 : s > 1 ? 1 : s;
|
|
289
|
+
const u0 = 1 - s;
|
|
290
|
+
const b0 = u0 * u0;
|
|
291
|
+
const b1 = 2 * s * u0;
|
|
292
|
+
const b2 = s * s;
|
|
293
|
+
const x = data[2 * i] - b0 * fxW - b2 * lxW;
|
|
294
|
+
const y = data[2 * i + 1] - b0 * fyW - b2 * lyW;
|
|
295
|
+
const wP = b0 * r0;
|
|
296
|
+
const wC = b1 + b0 * omR0 + b2 * r1;
|
|
297
|
+
const wN = b2 * omR1;
|
|
298
|
+
rxP += wP * x;
|
|
299
|
+
ryP += wP * y;
|
|
300
|
+
rxC += wC * x;
|
|
301
|
+
ryC += wC * y;
|
|
302
|
+
rxN += wN * x;
|
|
303
|
+
ryN += wN * y;
|
|
304
|
+
mPP += wP * wP;
|
|
305
|
+
mPC += wP * wC;
|
|
306
|
+
mPN += wP * wN;
|
|
307
|
+
mCC += wC * wC;
|
|
308
|
+
mCN += wC * wN;
|
|
309
|
+
mNN += wN * wN;
|
|
239
310
|
}
|
|
240
|
-
if (
|
|
241
|
-
|
|
242
|
-
|
|
311
|
+
if (i === start) continue;
|
|
312
|
+
rhs[piece] += rxC;
|
|
313
|
+
rhs[stride + piece] += ryC;
|
|
314
|
+
if (periodic) {
|
|
315
|
+
const prev = piece > 0 ? piece - 1 : m - 1;
|
|
316
|
+
const next = piece + 1 < m ? piece + 1 : 0;
|
|
317
|
+
rhs[prev] += rxP;
|
|
318
|
+
rhs[stride + prev] += ryP;
|
|
319
|
+
rhs[next] += rxN;
|
|
320
|
+
rhs[stride + next] += ryN;
|
|
321
|
+
addEntry(matrix, stride, n, piece, piece, mCC);
|
|
322
|
+
addEntry(matrix, stride, n, prev, prev, mPP);
|
|
323
|
+
addEntry(matrix, stride, n, prev, piece, mPC);
|
|
324
|
+
addEntry(matrix, stride, n, prev, next, mPN);
|
|
325
|
+
addEntry(matrix, stride, n, next, next, mNN);
|
|
326
|
+
addEntry(matrix, stride, n, piece, next, mCN);
|
|
243
327
|
} else {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
328
|
+
matrix[piece] += mCC;
|
|
329
|
+
if (hasPrev) {
|
|
330
|
+
const prev = piece - 1;
|
|
331
|
+
rhs[prev] += rxP;
|
|
332
|
+
rhs[stride + prev] += ryP;
|
|
333
|
+
matrix[prev] += mPP;
|
|
334
|
+
matrix[d1 + prev] += mPC;
|
|
335
|
+
if (hasNext) matrix[d2 + prev] += mPN;
|
|
336
|
+
}
|
|
337
|
+
if (hasNext) {
|
|
338
|
+
const next = piece + 1;
|
|
339
|
+
rhs[next] += rxN;
|
|
340
|
+
rhs[stride + next] += ryN;
|
|
341
|
+
matrix[next] += mNN;
|
|
342
|
+
matrix[d1 + piece] += mCN;
|
|
343
|
+
}
|
|
258
344
|
}
|
|
259
|
-
addTriad(matrix, stride, n, prev, piece, next, wPrev, wCurr, wNext, 1);
|
|
260
345
|
}
|
|
261
|
-
if (m >= 3)
|
|
262
|
-
|
|
346
|
+
if (m >= 3) {
|
|
347
|
+
if (periodic) for (let k = 0; k < m; k++) addTriad(matrix, stride, n, k > 0 ? k - 1 : m - 1, k, k + 1 < m ? k + 1 : 0, 1, -2, 1, SMOOTHING);
|
|
348
|
+
else for (let k = 1, to = m - 1; k < to; k++) {
|
|
349
|
+
matrix[k - 1] += SMOOTHING;
|
|
350
|
+
matrix[k] += 4 * SMOOTHING;
|
|
351
|
+
matrix[k + 1] += SMOOTHING;
|
|
352
|
+
matrix[d1 + k - 1] += -2 * SMOOTHING;
|
|
353
|
+
matrix[d1 + k] += -2 * SMOOTHING;
|
|
354
|
+
matrix[d2 + k - 1] += SMOOTHING;
|
|
355
|
+
}
|
|
356
|
+
} else for (let k = 0; k < m; k++) matrix[k] += 1e-9;
|
|
263
357
|
if (periodic) rhs.set(matrix.subarray(3 * stride, 5 * stride), 2 * stride);
|
|
264
|
-
const d1 = stride;
|
|
265
|
-
const d2 = 2 * stride;
|
|
266
358
|
const uOff = 2 * stride;
|
|
267
359
|
const vOff = 3 * stride;
|
|
268
360
|
for (let k = 0; k < n; k++) {
|
|
@@ -319,7 +411,7 @@ function fitSpline(data, u, uLen, knots, first, last, scratch) {
|
|
|
319
411
|
for (let k = 0; k < n; k++) rhs[aOff + k] -= xu * rhs[uOff + k] + xv * rhs[vOff + k];
|
|
320
412
|
}
|
|
321
413
|
}
|
|
322
|
-
const quads =
|
|
414
|
+
const quads = scratch.quads = grow(scratch.quads, m * 6);
|
|
323
415
|
let sx = periodic ? ratio[0] * rhs[m - 1] + (1 - ratio[0]) * rhs[0] : first[0];
|
|
324
416
|
let sy = periodic ? ratio[0] * rhs[d1 + m - 1] + (1 - ratio[0]) * rhs[d1] : first[1];
|
|
325
417
|
for (let k = 0; k < m; k++) {
|
|
@@ -335,20 +427,32 @@ function fitSpline(data, u, uLen, knots, first, last, scratch) {
|
|
|
335
427
|
quads[o + 4] = sx = ex;
|
|
336
428
|
quads[o + 5] = sy = ey;
|
|
337
429
|
}
|
|
338
|
-
return quads;
|
|
430
|
+
return quads.subarray(0, m * 6);
|
|
339
431
|
}
|
|
340
|
-
function evaluateSpan(data, u, uLen, knots, first, last, tolerance, canEarlySplit, scratch) {
|
|
432
|
+
function evaluateSpan(data, u, uLen, knots, m, first, last, tolerance, canEarlySplit, measureError, scratch) {
|
|
341
433
|
const periodic = first === null;
|
|
342
|
-
const
|
|
343
|
-
const quads = fitSpline(data, u, uLen, knots, first, last, scratch);
|
|
434
|
+
const quads = fitSpline(data, u, uLen, knots, m, first, last, scratch);
|
|
344
435
|
const poly = scratch.rhs = grow(scratch.rhs, m * 6);
|
|
345
436
|
for (let k = 0; k < m; k++) toPoly(quads, k * 6, poly, k * 6);
|
|
346
|
-
const error =
|
|
437
|
+
const error = scratch.error = grow(scratch.error, m);
|
|
438
|
+
error.fill(0, 0, m);
|
|
347
439
|
let piece = -1;
|
|
348
440
|
let worst = 0;
|
|
441
|
+
let willSplit = false;
|
|
349
442
|
const halfTolSq = (tolerance * .5) ** 2;
|
|
350
443
|
const tolSq = tolerance * tolerance;
|
|
351
|
-
let k0 = 0
|
|
444
|
+
let k0 = 0;
|
|
445
|
+
let k1 = 0;
|
|
446
|
+
let invSpan = 0;
|
|
447
|
+
let splittable = false;
|
|
448
|
+
let skipPiece = false;
|
|
449
|
+
let useTolCutoff = false;
|
|
450
|
+
let x0 = 0;
|
|
451
|
+
let y0 = 0;
|
|
452
|
+
let bx = 0;
|
|
453
|
+
let by = 0;
|
|
454
|
+
let ax = 0;
|
|
455
|
+
let ay = 0;
|
|
352
456
|
for (let i = 0; i < uLen; i++) {
|
|
353
457
|
const ui = u[i];
|
|
354
458
|
while (piece < 0 || piece < m - 1 && ui >= k1) {
|
|
@@ -357,25 +461,59 @@ function evaluateSpan(data, u, uLen, knots, first, last, tolerance, canEarlySpli
|
|
|
357
461
|
k1 = knots[piece + 1];
|
|
358
462
|
invSpan = 1 / (k1 - k0);
|
|
359
463
|
splittable = canEarlySplit && k1 - k0 >= 2;
|
|
464
|
+
skipPiece = willSplit && !splittable;
|
|
465
|
+
if (skipPiece) continue;
|
|
466
|
+
useTolCutoff = splittable && (willSplit || !measureError);
|
|
467
|
+
const po = piece * 6;
|
|
468
|
+
x0 = poly[po];
|
|
469
|
+
y0 = poly[po + 1];
|
|
470
|
+
bx = poly[po + 2];
|
|
471
|
+
by = poly[po + 3];
|
|
472
|
+
ax = poly[po + 4];
|
|
473
|
+
ay = poly[po + 5];
|
|
474
|
+
}
|
|
475
|
+
if (skipPiece) {
|
|
476
|
+
if (piece < m - 1) {
|
|
477
|
+
while (i + 1 < uLen && u[i + 1] < k1) i++;
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
break;
|
|
360
481
|
}
|
|
361
482
|
const px = data[2 * i];
|
|
362
483
|
const py = data[2 * i + 1];
|
|
363
484
|
const curErr = error[piece];
|
|
364
|
-
let
|
|
365
|
-
if (
|
|
485
|
+
let t = (ui - k0) * invSpan;
|
|
486
|
+
if (t < 0) t = 0;
|
|
487
|
+
else if (t > 1) t = 1;
|
|
488
|
+
const dx0 = x0 - px;
|
|
489
|
+
const dy0 = y0 - py;
|
|
490
|
+
const gx = (ax * t + bx) * t + dx0;
|
|
491
|
+
const gy = (ay * t + by) * t + dy0;
|
|
492
|
+
const guessSq = gx * gx + gy * gy;
|
|
493
|
+
const skipSq = useTolCutoff ? curErr > tolSq ? curErr : tolSq : curErr;
|
|
494
|
+
if (guessSq <= skipSq) continue;
|
|
495
|
+
let dSq = refineNearQuadraticSq(dx0, dy0, bx, by, ax, ay, t, gx, gy, guessSq, skipSq);
|
|
496
|
+
if (dSq > skipSq) {
|
|
366
497
|
if (dSq > halfTolSq) {
|
|
367
498
|
if (piece > 0 || periodic) {
|
|
368
|
-
const
|
|
499
|
+
const po = (piece > 0 ? piece - 1 : m - 1) * 6;
|
|
500
|
+
const prevSq = nearQuadraticSq(px, py, poly[po], poly[po + 1], poly[po + 2], poly[po + 3], poly[po + 4], poly[po + 5], 1, skipSq);
|
|
369
501
|
if (prevSq < dSq) dSq = prevSq;
|
|
370
502
|
}
|
|
371
|
-
if (dSq >
|
|
372
|
-
const
|
|
503
|
+
if (dSq > skipSq && (piece < m - 1 || periodic)) {
|
|
504
|
+
const po = (piece + 1 < m ? piece + 1 : 0) * 6;
|
|
505
|
+
const nextSq = nearQuadraticSq(px, py, poly[po], poly[po + 1], poly[po + 2], poly[po + 3], poly[po + 4], poly[po + 5], 0, skipSq);
|
|
373
506
|
if (nextSq < dSq) dSq = nextSq;
|
|
374
507
|
}
|
|
375
508
|
}
|
|
376
|
-
if (dSq >
|
|
509
|
+
if (dSq > skipSq) {
|
|
377
510
|
error[piece] = dSq;
|
|
378
|
-
if (splittable && dSq > tolSq
|
|
511
|
+
if (splittable && dSq > tolSq) {
|
|
512
|
+
willSplit = true;
|
|
513
|
+
useTolCutoff = true;
|
|
514
|
+
if (piece < m - 1) while (i + 1 < uLen && u[i + 1] < k1) i++;
|
|
515
|
+
else break;
|
|
516
|
+
}
|
|
379
517
|
}
|
|
380
518
|
}
|
|
381
519
|
}
|
|
@@ -390,114 +528,255 @@ function evaluateSpan(data, u, uLen, knots, first, last, tolerance, canEarlySpli
|
|
|
390
528
|
worst
|
|
391
529
|
};
|
|
392
530
|
}
|
|
393
|
-
function
|
|
531
|
+
function tryMergePair(quads, a, b, out) {
|
|
532
|
+
const ax0 = quads[a];
|
|
533
|
+
const ay0 = quads[a + 1];
|
|
534
|
+
const bx1 = quads[b + 4];
|
|
535
|
+
const by1 = quads[b + 5];
|
|
536
|
+
const d1x = quads[a + 2] - ax0;
|
|
537
|
+
const d1y = quads[a + 3] - ay0;
|
|
538
|
+
const d2x = quads[b + 2] - bx1;
|
|
539
|
+
const d2y = quads[b + 3] - by1;
|
|
540
|
+
const rx = bx1 - ax0;
|
|
541
|
+
const ry = by1 - ay0;
|
|
542
|
+
const chordSq = rx * rx + ry * ry;
|
|
543
|
+
if (!(chordSq > 1e-18)) return 0;
|
|
544
|
+
const chord = Math.sqrt(chordSq);
|
|
545
|
+
const det = d1x * d2y - d1y * d2x;
|
|
546
|
+
out[0] = ax0;
|
|
547
|
+
out[1] = ay0;
|
|
548
|
+
out[4] = bx1;
|
|
549
|
+
out[5] = by1;
|
|
550
|
+
if (Math.abs(det) > 1e-12) {
|
|
551
|
+
const t = (rx * d2y - ry * d2x) / det;
|
|
552
|
+
const v = (rx * d1y - ry * d1x) / det;
|
|
553
|
+
if (t > 1e-6 && v > 1e-6) {
|
|
554
|
+
const cx = ax0 + d1x * t;
|
|
555
|
+
const cy = ay0 + d1y * t;
|
|
556
|
+
const cd1x = cx - ax0;
|
|
557
|
+
const cd1y = cy - ay0;
|
|
558
|
+
const cd2x = cx - bx1;
|
|
559
|
+
const cd2y = cy - by1;
|
|
560
|
+
if (Math.sqrt(cd1x * cd1x + cd1y * cd1y) + Math.sqrt(cd2x * cd2x + cd2y * cd2y) <= 3 * chord) {
|
|
561
|
+
out[2] = cx;
|
|
562
|
+
out[3] = cy;
|
|
563
|
+
return 1;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
if (d1x * rx + d1y * ry > 0 && d2x * rx + d2y * ry < 0 && (d1x * ry - d1y * rx) ** 2 <= MERGE_STRAIGHT_SQ * (d1x * d1x + d1y * d1y) * chordSq && (d2x * ry - d2y * rx) ** 2 <= MERGE_STRAIGHT_SQ * (d2x * d2x + d2y * d2y) * chordSq) {
|
|
568
|
+
out[2] = .5 * (ax0 + bx1);
|
|
569
|
+
out[3] = .5 * (ay0 + by1);
|
|
570
|
+
return 2;
|
|
571
|
+
}
|
|
572
|
+
return 0;
|
|
573
|
+
}
|
|
574
|
+
function spanSliceErrorSq(data, u, from, to, uOrigin, invSpan, c0x, c0y, cbx, cby, cax, cay, budgetSq, errorSq) {
|
|
575
|
+
const len = to - from;
|
|
576
|
+
const mid = len >> 1;
|
|
577
|
+
for (let step = 0; step < len; step++) {
|
|
578
|
+
const i = from + (step === 0 ? mid : step <= mid ? step - 1 : step);
|
|
579
|
+
const px = data[2 * i];
|
|
580
|
+
const py = data[2 * i + 1];
|
|
581
|
+
let t = (u[i] - uOrigin) * invSpan;
|
|
582
|
+
if (t < 0) t = 0;
|
|
583
|
+
else if (t > 1) t = 1;
|
|
584
|
+
const dx0 = c0x - px;
|
|
585
|
+
const dy0 = c0y - py;
|
|
586
|
+
const ex = (cax * t + cbx) * t + dx0;
|
|
587
|
+
const ey = (cay * t + cby) * t + dy0;
|
|
588
|
+
const guessSq = ex * ex + ey * ey;
|
|
589
|
+
if (guessSq <= budgetSq) {
|
|
590
|
+
if (guessSq > errorSq) errorSq = guessSq;
|
|
591
|
+
continue;
|
|
592
|
+
}
|
|
593
|
+
const dSq = refineNearQuadraticSq(dx0, dy0, cbx, cby, cax, cay, t, ex, ey, guessSq, budgetSq);
|
|
594
|
+
if (dSq > errorSq) {
|
|
595
|
+
errorSq = dSq;
|
|
596
|
+
if (errorSq > budgetSq) return errorSq;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return errorSq;
|
|
600
|
+
}
|
|
601
|
+
function fitSpan(data, u, uLen, range, first, last, tolerance, measureError, scratch) {
|
|
602
|
+
if (first !== null && last !== null) {
|
|
603
|
+
const fx = first[0];
|
|
604
|
+
const fy = first[1];
|
|
605
|
+
const lx = last[0];
|
|
606
|
+
const ly = last[1];
|
|
607
|
+
const dx = lx - fx;
|
|
608
|
+
const dy = ly - fy;
|
|
609
|
+
const lenSq = dx * dx + dy * dy;
|
|
610
|
+
if (lenSq > 1e-10 && Math.abs(dx) + Math.abs(dy) >= 1e-6) {
|
|
611
|
+
const invLenSq = 1 / lenSq;
|
|
612
|
+
const tolSq = tolerance * tolerance;
|
|
613
|
+
let maxDistSq = 0;
|
|
614
|
+
for (let i = 0; i < uLen; i++) {
|
|
615
|
+
const px = data[2 * i] - fx;
|
|
616
|
+
const py = data[2 * i + 1] - fy;
|
|
617
|
+
let t = (px * dx + py * dy) * invLenSq;
|
|
618
|
+
if (t < 0) t = 0;
|
|
619
|
+
else if (t > 1) t = 1;
|
|
620
|
+
const ex = px - t * dx;
|
|
621
|
+
const ey = py - t * dy;
|
|
622
|
+
const dSq = ex * ex + ey * ey;
|
|
623
|
+
if (dSq > maxDistSq) {
|
|
624
|
+
maxDistSq = dSq;
|
|
625
|
+
if (maxDistSq > tolSq) break;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
if (maxDistSq <= tolSq) {
|
|
629
|
+
const quads = scratch.quads = grow(scratch.quads, 6);
|
|
630
|
+
quads[0] = fx;
|
|
631
|
+
quads[1] = fy;
|
|
632
|
+
quads[2] = .5 * (fx + lx);
|
|
633
|
+
quads[3] = .5 * (fy + ly);
|
|
634
|
+
quads[4] = lx;
|
|
635
|
+
quads[5] = ly;
|
|
636
|
+
return {
|
|
637
|
+
quads: quads.subarray(0, 6),
|
|
638
|
+
error: Math.sqrt(maxDistSq)
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
394
643
|
const minimum = first === null ? 4 : 1;
|
|
395
|
-
|
|
396
|
-
let knots =
|
|
397
|
-
let
|
|
644
|
+
let m = Math.max(minimum, Math.round(range / INITIAL_PIECE));
|
|
645
|
+
let knots = scratch.knots = grow(scratch.knots, m + 1);
|
|
646
|
+
for (let k = 0; k <= m; k++) knots[k] = k * range / m;
|
|
647
|
+
let fitted = evaluateSpan(data, u, uLen, knots, m, first, last, tolerance, m * 2 <= MAX_PIECES, measureError, scratch);
|
|
398
648
|
for (let round = 0; round < 12 && fitted.worst > tolerance; round++) {
|
|
399
|
-
const
|
|
400
|
-
|
|
649
|
+
const next = scratch.nextKnots = grow(scratch.nextKnots, m * 2 + 1);
|
|
650
|
+
let nextLen = 0;
|
|
651
|
+
next[nextLen++] = knots[0];
|
|
401
652
|
let split = false;
|
|
402
653
|
for (let k = 0; k < m; k++) {
|
|
403
654
|
if (fitted.error[k] > tolerance && knots[k + 1] - knots[k] >= 2) {
|
|
404
|
-
next.
|
|
655
|
+
next[nextLen++] = .5 * (knots[k] + knots[k + 1]);
|
|
405
656
|
split = true;
|
|
406
657
|
}
|
|
407
|
-
next
|
|
658
|
+
next[nextLen++] = knots[k + 1];
|
|
408
659
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
660
|
+
const nextM = nextLen - 1;
|
|
661
|
+
if (!split || nextM > MAX_PIECES) break;
|
|
662
|
+
scratch.knots = next;
|
|
663
|
+
scratch.nextKnots = knots;
|
|
664
|
+
knots = next;
|
|
665
|
+
m = nextM;
|
|
666
|
+
fitted = evaluateSpan(data, u, uLen, knots, m, first, last, tolerance, round + 1 < 12 && m * 2 <= MAX_PIECES, measureError, scratch);
|
|
412
667
|
}
|
|
413
668
|
const quads = fitted.quads;
|
|
414
|
-
|
|
669
|
+
const numPieces = quads.length / 6;
|
|
415
670
|
const bounds = knots;
|
|
416
|
-
let boundLen = bounds.length;
|
|
417
671
|
const budget = Math.max(tolerance, fitted.worst);
|
|
418
672
|
const budgetSq = budget * budget;
|
|
419
673
|
let worst = fitted.worst;
|
|
420
674
|
let low = 0;
|
|
421
|
-
const
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
const
|
|
427
|
-
const
|
|
428
|
-
|
|
429
|
-
const
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
const d2x = quads[b + 2] - bx1;
|
|
434
|
-
const d2y = quads[b + 3] - by1;
|
|
435
|
-
const rx = bx1 - ax0;
|
|
436
|
-
const ry = by1 - ay0;
|
|
437
|
-
const det = d1x * d2y - d1y * d2x;
|
|
438
|
-
const chord = Math.hypot(rx, ry);
|
|
439
|
-
let ok = chord > 1e-9;
|
|
440
|
-
candidate[0] = ax0;
|
|
441
|
-
candidate[1] = ay0;
|
|
442
|
-
candidate[4] = bx1;
|
|
443
|
-
candidate[5] = by1;
|
|
444
|
-
let curved = false;
|
|
445
|
-
if (ok && Math.abs(det) > 1e-12) {
|
|
446
|
-
const t = (rx * d2y - ry * d2x) / det;
|
|
447
|
-
const v = (rx * d1y - ry * d1x) / det;
|
|
448
|
-
if (t > 1e-6 && v > 1e-6) {
|
|
449
|
-
const cx = ax0 + d1x * t;
|
|
450
|
-
const cy = ay0 + d1y * t;
|
|
451
|
-
candidate[2] = cx;
|
|
452
|
-
candidate[3] = cy;
|
|
453
|
-
curved = Math.hypot(cx - ax0, cy - ay0) + Math.hypot(cx - bx1, cy - by1) <= 3 * chord;
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
if (ok && !curved) {
|
|
457
|
-
const l1 = Math.hypot(d1x, d1y);
|
|
458
|
-
const l2 = Math.hypot(d2x, d2y);
|
|
459
|
-
if (Math.abs(d1x * ry - d1y * rx) <= STRAIGHT * l1 * chord && d1x * rx + d1y * ry > 0 && Math.abs(d2x * ry - d2y * rx) <= STRAIGHT * l2 * chord && d2x * rx + d2y * ry < 0) {
|
|
460
|
-
candidate[2] = .5 * (ax0 + bx1);
|
|
461
|
-
candidate[3] = .5 * (ay0 + by1);
|
|
462
|
-
} else ok = false;
|
|
463
|
-
}
|
|
464
|
-
let error = 0;
|
|
465
|
-
if (ok) {
|
|
466
|
-
toPoly(candidate, 0, candPoly, 0);
|
|
467
|
-
const b0 = bounds[k];
|
|
468
|
-
const b2 = bounds[k + 2];
|
|
675
|
+
const cand = scratch.matrix = grow(scratch.matrix, 6);
|
|
676
|
+
let write = 0;
|
|
677
|
+
let read = 1;
|
|
678
|
+
let livePieces = numPieces;
|
|
679
|
+
for (; read < numPieces && livePieces > minimum; read++) {
|
|
680
|
+
const wOff = write * 6;
|
|
681
|
+
const rOff = read * 6;
|
|
682
|
+
let merged = false;
|
|
683
|
+
const mergeKind = tryMergePair(quads, wOff, rOff, cand);
|
|
684
|
+
if (mergeKind !== 0) {
|
|
685
|
+
const b0 = bounds[write];
|
|
686
|
+
const b2 = bounds[read + 1];
|
|
469
687
|
while (low < uLen && u[low] < b0) low++;
|
|
470
688
|
let high = low;
|
|
471
689
|
while (high < uLen && u[high] < b2) high++;
|
|
472
|
-
let errorSq = 0;
|
|
473
690
|
const invSpan = 1 / (b2 - b0);
|
|
474
|
-
const
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
691
|
+
const c0x = cand[0];
|
|
692
|
+
const c0y = cand[1];
|
|
693
|
+
let cbx = 2 * (cand[2] - c0x);
|
|
694
|
+
let cby = 2 * (cand[3] - c0y);
|
|
695
|
+
const cax = cand[4] - c0x - cbx;
|
|
696
|
+
const cay = cand[5] - c0y - cby;
|
|
697
|
+
let errorSq = spanSliceErrorSq(data, u, low, high, b0, invSpan, c0x, c0y, cbx, cby, cax, cay, budgetSq, 0);
|
|
698
|
+
if (errorSq > budgetSq && mergeKind === 1) {
|
|
699
|
+
cand[2] = .5 * (c0x + cand[4]);
|
|
700
|
+
cand[3] = .5 * (c0y + cand[5]);
|
|
701
|
+
cbx = 2 * (cand[2] - c0x);
|
|
702
|
+
cby = 2 * (cand[3] - c0y);
|
|
703
|
+
errorSq = spanSliceErrorSq(data, u, low, high, b0, invSpan, c0x, c0y, cbx, cby, 0, 0, budgetSq, 0);
|
|
704
|
+
}
|
|
705
|
+
if (errorSq <= budgetSq) {
|
|
706
|
+
quads[wOff] = cand[0];
|
|
707
|
+
quads[wOff + 1] = cand[1];
|
|
708
|
+
quads[wOff + 2] = cand[2];
|
|
709
|
+
quads[wOff + 3] = cand[3];
|
|
710
|
+
quads[wOff + 4] = cand[4];
|
|
711
|
+
quads[wOff + 5] = cand[5];
|
|
712
|
+
const err = Math.sqrt(errorSq);
|
|
713
|
+
if (err > worst) worst = err;
|
|
714
|
+
livePieces--;
|
|
715
|
+
merged = true;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
if (!merged) {
|
|
719
|
+
write++;
|
|
720
|
+
if (write !== read) {
|
|
721
|
+
const dst = write * 6;
|
|
722
|
+
quads[dst] = quads[rOff];
|
|
723
|
+
quads[dst + 1] = quads[rOff + 1];
|
|
724
|
+
quads[dst + 2] = quads[rOff + 2];
|
|
725
|
+
quads[dst + 3] = quads[rOff + 3];
|
|
726
|
+
quads[dst + 4] = quads[rOff + 4];
|
|
727
|
+
quads[dst + 5] = quads[rOff + 5];
|
|
728
|
+
bounds[write] = bounds[read];
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (read < numPieces) {
|
|
733
|
+
const tailCount = numPieces - read;
|
|
734
|
+
if (write + 1 !== read) {
|
|
735
|
+
quads.copyWithin((write + 1) * 6, read * 6, numPieces * 6);
|
|
736
|
+
bounds.copyWithin(write + 1, read, numPieces);
|
|
737
|
+
}
|
|
738
|
+
write += tailCount;
|
|
739
|
+
}
|
|
740
|
+
let finalPieces = write + 1;
|
|
741
|
+
bounds[finalPieces] = bounds[numPieces];
|
|
742
|
+
if (first === null && finalPieces > minimum) {
|
|
743
|
+
if (tryMergePair(quads, (finalPieces - 1) * 6, 0, cand) !== 0) {
|
|
744
|
+
const c0x = cand[0];
|
|
745
|
+
const c0y = cand[1];
|
|
746
|
+
const cbx = 2 * (cand[2] - c0x);
|
|
747
|
+
const cby = 2 * (cand[3] - c0y);
|
|
748
|
+
const cax = cand[4] - c0x - cbx;
|
|
749
|
+
const cay = cand[5] - c0y - cby;
|
|
750
|
+
const seamStart = bounds[finalPieces - 1];
|
|
751
|
+
const b1 = bounds[1];
|
|
752
|
+
const invSpan = 1 / (range - seamStart + b1);
|
|
753
|
+
let headEnd = 0;
|
|
754
|
+
while (headEnd < uLen && u[headEnd] < b1) headEnd++;
|
|
755
|
+
let tailStart = uLen;
|
|
756
|
+
while (tailStart > 0 && u[tailStart - 1] >= seamStart) tailStart--;
|
|
757
|
+
let errorSq = spanSliceErrorSq(data, u, tailStart, uLen, seamStart, invSpan, c0x, c0y, cbx, cby, cax, cay, budgetSq, 0);
|
|
758
|
+
if (errorSq <= budgetSq) {
|
|
759
|
+
errorSq = spanSliceErrorSq(data, u, 0, headEnd, seamStart - range, invSpan, c0x, c0y, cbx, cby, cax, cay, budgetSq, errorSq);
|
|
760
|
+
if (errorSq <= budgetSq) {
|
|
761
|
+
quads[0] = cand[0];
|
|
762
|
+
quads[1] = cand[1];
|
|
763
|
+
quads[2] = cand[2];
|
|
764
|
+
quads[3] = cand[3];
|
|
765
|
+
quads[4] = cand[4];
|
|
766
|
+
quads[5] = cand[5];
|
|
767
|
+
const err = Math.sqrt(errorSq);
|
|
768
|
+
if (err > worst) worst = err;
|
|
769
|
+
finalPieces--;
|
|
482
770
|
}
|
|
483
771
|
}
|
|
484
|
-
error = Math.sqrt(errorSq);
|
|
485
772
|
}
|
|
486
|
-
if (ok && error <= budget) {
|
|
487
|
-
quads.set(candidate, a);
|
|
488
|
-
quads.copyWithin(a + 6, a + 12, quadLen);
|
|
489
|
-
quadLen -= 6;
|
|
490
|
-
bounds.copyWithin(k + 1, k + 2, boundLen);
|
|
491
|
-
boundLen--;
|
|
492
|
-
if (error > worst) worst = error;
|
|
493
|
-
} else k++;
|
|
494
773
|
}
|
|
495
774
|
return {
|
|
496
|
-
quads: quads.subarray(0,
|
|
775
|
+
quads: quads.subarray(0, finalPieces * 6),
|
|
497
776
|
error: worst
|
|
498
777
|
};
|
|
499
778
|
}
|
|
500
|
-
function
|
|
779
|
+
function fitClosedPolylineInternal(points, tolerance, scratch, measureError, ownResult) {
|
|
501
780
|
if (points.length < 6) return null;
|
|
502
781
|
const { samples: q, count, length, spacing } = resampleClosed(points, scratch);
|
|
503
782
|
if (!(length > 1e-6)) return null;
|
|
@@ -507,12 +786,13 @@ function fitClosedPolyline(points, tolerance, scratch) {
|
|
|
507
786
|
if (!corners.length) {
|
|
508
787
|
const u = scratch.work.subarray(2 * count, 3 * count);
|
|
509
788
|
for (let i = 0; i < count; i++) u[i] = i * spacing;
|
|
510
|
-
const fitted = fitSpan(q, u, count, length, null, null, tolerance, scratch);
|
|
789
|
+
const fitted = fitSpan(q, u, count, length, null, null, tolerance, measureError, scratch);
|
|
511
790
|
maxError = fitted.error;
|
|
512
|
-
quadratics =
|
|
791
|
+
quadratics = fitted.quads.slice();
|
|
513
792
|
} else {
|
|
514
793
|
const trim = Math.max(1, Math.round(CORNER_TRIM / spacing));
|
|
515
|
-
|
|
794
|
+
let outQuads = scratch.outQuads;
|
|
795
|
+
let outLen = 0;
|
|
516
796
|
const uBuf = scratch.work.subarray(2 * count, 3 * count);
|
|
517
797
|
const dataBuf = scratch.work.subarray(3 * count, 5 * count);
|
|
518
798
|
for (let c = 0; c < corners.length; c++) {
|
|
@@ -521,20 +801,32 @@ function fitClosedPolyline(points, tolerance, scratch) {
|
|
|
521
801
|
const steps = corners.length === 1 ? count : mod(to.index - from.index, count);
|
|
522
802
|
const range = steps * spacing;
|
|
523
803
|
const spanLen = steps - 2 * trim + 1;
|
|
524
|
-
if (spanLen < 2)
|
|
525
|
-
|
|
804
|
+
if (spanLen < 2) {
|
|
805
|
+
outQuads = scratch.outQuads = growPreserve(outQuads, outLen, outLen + 6);
|
|
806
|
+
outQuads[outLen++] = from.x;
|
|
807
|
+
outQuads[outLen++] = from.y;
|
|
808
|
+
outQuads[outLen++] = (from.x + to.x) / 2;
|
|
809
|
+
outQuads[outLen++] = (from.y + to.y) / 2;
|
|
810
|
+
outQuads[outLen++] = to.x;
|
|
811
|
+
outQuads[outLen++] = to.y;
|
|
812
|
+
} else {
|
|
526
813
|
for (let idx = 0, k = trim; idx < spanLen; idx++, k++) {
|
|
527
814
|
const i = (from.index + k) % count;
|
|
528
815
|
dataBuf[2 * idx] = q[2 * i];
|
|
529
816
|
dataBuf[2 * idx + 1] = q[2 * i + 1];
|
|
530
817
|
uBuf[idx] = k * spacing;
|
|
531
818
|
}
|
|
532
|
-
const fitted = fitSpan(dataBuf, uBuf, spanLen, range, [from.x, from.y], [to.x, to.y], tolerance, scratch);
|
|
819
|
+
const fitted = fitSpan(dataBuf, uBuf, spanLen, range, [from.x, from.y], [to.x, to.y], tolerance, measureError, scratch);
|
|
533
820
|
if (fitted.error > maxError) maxError = fitted.error;
|
|
534
|
-
|
|
821
|
+
const fq = fitted.quads;
|
|
822
|
+
const fqLen = fq.length;
|
|
823
|
+
outQuads = scratch.outQuads = growPreserve(outQuads, outLen, outLen + fqLen);
|
|
824
|
+
outQuads.set(fq, outLen);
|
|
825
|
+
outLen += fqLen;
|
|
535
826
|
}
|
|
536
827
|
}
|
|
537
|
-
|
|
828
|
+
const view = outQuads.subarray(0, outLen);
|
|
829
|
+
quadratics = ownResult ? view.slice() : view;
|
|
538
830
|
}
|
|
539
831
|
for (let i = 0, n = quadratics.length; i < n; i += 6) {
|
|
540
832
|
const prev = (i || n) - 6;
|
|
@@ -546,6 +838,10 @@ function fitClosedPolyline(points, tolerance, scratch) {
|
|
|
546
838
|
maxError
|
|
547
839
|
};
|
|
548
840
|
}
|
|
549
|
-
|
|
841
|
+
/** Zero-copy variant for `traceCoverage`; caller consumes `.quadratics` before mutating `scratch`. */
|
|
842
|
+
function fitClosedPolylineScratch(points, tolerance, scratch) {
|
|
843
|
+
return fitClosedPolylineInternal(points, tolerance, scratch, false, false);
|
|
844
|
+
}
|
|
845
|
+
export { fitClosedPolylineScratch };
|
|
550
846
|
|
|
551
847
|
//# sourceMappingURL=fit.js.map
|