@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
package/CHANGELOG.md CHANGED
@@ -1,5 +1,158 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 0b4fe43: `signedDistanceFromCoverage2(out, coverage, width, height, range, scratch?)`
8
+ measures every texel's distance to the contour located inside each edge texel
9
+ from the coverage, merged into straight runs with corners where two runs meet at
10
+ an angle (Gustavson and Strand's antialiased Euclidean distance transform). The
11
+ field is within 0.1 texel of the analytic distance, up to 5 texels from the
12
+ edge, on discs of radius 4 and more (6 before) and on polygons whose sides are
13
+ at least 8 texels long and whose corners are at least 20° (coverage sampled
14
+ 16 × 16 per texel, rounded to bytes); a stripe thinner than a texel along a
15
+ row, a column or a diagonal of texel centres, of ink or a gap, keeps its
16
+ centreline half its width deep, within 0.02 texel. A feature thinner than a
17
+ texel that crosses texels off their centres can be up to half a texel off. A
18
+ texel's side is decided before its magnitude is capped, by the corner or stripe
19
+ that places it where its coverage alone would not. Magnitudes are capped at the
20
+ largest 32-bit float not above `range`, so the field never passes `range` and
21
+ stays finite for any finite `range`. It runs in time linear in the edge texels,
22
+ then the lesser of O(edge texels · range²) and O(width · height). With a
23
+ `scratch` of at least `8 · width · height` entries it allocates nothing, at any
24
+ range. Two of `out`, `coverage` and `scratch` sharing bytes within the entries
25
+ the call uses throw a `RangeError` before anything is written, shadowed
26
+ `byteOffset`, `byteLength` or `buffer` properties included.
27
+
28
+ ### Patch Changes
29
+
30
+ - 0b4fe43: Instance state that was held in `#` private fields is now held in
31
+ TypeScript-private properties (named with a trailing `_`). They are no longer
32
+ hidden at runtime: they appear as own properties (`Object.keys`,
33
+ `JSON.stringify`, `structuredClone`, deep equality), and the declarations list
34
+ them as `private`. Public members are unchanged.
35
+ - Updated dependencies [0b4fe43]
36
+ - Updated dependencies [0b4fe43]
37
+ - Updated dependencies [0b4fe43]
38
+ - Updated dependencies [0b4fe43]
39
+ - Updated dependencies [0b4fe43]
40
+ - Updated dependencies [0b4fe43]
41
+ - Updated dependencies [0b4fe43]
42
+ - Updated dependencies [0b4fe43]
43
+ - Updated dependencies [0b4fe43]
44
+ - Updated dependencies [0b4fe43]
45
+ - Updated dependencies [0b4fe43]
46
+ - Updated dependencies [0b4fe43]
47
+ - Updated dependencies [0b4fe43]
48
+ - Updated dependencies [0b4fe43]
49
+ - Updated dependencies [0b4fe43]
50
+ - Updated dependencies [0b4fe43]
51
+ - Updated dependencies [0b4fe43]
52
+ - Updated dependencies [0b4fe43]
53
+ - Updated dependencies [0b4fe43]
54
+ - Updated dependencies [0b4fe43]
55
+ - Updated dependencies [0b4fe43]
56
+ - Updated dependencies [0b4fe43]
57
+ - Updated dependencies [0b4fe43]
58
+ - Updated dependencies [0b4fe43]
59
+ - Updated dependencies [0b4fe43]
60
+ - Updated dependencies [0b4fe43]
61
+ - Updated dependencies [0b4fe43]
62
+ - Updated dependencies [0b4fe43]
63
+ - @ac-kit/algo@0.6.0
64
+ - @ac-kit/core@0.13.0
65
+ - @ac-kit/math-geometry@0.4.0
66
+ - @ac-kit/math-linear@0.4.0
67
+
68
+ ## 0.3.0
69
+
70
+ ### Minor Changes
71
+
72
+ - 34e8cfa: `DistanceType` and the `type` argument of `gridCoord2Distance` are removed:
73
+ `gridCoord2Distance(a, b)` is the Euclidean distance, and
74
+ `gridCoord2DistanceManhattan` and `gridCoord2DistanceChebyshev` are the others.
75
+ `gridCoord2Distance(a, b, "manhattan")` becomes
76
+ `gridCoord2DistanceManhattan(a, b)`; code that chose the measure at run time
77
+ passes the function itself, typed as `@ac-kit/core`'s `Distance<GridCoord2>`.
78
+ `@ac-kit/math-grid` now depends on `@ac-kit/core` at run time.
79
+
80
+ The conversions to and from continuous space take and return
81
+ `@ac-kit/math-linear` vectors and `@ac-kit/math-geometry` boxes: the twelve
82
+ `gridCoord2ToPoint2`, `point2ToGridCoord2`, `gridSize2ToSize2`,
83
+ `size2ToGridSize2`, `gridRect2ToRect2`, `rect2ToGridRect2`,
84
+ `gridCoord2ToPixelPoint2`, `gridSize2ToPixelSize2`, `gridRect2ToPixelRect2`,
85
+ `pixelPoint2ToGridCoord2`, `pixelSize2ToGridSize2` and `pixelRect2ToGridRect2`
86
+ become `vec2FromGridCoord2(out, coord, cellSize?)`,
87
+ `vec2FromGridSize2(out, size, cellSize?)`, `box2FromGridRect2(out, rect,
88
+ cellSize?)`, `gridCoord2FromVec2(point, cellSize?)`,
89
+ `gridSize2FromVec2(size, cellSize?)` and `gridRect2FromBox2(box, cellSize?)`,
90
+ the cell size being a `Vec2` and omitted meaning unit cells. A pixel rectangle
91
+ `{ origin, size }` becomes the box `[x, y, x + width, y + height]`.
92
+ - 34e8cfa: `ShelfAllocator` allocates and releases rectangles one by one in a fixed area,
93
+ on shelves of similar height, for texture atlases: `allocate(width, height)`
94
+ returns a top-left and an id, or `undefined` when nothing fits; `release(id)`
95
+ frees it (a stale, unknown or cleared id throws a `RangeError`; ids are never
96
+ reused), at once and merged with neighbouring free space, so an atlas can evict
97
+ entries one by one; `clear()` and `occupancy` complete it. The constructor
98
+ throws a `RangeError` when `width · height` is not a safe integer. `allocate`
99
+ is O(shelves + free spans of a shelf), `release` O(free spans of a shelf).
100
+ `squaredDistanceTransform2(grid, width, height, scratch?)` replaces a grid of
101
+ seeds (0 at a feature, `Infinity` far from any, a squared sub-texel distance at
102
+ an edge) with the exact squared Euclidean distance to the nearest seed, in
103
+ place and in O(width · height), without allocation when given a `scratch` of at
104
+ least `3 · max(width, height) + 1` entries. A `NaN` or negative seed, or a
105
+ `scratch` sharing bytes with the grid, throws a `RangeError`; seeds up to
106
+ `Number.MAX_VALUE` are handled.
107
+ - 34e8cfa: `signedDistanceFromCoverage2(out, coverage, width, height, range)` writes the
108
+ signed distance field, in texels, of an antialiased coverage grid (bytes, 0
109
+ outside to 255 inside): negative inside, zero on the 50 % contour, capped at
110
+ `range`. Each edge texel's contour is located from the coverage gradient and
111
+ the exact coverage of a straight edge, so the field is within 0.1 texel of the
112
+ analytic distance on discs of radius 6 and more, where seeding
113
+ `squaredDistanceTransform2` from coverage is 0.5 texel off. O(edge texels ·
114
+ range²), no allocation; a bad size, a non-positive `range`, a short buffer or
115
+ an `out` that shares bytes with the coverage it reads throws a `RangeError`
116
+ (views over shared memory, a `SharedArrayBuffer` and its clones, count as
117
+ sharing).
118
+
119
+ ### Patch Changes
120
+
121
+ - Updated dependencies [34e8cfa]
122
+ - Updated dependencies [34e8cfa]
123
+ - Updated dependencies [34e8cfa]
124
+ - Updated dependencies [34e8cfa]
125
+ - Updated dependencies [34e8cfa]
126
+ - Updated dependencies [34e8cfa]
127
+ - Updated dependencies [34e8cfa]
128
+ - Updated dependencies [34e8cfa]
129
+ - Updated dependencies [34e8cfa]
130
+ - Updated dependencies [34e8cfa]
131
+ - Updated dependencies [34e8cfa]
132
+ - Updated dependencies [34e8cfa]
133
+ - Updated dependencies [34e8cfa]
134
+ - Updated dependencies [34e8cfa]
135
+ - Updated dependencies [34e8cfa]
136
+ - Updated dependencies [34e8cfa]
137
+ - Updated dependencies [34e8cfa]
138
+ - Updated dependencies [34e8cfa]
139
+ - Updated dependencies [34e8cfa]
140
+ - Updated dependencies [34e8cfa]
141
+ - Updated dependencies [34e8cfa]
142
+ - Updated dependencies [34e8cfa]
143
+ - Updated dependencies [34e8cfa]
144
+ - Updated dependencies [34e8cfa]
145
+ - Updated dependencies [34e8cfa]
146
+ - Updated dependencies [34e8cfa]
147
+ - Updated dependencies [34e8cfa]
148
+ - Updated dependencies [34e8cfa]
149
+ - Updated dependencies [34e8cfa]
150
+ - Updated dependencies [34e8cfa]
151
+ - @ac-kit/algo@0.5.0
152
+ - @ac-kit/core@0.12.0
153
+ - @ac-kit/math-linear@0.3.0
154
+ - @ac-kit/math-geometry@0.3.0
155
+
3
156
  ## 0.2.3
4
157
 
5
158
  ### Patch Changes
package/README.md CHANGED
@@ -33,9 +33,106 @@ between the two are here.
33
33
  | `RowGrid2` | Row-major | Fixed rows, unbounded columns |
34
34
  | `StripGrid2` | Row-major | Fixed columns, unbounded rows |
35
35
 
36
- Plus `GridCoord2`, `GridSize2`, `GridRect2`, the pixel conversions
37
- (`gridCoord2ToPoint2`, `gridRect2ToPixelRect2`, …), and the tiling algorithms
38
- `findFrontierTilings` and `dfsPackGrid2`.
36
+ Plus `GridCoord2`, `GridSize2`, `GridRect2`, the conversions to and from
37
+ continuous space (`vec2FromGridCoord2`, `box2FromGridRect2`,
38
+ `gridRect2FromBox2`, …), and the tiling algorithms `findFrontierTilings` and
39
+ `dfsPackGrid2`.
40
+
41
+ ## Atlases and distance fields
42
+
43
+ `ShelfAllocator` hands out rectangles of a fixed area one at a time and takes
44
+ them back one by one, on shelves of similar height: the online packer of a
45
+ texture atlas. `allocate` is O(shelves + free spans of a shelf), `release`
46
+ O(free spans of a shelf).
47
+
48
+ ```ts
49
+ import { ShelfAllocator } from "@ac-kit/math-grid";
50
+
51
+ const atlas = new ShelfAllocator(4096, 4096);
52
+ const slot = atlas.allocate(40, 12); // { x: 0, y: 0, id: 0 }, or undefined when full
53
+ if (slot !== undefined) {
54
+ atlas.release(slot.id); // throws RangeError for an unknown or released id
55
+ }
56
+ atlas.occupancy; // 0
57
+ ```
58
+
59
+ A released rectangle's space is reused once every rectangle of its shelf is
60
+ released; until then it is not.
61
+
62
+ `squaredDistanceTransform2` turns a grid of seeds (0 at a feature, `Infinity`
63
+ far from any, or any other non-negative cost) into the exact squared distance
64
+ to the nearest feature, cost included, in place, in O(width · height).
65
+ Features are cell centres. Pass a `scratch` of at least
66
+ `3 · max(width, height) + 1` entries to avoid any allocation.
67
+
68
+ ```ts
69
+ import { squaredDistanceTransform2 } from "@ac-kit/math-grid";
70
+
71
+ const grid = new Float64Array(15).fill(Infinity);
72
+ grid[7] = 0; // row 1, column 2 of a 5 × 3 grid
73
+ squaredDistanceTransform2(grid, 5, 3); // 5 2 1 2 5 / 4 1 0 1 4 / 5 2 1 2 5
74
+ ```
75
+
76
+ `signedDistanceFromCoverage2` turns antialiased coverage bytes (0 outside, 255
77
+ inside) into a signed distance field in texels, negative inside, capped at a
78
+ `range`: the sub-texel contour, located in each edge texel from the coverage,
79
+ is merged into straight runs, with a corner where two runs meet at an angle,
80
+ and every texel within `range` measures its distance to it (Gustavson &
81
+ Strand's antialiased Euclidean distance transform; vector propagation for a
82
+ long range). Within a tenth of a texel of the analytic distance, up to 5
83
+ texels from the edge, on discs of radius 4 and more and on polygons whose sides
84
+ are at least 8 texels long and whose corners are at least 20° (coverage sampled
85
+ 16 × 16 per texel, rounded to bytes), and within 0.02 texel on stripes 0.1 to
86
+ 0.8 texel wide along a row, a column or a diagonal of texel centres, whose
87
+ centreline lies half their width deep; seeding the transform above from
88
+ coverage is half a texel off. A feature thinner than a texel that crosses
89
+ texels off their centres can still be up to half a texel off: one texel's
90
+ coverage cannot tell where in it the feature lies. The field is 32-bit floats,
91
+ capped at the largest one not above `range`, so it stays finite whatever the
92
+ range, and a texel's side is decided before the cap. Pass a `scratch` of
93
+ `8 · width · height` entries to avoid any allocation: the call then allocates
94
+ nothing, whatever the range.
95
+
96
+ ```ts
97
+ import { signedDistanceFromCoverage2 } from "@ac-kit/math-grid";
98
+
99
+ const field = new Float32Array(5);
100
+ signedDistanceFromCoverage2(field, new Uint8Array([0, 0, 128, 255, 255]), 5, 1, 4);
101
+ // 2, 1, 0, -1, -2 within a tenth of a texel
102
+ ```
103
+
104
+ ## Distances between coordinates
105
+
106
+ `gridCoord2Distance` is the Euclidean distance, `gridCoord2DistanceManhattan`
107
+ counts the moves of a 4-connected grid and `gridCoord2DistanceChebyshev` those
108
+ of an 8-connected grid with unit diagonals. To choose the measure at run time,
109
+ pass the function, typed as `@ac-kit/core`'s `Distance<GridCoord2>`:
110
+
111
+ ```ts
112
+ import type { Distance } from "@ac-kit/core";
113
+ import {
114
+ type GridCoord2,
115
+ gridCoord2DistanceChebyshev,
116
+ gridCoord2DistanceManhattan,
117
+ } from "@ac-kit/math-grid";
118
+
119
+ function nearest(
120
+ from: GridCoord2,
121
+ candidates: readonly GridCoord2[],
122
+ distance: Distance<GridCoord2>,
123
+ ): GridCoord2 | undefined {
124
+ return candidates.toSorted((a, b) => distance(from, a) - distance(from, b))[0];
125
+ }
126
+
127
+ const origin = { row: 0, col: 0 };
128
+ const cells = [
129
+ { row: 3, col: 3 },
130
+ { row: 0, col: 5 },
131
+ ];
132
+
133
+ nearest(origin, cells, gridCoord2DistanceManhattan); // { row: 0, col: 5 }
134
+ nearest(origin, cells, gridCoord2DistanceChebyshev); // { row: 3, col: 3 }
135
+ ```
39
136
 
40
137
  ## Choosing a storage strategy
41
138
 
@@ -0,0 +1,48 @@
1
+ /**
2
+ * The contour of an antialiased coverage grid, as sites (see
3
+ * {@link COVERAGE_STRIDE_}): each edge texel's straight edge, merged into one
4
+ * segment per straight run of them, and a corner where two runs meet.
5
+ *
6
+ * Cost is linear in the edge texels: the runs grow once from their pieces, and
7
+ * a corner is only tried where two runs end next to each other, so a texel on a
8
+ * straight edge (most of them) costs its piece and one membership test.
9
+ */
10
+ /**
11
+ * The largest coverage error, as a fraction of a texel, of a straight edge's
12
+ * prediction over the 3 × 3 texels around an edge texel for its texel to count
13
+ * as straight: above the curvature of a disc of radius 6, below what a corner
14
+ * inside the window leaves.
15
+ */
16
+ export declare const STRAIGHT_RESIDUAL_ = 0.1;
17
+ /**
18
+ * Writes the straight edge of antialiased texel `(x, y)`, of coverage `value`,
19
+ * at `sites[at]` in the `COVERAGE_STRIDE_` layout.
20
+ */
21
+ export declare function locateEdge_(sites: Float64Array, at: number, coverage: Uint8Array, value: number, x: number, y: number, width: number, height: number): void;
22
+ /**
23
+ * Writes into `residuals[cell]` how far the coverage of the texels around
24
+ * antialiased texel `(x, y)`, as a fraction of a texel, strays from what its
25
+ * straight edge (at `sites[at]`) predicts; `NaN` for a texel without one (a
26
+ * zero gradient).
27
+ */
28
+ export declare function edgeResidual_(sites: Float64Array, at: number, coverage: Uint8Array, x: number, y: number, width: number, height: number, residuals: Float32Array, cell: number): void;
29
+ /**
30
+ * Merges the straight pieces of contour of a coverage grid into runs, one
31
+ * segment site each, and places a corner where two runs meet at an angle (see
32
+ * the module notes). `scratch` holds each site cell's label in `[0, count)`
33
+ * (`UNASSIGNED_` on entry; afterwards its run's holder for a run's piece, its
34
+ * own cell for a corner's holder, the holder for a cell that took a corner,
35
+ * else negative), free room in `[count, 2 · count)`, and the site geometry from
36
+ * `2 · count`; `residuals` the straight-edge residual of each antialiased
37
+ * texel. Returns how many corners there are, their holders listed from
38
+ * `scratch[count]`.
39
+ */
40
+ export declare function findContour_(scratch: Float64Array, residuals: Float32Array, coverage: Uint8Array, width: number, height: number): number;
41
+ /**
42
+ * Whether cell `cell` holds contour: an antialiased texel, or a full or empty
43
+ * texel with an opposite neighbour to the right or below.
44
+ */
45
+ export declare function isSiteCell_(coverage: Uint8Array, cell: number, width: number, count: number): boolean;
46
+ /** Whether the site of cell `cell` is a corner. */
47
+ export declare function isCorner_(scratch: Float64Array, base: number, cell: number): boolean;
48
+ //# sourceMappingURL=_coverage-contour2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_coverage-contour2.d.ts","sourceRoot":"","sources":["../src/_coverage-contour2.ts"],"names":[],"mappings":"AAgBA;;;;;;;;GAQG;AAEH;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,MAAM,CAAC;AAuEtC;;;GAGG;AACH,wBAAgB,WAAW,CAC1B,KAAK,EAAE,YAAY,EACnB,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,UAAU,EACpB,KAAK,EAAE,MAAM,EACb,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,GACZ,IAAI,CAoFN;AAwQD;;;;;GAKG;AACH,wBAAgB,aAAa,CAC5B,KAAK,EAAE,YAAY,EACnB,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,UAAU,EACpB,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,YAAY,EACvB,IAAI,EAAE,MAAM,GACV,IAAI,CA8BN;AAcD;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAC3B,OAAO,EAAE,YAAY,EACrB,SAAS,EAAE,YAAY,EACvB,QAAQ,EAAE,UAAU,EACpB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,GACZ,MAAM,CAWR;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAC1B,QAAQ,EAAE,UAAU,EACpB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GACX,OAAO,CAUT;AA0kBD,mDAAmD;AACnD,wBAAgB,SAAS,CACxB,OAAO,EAAE,YAAY,EACrB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GACV,OAAO,CAOT"}