@ac-kit/math-grid 0.2.2 → 0.3.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,106 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 34e8cfa: `DistanceType` and the `type` argument of `gridCoord2Distance` are removed:
8
+ `gridCoord2Distance(a, b)` is the Euclidean distance, and
9
+ `gridCoord2DistanceManhattan` and `gridCoord2DistanceChebyshev` are the others.
10
+ `gridCoord2Distance(a, b, "manhattan")` becomes
11
+ `gridCoord2DistanceManhattan(a, b)`; code that chose the measure at run time
12
+ passes the function itself, typed as `@ac-kit/core`'s `Distance<GridCoord2>`.
13
+ `@ac-kit/math-grid` now depends on `@ac-kit/core` at run time.
14
+
15
+ The conversions to and from continuous space take and return
16
+ `@ac-kit/math-linear` vectors and `@ac-kit/math-geometry` boxes: the twelve
17
+ `gridCoord2ToPoint2`, `point2ToGridCoord2`, `gridSize2ToSize2`,
18
+ `size2ToGridSize2`, `gridRect2ToRect2`, `rect2ToGridRect2`,
19
+ `gridCoord2ToPixelPoint2`, `gridSize2ToPixelSize2`, `gridRect2ToPixelRect2`,
20
+ `pixelPoint2ToGridCoord2`, `pixelSize2ToGridSize2` and `pixelRect2ToGridRect2`
21
+ become `vec2FromGridCoord2(out, coord, cellSize?)`,
22
+ `vec2FromGridSize2(out, size, cellSize?)`, `box2FromGridRect2(out, rect,
23
+ cellSize?)`, `gridCoord2FromVec2(point, cellSize?)`,
24
+ `gridSize2FromVec2(size, cellSize?)` and `gridRect2FromBox2(box, cellSize?)`,
25
+ the cell size being a `Vec2` and omitted meaning unit cells. A pixel rectangle
26
+ `{ origin, size }` becomes the box `[x, y, x + width, y + height]`.
27
+ - 34e8cfa: `ShelfAllocator` allocates and releases rectangles one by one in a fixed area,
28
+ on shelves of similar height, for texture atlases: `allocate(width, height)`
29
+ returns a top-left and an id, or `undefined` when nothing fits; `release(id)`
30
+ frees it (a stale, unknown or cleared id throws a `RangeError`; ids are never
31
+ reused), at once and merged with neighbouring free space, so an atlas can evict
32
+ entries one by one; `clear()` and `occupancy` complete it. The constructor
33
+ throws a `RangeError` when `width · height` is not a safe integer. `allocate`
34
+ is O(shelves + free spans of a shelf), `release` O(free spans of a shelf).
35
+ `squaredDistanceTransform2(grid, width, height, scratch?)` replaces a grid of
36
+ seeds (0 at a feature, `Infinity` far from any, a squared sub-texel distance at
37
+ an edge) with the exact squared Euclidean distance to the nearest seed, in
38
+ place and in O(width · height), without allocation when given a `scratch` of at
39
+ least `3 · max(width, height) + 1` entries. A `NaN` or negative seed, or a
40
+ `scratch` sharing bytes with the grid, throws a `RangeError`; seeds up to
41
+ `Number.MAX_VALUE` are handled.
42
+ - 34e8cfa: `signedDistanceFromCoverage2(out, coverage, width, height, range)` writes the
43
+ signed distance field, in texels, of an antialiased coverage grid (bytes, 0
44
+ outside to 255 inside): negative inside, zero on the 50 % contour, capped at
45
+ `range`. Each edge texel's contour is located from the coverage gradient and
46
+ the exact coverage of a straight edge, so the field is within 0.1 texel of the
47
+ analytic distance on discs of radius 6 and more, where seeding
48
+ `squaredDistanceTransform2` from coverage is 0.5 texel off. O(edge texels ·
49
+ range²), no allocation; a bad size, a non-positive `range`, a short buffer or
50
+ an `out` that shares bytes with the coverage it reads throws a `RangeError`
51
+ (views over shared memory, a `SharedArrayBuffer` and its clones, count as
52
+ sharing).
53
+
54
+ ### Patch Changes
55
+
56
+ - Updated dependencies [34e8cfa]
57
+ - Updated dependencies [34e8cfa]
58
+ - Updated dependencies [34e8cfa]
59
+ - Updated dependencies [34e8cfa]
60
+ - Updated dependencies [34e8cfa]
61
+ - Updated dependencies [34e8cfa]
62
+ - Updated dependencies [34e8cfa]
63
+ - Updated dependencies [34e8cfa]
64
+ - Updated dependencies [34e8cfa]
65
+ - Updated dependencies [34e8cfa]
66
+ - Updated dependencies [34e8cfa]
67
+ - Updated dependencies [34e8cfa]
68
+ - Updated dependencies [34e8cfa]
69
+ - Updated dependencies [34e8cfa]
70
+ - Updated dependencies [34e8cfa]
71
+ - Updated dependencies [34e8cfa]
72
+ - Updated dependencies [34e8cfa]
73
+ - Updated dependencies [34e8cfa]
74
+ - Updated dependencies [34e8cfa]
75
+ - Updated dependencies [34e8cfa]
76
+ - Updated dependencies [34e8cfa]
77
+ - Updated dependencies [34e8cfa]
78
+ - Updated dependencies [34e8cfa]
79
+ - Updated dependencies [34e8cfa]
80
+ - Updated dependencies [34e8cfa]
81
+ - Updated dependencies [34e8cfa]
82
+ - Updated dependencies [34e8cfa]
83
+ - Updated dependencies [34e8cfa]
84
+ - Updated dependencies [34e8cfa]
85
+ - Updated dependencies [34e8cfa]
86
+ - @ac-kit/algo@0.5.0
87
+ - @ac-kit/core@0.12.0
88
+ - @ac-kit/math-linear@0.3.0
89
+ - @ac-kit/math-geometry@0.3.0
90
+
91
+ ## 0.2.3
92
+
93
+ ### Patch Changes
94
+
95
+ - Updated dependencies [2a0fe74]
96
+ - Updated dependencies [5152230]
97
+ - Updated dependencies [5152230]
98
+ - Updated dependencies [5152230]
99
+ - Updated dependencies [5152230]
100
+ - @ac-kit/algo@0.4.0
101
+ - @ac-kit/math-linear@0.2.0
102
+ - @ac-kit/math-geometry@0.2.2
103
+
3
104
  ## 0.2.2
4
105
 
5
106
  ### Patch Changes
package/README.md CHANGED
@@ -33,9 +33,93 @@ 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, a squared sub-texel distance for a coverage-estimated edge cell)
64
+ into exact squared distances, in place, in O(width · height): the core of a
65
+ signed-distance-field glyph. 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`, within a tenth of a texel on discs — where seeding the transform above
79
+ from coverage is half a texel off. It keeps sub-texel edges by locating the
80
+ contour in each edge texel from the coverage gradient. O(edge texels · range²),
81
+ no allocation.
82
+
83
+ ```ts
84
+ import { signedDistanceFromCoverage2 } from "@ac-kit/math-grid";
85
+
86
+ const field = new Float32Array(5);
87
+ signedDistanceFromCoverage2(field, new Uint8Array([0, 0, 128, 255, 255]), 5, 1, 4);
88
+ // 2, 1, 0, -1, -2 within a tenth of a texel
89
+ ```
90
+
91
+ ## Distances between coordinates
92
+
93
+ `gridCoord2Distance` is the Euclidean distance, `gridCoord2DistanceManhattan`
94
+ counts the moves of a 4-connected grid and `gridCoord2DistanceChebyshev` those
95
+ of an 8-connected grid with unit diagonals. To choose the measure at run time,
96
+ pass the function, typed as `@ac-kit/core`'s `Distance<GridCoord2>`:
97
+
98
+ ```ts
99
+ import type { Distance } from "@ac-kit/core";
100
+ import {
101
+ type GridCoord2,
102
+ gridCoord2DistanceChebyshev,
103
+ gridCoord2DistanceManhattan,
104
+ } from "@ac-kit/math-grid";
105
+
106
+ function nearest(
107
+ from: GridCoord2,
108
+ candidates: readonly GridCoord2[],
109
+ distance: Distance<GridCoord2>,
110
+ ): GridCoord2 | undefined {
111
+ return candidates.toSorted((a, b) => distance(from, a) - distance(from, b))[0];
112
+ }
113
+
114
+ const origin = { row: 0, col: 0 };
115
+ const cells = [
116
+ { row: 3, col: 3 },
117
+ { row: 0, col: 5 },
118
+ ];
119
+
120
+ nearest(origin, cells, gridCoord2DistanceManhattan); // { row: 0, col: 5 }
121
+ nearest(origin, cells, gridCoord2DistanceChebyshev); // { row: 3, col: 3 }
122
+ ```
39
123
 
40
124
  ## Choosing a storage strategy
41
125
 
@@ -1,10 +1,52 @@
1
- import type { DistanceType } from "@ac-kit/math-linear";
2
1
  export type GridCoord2 = {
3
2
  row: number;
4
3
  col: number;
5
4
  };
6
5
  export declare function gridCoord2Add(a: GridCoord2, b: GridCoord2): GridCoord2;
7
- export declare function gridCoord2Distance(a: GridCoord2, b: GridCoord2, type?: DistanceType): number;
6
+ /**
7
+ * The Euclidean distance `√(Δrow² + Δcol²)` between two coordinates: the
8
+ * straight-line distance between the cell centres.
9
+ *
10
+ * Time complexity: O(1). Coordinates are not validated, and `NaN` propagates.
11
+ * The squares overflow beyond about `1e154`.
12
+ *
13
+ * @example
14
+ * gridCoord2Distance({ row: 0, col: 0 }, { row: 3, col: 4 }); // 5
15
+ *
16
+ * @param a - The first coordinate.
17
+ * @param b - The second coordinate.
18
+ * @returns The distance, `0` only for equal coordinates.
19
+ */
20
+ export declare function gridCoord2Distance(a: GridCoord2, b: GridCoord2): number;
21
+ /**
22
+ * The Manhattan distance `|Δrow| + |Δcol|`: the number of moves between two
23
+ * coordinates when only the four edge-adjacent steps are allowed.
24
+ *
25
+ * Time complexity: O(1). Coordinates are not validated, and `NaN` propagates.
26
+ *
27
+ * @example
28
+ * gridCoord2DistanceManhattan({ row: 0, col: 0 }, { row: 3, col: 4 }); // 7
29
+ *
30
+ * @param a - The first coordinate.
31
+ * @param b - The second coordinate.
32
+ * @returns The distance.
33
+ */
34
+ export declare function gridCoord2DistanceManhattan(a: GridCoord2, b: GridCoord2): number;
35
+ /**
36
+ * The Chebyshev distance `max(|Δrow|, |Δcol|)`: the number of moves between two
37
+ * coordinates when the eight neighbouring steps are allowed and a diagonal
38
+ * costs one.
39
+ *
40
+ * Time complexity: O(1). Coordinates are not validated, and `NaN` propagates.
41
+ *
42
+ * @example
43
+ * gridCoord2DistanceChebyshev({ row: 0, col: 0 }, { row: 3, col: 4 }); // 4
44
+ *
45
+ * @param a - The first coordinate.
46
+ * @param b - The second coordinate.
47
+ * @returns The distance.
48
+ */
49
+ export declare function gridCoord2DistanceChebyshev(a: GridCoord2, b: GridCoord2): number;
8
50
  export declare function gridCoord2Equals(a: GridCoord2, b: GridCoord2): boolean;
9
51
  export declare function gridCoord2Scale(coord: GridCoord2, factor: number): GridCoord2;
10
52
  export declare function gridCoord2Sub(a: GridCoord2, b: GridCoord2): GridCoord2;
@@ -1 +1 @@
1
- {"version":3,"file":"grid-coord2.d.ts","sourceRoot":"","sources":["../src/grid-coord2.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAExD,MAAM,MAAM,UAAU,GAAG;IACxB,GAAG,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e,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,UAAU,CAE7E;AAED,wBAAgB,aAAa,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,UAAU,CAEtE"}
@@ -1,17 +1,57 @@
1
1
  export function gridCoord2Add(a, b) {
2
2
  return { row: a.row + b.row, col: a.col + b.col };
3
3
  }
4
- export function gridCoord2Distance(a, b, type = "euclidean") {
5
- const dr = Math.abs(a.row - b.row);
6
- const dc = Math.abs(a.col - b.col);
7
- switch (type) {
8
- case "manhattan":
9
- return dr + dc;
10
- case "chebyshev":
11
- return Math.max(dr, dc);
12
- case "euclidean":
13
- return Math.sqrt(dr * dr + dc * dc);
14
- }
4
+ /**
5
+ * The Euclidean distance `√(Δrow² + Δcol²)` between two coordinates: the
6
+ * straight-line distance between the cell centres.
7
+ *
8
+ * Time complexity: O(1). Coordinates are not validated, and `NaN` propagates.
9
+ * The squares overflow beyond about `1e154`.
10
+ *
11
+ * @example
12
+ * gridCoord2Distance({ row: 0, col: 0 }, { row: 3, col: 4 }); // 5
13
+ *
14
+ * @param a - The first coordinate.
15
+ * @param b - The second coordinate.
16
+ * @returns The distance, `0` only for equal coordinates.
17
+ */
18
+ export function gridCoord2Distance(a, b) {
19
+ const rowDelta = a.row - b.row;
20
+ const colDelta = a.col - b.col;
21
+ return Math.sqrt(rowDelta * rowDelta + colDelta * colDelta);
22
+ }
23
+ /**
24
+ * The Manhattan distance `|Δrow| + |Δcol|`: the number of moves between two
25
+ * coordinates when only the four edge-adjacent steps are allowed.
26
+ *
27
+ * Time complexity: O(1). Coordinates are not validated, and `NaN` propagates.
28
+ *
29
+ * @example
30
+ * gridCoord2DistanceManhattan({ row: 0, col: 0 }, { row: 3, col: 4 }); // 7
31
+ *
32
+ * @param a - The first coordinate.
33
+ * @param b - The second coordinate.
34
+ * @returns The distance.
35
+ */
36
+ export function gridCoord2DistanceManhattan(a, b) {
37
+ return Math.abs(a.row - b.row) + Math.abs(a.col - b.col);
38
+ }
39
+ /**
40
+ * The Chebyshev distance `max(|Δrow|, |Δcol|)`: the number of moves between two
41
+ * coordinates when the eight neighbouring steps are allowed and a diagonal
42
+ * costs one.
43
+ *
44
+ * Time complexity: O(1). Coordinates are not validated, and `NaN` propagates.
45
+ *
46
+ * @example
47
+ * gridCoord2DistanceChebyshev({ row: 0, col: 0 }, { row: 3, col: 4 }); // 4
48
+ *
49
+ * @param a - The first coordinate.
50
+ * @param b - The second coordinate.
51
+ * @returns The distance.
52
+ */
53
+ export function gridCoord2DistanceChebyshev(a, b) {
54
+ return Math.max(Math.abs(a.row - b.row), Math.abs(a.col - b.col));
15
55
  }
16
56
  export function gridCoord2Equals(a, b) {
17
57
  return a.row === b.row && a.col === b.col;
@@ -1 +1 @@
1
- {"version":3,"file":"grid-coord2.js","sourceRoot":"","sources":["../src/grid-coord2.ts"],"names":[],"mappings":"AAOA,MAAM,UAAU,aAAa,CAAC,CAAa,EAAE,CAAa;IACzD,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,EAAE,CAAC;AACnD,CAAC;AAED,MAAM,UAAU,kBAAkB,CACjC,CAAa,EACb,CAAa,EACb,IAAI,GAAiB,WAAW;IAEhC,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;IACnC,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC;IACnC,QAAQ,IAAI,EAAE,CAAC;QACd,KAAK,WAAW;YACf,OAAO,EAAE,GAAG,EAAE,CAAC;QAChB,KAAK,WAAW;YACf,OAAO,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;QACzB,KAAK,WAAW;YAC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ometry";
3
- import { GridCoord2 } from "./grid-coord2.js";
4
- import { GridRect2 } from "./grid-rect2.js";
5
- import { GridSize2 } from "./grid-size2.js";
6
- export declare function gridCoord2ToPoint2(coord: GridCoord2): Point2;
7
- export declare function point2ToGridCoord2(point: Point2): GridCoord2;
8
- export declare function gridSize2ToSize2(size: GridSize2): Size2;
9
- export declare function size2ToGridSize2(size: Size2): GridSize2;
10
- export declare function gridRect2ToRect2(rect: GridRect2): Rect2;
11
- export declare function rect2ToGridRect2(rect: Rect2): GridRect2;
12
- export declare function gridCoord2ToPixelPoint2(coord: GridCoord2, cellSize: Size2): Point2;
13
- export declare function gridSize2ToPixelSize2(size: GridSize2, cellSize: Size2): Size2;
14
- export declare function gridRect2ToPixelRect2(rect: GridRect2, cellSize: Size2): Rect2;
15
- export declare function pixelPoint2ToGridCoord2(point: Point2, cellSize: Size2): GridCoord2;
16
- export declare function pixelSize2ToGridSize2(size: Size2, cellSize: Size2): GridSize2;
17
- export declare function pixelRect2ToGridRect2(rect: Rect2, cellSize: Size2): GridRect2;
1
+ import { type Box2, type ReadonlyBox2 } from "@ac-kit/math-geometry";
2
+ import type { ReadonlyVec2, Vec2 } from "@ac-kit/math-linear";
3
+ import type { GridCoord2 } from "./grid-coord2.js";
4
+ import type { GridRect2 } from "./grid-rect2.js";
5
+ import type { GridSize2 } from "./grid-size2.js";
6
+ /**
7
+ * Writes the position of the cell `coord`'s top-left corner, `[col·width,
8
+ * row·height]`, into `out`.
9
+ */
10
+ export declare function vec2FromGridCoord2(out: Vec2, coord: GridCoord2, cellSize?: ReadonlyVec2): Vec2;
11
+ /** Writes the extent of `size` cells, `[cols·width, rows·height]`, into `out`. */
12
+ export declare function vec2FromGridSize2(out: Vec2, size: GridSize2, cellSize?: ReadonlyVec2): Vec2;
13
+ /**
14
+ * Writes the box covered by `rect` (its start to its start plus its span) into
15
+ * `out`.
16
+ */
17
+ export declare function box2FromGridRect2(out: Box2, rect: GridRect2, cellSize?: ReadonlyVec2): Box2;
18
+ /**
19
+ * The cell coordinates, not rounded, of the point `point`: `{ col: x / width,
20
+ * row: y / height }`.
21
+ */
22
+ export declare function gridCoord2FromVec2(point: ReadonlyVec2, cellSize?: ReadonlyVec2): GridCoord2;
23
+ /**
24
+ * The size in cells, not rounded, of the extent `size`: `{ cols: width /
25
+ * cellWidth, rows: height / cellHeight }`.
26
+ */
27
+ export declare function gridSize2FromVec2(size: ReadonlyVec2, cellSize?: ReadonlyVec2): GridSize2;
28
+ /**
29
+ * The rectangle of cells, not rounded, covered by `box`: its minimum corner as
30
+ * the start, its extent as the span.
31
+ */
32
+ export declare function gridRect2FromBox2(box: ReadonlyBox2, cellSize?: ReadonlyVec2): GridRect2;
18
33
  //# sourceMappingURL=grid2-pixel.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"grid2-pixel.d.ts","sourceRoot":"","sources":["../src/grid2-pixel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAO5C,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAE5D;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAE5D;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,SAAS,GAAG,KAAK,CAEvD;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,KAAK,GAAG,SAAS,CAEvD;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,SAAS,GAAG,KAAK,CAKvD;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,KAAK,GAAG,SAAS,CAKvD;A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C,GAAG,EAAE,IAAI,EACT,IAAI,EAAE,SAAS,EACf,QAAQ,CAAC,EAAE,YAAY,GACrB,IAAI,CAYN;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CACjC,KAAK,EAAE,YAAY,EACnB,QAAQ,CAAC,EAAE,YAAY,GACrB,UAAU,CAIZ;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAChC,IAAI,EAAE,YAAY,EAClB,QAAQ,CAAC,EAAE,YAAY,GACrB,SAAS,CAIX;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAChC,GAAG,EAAE,YAAY,EACjB,QAAQ,CAAC,EAAE,YAAY,GACrB,SAAS,CAUX"}
@@ -1,63 +1,79 @@
1
- // ─── Structural (unit) conversions ────────────────────────────────────────────
2
- //
3
- // Convention: col = x, row = y, cols = width, rows = height.
4
- // These conversions are exact (integer-preserving) field remappings.
5
- export function gridCoord2ToPoint2(coord) {
6
- return { x: coord.col, y: coord.row };
1
+ import { box2Set } from "@ac-kit/math-geometry";
2
+ // The conversions between lattice records (integers, cells) and continuous
3
+ // space (`Vec2`, `Box2`). Convention: col = x, row = y, cols = width, rows =
4
+ // height. `cellSize` is the width and height of one cell in continuous units;
5
+ // omitted, the cells are one unit wide and high, so each conversion is an exact
6
+ // field remapping. Going from continuous space to the grid is exact too:
7
+ // fractional cells come back, round at the call site.
8
+ /**
9
+ * Writes the position of the cell `coord`'s top-left corner, `[col·width,
10
+ * row·height]`, into `out`.
11
+ */
12
+ export function vec2FromGridCoord2(out, coord, cellSize) {
13
+ if (cellSize === undefined) {
14
+ out[0] = coord.col;
15
+ out[1] = coord.row;
16
+ }
17
+ else {
18
+ out[0] = coord.col * cellSize[0];
19
+ out[1] = coord.row * cellSize[1];
20
+ }
21
+ return out;
7
22
  }
8
- export function point2ToGridCoord2(point) {
9
- return { col: point.x, row: point.y };
23
+ /** Writes the extent of `size` cells, `[cols·width, rows·height]`, into `out`. */
24
+ export function vec2FromGridSize2(out, size, cellSize) {
25
+ if (cellSize === undefined) {
26
+ out[0] = size.cols;
27
+ out[1] = size.rows;
28
+ }
29
+ else {
30
+ out[0] = size.cols * cellSize[0];
31
+ out[1] = size.rows * cellSize[1];
32
+ }
33
+ return out;
10
34
  }
11
- export function gridSize2ToSize2(size) {
12
- return { width: size.cols, height: size.rows };
35
+ /**
36
+ * Writes the box covered by `rect` (its start to its start plus its span) into
37
+ * `out`.
38
+ */
39
+ export function box2FromGridRect2(out, rect, cellSize) {
40
+ const width = cellSize === undefined ? 1 : cellSize[0];
41
+ const height = cellSize === undefined ? 1 : cellSize[1];
42
+ const minX = rect.start.col * width;
43
+ const minY = rect.start.row * height;
44
+ return box2Set(out, minX, minY, minX + rect.span.cols * width, minY + rect.span.rows * height);
13
45
  }
14
- export function size2ToGridSize2(size) {
15
- return { cols: size.width, rows: size.height };
46
+ /**
47
+ * The cell coordinates, not rounded, of the point `point`: `{ col: x / width,
48
+ * row: y / height }`.
49
+ */
50
+ export function gridCoord2FromVec2(point, cellSize) {
51
+ return cellSize === undefined
52
+ ? { col: point[0], row: point[1] }
53
+ : { col: point[0] / cellSize[0], row: point[1] / cellSize[1] };
16
54
  }
17
- export function gridRect2ToRect2(rect) {
18
- return {
19
- origin: gridCoord2ToPoint2(rect.start),
20
- size: gridSize2ToSize2(rect.span),
21
- };
22
- }
23
- export function rect2ToGridRect2(rect) {
24
- return {
25
- start: point2ToGridCoord2(rect.origin),
26
- span: size2ToGridSize2(rect.size),
27
- };
28
- }
29
- // ─── Scaled (pixel) conversions ───────────────────────────────────────────────
30
- //
31
- // `cellSize` is the pixel dimensions of one grid cell.
32
- // Inverse (pixel→grid) returns exact (non-integer) values — round at the call site.
33
- export function gridCoord2ToPixelPoint2(coord, cellSize) {
34
- return { x: coord.col * cellSize.width, y: coord.row * cellSize.height };
35
- }
36
- export function gridSize2ToPixelSize2(size, cellSize) {
37
- return {
38
- width: size.cols * cellSize.width,
39
- height: size.rows * cellSize.height,
40
- };
41
- }
42
- export function gridRect2ToPixelRect2(rect, cellSize) {
43
- return {
44
- origin: gridCoord2ToPixelPoint2(rect.start, cellSize),
45
- size: gridSize2ToPixelSize2(rect.span, cellSize),
46
- };
47
- }
48
- export function pixelPoint2ToGridCoord2(point, cellSize) {
49
- return { col: point.x / cellSize.width, row: point.y / cellSize.height };
50
- }
51
- export function pixelSize2ToGridSize2(size, cellSize) {
52
- return {
53
- cols: size.width / cellSize.width,
54
- rows: size.height / cellSize.height,
55
- };
55
+ /**
56
+ * The size in cells, not rounded, of the extent `size`: `{ cols: width /
57
+ * cellWidth, rows: height / cellHeight }`.
58
+ */
59
+ export function gridSize2FromVec2(size, cellSize) {
60
+ return cellSize === undefined
61
+ ? { cols: size[0], rows: size[1] }
62
+ : { cols: size[0] / cellSize[0], rows: size[1] / cellSize[1] };
56
63
  }
57
- export function pixelRect2ToGridRect2(rect, cellSize) {
64
+ /**
65
+ * The rectangle of cells, not rounded, covered by `box`: its minimum corner as
66
+ * the start, its extent as the span.
67
+ */
68
+ export function gridRect2FromBox2(box, cellSize) {
69
+ const width = cellSize === undefined ? 1 : cellSize[0];
70
+ const height = cellSize === undefined ? 1 : cellSize[1];
58
71
  return {
59
- start: pixelPoint2ToGridCoord2(rect.origin, cellSize),
60
- span: pixelSize2ToGridSize2(rect.size, cellSize),
72
+ start: { col: box[0] / width, row: box[1] / height },
73
+ span: {
74
+ cols: (box[2] - box[0]) / width,
75
+ rows: (box[3] - box[1]) / height,
76
+ },
61
77
  };
62
78
  }
63
79
  //# sourceMappingURL=grid2-pixel.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"grid2-pixel.js","sourceRoot":"","sources":["../src/grid2-pixel.ts"],"names":[],"mappings":"AAOA,iFAAiF;AACjF,EAAE;AACF,6DAA6D;AAC7D,qEAAqE;AAErE,MAAM,UAAU,kBAAkB,CAAC,KAAiB;IACnD,OAAO,EAAE,CAAC,EAAE,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,CAAC,GAAG,EAAE,CAAC;AACvC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,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IACvD,MAAM,MAAM,GAAG,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IACxD,OAAO;QACN,KAAK,EAAE,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,GAAG,MAAM,EAAE;QACpD,IAAI,EAAE;YACL,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK;YAC/B,IAAI,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM;SAChC;KACD,CAAC;AACH,CAAC"}
package/dist/index.d.ts CHANGED
@@ -8,5 +8,8 @@ export * from "./grid2-pixel.js";
8
8
  export * from "./grid2.js";
9
9
  export * from "./infinite-grid2.js";
10
10
  export * from "./row-grid2.js";
11
+ export * from "./shelf-allocator.js";
12
+ export * from "./signed-distance-from-coverage2.js";
13
+ export * from "./squared-distance-transform2.js";
11
14
  export * from "./strip-grid2.js";
12
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,qCAAqC,CAAC;AACpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,kBAAkB,CAAC"}
package/dist/index.js CHANGED
@@ -8,5 +8,8 @@ export * from "./grid2-pixel.js";
8
8
  export * from "./grid2.js";
9
9
  export * from "./infinite-grid2.js";
10
10
  export * from "./row-grid2.js";
11
+ export * from "./shelf-allocator.js";
12
+ export * from "./signed-distance-from-coverage2.js";
13
+ export * from "./squared-distance-transform2.js";
11
14
  export * from "./strip-grid2.js";
12
15
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,qCAAqC,CAAC;AACpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,61 @@
1
+ /** A rectangle handed out by a `ShelfAllocator`. */
2
+ export type ShelfAllocation = {
3
+ /** Left edge. */
4
+ readonly x: number;
5
+ /** Top edge. */
6
+ readonly y: number;
7
+ /** Pass to `ShelfAllocator.release`; never reused by the allocator. */
8
+ readonly id: number;
9
+ };
10
+ /**
11
+ * Rectangles allocated and released one by one in a fixed area, on shelves
12
+ * (rows of similar height): a rectangle goes on the shelf of the least height
13
+ * that holds it and has a free span wide enough (first fit), or on a new shelf
14
+ * below the last one. A released rectangle frees its own span at once, merged
15
+ * with adjacent free spans; a shelf left with no rectangle is emptied and the
16
+ * last empty shelves are removed. Rows are never split or merged, so entries of
17
+ * very different heights can fragment the area.
18
+ *
19
+ * Coordinates are integers in `[0, width) × [0, height)` (texels, pixels).
20
+ * Identifiers increase from 0 and are not reused, even after `clear`, so a
21
+ * stale identifier is always detected.
22
+ *
23
+ * ```ts
24
+ * const atlas = new ShelfAllocator(256, 256);
25
+ * const slot = atlas.allocate(40, 12); // { x: 0, y: 0, id: 0 }
26
+ * atlas.release(slot!.id);
27
+ * ```
28
+ */
29
+ export declare class ShelfAllocator {
30
+ #private;
31
+ /**
32
+ * @throws {RangeError} When `width` or `height` is not a positive integer, or
33
+ * when `width · height` is not a safe integer.
34
+ */
35
+ constructor(width: number, height: number);
36
+ /** The area given to live allocations over the total area, in `[0, 1]`. */
37
+ get occupancy(): number;
38
+ /**
39
+ * The top-left of a free area and its id, or `undefined` when none fits.
40
+ *
41
+ * O(shelves + free spans of the chosen shelf).
42
+ *
43
+ * @throws {RangeError} When `width` or `height` is not a positive integer.
44
+ */
45
+ allocate(width: number, height: number): ShelfAllocation | undefined;
46
+ /**
47
+ * Frees a rectangle.
48
+ *
49
+ * O(shelves + free spans of its shelf).
50
+ *
51
+ * @throws {RangeError} For an id that was never allocated, was already
52
+ * released or was cleared.
53
+ */
54
+ release(id: number): void;
55
+ /**
56
+ * Releases everything. Previous ids stay invalid. O(1) besides dropping the
57
+ * entries.
58
+ */
59
+ clear(): void;
60
+ }
61
+ //# sourceMappingURL=shelf-allocator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shelf-allocator.d.ts","sourceRoot":"","sources":["../src/shelf-allocator.ts"],"names":[],"mappings":"AAEA,oDAAoD;AACpD,MAAM,MAAM,eAAe,GAAG;IAC7B,iBAAiB;IACjB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB;IAChB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,uEAAuE;IACvE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;CACpB,CAAC;AA0BF;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,cAAc;;IAS1B;;;OAGG;IACH,YAAY,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAUxC;IAED,2EAA2E;IAC3E,IAAI,SAAS,IAAI,MAAM,CAEtB;IAED;;;;;;OAMG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,eAAe,GAAG,SAAS,CAmDnE;IAED;;;;;;;OAOG;IACH,OAAO,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAwCxB;IAED;;;OAGG;IACH,KAAK,IAAI,IAAI,CAKZ;CACD"}
@@ -0,0 +1,163 @@
1
+ import { describeValue } from "@ac-kit/core";
2
+ function assertDimension(name, value) {
3
+ if (!Number.isSafeInteger(value) || value <= 0) {
4
+ throw new RangeError(`${name} must be a positive integer, got ${describeValue(value)}`);
5
+ }
6
+ }
7
+ /**
8
+ * Rectangles allocated and released one by one in a fixed area, on shelves
9
+ * (rows of similar height): a rectangle goes on the shelf of the least height
10
+ * that holds it and has a free span wide enough (first fit), or on a new shelf
11
+ * below the last one. A released rectangle frees its own span at once, merged
12
+ * with adjacent free spans; a shelf left with no rectangle is emptied and the
13
+ * last empty shelves are removed. Rows are never split or merged, so entries of
14
+ * very different heights can fragment the area.
15
+ *
16
+ * Coordinates are integers in `[0, width) × [0, height)` (texels, pixels).
17
+ * Identifiers increase from 0 and are not reused, even after `clear`, so a
18
+ * stale identifier is always detected.
19
+ *
20
+ * ```ts
21
+ * const atlas = new ShelfAllocator(256, 256);
22
+ * const slot = atlas.allocate(40, 12); // { x: 0, y: 0, id: 0 }
23
+ * atlas.release(slot!.id);
24
+ * ```
25
+ */
26
+ export class ShelfAllocator {
27
+ #width;
28
+ #height;
29
+ #shelves = [];
30
+ #entries = new Map();
31
+ #nextId = 0;
32
+ #nextY = 0;
33
+ #usedArea = 0;
34
+ /**
35
+ * @throws {RangeError} When `width` or `height` is not a positive integer, or
36
+ * when `width · height` is not a safe integer.
37
+ */
38
+ constructor(width, height) {
39
+ assertDimension("width", width);
40
+ assertDimension("height", height);
41
+ if (!Number.isSafeInteger(width * height)) {
42
+ throw new RangeError(`area ${width}×${height} exceeds the safe integer range`);
43
+ }
44
+ this.#width = width;
45
+ this.#height = height;
46
+ }
47
+ /** The area given to live allocations over the total area, in `[0, 1]`. */
48
+ get occupancy() {
49
+ return this.#usedArea / (this.#width * this.#height);
50
+ }
51
+ /**
52
+ * The top-left of a free area and its id, or `undefined` when none fits.
53
+ *
54
+ * O(shelves + free spans of the chosen shelf).
55
+ *
56
+ * @throws {RangeError} When `width` or `height` is not a positive integer.
57
+ */
58
+ allocate(width, height) {
59
+ assertDimension("width", width);
60
+ assertDimension("height", height);
61
+ if (width > this.#width || height > this.#height) {
62
+ return undefined;
63
+ }
64
+ let best;
65
+ let bestSpan = -1;
66
+ for (const shelf of this.#shelves) {
67
+ if (shelf.height < height ||
68
+ (best !== undefined && shelf.height >= best.height)) {
69
+ continue;
70
+ }
71
+ const span = shelf.free.findIndex((free) => free.length >= width);
72
+ if (span >= 0) {
73
+ best = shelf;
74
+ bestSpan = span;
75
+ }
76
+ }
77
+ if (best === undefined) {
78
+ if (this.#height - this.#nextY < height) {
79
+ return undefined;
80
+ }
81
+ best = {
82
+ y: this.#nextY,
83
+ height,
84
+ free: [{ start: 0, length: this.#width }],
85
+ live: 0,
86
+ };
87
+ bestSpan = 0;
88
+ this.#shelves.push(best);
89
+ this.#nextY += height;
90
+ }
91
+ const id = this.#nextId++;
92
+ const span = best.free[bestSpan];
93
+ const x = span.start;
94
+ if (span.length === width) {
95
+ best.free.splice(bestSpan, 1);
96
+ }
97
+ else {
98
+ span.start += width;
99
+ span.length -= width;
100
+ }
101
+ best.live += 1;
102
+ this.#entries.set(id, { shelf: best, x, width, height });
103
+ this.#usedArea += width * height;
104
+ return { x, y: best.y, id };
105
+ }
106
+ /**
107
+ * Frees a rectangle.
108
+ *
109
+ * O(shelves + free spans of its shelf).
110
+ *
111
+ * @throws {RangeError} For an id that was never allocated, was already
112
+ * released or was cleared.
113
+ */
114
+ release(id) {
115
+ const entry = this.#entries.get(id);
116
+ if (entry === undefined) {
117
+ throw new RangeError(`unknown or already released id ${id}`);
118
+ }
119
+ this.#entries.delete(id);
120
+ this.#usedArea -= entry.width * entry.height;
121
+ const shelf = entry.shelf;
122
+ shelf.live -= 1;
123
+ if (shelf.live > 0) {
124
+ const free = shelf.free;
125
+ let index = free.findIndex((span) => span.start > entry.x);
126
+ if (index < 0) {
127
+ index = free.length;
128
+ }
129
+ free.splice(index, 0, { start: entry.x, length: entry.width });
130
+ const next = free[index + 1];
131
+ if (next !== undefined && next.start === entry.x + entry.width) {
132
+ free[index].length += next.length;
133
+ free.splice(index + 1, 1);
134
+ }
135
+ const previous = free[index - 1];
136
+ if (previous !== undefined &&
137
+ previous.start + previous.length === entry.x) {
138
+ previous.length += free[index].length;
139
+ free.splice(index, 1);
140
+ }
141
+ return;
142
+ }
143
+ shelf.free.length = 0;
144
+ shelf.free.push({ start: 0, length: this.#width });
145
+ let last = this.#shelves.at(-1);
146
+ while (last !== undefined && last.live === 0) {
147
+ this.#shelves.pop();
148
+ this.#nextY = last.y;
149
+ last = this.#shelves.at(-1);
150
+ }
151
+ }
152
+ /**
153
+ * Releases everything. Previous ids stay invalid. O(1) besides dropping the
154
+ * entries.
155
+ */
156
+ clear() {
157
+ this.#shelves.length = 0;
158
+ this.#entries.clear();
159
+ this.#nextY = 0;
160
+ this.#usedArea = 0;
161
+ }
162
+ }
163
+ //# sourceMappingURL=shelf-allocator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shelf-allocator.js","sourceRoot":"","sources":["../src/shelf-allocator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AA4B7C,SAAS,eAAe,CAAC,IAAY,EAAE,KAAa;IACnD,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;QAChD,MAAM,IAAI,UAAU,CACnB,GAAG,IAAI,oCAAoC,aAAa,CAAC,KAAK,CAAC,EAAE,CACjE,CAAC;IACH,CAAC;AACF,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,cAAc;IACjB,MAAM,CAAS;IACf,OAAO,CAAS;IAChB,QAAQ,GAAY,EAAE,CAAC;IACvB,QAAQ,GAAG,IAAI,GAAG,EAAiB,CAAC;IAC7C,OAAO,GAAG,CAAC,CAAC;IACZ,MAAM,GAAG,CAAC,CAAC;IACX,SAAS,GAAG,CAAC,CAAC;IAEd;;;OAGG;IACH,YAAY,KAAa,EAAE,MAAc;QACxC,eAAe,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QAChC,eAAe,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAClC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,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 50 % contour, magnitudes
4
+ * capped at `range`. Rows are stored contiguously, `index = y · width + x`,
5
+ * texel centres at `(x, y)`.
6
+ *
7
+ * Each edge texel (coverage strictly between 0 and 255) locates the contour
8
+ * inside it: the normal is the Sobel gradient of the coverage, and the offset
9
+ * from the texel centre inverts the exact area coverage of a straight edge
10
+ * through a unit square at that angle, so the field keeps sub-texel edges
11
+ * instead of snapping to a threshold. Binary transitions (a full texel beside
12
+ * an empty one, as hand-drawn masks have) put the contour midway. Each contour
13
+ * piece lowers the distance of every texel within `range` of it, by the
14
+ * distance to the piece's segment, which a bare point would overestimate by up
15
+ * to a quarter texel between samples. Texels farther than `range` from any
16
+ * contour saturate at `±range` (inside when coverage is at least 128).
17
+ *
18
+ * Accurate to 0.1 texel on discs of radius 6 and more; the TinySDF seeding of
19
+ * {@link squaredDistanceTransform2} (a seed adds its offset to a squared
20
+ * distance) is 0.5 texel off near the edge, so use this for fields that keep
21
+ * sub-texel edges and the transform for exact distances between feature cells.
22
+ *
23
+ * O(edge texels · range²) time, O(1) extra memory, no allocation.
24
+ *
25
+ * @example
26
+ * const coverage = new Uint8Array([0, 0, 128, 255, 255]);
27
+ * signedDistanceFromCoverage2(new Float32Array(5), coverage, 5, 1, 4);
28
+ * // [2, 1, 0, -1, -2] within a tenth of a texel
29
+ *
30
+ * @param out - At least `width · height` entries; overwritten.
31
+ * @param coverage - `width · height` bytes, 0 outside to 255 inside.
32
+ * @param range - The most distance kept, in texels, positive and finite.
33
+ * @returns `out`.
34
+ * @throws {RangeError} When a size is not a positive integer, `range` is not
35
+ * positive and finite, `coverage` or `out` is too short, or `out` shares
36
+ * bytes with the first `width · height` entries of `coverage` (checked before
37
+ * anything is written: the field is built in `out` while `coverage` is read).
38
+ * Views over a `SharedArrayBuffer` and over any other shared buffer, a clone
39
+ * of it included, are taken to share memory.
40
+ */
41
+ export declare function signedDistanceFromCoverage2(out: Float32Array, coverage: Uint8Array, width: number, height: number, range: number): Float32Array;
42
+ //# 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":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,wBAAgB,2BAA2B,CAC1C,GAAG,EAAE,YAAY,EACjB,QAAQ,EAAE,UAAU,EACpB,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,GACX,YAAY,CA8Dd"}
@@ -0,0 +1,145 @@
1
+ import { buffersMayShareMemory, describeValue } from "@ac-kit/core";
2
+ /**
3
+ * Signed distance field of an antialiased coverage grid, in texels: negative
4
+ * inside the shape, positive outside, zero on the 50 % contour, magnitudes
5
+ * capped at `range`. Rows are stored contiguously, `index = y · width + x`,
6
+ * texel centres at `(x, y)`.
7
+ *
8
+ * Each edge texel (coverage strictly between 0 and 255) locates the contour
9
+ * inside it: the normal is the Sobel gradient of the coverage, and the offset
10
+ * from the texel centre inverts the exact area coverage of a straight edge
11
+ * through a unit square at that angle, so the field keeps sub-texel edges
12
+ * instead of snapping to a threshold. Binary transitions (a full texel beside
13
+ * an empty one, as hand-drawn masks have) put the contour midway. Each contour
14
+ * piece lowers the distance of every texel within `range` of it, by the
15
+ * distance to the piece's segment, which a bare point would overestimate by up
16
+ * to a quarter texel between samples. Texels farther than `range` from any
17
+ * contour saturate at `±range` (inside when coverage is at least 128).
18
+ *
19
+ * Accurate to 0.1 texel on discs of radius 6 and more; the TinySDF seeding of
20
+ * {@link squaredDistanceTransform2} (a seed adds its offset to a squared
21
+ * distance) is 0.5 texel off near the edge, so use this for fields that keep
22
+ * sub-texel edges and the transform for exact distances between feature cells.
23
+ *
24
+ * O(edge texels · range²) time, O(1) extra memory, no allocation.
25
+ *
26
+ * @example
27
+ * const coverage = new Uint8Array([0, 0, 128, 255, 255]);
28
+ * signedDistanceFromCoverage2(new Float32Array(5), coverage, 5, 1, 4);
29
+ * // [2, 1, 0, -1, -2] within a tenth of a texel
30
+ *
31
+ * @param out - At least `width · height` entries; overwritten.
32
+ * @param coverage - `width · height` bytes, 0 outside to 255 inside.
33
+ * @param range - The most distance kept, in texels, positive and finite.
34
+ * @returns `out`.
35
+ * @throws {RangeError} When a size is not a positive integer, `range` is not
36
+ * positive and finite, `coverage` or `out` is too short, or `out` shares
37
+ * bytes with the first `width · height` entries of `coverage` (checked before
38
+ * anything is written: the field is built in `out` while `coverage` is read).
39
+ * Views over a `SharedArrayBuffer` and over any other shared buffer, a clone
40
+ * of it included, are taken to share memory.
41
+ */
42
+ export function signedDistanceFromCoverage2(out, coverage, width, height, range) {
43
+ if (!Number.isSafeInteger(width) ||
44
+ width <= 0 ||
45
+ !Number.isSafeInteger(height) ||
46
+ height <= 0) {
47
+ throw new RangeError(`width and height must be positive integers, got ${describeValue(width)}×${describeValue(height)}`);
48
+ }
49
+ if (!(range > 0) || !Number.isFinite(range)) {
50
+ throw new RangeError(`range must be positive and finite, got ${describeValue(range)}`);
51
+ }
52
+ const count = width * height;
53
+ if (coverage.length < count || out.length < count) {
54
+ throw new RangeError(`coverage and out must hold ${count} entries, got ${coverage.length} and ${out.length}`);
55
+ }
56
+ if (buffersMayShareMemory(coverage.buffer, out.buffer) &&
57
+ coverage.byteOffset < out.byteOffset + count * 4 &&
58
+ out.byteOffset < coverage.byteOffset + count) {
59
+ throw new RangeError("out must not overlap coverage");
60
+ }
61
+ out.fill(range, 0, count);
62
+ const reach = Math.ceil(range) + 1;
63
+ for (let y = 0; y < height; y += 1) {
64
+ for (let x = 0; x < width; x += 1) {
65
+ const value = coverage[y * width + x];
66
+ if (value > 0 && value < 255) {
67
+ splatEdge_(coverage, x, y, value / 255, width, height, range, reach, out);
68
+ }
69
+ else {
70
+ const opposite = value === 255 ? 0 : 255;
71
+ if (x + 1 < width && coverage[y * width + x + 1] === opposite) {
72
+ splat_(x + 0.5, y, 1, 0, 0.5, width, height, reach, out);
73
+ }
74
+ if (y + 1 < height && coverage[(y + 1) * width + x] === opposite) {
75
+ splat_(x, y + 0.5, 0, 1, 0.5, width, height, reach, out);
76
+ }
77
+ }
78
+ }
79
+ }
80
+ for (let index = 0; index < count; index += 1) {
81
+ const distance = Math.min(out[index], range);
82
+ out[index] = coverage[index] >= 128 ? -distance : distance;
83
+ }
84
+ return out;
85
+ }
86
+ function at_(coverage, width, height, x, y) {
87
+ return coverage[Math.min(height - 1, Math.max(0, y)) * width +
88
+ Math.min(width - 1, Math.max(0, x))];
89
+ }
90
+ function splatEdge_(coverage, x, y, alpha, width, height, _range, reach, out) {
91
+ const c = (dx, dy) => at_(coverage, width, height, x + dx, y + dy);
92
+ // the Sobel gradient points toward more coverage; the outward normal is its opposite
93
+ const gradientX = c(1, -1) + 2 * c(1, 0) + c(1, 1) - c(-1, -1) - 2 * c(-1, 0) - c(-1, 1);
94
+ const gradientY = c(-1, 1) + 2 * c(0, 1) + c(1, 1) - c(-1, -1) - 2 * c(0, -1) - c(1, -1);
95
+ const length = Math.hypot(gradientX, gradientY);
96
+ if (length === 0) {
97
+ splat_(x, y, 1, 0, 0, width, height, reach, out);
98
+ return;
99
+ }
100
+ const outwardX = -gradientX / length;
101
+ const outwardY = -gradientY / length;
102
+ // a straight edge through a unit square with normal (a, b), a ≥ b ≥ 0:
103
+ // coverage is a function of the edge's offset from the centre, inverted
104
+ const a = Math.max(Math.abs(outwardX), Math.abs(outwardY));
105
+ const b = Math.min(Math.abs(outwardX), Math.abs(outwardY));
106
+ let offset;
107
+ if (b < 1e-6) {
108
+ offset = a * (alpha - 0.5);
109
+ }
110
+ else if (alpha <= b / (2 * a)) {
111
+ offset = -(a + b) / 2 + Math.sqrt(2 * a * b * alpha);
112
+ }
113
+ else if (alpha >= 1 - b / (2 * a)) {
114
+ offset = (a + b) / 2 - Math.sqrt(2 * a * b * (1 - alpha));
115
+ }
116
+ else {
117
+ offset = a * (alpha - 0.5);
118
+ }
119
+ // consecutive edge texels are 1 / a apart along the contour: this one's share
120
+ splat_(x + outwardX * offset, y + outwardY * offset, outwardX, outwardY, 0.5 / a, width, height, reach, out);
121
+ }
122
+ /**
123
+ * Lowers the distance of every texel within reach of the contour segment
124
+ * through (`pointX`, `pointY`), normal (`normalX`, `normalY`), of half-length
125
+ * `half` along its tangent.
126
+ */
127
+ function splat_(pointX, pointY, normalX, normalY, half, width, height, reach, out) {
128
+ const left = Math.max(0, Math.round(pointX) - reach);
129
+ const right = Math.min(width - 1, Math.round(pointX) + reach);
130
+ const top = Math.max(0, Math.round(pointY) - reach);
131
+ const bottom = Math.min(height - 1, Math.round(pointY) + reach);
132
+ for (let y = top; y <= bottom; y += 1) {
133
+ const dy = y - pointY;
134
+ for (let x = left; x <= right; x += 1) {
135
+ const dx = x - pointX;
136
+ const across = normalX * dx + normalY * dy;
137
+ const along = Math.max(0, Math.abs(-normalY * dx + normalX * dy) - half);
138
+ const value = Math.sqrt(across * across + along * along);
139
+ if (value < out[y * width + x]) {
140
+ out[y * width + x] = value;
141
+ }
142
+ }
143
+ }
144
+ }
145
+ //# 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"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,MAAM,UAAU,2BAA2B,CAC1C,GAAiB,EACjB,QAAoB,EACpB,KAAa,EACb,MAAc,EACd,KAAa;IAEb,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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AG,KAAK,CAAC,CAAC;IAChE,KAAK,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACvC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC;QACtB,KAAK,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC,IAAI,KAAK,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;YACvC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC;YACtB,MAAM,MAAM,GAAG,OAAO,GAAG,EAAE,GAAG,OAAO,GAAG,EAAE,CAAC;YAC3C,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,GAAG,EAAE,GAAG,OAAO,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;YACzE,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,CAAC,CAAC;YACzD,IAAI,KAAK,GAAI,GAAG,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,CAAY,EAAE,CAAC;gBAC5C,GAAG,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;YAC5B,CAAC;QACF,CAAC;IACF,CAAC;AACF,CAAC"}
@@ -0,0 +1,20 @@
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 a squared sub-texel distance for an
5
+ * edge cell estimated from coverage (TinySDF's seeding) — and becomes `min over
6
+ * q of (|p − q|² + f(q))`, in cell units. Rows are stored contiguously, `grid[y *
7
+ * width + x]`.
8
+ *
9
+ * In place; `scratch` (at least `3 · max(width, height) + 1` entries) avoids
10
+ * the allocation of a working buffer. A grid with no finite seed stays
11
+ * `Infinity`.
12
+ *
13
+ * @param grid - The seeds, at least `width · height` entries; overwritten.
14
+ * @returns `grid`.
15
+ * @throws {RangeError} When a size is not a positive integer, when `grid` or
16
+ * `scratch` is too short, when `scratch` shares bytes with the first `width ·
17
+ * height` cells of `grid`, or when a seed is `NaN` or negative.
18
+ */
19
+ export declare function squaredDistanceTransform2(grid: Float64Array, width: number, height: number, scratch?: Float64Array): Float64Array;
20
+ //# 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;;;;;;;;;;;;;;;;;GAiBG;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,118 @@
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 a squared sub-texel distance for an
6
+ * edge cell estimated from coverage (TinySDF's seeding) — and becomes `min over
7
+ * q of (|p − q|² + f(q))`, in cell units. Rows are stored contiguously, `grid[y *
8
+ * width + x]`.
9
+ *
10
+ * In place; `scratch` (at least `3 · max(width, height) + 1` entries) avoids
11
+ * the allocation of a working buffer. A grid with no finite seed stays
12
+ * `Infinity`.
13
+ *
14
+ * @param grid - The seeds, at least `width · height` entries; overwritten.
15
+ * @returns `grid`.
16
+ * @throws {RangeError} When a size is not a positive integer, when `grid` or
17
+ * `scratch` is too short, when `scratch` shares bytes with the first `width ·
18
+ * height` cells of `grid`, or when a seed is `NaN` or negative.
19
+ */
20
+ export function squaredDistanceTransform2(grid, width, height, scratch) {
21
+ if (!Number.isSafeInteger(width) ||
22
+ width <= 0 ||
23
+ !Number.isSafeInteger(height) ||
24
+ height <= 0) {
25
+ throw new RangeError(`width and height must be positive integers, got ${describeValue(width)}×${describeValue(height)}`);
26
+ }
27
+ const cellCount = width * height;
28
+ if (grid.length < cellCount) {
29
+ throw new RangeError(`grid holds ${grid.length} cells, ${cellCount} needed`);
30
+ }
31
+ const longest = Math.max(width, height);
32
+ const needed = 3 * longest + 1;
33
+ if (scratch === undefined) {
34
+ scratch = new Float64Array(needed);
35
+ }
36
+ else {
37
+ if (scratch.length < needed) {
38
+ throw new RangeError(`scratch holds ${scratch.length} entries, ${needed} needed`);
39
+ }
40
+ if (scratch.buffer === grid.buffer &&
41
+ scratch.byteOffset < grid.byteOffset + cellCount * 8 &&
42
+ grid.byteOffset < scratch.byteOffset + needed * 8) {
43
+ throw new RangeError("scratch must not overlap grid");
44
+ }
45
+ }
46
+ for (let index = 0; index < cellCount; index += 1) {
47
+ const seed = grid[index];
48
+ if (!(seed >= 0)) {
49
+ throw new RangeError(`seed ${seed} at cell ${index} is not a non-negative number`);
50
+ }
51
+ }
52
+ for (let x = 0; x < width; x += 1) {
53
+ transformLine_(grid, x, width, height, scratch);
54
+ }
55
+ for (let y = 0; y < height; y += 1) {
56
+ transformLine_(grid, y * width, 1, width, scratch);
57
+ }
58
+ return grid;
59
+ }
60
+ /**
61
+ * One-dimensional lower envelope of parabolas over `count` cells `stride`
62
+ * apart. Parabolas of infinite seeds are skipped, which avoids the `Infinity −
63
+ * Infinity` of the textbook formulation. O(count).
64
+ */
65
+ function transformLine_(grid, offset, stride, count, scratch) {
66
+ // scratch layout: seeds [0, count), parabola positions [count, 2·count),
67
+ // boundaries [2·count, 3·count].
68
+ const positions = count;
69
+ const boundaries = 2 * count;
70
+ let last = -1;
71
+ for (let q = 0; q < count; q += 1) {
72
+ const seed = grid[offset + q * stride];
73
+ scratch[q] = seed;
74
+ if (seed === Infinity) {
75
+ continue;
76
+ }
77
+ if (last < 0) {
78
+ last = 0;
79
+ scratch[positions] = q;
80
+ scratch[boundaries] = -Infinity;
81
+ scratch[boundaries + 1] = Infinity;
82
+ continue;
83
+ }
84
+ let start;
85
+ for (;;) {
86
+ const p = scratch[positions + last];
87
+ // Intersection of two parabolas, written without the sum of a seed
88
+ // and a square so that seeds near MAX_VALUE cannot overflow.
89
+ start = (seed - scratch[p]) / (2 * (q - p)) + (q + p) / 2;
90
+ if (start > scratch[boundaries + last]) {
91
+ break;
92
+ }
93
+ last -= 1;
94
+ if (last < 0) {
95
+ // Every earlier parabola is dominated: the new one starts the envelope.
96
+ start = -Infinity;
97
+ break;
98
+ }
99
+ }
100
+ last += 1;
101
+ scratch[positions + last] = q;
102
+ scratch[boundaries + last] = start;
103
+ scratch[boundaries + last + 1] = Infinity;
104
+ }
105
+ if (last < 0) {
106
+ return;
107
+ }
108
+ let current = 0;
109
+ for (let q = 0; q < count; q += 1) {
110
+ while (scratch[boundaries + current + 1] < q) {
111
+ current += 1;
112
+ }
113
+ const p = scratch[positions + current];
114
+ const delta = q - p;
115
+ grid[offset + q * stride] = delta * delta + scratch[p];
116
+ }
117
+ }
118
+ //# 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;;;;;;;;;;;;;;;;;GAiBG;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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C,GAAG,KAAK,GAAG,KAAK,GAAI,OAAO,CAAC,CAAC,CAAY,CAAC;IACpE,CAAC;AACF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ac-kit/math-grid",
3
- "version": "0.2.2",
3
+ "version": "0.3.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,17 @@
45
45
  "prepack": "yarn build"
46
46
  },
47
47
  "dependencies": {
48
- "@ac-kit/algo": "^0.3.1",
49
- "@ac-kit/math-geometry": "^0.2.0",
50
- "@ac-kit/math-linear": "^0.1.7",
48
+ "@ac-kit/algo": "^0.5.0",
49
+ "@ac-kit/core": "^0.12.0",
50
+ "@ac-kit/math-geometry": "^0.3.0",
51
+ "@ac-kit/math-linear": "^0.3.0",
51
52
  "@types/web": "^0.0.367"
52
53
  },
53
54
  "devDependencies": {
55
+ "@ac-kit/math-random": "0.4.0",
54
56
  "@ac-kit/tsconfig": "0.1.0",
55
57
  "@ac-kit/typedoc-config": "0.1.0",
56
- "@ac-kit/vitest-config": "0.1.1",
58
+ "@ac-kit/vitest-config": "0.2.0",
57
59
  "@typescript/native": "typescript@7.0.2",
58
60
  "rimraf": "6.1.4",
59
61
  "typedoc": "0.28.20",