@ac-kit/math-grid 0.3.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 +149 -0
- package/README.md +117 -10
- 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 +55 -0
- package/dist/_coverage-contour2.d.ts.map +1 -0
- package/dist/_coverage-contour2.js +2068 -0
- package/dist/_coverage-contour2.js.map +1 -0
- 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 +80 -0
- package/dist/_nearest-site-propagation2.d.ts.map +1 -0
- package/dist/_nearest-site-propagation2.js +707 -0
- package/dist/_nearest-site-propagation2.js.map +1 -0
- 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/grid-pack-dfs.d.ts.map +1 -1
- package/dist/grid-pack-dfs.js +4 -2
- package/dist/grid-pack-dfs.js.map +1 -1
- 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/infinite-grid2.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/shelf-allocator.d.ts +7 -1
- package/dist/shelf-allocator.d.ts.map +1 -1
- package/dist/shelf-allocator.js +32 -32
- package/dist/shelf-allocator.js.map +1 -1
- package/dist/signed-distance-from-coverage2.d.ts +63 -21
- package/dist/signed-distance-from-coverage2.d.ts.map +1 -1
- package/dist/signed-distance-from-coverage2.js +327 -99
- package/dist/signed-distance-from-coverage2.js.map +1 -1
- package/dist/squared-distance-transform2.d.ts +9 -4
- package/dist/squared-distance-transform2.d.ts.map +1 -1
- package/dist/squared-distance-transform2.js +9 -4
- package/dist/squared-distance-transform2.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 -8
|
@@ -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;AAC1C,MAAM,MAAM,eAAe,GAAG;IAC7B;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,SAAS,CAAC,iBAAiB,GAAG,SAAS,CAAC,EAAE,CAAC;IAChE,0EAA0E;IAC1E,QAAQ,CAAC,SAAS,EAAE,YAAY,CAAC;IACjC,gFAAgF;IAChF,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC3B,CAAC;AAIF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,wBAAgB,YAAY,CAC3B,IAAI,EAAE,SAAS,YAAY,EAAE,EAC7B,KAAK,EAAE,SAAS,YAAY,EAAE,EAC9B,OAAO,GAAE,mBAAwB,GAC/B,eAAe,CAmGjB"}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { describeValue } from "@ac-kit/core";
|
|
2
|
+
/**
|
|
3
|
+
* Packs rectangles into bins with the MaxRects algorithm (Jylänki 2010): each
|
|
4
|
+
* bin keeps the maximal free rectangles left by what is placed, and the item
|
|
5
|
+
* placed next is the one, among all still unplaced, whose best position scores
|
|
6
|
+
* best under the heuristic — the "global best fit" variant, which needs no
|
|
7
|
+
* presorting and packs tighter than placing items in a fixed order.
|
|
8
|
+
*
|
|
9
|
+
* Bins are filled one after another: the first until no remaining item fits,
|
|
10
|
+
* then the second, and so on; an item fitting none is left out (`undefined`).
|
|
11
|
+
* Coordinates are integers from the bin's top-left corner (texels, pixels).
|
|
12
|
+
* Ties go to the earlier item, then the earlier free rectangle, so the result
|
|
13
|
+
* is deterministic.
|
|
14
|
+
*
|
|
15
|
+
* O(n² · f) for `n` items and `f` free rectangles (f grows roughly linearly
|
|
16
|
+
* with the items placed); for streaming allocation in an atlas, see
|
|
17
|
+
* {@link ShelfAllocator}.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const { placements, fillRatio } = maxRectsPack(
|
|
22
|
+
* [{ width: 4, height: 4 }],
|
|
23
|
+
* [
|
|
24
|
+
* { width: 4, height: 2 },
|
|
25
|
+
* { width: 2, height: 2 },
|
|
26
|
+
* { width: 2, height: 2 },
|
|
27
|
+
* ],
|
|
28
|
+
* );
|
|
29
|
+
* // every item placed, fillRatio 1
|
|
30
|
+
* ```;
|
|
31
|
+
*
|
|
32
|
+
* @param bins The bins' sizes, filled in this order.
|
|
33
|
+
* @param items The items' sizes.
|
|
34
|
+
* @param options The heuristic and whether items may be rotated.
|
|
35
|
+
* @returns Each item's placement, and how full each bin is.
|
|
36
|
+
* @throws {RangeError} When a width or height is not a positive safe integer,
|
|
37
|
+
* or the heuristic is unknown.
|
|
38
|
+
*/
|
|
39
|
+
export function maxRectsPack(bins, items, options = {}) {
|
|
40
|
+
const heuristic = options.heuristic ?? "best-short-side-fit";
|
|
41
|
+
if (!SCORERS_.has(heuristic)) {
|
|
42
|
+
throw new RangeError(`maxRectsPack: unknown heuristic ${describeValue(heuristic)}`);
|
|
43
|
+
}
|
|
44
|
+
const allowRotation = options.allowRotation ?? false;
|
|
45
|
+
for (const [index, bin] of bins.entries()) {
|
|
46
|
+
checkSize_(`bin ${index}`, bin);
|
|
47
|
+
}
|
|
48
|
+
for (const [index, item] of items.entries()) {
|
|
49
|
+
checkSize_(`item ${index}`, item);
|
|
50
|
+
}
|
|
51
|
+
const placements = Array.from({
|
|
52
|
+
length: items.length,
|
|
53
|
+
});
|
|
54
|
+
const occupancy = new Float64Array(bins.length);
|
|
55
|
+
const remaining = new Set(items.keys());
|
|
56
|
+
let placedArea = 0;
|
|
57
|
+
let usedBinArea = 0;
|
|
58
|
+
const best = new Candidate_();
|
|
59
|
+
for (let binIndex = 0; binIndex < bins.length && remaining.size > 0; binIndex++) {
|
|
60
|
+
const bin = bins[binIndex];
|
|
61
|
+
const free = [
|
|
62
|
+
{ x: 0, y: 0, width: bin.width, height: bin.height },
|
|
63
|
+
];
|
|
64
|
+
const used = [];
|
|
65
|
+
let binArea = 0;
|
|
66
|
+
for (;;) {
|
|
67
|
+
best.reset();
|
|
68
|
+
for (const itemIndex of remaining) {
|
|
69
|
+
const item = items[itemIndex];
|
|
70
|
+
findPosition_(best, itemIndex, item.width, item.height, false, free, used, bin, heuristic);
|
|
71
|
+
if (allowRotation && item.width !== item.height) {
|
|
72
|
+
findPosition_(best, itemIndex, item.height, item.width, true, free, used, bin, heuristic);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (best.item < 0) {
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
const placed = {
|
|
79
|
+
x: best.x,
|
|
80
|
+
y: best.y,
|
|
81
|
+
width: best.width,
|
|
82
|
+
height: best.height,
|
|
83
|
+
};
|
|
84
|
+
splitFree_(free, placed);
|
|
85
|
+
pruneFree_(free);
|
|
86
|
+
used.push(placed);
|
|
87
|
+
remaining.delete(best.item);
|
|
88
|
+
placements[best.item] = {
|
|
89
|
+
bin: binIndex,
|
|
90
|
+
x: best.x,
|
|
91
|
+
y: best.y,
|
|
92
|
+
rotated: best.rotated,
|
|
93
|
+
};
|
|
94
|
+
binArea += placed.width * placed.height;
|
|
95
|
+
}
|
|
96
|
+
if (binArea > 0) {
|
|
97
|
+
occupancy[binIndex] = binArea / (bin.width * bin.height);
|
|
98
|
+
placedArea += binArea;
|
|
99
|
+
usedBinArea += bin.width * bin.height;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
placements,
|
|
104
|
+
occupancy,
|
|
105
|
+
fillRatio: usedBinArea === 0 ? 0 : placedArea / usedBinArea,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function checkSize_(name, size) {
|
|
109
|
+
for (const value of [size.width, size.height]) {
|
|
110
|
+
if (!Number.isSafeInteger(value) || value <= 0) {
|
|
111
|
+
throw new RangeError(`maxRectsPack: ${name} has a side of ${describeValue(value)}, not a positive integer`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/** The best candidate so far; mutated in place, one per pack. */
|
|
116
|
+
class Candidate_ {
|
|
117
|
+
item = -1;
|
|
118
|
+
x = 0;
|
|
119
|
+
y = 0;
|
|
120
|
+
width = 0;
|
|
121
|
+
height = 0;
|
|
122
|
+
rotated = false;
|
|
123
|
+
primary = Infinity;
|
|
124
|
+
secondary = Infinity;
|
|
125
|
+
reset() {
|
|
126
|
+
this.item = -1;
|
|
127
|
+
this.primary = Infinity;
|
|
128
|
+
this.secondary = Infinity;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const SCORERS_ = new Map([
|
|
132
|
+
[
|
|
133
|
+
"best-short-side-fit",
|
|
134
|
+
(free, width, height, _used, _bin, out) => {
|
|
135
|
+
const horizontal = free.width - width;
|
|
136
|
+
const vertical = free.height - height;
|
|
137
|
+
out[0] = Math.min(horizontal, vertical);
|
|
138
|
+
out[1] = Math.max(horizontal, vertical);
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
[
|
|
142
|
+
"best-long-side-fit",
|
|
143
|
+
(free, width, height, _used, _bin, out) => {
|
|
144
|
+
const horizontal = free.width - width;
|
|
145
|
+
const vertical = free.height - height;
|
|
146
|
+
out[0] = Math.max(horizontal, vertical);
|
|
147
|
+
out[1] = Math.min(horizontal, vertical);
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
[
|
|
151
|
+
"best-area-fit",
|
|
152
|
+
(free, width, height, _used, _bin, out) => {
|
|
153
|
+
out[0] = free.width * free.height - width * height;
|
|
154
|
+
out[1] = Math.min(free.width - width, free.height - height);
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
[
|
|
158
|
+
"bottom-left",
|
|
159
|
+
(free, _width, height, _used, _bin, out) => {
|
|
160
|
+
out[0] = free.y + height;
|
|
161
|
+
out[1] = free.x;
|
|
162
|
+
},
|
|
163
|
+
],
|
|
164
|
+
[
|
|
165
|
+
"contact-point",
|
|
166
|
+
(free, width, height, used, bin, out) => {
|
|
167
|
+
out[0] = -contactLength_(free.x, free.y, width, height, used, bin);
|
|
168
|
+
out[1] = 0;
|
|
169
|
+
},
|
|
170
|
+
],
|
|
171
|
+
]);
|
|
172
|
+
const score_ = [0, 0];
|
|
173
|
+
/** Offers every free rectangle that holds `width × height` to `best`. */
|
|
174
|
+
function findPosition_(best, item, width, height, rotated, free, used, bin, heuristic) {
|
|
175
|
+
const scorer = SCORERS_.get(heuristic);
|
|
176
|
+
for (const rect of free) {
|
|
177
|
+
if (rect.width < width || rect.height < height) {
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
scorer(rect, width, height, used, bin, score_);
|
|
181
|
+
if (score_[0] < best.primary ||
|
|
182
|
+
(score_[0] === best.primary && score_[1] < best.secondary)) {
|
|
183
|
+
best.item = item;
|
|
184
|
+
best.x = rect.x;
|
|
185
|
+
best.y = rect.y;
|
|
186
|
+
best.width = width;
|
|
187
|
+
best.height = height;
|
|
188
|
+
best.rotated = rotated;
|
|
189
|
+
best.primary = score_[0];
|
|
190
|
+
best.secondary = score_[1];
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* The length of a rectangle's perimeter lying on the bin's edges or a placed
|
|
196
|
+
* rectangle's.
|
|
197
|
+
*/
|
|
198
|
+
function contactLength_(x, y, width, height, used, bin) {
|
|
199
|
+
let length = 0;
|
|
200
|
+
if (x === 0 || x + width === bin.width) {
|
|
201
|
+
length += height;
|
|
202
|
+
}
|
|
203
|
+
if (y === 0 || y + height === bin.height) {
|
|
204
|
+
length += width;
|
|
205
|
+
}
|
|
206
|
+
for (const other of used) {
|
|
207
|
+
if (other.x === x + width || other.x + other.width === x) {
|
|
208
|
+
length += overlap_(other.y, other.y + other.height, y, y + height);
|
|
209
|
+
}
|
|
210
|
+
if (other.y === y + height || other.y + other.height === y) {
|
|
211
|
+
length += overlap_(other.x, other.x + other.width, x, x + width);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return length;
|
|
215
|
+
}
|
|
216
|
+
function overlap_(start0, end0, start1, end1) {
|
|
217
|
+
return Math.max(0, Math.min(end0, end1) - Math.max(start0, start1));
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Replaces each free rectangle `placed` overlaps by the up to four maximal
|
|
221
|
+
* pieces around it.
|
|
222
|
+
*/
|
|
223
|
+
function splitFree_(free, placed) {
|
|
224
|
+
const right = placed.x + placed.width;
|
|
225
|
+
const bottom = placed.y + placed.height;
|
|
226
|
+
const count = free.length;
|
|
227
|
+
let kept = 0;
|
|
228
|
+
const pieces = [];
|
|
229
|
+
for (let index = 0; index < count; index++) {
|
|
230
|
+
const rect = free[index];
|
|
231
|
+
const rectRight = rect.x + rect.width;
|
|
232
|
+
const rectBottom = rect.y + rect.height;
|
|
233
|
+
if (placed.x >= rectRight ||
|
|
234
|
+
right <= rect.x ||
|
|
235
|
+
placed.y >= rectBottom ||
|
|
236
|
+
bottom <= rect.y) {
|
|
237
|
+
free[kept++] = rect;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
if (placed.x > rect.x) {
|
|
241
|
+
pieces.push({
|
|
242
|
+
x: rect.x,
|
|
243
|
+
y: rect.y,
|
|
244
|
+
width: placed.x - rect.x,
|
|
245
|
+
height: rect.height,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
if (right < rectRight) {
|
|
249
|
+
pieces.push({
|
|
250
|
+
x: right,
|
|
251
|
+
y: rect.y,
|
|
252
|
+
width: rectRight - right,
|
|
253
|
+
height: rect.height,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
if (placed.y > rect.y) {
|
|
257
|
+
pieces.push({
|
|
258
|
+
x: rect.x,
|
|
259
|
+
y: rect.y,
|
|
260
|
+
width: rect.width,
|
|
261
|
+
height: placed.y - rect.y,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
if (bottom < rectBottom) {
|
|
265
|
+
pieces.push({
|
|
266
|
+
x: rect.x,
|
|
267
|
+
y: bottom,
|
|
268
|
+
width: rect.width,
|
|
269
|
+
height: rectBottom - bottom,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
free.length = kept;
|
|
274
|
+
for (const piece of pieces) {
|
|
275
|
+
free.push(piece);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Removes every free rectangle another one contains (one of two equal ones
|
|
280
|
+
* stays).
|
|
281
|
+
*/
|
|
282
|
+
function pruneFree_(free) {
|
|
283
|
+
const count = free.length;
|
|
284
|
+
const removed = new Uint8Array(count);
|
|
285
|
+
for (let index = 0; index < count; index++) {
|
|
286
|
+
const rect = free[index];
|
|
287
|
+
for (let other = 0; other < count; other++) {
|
|
288
|
+
if (other === index || removed[other] === 1) {
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
const candidate = free[other];
|
|
292
|
+
const holds = candidate.x <= rect.x &&
|
|
293
|
+
candidate.y <= rect.y &&
|
|
294
|
+
candidate.x + candidate.width >= rect.x + rect.width &&
|
|
295
|
+
candidate.y + candidate.height >= rect.y + rect.height;
|
|
296
|
+
if (holds) {
|
|
297
|
+
removed[index] = 1;
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
let kept = 0;
|
|
303
|
+
for (let index = 0; index < count; index++) {
|
|
304
|
+
if (removed[index] === 0) {
|
|
305
|
+
free[kept++] = free[index];
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
free.length = kept;
|
|
309
|
+
}
|
|
310
|
+
//# sourceMappingURL=max-rects-pack.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"max-rects-pack.js","sourceRoot":"","sources":["../src/max-rects-pack.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAiE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,MAAM,UAAU,YAAY,CAC3B,IAA6B,EAC7B,KAA8B,EAC9B,OAAO,GAAwB,EAAE;IAEjC,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,qBAAqB,CAAC;IAC7D,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,UAAU,CACnB,mCAAmC,aAAa,CAAC,SAAS,CAAC,EAAE,CAC7D,CAAC;IACH,CAAC;IACD,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,IAAI,KAAK,CAAC;IACrD,KAAK,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,EAAE,CAAC;QAC3C,UAAU,CAAC,OAAO,KAAK,EAAE,EAAE,GAAG,CAAC,CAAC;IACjC,CAAC;IACD,KAAK,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;QAC7C,UAAU,CAAC,QAAQ,KAAK,EAAE,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;IAED,MAAM,UAAU,GAAsC,KAAK,CAAC,IAAI,CAAC;QAChE,MAAM,EAAE,KAAK,CAAC,MAAM;KACpB,CAAC,CAAC;IACH,MAAM,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,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAE,CAAC;QAC1B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC;YAC5C,IAAI,KAAK,KAAK,KAAK,IAAI,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC7C,SAAS;YACV,CAAC;YACD,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAE,CAAC;YAC/B,MAAM,KAAK,GACV,SAAS,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC;gBACrB,SAAS,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC;gBACrB,SAAS,CAAC,CAAC,GAAG,SAAS,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK;gBACpD,SAAS,CAAC,CAAC,GAAG,SAAS,CAAC,MAAM,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;YACxD,IAAI,KAAK,EAAE,CAAC;gBACX,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBACnB,MAAM;YACP,CAAC;QACF,CAAC;IACF,CAAC;IACD,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC;QAC5C,IAAI,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YAC1B,IAAI,CAAC,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC,KAAK,CAAE,CAAC;QAC7B,CAAC;IACF,CAAC;IACD,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calls `visit(col, row)` for every cell of the digital line from `(fromCol,
|
|
3
|
+
* fromRow)` to `(toCol, toRow)`, both ends included and in that order, and
|
|
4
|
+
* returns how many there were — Bresenham's line: one cell per step along the
|
|
5
|
+
* longer axis, the other coordinate the nearest to the true line.
|
|
6
|
+
*
|
|
7
|
+
* The cells are the same whichever end the line is drawn from: the line is
|
|
8
|
+
* always computed from the lexicographically smaller end (least column, then
|
|
9
|
+
* least row) and only walked the other way when asked, so a ray cast and its
|
|
10
|
+
* return trip, or a polygon edge shared by two neighbours, cover exactly the
|
|
11
|
+
* same cells. A tie (the true line exactly halfway between two cells) goes to
|
|
12
|
+
* the cell further along from that canonical end. 8-connected: consecutive
|
|
13
|
+
* cells share an edge or a corner. Integer arithmetic throughout, no
|
|
14
|
+
* allocation.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const cells: number[][] = [];
|
|
19
|
+
* rasterizeLine2(0, 0, 4, 2, (col, row) => cells.push([col, row]));
|
|
20
|
+
* // cells: [[0, 0], [1, 1], [2, 1], [3, 2], [4, 2]]
|
|
21
|
+
* ```;
|
|
22
|
+
*
|
|
23
|
+
* @param fromCol The first end's column.
|
|
24
|
+
* @param fromRow The first end's row.
|
|
25
|
+
* @param toCol The last end's column.
|
|
26
|
+
* @param toRow The last end's row.
|
|
27
|
+
* @param visit Called with each cell's column and row.
|
|
28
|
+
* @returns The number of cells visited, `max(|Δcol|, |Δrow|) + 1`.
|
|
29
|
+
* @throws {RangeError} When a coordinate is not a safe integer, or the line
|
|
30
|
+
* spans more than `2⁵²` cells along an axis.
|
|
31
|
+
*/
|
|
32
|
+
export declare function rasterizeLine2(fromCol: number, fromRow: number, toCol: number, toRow: number, visit: (col: number, row: number) => void): number;
|
|
33
|
+
//# sourceMappingURL=rasterize-line2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rasterize-line2.d.ts","sourceRoot":"","sources":["../src/rasterize-line2.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,cAAc,CAC7B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,IAAI,GACvC,MAAM,CA0ER"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/** Spans up to this keep `2 · span` a safe integer in the step arithmetic. */
|
|
2
|
+
const MAX_SPAN_ = 2 ** 52;
|
|
3
|
+
/**
|
|
4
|
+
* Calls `visit(col, row)` for every cell of the digital line from `(fromCol,
|
|
5
|
+
* fromRow)` to `(toCol, toRow)`, both ends included and in that order, and
|
|
6
|
+
* returns how many there were — Bresenham's line: one cell per step along the
|
|
7
|
+
* longer axis, the other coordinate the nearest to the true line.
|
|
8
|
+
*
|
|
9
|
+
* The cells are the same whichever end the line is drawn from: the line is
|
|
10
|
+
* always computed from the lexicographically smaller end (least column, then
|
|
11
|
+
* least row) and only walked the other way when asked, so a ray cast and its
|
|
12
|
+
* return trip, or a polygon edge shared by two neighbours, cover exactly the
|
|
13
|
+
* same cells. A tie (the true line exactly halfway between two cells) goes to
|
|
14
|
+
* the cell further along from that canonical end. 8-connected: consecutive
|
|
15
|
+
* cells share an edge or a corner. Integer arithmetic throughout, no
|
|
16
|
+
* allocation.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const cells: number[][] = [];
|
|
21
|
+
* rasterizeLine2(0, 0, 4, 2, (col, row) => cells.push([col, row]));
|
|
22
|
+
* // cells: [[0, 0], [1, 1], [2, 1], [3, 2], [4, 2]]
|
|
23
|
+
* ```;
|
|
24
|
+
*
|
|
25
|
+
* @param fromCol The first end's column.
|
|
26
|
+
* @param fromRow The first end's row.
|
|
27
|
+
* @param toCol The last end's column.
|
|
28
|
+
* @param toRow The last end's row.
|
|
29
|
+
* @param visit Called with each cell's column and row.
|
|
30
|
+
* @returns The number of cells visited, `max(|Δcol|, |Δrow|) + 1`.
|
|
31
|
+
* @throws {RangeError} When a coordinate is not a safe integer, or the line
|
|
32
|
+
* spans more than `2⁵²` cells along an axis.
|
|
33
|
+
*/
|
|
34
|
+
export function rasterizeLine2(fromCol, fromRow, toCol, toRow, visit) {
|
|
35
|
+
if (!Number.isSafeInteger(fromCol) ||
|
|
36
|
+
!Number.isSafeInteger(fromRow) ||
|
|
37
|
+
!Number.isSafeInteger(toCol) ||
|
|
38
|
+
!Number.isSafeInteger(toRow)) {
|
|
39
|
+
throw new RangeError(`rasterizeLine2: (${fromCol}, ${fromRow}) → (${toCol}, ${toRow}) is not integral`);
|
|
40
|
+
}
|
|
41
|
+
const forward = fromCol < toCol || (fromCol === toCol && fromRow <= toRow);
|
|
42
|
+
const startCol = forward ? fromCol : toCol;
|
|
43
|
+
const startRow = forward ? fromRow : toRow;
|
|
44
|
+
const colSpan = Math.abs(toCol - fromCol);
|
|
45
|
+
const rowSpan = Math.abs(toRow - fromRow);
|
|
46
|
+
const colStep = (forward ? toCol - fromCol : fromCol - toCol) < 0 ? -1 : 1;
|
|
47
|
+
const rowStep = (forward ? toRow - fromRow : fromRow - toRow) < 0 ? -1 : 1;
|
|
48
|
+
const colMajor = colSpan >= rowSpan;
|
|
49
|
+
const steps = colMajor ? colSpan : rowSpan;
|
|
50
|
+
const minorSpan = colMajor ? rowSpan : colSpan;
|
|
51
|
+
if (steps > MAX_SPAN_) {
|
|
52
|
+
throw new RangeError(`rasterizeLine2: a span of ${steps} cells is too long`);
|
|
53
|
+
}
|
|
54
|
+
if (steps === 0) {
|
|
55
|
+
visit(startCol, startRow);
|
|
56
|
+
return 1;
|
|
57
|
+
}
|
|
58
|
+
// The minor offset at step j from the canonical end is ⌊(2j·m + n) / 2n⌋,
|
|
59
|
+
// the nearest integer to j·m/n with ties rounded up. Keep the numerator as
|
|
60
|
+
// quotient and remainder by 2n, which move by at most one per step.
|
|
61
|
+
const denominator = 2 * steps;
|
|
62
|
+
const increment = 2 * minorSpan;
|
|
63
|
+
let step;
|
|
64
|
+
let quotient;
|
|
65
|
+
let remainder;
|
|
66
|
+
if (forward) {
|
|
67
|
+
step = 0;
|
|
68
|
+
quotient = 0;
|
|
69
|
+
remainder = steps;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
// At j = n the numerator is 2n·m + n: quotient m, remainder n.
|
|
73
|
+
step = steps;
|
|
74
|
+
quotient = minorSpan;
|
|
75
|
+
remainder = steps;
|
|
76
|
+
}
|
|
77
|
+
for (let visited = 0; visited <= steps; visited++) {
|
|
78
|
+
if (colMajor) {
|
|
79
|
+
visit(startCol + colStep * step, startRow + rowStep * quotient);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
visit(startCol + colStep * quotient, startRow + rowStep * step);
|
|
83
|
+
}
|
|
84
|
+
if (forward) {
|
|
85
|
+
step++;
|
|
86
|
+
remainder += increment;
|
|
87
|
+
if (remainder >= denominator) {
|
|
88
|
+
remainder -= denominator;
|
|
89
|
+
quotient++;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
step--;
|
|
94
|
+
remainder -= increment;
|
|
95
|
+
if (remainder < 0) {
|
|
96
|
+
remainder += denominator;
|
|
97
|
+
quotient--;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return steps + 1;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=rasterize-line2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rasterize-line2.js","sourceRoot":"","sources":["../src/rasterize-line2.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,MAAM,SAAS,GAAG,CAAC,IAAI,EAAE,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,MAAM,UAAU,cAAc,CAC7B,OAAe,EACf,OAAe,EACf,KAAa,EACb,KAAa,EACb,KAAyC;IAEzC,IACC,CAAC,MAAM,CAAC,aAAa,CAAC,OA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SAAS,CAAC;YACvB,IAAI,SAAS,IAAI,WAAW,EAAE,CAAC;gBAC9B,SAAS,IAAI,WAAW,CAAC;gBACzB,QAAQ,EAAE,CAAC;YACZ,CAAC;QACF,CAAC;aAAM,CAAC;YACP,IAAI,EAAE,CAAC;YACP,SAAS,IAAI,SAAS,CAAC;YACvB,IAAI,SAAS,GAAG,CAAC,EAAE,CAAC;gBACnB,SAAS,IAAI,WAAW,CAAC;gBACzB,QAAQ,EAAE,CAAC;YACZ,CAAC;QACF,CAAC;IACF,CAAC;IACD,OAAO,KAAK,GAAG,CAAC,CAAC;AAClB,CAAC"}
|
|
@@ -27,7 +27,13 @@ export type ShelfAllocation = {
|
|
|
27
27
|
* ```
|
|
28
28
|
*/
|
|
29
29
|
export declare class ShelfAllocator {
|
|
30
|
-
|
|
30
|
+
private readonly width_;
|
|
31
|
+
private readonly height_;
|
|
32
|
+
private readonly shelves_;
|
|
33
|
+
private readonly entries_;
|
|
34
|
+
private nextId_;
|
|
35
|
+
private nextY_;
|
|
36
|
+
private usedArea_;
|
|
31
37
|
/**
|
|
32
38
|
* @throws {RangeError} When `width` or `height` is not a positive integer, or
|
|
33
39
|
* when `width · height` is not a safe integer.
|
|
@@ -1 +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
|
|
1
|
+
{"version":3,"file":"shelf-allocator.d.ts","sourceRoot":"","sources":["../src/shelf-allocator.ts"],"names":[],"mappings":"AAEA,oDA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