@mark1russell7/layout-contract 0.9.9 → 0.10.1
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 +320 -11
- package/build/index.d.ts.map +1 -1
- package/build/index.js +799 -38
- 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 +2 -2
- package/src/balance.ts +11 -4
- package/src/cell.ts +107 -0
- package/src/curves.ts +203 -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 +194 -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/strategy-catalog.ts +4 -4
- 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,179 @@ 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 = /* @__PURE__ */ 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 = /* @__PURE__ */ Object.freeze([
|
|
466
|
+
/* @__PURE__ */ Object.freeze([0, 0]),
|
|
467
|
+
/* @__PURE__ */ Object.freeze([0, 1]),
|
|
468
|
+
/* @__PURE__ */ Object.freeze([1, 1]),
|
|
469
|
+
/* @__PURE__ */ 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 = /* @__PURE__ */ 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 = /* @__PURE__ */ 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 = /* @__PURE__ */ 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 = /* @__PURE__ */ 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 = /* @__PURE__ */ Object.freeze({
|
|
501
|
+
name: "morton",
|
|
502
|
+
order: /* @__PURE__ */ Object.freeze([
|
|
503
|
+
/* @__PURE__ */ Object.freeze([0, 0]),
|
|
504
|
+
/* @__PURE__ */ Object.freeze([1, 0]),
|
|
505
|
+
/* @__PURE__ */ Object.freeze([0, 1]),
|
|
506
|
+
/* @__PURE__ */ Object.freeze([1, 1])
|
|
507
|
+
]),
|
|
508
|
+
root: /* @__PURE__ */ Object.freeze([
|
|
509
|
+
"same",
|
|
510
|
+
"same",
|
|
511
|
+
"same",
|
|
512
|
+
"same"
|
|
513
|
+
]),
|
|
514
|
+
child: /* @__PURE__ */ Object.freeze([
|
|
515
|
+
"same",
|
|
516
|
+
"same",
|
|
517
|
+
"same",
|
|
518
|
+
"same"
|
|
519
|
+
])
|
|
520
|
+
});
|
|
521
|
+
/**
|
|
522
|
+
* 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. The value is a literal,
|
|
523
|
+
* because a bundler keeps a top-level `2 ** 26`, also when no code uses it.
|
|
524
|
+
*/
|
|
525
|
+
const CURVE_MAX_SIDE = 67108864;
|
|
526
|
+
/**
|
|
527
|
+
* This function gives a point of the square `[0, size] x [0, size]` after a symmetry. The cell `(x, y)` of a grid of
|
|
528
|
+
* side `n` goes to a cell of the same grid with `size = n - 1`.
|
|
529
|
+
*/
|
|
530
|
+
function applySymmetry(symmetry, s, t, size = 1) {
|
|
531
|
+
switch (symmetry) {
|
|
532
|
+
case "same": return [s, t];
|
|
533
|
+
case "swap": return [t, s];
|
|
534
|
+
case "back": return [size - t, size - s];
|
|
535
|
+
case "half": return [size - s, size - t];
|
|
536
|
+
case "rise": return [size - t, s];
|
|
537
|
+
case "fall": return [t, size - s];
|
|
538
|
+
case "flip-s": return [size - s, t];
|
|
539
|
+
case "flip-t": return [s, size - t];
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
/** This function gives the symmetry that undoes `symmetry`. Each symmetry undoes itself, but `rise` and `fall` undo each other. */
|
|
543
|
+
function inverseSymmetry(symmetry) {
|
|
544
|
+
return symmetry === "rise" ? "fall" : symmetry === "fall" ? "rise" : symmetry;
|
|
545
|
+
}
|
|
546
|
+
/** 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`. */
|
|
547
|
+
function levelsOf$1(side) {
|
|
548
|
+
if (!Number.isInteger(side) || side < 1 || side > 67108864 || (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}.`);
|
|
549
|
+
return Math.round(Math.log2(side));
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* This function gives the cell `[x, y]` at the place `index` along a curve on a grid of `side` cells on each side.
|
|
553
|
+
* 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`.
|
|
554
|
+
* The cost is `O(log side)`.
|
|
555
|
+
*/
|
|
556
|
+
function curveCell(curve, index, side) {
|
|
557
|
+
const levels = levelsOf$1(side);
|
|
558
|
+
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}.`);
|
|
559
|
+
let x = 0;
|
|
560
|
+
let y = 0;
|
|
561
|
+
let rest = index;
|
|
562
|
+
for (let level = 0, h = 1; level < levels; level += 1, h *= 2) {
|
|
563
|
+
const part = rest % 4;
|
|
564
|
+
rest = Math.floor(rest / 4);
|
|
565
|
+
const pattern = level === levels - 1 ? curve.root : curve.child;
|
|
566
|
+
[x, y] = applySymmetry(pattern[part], x, y, h - 1);
|
|
567
|
+
const [qx, qy] = curve.order[part];
|
|
568
|
+
x += qx * h;
|
|
569
|
+
y += qy * h;
|
|
570
|
+
}
|
|
571
|
+
return [x, y];
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* 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
|
|
575
|
+
* 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)`.
|
|
576
|
+
*/
|
|
577
|
+
function curveIndex(curve, x, y, side) {
|
|
578
|
+
const levels = levelsOf$1(side);
|
|
579
|
+
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}.`);
|
|
580
|
+
let index = 0;
|
|
581
|
+
let s = x;
|
|
582
|
+
let t = y;
|
|
583
|
+
for (let level = levels - 1, h = side / 2; level >= 0; level -= 1, h /= 2) {
|
|
584
|
+
const qx = s >= h ? 1 : 0;
|
|
585
|
+
const qy = t >= h ? 1 : 0;
|
|
586
|
+
const part = curve.order.findIndex(([ox, oy]) => ox === qx && oy === qy);
|
|
587
|
+
const pattern = level === levels - 1 ? curve.root : curve.child;
|
|
588
|
+
[s, t] = applySymmetry(inverseSymmetry(pattern[part]), s - qx * h, t - qy * h, h - 1);
|
|
589
|
+
index += part * h * h;
|
|
590
|
+
}
|
|
591
|
+
return index;
|
|
592
|
+
}
|
|
593
|
+
/** This function gives the place along the Hilbert curve of the cell `(x, y)` of a grid of side `side`. */
|
|
594
|
+
function hilbertIndex(x, y, side) {
|
|
595
|
+
return curveIndex(HILBERT_CURVE, x, y, side);
|
|
596
|
+
}
|
|
597
|
+
/** This function gives the cell at the place `index` along the Hilbert curve on a grid of side `side`. */
|
|
598
|
+
function hilbertCell(index, side) {
|
|
599
|
+
return curveCell(HILBERT_CURVE, index, side);
|
|
600
|
+
}
|
|
601
|
+
/** This function gives the place along the Moore curve of the cell `(x, y)` of a grid of side `side`. */
|
|
602
|
+
function mooreIndex(x, y, side) {
|
|
603
|
+
return curveIndex(MOORE_CURVE, x, y, side);
|
|
604
|
+
}
|
|
605
|
+
/** This function gives the cell at the place `index` along the Moore curve on a grid of side `side`. */
|
|
606
|
+
function mooreCell(index, side) {
|
|
607
|
+
return curveCell(MOORE_CURVE, index, side);
|
|
608
|
+
}
|
|
609
|
+
/** This function gives the place along the Morton curve of the cell `(x, y)` of a grid of side `side`. */
|
|
610
|
+
function mortonIndex(x, y, side) {
|
|
611
|
+
return curveIndex(MORTON_CURVE, x, y, side);
|
|
612
|
+
}
|
|
613
|
+
/** This function gives the cell at the place `index` along the Morton curve on a grid of side `side`. */
|
|
614
|
+
function mortonCell(index, side) {
|
|
615
|
+
return curveCell(MORTON_CURVE, index, side);
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* 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)`.
|
|
619
|
+
* A side that is not a power of 2 is a `RangeError`.
|
|
620
|
+
*/
|
|
621
|
+
function curveCells(curve, side) {
|
|
622
|
+
levelsOf$1(side);
|
|
623
|
+
return Array.from({ length: side * side }, (_, index) => curveCell(curve, index, side));
|
|
624
|
+
}
|
|
329
625
|
// src/shapes.ts
|
|
330
626
|
/**
|
|
331
627
|
* The declared shape kinds, in their order. Only `rect` has all of its geometry.
|
|
@@ -707,6 +1003,15 @@ function sizeFits(box, w, h) {
|
|
|
707
1003
|
if (w < b.minW - .001 || w > b.maxW + .001 || h < b.minH - .001 || h > b.maxH + .001) return false;
|
|
708
1004
|
return b.aspect === "free" || Math.abs(w * box.h - h * box.w) <= .001 * (box.w + box.h);
|
|
709
1005
|
}
|
|
1006
|
+
/**
|
|
1007
|
+
* This function tells if a placement can give a box the shape `shape` at the size `w` by `h`.
|
|
1008
|
+
* 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`.
|
|
1009
|
+
*/
|
|
1010
|
+
function shapeFits(box, shape, w, h) {
|
|
1011
|
+
if (box.resize?.reshape !== true) return false;
|
|
1012
|
+
const b = boundsOf(shape);
|
|
1013
|
+
return b.x >= -.001 && b.y >= -.001 && b.x + b.w <= w + .001 && b.y + b.h <= h + .001;
|
|
1014
|
+
}
|
|
710
1015
|
/** 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
1016
|
function resizePort(port, w, h, toW, toH) {
|
|
712
1017
|
return {
|
|
@@ -732,7 +1037,7 @@ function resizeBox(box, w, h) {
|
|
|
732
1037
|
/**
|
|
733
1038
|
* This function gives the problems of the `resize` of a box. A box without `resize` has no problem.
|
|
734
1039
|
* 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,
|
|
1040
|
+
* The size of the box is in its bounds, `aspect` is `keep` or `free`, and `reshape` is `true` or `false`.
|
|
736
1041
|
*/
|
|
737
1042
|
function resizeProblems(box) {
|
|
738
1043
|
const r = box.resize;
|
|
@@ -746,6 +1051,7 @@ function resizeProblems(box) {
|
|
|
746
1051
|
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
1052
|
}
|
|
748
1053
|
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`);
|
|
1054
|
+
if (r.reshape !== void 0 && typeof r.reshape !== "boolean") problems.push(`${at}: the resize reshape ${String(r.reshape)} is not true or false`);
|
|
749
1055
|
const b = sizeBoundsOf(box);
|
|
750
1056
|
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
1057
|
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 +1152,21 @@ function turnBox(box) {
|
|
|
846
1152
|
};
|
|
847
1153
|
}
|
|
848
1154
|
// src/output.ts
|
|
1155
|
+
/** This function gives a box with the shape `shape` and without its `shapeKind`. */
|
|
1156
|
+
function reshaped(box, shape) {
|
|
1157
|
+
const { shapeKind: _kind, ...rest } = box;
|
|
1158
|
+
return {
|
|
1159
|
+
...rest,
|
|
1160
|
+
shape
|
|
1161
|
+
};
|
|
1162
|
+
}
|
|
849
1163
|
/**
|
|
850
1164
|
* This function gives the input with the output of a packer applied. Each placed box gets its position.
|
|
851
1165
|
* A turned box gets its turned size, ports, margin and shape. Each moved obstacle moves.
|
|
852
1166
|
* The container gets the size of the output. The router reads this result. It does not read `turn`.
|
|
853
1167
|
*
|
|
854
1168
|
* A placement with `w` or `h` gives the box that size after the turn (`resizeBox`).
|
|
1169
|
+
* A placement with `shape` gives the box that shape. The box loses its `shapeKind`, so `shapeKindOf` reads the kind of the new shape.
|
|
855
1170
|
*/
|
|
856
1171
|
function applyPack(input, output) {
|
|
857
1172
|
const at = new Map(output.placements.map((p) => [p.id, p]));
|
|
@@ -860,8 +1175,9 @@ function applyPack(input, output) {
|
|
|
860
1175
|
const p = at.get(box.id);
|
|
861
1176
|
if (p === void 0) return box;
|
|
862
1177
|
const turned = p.turn === 90 ? turnBox(box) : box;
|
|
1178
|
+
const sized = p.w === void 0 && p.h === void 0 ? turned : resizeBox(turned, p.w ?? turned.w, p.h ?? turned.h);
|
|
863
1179
|
return {
|
|
864
|
-
...p.
|
|
1180
|
+
...p.shape === void 0 ? sized : reshaped(sized, p.shape),
|
|
865
1181
|
x: p.x,
|
|
866
1182
|
y: p.y
|
|
867
1183
|
};
|
|
@@ -1107,8 +1423,10 @@ function placedRect(box) {
|
|
|
1107
1423
|
*
|
|
1108
1424
|
* - 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
1425
|
* - 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.
|
|
1426
|
+
* - 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.
|
|
1427
|
+
* A convex polygon without a margin in a convex container counts by its corners. The counts of the other inputs do not change.
|
|
1110
1428
|
*/
|
|
1111
|
-
const HARD_RULE_VERSION =
|
|
1429
|
+
const HARD_RULE_VERSION = 3;
|
|
1112
1430
|
/** This function gives the circle of a placed box. A box without a position, or with another shape, gives `undefined`. */
|
|
1113
1431
|
function placedCircle(box) {
|
|
1114
1432
|
if (box.shape?.kind !== "circle" || box.x === void 0 || box.y === void 0) return void 0;
|
|
@@ -1118,6 +1436,25 @@ function placedCircle(box) {
|
|
|
1118
1436
|
r: box.shape.r
|
|
1119
1437
|
};
|
|
1120
1438
|
}
|
|
1439
|
+
/**
|
|
1440
|
+
* 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`.
|
|
1441
|
+
* A box without a shape is its rectangle. A box with a shape gives `convexCorners` of the shape at the position of the box.
|
|
1442
|
+
*/
|
|
1443
|
+
function placedConvex(box) {
|
|
1444
|
+
if (box.x === void 0 || box.y === void 0) return void 0;
|
|
1445
|
+
const m = marginSides(box.margin);
|
|
1446
|
+
if (m.top !== 0 || m.right !== 0 || m.bottom !== 0 || m.left !== 0) return void 0;
|
|
1447
|
+
const corners = convexCorners(box.shape ?? {
|
|
1448
|
+
kind: "rect",
|
|
1449
|
+
x: 0,
|
|
1450
|
+
y: 0,
|
|
1451
|
+
w: box.w,
|
|
1452
|
+
h: box.h
|
|
1453
|
+
});
|
|
1454
|
+
if (corners === void 0) return void 0;
|
|
1455
|
+
const { x, y } = box;
|
|
1456
|
+
return corners.map(([cx, cy]) => [cx + x, cy + y]);
|
|
1457
|
+
}
|
|
1121
1458
|
/** 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
1459
|
function marginReach(margin) {
|
|
1123
1460
|
const m = marginSides(margin);
|
|
@@ -1135,6 +1472,7 @@ function nearerThan(a, b, need) {
|
|
|
1135
1472
|
*
|
|
1136
1473
|
* - 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
1474
|
* - 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.
|
|
1475
|
+
* - 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
1476
|
* - 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
1477
|
* - A box without a position does not overlap.
|
|
1140
1478
|
*/
|
|
@@ -1146,7 +1484,18 @@ function shapesOverlap(a, b, tolerance = DEFAULT_TOLERANCE.overlap, gap) {
|
|
|
1146
1484
|
const rb = placedRect(b);
|
|
1147
1485
|
if (ra === void 0 || rb === void 0) return false;
|
|
1148
1486
|
const o = gap === void 0 ? overlapOf(grow(ra, a.margin), grow(rb, b.margin)) : overlapOf(grow(ra, gap), rb);
|
|
1149
|
-
|
|
1487
|
+
if (!(o.x > tolerance && o.y > tolerance)) return false;
|
|
1488
|
+
return gap !== void 0 || !convexApart(a, b, tolerance);
|
|
1489
|
+
}
|
|
1490
|
+
/**
|
|
1491
|
+
* This function tells if the separating axis theorem puts two placed boxes apart, for `no-overlap` of version 3.
|
|
1492
|
+
* 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.
|
|
1493
|
+
*/
|
|
1494
|
+
function convexApart(a, b, tolerance) {
|
|
1495
|
+
if (a.shape?.kind !== "polygon" && b.shape?.kind !== "polygon") return false;
|
|
1496
|
+
const ca = placedConvex(a);
|
|
1497
|
+
const cb = placedConvex(b);
|
|
1498
|
+
return ca !== void 0 && cb !== void 0 && convexOverlap(ca, cb) <= tolerance;
|
|
1150
1499
|
}
|
|
1151
1500
|
/** 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
1501
|
function gapsOf(pairGaps) {
|
|
@@ -1160,6 +1509,7 @@ function gapsOf(pairGaps) {
|
|
|
1160
1509
|
/**
|
|
1161
1510
|
* This function counts the pairs of placed boxes that break `no-overlap`. `shapesOverlap` is the rule for one pair.
|
|
1162
1511
|
* 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).
|
|
1512
|
+
* 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
1513
|
*
|
|
1164
1514
|
* 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
1515
|
* 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 +1574,16 @@ function circleOverlaps(boxes, tolerance = DEFAULT_TOLERANCE.overlap, pairGaps,
|
|
|
1224
1574
|
}
|
|
1225
1575
|
return count;
|
|
1226
1576
|
}
|
|
1227
|
-
/**
|
|
1577
|
+
/**
|
|
1578
|
+
* This function counts the overlaps by the bounding boxes with their margins. It skips the pairs of `skip` and the pairs of two circles.
|
|
1579
|
+
* A pair that `convexApart` puts apart does not count.
|
|
1580
|
+
*/
|
|
1228
1581
|
function marginOverlaps(boxes, tolerance, skip) {
|
|
1229
1582
|
const rects = [];
|
|
1230
1583
|
for (const box of boxes) {
|
|
1231
1584
|
const r = placedRect(box);
|
|
1232
1585
|
if (r !== void 0) rects.push({
|
|
1586
|
+
box,
|
|
1233
1587
|
id: box.id,
|
|
1234
1588
|
r: grow(r, box.margin),
|
|
1235
1589
|
circle: box.shape?.kind === "circle"
|
|
@@ -1245,7 +1599,7 @@ function marginOverlaps(boxes, tolerance, skip) {
|
|
|
1245
1599
|
if (rects[i].circle && rects[j].circle) continue;
|
|
1246
1600
|
if (skip !== void 0 && skip.has(pairKey(rects[i].id, rects[j].id))) continue;
|
|
1247
1601
|
const o = overlapOf(a, b);
|
|
1248
|
-
if (o.x > tolerance && o.y > tolerance) count++;
|
|
1602
|
+
if (o.x > tolerance && o.y > tolerance && !convexApart(rects[i].box, rects[j].box, tolerance)) count++;
|
|
1249
1603
|
}
|
|
1250
1604
|
}
|
|
1251
1605
|
return count;
|
|
@@ -1401,8 +1755,10 @@ function countLinksThroughShapes(boxes, links, tolerance = DEFAULT_TOLERANCE.thr
|
|
|
1401
1755
|
*
|
|
1402
1756
|
* 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
1757
|
* 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
1758
|
* An input without a container gives 0.
|
|
1759
|
+
*
|
|
1760
|
+
* A circle in a circle container does not use `rectInRegion`. It counts by distance, as in `circlesOutside`, also when the size is free.
|
|
1761
|
+
* 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
1762
|
*/
|
|
1407
1763
|
function countOutside(input, tolerance = DEFAULT_TOLERANCE.overlap, options = {}) {
|
|
1408
1764
|
const c = input.container;
|
|
@@ -1430,10 +1786,17 @@ function countOutside(input, tolerance = DEFAULT_TOLERANCE.overlap, options = {}
|
|
|
1430
1786
|
if (fixW && outX || fixH && outY) count++;
|
|
1431
1787
|
else if (ring !== void 0 && disc !== void 0) {
|
|
1432
1788
|
if (circleOutside(disc, marginReach(box.margin) + inset, ring, tolerance)) count++;
|
|
1433
|
-
} else if (shape !== void 0 && !
|
|
1789
|
+
} else if (shape !== void 0 && !inShape(box, shape, r, inset, tolerance)) count++;
|
|
1434
1790
|
}
|
|
1435
1791
|
return count;
|
|
1436
1792
|
}
|
|
1793
|
+
/** 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. */
|
|
1794
|
+
function inShape(box, shape, r, inset, tolerance) {
|
|
1795
|
+
const inner = box.shape?.kind === "polygon" ? placedConvex(box) : void 0;
|
|
1796
|
+
const outer = inner === void 0 ? void 0 : convexCorners(shape);
|
|
1797
|
+
if (inner !== void 0 && outer !== void 0) return cornersInConvex(inner, outer, inset, tolerance);
|
|
1798
|
+
return rectInRegion(shape, shrink(grow(r, inset), tolerance));
|
|
1799
|
+
}
|
|
1437
1800
|
/** 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
1801
|
function countPortsOffBorder(boxes, eps = RECORD_EPS) {
|
|
1439
1802
|
let count = 0;
|
|
@@ -1456,6 +1819,9 @@ function countPortsOffBorder(boxes, eps = RECORD_EPS) {
|
|
|
1456
1819
|
* `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
1820
|
* 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
1821
|
* `locked-kept` and `complete` read only the positions and the ids, so they support each kind.
|
|
1822
|
+
*
|
|
1823
|
+
* A convex `polygon` without a margin is exact beside a rectangle or a convex polygon, and in a convex container (version 3).
|
|
1824
|
+
* The kind `polygon` stays `safe`, because a concave polygon is not exact.
|
|
1459
1825
|
*/
|
|
1460
1826
|
const PACK_HARD_RULES = /* @__PURE__ */ Object.freeze([
|
|
1461
1827
|
{
|
|
@@ -1630,19 +1996,22 @@ function countObstacleHits(boxes, obstacles, tolerance = DEFAULT_TOLERANCE.overl
|
|
|
1630
1996
|
return count;
|
|
1631
1997
|
}
|
|
1632
1998
|
/**
|
|
1633
|
-
* This function
|
|
1999
|
+
* 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
2000
|
* 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.
|
|
2001
|
+
* 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
2002
|
* A placement of an id that is not in the input does not count.
|
|
1636
2003
|
*/
|
|
1637
2004
|
function countResizeBreaks(input, output) {
|
|
1638
2005
|
const was = new Map(input.boxes.map((b) => [b.id, b]));
|
|
1639
2006
|
let count = 0;
|
|
1640
2007
|
for (const p of output.placements) {
|
|
1641
|
-
if (p.w === void 0 && p.h === void 0) continue;
|
|
2008
|
+
if (p.w === void 0 && p.h === void 0 && p.shape === void 0) continue;
|
|
1642
2009
|
const box = was.get(p.id);
|
|
1643
2010
|
if (box === void 0) continue;
|
|
1644
2011
|
const placed = p.turn === 90 ? turnBox(box) : box;
|
|
1645
|
-
|
|
2012
|
+
const w = p.w ?? placed.w;
|
|
2013
|
+
const h = p.h ?? placed.h;
|
|
2014
|
+
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
2015
|
}
|
|
1647
2016
|
return count;
|
|
1648
2017
|
}
|
|
@@ -1654,7 +2023,7 @@ function countResizeBreaks(input, output) {
|
|
|
1654
2023
|
* A locked box without a placement, or with a move of more than `RECORD_EPS`, breaks `locked-kept`.
|
|
1655
2024
|
* A locked box without a position in the input is a bad input, not a break. `checkInput` finds it.
|
|
1656
2025
|
* 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 `
|
|
2026
|
+
* `size-kept` is in the result only when a box has `resize` or a placement has `w`, `h` or `shape` (`countResizeBreaks`).
|
|
1658
2027
|
*
|
|
1659
2028
|
* `options` goes to `countOutside` for `in-container`.
|
|
1660
2029
|
*/
|
|
@@ -1687,9 +2056,9 @@ function packHard(input, output, tolerance = DEFAULT_TOLERANCE.overlap, options
|
|
|
1687
2056
|
...resizes(input, output) ? { "size-kept": countResizeBreaks(input, output) } : {}
|
|
1688
2057
|
};
|
|
1689
2058
|
}
|
|
1690
|
-
/** This function tells if a box of the input has `resize` or a placement of the output has `w` or `
|
|
2059
|
+
/** This function tells if a box of the input has `resize` or a placement of the output has `w`, `h` or `shape`. */
|
|
1691
2060
|
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);
|
|
2061
|
+
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
2062
|
}
|
|
1694
2063
|
/**
|
|
1695
2064
|
* This function gives the problems of an input file. An empty list tells that the file is good.
|
|
@@ -1763,7 +2132,7 @@ const JOINT_MEASURES = /* @__PURE__ */ Object.freeze([
|
|
|
1763
2132
|
{
|
|
1764
2133
|
id: "joint.fill",
|
|
1765
2134
|
minEffect: .001,
|
|
1766
|
-
unit: "
|
|
2135
|
+
unit: "share",
|
|
1767
2136
|
direction: "higher",
|
|
1768
2137
|
min: 0,
|
|
1769
2138
|
max: 1,
|
|
@@ -1786,7 +2155,7 @@ const JOINT_MEASURES = /* @__PURE__ */ Object.freeze([
|
|
|
1786
2155
|
{
|
|
1787
2156
|
id: "joint.spill",
|
|
1788
2157
|
minEffect: .001,
|
|
1789
|
-
unit: "
|
|
2158
|
+
unit: "share",
|
|
1790
2159
|
direction: "lower",
|
|
1791
2160
|
min: 0,
|
|
1792
2161
|
max: 1,
|
|
@@ -1834,7 +2203,7 @@ const JOINT_MEASURES = /* @__PURE__ */ Object.freeze([
|
|
|
1834
2203
|
{
|
|
1835
2204
|
id: "joint.stability.moved",
|
|
1836
2205
|
minEffect: .02,
|
|
1837
|
-
unit: "
|
|
2206
|
+
unit: "share",
|
|
1838
2207
|
direction: "lower",
|
|
1839
2208
|
min: 0,
|
|
1840
2209
|
max: 1,
|
|
@@ -1845,7 +2214,7 @@ const JOINT_MEASURES = /* @__PURE__ */ Object.freeze([
|
|
|
1845
2214
|
},
|
|
1846
2215
|
{
|
|
1847
2216
|
id: "joint.proxy.rank",
|
|
1848
|
-
unit: "
|
|
2217
|
+
unit: "no unit",
|
|
1849
2218
|
direction: "higher",
|
|
1850
2219
|
min: -1,
|
|
1851
2220
|
max: 1,
|
|
@@ -1914,6 +2283,7 @@ const BALANCE_SIDE_DEPTH = .25;
|
|
|
1914
2283
|
/** The suffixes of the balance and stability measures, in the order of `docs/BALANCE.md`. */
|
|
1915
2284
|
const BALANCE_MEASURE_SUFFIXES = /* @__PURE__ */ Object.freeze([
|
|
1916
2285
|
"balance.gapCv",
|
|
2286
|
+
"balance.gapMean",
|
|
1917
2287
|
"balance.centroidOffset",
|
|
1918
2288
|
"balance.side.top",
|
|
1919
2289
|
"balance.side.right",
|
|
@@ -2034,6 +2404,7 @@ function balanceOf(boxes, container) {
|
|
|
2034
2404
|
const values = SIDES.map((s) => side[s]);
|
|
2035
2405
|
return {
|
|
2036
2406
|
gapCv,
|
|
2407
|
+
gapMean: mean,
|
|
2037
2408
|
centroidOffset,
|
|
2038
2409
|
side,
|
|
2039
2410
|
sideRange: Math.max(...values) - Math.min(...values)
|
|
@@ -2091,6 +2462,7 @@ function perChange(move, changed, count) {
|
|
|
2091
2462
|
function balanceMeasures(stage, balance) {
|
|
2092
2463
|
const m = {
|
|
2093
2464
|
[`${stage}.balance.gapCv`]: balance.gapCv,
|
|
2465
|
+
[`${stage}.balance.gapMean`]: balance.gapMean,
|
|
2094
2466
|
[`${stage}.balance.centroidOffset`]: balance.centroidOffset,
|
|
2095
2467
|
[`${stage}.balance.sideRange`]: balance.sideRange
|
|
2096
2468
|
};
|
|
@@ -2100,6 +2472,7 @@ function balanceMeasures(stage, balance) {
|
|
|
2100
2472
|
/**
|
|
2101
2473
|
* This function gives the specs of the balance and stability measures, with the prefix of the stage.
|
|
2102
2474
|
* The balance measures read the area of each box, so they support only `rect`. The stability measures read only the centers.
|
|
2475
|
+
* `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
2476
|
*/
|
|
2104
2477
|
function balanceMeasureSpecs(stage) {
|
|
2105
2478
|
const id = (suffix) => `${stage}.${suffix}`;
|
|
@@ -2116,9 +2489,19 @@ function balanceMeasureSpecs(stage) {
|
|
|
2116
2489
|
family: "balance",
|
|
2117
2490
|
shapes: rect
|
|
2118
2491
|
},
|
|
2492
|
+
{
|
|
2493
|
+
id: id("balance.gapMean"),
|
|
2494
|
+
unit: "layout units",
|
|
2495
|
+
direction: "higher",
|
|
2496
|
+
min: 0,
|
|
2497
|
+
max: Infinity,
|
|
2498
|
+
family: "balance",
|
|
2499
|
+
role: "diagnostic",
|
|
2500
|
+
shapes: rect
|
|
2501
|
+
},
|
|
2119
2502
|
{
|
|
2120
2503
|
id: id("balance.centroidOffset"),
|
|
2121
|
-
unit: "
|
|
2504
|
+
unit: "share",
|
|
2122
2505
|
direction: "lower",
|
|
2123
2506
|
min: 0,
|
|
2124
2507
|
max: 1,
|
|
@@ -2128,7 +2511,7 @@ function balanceMeasureSpecs(stage) {
|
|
|
2128
2511
|
},
|
|
2129
2512
|
...SIDES.map((s) => ({
|
|
2130
2513
|
id: id(`balance.side.${s}`),
|
|
2131
|
-
unit: "
|
|
2514
|
+
unit: "share",
|
|
2132
2515
|
direction: "lower",
|
|
2133
2516
|
min: 0,
|
|
2134
2517
|
max: 1,
|
|
@@ -2138,7 +2521,7 @@ function balanceMeasureSpecs(stage) {
|
|
|
2138
2521
|
})),
|
|
2139
2522
|
{
|
|
2140
2523
|
id: id("balance.sideRange"),
|
|
2141
|
-
unit: "
|
|
2524
|
+
unit: "share",
|
|
2142
2525
|
direction: "lower",
|
|
2143
2526
|
min: 0,
|
|
2144
2527
|
max: 1,
|
|
@@ -2173,6 +2556,278 @@ function balanceMeasureSpecs(stage) {
|
|
|
2173
2556
|
}
|
|
2174
2557
|
]);
|
|
2175
2558
|
}
|
|
2559
|
+
// src/measure-registry.ts
|
|
2560
|
+
/** The ids of the noise kinds, in the order of `NoiseKind`. */
|
|
2561
|
+
const NOISE_KINDS = /* @__PURE__ */ Object.freeze([
|
|
2562
|
+
"exact",
|
|
2563
|
+
"sampled",
|
|
2564
|
+
"timed"
|
|
2565
|
+
]);
|
|
2566
|
+
/**
|
|
2567
|
+
* The one unit word of a value from 0 to 1: a share, or a flag of 0 or 1 that a record averages.
|
|
2568
|
+
* `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`.
|
|
2569
|
+
*/
|
|
2570
|
+
const SHARE_UNIT = "share";
|
|
2571
|
+
/** 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. */
|
|
2572
|
+
const NO_UNIT = "no unit";
|
|
2573
|
+
/** A unit text that names a range, for example `0 to 1` or `-0.5 to 1`. */
|
|
2574
|
+
const RANGE_UNIT = /^-?\d+(?:\.\d+)? (?:to|or) -?\d+(?:\.\d+)?$/;
|
|
2575
|
+
/** The other words of a share. */
|
|
2576
|
+
const SHARE_WORDS = /* @__PURE__ */ new Set([
|
|
2577
|
+
"boolean",
|
|
2578
|
+
"fraction",
|
|
2579
|
+
"proportion"
|
|
2580
|
+
]);
|
|
2581
|
+
/** This function gives the fault of a unit text, or `undefined` for a good unit. */
|
|
2582
|
+
function unitFault(spec) {
|
|
2583
|
+
const u = spec.unit;
|
|
2584
|
+
if (typeof u !== "string" || u.length === 0) return "the unit is empty";
|
|
2585
|
+
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`;
|
|
2586
|
+
if (SHARE_WORDS.has(u)) return `the unit ${JSON.stringify(u)} is a share: use ${JSON.stringify(SHARE_UNIT)}`;
|
|
2587
|
+
if (u === "share" && !(spec.min === 0 && spec.max === 1)) return `the unit ${JSON.stringify(SHARE_UNIT)} needs the range 0 to 1`;
|
|
2588
|
+
}
|
|
2589
|
+
/**
|
|
2590
|
+
* The pattern of a `CostOrder`: terms of `1`, `n`, `e` or `p`, each with an optional `^2`, `^3` or `log`, joined by ` + `. The pattern is a
|
|
2591
|
+
* literal, because a bundler keeps a top-level `new RegExp` of a template, also when no code uses it.
|
|
2592
|
+
*/
|
|
2593
|
+
const COST_ORDER = /^(?:1|[nep](?:\^[23])?(?: log [nep])?)(?: \+ (?:1|[nep](?:\^[23])?(?: log [nep])?))*$/;
|
|
2594
|
+
/** The reason of the default rule `none`. */
|
|
2595
|
+
const NO_FINITE_RANGE = "The range is not finite, and the registry has no agreed scale for the measure.";
|
|
2596
|
+
/** This function gives the default normalization of a spec: `range` when `min` and `max` are finite, else `none` with the reason `NO_FINITE_RANGE`. */
|
|
2597
|
+
function defaultNormalize(spec) {
|
|
2598
|
+
return Number.isFinite(spec.min) && Number.isFinite(spec.max) ? { kind: "range" } : {
|
|
2599
|
+
kind: "none",
|
|
2600
|
+
reason: NO_FINITE_RANGE
|
|
2601
|
+
};
|
|
2602
|
+
}
|
|
2603
|
+
/** This function makes a registry entry from a spec and the added facts. The noise is `exact` by default. */
|
|
2604
|
+
function measureEntry(spec, facts) {
|
|
2605
|
+
return Object.freeze({
|
|
2606
|
+
...spec,
|
|
2607
|
+
source: facts.source,
|
|
2608
|
+
noise: facts.noise ?? "exact",
|
|
2609
|
+
normalize: facts.normalize ?? defaultNormalize(spec),
|
|
2610
|
+
...facts.cost === void 0 ? {} : { cost: facts.cost },
|
|
2611
|
+
...facts.planned === void 0 ? {} : { planned: facts.planned }
|
|
2612
|
+
});
|
|
2613
|
+
}
|
|
2614
|
+
/**
|
|
2615
|
+
* This function gives the loss of the value `value` of a measure: a number from 0 to 1, where 0 is the best value.
|
|
2616
|
+
* A value outside the range of the spec gives a `RangeError`. A value `NaN` also gives a `RangeError`.
|
|
2617
|
+
* The rule `none` gives `undefined`: the measure has no agreed scale.
|
|
2618
|
+
* A planned measure gives an `Error`: no code computes it, so it has no value.
|
|
2619
|
+
*/
|
|
2620
|
+
function normalizeMeasure(value, entry) {
|
|
2621
|
+
if (entry.planned !== void 0) throw new Error(`normalizeMeasure: ${entry.id} is planned, and no code computes it: ${entry.planned}`);
|
|
2622
|
+
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}].`);
|
|
2623
|
+
const rule = entry.normalize;
|
|
2624
|
+
if (rule.kind === "none") return void 0;
|
|
2625
|
+
let worse;
|
|
2626
|
+
if (rule.kind === "range") {
|
|
2627
|
+
const t = entry.max === entry.min ? 0 : (value - entry.min) / (entry.max - entry.min);
|
|
2628
|
+
worse = entry.direction === "lower" ? t : 1 - t;
|
|
2629
|
+
} else {
|
|
2630
|
+
const d = value - entry.min;
|
|
2631
|
+
if (d === Infinity) worse = entry.direction === "lower" ? 1 : 0;
|
|
2632
|
+
else worse = entry.direction === "lower" ? d / (d + rule.half) : rule.half / (d + rule.half);
|
|
2633
|
+
}
|
|
2634
|
+
return worse === 0 ? 0 : worse;
|
|
2635
|
+
}
|
|
2636
|
+
/**
|
|
2637
|
+
* This function gives the faults of a list of registry entries, as plain text. An empty list tells that the entries are good.
|
|
2638
|
+
* It examines the unique ids, the unit, the range, the floor, the noise, the normalization, the cost, the source and the planned reason.
|
|
2639
|
+
*/
|
|
2640
|
+
function checkRegistry(entries) {
|
|
2641
|
+
const out = [];
|
|
2642
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2643
|
+
for (const e of entries) {
|
|
2644
|
+
const at = `measure ${JSON.stringify(e.id)}`;
|
|
2645
|
+
if (typeof e.id !== "string" || e.id.length === 0) out.push(`${at}: the id is empty`);
|
|
2646
|
+
if (seen.has(e.id)) out.push(`${at}: the id is not unique`);
|
|
2647
|
+
seen.add(e.id);
|
|
2648
|
+
const unit = unitFault(e);
|
|
2649
|
+
if (unit !== void 0) out.push(`${at}: ${unit}`);
|
|
2650
|
+
if (e.direction !== "lower" && e.direction !== "higher") out.push(`${at}: the direction is not lower or higher`);
|
|
2651
|
+
if (typeof e.min !== "number" || Number.isNaN(e.min) || e.min === Infinity) out.push(`${at}: min is not a number below Infinity`);
|
|
2652
|
+
if (!(e.max > e.min)) out.push(`${at}: max is not above min`);
|
|
2653
|
+
if (e.floor !== void 0 && !(e.floor >= e.min && e.floor <= e.max)) out.push(`${at}: the floor is not in the range`);
|
|
2654
|
+
if (typeof e.source !== "string" || e.source.length === 0) out.push(`${at}: the source is empty`);
|
|
2655
|
+
if (!NOISE_KINDS.includes(e.noise)) out.push(`${at}: the noise is not one of ${NOISE_KINDS.join(", ")}`);
|
|
2656
|
+
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`);
|
|
2657
|
+
if (e.planned !== void 0 && !(typeof e.planned === "string" && e.planned.length > 0)) out.push(`${at}: the planned reason is empty`);
|
|
2658
|
+
if (e.planned !== void 0 && e.cost !== void 0) out.push(`${at}: a planned measure has no code, so it has no cost`);
|
|
2659
|
+
const n = e.normalize;
|
|
2660
|
+
if (n === void 0 || n.kind !== "range" && n.kind !== "scale" && n.kind !== "none") out.push(`${at}: the normalization is not range, scale or none`);
|
|
2661
|
+
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`);
|
|
2662
|
+
else if (n.kind === "scale" && !Number.isFinite(e.min)) out.push(`${at}: the normalization scale needs a finite min`);
|
|
2663
|
+
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`);
|
|
2664
|
+
else if (n.kind === "scale" && !(typeof n.reason === "string" && n.reason.length > 0)) out.push(`${at}: the normalization scale has no reason`);
|
|
2665
|
+
else if (n.kind === "none" && !(typeof n.reason === "string" && n.reason.length > 0)) out.push(`${at}: the normalization none has no reason`);
|
|
2666
|
+
}
|
|
2667
|
+
return out;
|
|
2668
|
+
}
|
|
2669
|
+
/** The package of the joint bench. It is private, so the joint measures have no npm version. */
|
|
2670
|
+
const JOINT_SOURCE = "@layout-contract/joint";
|
|
2671
|
+
/** The planned reason of the two joint stability measures. */
|
|
2672
|
+
const NO_CHANGE_PAIRS = "The joint bench has no pairs of a base input and a changed input, so no code computes the measure.";
|
|
2673
|
+
const JOINT_FACTS = {
|
|
2674
|
+
"joint.fill": { cost: "n + p" },
|
|
2675
|
+
"joint.aspectError": {
|
|
2676
|
+
cost: "n + p",
|
|
2677
|
+
normalize: {
|
|
2678
|
+
kind: "scale",
|
|
2679
|
+
half: Math.LN2,
|
|
2680
|
+
reason: "An aspect at a factor of 2 from the target has the loss 0.5."
|
|
2681
|
+
}
|
|
2682
|
+
},
|
|
2683
|
+
"joint.spill": { cost: "n + p" },
|
|
2684
|
+
"joint.straight.crossings": {
|
|
2685
|
+
cost: "n + e^2",
|
|
2686
|
+
normalize: {
|
|
2687
|
+
kind: "none",
|
|
2688
|
+
reason: "A count of crossings has no agreed scale."
|
|
2689
|
+
}
|
|
2690
|
+
},
|
|
2691
|
+
"joint.straight.length": {
|
|
2692
|
+
cost: "n + e",
|
|
2693
|
+
normalize: {
|
|
2694
|
+
kind: "scale",
|
|
2695
|
+
half: 1,
|
|
2696
|
+
reason: "An edge as long as the side S of the square of the box area has the loss 0.5."
|
|
2697
|
+
}
|
|
2698
|
+
},
|
|
2699
|
+
"joint.stability.mean": { planned: NO_CHANGE_PAIRS },
|
|
2700
|
+
"joint.stability.moved": { planned: NO_CHANGE_PAIRS },
|
|
2701
|
+
"joint.proxy.rank": { cost: "n + p + e log e" },
|
|
2702
|
+
"joint.route.length": {
|
|
2703
|
+
cost: "p",
|
|
2704
|
+
normalize: {
|
|
2705
|
+
kind: "none",
|
|
2706
|
+
reason: "A length in layout units has no agreed scale."
|
|
2707
|
+
}
|
|
2708
|
+
},
|
|
2709
|
+
"joint.ms": {
|
|
2710
|
+
noise: "timed",
|
|
2711
|
+
normalize: {
|
|
2712
|
+
kind: "none",
|
|
2713
|
+
reason: "A time of the machine has no agreed scale, and each read gives another value."
|
|
2714
|
+
}
|
|
2715
|
+
}
|
|
2716
|
+
};
|
|
2717
|
+
/**
|
|
2718
|
+
* 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.
|
|
2719
|
+
* The engines keep the specs of their own measures. `docs/measure-survey.json` lists them, and an engine adopts the registry with `measureEntry`.
|
|
2720
|
+
*/
|
|
2721
|
+
const MEASURE_REGISTRY = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ JOINT_MEASURES.map((spec) => measureEntry(spec, {
|
|
2722
|
+
source: JOINT_SOURCE,
|
|
2723
|
+
...JOINT_FACTS[spec.id]
|
|
2724
|
+
})));
|
|
2725
|
+
// src/cell.ts
|
|
2726
|
+
/** The suffixes of the cell measures, in the order of `cellMeasureSpecs`. */
|
|
2727
|
+
const CELL_MEASURE_SUFFIXES = /* @__PURE__ */ Object.freeze(["cell.areaError", "cell.compactness"]);
|
|
2728
|
+
/**
|
|
2729
|
+
* The compactness of a square: 4 over PI. The value is the literal of `4 / Math.PI`, because a bundler keeps a top-level division, also when
|
|
2730
|
+
* no code uses it.
|
|
2731
|
+
*/
|
|
2732
|
+
const SQUARE_COMPACTNESS = 1.2732395447351628;
|
|
2733
|
+
/**
|
|
2734
|
+
* This function gives the cell measures of the placed items in a container of the area `containerArea`.
|
|
2735
|
+
*
|
|
2736
|
+
* - 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.
|
|
2737
|
+
* - `containerArea` is the area of the container region. For a container with a shape, give `regionArea` of the shape, not `w` by `h`.
|
|
2738
|
+
* - `areaError` is half the sum of the absolute differences of the two shares. Cells that fill the container in the target shares give 0.
|
|
2739
|
+
* 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.
|
|
2740
|
+
* - `compactness` is the mean of `P * P / (4 * PI * A)` over the cells, for the perimeter `P` and the area `A` of each cell.
|
|
2741
|
+
* A circle gives 1, and a square gives 4 over PI. A cell with the area 0 gives `Infinity`.
|
|
2742
|
+
* - 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.
|
|
2743
|
+
*
|
|
2744
|
+
* 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`.
|
|
2745
|
+
*/
|
|
2746
|
+
function cellsOf(items, containerArea) {
|
|
2747
|
+
if (!(Number.isFinite(containerArea) && containerArea > 0)) throw new RangeError(`cellsOf: the container area is a finite number above 0, not ${containerArea}.`);
|
|
2748
|
+
if (items.length === 0) return {
|
|
2749
|
+
areaError: 0,
|
|
2750
|
+
compactness: 1
|
|
2751
|
+
};
|
|
2752
|
+
let targets = 0;
|
|
2753
|
+
for (const item of items) {
|
|
2754
|
+
if (!(Number.isFinite(item.target) && item.target > 0)) throw new RangeError(`cellsOf: a target is a finite number above 0, not ${item.target}.`);
|
|
2755
|
+
targets += item.target;
|
|
2756
|
+
}
|
|
2757
|
+
let error = 0;
|
|
2758
|
+
let compact = 0;
|
|
2759
|
+
for (const item of items) {
|
|
2760
|
+
const area = regionArea(item.cell);
|
|
2761
|
+
error += Math.abs(area / containerArea - item.target / targets);
|
|
2762
|
+
const p = regionPerimeter(item.cell);
|
|
2763
|
+
compact += area > 0 ? p * p / (4 * Math.PI * area) : Infinity;
|
|
2764
|
+
}
|
|
2765
|
+
return {
|
|
2766
|
+
areaError: Math.min(1, error / 2),
|
|
2767
|
+
compactness: compact / items.length
|
|
2768
|
+
};
|
|
2769
|
+
}
|
|
2770
|
+
/** This function gives the cell values as measures, with the prefix of the stage, for example `pack.cell.areaError`. */
|
|
2771
|
+
function cellMeasures(stage, cells) {
|
|
2772
|
+
return {
|
|
2773
|
+
[`${stage}.cell.areaError`]: cells.areaError,
|
|
2774
|
+
[`${stage}.cell.compactness`]: cells.compactness
|
|
2775
|
+
};
|
|
2776
|
+
}
|
|
2777
|
+
/**
|
|
2778
|
+
* This function gives the specs of the cell measures, with the prefix of the stage. The family is `cell`.
|
|
2779
|
+
* 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.
|
|
2780
|
+
*/
|
|
2781
|
+
function cellMeasureSpecs(stage) {
|
|
2782
|
+
const shapes = [
|
|
2783
|
+
"rect",
|
|
2784
|
+
"polygon",
|
|
2785
|
+
"circle"
|
|
2786
|
+
];
|
|
2787
|
+
return Object.freeze([{
|
|
2788
|
+
id: `${stage}.cell.areaError`,
|
|
2789
|
+
unit: "share",
|
|
2790
|
+
direction: "lower",
|
|
2791
|
+
min: 0,
|
|
2792
|
+
max: 1,
|
|
2793
|
+
floor: 0,
|
|
2794
|
+
tol: 1e-9,
|
|
2795
|
+
minEffect: .01,
|
|
2796
|
+
family: "cell",
|
|
2797
|
+
shapes
|
|
2798
|
+
}, {
|
|
2799
|
+
id: `${stage}.cell.compactness`,
|
|
2800
|
+
unit: "ratio",
|
|
2801
|
+
direction: "lower",
|
|
2802
|
+
min: 1,
|
|
2803
|
+
max: Infinity,
|
|
2804
|
+
minEffect: .01,
|
|
2805
|
+
family: "cell",
|
|
2806
|
+
shapes
|
|
2807
|
+
}]);
|
|
2808
|
+
}
|
|
2809
|
+
/** The npm name of the package that computes the cell measures. */
|
|
2810
|
+
const CELL_SOURCE = "@mark1russell7/layout-contract";
|
|
2811
|
+
/**
|
|
2812
|
+
* This function gives the registry entries of the cell measures, with the prefix of the stage.
|
|
2813
|
+
* `areaError` uses the rule `range`. `compactness` uses the rule `scale`: a square has the loss 0.5.
|
|
2814
|
+
* 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`.
|
|
2815
|
+
*/
|
|
2816
|
+
function cellMeasureEntries(stage) {
|
|
2817
|
+
const [area, compact] = cellMeasureSpecs(stage);
|
|
2818
|
+
return Object.freeze([measureEntry(area, {
|
|
2819
|
+
source: CELL_SOURCE,
|
|
2820
|
+
cost: "n"
|
|
2821
|
+
}), measureEntry(compact, {
|
|
2822
|
+
source: CELL_SOURCE,
|
|
2823
|
+
cost: "n",
|
|
2824
|
+
normalize: {
|
|
2825
|
+
kind: "scale",
|
|
2826
|
+
half: .27323954473516276,
|
|
2827
|
+
reason: "A square cell has the loss 0.5, and a circle has the loss 0."
|
|
2828
|
+
}
|
|
2829
|
+
})]);
|
|
2830
|
+
}
|
|
2176
2831
|
// src/frame.ts
|
|
2177
2832
|
/** The schema of a frame. */
|
|
2178
2833
|
const FRAME_SCHEMA = "layout-frame/1";
|
|
@@ -2683,7 +3338,7 @@ function byId(a, b) {
|
|
|
2683
3338
|
* Thus two packers with the same result in a different order have the same form.
|
|
2684
3339
|
* The function throws a `RangeError` when `placements`, `overflow` or `moved` has an id two times.
|
|
2685
3340
|
*
|
|
2686
|
-
* A placement has `w` and `
|
|
3341
|
+
* 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
3342
|
*/
|
|
2688
3343
|
function packOutputForm(output) {
|
|
2689
3344
|
uniqueIds("placements", output.placements.map((p) => p.id));
|
|
@@ -2696,7 +3351,8 @@ function packOutputForm(output) {
|
|
|
2696
3351
|
y: p.y,
|
|
2697
3352
|
turn: p.turn ?? 0,
|
|
2698
3353
|
...p.w === void 0 ? {} : { w: p.w },
|
|
2699
|
-
...p.h === void 0 ? {} : { h: p.h }
|
|
3354
|
+
...p.h === void 0 ? {} : { h: p.h },
|
|
3355
|
+
...p.shape === void 0 ? {} : { shape: p.shape }
|
|
2700
3356
|
})).sort(byId),
|
|
2701
3357
|
overflow: [...output.overflow].sort(),
|
|
2702
3358
|
size: {
|
|
@@ -3443,8 +4099,7 @@ const COMMON_FIELDS = [
|
|
|
3443
4099
|
"noDefault",
|
|
3444
4100
|
"when"
|
|
3445
4101
|
];
|
|
3446
|
-
const NUMBER_FIELDS = [
|
|
3447
|
-
...COMMON_FIELDS,
|
|
4102
|
+
const NUMBER_FIELDS = /* @__PURE__ */ COMMON_FIELDS.concat([
|
|
3448
4103
|
"min",
|
|
3449
4104
|
"max",
|
|
3450
4105
|
"step",
|
|
@@ -3452,32 +4107,26 @@ const NUMBER_FIELDS = [
|
|
|
3452
4107
|
"special",
|
|
3453
4108
|
"marks",
|
|
3454
4109
|
"values"
|
|
3455
|
-
];
|
|
3456
|
-
const ENUM_FIELDS = [
|
|
3457
|
-
|
|
3458
|
-
"values",
|
|
3459
|
-
"custom"
|
|
3460
|
-
];
|
|
3461
|
-
const STRATEGY_FIELDS = [
|
|
3462
|
-
...COMMON_FIELDS,
|
|
4110
|
+
]);
|
|
4111
|
+
const ENUM_FIELDS = /* @__PURE__ */ COMMON_FIELDS.concat(["values", "custom"]);
|
|
4112
|
+
const STRATEGY_FIELDS = /* @__PURE__ */ COMMON_FIELDS.concat([
|
|
3463
4113
|
"step",
|
|
3464
4114
|
"kinds",
|
|
3465
4115
|
"optionsKey",
|
|
3466
4116
|
"inlineOptions",
|
|
3467
4117
|
"optIn",
|
|
3468
4118
|
"needs"
|
|
3469
|
-
];
|
|
4119
|
+
]);
|
|
3470
4120
|
const MEMBER_FIELDS = [
|
|
3471
4121
|
"strategy",
|
|
3472
4122
|
"options",
|
|
3473
4123
|
"id"
|
|
3474
4124
|
];
|
|
3475
|
-
const LIST_FIELDS = [
|
|
3476
|
-
...COMMON_FIELDS,
|
|
4125
|
+
const LIST_FIELDS = /* @__PURE__ */ COMMON_FIELDS.concat([
|
|
3477
4126
|
"of",
|
|
3478
4127
|
"min",
|
|
3479
4128
|
"max"
|
|
3480
|
-
];
|
|
4129
|
+
]);
|
|
3481
4130
|
const ROW_FIELDS = [
|
|
3482
4131
|
"property",
|
|
3483
4132
|
"class",
|
|
@@ -5385,6 +6034,118 @@ const RECT_SCATTERED = /* @__PURE__ */ Object.freeze({
|
|
|
5385
6034
|
};
|
|
5386
6035
|
}
|
|
5387
6036
|
});
|
|
5388
|
-
|
|
6037
|
+
// src/measure-survey.ts
|
|
6038
|
+
/** The schema of a measure survey. */
|
|
6039
|
+
const MEASURE_SURVEY_SCHEMA = "measure-survey/1";
|
|
6040
|
+
const DATE = /^\d{4}-\d{2}-\d{2}$/;
|
|
6041
|
+
const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
6042
|
+
/** This function gives the faults of a survey, as plain text. An empty list tells that the survey is good. */
|
|
6043
|
+
function checkSurvey(v) {
|
|
6044
|
+
const out = [];
|
|
6045
|
+
if (!isObj(v)) return ["the survey is not an object"];
|
|
6046
|
+
if (v["schema"] !== "measure-survey/1") out.push(`the schema is not ${MEASURE_SURVEY_SCHEMA}`);
|
|
6047
|
+
if (typeof v["date"] !== "string" || !DATE.test(v["date"])) out.push("the date is not YYYY-MM-DD");
|
|
6048
|
+
const sources = v["sources"];
|
|
6049
|
+
if (!Array.isArray(sources) || sources.length === 0) return [...out, "the survey has no sources"];
|
|
6050
|
+
const names = /* @__PURE__ */ new Set();
|
|
6051
|
+
for (const [i, s] of sources.entries()) {
|
|
6052
|
+
const at = `sources[${i}]`;
|
|
6053
|
+
if (!isObj(s)) {
|
|
6054
|
+
out.push(`${at} is not an object`);
|
|
6055
|
+
continue;
|
|
6056
|
+
}
|
|
6057
|
+
if (typeof s["package"] !== "string" || s["package"].length === 0) out.push(`${at}.package is empty`);
|
|
6058
|
+
else if (names.has(s["package"])) out.push(`${at}.package ${s["package"]} is not unique`);
|
|
6059
|
+
else names.add(s["package"]);
|
|
6060
|
+
if (typeof s["version"] !== "string" || s["version"].length === 0) out.push(`${at}.version is empty`);
|
|
6061
|
+
const measures = s["measures"];
|
|
6062
|
+
if (!Array.isArray(measures)) {
|
|
6063
|
+
out.push(`${at}.measures is not a list`);
|
|
6064
|
+
continue;
|
|
6065
|
+
}
|
|
6066
|
+
const ids = /* @__PURE__ */ new Set();
|
|
6067
|
+
for (const [j, m] of measures.entries()) {
|
|
6068
|
+
const mat = `${at}.measures[${j}]`;
|
|
6069
|
+
if (!isObj(m)) {
|
|
6070
|
+
out.push(`${mat} is not an object`);
|
|
6071
|
+
continue;
|
|
6072
|
+
}
|
|
6073
|
+
if (typeof m["id"] !== "string" || m["id"].length === 0) out.push(`${mat}.id is empty`);
|
|
6074
|
+
else if (ids.has(m["id"])) out.push(`${mat}.id ${m["id"]} is not unique in its source`);
|
|
6075
|
+
else ids.add(m["id"]);
|
|
6076
|
+
if (typeof m["unit"] !== "string" || m["unit"].length === 0) out.push(`${mat}.unit is empty`);
|
|
6077
|
+
if (m["direction"] !== "lower" && m["direction"] !== "higher") out.push(`${mat}.direction is not lower or higher`);
|
|
6078
|
+
const min = m["min"];
|
|
6079
|
+
const max = m["max"];
|
|
6080
|
+
if (min === void 0 !== (max === void 0)) out.push(`${mat} has min or max, not both`);
|
|
6081
|
+
if (min !== void 0 && (typeof min !== "number" || !Number.isFinite(min))) out.push(`${mat}.min is not a finite number`);
|
|
6082
|
+
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`);
|
|
6083
|
+
}
|
|
6084
|
+
}
|
|
6085
|
+
return out;
|
|
6086
|
+
}
|
|
6087
|
+
/** 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. */
|
|
6088
|
+
function nameAfterStage(id) {
|
|
6089
|
+
const dot = id.indexOf(".");
|
|
6090
|
+
return dot < 0 ? id : id.slice(dot + 1);
|
|
6091
|
+
}
|
|
6092
|
+
/** This function gives the findings of a good survey. `checkSurvey` examines the survey first. */
|
|
6093
|
+
function surveyFindings(survey) {
|
|
6094
|
+
const bySource = {};
|
|
6095
|
+
const byId = /* @__PURE__ */ new Map();
|
|
6096
|
+
const units = {};
|
|
6097
|
+
const noRange = /* @__PURE__ */ new Set();
|
|
6098
|
+
for (const s of survey.sources) {
|
|
6099
|
+
bySource[s.package] = s.measures.length;
|
|
6100
|
+
for (const m of s.measures) {
|
|
6101
|
+
const list = byId.get(m.id) ?? [];
|
|
6102
|
+
list.push({
|
|
6103
|
+
source: s.package,
|
|
6104
|
+
m
|
|
6105
|
+
});
|
|
6106
|
+
byId.set(m.id, list);
|
|
6107
|
+
units[m.unit] = (units[m.unit] ?? 0) + 1;
|
|
6108
|
+
if (m.min === void 0) noRange.add(m.id);
|
|
6109
|
+
}
|
|
6110
|
+
}
|
|
6111
|
+
const conflicts = [];
|
|
6112
|
+
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({
|
|
6113
|
+
id,
|
|
6114
|
+
field,
|
|
6115
|
+
sources: list.map((e) => e.source)
|
|
6116
|
+
});
|
|
6117
|
+
const byName = /* @__PURE__ */ new Map();
|
|
6118
|
+
for (const [id, list] of byId) {
|
|
6119
|
+
const name = nameAfterStage(id);
|
|
6120
|
+
const group = byName.get(name) ?? [];
|
|
6121
|
+
for (const e of list) group.push({
|
|
6122
|
+
id,
|
|
6123
|
+
...e
|
|
6124
|
+
});
|
|
6125
|
+
byName.set(name, group);
|
|
6126
|
+
}
|
|
6127
|
+
const twins = [];
|
|
6128
|
+
for (const [name, group] of [...byName].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)) {
|
|
6129
|
+
if (new Set(group.map((e) => e.source)).size < 2) continue;
|
|
6130
|
+
for (const field of ["unit", "direction"]) if (new Set(group.map((e) => e.m[field])).size > 1) twins.push({
|
|
6131
|
+
name,
|
|
6132
|
+
field,
|
|
6133
|
+
values: group.map((e) => ({
|
|
6134
|
+
id: e.id,
|
|
6135
|
+
source: e.source,
|
|
6136
|
+
value: e.m[field]
|
|
6137
|
+
}))
|
|
6138
|
+
});
|
|
6139
|
+
}
|
|
6140
|
+
return {
|
|
6141
|
+
ids: byId.size,
|
|
6142
|
+
bySource,
|
|
6143
|
+
conflicts,
|
|
6144
|
+
twins,
|
|
6145
|
+
noRange: [...noRange].sort(),
|
|
6146
|
+
units
|
|
6147
|
+
};
|
|
6148
|
+
}
|
|
6149
|
+
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
6150
|
|
|
5390
6151
|
//# sourceMappingURL=index.js.map
|