@mark1russell7/packing 0.1.3 → 0.1.4

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/build/index.js CHANGED
@@ -824,7 +824,7 @@ function isConvex(points) {
824
824
  return true;
825
825
  }
826
826
  /** This function gives the value of `containerShape` of one region. */
827
- function shapeOf(region) {
827
+ function shapeOf$1(region) {
828
828
  switch (region.kind) {
829
829
  case "rect":
830
830
  case "circle":
@@ -862,7 +862,7 @@ function cellOf(input) {
862
862
  height: "freeWidth",
863
863
  none: container.aspectWeight === 0 ? "freeArea" : "freeAspect"
864
864
  }[container.fixed];
865
- let containerShape = container.shape ? shapeOf(container.shape) : "rect";
865
+ let containerShape = container.shape ? shapeOf$1(container.shape) : "rect";
866
866
  if (containerShape === "rect" && obstacles.some((o) => o.region.kind !== "rect")) containerShape = "holes";
867
867
  let obstacleMove = items.some((i) => i.locked === true) ? "fixed" : void 0;
868
868
  for (const o of obstacles) {
@@ -1642,6 +1642,1115 @@ function checkHard(input, output, tolerance = DEFAULT_TOLERANCE) {
1642
1642
  }
1643
1643
  return breaks;
1644
1644
  }
1645
- export { DEFAULT_TOLERANCE, EPS, RECORD_EPS, Rect, SORT_ORDERS, UnsupportedCell, Vector, aspectError, assertSupported, cellOf, checkHard, compareRects, createContext, fromRects, logSweep, lowestWindow, marginOf, mulberry32, pack, packFree, packRows, packShelf, packSkyline, packWith, packingMetrics, push, pushCascade, rows, shelf, skyline, strategies, strategy, supportsCell, toRects, turnItem, turnPoint, turnPort, turnRegion, unsupportedOf };
1645
+ // src/sha256.ts
1646
+ /** The round constants of SHA-256. */
1647
+ const K = new Uint32Array([
1648
+ 1116352408,
1649
+ 1899447441,
1650
+ 3049323471,
1651
+ 3921009573,
1652
+ 961987163,
1653
+ 1508970993,
1654
+ 2453635748,
1655
+ 2870763221,
1656
+ 3624381080,
1657
+ 310598401,
1658
+ 607225278,
1659
+ 1426881987,
1660
+ 1925078388,
1661
+ 2162078206,
1662
+ 2614888103,
1663
+ 3248222580,
1664
+ 3835390401,
1665
+ 4022224774,
1666
+ 264347078,
1667
+ 604807628,
1668
+ 770255983,
1669
+ 1249150122,
1670
+ 1555081692,
1671
+ 1996064986,
1672
+ 2554220882,
1673
+ 2821834349,
1674
+ 2952996808,
1675
+ 3210313671,
1676
+ 3336571891,
1677
+ 3584528711,
1678
+ 113926993,
1679
+ 338241895,
1680
+ 666307205,
1681
+ 773529912,
1682
+ 1294757372,
1683
+ 1396182291,
1684
+ 1695183700,
1685
+ 1986661051,
1686
+ 2177026350,
1687
+ 2456956037,
1688
+ 2730485921,
1689
+ 2820302411,
1690
+ 3259730800,
1691
+ 3345764771,
1692
+ 3516065817,
1693
+ 3600352804,
1694
+ 4094571909,
1695
+ 275423344,
1696
+ 430227734,
1697
+ 506948616,
1698
+ 659060556,
1699
+ 883997877,
1700
+ 958139571,
1701
+ 1322822218,
1702
+ 1537002063,
1703
+ 1747873779,
1704
+ 1955562222,
1705
+ 2024104815,
1706
+ 2227730452,
1707
+ 2361852424,
1708
+ 2428436474,
1709
+ 2756734187,
1710
+ 3204031479,
1711
+ 3329325298
1712
+ ]);
1713
+ /** The first hash value of SHA-256. */
1714
+ const H0 = [
1715
+ 1779033703,
1716
+ 3144134277,
1717
+ 1013904242,
1718
+ 2773480762,
1719
+ 1359893119,
1720
+ 2600822924,
1721
+ 528734635,
1722
+ 1541459225
1723
+ ];
1724
+ /** This function turns a 32-bit word to the right by `n` bits. */
1725
+ const rotr = (x, n) => x >>> n | x << 32 - n;
1726
+ /**
1727
+ * This function gives the UTF-8 bytes of a string. A lone surrogate becomes U+FFFD, as with `TextEncoder`. The
1728
+ * package has no `TextEncoder` type, because its code operates on each runtime with no global types.
1729
+ */
1730
+ function utf8(text) {
1731
+ const out = [];
1732
+ for (let i = 0; i < text.length; i++) {
1733
+ let c = text.charCodeAt(i);
1734
+ if (c >= 55296 && c <= 56319 && i + 1 < text.length) {
1735
+ const next = text.charCodeAt(i + 1);
1736
+ if (next >= 56320 && next <= 57343) {
1737
+ c = 65536 + (c - 55296 << 10) + (next - 56320);
1738
+ i++;
1739
+ }
1740
+ }
1741
+ if (c >= 55296 && c <= 57343) c = 65533;
1742
+ if (c < 128) out.push(c);
1743
+ else if (c < 2048) out.push(192 | c >> 6, 128 | c & 63);
1744
+ else if (c < 65536) out.push(224 | c >> 12, 128 | c >> 6 & 63, 128 | c & 63);
1745
+ else out.push(240 | c >> 18, 128 | c >> 12 & 63, 128 | c >> 6 & 63, 128 | c & 63);
1746
+ }
1747
+ return Uint8Array.from(out);
1748
+ }
1749
+ /** This function gives the SHA-256 of the UTF-8 bytes of a string, as 64 lowercase hex characters. */
1750
+ function sha256Hex(text) {
1751
+ const bytes = utf8(text);
1752
+ const length = bytes.length;
1753
+ const padded = new Uint8Array(((length + 9 + 63) / 64 | 0) * 64);
1754
+ padded.set(bytes);
1755
+ padded[length] = 128;
1756
+ const view = new DataView(padded.buffer);
1757
+ const bits = length * 8;
1758
+ view.setUint32(padded.length - 8, Math.floor(bits / 4294967296));
1759
+ view.setUint32(padded.length - 4, bits >>> 0);
1760
+ const hash = [...H0];
1761
+ const w = /* @__PURE__ */ new Uint32Array(64);
1762
+ for (let block = 0; block < padded.length; block += 64) {
1763
+ for (let t = 0; t < 16; t++) w[t] = view.getUint32(block + t * 4);
1764
+ for (let t = 16; t < 64; t++) {
1765
+ const a = w[t - 15];
1766
+ const b = w[t - 2];
1767
+ const s0 = rotr(a, 7) ^ rotr(a, 18) ^ a >>> 3;
1768
+ const s1 = rotr(b, 17) ^ rotr(b, 19) ^ b >>> 10;
1769
+ w[t] = w[t - 16] + s0 + w[t - 7] + s1 | 0;
1770
+ }
1771
+ let [a, b, c, d, e, f, g, h] = hash;
1772
+ for (let t = 0; t < 64; t++) {
1773
+ const s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
1774
+ const ch = e & f ^ ~e & g;
1775
+ const t1 = h + s1 + ch + K[t] + w[t] | 0;
1776
+ const t2 = (rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + (a & b ^ a & c ^ b & c) | 0;
1777
+ h = g;
1778
+ g = f;
1779
+ f = e;
1780
+ e = d + t1 | 0;
1781
+ d = c;
1782
+ c = b;
1783
+ b = a;
1784
+ a = t1 + t2 | 0;
1785
+ }
1786
+ hash[0] = hash[0] + a | 0;
1787
+ hash[1] = hash[1] + b | 0;
1788
+ hash[2] = hash[2] + c | 0;
1789
+ hash[3] = hash[3] + d | 0;
1790
+ hash[4] = hash[4] + e | 0;
1791
+ hash[5] = hash[5] + f | 0;
1792
+ hash[6] = hash[6] + g | 0;
1793
+ hash[7] = hash[7] + h | 0;
1794
+ }
1795
+ return hash.map((x) => (x >>> 0).toString(16).padStart(8, "0")).join("");
1796
+ }
1797
+ // src/layout-input.ts
1798
+ /** This function gives the `fnv1a32` hash of the UTF-8 bytes of a string, as `docs/FORMAT.md` gives it. */
1799
+ function fnv1a32(text) {
1800
+ let h = 2166136261;
1801
+ for (const b of utf8(text)) h = Math.imul(h ^ b, 16777619) >>> 0;
1802
+ return h;
1803
+ }
1804
+ /** This function rounds the numbers of a file to 3 decimals. A half goes up, and the value `-0` becomes 0. */
1805
+ function round3(x) {
1806
+ const v = Math.round(x * 1e3) / 1e3;
1807
+ return Object.is(v, -0) ? 0 : v;
1808
+ }
1809
+ /**
1810
+ * This function gives the canonical form of a value: sorted keys, no whitespace and the numbers of `JSON.stringify`
1811
+ * after `round3`. A key with the value `undefined` is not in the form.
1812
+ */
1813
+ function canonicalJson(value) {
1814
+ if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
1815
+ if (value !== null && typeof value === "object") return `{${Object.entries(value).filter(([, v]) => v !== void 0).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([k, v]) => `${JSON.stringify(k)}:${canonicalJson(v)}`).join(",")}}`;
1816
+ return JSON.stringify(typeof value === "number" ? round3(value) : value);
1817
+ }
1818
+ /** This function gives the hash of an input file: the first 16 hex characters of the SHA-256 of its canonical form without `hash`. */
1819
+ function hashInput(file) {
1820
+ const rest = { ...file };
1821
+ delete rest.hash;
1822
+ return sha256Hex(canonicalJson(rest)).slice(0, 16);
1823
+ }
1824
+ /** This function gives the scale of a count: the nearest of 1, 10, 100, 1000 and 10000 on a log scale. The count 0 gives `1`. */
1825
+ function countCell(count) {
1826
+ if (count <= 3) return "1";
1827
+ if (count <= 31) return "10";
1828
+ if (count <= 316) return "100";
1829
+ if (count <= 3162) return "1k";
1830
+ return "10k";
1831
+ }
1832
+ /** This function gives the packing input of a file. The boxes become the items, and the edges go. */
1833
+ function toPackInput(file) {
1834
+ const input = {
1835
+ container: file.container,
1836
+ items: file.boxes
1837
+ };
1838
+ if (file.obstacles !== void 0) input.obstacles = file.obstacles;
1839
+ if (file.gap !== void 0) input.gap = file.gap;
1840
+ return input;
1841
+ }
1842
+ // src/generators.ts
1843
+ /** The largest count of boxes that a generator makes. */
1844
+ const MAX_COUNT = 1e5;
1845
+ /** This function gives the random numbers of one step of a seed. */
1846
+ const stepRandom = (seed, step) => mulberry32(fnv1a32(`${seed}:${step}`));
1847
+ /** A whole number from lo to hi, both included. */
1848
+ const int = (r, lo, hi) => lo + Math.floor(r() * (hi - lo + 1));
1849
+ /** A whole number from the floor of lo to the floor of hi, both included. The upper end is not less than the lower end at any time. */
1850
+ const span = (r, lo, hi) => {
1851
+ const a = Math.floor(lo);
1852
+ return int(r, a, Math.max(a, Math.floor(hi)));
1853
+ };
1854
+ /** A number from lo to hi. */
1855
+ const between = (r, lo, hi) => lo + r() * (hi - lo);
1856
+ /** This function gives an index with a probability in proportion to its weight. */
1857
+ const pick = (r, weights) => {
1858
+ let x = r() * weights.reduce((a, b) => a + b, 0);
1859
+ for (const [i, w] of weights.entries()) {
1860
+ x -= w;
1861
+ if (x < 0) return i;
1862
+ }
1863
+ return weights.length - 1;
1864
+ };
1865
+ /** This function gives a copy of a value with each number rounded by `round3`. A key with the value `undefined` goes. */
1866
+ function roundNumbers(value) {
1867
+ if (typeof value === "number") return round3(value);
1868
+ if (Array.isArray(value)) return value.map(roundNumbers);
1869
+ if (value !== null && typeof value === "object") {
1870
+ const out = {};
1871
+ for (const [k, v] of Object.entries(value)) if (v !== void 0) out[k] = roundNumbers(v);
1872
+ return out;
1873
+ }
1874
+ return value;
1875
+ }
1876
+ /** This function gives the cell of an input with rectangle items, no rotation, no order and no groups. */
1877
+ function cellWith(containerSize, containerShape, obstacles, sizes, count, locked = false) {
1878
+ const cell = {
1879
+ containerSize,
1880
+ containerShape,
1881
+ itemShape: "rect",
1882
+ rotation: "none",
1883
+ order: "free",
1884
+ groups: "none",
1885
+ obstacles,
1886
+ sizes,
1887
+ count: countCell(count),
1888
+ mode: "offline"
1889
+ };
1890
+ if (obstacles !== "none" || locked) cell.obstacleMove = "fixed";
1891
+ return cell;
1892
+ }
1893
+ /** This function makes a generator from its data. It adds the defaults, the checks, the stamp, the rounding and the hash. */
1894
+ function defineGenerator(spec) {
1895
+ const withDefaults = (params = {}) => {
1896
+ const full = { ...spec.defaults };
1897
+ for (const [key, value] of Object.entries(params)) {
1898
+ if (value === void 0) continue;
1899
+ if (!(key in spec.defaults)) throw new RangeError(`The generator "${spec.name}" has no parameter "${key}".`);
1900
+ full[key] = value;
1901
+ }
1902
+ for (const [key, allowed] of Object.entries(spec.values)) if (allowed !== void 0 && !allowed.includes(full[key])) throw new RangeError(`The parameter "${key}" of the generator "${spec.name}" has no value "${String(full[key])}".`);
1903
+ const count = full["count"];
1904
+ if (count !== void 0 && (typeof count !== "number" || !Number.isInteger(count) || count < 0 || count > MAX_COUNT)) throw new RangeError(`The parameter "count" of the generator "${spec.name}" is a whole number from 0 to ${MAX_COUNT}.`);
1905
+ return full;
1906
+ };
1907
+ return Object.freeze({
1908
+ name: spec.name,
1909
+ version: spec.version,
1910
+ set: spec.set,
1911
+ defaults: Object.freeze({ ...spec.defaults }),
1912
+ values: spec.values,
1913
+ withDefaults,
1914
+ cell: (params) => spec.cell(withDefaults(params)),
1915
+ generate(seed, params, set = spec.set) {
1916
+ if (!Number.isInteger(seed) || seed < 0) throw new RangeError(`The seed is a whole number from 0. It is ${seed}.`);
1917
+ const full = withDefaults(params);
1918
+ const family = spec.family(full);
1919
+ const { container, obstacles, boxes } = spec.build(seed, full);
1920
+ const file = roundNumbers({
1921
+ schema: "layout-input/1",
1922
+ id: `${set}/${family}/s${seed}`,
1923
+ set,
1924
+ family,
1925
+ seed,
1926
+ source: "made-up",
1927
+ generator: {
1928
+ name: spec.name,
1929
+ version: spec.version,
1930
+ params: full,
1931
+ seed
1932
+ },
1933
+ container,
1934
+ obstacles,
1935
+ boxes,
1936
+ edges: []
1937
+ });
1938
+ return {
1939
+ ...file,
1940
+ hash: hashInput(file)
1941
+ };
1942
+ }
1943
+ });
1944
+ }
1945
+ /** The shared generator `rect-scattered` of `docs/FORMAT.md`, version 1: rectangle obstacles at random places in a fixed rectangle. */
1946
+ const rectScattered = defineGenerator({
1947
+ name: "rect-scattered",
1948
+ version: 1,
1949
+ set: "xcheck-v1",
1950
+ defaults: { count: 40 },
1951
+ values: {},
1952
+ family: () => "rect-scattered",
1953
+ cell: (p) => cellWith("fixed", "rect", "scattered", "uniform", p.count),
1954
+ build(seed, { count }) {
1955
+ let r = stepRandom(seed, "container");
1956
+ const w = int(r, 600, 1e3);
1957
+ const h = int(r, 400, 800);
1958
+ const container = {
1959
+ w,
1960
+ h,
1961
+ fixed: "size"
1962
+ };
1963
+ r = stepRandom(seed, "obstacles");
1964
+ const n = int(r, 3, 8);
1965
+ const obstacles = [];
1966
+ for (let i = 0; i < n; i++) {
1967
+ const ow = int(r, 40, 160);
1968
+ const oh = int(r, 40, 160);
1969
+ const x = int(r, 0, w - ow);
1970
+ const y = int(r, 0, h - oh);
1971
+ obstacles.push({
1972
+ id: `o${i}`,
1973
+ region: {
1974
+ kind: "rect",
1975
+ x,
1976
+ y,
1977
+ w: ow,
1978
+ h: oh
1979
+ }
1980
+ });
1981
+ }
1982
+ r = stepRandom(seed, "boxes");
1983
+ const boxes = [];
1984
+ for (let i = 0; i < count; i++) {
1985
+ const bw = int(r, 20, 120);
1986
+ const bh = int(r, 20, 80);
1987
+ boxes.push({
1988
+ id: `b${i}`,
1989
+ w: bw,
1990
+ h: bh
1991
+ });
1992
+ }
1993
+ return {
1994
+ container,
1995
+ obstacles,
1996
+ boxes
1997
+ };
1998
+ }
1999
+ });
2000
+ /** The values of `containerSize` that the generators make. */
2001
+ const CONTAINER_SIZES = [
2002
+ "fixed",
2003
+ "freeHeight",
2004
+ "freeWidth",
2005
+ "freeAspect",
2006
+ "freeArea"
2007
+ ];
2008
+ /** The values of `containerShape`. */
2009
+ const CONTAINER_SHAPES = [
2010
+ "rect",
2011
+ "convex",
2012
+ "polygon",
2013
+ "circle",
2014
+ "ellipse",
2015
+ "holes"
2016
+ ];
2017
+ /** The values of `obstacles`. */
2018
+ const OBSTACLE_LAYOUTS = [
2019
+ "none",
2020
+ "center",
2021
+ "scattered",
2022
+ "wall",
2023
+ "corridors",
2024
+ "touching",
2025
+ "edge"
2026
+ ];
2027
+ /** The values of `sizes`. */
2028
+ const SIZE_DISTRIBUTIONS = [
2029
+ "identical",
2030
+ "nearIdentical",
2031
+ "uniform",
2032
+ "heavyTail",
2033
+ "bimodal",
2034
+ "extremeAspect"
2035
+ ];
2036
+ /** This function gives the container of a size value for the nominal rectangle `w` by `h`, with an optional shape. */
2037
+ function containerOf(size, w, h, shape) {
2038
+ const container = {
2039
+ fixed: {
2040
+ w,
2041
+ h,
2042
+ fixed: "size"
2043
+ },
2044
+ freeHeight: {
2045
+ w,
2046
+ fixed: "width"
2047
+ },
2048
+ freeWidth: {
2049
+ h,
2050
+ fixed: "height"
2051
+ },
2052
+ freeAspect: {
2053
+ fixed: "none",
2054
+ aspect: round3(w / h)
2055
+ },
2056
+ freeArea: {
2057
+ fixed: "none",
2058
+ aspectWeight: 0
2059
+ }
2060
+ }[size];
2061
+ if (shape !== void 0) container.shape = shape;
2062
+ return container;
2063
+ }
2064
+ /** This function gives the 4 corners of a rectangle, clockwise from the top left. */
2065
+ const corners = (x, y, w, h) => [
2066
+ [x, y],
2067
+ [x + w, y],
2068
+ [x + w, y + h],
2069
+ [x, y + h]
2070
+ ];
2071
+ /**
2072
+ * This function gives the shape of a container in the rectangle `w` by `h`, and its frame. The frame is a rectangle in
2073
+ * the shape. The obstacles and the sizes of the boxes come from the frame.
2074
+ */
2075
+ function shapeOf(r, shape, w, h) {
2076
+ const m = Math.min(w, h);
2077
+ switch (shape) {
2078
+ case "rect": return { frame: {
2079
+ x: 0,
2080
+ y: 0,
2081
+ w,
2082
+ h
2083
+ } };
2084
+ case "convex": {
2085
+ const [a, b, c, d] = [
2086
+ 0,
2087
+ 1,
2088
+ 2,
2089
+ 3
2090
+ ].map(() => span(r, m * .1, m * .25));
2091
+ const points = [
2092
+ [a, 0],
2093
+ [w - b, 0],
2094
+ [w, b],
2095
+ [w, h - c],
2096
+ [w - c, h],
2097
+ [d, h],
2098
+ [0, h - d],
2099
+ [0, a]
2100
+ ];
2101
+ const top = Math.max(a, b);
2102
+ const bottom = Math.max(c, d);
2103
+ return {
2104
+ region: {
2105
+ kind: "polygon",
2106
+ points
2107
+ },
2108
+ frame: {
2109
+ x: 0,
2110
+ y: top,
2111
+ w,
2112
+ h: h - top - bottom
2113
+ }
2114
+ };
2115
+ }
2116
+ case "polygon": {
2117
+ const nw = span(r, w * .25, w * .45);
2118
+ const nh = span(r, h * .25, h * .45);
2119
+ return {
2120
+ region: {
2121
+ kind: "polygon",
2122
+ points: [
2123
+ [0, 0],
2124
+ [w, 0],
2125
+ [w, h - nh],
2126
+ [w - nw, h - nh],
2127
+ [w - nw, h],
2128
+ [0, h]
2129
+ ]
2130
+ },
2131
+ frame: {
2132
+ x: 0,
2133
+ y: 0,
2134
+ w: w - nw,
2135
+ h
2136
+ }
2137
+ };
2138
+ }
2139
+ case "circle": {
2140
+ const cx = Math.floor(w / 2);
2141
+ const cy = Math.floor(h / 2);
2142
+ const radius = Math.min(cx, cy);
2143
+ const side = 2 * Math.floor((radius * Math.SQRT2 - 1) / 2);
2144
+ return {
2145
+ region: {
2146
+ kind: "circle",
2147
+ cx,
2148
+ cy,
2149
+ r: radius
2150
+ },
2151
+ frame: {
2152
+ x: cx - side / 2,
2153
+ y: cy - side / 2,
2154
+ w: side,
2155
+ h: side
2156
+ }
2157
+ };
2158
+ }
2159
+ case "ellipse": {
2160
+ const rx = Math.floor(w / 2);
2161
+ const ry = Math.floor(h / 2);
2162
+ const a = Math.floor(rx / Math.SQRT2);
2163
+ const b = Math.floor(ry / Math.SQRT2);
2164
+ return {
2165
+ region: {
2166
+ kind: "ellipse",
2167
+ cx: rx,
2168
+ cy: ry,
2169
+ rx,
2170
+ ry
2171
+ },
2172
+ frame: {
2173
+ x: rx - a,
2174
+ y: ry - b,
2175
+ w: 2 * a,
2176
+ h: 2 * b
2177
+ }
2178
+ };
2179
+ }
2180
+ case "holes": {
2181
+ const count = int(r, 1, 2);
2182
+ const holes = [];
2183
+ for (let i = 0; i < count; i++) {
2184
+ const hw = span(r, w * .08, w * .15);
2185
+ const hh = span(r, h * .1, h * .25);
2186
+ const x = i === 0 ? span(r, 20, w / 2 - hw - 10) : span(r, w / 2 + 10, w - hw - 20);
2187
+ const y = span(r, 20, h - hh - 20);
2188
+ holes.push(corners(x, y, hw, hh));
2189
+ }
2190
+ return {
2191
+ region: {
2192
+ kind: "polygon",
2193
+ points: corners(0, 0, w, h),
2194
+ holes
2195
+ },
2196
+ frame: {
2197
+ x: 0,
2198
+ y: 0,
2199
+ w,
2200
+ h
2201
+ }
2202
+ };
2203
+ }
2204
+ }
2205
+ }
2206
+ /** This function gives the obstacles of a layout in a frame. Each obstacle is a rectangle with whole-number sides. */
2207
+ function obstaclesOf(r, layout, f) {
2208
+ const m = Math.min(f.w, f.h);
2209
+ const rects = [];
2210
+ switch (layout) {
2211
+ case "none": break;
2212
+ case "center": {
2213
+ const w = span(r, f.w * .15, f.w * .3);
2214
+ const h = span(r, f.h * .15, f.h * .3);
2215
+ const jx = Math.floor(f.w * .05);
2216
+ const jy = Math.floor(f.h * .05);
2217
+ rects.push({
2218
+ x: f.x + Math.floor((f.w - w) / 2) + int(r, -jx, jx),
2219
+ y: f.y + Math.floor((f.h - h) / 2) + int(r, -jy, jy),
2220
+ w,
2221
+ h
2222
+ });
2223
+ break;
2224
+ }
2225
+ case "scattered": {
2226
+ const n = int(r, 3, 8);
2227
+ for (let i = 0; i < n; i++) {
2228
+ const w = span(r, m * .05, m * .2);
2229
+ const h = span(r, m * .05, m * .2);
2230
+ rects.push({
2231
+ x: int(r, f.x, f.x + f.w - w),
2232
+ y: int(r, f.y, f.y + f.h - h),
2233
+ w,
2234
+ h
2235
+ });
2236
+ }
2237
+ break;
2238
+ }
2239
+ case "wall": {
2240
+ const vertical = r() < .5;
2241
+ const across = vertical ? f.w : f.h;
2242
+ const along = vertical ? f.h : f.w;
2243
+ const thick = span(r, Math.max(1, across * .03), Math.max(1, across * .06));
2244
+ const length = span(r, along * .7, along * .85);
2245
+ const at = span(r, across * .3, across * .7 - thick);
2246
+ const from = r() < .5 ? 0 : along - length;
2247
+ rects.push(vertical ? {
2248
+ x: f.x + at,
2249
+ y: f.y + from,
2250
+ w: thick,
2251
+ h: length
2252
+ } : {
2253
+ x: f.x + from,
2254
+ y: f.y + at,
2255
+ w: length,
2256
+ h: thick
2257
+ });
2258
+ break;
2259
+ }
2260
+ case "corridors": {
2261
+ const cols = int(r, 2, 4);
2262
+ const rows = int(r, 2, 3);
2263
+ const path = span(r, m * .08, m * .15);
2264
+ const bw = Math.floor((f.w - (cols + 1) * path) / cols);
2265
+ const bh = Math.floor((f.h - (rows + 1) * path) / rows);
2266
+ for (let j = 0; j < rows; j++) for (let i = 0; i < cols; i++) rects.push({
2267
+ x: f.x + path + i * (bw + path),
2268
+ y: f.y + path + j * (bh + path),
2269
+ w: bw,
2270
+ h: bh
2271
+ });
2272
+ break;
2273
+ }
2274
+ case "touching": {
2275
+ const n = int(r, 2, 4);
2276
+ let w = span(r, m * .08, m * .15);
2277
+ let h = span(r, m * .08, m * .15);
2278
+ let x = span(r, f.x + f.w * .15, f.x + f.w * .35);
2279
+ let y = span(r, f.y + f.h * .15, f.y + f.h * .35);
2280
+ rects.push({
2281
+ x,
2282
+ y,
2283
+ w,
2284
+ h
2285
+ });
2286
+ for (let i = 1; i < n; i++) {
2287
+ const right = r() < .5;
2288
+ const nw = span(r, m * .08, m * .15);
2289
+ const nh = span(r, m * .08, m * .15);
2290
+ if (right) {
2291
+ y = int(r, y, y + Math.floor(h / 2));
2292
+ x += w;
2293
+ } else {
2294
+ x = int(r, x, x + Math.floor(w / 2));
2295
+ y += h;
2296
+ }
2297
+ w = nw;
2298
+ h = nh;
2299
+ rects.push({
2300
+ x,
2301
+ y,
2302
+ w,
2303
+ h
2304
+ });
2305
+ }
2306
+ break;
2307
+ }
2308
+ case "edge": {
2309
+ const n = int(r, 2, 5);
2310
+ for (let i = 0; i < n; i++) {
2311
+ const side = int(r, 0, 3);
2312
+ const w = span(r, m * .06, m * .15);
2313
+ const h = span(r, m * .06, m * .15);
2314
+ const x = side === 1 ? f.x + f.w - w : side === 3 ? f.x : int(r, f.x, f.x + f.w - w);
2315
+ const y = side === 0 ? f.y : side === 2 ? f.y + f.h - h : int(r, f.y, f.y + f.h - h);
2316
+ rects.push({
2317
+ x,
2318
+ y,
2319
+ w,
2320
+ h
2321
+ });
2322
+ }
2323
+ break;
2324
+ }
2325
+ }
2326
+ return rects.map((rect, i) => ({
2327
+ id: `o${i}`,
2328
+ region: {
2329
+ kind: "rect",
2330
+ ...rect
2331
+ }
2332
+ }));
2333
+ }
2334
+ /**
2335
+ * This function gives the boxes of a size distribution for a frame. The mean box has an area of 40 percent of the
2336
+ * frame divided by the count. No box is wider or higher than the frame.
2337
+ */
2338
+ function boxesOf(r, sizes, count, f) {
2339
+ const unit = Math.sqrt(f.w * f.h * .4 / Math.max(count, 1));
2340
+ const fit = (x, max) => Math.min(Math.max(round3(x), .01), max);
2341
+ const boxes = new Array(count);
2342
+ const put = (i, w, h) => {
2343
+ boxes[i] = {
2344
+ id: `b${i}`,
2345
+ w: fit(w, f.w),
2346
+ h: fit(h, f.h)
2347
+ };
2348
+ };
2349
+ switch (sizes) {
2350
+ case "identical":
2351
+ case "nearIdentical": {
2352
+ const aspect = Math.sqrt(between(r, .6, 1.6));
2353
+ const bw = unit * aspect;
2354
+ const bh = unit / aspect;
2355
+ for (let i = 0; i < count; i++) if (sizes === "identical") put(i, bw, bh);
2356
+ else put(i, bw * (1 + between(r, -.03, .03)), bh * (1 + between(r, -.03, .03)));
2357
+ break;
2358
+ }
2359
+ case "uniform":
2360
+ for (let i = 0; i < count; i++) put(i, unit * between(r, .5, 1.5), unit * between(r, .5, 1.5));
2361
+ break;
2362
+ case "heavyTail": {
2363
+ const cap = .6 * Math.min(f.w, f.h);
2364
+ for (let i = 0; i < count; i++) {
2365
+ const s = Math.min(.6 * unit * (1 - r()) ** (-1 / 1.6), cap);
2366
+ put(i, s * between(r, .75, 1.33), s * between(r, .75, 1.33));
2367
+ }
2368
+ break;
2369
+ }
2370
+ case "bimodal":
2371
+ for (let i = 0; i < count; i++) {
2372
+ const s = r() < .25 ? 1.8 * unit : .6 * unit;
2373
+ put(i, s * between(r, .85, 1.15), s * between(r, .85, 1.15));
2374
+ }
2375
+ break;
2376
+ case "extremeAspect": for (let i = 0; i < count; i++) {
2377
+ const long = unit * between(r, 2.5, 4);
2378
+ const short = unit * between(r, .15, .35);
2379
+ if (r() < .5) put(i, long, short);
2380
+ else put(i, short, long);
2381
+ }
2382
+ }
2383
+ return boxes;
2384
+ }
2385
+ /**
2386
+ * The generator `rect-grid`, version 1: rectangle boxes for each value of `containerSize`, `containerShape`, `obstacles`
2387
+ * and `sizes`, with each count. The container is a nominal rectangle of 600 to 1000 by 400 to 800.
2388
+ */
2389
+ const rectGrid = defineGenerator({
2390
+ name: "rect-grid",
2391
+ version: 1,
2392
+ set: "corpus-v1",
2393
+ defaults: {
2394
+ containerSize: "fixed",
2395
+ containerShape: "rect",
2396
+ obstacles: "none",
2397
+ sizes: "uniform",
2398
+ count: 100
2399
+ },
2400
+ values: {
2401
+ containerSize: CONTAINER_SIZES,
2402
+ containerShape: CONTAINER_SHAPES,
2403
+ obstacles: OBSTACLE_LAYOUTS,
2404
+ sizes: SIZE_DISTRIBUTIONS
2405
+ },
2406
+ family: (p) => `rect-grid.${p.containerSize}.${p.containerShape}.${p.obstacles}.${p.sizes}.n${p.count}`,
2407
+ cell: (p) => cellWith(p.containerSize, p.containerShape, p.obstacles, p.sizes, p.count),
2408
+ build(seed, p) {
2409
+ let r = stepRandom(seed, "container");
2410
+ const w = int(r, 600, 1e3);
2411
+ const h = int(r, 400, 800);
2412
+ const { region, frame } = shapeOf(r, p.containerShape, w, h);
2413
+ const container = containerOf(p.containerSize, w, h, region);
2414
+ r = stepRandom(seed, "obstacles");
2415
+ const obstacles = obstaclesOf(r, p.obstacles, frame);
2416
+ r = stepRandom(seed, "boxes");
2417
+ return {
2418
+ container,
2419
+ obstacles,
2420
+ boxes: boxesOf(r, p.sizes, p.count, frame)
2421
+ };
2422
+ }
2423
+ });
2424
+ /** This function makes the generator of a family of a user interface. The container is 800 to 1400 by 600 to 1000, with no obstacle. */
2425
+ function uiGenerator(name, sizes, count, boxes) {
2426
+ return defineGenerator({
2427
+ name,
2428
+ version: 1,
2429
+ set: "corpus-v1",
2430
+ defaults: {
2431
+ containerSize: "freeHeight",
2432
+ count
2433
+ },
2434
+ values: { containerSize: CONTAINER_SIZES },
2435
+ family: (p) => `${name}.${p.containerSize}.n${p.count}`,
2436
+ cell: (p) => cellWith(p.containerSize, "rect", "none", sizes, p.count),
2437
+ build(seed, p) {
2438
+ const r = stepRandom(seed, "container");
2439
+ const w = int(r, 800, 1400);
2440
+ const h = int(r, 600, 1e3);
2441
+ return {
2442
+ container: containerOf(p.containerSize, w, h),
2443
+ obstacles: [],
2444
+ boxes: boxes(stepRandom(seed, "boxes"), p.count)
2445
+ };
2446
+ }
2447
+ });
2448
+ }
2449
+ /** The aspects of the thumbnails of a gallery. */
2450
+ const THUMBNAIL_ASPECTS = [
2451
+ 1,
2452
+ 4 / 3,
2453
+ 3 / 2,
2454
+ 16 / 9
2455
+ ];
2456
+ /** The generator `gallery`, version 1: thumbnails with one size and one aspect (1, 4:3, 3:2 or 16:9). The default container has a fixed width. */
2457
+ const gallery = uiGenerator("gallery", "identical", 100, (r, count) => {
2458
+ const aspect = THUMBNAIL_ASPECTS[int(r, 0, THUMBNAIL_ASPECTS.length - 1)];
2459
+ const w = int(r, 120, 240);
2460
+ return Array.from({ length: count }, (_, i) => ({
2461
+ id: `b${i}`,
2462
+ w,
2463
+ h: round3(w / aspect)
2464
+ }));
2465
+ });
2466
+ /** The spans of the dashboard tiles in grid units, and their weights. */
2467
+ const TILE_SPANS = [
2468
+ [1, 1],
2469
+ [2, 1],
2470
+ [1, 2],
2471
+ [2, 2],
2472
+ [3, 2],
2473
+ [4, 2]
2474
+ ];
2475
+ const TILE_WEIGHTS = [
2476
+ 4,
2477
+ 3,
2478
+ 2,
2479
+ 3,
2480
+ 1,
2481
+ 1
2482
+ ];
2483
+ /** The generator `dashboard`, version 1: tiles in 6 standard sizes, from 1 by 1 to 4 by 2 grid units of 60 to 100. */
2484
+ const dashboard = uiGenerator("dashboard", "bimodal", 10, (r, count) => {
2485
+ const unit = int(r, 60, 100);
2486
+ return Array.from({ length: count }, (_, i) => {
2487
+ const [cw, ch] = TILE_SPANS[pick(r, TILE_WEIGHTS)];
2488
+ return {
2489
+ id: `b${i}`,
2490
+ w: cw * unit,
2491
+ h: ch * unit
2492
+ };
2493
+ });
2494
+ });
2495
+ /** The generator `cards`, version 1: cards with one width from 200 to 320, and a height from 120 to 420 each. */
2496
+ const cards = uiGenerator("cards", "uniform", 100, (r, count) => {
2497
+ const w = int(r, 200, 320);
2498
+ return Array.from({ length: count }, (_, i) => ({
2499
+ id: `b${i}`,
2500
+ w,
2501
+ h: int(r, 120, 420)
2502
+ }));
2503
+ });
2504
+ /** The font sizes of a word cloud, and their weights: many small words and few large words. */
2505
+ const FONT_SIZES = [
2506
+ 12,
2507
+ 14,
2508
+ 16,
2509
+ 20,
2510
+ 28,
2511
+ 40
2512
+ ];
2513
+ const FONT_WEIGHTS = [
2514
+ 30,
2515
+ 25,
2516
+ 20,
2517
+ 12,
2518
+ 8,
2519
+ 5
2520
+ ];
2521
+ /** The generator `word-cloud`, version 1: boxes of text. The height comes from 6 font sizes, the width from 3 to 12 letters. */
2522
+ const wordCloud = uiGenerator("word-cloud", "heavyTail", 100, (r, count) => Array.from({ length: count }, (_, i) => {
2523
+ const font = FONT_SIZES[pick(r, FONT_WEIGHTS)];
2524
+ const letters = int(r, 3, 12);
2525
+ return {
2526
+ id: `b${i}`,
2527
+ w: round3(letters * font * .6),
2528
+ h: round3(font * 1.25)
2529
+ };
2530
+ }));
2531
+ /** The values of `case`. */
2532
+ const EDGE_CASES = [
2533
+ "empty",
2534
+ "one",
2535
+ "oversize",
2536
+ "exactTiling",
2537
+ "touchObstacles"
2538
+ ];
2539
+ /** This function gives the largest divisor of `n` that is not more than the square root of `n`. */
2540
+ const rowsOf = (n) => {
2541
+ for (let d = Math.floor(Math.sqrt(n)); d > 1; d--) if (n % d === 0) return d;
2542
+ return 1;
2543
+ };
2544
+ /**
2545
+ * The generator `edge-cases`, version 1. Each case has a fixed rectangle container.
2546
+ *
2547
+ * - `empty`: no box.
2548
+ * - `one`: one box.
2549
+ * - `oversize`: `count` boxes. One box is wider than the container.
2550
+ * - `exactTiling`: `count` identical boxes that fill the container with no free space.
2551
+ * - `touchObstacles`: a locked box on the top of an obstacle, and boxes as wide as the path between 2 obstacles.
2552
+ */
2553
+ const edgeCases = defineGenerator({
2554
+ name: "edge-cases",
2555
+ version: 1,
2556
+ set: "corpus-v1",
2557
+ defaults: {
2558
+ case: "oversize",
2559
+ count: 12
2560
+ },
2561
+ values: { case: EDGE_CASES },
2562
+ family: (p) => p.case === "empty" || p.case === "one" ? `edge-cases.${p.case}` : `edge-cases.${p.case}.n${p.count}`,
2563
+ cell: (p) => {
2564
+ switch (p.case) {
2565
+ case "empty": return cellWith("fixed", "rect", "none", "identical", 0);
2566
+ case "one": return cellWith("fixed", "rect", "none", "identical", 1);
2567
+ case "oversize": return cellWith("fixed", "rect", "none", "heavyTail", p.count);
2568
+ case "exactTiling": return cellWith("fixed", "rect", "none", "identical", p.count);
2569
+ case "touchObstacles": return cellWith("fixed", "rect", "corridors", "uniform", p.count, true);
2570
+ }
2571
+ },
2572
+ build(seed, p) {
2573
+ if ((p.case === "oversize" || p.case === "exactTiling" || p.case === "touchObstacles") && p.count < 1) throw new RangeError(`The case "${p.case}" of "edge-cases" needs a count of 1 or more.`);
2574
+ let r = stepRandom(seed, "container");
2575
+ let w;
2576
+ let h;
2577
+ let tile = {
2578
+ w: 0,
2579
+ h: 0,
2580
+ cols: 0,
2581
+ rows: 0
2582
+ };
2583
+ if (p.case === "exactTiling") {
2584
+ const rows = rowsOf(p.count);
2585
+ tile = {
2586
+ w: int(r, 40, 120),
2587
+ h: int(r, 30, 90),
2588
+ cols: p.count / rows,
2589
+ rows
2590
+ };
2591
+ w = tile.cols * tile.w;
2592
+ h = tile.rows * tile.h;
2593
+ } else {
2594
+ w = int(r, 600, 1e3);
2595
+ h = int(r, 400, 800);
2596
+ }
2597
+ const container = {
2598
+ w,
2599
+ h,
2600
+ fixed: "size"
2601
+ };
2602
+ r = stepRandom(seed, "obstacles");
2603
+ const obstacles = [];
2604
+ let path = 0;
2605
+ let first = {
2606
+ x: 0,
2607
+ y: 0
2608
+ };
2609
+ if (p.case === "touchObstacles") {
2610
+ path = int(r, 40, 100);
2611
+ const ow = int(r, 60, 150);
2612
+ const oh = int(r, 100, 200);
2613
+ first = {
2614
+ x: span(r, w * .1, w * .25),
2615
+ y: span(r, h * .2, h * .4)
2616
+ };
2617
+ obstacles.push({
2618
+ id: "o0",
2619
+ region: {
2620
+ kind: "rect",
2621
+ x: first.x,
2622
+ y: first.y,
2623
+ w: ow,
2624
+ h: oh
2625
+ }
2626
+ });
2627
+ obstacles.push({
2628
+ id: "o1",
2629
+ region: {
2630
+ kind: "rect",
2631
+ x: first.x + ow + path,
2632
+ y: first.y,
2633
+ w: int(r, 60, 150),
2634
+ h: oh
2635
+ }
2636
+ });
2637
+ }
2638
+ r = stepRandom(seed, "boxes");
2639
+ const boxes = [];
2640
+ switch (p.case) {
2641
+ case "empty": break;
2642
+ case "one":
2643
+ boxes.push({
2644
+ id: "b0",
2645
+ w: int(r, 40, 200),
2646
+ h: int(r, 30, 150)
2647
+ });
2648
+ break;
2649
+ case "oversize": {
2650
+ const big = int(r, 0, p.count - 1);
2651
+ for (let i = 0; i < p.count; i++) if (i === big) boxes.push({
2652
+ id: `b${i}`,
2653
+ w: w + int(r, 10, 200),
2654
+ h: int(r, 40, 100)
2655
+ });
2656
+ else boxes.push({
2657
+ id: `b${i}`,
2658
+ w: int(r, 20, 120),
2659
+ h: int(r, 20, 80)
2660
+ });
2661
+ break;
2662
+ }
2663
+ case "exactTiling":
2664
+ for (let i = 0; i < p.count; i++) boxes.push({
2665
+ id: `b${i}`,
2666
+ w: tile.w,
2667
+ h: tile.h
2668
+ });
2669
+ break;
2670
+ case "touchObstacles": {
2671
+ const lw = int(r, 30, 80);
2672
+ const lh = int(r, 20, 60);
2673
+ boxes.push({
2674
+ id: "b0",
2675
+ w: lw,
2676
+ h: lh,
2677
+ x: first.x,
2678
+ y: first.y - lh,
2679
+ locked: true
2680
+ });
2681
+ for (let i = 1; i < p.count; i++) boxes.push({
2682
+ id: `b${i}`,
2683
+ w: path,
2684
+ h: int(r, 20, 80)
2685
+ });
2686
+ break;
2687
+ }
2688
+ }
2689
+ return {
2690
+ container,
2691
+ obstacles,
2692
+ boxes
2693
+ };
2694
+ }
2695
+ });
2696
+ /** Each generator of the package. */
2697
+ const generators = Object.freeze([
2698
+ rectScattered,
2699
+ rectGrid,
2700
+ gallery,
2701
+ dashboard,
2702
+ cards,
2703
+ wordCloud,
2704
+ edgeCases
2705
+ ]);
2706
+ /** This function gives the generator with a name. It throws a `RangeError` for a name that the package does not have. */
2707
+ function generator(name) {
2708
+ const found = generators.find((g) => g.name === name);
2709
+ if (found === void 0) throw new RangeError(`The package has no generator "${name}".`);
2710
+ return found;
2711
+ }
2712
+ // src/corpus.ts
2713
+ /**
2714
+ * This function generates a corpus from specs. Each input gets the name of the corpus as its set. The function throws a
2715
+ * `RangeError` for an unknown generator, a different version or 2 inputs with the same id.
2716
+ */
2717
+ function corpus(name, specs) {
2718
+ const manifest = {
2719
+ schema: "layout-corpus/1",
2720
+ corpus: name,
2721
+ specs: [],
2722
+ inputs: []
2723
+ };
2724
+ const inputs = [];
2725
+ const ids = /* @__PURE__ */ new Set();
2726
+ for (const spec of specs) {
2727
+ const gen = generator(spec.generator);
2728
+ if (spec.version !== void 0 && spec.version !== gen.version) throw new RangeError(`The generator "${gen.name}" has the version ${gen.version}, not ${spec.version}.`);
2729
+ const params = gen.withDefaults(spec.params);
2730
+ const cell = gen.cell(params);
2731
+ manifest.specs.push({
2732
+ generator: gen.name,
2733
+ version: gen.version,
2734
+ params,
2735
+ seeds: [...spec.seeds]
2736
+ });
2737
+ for (const seed of spec.seeds) {
2738
+ const file = gen.generate(seed, params, name);
2739
+ if (ids.has(file.id)) throw new RangeError(`The corpus has 2 inputs with the id "${file.id}".`);
2740
+ ids.add(file.id);
2741
+ inputs.push(file);
2742
+ manifest.inputs.push({
2743
+ id: file.id,
2744
+ hash: file.hash,
2745
+ cell
2746
+ });
2747
+ }
2748
+ }
2749
+ return {
2750
+ manifest,
2751
+ inputs
2752
+ };
2753
+ }
2754
+ export { DEFAULT_TOLERANCE, EPS, RECORD_EPS, Rect, SORT_ORDERS, UnsupportedCell, Vector, aspectError, assertSupported, canonicalJson, cards, cellOf, checkHard, compareRects, corpus, countCell, createContext, dashboard, edgeCases, fnv1a32, fromRects, gallery, generator, generators, hashInput, logSweep, lowestWindow, marginOf, mulberry32, pack, packFree, packRows, packShelf, packSkyline, packWith, packingMetrics, push, pushCascade, rectGrid, rectScattered, round3, rows, shelf, skyline, strategies, strategy, supportsCell, toPackInput, toRects, turnItem, turnPoint, turnPort, turnRegion, unsupportedOf, wordCloud };
1646
2755
 
1647
2756
  //# sourceMappingURL=index.js.map