@mark1russell7/layout-contract 0.2.18 → 0.2.20
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/README.md +4 -0
- package/build/index.d.ts +51 -549
- package/build/index.d.ts.map +1 -1
- package/build/index.js +266 -1
- package/build/index.js.map +1 -1
- package/build/measures-CQAuu3T5.d.ts +560 -0
- package/build/measures-CQAuu3T5.d.ts.map +1 -0
- package/build/text.d.ts +104 -0
- package/build/text.d.ts.map +1 -0
- package/build/text.js +265 -0
- package/build/text.js.map +1 -0
- package/package.json +5 -1
- package/src/balance.ts +233 -0
- package/src/index.ts +1 -0
- package/src/text.ts +311 -0
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/tolerance.ts","../src/output.ts","../src/record.ts","../src/rules.ts","../src/turn.ts","../src/balance.ts","../src/strategy.ts","../src/random.ts","../src/edges.ts","../src/hash.ts","../src/portfolio.ts","../src/strategy-space.ts","../src/verdict.ts","../src/families.ts"],"mappings":";;qBACa;;;;;qBAMA;;;;;qBAMA;;;;;iBAMI;;WAEN;;WAEA;;;;;;qBAOE,mBAAmB;;iBCzBf;WACN;WACA;WACA;WACA;;;iBAIM;WACN;WACA;WACA,WAAW,SAAS;;;iBAId;WACN,qBAAqB;;WAErB;;WAEA;aAAiB;aAAoB;;;WAErC;aAA2B;aAAqB;aAAoB;;WACpE,iBAAiB;;;;;;;wBAQZ,UAAU,OAAO,aAAa,QAAQ,aAAa;;YC/BvD;;iBAGK;WACN;WACA,OAAO;WACP;WACA;;WAEA,SAAS,SAAS;WAClB;;WAEA;;WAEA;WACA;WACA;WACA;WACA;;WAEA,kBAAkB;;WAElB,UAAU,SAAS;;WAEnB,MAAM,SAAS;;WAEf,YAAY,SAAS,QAAQ;;WAE7B;;WAEA;;WAEA;;WAEA;;;;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;;;;wBAsB/B,SAAS,KAAK,iBAAiB,iBAAiB;;;;;;;;wBAsChD,kBACd,qBAAqB,mBACrB,OAAM,SAAS,eAAe,cAC9B,gBACA,cACC;;wBA8Ha,qBAAqB,IAAI,aAAa,WAAU,SAAS,aAAa,MAAW,eAAe;;iBA2G/F;WACN;WACA;;WAEA;WACA;;WAEA,qBAAqB;;WAErB;WACA,aAAa;;WAEb,cAAc,MAAM;;EAE7B,MAAM,OAAO,aAAa,cAAc,MAAM,kBAAkB;;;iBAIjD;;WAEN;;WAEA;WACA;;WAEA,QAAQ,OAAO;;;;;;;wBAWV,cAAc,MAAM,aAAa,cAAc,gBAAgB,cAAc,UAAS,iBAAsB,aAAa;;;;;qBA+D5H,YAAY;;;;;;qBAoBZ,gBAAgB,aAAa"}
|
package/build/index.js
CHANGED
|
@@ -1110,6 +1110,271 @@ function compareMeasure(candidate, baseline, spec) {
|
|
|
1110
1110
|
}
|
|
1111
1111
|
return (spec.direction === "lower" ? delta < 0 : delta > 0) ? "better" : "worse";
|
|
1112
1112
|
}
|
|
1113
|
+
// src/balance.ts
|
|
1114
|
+
/** The depth of the strip of each side, as a share of the container. For `top` and `bottom`, it is a share of the height. For `left` and `right`, it is a share of the width. */
|
|
1115
|
+
const BALANCE_SIDE_DEPTH = .25;
|
|
1116
|
+
/** The suffixes of the balance and stability measures, in the order of `docs/BALANCE.md`. */
|
|
1117
|
+
const BALANCE_MEASURE_SUFFIXES = Object.freeze([
|
|
1118
|
+
"balance.gapCv",
|
|
1119
|
+
"balance.centroidOffset",
|
|
1120
|
+
"balance.side.top",
|
|
1121
|
+
"balance.side.right",
|
|
1122
|
+
"balance.side.bottom",
|
|
1123
|
+
"balance.side.left",
|
|
1124
|
+
"balance.sideRange",
|
|
1125
|
+
"stability.procrustes",
|
|
1126
|
+
"stability.seed"
|
|
1127
|
+
]);
|
|
1128
|
+
/** This function gives the area of the overlap of two rectangles. */
|
|
1129
|
+
function overlapArea(a, b) {
|
|
1130
|
+
const w = Math.min(a.x + a.w, b.x + b.w) - Math.max(a.x, b.x);
|
|
1131
|
+
const h = Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y);
|
|
1132
|
+
return w > 0 && h > 0 ? w * h : 0;
|
|
1133
|
+
}
|
|
1134
|
+
/** This function gives the distance between two rectangles. Two rectangles that touch or overlap give 0. */
|
|
1135
|
+
function rectDistance(a, b) {
|
|
1136
|
+
const dx = Math.max(0, b.x - (a.x + a.w), a.x - (b.x + b.w));
|
|
1137
|
+
const dy = Math.max(0, b.y - (a.y + a.h), a.y - (b.y + b.h));
|
|
1138
|
+
return Math.hypot(dx, dy);
|
|
1139
|
+
}
|
|
1140
|
+
/**
|
|
1141
|
+
* This function gives the gap of each box to its nearest other box, in the order of `boxes`.
|
|
1142
|
+
* Each box scans the boxes sorted by `x` in the two directions. A scan stops when the distance on `x` alone is above the best gap of the box.
|
|
1143
|
+
* To the left, the distance on `x` is at least `a.x - b.x - maxW`, with `maxW` the largest width. With fewer than 2 boxes, a gap is `Infinity`.
|
|
1144
|
+
*/
|
|
1145
|
+
function nearestGaps(boxes) {
|
|
1146
|
+
const order = boxes.map((_, i) => i).sort((i, j) => boxes[i].x - boxes[j].x || i - j);
|
|
1147
|
+
const maxW = boxes.reduce((m, b) => Math.max(m, b.w), 0);
|
|
1148
|
+
const gaps = new Array(boxes.length).fill(Infinity);
|
|
1149
|
+
for (let k = 0; k < order.length; k++) {
|
|
1150
|
+
const a = boxes[order[k]];
|
|
1151
|
+
let best = Infinity;
|
|
1152
|
+
for (let m = k + 1; m < order.length; m++) {
|
|
1153
|
+
const b = boxes[order[m]];
|
|
1154
|
+
if (b.x - (a.x + a.w) > best) break;
|
|
1155
|
+
best = Math.min(best, rectDistance(a, b));
|
|
1156
|
+
}
|
|
1157
|
+
for (let m = k - 1; m >= 0; m--) {
|
|
1158
|
+
const b = boxes[order[m]];
|
|
1159
|
+
if (a.x - b.x - maxW > best) break;
|
|
1160
|
+
best = Math.min(best, rectDistance(a, b));
|
|
1161
|
+
}
|
|
1162
|
+
gaps[order[k]] = best;
|
|
1163
|
+
}
|
|
1164
|
+
return gaps;
|
|
1165
|
+
}
|
|
1166
|
+
/**
|
|
1167
|
+
* This function gives the balance of the placed boxes in the container: the evenness of the gaps and the place of the free space.
|
|
1168
|
+
* The boxes do not overlap, as in the output of a packer. The area of a box outside the container does not count.
|
|
1169
|
+
* `docs/BALANCE.md` gives the definitions and the reasons.
|
|
1170
|
+
*/
|
|
1171
|
+
function balanceOf(boxes, container) {
|
|
1172
|
+
const gaps = boxes.length < 2 ? [] : nearestGaps(boxes);
|
|
1173
|
+
const mean = gaps.length === 0 ? 0 : gaps.reduce((s, g) => s + g, 0) / gaps.length;
|
|
1174
|
+
const sd = gaps.length === 0 ? 0 : Math.sqrt(gaps.reduce((s, g) => s + (g - mean) ** 2, 0) / gaps.length);
|
|
1175
|
+
const gapCv = mean > 0 ? sd / mean : 0;
|
|
1176
|
+
const area = container.w * container.h;
|
|
1177
|
+
const cx = container.x + container.w / 2;
|
|
1178
|
+
const cy = container.y + container.h / 2;
|
|
1179
|
+
let used = 0;
|
|
1180
|
+
let mx = 0;
|
|
1181
|
+
let my = 0;
|
|
1182
|
+
for (const b of boxes) {
|
|
1183
|
+
const x0 = Math.max(b.x, container.x);
|
|
1184
|
+
const y0 = Math.max(b.y, container.y);
|
|
1185
|
+
const x1 = Math.min(b.x + b.w, container.x + container.w);
|
|
1186
|
+
const y1 = Math.min(b.y + b.h, container.y + container.h);
|
|
1187
|
+
if (x1 <= x0 || y1 <= y0) continue;
|
|
1188
|
+
const a = (x1 - x0) * (y1 - y0);
|
|
1189
|
+
used += a;
|
|
1190
|
+
mx += a * (x0 + x1) / 2;
|
|
1191
|
+
my += a * (y0 + y1) / 2;
|
|
1192
|
+
}
|
|
1193
|
+
const free = area - used;
|
|
1194
|
+
const half = Math.hypot(container.w, container.h) / 2;
|
|
1195
|
+
let centroidOffset = 0;
|
|
1196
|
+
if (free > 0 && half > 0) {
|
|
1197
|
+
const fx = (area * cx - mx) / free;
|
|
1198
|
+
const fy = (area * cy - my) / free;
|
|
1199
|
+
centroidOffset = Math.min(1, Math.hypot(fx - cx, fy - cy) / half);
|
|
1200
|
+
}
|
|
1201
|
+
const dh = container.h * BALANCE_SIDE_DEPTH;
|
|
1202
|
+
const dw = container.w * BALANCE_SIDE_DEPTH;
|
|
1203
|
+
const strips = {
|
|
1204
|
+
top: {
|
|
1205
|
+
x: container.x,
|
|
1206
|
+
y: container.y,
|
|
1207
|
+
w: container.w,
|
|
1208
|
+
h: dh
|
|
1209
|
+
},
|
|
1210
|
+
right: {
|
|
1211
|
+
x: container.x + container.w - dw,
|
|
1212
|
+
y: container.y,
|
|
1213
|
+
w: dw,
|
|
1214
|
+
h: container.h
|
|
1215
|
+
},
|
|
1216
|
+
bottom: {
|
|
1217
|
+
x: container.x,
|
|
1218
|
+
y: container.y + container.h - dh,
|
|
1219
|
+
w: container.w,
|
|
1220
|
+
h: dh
|
|
1221
|
+
},
|
|
1222
|
+
left: {
|
|
1223
|
+
x: container.x,
|
|
1224
|
+
y: container.y,
|
|
1225
|
+
w: dw,
|
|
1226
|
+
h: container.h
|
|
1227
|
+
}
|
|
1228
|
+
};
|
|
1229
|
+
const side = {};
|
|
1230
|
+
for (const s of SIDES) {
|
|
1231
|
+
const strip = strips[s];
|
|
1232
|
+
const stripArea = strip.w * strip.h;
|
|
1233
|
+
const filled = boxes.reduce((sum, b) => sum + overlapArea(b, strip), 0);
|
|
1234
|
+
side[s] = stripArea > 0 ? Math.min(1, Math.max(0, 1 - filled / stripArea)) : 0;
|
|
1235
|
+
}
|
|
1236
|
+
const values = SIDES.map((s) => side[s]);
|
|
1237
|
+
return {
|
|
1238
|
+
gapCv,
|
|
1239
|
+
centroidOffset,
|
|
1240
|
+
side,
|
|
1241
|
+
sideRange: Math.max(...values) - Math.min(...values)
|
|
1242
|
+
};
|
|
1243
|
+
}
|
|
1244
|
+
/**
|
|
1245
|
+
* This function gives the root mean square move of the box centers from `base` to `next`, after the best alignment, in units of `unit`.
|
|
1246
|
+
* The alignment is the orthogonal Procrustes fit: one move and, by default, one rotation. Only the ids in the two lists count.
|
|
1247
|
+
* Fewer than 2 shared ids give 0, because the alignment then removes each move.
|
|
1248
|
+
*/
|
|
1249
|
+
function procrustesOf(base, next, options) {
|
|
1250
|
+
if (!(Number.isFinite(options.unit) && options.unit > 0)) throw new RangeError(`procrustesOf: the unit is a finite number above 0, not ${options.unit}.`);
|
|
1251
|
+
const after = new Map(next.map((b) => [b.id, b]));
|
|
1252
|
+
const a = [];
|
|
1253
|
+
const b = [];
|
|
1254
|
+
for (const p of base) {
|
|
1255
|
+
const q = after.get(p.id);
|
|
1256
|
+
if (q === void 0) continue;
|
|
1257
|
+
a.push([p.x + p.w / 2, p.y + p.h / 2]);
|
|
1258
|
+
b.push([q.x + q.w / 2, q.y + q.h / 2]);
|
|
1259
|
+
}
|
|
1260
|
+
const n = a.length;
|
|
1261
|
+
if (n < 2) return 0;
|
|
1262
|
+
const mean = (list) => [list.reduce((s, p) => s + p[0], 0) / n, list.reduce((s, p) => s + p[1], 0) / n];
|
|
1263
|
+
const [ax, ay] = mean(a);
|
|
1264
|
+
const [bx, by] = mean(b);
|
|
1265
|
+
let dot = 0;
|
|
1266
|
+
let cross = 0;
|
|
1267
|
+
for (let i = 0; i < n; i++) {
|
|
1268
|
+
const [px, py] = [a[i][0] - ax, a[i][1] - ay];
|
|
1269
|
+
const [qx, qy] = [b[i][0] - bx, b[i][1] - by];
|
|
1270
|
+
dot += px * qx + py * qy;
|
|
1271
|
+
cross += px * qy - py * qx;
|
|
1272
|
+
}
|
|
1273
|
+
const theta = options.rotate === false || dot === 0 && cross === 0 ? 0 : Math.atan2(cross, dot);
|
|
1274
|
+
const cos = Math.cos(theta);
|
|
1275
|
+
const sin = Math.sin(theta);
|
|
1276
|
+
let sum = 0;
|
|
1277
|
+
for (let i = 0; i < n; i++) {
|
|
1278
|
+
const [px, py] = [a[i][0] - ax, a[i][1] - ay];
|
|
1279
|
+
const [qx, qy] = [b[i][0] - bx, b[i][1] - by];
|
|
1280
|
+
sum += (cos * px - sin * py - qx) ** 2 + (sin * px + cos * py - qy) ** 2;
|
|
1281
|
+
}
|
|
1282
|
+
return Math.sqrt(sum / n) / options.unit;
|
|
1283
|
+
}
|
|
1284
|
+
/**
|
|
1285
|
+
* This function gives the value of `stability.procrustes`: the move of `procrustesOf` divided by the change share.
|
|
1286
|
+
* The change share is `changed / count`, and it is at least `1 / count`. Thus a change of one box divides by the smallest share.
|
|
1287
|
+
*/
|
|
1288
|
+
function perChange(move, changed, count) {
|
|
1289
|
+
if (!(Number.isInteger(count) && count >= 1)) throw new RangeError(`perChange: the count is an integer of 1 or more, not ${count}.`);
|
|
1290
|
+
return move / (Math.max(1, Math.min(count, changed)) / count);
|
|
1291
|
+
}
|
|
1292
|
+
/** This function gives the balance values as measures, with the prefix of the stage, for example `pack.balance.gapCv`. */
|
|
1293
|
+
function balanceMeasures(stage, balance) {
|
|
1294
|
+
const m = {
|
|
1295
|
+
[`${stage}.balance.gapCv`]: balance.gapCv,
|
|
1296
|
+
[`${stage}.balance.centroidOffset`]: balance.centroidOffset,
|
|
1297
|
+
[`${stage}.balance.sideRange`]: balance.sideRange
|
|
1298
|
+
};
|
|
1299
|
+
for (const s of SIDES) m[`${stage}.balance.side.${s}`] = balance.side[s];
|
|
1300
|
+
return m;
|
|
1301
|
+
}
|
|
1302
|
+
/**
|
|
1303
|
+
* This function gives the specs of the balance and stability measures, with the prefix of the stage.
|
|
1304
|
+
* The balance measures read the area of each box, so they support only `rect`. The stability measures read only the centers.
|
|
1305
|
+
*/
|
|
1306
|
+
function balanceMeasureSpecs(stage) {
|
|
1307
|
+
const id = (suffix) => `${stage}.${suffix}`;
|
|
1308
|
+
const rect = ["rect"];
|
|
1309
|
+
const centers = SHAPE_KIND_IDS;
|
|
1310
|
+
return Object.freeze([
|
|
1311
|
+
{
|
|
1312
|
+
id: id("balance.gapCv"),
|
|
1313
|
+
unit: "ratio",
|
|
1314
|
+
direction: "lower",
|
|
1315
|
+
min: 0,
|
|
1316
|
+
max: Infinity,
|
|
1317
|
+
minEffect: .01,
|
|
1318
|
+
family: "balance",
|
|
1319
|
+
shapes: rect
|
|
1320
|
+
},
|
|
1321
|
+
{
|
|
1322
|
+
id: id("balance.centroidOffset"),
|
|
1323
|
+
unit: "0 to 1",
|
|
1324
|
+
direction: "lower",
|
|
1325
|
+
min: 0,
|
|
1326
|
+
max: 1,
|
|
1327
|
+
minEffect: .01,
|
|
1328
|
+
family: "balance",
|
|
1329
|
+
shapes: rect
|
|
1330
|
+
},
|
|
1331
|
+
...SIDES.map((s) => ({
|
|
1332
|
+
id: id(`balance.side.${s}`),
|
|
1333
|
+
unit: "0 to 1",
|
|
1334
|
+
direction: "lower",
|
|
1335
|
+
min: 0,
|
|
1336
|
+
max: 1,
|
|
1337
|
+
family: "balance",
|
|
1338
|
+
role: "diagnostic",
|
|
1339
|
+
shapes: rect
|
|
1340
|
+
})),
|
|
1341
|
+
{
|
|
1342
|
+
id: id("balance.sideRange"),
|
|
1343
|
+
unit: "0 to 1",
|
|
1344
|
+
direction: "lower",
|
|
1345
|
+
min: 0,
|
|
1346
|
+
max: 1,
|
|
1347
|
+
minEffect: .02,
|
|
1348
|
+
family: "balance",
|
|
1349
|
+
shapes: rect
|
|
1350
|
+
},
|
|
1351
|
+
{
|
|
1352
|
+
id: id("stability.procrustes"),
|
|
1353
|
+
unit: "W for each change share",
|
|
1354
|
+
direction: "lower",
|
|
1355
|
+
min: 0,
|
|
1356
|
+
max: Infinity,
|
|
1357
|
+
floor: 0,
|
|
1358
|
+
tol: .005,
|
|
1359
|
+
minEffect: .005,
|
|
1360
|
+
family: "stability",
|
|
1361
|
+
role: "change-guard",
|
|
1362
|
+
shapes: centers
|
|
1363
|
+
},
|
|
1364
|
+
{
|
|
1365
|
+
id: id("stability.seed"),
|
|
1366
|
+
unit: "W",
|
|
1367
|
+
direction: "lower",
|
|
1368
|
+
min: 0,
|
|
1369
|
+
max: Infinity,
|
|
1370
|
+
floor: 0,
|
|
1371
|
+
tol: .005,
|
|
1372
|
+
minEffect: .005,
|
|
1373
|
+
family: "stability",
|
|
1374
|
+
shapes: centers
|
|
1375
|
+
}
|
|
1376
|
+
]);
|
|
1377
|
+
}
|
|
1113
1378
|
// src/utf8.ts
|
|
1114
1379
|
/** This function gives the UTF-8 bytes of a string. The package has no runtime dependencies, thus it does not use `TextEncoder`. */
|
|
1115
1380
|
function utf8(text) {
|
|
@@ -3991,6 +4256,6 @@ const RECT_SCATTERED = Object.freeze({
|
|
|
3991
4256
|
};
|
|
3992
4257
|
}
|
|
3993
4258
|
});
|
|
3994
|
-
export { CORPUS_DIMENSIONS, DEFAULT_FAMILY_BY, DEFAULT_RNG, DEFAULT_SHAPES, DEFAULT_TOLERANCE, EDGES_STEP, EDGE_DENSITY, EDGE_STEP_VERSION, EPS, FAMILY_ANY_KIND, INVALIDATED_FIELD, JOINT_MEASURES, JOINT_MOVED_MIN, PACK_HARD_RULES, RECORD_EPS, RECORD_SCALE, RECT_SCATTERED, SHAPE_KINDS, SHAPE_KIND_IDS, SIDES, SPLIT_SCHEMA, STRATEGY_SPACE_SCHEMA, VERDICT_DRAWS, VERDICT_EXACT_CAP, VERDICT_EXACT_MAX, VERDICT_RUNS, VERDICT_SEED, applyPack, boundsOf, boxArea, canonicalConfig, canonicalInput, canonicalJson, checkConfig, checkDeclared, checkInput, checkShapes, checkSpace, checkSplit, compareMeasure, composeFamily, configId, corpusHashOf, countObstacleHits, countOutside, countOverlaps, countPortsOffBorder, createFamilyRegistry, declaredClaimOf, declaredCounts, dimensionAt, dimensionSupports, dimensionUnit, documentHash, edgeStep, enumerateSpace, exercised, familyKind, familyOf, fnv1a32, grow, isShapeKind, itemShapeOf, joinConfigs, leaveOneSeedOut, levelsOf, marginSides, minEffectOf, mulberry32, outputGroups, outputHash, overlapOf, ownerDimension, packHard, paramActive, paramLevels, paramProblem, paramValue, pickMoves, placedRect, portPoint, portfolio, productSpace, randomInt, rectInRegion, regionArea, regionHitsRect, resolveDimensions, ruleLevel, selectBest, sha256Hex, shapeKindOf, shapeKindProblem, shapeKindsOf, shapeLevel, shapeSupportProblems, shapesOf, sharedDimension, spaceShapeKinds, spaceSize, splitConfig, splitFileName, splitFolds, stepRandom, toLayoutRecord, toPackInput, translate, turnBox, turnMargin, turnPoint, turnPort, turnRegion, turnSide, verdict };
|
|
4259
|
+
export { BALANCE_MEASURE_SUFFIXES, BALANCE_SIDE_DEPTH, CORPUS_DIMENSIONS, DEFAULT_FAMILY_BY, DEFAULT_RNG, DEFAULT_SHAPES, DEFAULT_TOLERANCE, EDGES_STEP, EDGE_DENSITY, EDGE_STEP_VERSION, EPS, FAMILY_ANY_KIND, INVALIDATED_FIELD, JOINT_MEASURES, JOINT_MOVED_MIN, PACK_HARD_RULES, RECORD_EPS, RECORD_SCALE, RECT_SCATTERED, SHAPE_KINDS, SHAPE_KIND_IDS, SIDES, SPLIT_SCHEMA, STRATEGY_SPACE_SCHEMA, VERDICT_DRAWS, VERDICT_EXACT_CAP, VERDICT_EXACT_MAX, VERDICT_RUNS, VERDICT_SEED, applyPack, balanceMeasureSpecs, balanceMeasures, balanceOf, boundsOf, boxArea, canonicalConfig, canonicalInput, canonicalJson, checkConfig, checkDeclared, checkInput, checkShapes, checkSpace, checkSplit, compareMeasure, composeFamily, configId, corpusHashOf, countObstacleHits, countOutside, countOverlaps, countPortsOffBorder, createFamilyRegistry, declaredClaimOf, declaredCounts, dimensionAt, dimensionSupports, dimensionUnit, documentHash, edgeStep, enumerateSpace, exercised, familyKind, familyOf, fnv1a32, grow, isShapeKind, itemShapeOf, joinConfigs, leaveOneSeedOut, levelsOf, marginSides, minEffectOf, mulberry32, nearestGaps, outputGroups, outputHash, overlapOf, ownerDimension, packHard, paramActive, paramLevels, paramProblem, paramValue, perChange, pickMoves, placedRect, portPoint, portfolio, procrustesOf, productSpace, randomInt, rectInRegion, regionArea, regionHitsRect, resolveDimensions, ruleLevel, selectBest, sha256Hex, shapeKindOf, shapeKindProblem, shapeKindsOf, shapeLevel, shapeSupportProblems, shapesOf, sharedDimension, spaceShapeKinds, spaceSize, splitConfig, splitFileName, splitFolds, stepRandom, toLayoutRecord, toPackInput, translate, turnBox, turnMargin, turnPoint, turnPort, turnRegion, turnSide, verdict };
|
|
3995
4260
|
|
|
3996
4261
|
//# sourceMappingURL=index.js.map
|