@ac-kit/math-grid 0.4.0 → 0.5.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 +84 -0
- package/README.md +97 -3
- package/dist/_brand.d.ts +14 -0
- package/dist/_brand.d.ts.map +1 -0
- package/dist/_brand.js +2 -0
- package/dist/_brand.js.map +1 -0
- package/dist/_coverage-contour2.d.ts +8 -1
- package/dist/_coverage-contour2.d.ts.map +1 -1
- package/dist/_coverage-contour2.js +477 -79
- package/dist/_coverage-contour2.js.map +1 -1
- package/dist/_grid-offsets2.d.ts +15 -0
- package/dist/_grid-offsets2.d.ts.map +1 -0
- package/dist/_grid-offsets2.js +53 -0
- package/dist/_grid-offsets2.js.map +1 -0
- package/dist/_nearest-site-propagation2.d.ts +1 -1
- package/dist/_nearest-site-propagation2.d.ts.map +1 -1
- package/dist/_nearest-site-propagation2.js +215 -59
- package/dist/_nearest-site-propagation2.js.map +1 -1
- package/dist/breadth-first-grid2.d.ts +65 -0
- package/dist/breadth-first-grid2.d.ts.map +1 -0
- package/dist/breadth-first-grid2.js +116 -0
- package/dist/breadth-first-grid2.js.map +1 -0
- package/dist/flood-fill2.d.ts +39 -0
- package/dist/flood-fill2.d.ts.map +1 -0
- package/dist/flood-fill2.js +76 -0
- package/dist/flood-fill2.js.map +1 -0
- package/dist/grid-neighbors2.d.ts +45 -0
- package/dist/grid-neighbors2.d.ts.map +1 -0
- package/dist/grid-neighbors2.js +45 -0
- package/dist/grid-neighbors2.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/max-rects-pack.d.ts +90 -0
- package/dist/max-rects-pack.d.ts.map +1 -0
- package/dist/max-rects-pack.js +310 -0
- package/dist/max-rects-pack.js.map +1 -0
- package/dist/rasterize-line2.d.ts +33 -0
- package/dist/rasterize-line2.d.ts.map +1 -0
- package/dist/rasterize-line2.js +103 -0
- package/dist/rasterize-line2.js.map +1 -0
- package/dist/signed-distance-from-coverage2.d.ts.map +1 -1
- package/dist/signed-distance-from-coverage2.js +147 -59
- package/dist/signed-distance-from-coverage2.js.map +1 -1
- package/dist/tile/_tile-matrix.d.ts +33 -0
- package/dist/tile/_tile-matrix.d.ts.map +1 -0
- package/dist/tile/_tile-matrix.js +186 -0
- package/dist/tile/_tile-matrix.js.map +1 -0
- package/dist/tile/index.d.ts +8 -0
- package/dist/tile/index.d.ts.map +1 -0
- package/dist/tile/index.js +8 -0
- package/dist/tile/index.js.map +1 -0
- package/dist/tile/quadkey.d.ts +30 -0
- package/dist/tile/quadkey.d.ts.map +1 -0
- package/dist/tile/quadkey.js +91 -0
- package/dist/tile/quadkey.js.map +1 -0
- package/dist/tile/tile-address.d.ts +91 -0
- package/dist/tile/tile-address.d.ts.map +1 -0
- package/dist/tile/tile-address.js +228 -0
- package/dist/tile/tile-address.js.map +1 -0
- package/dist/tile/tile-index.d.ts +26 -0
- package/dist/tile/tile-index.d.ts.map +1 -0
- package/dist/tile/tile-index.js +2 -0
- package/dist/tile/tile-index.js.map +1 -0
- package/dist/tile/tile-level.d.ts +24 -0
- package/dist/tile/tile-level.d.ts.map +1 -0
- package/dist/tile/tile-level.js +53 -0
- package/dist/tile/tile-level.js.map +1 -0
- package/dist/tile/tile-matrix-set-json.d.ts +67 -0
- package/dist/tile/tile-matrix-set-json.d.ts.map +1 -0
- package/dist/tile/tile-matrix-set-json.js +247 -0
- package/dist/tile/tile-matrix-set-json.js.map +1 -0
- package/dist/tile/tile-matrix-set.d.ts +174 -0
- package/dist/tile/tile-matrix-set.d.ts.map +1 -0
- package/dist/tile/tile-matrix-set.js +244 -0
- package/dist/tile/tile-matrix-set.js.map +1 -0
- package/dist/tile/tile-range.d.ts +57 -0
- package/dist/tile/tile-range.d.ts.map +1 -0
- package/dist/tile/tile-range.js +140 -0
- package/dist/tile/tile-range.js.map +1 -0
- package/package.json +10 -9
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { checkGrid_, neighborOffsets_ } from "./_grid-offsets2.js";
|
|
2
|
+
/** A cell `passable` refused, told apart from one not yet reached. */
|
|
3
|
+
const BLOCKED_ = -2;
|
|
4
|
+
/**
|
|
5
|
+
* Shortest paths in steps from one cell to every cell of a grid — breadth-first
|
|
6
|
+
* search, the uniform-cost case of Dijkstra's algorithm: the distance field a
|
|
7
|
+
* flow of agents follows, the reach of a move, or a shortest path once read
|
|
8
|
+
* back with {@link gridPath2}.
|
|
9
|
+
*
|
|
10
|
+
* A step goes to a neighbour (`connectivity`, 4 by default) for which
|
|
11
|
+
* `passable(col, row)` holds; it is asked once per cell at most. The start is
|
|
12
|
+
* always at distance 0, whatever `passable` says of it. Among equally short
|
|
13
|
+
* paths the one recorded is the first found, neighbours being tried in the
|
|
14
|
+
* order {@link gridNeighbors2} lists them, so the result is deterministic.
|
|
15
|
+
* O(cells) time; allocates the two result arrays and a queue of the same size.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const search = breadthFirstGrid2(3, 3, 0, 0, (col, row) => !(col === 1 && row < 2));
|
|
20
|
+
* search.distances[2]; // 6 — around the wall, (0,0) to (2,0)
|
|
21
|
+
* ```;
|
|
22
|
+
*
|
|
23
|
+
* @param width The number of columns.
|
|
24
|
+
* @param height The number of rows.
|
|
25
|
+
* @param col The start cell's column.
|
|
26
|
+
* @param row The start cell's row.
|
|
27
|
+
* @param passable Whether a cell can be stepped on.
|
|
28
|
+
* @param connectivity Which cells are neighbours; `4` by default.
|
|
29
|
+
* @throws {RangeError} When the size is not positive integers, the start is off
|
|
30
|
+
* the grid or not integral, or the connectivity is unknown.
|
|
31
|
+
*/
|
|
32
|
+
export function breadthFirstGrid2(width, height, col, row, passable, connectivity = 4) {
|
|
33
|
+
checkGrid_("breadthFirstGrid2", width, height, col, row);
|
|
34
|
+
neighborOffsets_(connectivity, 0, 0);
|
|
35
|
+
if (col < 0 || col >= width || row < 0 || row >= height) {
|
|
36
|
+
throw new RangeError(`breadthFirstGrid2: start (${col}, ${row}) is off the ${width} × ${height} grid`);
|
|
37
|
+
}
|
|
38
|
+
const count = width * height;
|
|
39
|
+
const distances = new Int32Array(count).fill(-1);
|
|
40
|
+
const previous = new Int32Array(count).fill(-1);
|
|
41
|
+
const queue = new Int32Array(count);
|
|
42
|
+
const start = row * width + col;
|
|
43
|
+
let head = 0;
|
|
44
|
+
let tail = 0;
|
|
45
|
+
distances[start] = 0;
|
|
46
|
+
queue[tail++] = start;
|
|
47
|
+
while (head < tail) {
|
|
48
|
+
const cell = queue[head++];
|
|
49
|
+
const cellRow = Math.floor(cell / width);
|
|
50
|
+
const cellCol = cell - cellRow * width;
|
|
51
|
+
const nextDistance = distances[cell] + 1;
|
|
52
|
+
const offsets = neighborOffsets_(connectivity, cellCol, cellRow);
|
|
53
|
+
for (let index = 0; index < offsets.length; index += 2) {
|
|
54
|
+
const nextCol = cellCol + offsets[index];
|
|
55
|
+
const nextRow = cellRow + offsets[index + 1];
|
|
56
|
+
if (nextCol < 0 || nextCol >= width || nextRow < 0 || nextRow >= height) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const next = nextRow * width + nextCol;
|
|
60
|
+
if (distances[next] !== -1) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (passable(nextCol, nextRow)) {
|
|
64
|
+
distances[next] = nextDistance;
|
|
65
|
+
previous[next] = cell;
|
|
66
|
+
queue[tail++] = next;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
// Asked once: a blocked cell is marked until the search ends.
|
|
70
|
+
distances[next] = BLOCKED_;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
for (let index = 0; index < count; index++) {
|
|
75
|
+
if (distances[index] === BLOCKED_) {
|
|
76
|
+
distances[index] = -1;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return { width, height, start, distances, previous };
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The shortest path a {@link breadthFirstGrid2} search found from its start to
|
|
83
|
+
* the cell `(col, row)`, as cell indices from the start to that cell, both
|
|
84
|
+
* included; empty when the cell cannot be reached. O(path length).
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* const search = breadthFirstGrid2(3, 1, 0, 0, () => true);
|
|
89
|
+
* gridPath2(search, 2, 0); // Int32Array [0, 1, 2]
|
|
90
|
+
* ```;
|
|
91
|
+
*
|
|
92
|
+
* @throws {RangeError} When the cell is off the searched grid.
|
|
93
|
+
*/
|
|
94
|
+
export function gridPath2(search, col, row) {
|
|
95
|
+
const { width, height, distances, previous } = search;
|
|
96
|
+
if (!Number.isSafeInteger(col) ||
|
|
97
|
+
!Number.isSafeInteger(row) ||
|
|
98
|
+
col < 0 ||
|
|
99
|
+
col >= width ||
|
|
100
|
+
row < 0 ||
|
|
101
|
+
row >= height) {
|
|
102
|
+
throw new RangeError(`gridPath2: cell (${col}, ${row}) is off the ${width} × ${height} grid`);
|
|
103
|
+
}
|
|
104
|
+
let cell = row * width + col;
|
|
105
|
+
const steps = distances[cell];
|
|
106
|
+
if (steps < 0) {
|
|
107
|
+
return new Int32Array(0);
|
|
108
|
+
}
|
|
109
|
+
const path = new Int32Array(steps + 1);
|
|
110
|
+
for (let index = steps; index >= 0; index--) {
|
|
111
|
+
path[index] = cell;
|
|
112
|
+
cell = previous[cell];
|
|
113
|
+
}
|
|
114
|
+
return path;
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=breadth-first-grid2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"breadth-first-grid2.js","sourceRoot":"","sources":["../src/breadth-first-grid2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAGnE,sEAAsE;AACtE,MAAM,QAAQ,GAAG,CAAC,CAAC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 -0,0 +1,39 @@
|
|
|
1
|
+
import type { GridConnectivity2 } from "./grid-neighbors2.js";
|
|
2
|
+
/**
|
|
3
|
+
* Fills the region connected to `(col, row)`: marks with `1` in `out` every
|
|
4
|
+
* cell reachable from it through neighbours for which `inside(col, row)` holds
|
|
5
|
+
* — the paint-bucket fill, region selection, or the island of a cell.
|
|
6
|
+
*
|
|
7
|
+
* `out` is a `width × height` mask in row-major order (`index = row · width +
|
|
8
|
+
* col`), and also the record of what is filled: a cell already non-zero is
|
|
9
|
+
* neither tested nor entered, so several fills can share one mask, and a mask
|
|
10
|
+
* pre-marked with walls bounds a fill without a test. Nothing happens when the
|
|
11
|
+
* start lies off the grid, is already marked, or is not inside.
|
|
12
|
+
*
|
|
13
|
+
* Breadth-first over a queue of cell indices: every cell is tested at most once
|
|
14
|
+
* per neighbour and entered once, O(cells) time; the queue is the one
|
|
15
|
+
* allocation, `4 · width · height` bytes.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* // A 4 × 3 picture with a wall in column 1.
|
|
20
|
+
* const wall = [1, 5, 9];
|
|
21
|
+
* const mask = new Uint8Array(12);
|
|
22
|
+
* floodFill2(mask, 4, 3, 0, 0, (col, row) => !wall.includes(row * 4 + col));
|
|
23
|
+
* // 3: the column left of the wall
|
|
24
|
+
* ```;
|
|
25
|
+
*
|
|
26
|
+
* @param out The mask to fill, `width · height` cells.
|
|
27
|
+
* @param width The number of columns.
|
|
28
|
+
* @param height The number of rows.
|
|
29
|
+
* @param col The start cell's column.
|
|
30
|
+
* @param row The start cell's row.
|
|
31
|
+
* @param inside Whether a cell belongs to the region.
|
|
32
|
+
* @param connectivity Which cells are neighbours; `4` by default.
|
|
33
|
+
* @returns The number of cells filled by this call.
|
|
34
|
+
* @throws {RangeError} When the size is not positive integers, `out` has
|
|
35
|
+
* another length, a coordinate is not an integer, or the connectivity is
|
|
36
|
+
* unknown.
|
|
37
|
+
*/
|
|
38
|
+
export declare function floodFill2(out: Uint8Array, width: number, height: number, col: number, row: number, inside: (col: number, row: number) => boolean, connectivity?: GridConnectivity2): number;
|
|
39
|
+
//# sourceMappingURL=flood-fill2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flood-fill2.d.ts","sourceRoot":"","sources":["../src/flood-fill2.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,UAAU,CACzB,GAAG,EAAE,UAAU,EACf,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,OAAO,EAC7C,YAAY,GAAE,iBAAqB,GACjC,MAAM,CAwCR"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { checkGrid_, neighborOffsets_ } from "./_grid-offsets2.js";
|
|
2
|
+
/**
|
|
3
|
+
* Fills the region connected to `(col, row)`: marks with `1` in `out` every
|
|
4
|
+
* cell reachable from it through neighbours for which `inside(col, row)` holds
|
|
5
|
+
* — the paint-bucket fill, region selection, or the island of a cell.
|
|
6
|
+
*
|
|
7
|
+
* `out` is a `width × height` mask in row-major order (`index = row · width +
|
|
8
|
+
* col`), and also the record of what is filled: a cell already non-zero is
|
|
9
|
+
* neither tested nor entered, so several fills can share one mask, and a mask
|
|
10
|
+
* pre-marked with walls bounds a fill without a test. Nothing happens when the
|
|
11
|
+
* start lies off the grid, is already marked, or is not inside.
|
|
12
|
+
*
|
|
13
|
+
* Breadth-first over a queue of cell indices: every cell is tested at most once
|
|
14
|
+
* per neighbour and entered once, O(cells) time; the queue is the one
|
|
15
|
+
* allocation, `4 · width · height` bytes.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* // A 4 × 3 picture with a wall in column 1.
|
|
20
|
+
* const wall = [1, 5, 9];
|
|
21
|
+
* const mask = new Uint8Array(12);
|
|
22
|
+
* floodFill2(mask, 4, 3, 0, 0, (col, row) => !wall.includes(row * 4 + col));
|
|
23
|
+
* // 3: the column left of the wall
|
|
24
|
+
* ```;
|
|
25
|
+
*
|
|
26
|
+
* @param out The mask to fill, `width · height` cells.
|
|
27
|
+
* @param width The number of columns.
|
|
28
|
+
* @param height The number of rows.
|
|
29
|
+
* @param col The start cell's column.
|
|
30
|
+
* @param row The start cell's row.
|
|
31
|
+
* @param inside Whether a cell belongs to the region.
|
|
32
|
+
* @param connectivity Which cells are neighbours; `4` by default.
|
|
33
|
+
* @returns The number of cells filled by this call.
|
|
34
|
+
* @throws {RangeError} When the size is not positive integers, `out` has
|
|
35
|
+
* another length, a coordinate is not an integer, or the connectivity is
|
|
36
|
+
* unknown.
|
|
37
|
+
*/
|
|
38
|
+
export function floodFill2(out, width, height, col, row, inside, connectivity = 4) {
|
|
39
|
+
checkGrid_("floodFill2", width, height, col, row);
|
|
40
|
+
if (out.length !== width * height) {
|
|
41
|
+
throw new RangeError(`floodFill2: mask of ${out.length} cells for a ${width} × ${height} grid`);
|
|
42
|
+
}
|
|
43
|
+
neighborOffsets_(connectivity, 0, 0);
|
|
44
|
+
if (col < 0 || col >= width || row < 0 || row >= height) {
|
|
45
|
+
return 0;
|
|
46
|
+
}
|
|
47
|
+
const start = row * width + col;
|
|
48
|
+
if (out[start] !== 0 || !inside(col, row)) {
|
|
49
|
+
return 0;
|
|
50
|
+
}
|
|
51
|
+
const queue = new Int32Array(width * height);
|
|
52
|
+
let head = 0;
|
|
53
|
+
let tail = 0;
|
|
54
|
+
out[start] = 1;
|
|
55
|
+
queue[tail++] = start;
|
|
56
|
+
while (head < tail) {
|
|
57
|
+
const cell = queue[head++];
|
|
58
|
+
const cellRow = Math.floor(cell / width);
|
|
59
|
+
const cellCol = cell - cellRow * width;
|
|
60
|
+
const offsets = neighborOffsets_(connectivity, cellCol, cellRow);
|
|
61
|
+
for (let index = 0; index < offsets.length; index += 2) {
|
|
62
|
+
const nextCol = cellCol + offsets[index];
|
|
63
|
+
const nextRow = cellRow + offsets[index + 1];
|
|
64
|
+
if (nextCol < 0 || nextCol >= width || nextRow < 0 || nextRow >= height) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const next = nextRow * width + nextCol;
|
|
68
|
+
if (out[next] === 0 && inside(nextCol, nextRow)) {
|
|
69
|
+
out[next] = 1;
|
|
70
|
+
queue[tail++] = next;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return tail;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=flood-fill2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flood-fill2.js","sourceRoot":"","sources":["../src/flood-fill2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAGnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,UAAU,UAAU,CACzB,GAAe,EACf,KAAa,EACb,MAAc,EACd,GAAW,EACX,GAAW,EACX,MAA6C,EAC7C,YAAY,GAAsB,CAAC;IAEnC,UAAU,CAAC,YAAY,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IAClD,IAAI,GAAG,CAAC,MAAM,KAAK,KAAK,GAAG,MAAM,EAAE,CAAC;QACnC,MAAM,IAAI,UAAU,CACnB,uBAAuB,GAAG,CAAC,MAAM,gBAAgB,KAAK,MAAM,MAAM,OAAO,CACzE,CAAC;IACH,CAAC;IACD,gBAAgB,CAAC,YAAY,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IACrC,IAAI,GAAG,GAAG,CAAC,IAAI,GAAG,IAAI,KAAK,IAAI,GAAG,GAAG,CAAC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C;YACD,MAAM,IAAI,GAAG,OAAO,GAAG,KAAK,GAAG,OAAO,CAAC;YACvC,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,CAAC;gBACjD,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;gBACd,KAAK,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;YACtB,CAAC;QACF,CAAC;IACF,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which cells count as neighbours of a cell, on a grid of `width` columns and
|
|
3
|
+
* `height` rows:
|
|
4
|
+
*
|
|
5
|
+
* - `4` — the cells sharing an edge (von Neumann).
|
|
6
|
+
* - `8` — those and the four sharing only a corner (Moore).
|
|
7
|
+
* - `"hex-odd-r"`, `"hex-even-r"` — the six cells of a hexagonal grid stored in
|
|
8
|
+
* rows ("pointy-top" hexagons), every odd (or even) row shoved half a cell to
|
|
9
|
+
* the right.
|
|
10
|
+
* - `"hex-odd-q"`, `"hex-even-q"` — the same for a grid stored in columns
|
|
11
|
+
* ("flat-top" hexagons), every odd (or even) column shoved half a cell down.
|
|
12
|
+
*
|
|
13
|
+
* The four offset layouts are the ones of Red Blob Games' hexagonal-grid
|
|
14
|
+
* reference; a hexagonal grid stored any other way (axial, cube, doubled)
|
|
15
|
+
* converts to one of them.
|
|
16
|
+
*/
|
|
17
|
+
export type GridConnectivity2 = 4 | 8 | "hex-odd-r" | "hex-even-r" | "hex-odd-q" | "hex-even-q";
|
|
18
|
+
/**
|
|
19
|
+
* Calls `visit(col, row)` for each neighbour of the cell `(col, row)` that lies
|
|
20
|
+
* on the grid, `[0, width) × [0, height)`, and returns how many there were.
|
|
21
|
+
*
|
|
22
|
+
* Neighbours come counter-clockwise on screen (rows growing downwards) from the
|
|
23
|
+
* east: east, north, west, south for `4`; east, north-east, north, … for `8`;
|
|
24
|
+
* the six hexagonal neighbours likewise for the hexagonal layouts. The cell
|
|
25
|
+
* itself may lie off the grid; only its neighbours are bounded. No allocation.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* const cells: number[][] = [];
|
|
30
|
+
* gridNeighbors2(0, 0, 3, 3, 8, (col, row) => cells.push([col, row]));
|
|
31
|
+
* // cells: [[1, 0], [0, 1], [1, 1]] — a corner has three
|
|
32
|
+
* ```;
|
|
33
|
+
*
|
|
34
|
+
* @param col The cell's column.
|
|
35
|
+
* @param row The cell's row.
|
|
36
|
+
* @param width The number of columns.
|
|
37
|
+
* @param height The number of rows.
|
|
38
|
+
* @param connectivity Which cells count as neighbours.
|
|
39
|
+
* @param visit Called with each neighbour's column and row.
|
|
40
|
+
* @returns The number of neighbours visited.
|
|
41
|
+
* @throws {RangeError} When the size is not positive integers, a coordinate is
|
|
42
|
+
* not an integer, or the connectivity is unknown.
|
|
43
|
+
*/
|
|
44
|
+
export declare function gridNeighbors2(col: number, row: number, width: number, height: number, connectivity: GridConnectivity2, visit: (col: number, row: number) => void): number;
|
|
45
|
+
//# sourceMappingURL=grid-neighbors2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grid-neighbors2.d.ts","sourceRoot":"","sources":["../src/grid-neighbors2.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,iBAAiB,GAC1B,CAAC,GACD,CAAC,GACD,WAAW,GACX,YAAY,GACZ,WAAW,GACX,YAAY,CAAC;AAEhB;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,cAAc,CAC7B,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,iBAAiB,EAC/B,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,GACvC,MAAM,CAkBR"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { checkGrid_, neighborOffsets_ } from "./_grid-offsets2.js";
|
|
2
|
+
/**
|
|
3
|
+
* Calls `visit(col, row)` for each neighbour of the cell `(col, row)` that lies
|
|
4
|
+
* on the grid, `[0, width) × [0, height)`, and returns how many there were.
|
|
5
|
+
*
|
|
6
|
+
* Neighbours come counter-clockwise on screen (rows growing downwards) from the
|
|
7
|
+
* east: east, north, west, south for `4`; east, north-east, north, … for `8`;
|
|
8
|
+
* the six hexagonal neighbours likewise for the hexagonal layouts. The cell
|
|
9
|
+
* itself may lie off the grid; only its neighbours are bounded. No allocation.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const cells: number[][] = [];
|
|
14
|
+
* gridNeighbors2(0, 0, 3, 3, 8, (col, row) => cells.push([col, row]));
|
|
15
|
+
* // cells: [[1, 0], [0, 1], [1, 1]] — a corner has three
|
|
16
|
+
* ```;
|
|
17
|
+
*
|
|
18
|
+
* @param col The cell's column.
|
|
19
|
+
* @param row The cell's row.
|
|
20
|
+
* @param width The number of columns.
|
|
21
|
+
* @param height The number of rows.
|
|
22
|
+
* @param connectivity Which cells count as neighbours.
|
|
23
|
+
* @param visit Called with each neighbour's column and row.
|
|
24
|
+
* @returns The number of neighbours visited.
|
|
25
|
+
* @throws {RangeError} When the size is not positive integers, a coordinate is
|
|
26
|
+
* not an integer, or the connectivity is unknown.
|
|
27
|
+
*/
|
|
28
|
+
export function gridNeighbors2(col, row, width, height, connectivity, visit) {
|
|
29
|
+
checkGrid_("gridNeighbors2", width, height, col, row);
|
|
30
|
+
const offsets = neighborOffsets_(connectivity, col, row);
|
|
31
|
+
let count = 0;
|
|
32
|
+
for (let index = 0; index < offsets.length; index += 2) {
|
|
33
|
+
const neighborCol = col + offsets[index];
|
|
34
|
+
const neighborRow = row + offsets[index + 1];
|
|
35
|
+
if (neighborCol >= 0 &&
|
|
36
|
+
neighborCol < width &&
|
|
37
|
+
neighborRow >= 0 &&
|
|
38
|
+
neighborRow < height) {
|
|
39
|
+
visit(neighborCol, neighborRow);
|
|
40
|
+
count++;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return count;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=grid-neighbors2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grid-neighbors2.js","sourceRoot":"","sources":["../src/grid-neighbors2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AA0BnE;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,cAAc,CAC7B,GAAW,EACX,GAAW,EACX,KAAa,EACb,MAAc,EACd,YAA+B,EAC/B,KAAyC;IAEzC,UAAU,CAAC,gBAAgB,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IACtD,MAAM,OAAO,GAAG,gBAAgB,CAAC,YAAY,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;IACzD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC,EAAE,CAAC;QACxD,MAAM,WAAW,GAAG,GAAG,GAAG,OAAO,CAAC,KAAK,CAAE,CAAC;QAC1C,MAAM,WAAW,GAAG,GAAG,GAAG,OAAO,CAAC,KAAK,GAAG,CAAC,CAAE,CAAC;QAC9C,IACC,WAAW,IAAI,CAAC;YAChB,WAAW,GAAG,KAAK;YACnB,WAAW,IAAI,CAAC;YAChB,WAAW,GAAG,MAAM,EACnB,CAAC;YACF,KAAK,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;YAChC,KAAK,EAAE,CAAC;QACT,CAAC;IACF,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
export * from "./bounded-grid2.js";
|
|
2
|
+
export * from "./breadth-first-grid2.js";
|
|
3
|
+
export * from "./flood-fill2.js";
|
|
2
4
|
export * from "./frontier-tiling.js";
|
|
3
5
|
export * from "./grid-coord2.js";
|
|
6
|
+
export * from "./grid-neighbors2.js";
|
|
4
7
|
export * from "./grid-pack-dfs.js";
|
|
5
8
|
export * from "./grid-rect2.js";
|
|
6
9
|
export * from "./grid-size2.js";
|
|
7
10
|
export * from "./grid2-pixel.js";
|
|
8
11
|
export * from "./grid2.js";
|
|
9
12
|
export * from "./infinite-grid2.js";
|
|
13
|
+
export * from "./max-rects-pack.js";
|
|
14
|
+
export * from "./rasterize-line2.js";
|
|
10
15
|
export * from "./row-grid2.js";
|
|
11
16
|
export * from "./shelf-allocator.js";
|
|
12
17
|
export * from "./signed-distance-from-coverage2.js";
|
|
13
18
|
export * from "./squared-distance-transform2.js";
|
|
14
19
|
export * from "./strip-grid2.js";
|
|
20
|
+
export * from "./tile/index.js";
|
|
15
21
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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,sBAAsB,CAAC;AACrC,cAAc,qCAAqC,CAAC;AACpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,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,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,qCAAqC,CAAC;AACpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
export * from "./bounded-grid2.js";
|
|
2
|
+
export * from "./breadth-first-grid2.js";
|
|
3
|
+
export * from "./flood-fill2.js";
|
|
2
4
|
export * from "./frontier-tiling.js";
|
|
3
5
|
export * from "./grid-coord2.js";
|
|
6
|
+
export * from "./grid-neighbors2.js";
|
|
4
7
|
export * from "./grid-pack-dfs.js";
|
|
5
8
|
export * from "./grid-rect2.js";
|
|
6
9
|
export * from "./grid-size2.js";
|
|
7
10
|
export * from "./grid2-pixel.js";
|
|
8
11
|
export * from "./grid2.js";
|
|
9
12
|
export * from "./infinite-grid2.js";
|
|
13
|
+
export * from "./max-rects-pack.js";
|
|
14
|
+
export * from "./rasterize-line2.js";
|
|
10
15
|
export * from "./row-grid2.js";
|
|
11
16
|
export * from "./shelf-allocator.js";
|
|
12
17
|
export * from "./signed-distance-from-coverage2.js";
|
|
13
18
|
export * from "./squared-distance-transform2.js";
|
|
14
19
|
export * from "./strip-grid2.js";
|
|
20
|
+
export * from "./tile/index.js";
|
|
15
21
|
//# 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,sBAAsB,CAAC;AACrC,cAAc,qCAAqC,CAAC;AACpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,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,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,qCAAqC,CAAC;AACpD,cAAc,kCAAkC,CAAC;AACjD,cAAc,kBAAkB,CAAC;AACjC,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How {@link maxRectsPack} scores a candidate position, lower first (Jylänki,
|
|
3
|
+
* "A Thousand Ways to Pack the Bin", 2010):
|
|
4
|
+
*
|
|
5
|
+
* - `"best-short-side-fit"` — the smaller leftover between the item and the free
|
|
6
|
+
* rectangle's sides, then the larger: the default, and the best on most
|
|
7
|
+
* inputs.
|
|
8
|
+
* - `"best-long-side-fit"` — the larger leftover, then the smaller.
|
|
9
|
+
* - `"best-area-fit"` — the free rectangle's leftover area, then the smaller
|
|
10
|
+
* leftover side.
|
|
11
|
+
* - `"bottom-left"` — the item's far edge along `y`, then its `x` (Tetris
|
|
12
|
+
* placement; `y` grows away from the origin).
|
|
13
|
+
* - `"contact-point"` — the length of the item's perimeter touching the bin's
|
|
14
|
+
* edges or placed items, the longest first.
|
|
15
|
+
*/
|
|
16
|
+
export type MaxRectsHeuristic = "best-short-side-fit" | "best-long-side-fit" | "best-area-fit" | "bottom-left" | "contact-point";
|
|
17
|
+
/** A rectangle's size: a bin to pack into, or an item to place. */
|
|
18
|
+
export type MaxRectsSize = {
|
|
19
|
+
readonly width: number;
|
|
20
|
+
readonly height: number;
|
|
21
|
+
};
|
|
22
|
+
/** Where {@link maxRectsPack} placed an item. */
|
|
23
|
+
export type MaxRectsPlacement = {
|
|
24
|
+
/** The index of the bin it went into. */
|
|
25
|
+
readonly bin: number;
|
|
26
|
+
/** Its left edge in the bin. */
|
|
27
|
+
readonly x: number;
|
|
28
|
+
/** Its top edge in the bin. */
|
|
29
|
+
readonly y: number;
|
|
30
|
+
/** Whether it was turned a quarter: its width then lies along `y`. */
|
|
31
|
+
readonly rotated: boolean;
|
|
32
|
+
};
|
|
33
|
+
/** The options of {@link maxRectsPack}. */
|
|
34
|
+
export type MaxRectsPackOptions = {
|
|
35
|
+
/** How to score positions; `"best-short-side-fit"` by default. */
|
|
36
|
+
readonly heuristic?: MaxRectsHeuristic;
|
|
37
|
+
/** Whether items may be turned a quarter; `false` by default. */
|
|
38
|
+
readonly allowRotation?: boolean;
|
|
39
|
+
};
|
|
40
|
+
/** The result of {@link maxRectsPack}. */
|
|
41
|
+
export type MaxRectsPacking = {
|
|
42
|
+
/**
|
|
43
|
+
* Per item, in the order given, its placement, or `undefined` when none of
|
|
44
|
+
* the bins held it.
|
|
45
|
+
*/
|
|
46
|
+
readonly placements: readonly (MaxRectsPlacement | undefined)[];
|
|
47
|
+
/** Per bin, the share of its area the placed items cover, in `[0, 1]`. */
|
|
48
|
+
readonly occupancy: Float64Array;
|
|
49
|
+
/** The placed items' area over the area of the bins used (`0` when none is). */
|
|
50
|
+
readonly fillRatio: number;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Packs rectangles into bins with the MaxRects algorithm (Jylänki 2010): each
|
|
54
|
+
* bin keeps the maximal free rectangles left by what is placed, and the item
|
|
55
|
+
* placed next is the one, among all still unplaced, whose best position scores
|
|
56
|
+
* best under the heuristic — the "global best fit" variant, which needs no
|
|
57
|
+
* presorting and packs tighter than placing items in a fixed order.
|
|
58
|
+
*
|
|
59
|
+
* Bins are filled one after another: the first until no remaining item fits,
|
|
60
|
+
* then the second, and so on; an item fitting none is left out (`undefined`).
|
|
61
|
+
* Coordinates are integers from the bin's top-left corner (texels, pixels).
|
|
62
|
+
* Ties go to the earlier item, then the earlier free rectangle, so the result
|
|
63
|
+
* is deterministic.
|
|
64
|
+
*
|
|
65
|
+
* O(n² · f) for `n` items and `f` free rectangles (f grows roughly linearly
|
|
66
|
+
* with the items placed); for streaming allocation in an atlas, see
|
|
67
|
+
* {@link ShelfAllocator}.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* const { placements, fillRatio } = maxRectsPack(
|
|
72
|
+
* [{ width: 4, height: 4 }],
|
|
73
|
+
* [
|
|
74
|
+
* { width: 4, height: 2 },
|
|
75
|
+
* { width: 2, height: 2 },
|
|
76
|
+
* { width: 2, height: 2 },
|
|
77
|
+
* ],
|
|
78
|
+
* );
|
|
79
|
+
* // every item placed, fillRatio 1
|
|
80
|
+
* ```;
|
|
81
|
+
*
|
|
82
|
+
* @param bins The bins' sizes, filled in this order.
|
|
83
|
+
* @param items The items' sizes.
|
|
84
|
+
* @param options The heuristic and whether items may be rotated.
|
|
85
|
+
* @returns Each item's placement, and how full each bin is.
|
|
86
|
+
* @throws {RangeError} When a width or height is not a positive safe integer,
|
|
87
|
+
* or the heuristic is unknown.
|
|
88
|
+
*/
|
|
89
|
+
export declare function maxRectsPack(bins: readonly MaxRectsSize[], items: readonly MaxRectsSize[], options?: MaxRectsPackOptions): MaxRectsPacking;
|
|
90
|
+
//# sourceMappingURL=max-rects-pack.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"max-rects-pack.d.ts","sourceRoot":"","sources":["../src/max-rects-pack.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,iBAAiB,GAC1B,qBAAqB,GACrB,oBAAoB,GACpB,eAAe,GACf,aAAa,GACb,eAAe,CAAC;AAEnB,mEAAmE;AACnE,MAAM,MAAM,YAAY,GAAG;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,iDAAiD;AACjD,MAAM,MAAM,iBAAiB,GAAG;IAC/B,yCAAyC;IACzC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,gCAAgC;IAChC,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,+BAA+B;IAC/B,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,sEAAsE;IACtE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC1B,CAAC;AAEF,2CAA2C;AAC3C,MAAM,MAAM,mBAAmB,GAAG;IACjC,kEAAkE;IAClE,QAAQ,CAAC,SAAS,CAAC,EAAE,iBAAiB,CAAC;IACvC,iEAAiE;IACjE,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;CACjC,CAAC;AAEF,0CAA0C;AAC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