@mark1russell7/layout-contract 0.9.9 → 0.10.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/README.md +12 -7
- package/build/index.d.ts +314 -11
- package/build/index.d.ts.map +1 -1
- package/build/index.js +784 -24
- package/build/index.js.map +1 -1
- package/build/{measures-CJZw-Y3H.d.ts → measures-J0yY_JvV.d.ts} +32 -2
- package/build/measures-J0yY_JvV.d.ts.map +1 -0
- package/build/text.d.ts +1 -1
- package/build/text.js +3 -3
- package/build/text.js.map +1 -1
- package/package.json +1 -1
- package/src/balance.ts +11 -4
- package/src/cell.ts +104 -0
- package/src/curves.ts +200 -0
- package/src/geometry.ts +136 -0
- package/src/hash.ts +2 -2
- package/src/index.ts +4 -0
- package/src/input.ts +5 -0
- package/src/measure-registry.ts +192 -0
- package/src/measure-survey.ts +149 -0
- package/src/measures.ts +4 -4
- package/src/output.ts +15 -3
- package/src/resize.ts +13 -1
- package/src/rules.ts +82 -18
- package/src/text.ts +3 -3
- package/build/measures-CJZw-Y3H.d.ts.map +0 -1
package/build/index.js
CHANGED
|
@@ -147,6 +147,37 @@ function ringArea(ring) {
|
|
|
147
147
|
}
|
|
148
148
|
return Math.abs(twice) / 2;
|
|
149
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* This function gives the perimeter of a region. A `rect` gives `2 * (w + h)`, and a circle gives `2 * PI * r`.
|
|
152
|
+
* A polygon gives the length of its outer ring plus the length of each hole. A ring with fewer than 2 points has the length 0.
|
|
153
|
+
*
|
|
154
|
+
* An ellipse has no closed form. It gives the second formula of Ramanujan, with a relative error below 5e-4 of the true perimeter. The error is largest for a flat ellipse.
|
|
155
|
+
*/
|
|
156
|
+
function regionPerimeter(region) {
|
|
157
|
+
switch (region.kind) {
|
|
158
|
+
case "rect": return 2 * (region.w + region.h);
|
|
159
|
+
case "circle": return 2 * Math.PI * region.r;
|
|
160
|
+
case "ellipse": {
|
|
161
|
+
const a = Math.abs(region.rx);
|
|
162
|
+
const b = Math.abs(region.ry);
|
|
163
|
+
if (a + b === 0) return 0;
|
|
164
|
+
const t = ((a - b) / (a + b)) ** 2;
|
|
165
|
+
return Math.PI * (a + b) * (1 + 3 * t / (10 + Math.sqrt(4 - 3 * t)));
|
|
166
|
+
}
|
|
167
|
+
case "polygon": return (region.holes ?? []).reduce((length, hole) => length + ringLength(hole), ringLength(region.points));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/** This function gives the length of the closed ring of a polygon. */
|
|
171
|
+
function ringLength(ring) {
|
|
172
|
+
if (ring.length < 2) return 0;
|
|
173
|
+
let length = 0;
|
|
174
|
+
for (let i = 0; i < ring.length; i++) {
|
|
175
|
+
const [ax, ay] = ring[i];
|
|
176
|
+
const [bx, by] = ring[(i + 1) % ring.length];
|
|
177
|
+
length += Math.hypot(bx - ax, by - ay);
|
|
178
|
+
}
|
|
179
|
+
return length;
|
|
180
|
+
}
|
|
150
181
|
/** This function gives the region moved by `dx` and `dy`. */
|
|
151
182
|
function translate(region, dx, dy) {
|
|
152
183
|
switch (region.kind) {
|
|
@@ -184,6 +215,98 @@ function overlapOf(a, b) {
|
|
|
184
215
|
y: Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y)
|
|
185
216
|
};
|
|
186
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* This function gives the corners of a convex region, or `undefined` for each other region.
|
|
220
|
+
* A `rect` gives its 4 corners. A `polygon` without holes gives its points when it has 3 or more corners, an area over 0, and each turn to the same side.
|
|
221
|
+
* The turns use exact signs. A corner on a straight side is permitted. A circle and an ellipse give `undefined`.
|
|
222
|
+
*/
|
|
223
|
+
function convexCorners(region) {
|
|
224
|
+
if (region.kind === "rect") {
|
|
225
|
+
const { x, y, w, h } = region;
|
|
226
|
+
return [
|
|
227
|
+
[x, y],
|
|
228
|
+
[x + w, y],
|
|
229
|
+
[x + w, y + h],
|
|
230
|
+
[x, y + h]
|
|
231
|
+
];
|
|
232
|
+
}
|
|
233
|
+
if (region.kind !== "polygon" || region.holes !== void 0 && region.holes.length > 0) return void 0;
|
|
234
|
+
const ring = region.points;
|
|
235
|
+
if (ring.length < 3 || !ring.every(([x, y]) => Number.isFinite(x) && Number.isFinite(y))) return void 0;
|
|
236
|
+
let turn = 0;
|
|
237
|
+
for (let i = 0; i < ring.length; i++) {
|
|
238
|
+
const [ax, ay] = ring[i];
|
|
239
|
+
const [bx, by] = ring[(i + 1) % ring.length];
|
|
240
|
+
const [cx, cy] = ring[(i + 2) % ring.length];
|
|
241
|
+
const s = orient2dSign(ax, ay, bx, by, cx, cy);
|
|
242
|
+
if (s === 0) continue;
|
|
243
|
+
if (turn !== 0 && s !== turn) return void 0;
|
|
244
|
+
turn = s;
|
|
245
|
+
}
|
|
246
|
+
return turn === 0 ? void 0 : ring;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* This function gives the least overlap of two convex rings over the normals of their sides, in the units of the corners.
|
|
250
|
+
* By the separating axis theorem, a value of 0 or less tells that a line separates the rings. For two rectangles, the value is the lesser axis of `overlapOf`.
|
|
251
|
+
* A side of length 0 gives no normal. The rings come from `convexCorners`.
|
|
252
|
+
*/
|
|
253
|
+
function convexOverlap(a, b) {
|
|
254
|
+
let least = Infinity;
|
|
255
|
+
for (const ring of [a, b]) for (let i = 0; i < ring.length; i++) {
|
|
256
|
+
const [px, py] = ring[i];
|
|
257
|
+
const [qx, qy] = ring[(i + 1) % ring.length];
|
|
258
|
+
const length = Math.hypot(qx - px, qy - py);
|
|
259
|
+
if (length === 0) continue;
|
|
260
|
+
const nx = (py - qy) / length;
|
|
261
|
+
const ny = (qx - px) / length;
|
|
262
|
+
const [aLow, aHigh] = project(a, nx, ny);
|
|
263
|
+
const [bLow, bHigh] = project(b, nx, ny);
|
|
264
|
+
least = Math.min(least, Math.min(aHigh, bHigh) - Math.max(aLow, bLow));
|
|
265
|
+
}
|
|
266
|
+
return least;
|
|
267
|
+
}
|
|
268
|
+
/** This function gives the least and the greatest projection of the corners of a ring on the direction (`nx`, `ny`). */
|
|
269
|
+
function project(ring, nx, ny) {
|
|
270
|
+
let low = Infinity;
|
|
271
|
+
let high = -Infinity;
|
|
272
|
+
for (const [x, y] of ring) {
|
|
273
|
+
const v = x * nx + y * ny;
|
|
274
|
+
if (v < low) low = v;
|
|
275
|
+
if (v > high) high = v;
|
|
276
|
+
}
|
|
277
|
+
return [low, high];
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* This function tells if each corner of the ring `inner` is inside the convex ring `outer` made smaller by `inset`, with the tolerance `tolerance`.
|
|
281
|
+
* For a convex `outer`, this test is exact: a polygon is inside a convex region when each of its corners is inside.
|
|
282
|
+
* The ring `outer` comes from `convexCorners`, in either direction of its corners.
|
|
283
|
+
*/
|
|
284
|
+
function cornersInConvex(inner, outer, inset = 0, tolerance = 0) {
|
|
285
|
+
const turn = Math.sign(ringTwiceArea(outer));
|
|
286
|
+
for (let i = 0; i < outer.length; i++) {
|
|
287
|
+
const [px, py] = outer[i];
|
|
288
|
+
const [qx, qy] = outer[(i + 1) % outer.length];
|
|
289
|
+
const length = Math.hypot(qx - px, qy - py);
|
|
290
|
+
if (length === 0) continue;
|
|
291
|
+
const nx = turn * (py - qy) / length;
|
|
292
|
+
const ny = turn * (qx - px) / length;
|
|
293
|
+
const edge = px * nx + py * ny;
|
|
294
|
+
for (const [x, y] of inner) if (x * nx + y * ny - edge < inset - tolerance) return false;
|
|
295
|
+
}
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
/** This function gives two times the signed area of a ring, by the shoelace formula, relative to its first corner. */
|
|
299
|
+
function ringTwiceArea(ring) {
|
|
300
|
+
if (ring.length === 0) return 0;
|
|
301
|
+
const [ox, oy] = ring[0];
|
|
302
|
+
let twice = 0;
|
|
303
|
+
for (let i = 0; i < ring.length; i++) {
|
|
304
|
+
const [ax, ay] = ring[i];
|
|
305
|
+
const [bx, by] = ring[(i + 1) % ring.length];
|
|
306
|
+
twice += (ax - ox) * (by - oy) - (bx - ox) * (ay - oy);
|
|
307
|
+
}
|
|
308
|
+
return twice;
|
|
309
|
+
}
|
|
187
310
|
/** This function gives the cross product of `ab` and `ac`. The sign tells the side of `ab` that has `c`. */
|
|
188
311
|
function orient(a, b, c) {
|
|
189
312
|
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
|
|
@@ -326,6 +449,176 @@ function dropIndex(point, centers, rowTolerance = 24) {
|
|
|
326
449
|
}
|
|
327
450
|
return centers.length;
|
|
328
451
|
}
|
|
452
|
+
// src/curves.ts
|
|
453
|
+
/** The 8 symmetries of a square, in a fixed order. */
|
|
454
|
+
const CURVE_SYMMETRIES = Object.freeze([
|
|
455
|
+
"same",
|
|
456
|
+
"swap",
|
|
457
|
+
"back",
|
|
458
|
+
"half",
|
|
459
|
+
"rise",
|
|
460
|
+
"fall",
|
|
461
|
+
"flip-s",
|
|
462
|
+
"flip-t"
|
|
463
|
+
]);
|
|
464
|
+
/** The order of the parts of the Hilbert and Moore curves: low left, high left, high right and low right. */
|
|
465
|
+
const LOOP_ORDER = Object.freeze([
|
|
466
|
+
Object.freeze([0, 0]),
|
|
467
|
+
Object.freeze([0, 1]),
|
|
468
|
+
Object.freeze([1, 1]),
|
|
469
|
+
Object.freeze([1, 0])
|
|
470
|
+
]);
|
|
471
|
+
/** The parts of the Hilbert curve. The curve goes in at the corner `(0, 0)` and out at the corner `(size, 0)`. */
|
|
472
|
+
const HILBERT_PATTERN = Object.freeze([
|
|
473
|
+
"swap",
|
|
474
|
+
"same",
|
|
475
|
+
"same",
|
|
476
|
+
"back"
|
|
477
|
+
]);
|
|
478
|
+
/** The top level of the Moore curve: four Hilbert curves in a loop. The loop goes in and out at the middle of the side `t = 0`. */
|
|
479
|
+
const MOORE_PATTERN = Object.freeze([
|
|
480
|
+
"rise",
|
|
481
|
+
"rise",
|
|
482
|
+
"fall",
|
|
483
|
+
"fall"
|
|
484
|
+
]);
|
|
485
|
+
/** The Hilbert curve. Two cells in a row along the curve share a side. */
|
|
486
|
+
const HILBERT_CURVE = Object.freeze({
|
|
487
|
+
name: "hilbert",
|
|
488
|
+
order: LOOP_ORDER,
|
|
489
|
+
root: HILBERT_PATTERN,
|
|
490
|
+
child: HILBERT_PATTERN
|
|
491
|
+
});
|
|
492
|
+
/** The Moore curve. Two cells in a row share a side, and the last cell shares a side with the first cell. */
|
|
493
|
+
const MOORE_CURVE = Object.freeze({
|
|
494
|
+
name: "moore",
|
|
495
|
+
order: LOOP_ORDER,
|
|
496
|
+
root: MOORE_PATTERN,
|
|
497
|
+
child: HILBERT_PATTERN
|
|
498
|
+
});
|
|
499
|
+
/** The Morton curve (the Z order). The index of a cell has the bits of `x` at the even places and the bits of `y` at the odd places. */
|
|
500
|
+
const MORTON_CURVE = Object.freeze({
|
|
501
|
+
name: "morton",
|
|
502
|
+
order: Object.freeze([
|
|
503
|
+
Object.freeze([0, 0]),
|
|
504
|
+
Object.freeze([1, 0]),
|
|
505
|
+
Object.freeze([0, 1]),
|
|
506
|
+
Object.freeze([1, 1])
|
|
507
|
+
]),
|
|
508
|
+
root: Object.freeze([
|
|
509
|
+
"same",
|
|
510
|
+
"same",
|
|
511
|
+
"same",
|
|
512
|
+
"same"
|
|
513
|
+
]),
|
|
514
|
+
child: Object.freeze([
|
|
515
|
+
"same",
|
|
516
|
+
"same",
|
|
517
|
+
"same",
|
|
518
|
+
"same"
|
|
519
|
+
])
|
|
520
|
+
});
|
|
521
|
+
/** The largest side of a curve grid: 2 to the power 26. Thus each index is less than 2 to the power 52, and is exact. */
|
|
522
|
+
const CURVE_MAX_SIDE = 2 ** 26;
|
|
523
|
+
/**
|
|
524
|
+
* This function gives a point of the square `[0, size] x [0, size]` after a symmetry. The cell `(x, y)` of a grid of
|
|
525
|
+
* side `n` goes to a cell of the same grid with `size = n - 1`.
|
|
526
|
+
*/
|
|
527
|
+
function applySymmetry(symmetry, s, t, size = 1) {
|
|
528
|
+
switch (symmetry) {
|
|
529
|
+
case "same": return [s, t];
|
|
530
|
+
case "swap": return [t, s];
|
|
531
|
+
case "back": return [size - t, size - s];
|
|
532
|
+
case "half": return [size - s, size - t];
|
|
533
|
+
case "rise": return [size - t, s];
|
|
534
|
+
case "fall": return [t, size - s];
|
|
535
|
+
case "flip-s": return [size - s, t];
|
|
536
|
+
case "flip-t": return [s, size - t];
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
/** This function gives the symmetry that undoes `symmetry`. Each symmetry undoes itself, but `rise` and `fall` undo each other. */
|
|
540
|
+
function inverseSymmetry(symmetry) {
|
|
541
|
+
return symmetry === "rise" ? "fall" : symmetry === "fall" ? "rise" : symmetry;
|
|
542
|
+
}
|
|
543
|
+
/** This function gives the count of levels of a side. A side that is not a power of 2 from 1 to `CURVE_MAX_SIDE` is a `RangeError`. */
|
|
544
|
+
function levelsOf$1(side) {
|
|
545
|
+
if (!Number.isInteger(side) || side < 1 || side > CURVE_MAX_SIDE || (side & side - 1) !== 0) throw new RangeError(`The side of a curve grid must be a power of 2 from 1 to ${CURVE_MAX_SIDE}, not ${side}.`);
|
|
546
|
+
return Math.round(Math.log2(side));
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* This function gives the cell `[x, y]` at the place `index` along a curve on a grid of `side` cells on each side.
|
|
550
|
+
* The side is a power of 2, and the index is a whole number from 0 to `side * side - 1`. Other values are a `RangeError`.
|
|
551
|
+
* The cost is `O(log side)`.
|
|
552
|
+
*/
|
|
553
|
+
function curveCell(curve, index, side) {
|
|
554
|
+
const levels = levelsOf$1(side);
|
|
555
|
+
if (!Number.isInteger(index) || index < 0 || index >= side * side) throw new RangeError(`The index ${index} is not a cell of a grid of side ${side}.`);
|
|
556
|
+
let x = 0;
|
|
557
|
+
let y = 0;
|
|
558
|
+
let rest = index;
|
|
559
|
+
for (let level = 0, h = 1; level < levels; level += 1, h *= 2) {
|
|
560
|
+
const part = rest % 4;
|
|
561
|
+
rest = Math.floor(rest / 4);
|
|
562
|
+
const pattern = level === levels - 1 ? curve.root : curve.child;
|
|
563
|
+
[x, y] = applySymmetry(pattern[part], x, y, h - 1);
|
|
564
|
+
const [qx, qy] = curve.order[part];
|
|
565
|
+
x += qx * h;
|
|
566
|
+
y += qy * h;
|
|
567
|
+
}
|
|
568
|
+
return [x, y];
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* This function gives the place along a curve of the cell `(x, y)` of a grid of `side` cells on each side. It is the inverse
|
|
572
|
+
* of `curveCell`. A side that is not a power of 2, or a cell outside the grid, is a `RangeError`. The cost is `O(log side)`.
|
|
573
|
+
*/
|
|
574
|
+
function curveIndex(curve, x, y, side) {
|
|
575
|
+
const levels = levelsOf$1(side);
|
|
576
|
+
for (const value of [x, y]) if (!Number.isInteger(value) || value < 0 || value >= side) throw new RangeError(`The cell (${x}, ${y}) is not in a grid of side ${side}.`);
|
|
577
|
+
let index = 0;
|
|
578
|
+
let s = x;
|
|
579
|
+
let t = y;
|
|
580
|
+
for (let level = levels - 1, h = side / 2; level >= 0; level -= 1, h /= 2) {
|
|
581
|
+
const qx = s >= h ? 1 : 0;
|
|
582
|
+
const qy = t >= h ? 1 : 0;
|
|
583
|
+
const part = curve.order.findIndex(([ox, oy]) => ox === qx && oy === qy);
|
|
584
|
+
const pattern = level === levels - 1 ? curve.root : curve.child;
|
|
585
|
+
[s, t] = applySymmetry(inverseSymmetry(pattern[part]), s - qx * h, t - qy * h, h - 1);
|
|
586
|
+
index += part * h * h;
|
|
587
|
+
}
|
|
588
|
+
return index;
|
|
589
|
+
}
|
|
590
|
+
/** This function gives the place along the Hilbert curve of the cell `(x, y)` of a grid of side `side`. */
|
|
591
|
+
function hilbertIndex(x, y, side) {
|
|
592
|
+
return curveIndex(HILBERT_CURVE, x, y, side);
|
|
593
|
+
}
|
|
594
|
+
/** This function gives the cell at the place `index` along the Hilbert curve on a grid of side `side`. */
|
|
595
|
+
function hilbertCell(index, side) {
|
|
596
|
+
return curveCell(HILBERT_CURVE, index, side);
|
|
597
|
+
}
|
|
598
|
+
/** This function gives the place along the Moore curve of the cell `(x, y)` of a grid of side `side`. */
|
|
599
|
+
function mooreIndex(x, y, side) {
|
|
600
|
+
return curveIndex(MOORE_CURVE, x, y, side);
|
|
601
|
+
}
|
|
602
|
+
/** This function gives the cell at the place `index` along the Moore curve on a grid of side `side`. */
|
|
603
|
+
function mooreCell(index, side) {
|
|
604
|
+
return curveCell(MOORE_CURVE, index, side);
|
|
605
|
+
}
|
|
606
|
+
/** This function gives the place along the Morton curve of the cell `(x, y)` of a grid of side `side`. */
|
|
607
|
+
function mortonIndex(x, y, side) {
|
|
608
|
+
return curveIndex(MORTON_CURVE, x, y, side);
|
|
609
|
+
}
|
|
610
|
+
/** This function gives the cell at the place `index` along the Morton curve on a grid of side `side`. */
|
|
611
|
+
function mortonCell(index, side) {
|
|
612
|
+
return curveCell(MORTON_CURVE, index, side);
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* This function gives each cell of a grid of side `side` in the order of a curve. The cost is `O(side^2 log side)`.
|
|
616
|
+
* A side that is not a power of 2 is a `RangeError`.
|
|
617
|
+
*/
|
|
618
|
+
function curveCells(curve, side) {
|
|
619
|
+
levelsOf$1(side);
|
|
620
|
+
return Array.from({ length: side * side }, (_, index) => curveCell(curve, index, side));
|
|
621
|
+
}
|
|
329
622
|
// src/shapes.ts
|
|
330
623
|
/**
|
|
331
624
|
* The declared shape kinds, in their order. Only `rect` has all of its geometry.
|
|
@@ -707,6 +1000,15 @@ function sizeFits(box, w, h) {
|
|
|
707
1000
|
if (w < b.minW - .001 || w > b.maxW + .001 || h < b.minH - .001 || h > b.maxH + .001) return false;
|
|
708
1001
|
return b.aspect === "free" || Math.abs(w * box.h - h * box.w) <= .001 * (box.w + box.h);
|
|
709
1002
|
}
|
|
1003
|
+
/**
|
|
1004
|
+
* This function tells if a placement can give a box the shape `shape` at the size `w` by `h`.
|
|
1005
|
+
* The box needs `resize.reshape`, and the bounding box of the shape stays inside the rectangle from 0, 0 to `w`, `h`, with the tolerance `RECORD_EPS`.
|
|
1006
|
+
*/
|
|
1007
|
+
function shapeFits(box, shape, w, h) {
|
|
1008
|
+
if (box.resize?.reshape !== true) return false;
|
|
1009
|
+
const b = boundsOf(shape);
|
|
1010
|
+
return b.x >= -.001 && b.y >= -.001 && b.x + b.w <= w + .001 && b.y + b.h <= h + .001;
|
|
1011
|
+
}
|
|
710
1012
|
/** This function gives a port of a box of the size `w` by `h` after a resize to `toW` by `toH`. A port with `along` keeps its value. */
|
|
711
1013
|
function resizePort(port, w, h, toW, toH) {
|
|
712
1014
|
return {
|
|
@@ -732,7 +1034,7 @@ function resizeBox(box, w, h) {
|
|
|
732
1034
|
/**
|
|
733
1035
|
* This function gives the problems of the `resize` of a box. A box without `resize` has no problem.
|
|
734
1036
|
* Only a `rect` box without a shape can have a resize. Each bound is a positive finite number.
|
|
735
|
-
* The size of the box is in its bounds,
|
|
1037
|
+
* The size of the box is in its bounds, `aspect` is `keep` or `free`, and `reshape` is `true` or `false`.
|
|
736
1038
|
*/
|
|
737
1039
|
function resizeProblems(box) {
|
|
738
1040
|
const r = box.resize;
|
|
@@ -746,6 +1048,7 @@ function resizeProblems(box) {
|
|
|
746
1048
|
if (v !== void 0 && !(typeof v === "number" && v > 0 && Number.isFinite(v))) problems.push(`${at}: the resize bound ${k} ${String(v)} is not positive and finite`);
|
|
747
1049
|
}
|
|
748
1050
|
if (r.aspect !== void 0 && r.aspect !== "keep" && r.aspect !== "free") problems.push(`${at}: the resize aspect ${String(r.aspect)} is not keep or free`);
|
|
1051
|
+
if (r.reshape !== void 0 && typeof r.reshape !== "boolean") problems.push(`${at}: the resize reshape ${String(r.reshape)} is not true or false`);
|
|
749
1052
|
const b = sizeBoundsOf(box);
|
|
750
1053
|
if (!(b.minW <= box.w && box.w <= b.maxW)) problems.push(`${at}: the width ${box.w} is not in the resize bounds ${b.minW} to ${b.maxW}`);
|
|
751
1054
|
if (!(b.minH <= box.h && box.h <= b.maxH)) problems.push(`${at}: the height ${box.h} is not in the resize bounds ${b.minH} to ${b.maxH}`);
|
|
@@ -846,12 +1149,21 @@ function turnBox(box) {
|
|
|
846
1149
|
};
|
|
847
1150
|
}
|
|
848
1151
|
// src/output.ts
|
|
1152
|
+
/** This function gives a box with the shape `shape` and without its `shapeKind`. */
|
|
1153
|
+
function reshaped(box, shape) {
|
|
1154
|
+
const { shapeKind: _kind, ...rest } = box;
|
|
1155
|
+
return {
|
|
1156
|
+
...rest,
|
|
1157
|
+
shape
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
849
1160
|
/**
|
|
850
1161
|
* This function gives the input with the output of a packer applied. Each placed box gets its position.
|
|
851
1162
|
* A turned box gets its turned size, ports, margin and shape. Each moved obstacle moves.
|
|
852
1163
|
* The container gets the size of the output. The router reads this result. It does not read `turn`.
|
|
853
1164
|
*
|
|
854
1165
|
* A placement with `w` or `h` gives the box that size after the turn (`resizeBox`).
|
|
1166
|
+
* A placement with `shape` gives the box that shape. The box loses its `shapeKind`, so `shapeKindOf` reads the kind of the new shape.
|
|
855
1167
|
*/
|
|
856
1168
|
function applyPack(input, output) {
|
|
857
1169
|
const at = new Map(output.placements.map((p) => [p.id, p]));
|
|
@@ -860,8 +1172,9 @@ function applyPack(input, output) {
|
|
|
860
1172
|
const p = at.get(box.id);
|
|
861
1173
|
if (p === void 0) return box;
|
|
862
1174
|
const turned = p.turn === 90 ? turnBox(box) : box;
|
|
1175
|
+
const sized = p.w === void 0 && p.h === void 0 ? turned : resizeBox(turned, p.w ?? turned.w, p.h ?? turned.h);
|
|
863
1176
|
return {
|
|
864
|
-
...p.
|
|
1177
|
+
...p.shape === void 0 ? sized : reshaped(sized, p.shape),
|
|
865
1178
|
x: p.x,
|
|
866
1179
|
y: p.y
|
|
867
1180
|
};
|
|
@@ -1107,8 +1420,10 @@ function placedRect(box) {
|
|
|
1107
1420
|
*
|
|
1108
1421
|
* - Version 1 counts each shape by its bounding box. On a circle, that count is an upper bound. A count over 0 proves nothing.
|
|
1109
1422
|
* - Version 2 counts two circles, and a circle in a circle container, by the distance of the centers. The other pairs stay on the bounding box.
|
|
1423
|
+
* - Version 3 counts a pair with a convex polygon by the separating axis theorem. The other box is a rectangle or a convex polygon, and neither box has a margin.
|
|
1424
|
+
* A convex polygon without a margin in a convex container counts by its corners. The counts of the other inputs do not change.
|
|
1110
1425
|
*/
|
|
1111
|
-
const HARD_RULE_VERSION =
|
|
1426
|
+
const HARD_RULE_VERSION = 3;
|
|
1112
1427
|
/** This function gives the circle of a placed box. A box without a position, or with another shape, gives `undefined`. */
|
|
1113
1428
|
function placedCircle(box) {
|
|
1114
1429
|
if (box.shape?.kind !== "circle" || box.x === void 0 || box.y === void 0) return void 0;
|
|
@@ -1118,6 +1433,25 @@ function placedCircle(box) {
|
|
|
1118
1433
|
r: box.shape.r
|
|
1119
1434
|
};
|
|
1120
1435
|
}
|
|
1436
|
+
/**
|
|
1437
|
+
* This function gives the corners of a placed box on the board, when its outline is convex and it has no margin. Each other box gives `undefined`.
|
|
1438
|
+
* A box without a shape is its rectangle. A box with a shape gives `convexCorners` of the shape at the position of the box.
|
|
1439
|
+
*/
|
|
1440
|
+
function placedConvex(box) {
|
|
1441
|
+
if (box.x === void 0 || box.y === void 0) return void 0;
|
|
1442
|
+
const m = marginSides(box.margin);
|
|
1443
|
+
if (m.top !== 0 || m.right !== 0 || m.bottom !== 0 || m.left !== 0) return void 0;
|
|
1444
|
+
const corners = convexCorners(box.shape ?? {
|
|
1445
|
+
kind: "rect",
|
|
1446
|
+
x: 0,
|
|
1447
|
+
y: 0,
|
|
1448
|
+
w: box.w,
|
|
1449
|
+
h: box.h
|
|
1450
|
+
});
|
|
1451
|
+
if (corners === void 0) return void 0;
|
|
1452
|
+
const { x, y } = box;
|
|
1453
|
+
return corners.map(([cx, cy]) => [cx + x, cy + y]);
|
|
1454
|
+
}
|
|
1121
1455
|
/** This function gives the largest side of a margin. A circle has one distance to the things near it, so its margin is this side. */
|
|
1122
1456
|
function marginReach(margin) {
|
|
1123
1457
|
const m = marginSides(margin);
|
|
@@ -1135,6 +1469,7 @@ function nearerThan(a, b, need) {
|
|
|
1135
1469
|
*
|
|
1136
1470
|
* - Two circles break the rule when their centers are nearer than the two radii and the two margins, less `tolerance`. The margin of a circle is the largest side of its margin.
|
|
1137
1471
|
* - Another pair breaks the rule when the bounding boxes with their margins overlap by more than `tolerance` on each axis. With a shape that is not a rectangle, this test is an upper bound.
|
|
1472
|
+
* - A pair with a convex polygon and no margin also needs an overlap over `tolerance` on the normal of each side (`convexOverlap`). Thus that pair is exact.
|
|
1138
1473
|
* - With `gap`, the gap replaces the two margins. Two circles are then apart by the two radii and the gap. For another pair, one box grows by the gap.
|
|
1139
1474
|
* - A box without a position does not overlap.
|
|
1140
1475
|
*/
|
|
@@ -1146,7 +1481,18 @@ function shapesOverlap(a, b, tolerance = DEFAULT_TOLERANCE.overlap, gap) {
|
|
|
1146
1481
|
const rb = placedRect(b);
|
|
1147
1482
|
if (ra === void 0 || rb === void 0) return false;
|
|
1148
1483
|
const o = gap === void 0 ? overlapOf(grow(ra, a.margin), grow(rb, b.margin)) : overlapOf(grow(ra, gap), rb);
|
|
1149
|
-
|
|
1484
|
+
if (!(o.x > tolerance && o.y > tolerance)) return false;
|
|
1485
|
+
return gap !== void 0 || !convexApart(a, b, tolerance);
|
|
1486
|
+
}
|
|
1487
|
+
/**
|
|
1488
|
+
* This function tells if the separating axis theorem puts two placed boxes apart, for `no-overlap` of version 3.
|
|
1489
|
+
* Only a pair with a box of a `polygon` shape is tested, and both boxes need `placedConvex`. Each other pair gives `false`, so its count does not change.
|
|
1490
|
+
*/
|
|
1491
|
+
function convexApart(a, b, tolerance) {
|
|
1492
|
+
if (a.shape?.kind !== "polygon" && b.shape?.kind !== "polygon") return false;
|
|
1493
|
+
const ca = placedConvex(a);
|
|
1494
|
+
const cb = placedConvex(b);
|
|
1495
|
+
return ca !== void 0 && cb !== void 0 && convexOverlap(ca, cb) <= tolerance;
|
|
1150
1496
|
}
|
|
1151
1497
|
/** This function gives the gap of each pair of `pairGaps` by `pairKey`. The first entry of a pair wins. A pair of one box has no gap. */
|
|
1152
1498
|
function gapsOf(pairGaps) {
|
|
@@ -1160,6 +1506,7 @@ function gapsOf(pairGaps) {
|
|
|
1160
1506
|
/**
|
|
1161
1507
|
* This function counts the pairs of placed boxes that break `no-overlap`. `shapesOverlap` is the rule for one pair.
|
|
1162
1508
|
* Two circles count by the distance of their centers. Each other pair counts by the bounding boxes, so a circle beside another shape is an upper bound (roadmap item L107).
|
|
1509
|
+
* A pair with a convex polygon and no margin also needs an overlap on the normal of each side, so it is exact (version 3).
|
|
1163
1510
|
*
|
|
1164
1511
|
* A pair of `pairGaps`, in either order, uses its `gap` in place of the two margins. One box grows by the gap, and the other box does not grow.
|
|
1165
1512
|
* Thus the pair breaks the rule only when the boxes are nearer than the gap on each axis. The other pairs do not change.
|
|
@@ -1224,12 +1571,16 @@ function circleOverlaps(boxes, tolerance = DEFAULT_TOLERANCE.overlap, pairGaps,
|
|
|
1224
1571
|
}
|
|
1225
1572
|
return count;
|
|
1226
1573
|
}
|
|
1227
|
-
/**
|
|
1574
|
+
/**
|
|
1575
|
+
* This function counts the overlaps by the bounding boxes with their margins. It skips the pairs of `skip` and the pairs of two circles.
|
|
1576
|
+
* A pair that `convexApart` puts apart does not count.
|
|
1577
|
+
*/
|
|
1228
1578
|
function marginOverlaps(boxes, tolerance, skip) {
|
|
1229
1579
|
const rects = [];
|
|
1230
1580
|
for (const box of boxes) {
|
|
1231
1581
|
const r = placedRect(box);
|
|
1232
1582
|
if (r !== void 0) rects.push({
|
|
1583
|
+
box,
|
|
1233
1584
|
id: box.id,
|
|
1234
1585
|
r: grow(r, box.margin),
|
|
1235
1586
|
circle: box.shape?.kind === "circle"
|
|
@@ -1245,7 +1596,7 @@ function marginOverlaps(boxes, tolerance, skip) {
|
|
|
1245
1596
|
if (rects[i].circle && rects[j].circle) continue;
|
|
1246
1597
|
if (skip !== void 0 && skip.has(pairKey(rects[i].id, rects[j].id))) continue;
|
|
1247
1598
|
const o = overlapOf(a, b);
|
|
1248
|
-
if (o.x > tolerance && o.y > tolerance) count++;
|
|
1599
|
+
if (o.x > tolerance && o.y > tolerance && !convexApart(rects[i].box, rects[j].box, tolerance)) count++;
|
|
1249
1600
|
}
|
|
1250
1601
|
}
|
|
1251
1602
|
return count;
|
|
@@ -1401,8 +1752,10 @@ function countLinksThroughShapes(boxes, links, tolerance = DEFAULT_TOLERANCE.thr
|
|
|
1401
1752
|
*
|
|
1402
1753
|
* With `exactContainer`, the default, a box of a shaped container with the size `fixed` also counts when it is not inside the shape, by `rectInRegion`.
|
|
1403
1754
|
* For that test, the box grows by `inset` and becomes smaller by `tolerance` on each side. A shaped container with a free side keeps the bounding test only.
|
|
1404
|
-
* A circle in a circle container does not use `rectInRegion`. It counts by distance, as in `circlesOutside`, also when the size is free.
|
|
1405
1755
|
* An input without a container gives 0.
|
|
1756
|
+
*
|
|
1757
|
+
* A circle in a circle container does not use `rectInRegion`. It counts by distance, as in `circlesOutside`, also when the size is free.
|
|
1758
|
+
* A box of a `polygon` shape with `placedConvex` in a convex shape does not use `rectInRegion`. It counts by its corners (`cornersInConvex`), so the count is exact (version 3).
|
|
1406
1759
|
*/
|
|
1407
1760
|
function countOutside(input, tolerance = DEFAULT_TOLERANCE.overlap, options = {}) {
|
|
1408
1761
|
const c = input.container;
|
|
@@ -1430,10 +1783,17 @@ function countOutside(input, tolerance = DEFAULT_TOLERANCE.overlap, options = {}
|
|
|
1430
1783
|
if (fixW && outX || fixH && outY) count++;
|
|
1431
1784
|
else if (ring !== void 0 && disc !== void 0) {
|
|
1432
1785
|
if (circleOutside(disc, marginReach(box.margin) + inset, ring, tolerance)) count++;
|
|
1433
|
-
} else if (shape !== void 0 && !
|
|
1786
|
+
} else if (shape !== void 0 && !inShape(box, shape, r, inset, tolerance)) count++;
|
|
1434
1787
|
}
|
|
1435
1788
|
return count;
|
|
1436
1789
|
}
|
|
1790
|
+
/** This function tells if a placed box is inside a container shape for `countOutside`. A convex polygon box in a convex shape counts by its corners. */
|
|
1791
|
+
function inShape(box, shape, r, inset, tolerance) {
|
|
1792
|
+
const inner = box.shape?.kind === "polygon" ? placedConvex(box) : void 0;
|
|
1793
|
+
const outer = inner === void 0 ? void 0 : convexCorners(shape);
|
|
1794
|
+
if (inner !== void 0 && outer !== void 0) return cornersInConvex(inner, outer, inset, tolerance);
|
|
1795
|
+
return rectInRegion(shape, shrink(grow(r, inset), tolerance));
|
|
1796
|
+
}
|
|
1437
1797
|
/** This function counts the fixed ports that are not on the border of their box, with the epsilon of a written file. A port without a side does not count. */
|
|
1438
1798
|
function countPortsOffBorder(boxes, eps = RECORD_EPS) {
|
|
1439
1799
|
let count = 0;
|
|
@@ -1456,6 +1816,9 @@ function countPortsOffBorder(boxes, eps = RECORD_EPS) {
|
|
|
1456
1816
|
* `no-overlap` and `in-container` are exact for a `rect` and a `circle`. `limit` tells the inputs where the count of a circle is only safe (roadmap item L107).
|
|
1457
1817
|
* For the other kinds, these rules test the bounding box. The count is safe: a shape is inside its bounding box, so the count is an upper bound. A count of 0 tells that the rule has no break.
|
|
1458
1818
|
* `locked-kept` and `complete` read only the positions and the ids, so they support each kind.
|
|
1819
|
+
*
|
|
1820
|
+
* A convex `polygon` without a margin is exact beside a rectangle or a convex polygon, and in a convex container (version 3).
|
|
1821
|
+
* The kind `polygon` stays `safe`, because a concave polygon is not exact.
|
|
1459
1822
|
*/
|
|
1460
1823
|
const PACK_HARD_RULES = /* @__PURE__ */ Object.freeze([
|
|
1461
1824
|
{
|
|
@@ -1630,19 +1993,22 @@ function countObstacleHits(boxes, obstacles, tolerance = DEFAULT_TOLERANCE.overl
|
|
|
1630
1993
|
return count;
|
|
1631
1994
|
}
|
|
1632
1995
|
/**
|
|
1633
|
-
* This function
|
|
1996
|
+
* This function counts the placements with a size outside the bounds of their box (`sizeFits`), or with a shape that does not fit (`shapeFits`).
|
|
1634
1997
|
* The bounds are in the axes of the placed box: a turned box has the turned bounds. A placement without `w` or `h` keeps that size of the box.
|
|
1998
|
+
* A placement with a shape on a box without `resize.reshape` counts. A placement counts one time, also when its size and its shape both break.
|
|
1635
1999
|
* A placement of an id that is not in the input does not count.
|
|
1636
2000
|
*/
|
|
1637
2001
|
function countResizeBreaks(input, output) {
|
|
1638
2002
|
const was = new Map(input.boxes.map((b) => [b.id, b]));
|
|
1639
2003
|
let count = 0;
|
|
1640
2004
|
for (const p of output.placements) {
|
|
1641
|
-
if (p.w === void 0 && p.h === void 0) continue;
|
|
2005
|
+
if (p.w === void 0 && p.h === void 0 && p.shape === void 0) continue;
|
|
1642
2006
|
const box = was.get(p.id);
|
|
1643
2007
|
if (box === void 0) continue;
|
|
1644
2008
|
const placed = p.turn === 90 ? turnBox(box) : box;
|
|
1645
|
-
|
|
2009
|
+
const w = p.w ?? placed.w;
|
|
2010
|
+
const h = p.h ?? placed.h;
|
|
2011
|
+
if (!(p.w === void 0 && p.h === void 0 || sizeFits(placed, w, h)) || p.shape !== void 0 && !shapeFits(placed, p.shape, w, h)) count++;
|
|
1646
2012
|
}
|
|
1647
2013
|
return count;
|
|
1648
2014
|
}
|
|
@@ -1654,7 +2020,7 @@ function countResizeBreaks(input, output) {
|
|
|
1654
2020
|
* A locked box without a placement, or with a move of more than `RECORD_EPS`, breaks `locked-kept`.
|
|
1655
2021
|
* A locked box without a position in the input is a bad input, not a break. `checkInput` finds it.
|
|
1656
2022
|
* Another box without a placement that is not in `overflow` breaks `complete`.
|
|
1657
|
-
* `size-kept` is in the result only when a box has `resize` or a placement has `w` or `
|
|
2023
|
+
* `size-kept` is in the result only when a box has `resize` or a placement has `w`, `h` or `shape` (`countResizeBreaks`).
|
|
1658
2024
|
*
|
|
1659
2025
|
* `options` goes to `countOutside` for `in-container`.
|
|
1660
2026
|
*/
|
|
@@ -1687,9 +2053,9 @@ function packHard(input, output, tolerance = DEFAULT_TOLERANCE.overlap, options
|
|
|
1687
2053
|
...resizes(input, output) ? { "size-kept": countResizeBreaks(input, output) } : {}
|
|
1688
2054
|
};
|
|
1689
2055
|
}
|
|
1690
|
-
/** This function tells if a box of the input has `resize` or a placement of the output has `w` or `
|
|
2056
|
+
/** This function tells if a box of the input has `resize` or a placement of the output has `w`, `h` or `shape`. */
|
|
1691
2057
|
function resizes(input, output) {
|
|
1692
|
-
return input.boxes.some((b) => b.resize !== void 0) || output.placements.some((p) => p.w !== void 0 || p.h !== void 0);
|
|
2058
|
+
return input.boxes.some((b) => b.resize !== void 0) || output.placements.some((p) => p.w !== void 0 || p.h !== void 0 || p.shape !== void 0);
|
|
1693
2059
|
}
|
|
1694
2060
|
/**
|
|
1695
2061
|
* This function gives the problems of an input file. An empty list tells that the file is good.
|
|
@@ -1763,7 +2129,7 @@ const JOINT_MEASURES = /* @__PURE__ */ Object.freeze([
|
|
|
1763
2129
|
{
|
|
1764
2130
|
id: "joint.fill",
|
|
1765
2131
|
minEffect: .001,
|
|
1766
|
-
unit: "
|
|
2132
|
+
unit: "share",
|
|
1767
2133
|
direction: "higher",
|
|
1768
2134
|
min: 0,
|
|
1769
2135
|
max: 1,
|
|
@@ -1786,7 +2152,7 @@ const JOINT_MEASURES = /* @__PURE__ */ Object.freeze([
|
|
|
1786
2152
|
{
|
|
1787
2153
|
id: "joint.spill",
|
|
1788
2154
|
minEffect: .001,
|
|
1789
|
-
unit: "
|
|
2155
|
+
unit: "share",
|
|
1790
2156
|
direction: "lower",
|
|
1791
2157
|
min: 0,
|
|
1792
2158
|
max: 1,
|
|
@@ -1834,7 +2200,7 @@ const JOINT_MEASURES = /* @__PURE__ */ Object.freeze([
|
|
|
1834
2200
|
{
|
|
1835
2201
|
id: "joint.stability.moved",
|
|
1836
2202
|
minEffect: .02,
|
|
1837
|
-
unit: "
|
|
2203
|
+
unit: "share",
|
|
1838
2204
|
direction: "lower",
|
|
1839
2205
|
min: 0,
|
|
1840
2206
|
max: 1,
|
|
@@ -1845,7 +2211,7 @@ const JOINT_MEASURES = /* @__PURE__ */ Object.freeze([
|
|
|
1845
2211
|
},
|
|
1846
2212
|
{
|
|
1847
2213
|
id: "joint.proxy.rank",
|
|
1848
|
-
unit: "
|
|
2214
|
+
unit: "no unit",
|
|
1849
2215
|
direction: "higher",
|
|
1850
2216
|
min: -1,
|
|
1851
2217
|
max: 1,
|
|
@@ -1914,6 +2280,7 @@ const BALANCE_SIDE_DEPTH = .25;
|
|
|
1914
2280
|
/** The suffixes of the balance and stability measures, in the order of `docs/BALANCE.md`. */
|
|
1915
2281
|
const BALANCE_MEASURE_SUFFIXES = /* @__PURE__ */ Object.freeze([
|
|
1916
2282
|
"balance.gapCv",
|
|
2283
|
+
"balance.gapMean",
|
|
1917
2284
|
"balance.centroidOffset",
|
|
1918
2285
|
"balance.side.top",
|
|
1919
2286
|
"balance.side.right",
|
|
@@ -2034,6 +2401,7 @@ function balanceOf(boxes, container) {
|
|
|
2034
2401
|
const values = SIDES.map((s) => side[s]);
|
|
2035
2402
|
return {
|
|
2036
2403
|
gapCv,
|
|
2404
|
+
gapMean: mean,
|
|
2037
2405
|
centroidOffset,
|
|
2038
2406
|
side,
|
|
2039
2407
|
sideRange: Math.max(...values) - Math.min(...values)
|
|
@@ -2091,6 +2459,7 @@ function perChange(move, changed, count) {
|
|
|
2091
2459
|
function balanceMeasures(stage, balance) {
|
|
2092
2460
|
const m = {
|
|
2093
2461
|
[`${stage}.balance.gapCv`]: balance.gapCv,
|
|
2462
|
+
[`${stage}.balance.gapMean`]: balance.gapMean,
|
|
2094
2463
|
[`${stage}.balance.centroidOffset`]: balance.centroidOffset,
|
|
2095
2464
|
[`${stage}.balance.sideRange`]: balance.sideRange
|
|
2096
2465
|
};
|
|
@@ -2100,6 +2469,7 @@ function balanceMeasures(stage, balance) {
|
|
|
2100
2469
|
/**
|
|
2101
2470
|
* This function gives the specs of the balance and stability measures, with the prefix of the stage.
|
|
2102
2471
|
* The balance measures read the area of each box, so they support only `rect`. The stability measures read only the centers.
|
|
2472
|
+
* `balance.gapMean` has no better side. An objective gives it a target, so its role is `diagnostic`, and the joint selector does not rank by it.
|
|
2103
2473
|
*/
|
|
2104
2474
|
function balanceMeasureSpecs(stage) {
|
|
2105
2475
|
const id = (suffix) => `${stage}.${suffix}`;
|
|
@@ -2116,9 +2486,19 @@ function balanceMeasureSpecs(stage) {
|
|
|
2116
2486
|
family: "balance",
|
|
2117
2487
|
shapes: rect
|
|
2118
2488
|
},
|
|
2489
|
+
{
|
|
2490
|
+
id: id("balance.gapMean"),
|
|
2491
|
+
unit: "layout units",
|
|
2492
|
+
direction: "higher",
|
|
2493
|
+
min: 0,
|
|
2494
|
+
max: Infinity,
|
|
2495
|
+
family: "balance",
|
|
2496
|
+
role: "diagnostic",
|
|
2497
|
+
shapes: rect
|
|
2498
|
+
},
|
|
2119
2499
|
{
|
|
2120
2500
|
id: id("balance.centroidOffset"),
|
|
2121
|
-
unit: "
|
|
2501
|
+
unit: "share",
|
|
2122
2502
|
direction: "lower",
|
|
2123
2503
|
min: 0,
|
|
2124
2504
|
max: 1,
|
|
@@ -2128,7 +2508,7 @@ function balanceMeasureSpecs(stage) {
|
|
|
2128
2508
|
},
|
|
2129
2509
|
...SIDES.map((s) => ({
|
|
2130
2510
|
id: id(`balance.side.${s}`),
|
|
2131
|
-
unit: "
|
|
2511
|
+
unit: "share",
|
|
2132
2512
|
direction: "lower",
|
|
2133
2513
|
min: 0,
|
|
2134
2514
|
max: 1,
|
|
@@ -2138,7 +2518,7 @@ function balanceMeasureSpecs(stage) {
|
|
|
2138
2518
|
})),
|
|
2139
2519
|
{
|
|
2140
2520
|
id: id("balance.sideRange"),
|
|
2141
|
-
unit: "
|
|
2521
|
+
unit: "share",
|
|
2142
2522
|
direction: "lower",
|
|
2143
2523
|
min: 0,
|
|
2144
2524
|
max: 1,
|
|
@@ -2173,6 +2553,273 @@ function balanceMeasureSpecs(stage) {
|
|
|
2173
2553
|
}
|
|
2174
2554
|
]);
|
|
2175
2555
|
}
|
|
2556
|
+
// src/measure-registry.ts
|
|
2557
|
+
/** The ids of the noise kinds, in the order of `NoiseKind`. */
|
|
2558
|
+
const NOISE_KINDS = /* @__PURE__ */ Object.freeze([
|
|
2559
|
+
"exact",
|
|
2560
|
+
"sampled",
|
|
2561
|
+
"timed"
|
|
2562
|
+
]);
|
|
2563
|
+
/**
|
|
2564
|
+
* The one unit word of a value from 0 to 1: a share, or a flag of 0 or 1 that a record averages.
|
|
2565
|
+
* `checkRegistry` refuses a unit text that names a range, for example `0 to 1`, `0 or 1` or `-1 to 1`. The range goes in `min` and `max`.
|
|
2566
|
+
*/
|
|
2567
|
+
const SHARE_UNIT = "share";
|
|
2568
|
+
/** The unit word of a measure with no other unit and a range that is not 0 to 1. An example is a correlation from -1 to 1. */
|
|
2569
|
+
const NO_UNIT = "no unit";
|
|
2570
|
+
/** A unit text that names a range, for example `0 to 1` or `-0.5 to 1`. */
|
|
2571
|
+
const RANGE_UNIT = /^-?\d+(?:\.\d+)? (?:to|or) -?\d+(?:\.\d+)?$/;
|
|
2572
|
+
/** The other words of a share. */
|
|
2573
|
+
const SHARE_WORDS = /* @__PURE__ */ new Set([
|
|
2574
|
+
"boolean",
|
|
2575
|
+
"fraction",
|
|
2576
|
+
"proportion"
|
|
2577
|
+
]);
|
|
2578
|
+
/** This function gives the fault of a unit text, or `undefined` for a good unit. */
|
|
2579
|
+
function unitFault(spec) {
|
|
2580
|
+
const u = spec.unit;
|
|
2581
|
+
if (typeof u !== "string" || u.length === 0) return "the unit is empty";
|
|
2582
|
+
if (RANGE_UNIT.test(u)) return `the unit ${JSON.stringify(u)} names a range: use ${JSON.stringify(SHARE_UNIT)} for the range 0 to 1, else ${JSON.stringify(NO_UNIT)}, with the range in min and max`;
|
|
2583
|
+
if (SHARE_WORDS.has(u)) return `the unit ${JSON.stringify(u)} is a share: use ${JSON.stringify(SHARE_UNIT)}`;
|
|
2584
|
+
if (u === "share" && !(spec.min === 0 && spec.max === 1)) return `the unit ${JSON.stringify(SHARE_UNIT)} needs the range 0 to 1`;
|
|
2585
|
+
}
|
|
2586
|
+
/** The pattern of a `CostOrder`: terms of `1`, `n`, `e` or `p`, each with an optional `^2`, `^3` or `log`, joined by ` + `. */
|
|
2587
|
+
const COST_TERM = "(?:1|[nep](?:\\^[23])?(?: log [nep])?)";
|
|
2588
|
+
const COST_ORDER = /* @__PURE__ */ new RegExp(`^${COST_TERM}(?: \\+ ${COST_TERM})*$`);
|
|
2589
|
+
/** The reason of the default rule `none`. */
|
|
2590
|
+
const NO_FINITE_RANGE = "The range is not finite, and the registry has no agreed scale for the measure.";
|
|
2591
|
+
/** This function gives the default normalization of a spec: `range` when `min` and `max` are finite, else `none` with the reason `NO_FINITE_RANGE`. */
|
|
2592
|
+
function defaultNormalize(spec) {
|
|
2593
|
+
return Number.isFinite(spec.min) && Number.isFinite(spec.max) ? { kind: "range" } : {
|
|
2594
|
+
kind: "none",
|
|
2595
|
+
reason: NO_FINITE_RANGE
|
|
2596
|
+
};
|
|
2597
|
+
}
|
|
2598
|
+
/** This function makes a registry entry from a spec and the added facts. The noise is `exact` by default. */
|
|
2599
|
+
function measureEntry(spec, facts) {
|
|
2600
|
+
return Object.freeze({
|
|
2601
|
+
...spec,
|
|
2602
|
+
source: facts.source,
|
|
2603
|
+
noise: facts.noise ?? "exact",
|
|
2604
|
+
normalize: facts.normalize ?? defaultNormalize(spec),
|
|
2605
|
+
...facts.cost === void 0 ? {} : { cost: facts.cost },
|
|
2606
|
+
...facts.planned === void 0 ? {} : { planned: facts.planned }
|
|
2607
|
+
});
|
|
2608
|
+
}
|
|
2609
|
+
/**
|
|
2610
|
+
* This function gives the loss of the value `value` of a measure: a number from 0 to 1, where 0 is the best value.
|
|
2611
|
+
* A value outside the range of the spec gives a `RangeError`. A value `NaN` also gives a `RangeError`.
|
|
2612
|
+
* The rule `none` gives `undefined`: the measure has no agreed scale.
|
|
2613
|
+
* A planned measure gives an `Error`: no code computes it, so it has no value.
|
|
2614
|
+
*/
|
|
2615
|
+
function normalizeMeasure(value, entry) {
|
|
2616
|
+
if (entry.planned !== void 0) throw new Error(`normalizeMeasure: ${entry.id} is planned, and no code computes it: ${entry.planned}`);
|
|
2617
|
+
if (Number.isNaN(value) || value < entry.min || value > entry.max) throw new RangeError(`normalizeMeasure: the value ${value} of ${entry.id} is not in [${entry.min}, ${entry.max}].`);
|
|
2618
|
+
const rule = entry.normalize;
|
|
2619
|
+
if (rule.kind === "none") return void 0;
|
|
2620
|
+
let worse;
|
|
2621
|
+
if (rule.kind === "range") {
|
|
2622
|
+
const t = entry.max === entry.min ? 0 : (value - entry.min) / (entry.max - entry.min);
|
|
2623
|
+
worse = entry.direction === "lower" ? t : 1 - t;
|
|
2624
|
+
} else {
|
|
2625
|
+
const d = value - entry.min;
|
|
2626
|
+
if (d === Infinity) worse = entry.direction === "lower" ? 1 : 0;
|
|
2627
|
+
else worse = entry.direction === "lower" ? d / (d + rule.half) : rule.half / (d + rule.half);
|
|
2628
|
+
}
|
|
2629
|
+
return worse === 0 ? 0 : worse;
|
|
2630
|
+
}
|
|
2631
|
+
/**
|
|
2632
|
+
* This function gives the faults of a list of registry entries, as plain text. An empty list tells that the entries are good.
|
|
2633
|
+
* It examines the unique ids, the unit, the range, the floor, the noise, the normalization, the cost, the source and the planned reason.
|
|
2634
|
+
*/
|
|
2635
|
+
function checkRegistry(entries) {
|
|
2636
|
+
const out = [];
|
|
2637
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2638
|
+
for (const e of entries) {
|
|
2639
|
+
const at = `measure ${JSON.stringify(e.id)}`;
|
|
2640
|
+
if (typeof e.id !== "string" || e.id.length === 0) out.push(`${at}: the id is empty`);
|
|
2641
|
+
if (seen.has(e.id)) out.push(`${at}: the id is not unique`);
|
|
2642
|
+
seen.add(e.id);
|
|
2643
|
+
const unit = unitFault(e);
|
|
2644
|
+
if (unit !== void 0) out.push(`${at}: ${unit}`);
|
|
2645
|
+
if (e.direction !== "lower" && e.direction !== "higher") out.push(`${at}: the direction is not lower or higher`);
|
|
2646
|
+
if (typeof e.min !== "number" || Number.isNaN(e.min) || e.min === Infinity) out.push(`${at}: min is not a number below Infinity`);
|
|
2647
|
+
if (!(e.max > e.min)) out.push(`${at}: max is not above min`);
|
|
2648
|
+
if (e.floor !== void 0 && !(e.floor >= e.min && e.floor <= e.max)) out.push(`${at}: the floor is not in the range`);
|
|
2649
|
+
if (typeof e.source !== "string" || e.source.length === 0) out.push(`${at}: the source is empty`);
|
|
2650
|
+
if (!NOISE_KINDS.includes(e.noise)) out.push(`${at}: the noise is not one of ${NOISE_KINDS.join(", ")}`);
|
|
2651
|
+
if (e.cost !== void 0 && !COST_ORDER.test(e.cost)) out.push(`${at}: the cost ${JSON.stringify(e.cost)} is not an order of growth`);
|
|
2652
|
+
if (e.planned !== void 0 && !(typeof e.planned === "string" && e.planned.length > 0)) out.push(`${at}: the planned reason is empty`);
|
|
2653
|
+
if (e.planned !== void 0 && e.cost !== void 0) out.push(`${at}: a planned measure has no code, so it has no cost`);
|
|
2654
|
+
const n = e.normalize;
|
|
2655
|
+
if (n === void 0 || n.kind !== "range" && n.kind !== "scale" && n.kind !== "none") out.push(`${at}: the normalization is not range, scale or none`);
|
|
2656
|
+
else if (n.kind === "range" && !(Number.isFinite(e.min) && Number.isFinite(e.max))) out.push(`${at}: the normalization range needs a finite min and max`);
|
|
2657
|
+
else if (n.kind === "scale" && !Number.isFinite(e.min)) out.push(`${at}: the normalization scale needs a finite min`);
|
|
2658
|
+
else if (n.kind === "scale" && !(Number.isFinite(n.half) && n.half > 0)) out.push(`${at}: the half of the normalization scale is not above 0`);
|
|
2659
|
+
else if (n.kind === "scale" && !(typeof n.reason === "string" && n.reason.length > 0)) out.push(`${at}: the normalization scale has no reason`);
|
|
2660
|
+
else if (n.kind === "none" && !(typeof n.reason === "string" && n.reason.length > 0)) out.push(`${at}: the normalization none has no reason`);
|
|
2661
|
+
}
|
|
2662
|
+
return out;
|
|
2663
|
+
}
|
|
2664
|
+
/** The package of the joint bench. It is private, so the joint measures have no npm version. */
|
|
2665
|
+
const JOINT_SOURCE = "@layout-contract/joint";
|
|
2666
|
+
/** The planned reason of the two joint stability measures. */
|
|
2667
|
+
const NO_CHANGE_PAIRS = "The joint bench has no pairs of a base input and a changed input, so no code computes the measure.";
|
|
2668
|
+
const JOINT_FACTS = {
|
|
2669
|
+
"joint.fill": { cost: "n + p" },
|
|
2670
|
+
"joint.aspectError": {
|
|
2671
|
+
cost: "n + p",
|
|
2672
|
+
normalize: {
|
|
2673
|
+
kind: "scale",
|
|
2674
|
+
half: Math.LN2,
|
|
2675
|
+
reason: "An aspect at a factor of 2 from the target has the loss 0.5."
|
|
2676
|
+
}
|
|
2677
|
+
},
|
|
2678
|
+
"joint.spill": { cost: "n + p" },
|
|
2679
|
+
"joint.straight.crossings": {
|
|
2680
|
+
cost: "n + e^2",
|
|
2681
|
+
normalize: {
|
|
2682
|
+
kind: "none",
|
|
2683
|
+
reason: "A count of crossings has no agreed scale."
|
|
2684
|
+
}
|
|
2685
|
+
},
|
|
2686
|
+
"joint.straight.length": {
|
|
2687
|
+
cost: "n + e",
|
|
2688
|
+
normalize: {
|
|
2689
|
+
kind: "scale",
|
|
2690
|
+
half: 1,
|
|
2691
|
+
reason: "An edge as long as the side S of the square of the box area has the loss 0.5."
|
|
2692
|
+
}
|
|
2693
|
+
},
|
|
2694
|
+
"joint.stability.mean": { planned: NO_CHANGE_PAIRS },
|
|
2695
|
+
"joint.stability.moved": { planned: NO_CHANGE_PAIRS },
|
|
2696
|
+
"joint.proxy.rank": { cost: "n + p + e log e" },
|
|
2697
|
+
"joint.route.length": {
|
|
2698
|
+
cost: "p",
|
|
2699
|
+
normalize: {
|
|
2700
|
+
kind: "none",
|
|
2701
|
+
reason: "A length in layout units has no agreed scale."
|
|
2702
|
+
}
|
|
2703
|
+
},
|
|
2704
|
+
"joint.ms": {
|
|
2705
|
+
noise: "timed",
|
|
2706
|
+
normalize: {
|
|
2707
|
+
kind: "none",
|
|
2708
|
+
reason: "A time of the machine has no agreed scale, and each read gives another value."
|
|
2709
|
+
}
|
|
2710
|
+
}
|
|
2711
|
+
};
|
|
2712
|
+
/**
|
|
2713
|
+
* The registry entries of the joint measures, in the order of `JOINT_MEASURES`. `docs/MEASURE-REGISTRY.md` gives the reason of each cost and scale.
|
|
2714
|
+
* The engines keep the specs of their own measures. `docs/measure-survey.json` lists them, and an engine adopts the registry with `measureEntry`.
|
|
2715
|
+
*/
|
|
2716
|
+
const MEASURE_REGISTRY = /* @__PURE__ */ Object.freeze(JOINT_MEASURES.map((spec) => measureEntry(spec, {
|
|
2717
|
+
source: JOINT_SOURCE,
|
|
2718
|
+
...JOINT_FACTS[spec.id]
|
|
2719
|
+
})));
|
|
2720
|
+
// src/cell.ts
|
|
2721
|
+
/** The suffixes of the cell measures, in the order of `cellMeasureSpecs`. */
|
|
2722
|
+
const CELL_MEASURE_SUFFIXES = /* @__PURE__ */ Object.freeze(["cell.areaError", "cell.compactness"]);
|
|
2723
|
+
/** The compactness of a square: 4 over PI. */
|
|
2724
|
+
const SQUARE_COMPACTNESS = 4 / Math.PI;
|
|
2725
|
+
/**
|
|
2726
|
+
* This function gives the cell measures of the placed items in a container of the area `containerArea`.
|
|
2727
|
+
*
|
|
2728
|
+
* - The cell share of an item is the area of its cell over `containerArea`. The target share is its `target` over the sum of the targets.
|
|
2729
|
+
* - `containerArea` is the area of the container region. For a container with a shape, give `regionArea` of the shape, not `w` by `h`.
|
|
2730
|
+
* - `areaError` is half the sum of the absolute differences of the two shares. Cells that fill the container in the target shares give 0.
|
|
2731
|
+
* Cells that overlap or go out of the container can give a sum over 1. Those cells break the hard rules, and the value stops at 1.
|
|
2732
|
+
* - `compactness` is the mean of `P * P / (4 * PI * A)` over the cells, for the perimeter `P` and the area `A` of each cell.
|
|
2733
|
+
* A circle gives 1, and a square gives 4 over PI. A cell with the area 0 gives `Infinity`.
|
|
2734
|
+
* - No items give the best values: an `areaError` of 0 and a `compactness` of 1. The hard rule `complete` counts the items that are not placed.
|
|
2735
|
+
*
|
|
2736
|
+
* A target that is not a finite number above 0, and a container area that is not a finite number above 0, give a `RangeError`.
|
|
2737
|
+
*/
|
|
2738
|
+
function cellsOf(items, containerArea) {
|
|
2739
|
+
if (!(Number.isFinite(containerArea) && containerArea > 0)) throw new RangeError(`cellsOf: the container area is a finite number above 0, not ${containerArea}.`);
|
|
2740
|
+
if (items.length === 0) return {
|
|
2741
|
+
areaError: 0,
|
|
2742
|
+
compactness: 1
|
|
2743
|
+
};
|
|
2744
|
+
let targets = 0;
|
|
2745
|
+
for (const item of items) {
|
|
2746
|
+
if (!(Number.isFinite(item.target) && item.target > 0)) throw new RangeError(`cellsOf: a target is a finite number above 0, not ${item.target}.`);
|
|
2747
|
+
targets += item.target;
|
|
2748
|
+
}
|
|
2749
|
+
let error = 0;
|
|
2750
|
+
let compact = 0;
|
|
2751
|
+
for (const item of items) {
|
|
2752
|
+
const area = regionArea(item.cell);
|
|
2753
|
+
error += Math.abs(area / containerArea - item.target / targets);
|
|
2754
|
+
const p = regionPerimeter(item.cell);
|
|
2755
|
+
compact += area > 0 ? p * p / (4 * Math.PI * area) : Infinity;
|
|
2756
|
+
}
|
|
2757
|
+
return {
|
|
2758
|
+
areaError: Math.min(1, error / 2),
|
|
2759
|
+
compactness: compact / items.length
|
|
2760
|
+
};
|
|
2761
|
+
}
|
|
2762
|
+
/** This function gives the cell values as measures, with the prefix of the stage, for example `pack.cell.areaError`. */
|
|
2763
|
+
function cellMeasures(stage, cells) {
|
|
2764
|
+
return {
|
|
2765
|
+
[`${stage}.cell.areaError`]: cells.areaError,
|
|
2766
|
+
[`${stage}.cell.compactness`]: cells.compactness
|
|
2767
|
+
};
|
|
2768
|
+
}
|
|
2769
|
+
/**
|
|
2770
|
+
* This function gives the specs of the cell measures, with the prefix of the stage. The family is `cell`.
|
|
2771
|
+
* The measures read the exact area and perimeter of each cell, so they support `rect`, `polygon` and `circle`. A `free` box has only its bounding box, so it is not supported.
|
|
2772
|
+
*/
|
|
2773
|
+
function cellMeasureSpecs(stage) {
|
|
2774
|
+
const shapes = [
|
|
2775
|
+
"rect",
|
|
2776
|
+
"polygon",
|
|
2777
|
+
"circle"
|
|
2778
|
+
];
|
|
2779
|
+
return Object.freeze([{
|
|
2780
|
+
id: `${stage}.cell.areaError`,
|
|
2781
|
+
unit: "share",
|
|
2782
|
+
direction: "lower",
|
|
2783
|
+
min: 0,
|
|
2784
|
+
max: 1,
|
|
2785
|
+
floor: 0,
|
|
2786
|
+
tol: 1e-9,
|
|
2787
|
+
minEffect: .01,
|
|
2788
|
+
family: "cell",
|
|
2789
|
+
shapes
|
|
2790
|
+
}, {
|
|
2791
|
+
id: `${stage}.cell.compactness`,
|
|
2792
|
+
unit: "ratio",
|
|
2793
|
+
direction: "lower",
|
|
2794
|
+
min: 1,
|
|
2795
|
+
max: Infinity,
|
|
2796
|
+
minEffect: .01,
|
|
2797
|
+
family: "cell",
|
|
2798
|
+
shapes
|
|
2799
|
+
}]);
|
|
2800
|
+
}
|
|
2801
|
+
/** The npm name of the package that computes the cell measures. */
|
|
2802
|
+
const CELL_SOURCE = "@mark1russell7/layout-contract";
|
|
2803
|
+
/**
|
|
2804
|
+
* This function gives the registry entries of the cell measures, with the prefix of the stage.
|
|
2805
|
+
* `areaError` uses the rule `range`. `compactness` uses the rule `scale`: a square has the loss 0.5.
|
|
2806
|
+
* The cost of each is `n`. Each makes one pass over the corners of the cells. The cells of a Voronoi diagram have a count of corners in the order of `n`.
|
|
2807
|
+
*/
|
|
2808
|
+
function cellMeasureEntries(stage) {
|
|
2809
|
+
const [area, compact] = cellMeasureSpecs(stage);
|
|
2810
|
+
return Object.freeze([measureEntry(area, {
|
|
2811
|
+
source: CELL_SOURCE,
|
|
2812
|
+
cost: "n"
|
|
2813
|
+
}), measureEntry(compact, {
|
|
2814
|
+
source: CELL_SOURCE,
|
|
2815
|
+
cost: "n",
|
|
2816
|
+
normalize: {
|
|
2817
|
+
kind: "scale",
|
|
2818
|
+
half: SQUARE_COMPACTNESS - 1,
|
|
2819
|
+
reason: "A square cell has the loss 0.5, and a circle has the loss 0."
|
|
2820
|
+
}
|
|
2821
|
+
})]);
|
|
2822
|
+
}
|
|
2176
2823
|
// src/frame.ts
|
|
2177
2824
|
/** The schema of a frame. */
|
|
2178
2825
|
const FRAME_SCHEMA = "layout-frame/1";
|
|
@@ -2683,7 +3330,7 @@ function byId(a, b) {
|
|
|
2683
3330
|
* Thus two packers with the same result in a different order have the same form.
|
|
2684
3331
|
* The function throws a `RangeError` when `placements`, `overflow` or `moved` has an id two times.
|
|
2685
3332
|
*
|
|
2686
|
-
* A placement has `w` and `
|
|
3333
|
+
* A placement has `w`, `h` and `shape` only when the packer set them, so the form of an output without a resize does not change.
|
|
2687
3334
|
*/
|
|
2688
3335
|
function packOutputForm(output) {
|
|
2689
3336
|
uniqueIds("placements", output.placements.map((p) => p.id));
|
|
@@ -2696,7 +3343,8 @@ function packOutputForm(output) {
|
|
|
2696
3343
|
y: p.y,
|
|
2697
3344
|
turn: p.turn ?? 0,
|
|
2698
3345
|
...p.w === void 0 ? {} : { w: p.w },
|
|
2699
|
-
...p.h === void 0 ? {} : { h: p.h }
|
|
3346
|
+
...p.h === void 0 ? {} : { h: p.h },
|
|
3347
|
+
...p.shape === void 0 ? {} : { shape: p.shape }
|
|
2700
3348
|
})).sort(byId),
|
|
2701
3349
|
overflow: [...output.overflow].sort(),
|
|
2702
3350
|
size: {
|
|
@@ -5385,6 +6033,118 @@ const RECT_SCATTERED = /* @__PURE__ */ Object.freeze({
|
|
|
5385
6033
|
};
|
|
5386
6034
|
}
|
|
5387
6035
|
});
|
|
5388
|
-
|
|
6036
|
+
// src/measure-survey.ts
|
|
6037
|
+
/** The schema of a measure survey. */
|
|
6038
|
+
const MEASURE_SURVEY_SCHEMA = "measure-survey/1";
|
|
6039
|
+
const DATE = /^\d{4}-\d{2}-\d{2}$/;
|
|
6040
|
+
const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
6041
|
+
/** This function gives the faults of a survey, as plain text. An empty list tells that the survey is good. */
|
|
6042
|
+
function checkSurvey(v) {
|
|
6043
|
+
const out = [];
|
|
6044
|
+
if (!isObj(v)) return ["the survey is not an object"];
|
|
6045
|
+
if (v["schema"] !== "measure-survey/1") out.push(`the schema is not ${MEASURE_SURVEY_SCHEMA}`);
|
|
6046
|
+
if (typeof v["date"] !== "string" || !DATE.test(v["date"])) out.push("the date is not YYYY-MM-DD");
|
|
6047
|
+
const sources = v["sources"];
|
|
6048
|
+
if (!Array.isArray(sources) || sources.length === 0) return [...out, "the survey has no sources"];
|
|
6049
|
+
const names = /* @__PURE__ */ new Set();
|
|
6050
|
+
for (const [i, s] of sources.entries()) {
|
|
6051
|
+
const at = `sources[${i}]`;
|
|
6052
|
+
if (!isObj(s)) {
|
|
6053
|
+
out.push(`${at} is not an object`);
|
|
6054
|
+
continue;
|
|
6055
|
+
}
|
|
6056
|
+
if (typeof s["package"] !== "string" || s["package"].length === 0) out.push(`${at}.package is empty`);
|
|
6057
|
+
else if (names.has(s["package"])) out.push(`${at}.package ${s["package"]} is not unique`);
|
|
6058
|
+
else names.add(s["package"]);
|
|
6059
|
+
if (typeof s["version"] !== "string" || s["version"].length === 0) out.push(`${at}.version is empty`);
|
|
6060
|
+
const measures = s["measures"];
|
|
6061
|
+
if (!Array.isArray(measures)) {
|
|
6062
|
+
out.push(`${at}.measures is not a list`);
|
|
6063
|
+
continue;
|
|
6064
|
+
}
|
|
6065
|
+
const ids = /* @__PURE__ */ new Set();
|
|
6066
|
+
for (const [j, m] of measures.entries()) {
|
|
6067
|
+
const mat = `${at}.measures[${j}]`;
|
|
6068
|
+
if (!isObj(m)) {
|
|
6069
|
+
out.push(`${mat} is not an object`);
|
|
6070
|
+
continue;
|
|
6071
|
+
}
|
|
6072
|
+
if (typeof m["id"] !== "string" || m["id"].length === 0) out.push(`${mat}.id is empty`);
|
|
6073
|
+
else if (ids.has(m["id"])) out.push(`${mat}.id ${m["id"]} is not unique in its source`);
|
|
6074
|
+
else ids.add(m["id"]);
|
|
6075
|
+
if (typeof m["unit"] !== "string" || m["unit"].length === 0) out.push(`${mat}.unit is empty`);
|
|
6076
|
+
if (m["direction"] !== "lower" && m["direction"] !== "higher") out.push(`${mat}.direction is not lower or higher`);
|
|
6077
|
+
const min = m["min"];
|
|
6078
|
+
const max = m["max"];
|
|
6079
|
+
if (min === void 0 !== (max === void 0)) out.push(`${mat} has min or max, not both`);
|
|
6080
|
+
if (min !== void 0 && (typeof min !== "number" || !Number.isFinite(min))) out.push(`${mat}.min is not a finite number`);
|
|
6081
|
+
if (max !== void 0 && max !== null && (typeof max !== "number" || !Number.isFinite(max) || !(typeof min === "number" && max > min))) out.push(`${mat}.max is not a number above min, or null`);
|
|
6082
|
+
}
|
|
6083
|
+
}
|
|
6084
|
+
return out;
|
|
6085
|
+
}
|
|
6086
|
+
/** This function gives the name of an id after its stage: the id without its first part. An id without a dot is its own name. */
|
|
6087
|
+
function nameAfterStage(id) {
|
|
6088
|
+
const dot = id.indexOf(".");
|
|
6089
|
+
return dot < 0 ? id : id.slice(dot + 1);
|
|
6090
|
+
}
|
|
6091
|
+
/** This function gives the findings of a good survey. `checkSurvey` examines the survey first. */
|
|
6092
|
+
function surveyFindings(survey) {
|
|
6093
|
+
const bySource = {};
|
|
6094
|
+
const byId = /* @__PURE__ */ new Map();
|
|
6095
|
+
const units = {};
|
|
6096
|
+
const noRange = /* @__PURE__ */ new Set();
|
|
6097
|
+
for (const s of survey.sources) {
|
|
6098
|
+
bySource[s.package] = s.measures.length;
|
|
6099
|
+
for (const m of s.measures) {
|
|
6100
|
+
const list = byId.get(m.id) ?? [];
|
|
6101
|
+
list.push({
|
|
6102
|
+
source: s.package,
|
|
6103
|
+
m
|
|
6104
|
+
});
|
|
6105
|
+
byId.set(m.id, list);
|
|
6106
|
+
units[m.unit] = (units[m.unit] ?? 0) + 1;
|
|
6107
|
+
if (m.min === void 0) noRange.add(m.id);
|
|
6108
|
+
}
|
|
6109
|
+
}
|
|
6110
|
+
const conflicts = [];
|
|
6111
|
+
for (const [id, list] of byId) for (const field of ["unit", "direction"]) if (new Set(list.map((e) => e.m[field])).size > 1) conflicts.push({
|
|
6112
|
+
id,
|
|
6113
|
+
field,
|
|
6114
|
+
sources: list.map((e) => e.source)
|
|
6115
|
+
});
|
|
6116
|
+
const byName = /* @__PURE__ */ new Map();
|
|
6117
|
+
for (const [id, list] of byId) {
|
|
6118
|
+
const name = nameAfterStage(id);
|
|
6119
|
+
const group = byName.get(name) ?? [];
|
|
6120
|
+
for (const e of list) group.push({
|
|
6121
|
+
id,
|
|
6122
|
+
...e
|
|
6123
|
+
});
|
|
6124
|
+
byName.set(name, group);
|
|
6125
|
+
}
|
|
6126
|
+
const twins = [];
|
|
6127
|
+
for (const [name, group] of [...byName].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) {
|
|
6128
|
+
if (new Set(group.map((e) => e.source)).size < 2) continue;
|
|
6129
|
+
for (const field of ["unit", "direction"]) if (new Set(group.map((e) => e.m[field])).size > 1) twins.push({
|
|
6130
|
+
name,
|
|
6131
|
+
field,
|
|
6132
|
+
values: group.map((e) => ({
|
|
6133
|
+
id: e.id,
|
|
6134
|
+
source: e.source,
|
|
6135
|
+
value: e.m[field]
|
|
6136
|
+
}))
|
|
6137
|
+
});
|
|
6138
|
+
}
|
|
6139
|
+
return {
|
|
6140
|
+
ids: byId.size,
|
|
6141
|
+
bySource,
|
|
6142
|
+
conflicts,
|
|
6143
|
+
twins,
|
|
6144
|
+
noRange: [...noRange].sort(),
|
|
6145
|
+
units
|
|
6146
|
+
};
|
|
6147
|
+
}
|
|
6148
|
+
export { BALANCE_MEASURE_SUFFIXES, BALANCE_SIDE_DEPTH, CELL_MEASURE_SUFFIXES, CELL_SOURCE, CIRCLE_SCATTERED, CORPUS_DIMENSIONS, CORPUS_MANIFEST_SCHEMA, CURVE_MAX_SIDE, CURVE_SYMMETRIES, DEFAULT_FAMILY_BY, DEFAULT_RNG, DEFAULT_SHAPES, DEFAULT_TOLERANCE, EDGES_STEP, EDGE_DENSITY, EDGE_KEY_STEP, EDGE_KEY_STEP_VERSION, EDGE_STEP_VERSION, EPS, EPS_STEPS, FAMILY_ANY_KIND, FRAME_SCHEMA, FRAME_STATES, HARD_RULE_VERSION, HILBERT_CURVE, HILBERT_PATTERN, INVALIDATED_FIELD, JOINT_MEASURES, JOINT_MOVED_MIN, JOINT_SOURCE, MEASURE_REGISTRY, MEASURE_SURVEY_SCHEMA, MOORE_CURVE, MOORE_PATTERN, MORTON_CURVE, NODE_POSITIONS_DIMENSION, NOISE_KINDS, NO_FINITE_RANGE, NO_UNIT, OUTPUT_CLASS_MIN_INPUTS, OUTPUT_GRID_PROPERTIES, OUTPUT_NEAR_WHOLE, PACK_HARD_RULES, RECORD_EPS, RECORD_SCALE, RECT_SCATTERED, REFUSED_PROBE, SHAPE_KINDS, SHAPE_KIND_IDS, SHARE_UNIT, SIDES, SPLIT_SCHEMA, SQUARE_COMPACTNESS, STRATEGY_CATALOG_SCHEMA, STRATEGY_CATALOG_SCHEMAS, STRATEGY_SPACE_SCHEMA, VERDICT_DRAWS, VERDICT_EXACT_CAP, VERDICT_EXACT_MAX, VERDICT_RUNS, VERDICT_SEED, applyPack, applySymmetry, balanceMeasureSpecs, balanceMeasures, balanceOf, boundsOf, boxArea, boxCenterDistance, canonicalConfig, canonicalInput, canonicalJson, carryRecord, catalogChoices, catalogDefaults, catalogKey, catalogOptions, cellMeasureEntries, cellMeasureSpecs, cellMeasures, cellsOf, checkCatalog, checkConfig, checkDeclared, checkInput, checkLayoutRecord, checkOptions, checkRegistry, checkSchema, checkShapes, checkSpace, checkSplit, checkSurvey, circleOverlaps, circlesOutside, clampOptions, clipRing, compareMeasure, composeFamily, configId, convexCorners, convexOverlap, cornersInConvex, corpusHashOf, corpusManifestText, countLinksThroughShapes, countObstacleHits, countOutside, countOverlaps, countPortsOffBorder, countResizeBreaks, createFamilyRegistry, crossingPoint, curveCell, curveCells, curveIndex, declarationIssues, declaredClaimOf, declaredCounts, decodeOptionValue, defaultNormalize, dimensionAt, dimensionSupports, dimensionUnit, documentHash, dropIndex, edgeKeyStep, edgeStep, encodeOptionValue, enumerateSpace, exercised, familyKind, familyOf, fingerprintOf, fixedPointMove, fnv1a32, frameAt, frameMove, frameOf, grow, hardRuleOf, hilbertCell, hilbertIndex, inputShapeLevel, inverseSymmetry, isShapeKind, itemShapeOf, joinConfigs, leaveOneSeedOut, levelsOf, lineJson, listOptions, manifestText, marginSides, measureEntry, minEffectOf, mooreCell, mooreIndex, mortonCell, mortonIndex, mulberry32, nameAfterStage, nearestGaps, normalizeMeasure, optionDefaults, optionIssue, outputGridClass, outputGroups, outputHash, outputOf, overlapOf, ownerDimension, packHard, packLinks, packOutputForm, paramActive, paramAt, paramLevels, paramProblem, paramUnit, paramValue, parseLayoutRecord, parseStrategyCatalog, perChange, pickMoves, placedCircle, placedConvex, placedRect, pointInRing, pointInRings, pointSegmentDistance, portPoint, portfolio, probeHash, procrustesOf, productSpace, randomInt, rectInRegion, regionArea, regionHitsRect, regionPerimeter, resizeBox, resizeProblems, resolveDimensions, ringCentroid, ruleLevel, scaleRect, segmentThroughShape, segmentsCross, selectBest, sha256Hex, shapeFits, shapeKindOf, shapeKindProblem, shapeKindsOf, shapeLevel, shapeSupportProblems, shapesOf, shapesOverlap, sharedDimension, sizeBoundsOf, sizeFits, snapRect, spaceShapeKinds, spaceSize, splitConfig, splitFileName, splitFolds, stepRandom, surveyFindings, sweepGrid, sweepOf, sweepValues, toLayoutRecord, toPackInput, toRect, toSizedRect, translate, turnBox, turnMargin, turnPoint, turnPort, turnRegion, turnResize, turnSide, unitFault, verdict, writeStrategyCatalog };
|
|
5389
6149
|
|
|
5390
6150
|
//# sourceMappingURL=index.js.map
|