@ac-kit/math-grid 0.2.3 → 0.4.0

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.
Files changed (39) hide show
  1. package/CHANGELOG.md +153 -0
  2. package/README.md +100 -3
  3. package/dist/_coverage-contour2.d.ts +48 -0
  4. package/dist/_coverage-contour2.d.ts.map +1 -0
  5. package/dist/_coverage-contour2.js +1670 -0
  6. package/dist/_coverage-contour2.js.map +1 -0
  7. package/dist/_nearest-site-propagation2.d.ts +80 -0
  8. package/dist/_nearest-site-propagation2.d.ts.map +1 -0
  9. package/dist/_nearest-site-propagation2.js +551 -0
  10. package/dist/_nearest-site-propagation2.js.map +1 -0
  11. package/dist/grid-coord2.d.ts +44 -2
  12. package/dist/grid-coord2.d.ts.map +1 -1
  13. package/dist/grid-coord2.js +51 -11
  14. package/dist/grid-coord2.js.map +1 -1
  15. package/dist/grid-pack-dfs.d.ts.map +1 -1
  16. package/dist/grid-pack-dfs.js +4 -2
  17. package/dist/grid-pack-dfs.js.map +1 -1
  18. package/dist/grid2-pixel.d.ts +32 -17
  19. package/dist/grid2-pixel.d.ts.map +1 -1
  20. package/dist/grid2-pixel.js +70 -54
  21. package/dist/grid2-pixel.js.map +1 -1
  22. package/dist/index.d.ts +3 -0
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +3 -0
  25. package/dist/index.js.map +1 -1
  26. package/dist/infinite-grid2.js.map +1 -1
  27. package/dist/shelf-allocator.d.ts +67 -0
  28. package/dist/shelf-allocator.d.ts.map +1 -0
  29. package/dist/shelf-allocator.js +163 -0
  30. package/dist/shelf-allocator.js.map +1 -0
  31. package/dist/signed-distance-from-coverage2.d.ts +84 -0
  32. package/dist/signed-distance-from-coverage2.d.ts.map +1 -0
  33. package/dist/signed-distance-from-coverage2.js +285 -0
  34. package/dist/signed-distance-from-coverage2.js.map +1 -0
  35. package/dist/squared-distance-transform2.d.ts +25 -0
  36. package/dist/squared-distance-transform2.d.ts.map +1 -0
  37. package/dist/squared-distance-transform2.js +123 -0
  38. package/dist/squared-distance-transform2.js.map +1 -0
  39. package/package.json +8 -5
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Signed distance field of an antialiased coverage grid, in texels: negative
3
+ * inside the shape, positive outside, zero on the 50 % contour, magnitudes
4
+ * capped at `range`. Rows are stored contiguously, `index = y · width + x`,
5
+ * texel centres at `(x, y)`.
6
+ *
7
+ * Gustavson & Strand's antialiased Euclidean distance transform (2011, _Pattern
8
+ * Recognition Letters_ 32(2)), seeded with sub-texel contour pieces and
9
+ * extended to straight runs and corners. Each edge texel (coverage strictly
10
+ * between 0 and 255) locates the contour inside it: the normal is the Sobel
11
+ * gradient of the coverage, and the offset from the texel centre inverts the
12
+ * exact area coverage of a straight edge through a unit square at that angle,
13
+ * so the field keeps sub-texel edges instead of snapping to a threshold. Binary
14
+ * transitions (a full texel beside an empty one, as hand-drawn masks have) put
15
+ * the contour midway. The pieces whose 3 × 3 coverage is one straight edge
16
+ * merge into straight runs, each one segment along the line fitted through
17
+ * them; where two runs end next to each other at an angle, the lines cross at a
18
+ * corner's vertex, which the coverage around it then places along the bisector
19
+ * (a sharp tip's crossing is ill-conditioned), the runs end there, and the
20
+ * texels around the vertex that the corner explains take it, so that no piece
21
+ * overshoots it. Every texel then learns its nearest piece of contour, measured
22
+ * to its segments rather than to a point, which would overestimate by up to a
23
+ * quarter texel between samples: from the pieces themselves, each measuring the
24
+ * texels within `range` of it, when that is cheaper (few edge texels, short
25
+ * range); else from its neighbours in two raster sweeps (vector propagation),
26
+ * then along the contour to a nearer piece, which leaves up to about 0.05 texel
27
+ * where pieces of the contour compete (its medial axis). A texel whose 3 × 3
28
+ * coverage is symmetric about it (no gradient) holds no straight edge: when a
29
+ * stripe thinner than a texel through its centre explains that coverage (a
30
+ * hairline, or a thin gap), the stripe's two edges are its contour, so that the
31
+ * stripe's centreline lies half its width inside (or outside, for a gap); else
32
+ * the coverage determines nothing and the contour is taken at its centre. The
33
+ * side of every texel is decided before its magnitude is capped: by its
34
+ * coverage (inside when at least 128), unless a corner it holds or is nearest
35
+ * to, or a stripe it holds, places its centre on the other side. Texels farther
36
+ * than `range` from any contour saturate at the cap.
37
+ *
38
+ * The field is stored as 32-bit floats: magnitudes are capped at the largest
39
+ * 32-bit float not above `range` (`3.4028234663852886e38` for a larger
40
+ * `range`), so the field never exceeds `range` and never overflows to an
41
+ * infinity; below the smallest 32-bit float (`2⁻¹⁴⁹`) only the sign of zero is
42
+ * left.
43
+ *
44
+ * Within 0.1 texel of the analytic distance, up to 5 texels from the edge, on
45
+ * coverage sampled 16 × 16 per texel and rounded to bytes, for discs of radius
46
+ * 4 and more and polygons whose sides are at least 8 texels long and whose
47
+ * corners are at least 20°, convex or concave; and within 0.02 texel, on
48
+ * exact-area coverage rounded to bytes, for stripes from 0.1 to 0.8 texel wide,
49
+ * of ink or a gap, along a row, a column or a diagonal of texel centres.
50
+ * Smaller polygons and sharper tips leave their corners to the straight pieces,
51
+ * up to half a texel off near the vertex; a feature thinner than a texel that
52
+ * crosses texels off their centres (a hairline at another angle) can be up to
53
+ * half a texel off, as one texel's coverage cannot tell where in it such a
54
+ * feature lies.
55
+ *
56
+ * O(width · height) time: a pass over the grid, the runs grown once from the
57
+ * pieces, a corner tried only where two runs end next to each other, then the
58
+ * lesser of O(pieces · range²) and two sweeps that measure only the texels
59
+ * within `range + 2` of the contour. `8 · width · height` doubles of working
60
+ * memory, none allocated when `scratch` is given. Unlike TinySDF's seeding of
61
+ * {@link squaredDistanceTransform2} (a squared sub-texel offset added at the
62
+ * edge texel, half a texel off near the edge), the distance is measured to the
63
+ * contour itself.
64
+ *
65
+ * @example
66
+ * const coverage = new Uint8Array([0, 0, 128, 255, 255]);
67
+ * signedDistanceFromCoverage2(new Float32Array(5), coverage, 5, 1, 4);
68
+ * // [2, 1, 0, -1, -2] within a tenth of a texel
69
+ *
70
+ * @param out - At least `width · height` entries; overwritten.
71
+ * @param coverage - `width · height` bytes, 0 outside to 255 inside.
72
+ * @param range - The most distance kept, in texels, positive and finite.
73
+ * @param scratch - At least `8 · width · height` entries of working memory,
74
+ * overwritten; allocated when omitted.
75
+ * @returns `out`.
76
+ * @throws {RangeError} When a size is not a positive integer, `range` is not
77
+ * positive and finite, `coverage`, `out` or `scratch` is too short, or two of
78
+ * `out`, `coverage` and `scratch` share bytes within the entries the call
79
+ * uses (checked before anything is written). Views over a `SharedArrayBuffer`
80
+ * and over any other shared buffer, a clone of it included, are taken to
81
+ * share memory.
82
+ */
83
+ export declare function signedDistanceFromCoverage2(out: Float32Array, coverage: Uint8Array, width: number, height: number, range: number, scratch?: Float64Array): Float32Array;
84
+ //# sourceMappingURL=signed-distance-from-coverage2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signed-distance-from-coverage2.d.ts","sourceRoot":"","sources":["../src/signed-distance-from-coverage2.ts"],"names":[],"mappings":"AAsBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiFG;AACH,wBAAgB,2BAA2B,CAC1C,GAAG,EAAE,YAAY,EACjB,QAAQ,EAAE,UAAU,EACpB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,YAAY,GACpB,YAAY,CAkKd"}
@@ -0,0 +1,285 @@
1
+ import { describeValue, viewByteLength, viewPrefixesMayOverlap, } from "@ac-kit/core";
2
+ import { edgeResidual_, findContour_, isCorner_, isSiteCell_, locateEdge_, } from "./_coverage-contour2.js";
3
+ import { COVERAGE_STRIDE_, coverageSiteSide_, propagateNearestSites_, REACH_, siteSegmentCount_, splatNearestSites_, } from "./_nearest-site-propagation2.js";
4
+ /**
5
+ * Signed distance field of an antialiased coverage grid, in texels: negative
6
+ * inside the shape, positive outside, zero on the 50 % contour, magnitudes
7
+ * capped at `range`. Rows are stored contiguously, `index = y · width + x`,
8
+ * texel centres at `(x, y)`.
9
+ *
10
+ * Gustavson & Strand's antialiased Euclidean distance transform (2011, _Pattern
11
+ * Recognition Letters_ 32(2)), seeded with sub-texel contour pieces and
12
+ * extended to straight runs and corners. Each edge texel (coverage strictly
13
+ * between 0 and 255) locates the contour inside it: the normal is the Sobel
14
+ * gradient of the coverage, and the offset from the texel centre inverts the
15
+ * exact area coverage of a straight edge through a unit square at that angle,
16
+ * so the field keeps sub-texel edges instead of snapping to a threshold. Binary
17
+ * transitions (a full texel beside an empty one, as hand-drawn masks have) put
18
+ * the contour midway. The pieces whose 3 × 3 coverage is one straight edge
19
+ * merge into straight runs, each one segment along the line fitted through
20
+ * them; where two runs end next to each other at an angle, the lines cross at a
21
+ * corner's vertex, which the coverage around it then places along the bisector
22
+ * (a sharp tip's crossing is ill-conditioned), the runs end there, and the
23
+ * texels around the vertex that the corner explains take it, so that no piece
24
+ * overshoots it. Every texel then learns its nearest piece of contour, measured
25
+ * to its segments rather than to a point, which would overestimate by up to a
26
+ * quarter texel between samples: from the pieces themselves, each measuring the
27
+ * texels within `range` of it, when that is cheaper (few edge texels, short
28
+ * range); else from its neighbours in two raster sweeps (vector propagation),
29
+ * then along the contour to a nearer piece, which leaves up to about 0.05 texel
30
+ * where pieces of the contour compete (its medial axis). A texel whose 3 × 3
31
+ * coverage is symmetric about it (no gradient) holds no straight edge: when a
32
+ * stripe thinner than a texel through its centre explains that coverage (a
33
+ * hairline, or a thin gap), the stripe's two edges are its contour, so that the
34
+ * stripe's centreline lies half its width inside (or outside, for a gap); else
35
+ * the coverage determines nothing and the contour is taken at its centre. The
36
+ * side of every texel is decided before its magnitude is capped: by its
37
+ * coverage (inside when at least 128), unless a corner it holds or is nearest
38
+ * to, or a stripe it holds, places its centre on the other side. Texels farther
39
+ * than `range` from any contour saturate at the cap.
40
+ *
41
+ * The field is stored as 32-bit floats: magnitudes are capped at the largest
42
+ * 32-bit float not above `range` (`3.4028234663852886e38` for a larger
43
+ * `range`), so the field never exceeds `range` and never overflows to an
44
+ * infinity; below the smallest 32-bit float (`2⁻¹⁴⁹`) only the sign of zero is
45
+ * left.
46
+ *
47
+ * Within 0.1 texel of the analytic distance, up to 5 texels from the edge, on
48
+ * coverage sampled 16 × 16 per texel and rounded to bytes, for discs of radius
49
+ * 4 and more and polygons whose sides are at least 8 texels long and whose
50
+ * corners are at least 20°, convex or concave; and within 0.02 texel, on
51
+ * exact-area coverage rounded to bytes, for stripes from 0.1 to 0.8 texel wide,
52
+ * of ink or a gap, along a row, a column or a diagonal of texel centres.
53
+ * Smaller polygons and sharper tips leave their corners to the straight pieces,
54
+ * up to half a texel off near the vertex; a feature thinner than a texel that
55
+ * crosses texels off their centres (a hairline at another angle) can be up to
56
+ * half a texel off, as one texel's coverage cannot tell where in it such a
57
+ * feature lies.
58
+ *
59
+ * O(width · height) time: a pass over the grid, the runs grown once from the
60
+ * pieces, a corner tried only where two runs end next to each other, then the
61
+ * lesser of O(pieces · range²) and two sweeps that measure only the texels
62
+ * within `range + 2` of the contour. `8 · width · height` doubles of working
63
+ * memory, none allocated when `scratch` is given. Unlike TinySDF's seeding of
64
+ * {@link squaredDistanceTransform2} (a squared sub-texel offset added at the
65
+ * edge texel, half a texel off near the edge), the distance is measured to the
66
+ * contour itself.
67
+ *
68
+ * @example
69
+ * const coverage = new Uint8Array([0, 0, 128, 255, 255]);
70
+ * signedDistanceFromCoverage2(new Float32Array(5), coverage, 5, 1, 4);
71
+ * // [2, 1, 0, -1, -2] within a tenth of a texel
72
+ *
73
+ * @param out - At least `width · height` entries; overwritten.
74
+ * @param coverage - `width · height` bytes, 0 outside to 255 inside.
75
+ * @param range - The most distance kept, in texels, positive and finite.
76
+ * @param scratch - At least `8 · width · height` entries of working memory,
77
+ * overwritten; allocated when omitted.
78
+ * @returns `out`.
79
+ * @throws {RangeError} When a size is not a positive integer, `range` is not
80
+ * positive and finite, `coverage`, `out` or `scratch` is too short, or two of
81
+ * `out`, `coverage` and `scratch` share bytes within the entries the call
82
+ * uses (checked before anything is written). Views over a `SharedArrayBuffer`
83
+ * and over any other shared buffer, a clone of it included, are taken to
84
+ * share memory.
85
+ */
86
+ export function signedDistanceFromCoverage2(out, coverage, width, height, range, scratch) {
87
+ if (!Number.isSafeInteger(width) ||
88
+ width <= 0 ||
89
+ !Number.isSafeInteger(height) ||
90
+ height <= 0) {
91
+ throw new RangeError(`width and height must be positive integers, got ${describeValue(width)}×${describeValue(height)}`);
92
+ }
93
+ // compared rather than Number.isFinite, which an engine tier calls with
94
+ // the double boxed
95
+ if (!(range > 0 && range < Infinity)) {
96
+ throw new RangeError(`range must be positive and finite, got ${describeValue(range)}`);
97
+ }
98
+ const count = width * height;
99
+ // the intrinsic lengths: a shadowed `length` cannot pass a short view
100
+ const coverageBytes = viewByteLength(coverage);
101
+ const outBytes = viewByteLength(out);
102
+ if (coverageBytes < count || outBytes < 4 * count) {
103
+ throw new RangeError(`coverage and out must hold ${count} entries, got ${coverageBytes} and ${outBytes / 4}`);
104
+ }
105
+ if (viewPrefixesMayOverlap(coverage, count, out, count * 4)) {
106
+ throw new RangeError("out must not overlap coverage");
107
+ }
108
+ const needed = (2 + COVERAGE_STRIDE_) * count;
109
+ if (scratch === undefined) {
110
+ scratch = new Float64Array(needed);
111
+ }
112
+ else {
113
+ const scratchBytes = viewByteLength(scratch);
114
+ if (scratchBytes < 8 * needed) {
115
+ throw new RangeError(`scratch holds ${scratchBytes / 8} entries, ${needed} needed`);
116
+ }
117
+ if (viewPrefixesMayOverlap(scratch, needed * 8, out, count * 4) ||
118
+ viewPrefixesMayOverlap(scratch, needed * 8, coverage, count)) {
119
+ throw new RangeError("scratch must not overlap out or coverage");
120
+ }
121
+ }
122
+ // scratch: each contour cell's label, then its nearest site's cell
123
+ // [0, count), room for lists, then the squared distance to the nearest
124
+ // site [count, 2 · count), then each cell's site geometry; out holds each
125
+ // edge texel's straight-edge residual until the end
126
+ const sites = 2 * count;
127
+ const siteCount = locateContour_(scratch, out, coverage, width, height);
128
+ const corners = findContour_(scratch, out, coverage, width, height);
129
+ // the corners' holders, as (column, row) pairs that 32-bit floats hold
130
+ // exactly: out is free until the end
131
+ for (let index = 0; index < corners; index += 1) {
132
+ const holder = scratch[count + index];
133
+ const column = holder % width;
134
+ out[2 * index] = column;
135
+ out[2 * index + 1] = (holder - column) / width;
136
+ }
137
+ // each texel within range finds its nearest site, by measuring from each
138
+ // site the texels within range of it while that is cheap (few sites, short
139
+ // range: exact), else by vector propagation through the grid, whose sweeps
140
+ // carry a site through texels at most √2 farther from it than the texel in
141
+ // range (an edge texel's site, within 0.71 of its centre, is always found)
142
+ scratch.fill(Infinity, count, 2 * count);
143
+ const splatReach = Math.max(range, 1);
144
+ const span = 2 * Math.ceil(splatReach + 1) + 1;
145
+ if (siteCount * span * span <= SPLAT_COST_ * count) {
146
+ REACH_[0] = splatReach;
147
+ REACH_[1] = splatReach * splatReach;
148
+ splatNearestSites_(scratch, coverage, out, corners, sites, width, height);
149
+ }
150
+ else {
151
+ // a cell that took a corner holds a copy of its holder's site: only the
152
+ // holder is a site
153
+ for (let cell = 0; cell < count; cell += 1) {
154
+ scratch[cell] =
155
+ isSiteCell_(coverage, cell, width, count) &&
156
+ siteSegmentCount_(scratch, sites, width, cell) > 0 &&
157
+ (!isCorner_(scratch, sites, cell) || scratch[cell] === cell)
158
+ ? cell
159
+ : -1;
160
+ }
161
+ REACH_[0] = range + 2;
162
+ REACH_[1] = (range + 2) * (range + 2);
163
+ propagateNearestSites_(scratch, out, sites, width, height);
164
+ }
165
+ // out holds 32-bit floats: the cap is the largest one not above range, so
166
+ // that no magnitude passes the range and none overflows to infinity
167
+ CAP_[0] = range;
168
+ if (CAP_[0] > range) {
169
+ // the 32-bit float below: cap · (1 − 2⁻²⁴) is exact and rounds to it,
170
+ // but below the normal range, where the step is 2⁻¹⁴⁹, and past the
171
+ // largest float; stored through CAP_, whose 32-bit rounding is
172
+ // Math.fround's, so that the cap stays a double the engine never boxes
173
+ const above = CAP_[0];
174
+ CAP_[0] = above * (1 - 2 ** -24);
175
+ if (above === Infinity) {
176
+ CAP_[0] = 3.4028234663852886e38;
177
+ }
178
+ else if (CAP_[0] === above) {
179
+ CAP_[0] = above - 2 ** -149;
180
+ }
181
+ }
182
+ const cap = CAP_[0];
183
+ const rangeSquared = range * range;
184
+ for (let cell = 0; cell < count; cell += 1) {
185
+ const value = coverage[cell];
186
+ const distanceSquared = scratch[count + cell];
187
+ // the side first, then the magnitude clamped, so that clamping never
188
+ // turns a texel the corner geometry puts inside to the outside
189
+ let side = value >= 128 ? -1 : 1;
190
+ if (value !== 0 && value !== 255) {
191
+ // an edge texel's centre may be inside with less than half its
192
+ // area (near a sharp corner, or on a stripe thinner than a
193
+ // texel): a corner it holds, or one nearest to it, else a stripe
194
+ // it holds, decides by its geometry
195
+ let site = scratch[cell];
196
+ if (isCorner_(scratch, sites, cell)) {
197
+ site = cell;
198
+ }
199
+ let decided = 0;
200
+ let holder = -1;
201
+ if (site >= 0 && isCorner_(scratch, sites, site)) {
202
+ holder = site;
203
+ }
204
+ else {
205
+ // a stripe's own site: an edge's format, its kind set
206
+ const own = sites + COVERAGE_STRIDE_ * cell;
207
+ const fourth = scratch[own + 3];
208
+ const first = scratch[own];
209
+ if (fourth !== fourth && first === first && scratch[own + 4] !== 0) {
210
+ holder = cell;
211
+ }
212
+ }
213
+ if (holder >= 0) {
214
+ // integer arithmetic: a floating-point remainder is a library call
215
+ const y = (cell / width) | 0;
216
+ decided = coverageSiteSide_(scratch, sites, width, holder, cell - y * width, y);
217
+ }
218
+ if (decided !== 0) {
219
+ side = decided;
220
+ }
221
+ }
222
+ let magnitude = cap;
223
+ if (distanceSquared < rangeSquared) {
224
+ const distance = Math.sqrt(distanceSquared);
225
+ if (distance < cap) {
226
+ magnitude = distance;
227
+ }
228
+ }
229
+ out[cell] = side * magnitude;
230
+ }
231
+ return out;
232
+ }
233
+ /**
234
+ * Where measuring from each site the texels within reach of it stops being
235
+ * cheaper than propagating through the grid: sites times squared texels of
236
+ * reach, per texel of the grid (measured on glyphs from 64² to 512²: the
237
+ * measuring wins up to a range of about 64 on a 512² glyph).
238
+ */
239
+ const SPLAT_COST_ = 100;
240
+ /**
241
+ * The cap of the field's magnitudes, as a 32-bit float: storing into it rounds
242
+ * as `Math.fround` does, and reading it back gives a double the engine keeps
243
+ * unboxed.
244
+ */
245
+ const CAP_ = new Float32Array(1);
246
+ /**
247
+ * Writes the site geometry of every contour cell of the coverage grid (see
248
+ * {@link COVERAGE_STRIDE_}), each antialiased texel's straight-edge residual
249
+ * into `residuals`, and labels each contour cell unassigned (`-1`) in
250
+ * `scratch[cell]`; the other cells are left as they are. Returns how many
251
+ * contour cells there are.
252
+ */
253
+ function locateContour_(scratch, residuals, coverage, width, height) {
254
+ const sites = 2 * width * height;
255
+ let siteCount = 0;
256
+ for (let y = 0; y < height; y += 1) {
257
+ for (let x = 0; x < width; x += 1) {
258
+ const cell = y * width + x;
259
+ const value = coverage[cell];
260
+ const at = sites + COVERAGE_STRIDE_ * cell;
261
+ if (value === 0 || value === 255) {
262
+ const opposite = 255 - value;
263
+ const right = x + 1 < width && coverage[cell + 1] === opposite;
264
+ const down = y + 1 < height && coverage[cell + width] === opposite;
265
+ if (!right && !down) {
266
+ continue;
267
+ }
268
+ // the flag's sign is the direction of the outward normal
269
+ const outward = value === 255 ? 1 : -1;
270
+ scratch[at] = Number.NaN;
271
+ scratch[at + 1] = right ? outward : 0;
272
+ scratch[at + 2] = down ? outward : 0;
273
+ scratch[at + 3] = Number.NaN;
274
+ }
275
+ else {
276
+ locateEdge_(scratch, at, coverage, value, x, y, width, height);
277
+ edgeResidual_(scratch, at, coverage, x, y, width, height, residuals, cell);
278
+ }
279
+ scratch[cell] = -1;
280
+ siteCount += 1;
281
+ }
282
+ }
283
+ return siteCount;
284
+ }
285
+ //# sourceMappingURL=signed-distance-from-coverage2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signed-distance-from-coverage2.js","sourceRoot":"","sources":["../src/signed-distance-from-coverage2.ts"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AAC,GAAG,MAAM,CAAC,GAAG,CAAC;YAC9B,CAAC;iBAAM,CAAC;gBACP,WAAW,CAAC,OAAO,EAAE,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;gBAC/D,aAAa,CACZ,OAAO,EACP,EAAE,EACF,QAAQ,EACR,CAAC,EACD,CAAC,EACD,KAAK,EACL,MAAM,EACN,SAAS,EACT,IAAI,CACJ,CAAC;YACH,CAAC;YACD,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;YACnB,SAAS,IAAI,CAAC,CAAC;QAChB,CAAC;IACF,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Felzenszwalb–Huttenlocher (2012) exact distance transform of a sampled
3
+ * function, separable, O(width · height): each cell holds its seed `f(q)` — 0
4
+ * at a feature, `Infinity` far from any, or any other non-negative cost — and
5
+ * becomes `min over q of (|p − q|² + f(q))`, in cell units, exactly. Rows are
6
+ * stored contiguously, `grid[y * width + x]`.
7
+ *
8
+ * Features are cell centres. A seed cannot place a feature between centres:
9
+ * adding a squared sub-texel offset to the squared distance of a cell
10
+ * (TinySDF's seeding from antialiased coverage) is up to half a texel off near
11
+ * the edge; {@link signedDistanceFromCoverage2} measures to the sub-texel
12
+ * contour itself, in O(width · height) too.
13
+ *
14
+ * In place; `scratch` (at least `3 · max(width, height) + 1` entries) avoids
15
+ * the allocation of a working buffer. A grid with no finite seed stays
16
+ * `Infinity`.
17
+ *
18
+ * @param grid - The seeds, at least `width · height` entries; overwritten.
19
+ * @returns `grid`.
20
+ * @throws {RangeError} When a size is not a positive integer, when `grid` or
21
+ * `scratch` is too short, when `scratch` shares bytes with the first `width ·
22
+ * height` cells of `grid`, or when a seed is `NaN` or negative.
23
+ */
24
+ export declare function squaredDistanceTransform2(grid: Float64Array, width: number, height: number, scratch?: Float64Array): Float64Array;
25
+ //# sourceMappingURL=squared-distance-transform2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"squared-distance-transform2.d.ts","sourceRoot":"","sources":["../src/squared-distance-transform2.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,yBAAyB,CACxC,IAAI,EAAE,YAAY,EAClB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,YAAY,GACpB,YAAY,CAoDd"}
@@ -0,0 +1,123 @@
1
+ import { describeValue } from "@ac-kit/core";
2
+ /**
3
+ * Felzenszwalb–Huttenlocher (2012) exact distance transform of a sampled
4
+ * function, separable, O(width · height): each cell holds its seed `f(q)` — 0
5
+ * at a feature, `Infinity` far from any, or any other non-negative cost — and
6
+ * becomes `min over q of (|p − q|² + f(q))`, in cell units, exactly. Rows are
7
+ * stored contiguously, `grid[y * width + x]`.
8
+ *
9
+ * Features are cell centres. A seed cannot place a feature between centres:
10
+ * adding a squared sub-texel offset to the squared distance of a cell
11
+ * (TinySDF's seeding from antialiased coverage) is up to half a texel off near
12
+ * the edge; {@link signedDistanceFromCoverage2} measures to the sub-texel
13
+ * contour itself, in O(width · height) too.
14
+ *
15
+ * In place; `scratch` (at least `3 · max(width, height) + 1` entries) avoids
16
+ * the allocation of a working buffer. A grid with no finite seed stays
17
+ * `Infinity`.
18
+ *
19
+ * @param grid - The seeds, at least `width · height` entries; overwritten.
20
+ * @returns `grid`.
21
+ * @throws {RangeError} When a size is not a positive integer, when `grid` or
22
+ * `scratch` is too short, when `scratch` shares bytes with the first `width ·
23
+ * height` cells of `grid`, or when a seed is `NaN` or negative.
24
+ */
25
+ export function squaredDistanceTransform2(grid, width, height, scratch) {
26
+ if (!Number.isSafeInteger(width) ||
27
+ width <= 0 ||
28
+ !Number.isSafeInteger(height) ||
29
+ height <= 0) {
30
+ throw new RangeError(`width and height must be positive integers, got ${describeValue(width)}×${describeValue(height)}`);
31
+ }
32
+ const cellCount = width * height;
33
+ if (grid.length < cellCount) {
34
+ throw new RangeError(`grid holds ${grid.length} cells, ${cellCount} needed`);
35
+ }
36
+ const longest = Math.max(width, height);
37
+ const needed = 3 * longest + 1;
38
+ if (scratch === undefined) {
39
+ scratch = new Float64Array(needed);
40
+ }
41
+ else {
42
+ if (scratch.length < needed) {
43
+ throw new RangeError(`scratch holds ${scratch.length} entries, ${needed} needed`);
44
+ }
45
+ if (scratch.buffer === grid.buffer &&
46
+ scratch.byteOffset < grid.byteOffset + cellCount * 8 &&
47
+ grid.byteOffset < scratch.byteOffset + needed * 8) {
48
+ throw new RangeError("scratch must not overlap grid");
49
+ }
50
+ }
51
+ for (let index = 0; index < cellCount; index += 1) {
52
+ const seed = grid[index];
53
+ if (!(seed >= 0)) {
54
+ throw new RangeError(`seed ${seed} at cell ${index} is not a non-negative number`);
55
+ }
56
+ }
57
+ for (let x = 0; x < width; x += 1) {
58
+ transformLine_(grid, x, width, height, scratch);
59
+ }
60
+ for (let y = 0; y < height; y += 1) {
61
+ transformLine_(grid, y * width, 1, width, scratch);
62
+ }
63
+ return grid;
64
+ }
65
+ /**
66
+ * One-dimensional lower envelope of parabolas over `count` cells `stride`
67
+ * apart. Parabolas of infinite seeds are skipped, which avoids the `Infinity −
68
+ * Infinity` of the textbook formulation. O(count).
69
+ */
70
+ function transformLine_(grid, offset, stride, count, scratch) {
71
+ // scratch layout: seeds [0, count), parabola positions [count, 2·count),
72
+ // boundaries [2·count, 3·count].
73
+ const positions = count;
74
+ const boundaries = 2 * count;
75
+ let last = -1;
76
+ for (let q = 0; q < count; q += 1) {
77
+ const seed = grid[offset + q * stride];
78
+ scratch[q] = seed;
79
+ if (seed === Infinity) {
80
+ continue;
81
+ }
82
+ if (last < 0) {
83
+ last = 0;
84
+ scratch[positions] = q;
85
+ scratch[boundaries] = -Infinity;
86
+ scratch[boundaries + 1] = Infinity;
87
+ continue;
88
+ }
89
+ let start;
90
+ for (;;) {
91
+ const p = scratch[positions + last];
92
+ // Intersection of two parabolas, written without the sum of a seed
93
+ // and a square so that seeds near MAX_VALUE cannot overflow.
94
+ start = (seed - scratch[p]) / (2 * (q - p)) + (q + p) / 2;
95
+ if (start > scratch[boundaries + last]) {
96
+ break;
97
+ }
98
+ last -= 1;
99
+ if (last < 0) {
100
+ // Every earlier parabola is dominated: the new one starts the envelope.
101
+ start = -Infinity;
102
+ break;
103
+ }
104
+ }
105
+ last += 1;
106
+ scratch[positions + last] = q;
107
+ scratch[boundaries + last] = start;
108
+ scratch[boundaries + last + 1] = Infinity;
109
+ }
110
+ if (last < 0) {
111
+ return;
112
+ }
113
+ let current = 0;
114
+ for (let q = 0; q < count; q += 1) {
115
+ while (scratch[boundaries + current + 1] < q) {
116
+ current += 1;
117
+ }
118
+ const p = scratch[positions + current];
119
+ const delta = q - p;
120
+ grid[offset + q * stride] = delta * delta + scratch[p];
121
+ }
122
+ }
123
+ //# sourceMappingURL=squared-distance-transform2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"squared-distance-transform2.js","sourceRoot":"","sources":["../src/squared-distance-transform2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,UAAU,yBAAyB,CACxC,IAAkB,EAClB,KAAa,EACb,MAAc,EACd,OAAsB;IAEtB,IACC,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC;QAC5B,KAAK,IAAI,CAAC;QACV,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC;QAC7B,MAAM,IAAI,CAAC,EACV,CAAC;QACF,MAAM,IAAI,UAAU,CACnB,mDAAmD,aAAa,CAAC,KAAK,CAAC,IAAI,aAAa,CAAC,MAAM,CAAC,EAAE,CAClG,CAAC;IACH,CAAC;IACD,MAAM,SAAS,GAAG,KAAK,GAAG,MAAM,CAAC;IACjC,IAAI,IAAI,CAAC,MAAM,GAAG,SAAS,EAAE,CA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AC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ac-kit/math-grid",
3
- "version": "0.2.3",
3
+ "version": "0.4.0",
4
4
  "description": "Discrete two-dimensional integer lattices: grid coordinates, storage strategies, and packing and tiling algorithms.",
5
5
  "keywords": [
6
6
  "2d",
@@ -45,15 +45,18 @@
45
45
  "prepack": "yarn build"
46
46
  },
47
47
  "dependencies": {
48
- "@ac-kit/algo": "^0.4.0",
49
- "@ac-kit/math-geometry": "^0.2.2",
50
- "@ac-kit/math-linear": "^0.2.0",
48
+ "@ac-kit/algo": "^0.6.0",
49
+ "@ac-kit/core": "^0.13.0",
50
+ "@ac-kit/math-geometry": "^0.4.0",
51
+ "@ac-kit/math-linear": "^0.4.0",
51
52
  "@types/web": "^0.0.367"
52
53
  },
53
54
  "devDependencies": {
55
+ "@ac-kit/math-random": "0.4.1",
56
+ "@ac-kit/test-util": "0.5.0",
54
57
  "@ac-kit/tsconfig": "0.1.0",
55
58
  "@ac-kit/typedoc-config": "0.1.0",
56
- "@ac-kit/vitest-config": "0.1.1",
59
+ "@ac-kit/vitest-config": "0.2.0",
57
60
  "@typescript/native": "typescript@7.0.2",
58
61
  "rimraf": "6.1.4",
59
62
  "typedoc": "0.28.20",