@mark1russell7/layout-contract 0.4.0 → 0.5.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 +5 -5
- package/build/index.d.ts +90 -8
- package/build/index.d.ts.map +1 -1
- package/build/index.js +334 -11
- package/build/index.js.map +1 -1
- package/build/{measures-KWg_was6.d.ts → measures-COqcrCzv.d.ts} +8 -3
- package/build/measures-COqcrCzv.d.ts.map +1 -0
- package/build/text.d.ts +1 -1
- package/package.json +1 -1
- package/src/families.ts +56 -0
- package/src/geometry.ts +11 -0
- package/src/measures.ts +2 -0
- package/src/record.ts +7 -3
- package/src/rules.ts +104 -1
- package/src/strategy-catalog.ts +140 -10
- package/src/tolerance.ts +3 -0
- package/build/measures-KWg_was6.d.ts.map +0 -1
|
@@ -88,6 +88,11 @@ declare function overlapOf(a: Rect, b: Rect): {
|
|
|
88
88
|
* Segments that only touch at an end, or that are collinear, do not cross. The test has no tolerance.
|
|
89
89
|
*/
|
|
90
90
|
declare function segmentsCross(a: Point, b: Point, c: Point, d: Point): boolean;
|
|
91
|
+
/**
|
|
92
|
+
* This function gives the point where the segments `ab` and `cd` cross, or `undefined`.
|
|
93
|
+
* It gives a point exactly when `segmentsCross` is true, so a touch at an end and collinear segments give `undefined`.
|
|
94
|
+
*/
|
|
95
|
+
declare function crossingPoint(a: Point, b: Point, c: Point, d: Point): Point | undefined;
|
|
91
96
|
/** This function gives the shortest distance from `p` to the segment `ab`. A segment of length 0 is the point `a`. */
|
|
92
97
|
declare function pointSegmentDistance(p: Point, a: Point, b: Point): number;
|
|
93
98
|
/** One broken claim of a corpus, with the ids of the inputs that break it. */
|
|
@@ -603,7 +608,7 @@ interface MeasureSpec<Id extends string = string> {
|
|
|
603
608
|
readonly bound?: ShapeBound;
|
|
604
609
|
}
|
|
605
610
|
/** The id of a joint measure. `docs/JOINT-MEASURES.md` gives the definitions. */
|
|
606
|
-
type JointMeasureId = "joint.fill" | "joint.aspectError" | "joint.spill" | "joint.straight.crossings" | "joint.straight.length" | "joint.stability.mean" | "joint.stability.moved" | "joint.proxy.rank" | "joint.ms";
|
|
611
|
+
type JointMeasureId = "joint.fill" | "joint.aspectError" | "joint.spill" | "joint.straight.crossings" | "joint.straight.length" | "joint.stability.mean" | "joint.stability.moved" | "joint.proxy.rank" | "joint.route.length" | "joint.ms";
|
|
607
612
|
/**
|
|
608
613
|
* The specs of the joint measures, in the order of `docs/JOINT-MEASURES.md`.
|
|
609
614
|
* `joint.fill` and `joint.straight.length` read the area of each box, so they support only `rect`. `docs/SHAPES.md` tells the reasons.
|
|
@@ -626,5 +631,5 @@ declare function minEffectOf(spec: Pick<MeasureSpec, "minEffect">, a?: number, b
|
|
|
626
631
|
* Two equal values are a tie, also two infinite values. A value `NaN` is an error.
|
|
627
632
|
*/
|
|
628
633
|
declare function compareMeasure(candidate: number, baseline: number, spec: Pick<MeasureSpec, "direction" | "minEffect">): Verdict;
|
|
629
|
-
export { CorpusSplit as $, shapeKindsOf as A,
|
|
630
|
-
//# sourceMappingURL=measures-
|
|
634
|
+
export { CorpusSplit as $, shapeKindsOf as A, pointSegmentDistance as At, LayoutInput as B, checkShapes as C, Sides as Ct, ruleLevel as D, grow as Dt, itemShapeOf as E, crossingPoint as Et, LayoutBox as F, translate as Ft, PairGap as G, PackInput as H, LayoutCell as I, packLinks as J, Port as K, LayoutContainer as L, shapeSupportProblems as M, segmentsCross as Mt, shapesOf as N, toRect as Nt, shapeKindOf as O, marginSides as Ot, Item as P, toSizedRect as Pt, CorpusEntry as Q, LayoutEdge as R, boxArea as S, Side as St, isShapeKind as T, boundsOf as Tt, PackInputOptions as U, Obstacle as V, PackLink as W, toPackInput as X, portPoint as Y, ClaimBreak as Z, ShapeKind as _, Margin as _t, MeasureSpec as a, SPLIT_SCHEMA as at, ShapeLimit as b, Region as bt, minEffectOf as c, checkDeclared as ct, SHAPE_KINDS as d, declaredClaimOf as dt, DeclaredClaim as et, SHAPE_KIND_IDS as f, declaredCounts as ft, ShapeInput as g, splitFolds as gt, ShapeGeometry as h, splitFileName as ht, JointMeasureId as i, FamilyCell as it, shapeLevel as j, regionArea as jt, shapeKindProblem as k, overlapOf as kt, DEFAULT_SHAPES as l, checkSplit as lt, ShapeCheck as m, leaveOneSeedOut as mt, JOINT_MEASURES as n, FAMILY_ANY_KIND as nt, Verdict as o, SplitCorpus as ot, ShapeBound as p, familyOf as pt, canonicalInput as q, JOINT_MOVED_MIN as r, FamilyBy as rt, compareMeasure as s, SplitFold as st, Direction as t, Declares as tt, RuleLevel as u, corpusHashOf as ut, ShapeKindSpec as v, Point as vt, inputShapeLevel as w, SizedRect as wt, ShapeSupport as x, SIDES as xt, ShapeLevel as y, Rect as yt, LayoutGenerator as z };
|
|
635
|
+
//# sourceMappingURL=measures-COqcrCzv.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"measures-COqcrCzv.d.ts","names":[],"sources":["../src/geometry.ts","../src/corpus.ts","../src/input.ts","../src/shapes.ts","../src/measures.ts"],"mappings":";KACY;;cAGC,gBAAgB;UAEZ;WACN;WACA;;;UAIM;WACN;WACA;WACA;WACA;;;;;;UAOM;WACN;WACA;WACA;WACA;;;iBAIK,OAAO,MAAM,YAAY;;iBAKzB,YAAY,MAAM,OAAO;;UAKxB;WACN;WACA;WACA;WACA;;;;;;KAOC;WACG;WAAuB;WAAoB;WAAoB;WAAoB;;WAEnF;WACA;WACA;;WAEA;WAAyB;WAAqB;WAAqB;;WACnE;WAA0B;WAAqB;WAAqB;WAAqB;;;KAG5F,kBAAkB;;iBAGd,YAAY,QAAQ,qBAAqB;;iBAOzC,KAAK,MAAM,MAAM,QAAQ,qBAAqB;;iBAM9C,SAAS,QAAQ,SAAS;;;;;iBA4B1B,WAAW,QAAQ;;iBAyBnB,UAAU,QAAQ,QAAQ,YAAY,aAAa;;;;;iBAoBnD,UAAU,GAAG,MAAM,GAAG;WAAkB;WAAoB;;;;;;iBAqB5D,cAAc,GA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gBAAgB,KAAK,KAAK,sDAAsD,MAAM,WAAW,OAAO,aAAa;;;;;;;iBAWrH,qBAAqB,eAAe;;iBAwCpC,YAAY,iBAAiB,SAAS;;;;;iBAQtC,YAAY,KAAK,YAAY;;;;;;;iBAW7B,QAAQ,KAAK;;iBAQb,aAAa,OAAO,KAAK,wBAAwB;;iBAMjD,YAAY,OAAO,KAAK,wBAAwB;;iBAMhD,iBAAiB,KAAK;;iBAWtB,SAAS,KAAK,KAAK,mCAAmC;;UAKrD;;;;;;WAMN;;;;;;;;iBASK,YAAY,OAAO,YAAY,eAAe,gBAAgB,UAAS,aAAkB;;KCtQ7F;;;;;;UAOK,YAAY;WAClB,IAAI;WACJ;WACA,WAAW;;WAEX;;WAEA;;WAEA;;WAEA;;WAEA;;;;;WAKA;aAAgC;;;WAEhC;;WAEA,kBAAkB;;;;;WAKlB,QAAQ;;;KAIP;;;;;cAqBC,yBAAyB,YAAY;;cAcrC;;KAGD;;;;;;iBAOI,YAAY,MAAM,KAAK,2BAA2B,YAAO;;;;;;;iBAezD,eAAe,mBAAmB,kBAAkB,MAAM,KAAK,0CAA0C"}
|
package/build/text.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as MeasureSpec } from "./measures-
|
|
1
|
+
import { a as MeasureSpec } from "./measures-COqcrCzv.js";
|
|
2
2
|
/**
|
|
3
3
|
* The advance function of a host: the width of a run of text in a font, in pixels.
|
|
4
4
|
* A browser host gives `canvasAdvance(context)`. A test or a server host gives a function of a font file.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/layout-contract",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.5.1",
|
|
5
5
|
"description": "The shared contract of the packing and highways layout engines: the input and record formats, the turn rule, margins, tolerances and hard rules.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"layout",
|
package/src/families.ts
CHANGED
|
@@ -832,6 +832,62 @@ export const EDGE_KEY_STEP: CorpusStep = /* @__PURE__ */ Object.freeze({
|
|
|
832
832
|
apply: (input: LayoutInput, seed: number, d: DimensionValues) => edgeKeyStep(input, { edgeKey: edgeKeyOf(d) }, seed),
|
|
833
833
|
});
|
|
834
834
|
|
|
835
|
+
/** The set of `CIRCLE_SCATTERED` when the caller gives no set. */
|
|
836
|
+
const CIRCLE_SET = "circle-v1";
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* The shared family `circle-scattered`, version 1: circle items of random whole radii in a fixed container, with no obstacles and no edges.
|
|
840
|
+
*
|
|
841
|
+
* - The dimension `containerShape` takes `rect` or `circle`. One seed gives the same radii in the 2 containers, so a pair joins on the seed.
|
|
842
|
+
* - The circles fill approximately half of the container, and the container is at least as large as the largest circle.
|
|
843
|
+
* - The stream `items` gives each radius, from 8 to 40. The stream `container` gives the width of a rectangle over its side, from 0.80 to 1.25.
|
|
844
|
+
* - The family has no margin and no gap. Each value comes from whole numbers and `Math.sqrt`, which each engine rounds correctly, so the family is `exact`.
|
|
845
|
+
*/
|
|
846
|
+
export const CIRCLE_SCATTERED: CorpusFamily<LayoutInput> = /* @__PURE__ */ Object.freeze({
|
|
847
|
+
name: "circle-scattered",
|
|
848
|
+
version: 1,
|
|
849
|
+
owner: "layout-contract",
|
|
850
|
+
meaning: "Circle items of random radii in a fixed rectangle or circle container, which they fill approximately half.",
|
|
851
|
+
dimensions: /* @__PURE__ */ Object.freeze([/* @__PURE__ */ sharedDimension("count", { default: 40 }), /* @__PURE__ */ sharedDimension("containerShape", { supported: ["rect", "circle"] })]),
|
|
852
|
+
seeding: "stepRandom(seed, items) gives the radius of each item. stepRandom(seed, container) gives the width over the side of a rectangle, in hundredths.",
|
|
853
|
+
crossEngine: "exact",
|
|
854
|
+
generate(seed: number, dims: DimensionValues, context: GenerateContext): LayoutInput {
|
|
855
|
+
const count = dims["count"];
|
|
856
|
+
if (typeof count !== "number" || !Number.isInteger(count) || count < 0) throw new RangeError(`circle-scattered: the count is a whole number of 0 or more, not ${String(count)}.`);
|
|
857
|
+
const containerShape = dims["containerShape"];
|
|
858
|
+
if (containerShape !== "rect" && containerShape !== "circle") throw new RangeError(`circle-scattered: the container shape is rect or circle, not ${String(containerShape)}.`);
|
|
859
|
+
const set = context.set ?? CIRCLE_SET;
|
|
860
|
+
let r = stepRandom(seed, "items");
|
|
861
|
+
const radii = Array.from({ length: count }, () => randomInt(r, 8, 40));
|
|
862
|
+
const largest = radii.reduce((m, v) => Math.max(m, v), 1);
|
|
863
|
+
const squares = radii.reduce((sum, v) => sum + v * v, 0);
|
|
864
|
+
let container: LayoutInput["container"];
|
|
865
|
+
if (containerShape === "circle") {
|
|
866
|
+
const big = Math.max(largest, Math.ceil(Math.sqrt(2 * squares)));
|
|
867
|
+
container = { w: 2 * big, h: 2 * big, fixed: "size", shape: { kind: "circle", cx: big, cy: big, r: big } };
|
|
868
|
+
} else {
|
|
869
|
+
// The circles have the area PI * squares. A rectangle of 6.3 * squares holds them at approximately half, and 6.3 keeps PI out of the input.
|
|
870
|
+
const area = Math.ceil((63 * squares) / 10);
|
|
871
|
+
r = stepRandom(seed, "container");
|
|
872
|
+
const w = Math.max(2 * largest, Math.ceil((Math.sqrt(area) * randomInt(r, 80, 125)) / 100));
|
|
873
|
+
container = { w, h: Math.max(2 * largest, Math.ceil(area / w)), fixed: "size" };
|
|
874
|
+
}
|
|
875
|
+
const body: LayoutInput = {
|
|
876
|
+
schema: "layout-input/1",
|
|
877
|
+
id: `${set}/circle-scattered/${containerShape}/s${seed}`,
|
|
878
|
+
set,
|
|
879
|
+
family: "circle-scattered",
|
|
880
|
+
seed,
|
|
881
|
+
source: "made-up",
|
|
882
|
+
generator: { name: "circle-scattered", version: 1, params: { count, containerShape }, seed },
|
|
883
|
+
container,
|
|
884
|
+
boxes: radii.map((v, i) => ({ id: `c${i}`, w: 2 * v, h: 2 * v, shape: { kind: "circle", cx: v, cy: v, r: v } })),
|
|
885
|
+
edges: [],
|
|
886
|
+
};
|
|
887
|
+
return { ...body, hash: documentHash(body) };
|
|
888
|
+
},
|
|
889
|
+
});
|
|
890
|
+
|
|
835
891
|
/** The set of `RECT_SCATTERED` when the caller gives no set. */
|
|
836
892
|
const XCHECK_SET = "xcheck-v1";
|
|
837
893
|
|
package/src/geometry.ts
CHANGED
|
@@ -175,6 +175,17 @@ export function segmentsCross(a: Point, b: Point, c: Point, d: Point): boolean {
|
|
|
175
175
|
return opposite(orient(c, d, a), orient(c, d, b)) && opposite(orient(a, b, c), orient(a, b, d));
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
+
/**
|
|
179
|
+
* This function gives the point where the segments `ab` and `cd` cross, or `undefined`.
|
|
180
|
+
* It gives a point exactly when `segmentsCross` is true, so a touch at an end and collinear segments give `undefined`.
|
|
181
|
+
*/
|
|
182
|
+
export function crossingPoint(a: Point, b: Point, c: Point, d: Point): Point | undefined {
|
|
183
|
+
if (!segmentsCross(a, b, c, d)) return undefined;
|
|
184
|
+
const from = orient(c, d, a);
|
|
185
|
+
const t = from / (from - orient(c, d, b));
|
|
186
|
+
return { x: a.x + t * (b.x - a.x), y: a.y + t * (b.y - a.y) };
|
|
187
|
+
}
|
|
188
|
+
|
|
178
189
|
/** This function gives the shortest distance from `p` to the segment `ab`. A segment of length 0 is the point `a`. */
|
|
179
190
|
export function pointSegmentDistance(p: Point, a: Point, b: Point): number {
|
|
180
191
|
const dx = b.x - a.x;
|
package/src/measures.ts
CHANGED
|
@@ -49,6 +49,7 @@ export type JointMeasureId =
|
|
|
49
49
|
| "joint.stability.mean"
|
|
50
50
|
| "joint.stability.moved"
|
|
51
51
|
| "joint.proxy.rank"
|
|
52
|
+
| "joint.route.length"
|
|
52
53
|
| "joint.ms";
|
|
53
54
|
|
|
54
55
|
/** The shape kinds of a joint measure that reads only the bounding boxes, the centers and the positions of the boxes. */
|
|
@@ -69,6 +70,7 @@ export const JOINT_MEASURES: readonly MeasureSpec<JointMeasureId>[] = /* @__PURE
|
|
|
69
70
|
{ id: "joint.stability.mean", minEffect: 0.005, unit: "W", direction: "lower", min: 0, max: Infinity, floor: 0, tol: 0.005, role: "change-guard", shapes: BOUNDS_ONLY },
|
|
70
71
|
{ id: "joint.stability.moved", minEffect: 0.02, unit: "0 to 1", direction: "lower", min: 0, max: 1, floor: 0, tol: 0.02, role: "change-guard", shapes: BOUNDS_ONLY },
|
|
71
72
|
{ id: "joint.proxy.rank", unit: "-1 to 1", direction: "higher", min: -1, max: 1, role: "diagnostic", shapes: BOUNDS_ONLY },
|
|
73
|
+
{ id: "joint.route.length", unit: "layout units for each route", direction: "lower", min: 0, max: Infinity, role: "diagnostic", shapes: BOUNDS_ONLY },
|
|
72
74
|
{ id: "joint.ms", unit: "milliseconds", direction: "lower", min: 0, max: Infinity, role: "time", shapes: BOUNDS_ONLY },
|
|
73
75
|
] as const);
|
|
74
76
|
|
package/src/record.ts
CHANGED
|
@@ -34,7 +34,9 @@ export interface LayoutRecord {
|
|
|
34
34
|
readonly hardRule?: number;
|
|
35
35
|
/**
|
|
36
36
|
* The value is `true` when two runs with the same seed give the same positions.
|
|
37
|
-
* A skipped record has `null`, because no run made a claim.
|
|
37
|
+
* A skipped record has `null`, because no run made a claim.
|
|
38
|
+
* A record with `oneRun: true` can also have `null`: the pair had no second run to compare.
|
|
39
|
+
* Each other record has a boolean.
|
|
38
40
|
*/
|
|
39
41
|
readonly deterministic: boolean | null;
|
|
40
42
|
/** The id of the prediction that was written before the run. */
|
|
@@ -46,6 +48,7 @@ export interface LayoutRecord {
|
|
|
46
48
|
/**
|
|
47
49
|
* The value is `true` when a measure of the pair chose 1 timed repeat and no warm-up.
|
|
48
50
|
* Then `ms` is one time, so a time gate does not read this record.
|
|
51
|
+
* A bench that runs such a pair only once writes `deterministic: null`, and its record makes no claim on determinism.
|
|
49
52
|
*/
|
|
50
53
|
readonly oneRun?: boolean;
|
|
51
54
|
/** The wall budget of the pair of a strategy and an input, in milliseconds. */
|
|
@@ -153,7 +156,7 @@ export function hardRuleOf(record: Pick<LayoutRecord, "hardRule">): number {
|
|
|
153
156
|
/**
|
|
154
157
|
* This function gives the problems of one `layout-record/1` value. An empty list means a good record.
|
|
155
158
|
* A record that has `skipped` has `deterministic: null`. An old skipped record with a boolean is also correct.
|
|
156
|
-
* A record without `skipped` has a boolean
|
|
159
|
+
* A record without `skipped` has a boolean, or `null` when it has `oneRun: true`. The check accepts fields that the format does not know.
|
|
157
160
|
*/
|
|
158
161
|
export function checkLayoutRecord(value: unknown): string[] {
|
|
159
162
|
if (!isFields(value)) return ["the record is not an object"];
|
|
@@ -179,7 +182,8 @@ export function checkLayoutRecord(value: unknown): string[] {
|
|
|
179
182
|
if (v["env"] !== undefined) out.push(...envProblems(v["env"]));
|
|
180
183
|
const deterministic = v["deterministic"];
|
|
181
184
|
if (v["skipped"] === undefined) {
|
|
182
|
-
|
|
185
|
+
const noClaim = deterministic === null && v["oneRun"] === true;
|
|
186
|
+
if (typeof deterministic !== "boolean" && !noClaim) out.push("deterministic is not a boolean, and the record has no skipped");
|
|
183
187
|
} else if (deterministic !== null && typeof deterministic !== "boolean") out.push("deterministic of a skipped record is not null or a boolean");
|
|
184
188
|
return out;
|
|
185
189
|
}
|
package/src/rules.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { boundsOf, grow, marginSides, overlapOf, type Margin, type Rect, type Region } from "./geometry.ts";
|
|
1
|
+
import { boundsOf, grow, marginSides, overlapOf, type Margin, type Point, type Rect, type Region } from "./geometry.ts";
|
|
2
2
|
import type { LayoutBox, LayoutContainer, LayoutInput, Obstacle, PairGap, Port } from "./input.ts";
|
|
3
3
|
import { applyPack, type PackOutput } from "./output.ts";
|
|
4
4
|
import { SHAPE_KIND_IDS, shapeKindProblem, type ShapeSupport } from "./shapes.ts";
|
|
@@ -215,6 +215,109 @@ export function circlesOutside(input: LayoutInput, tolerance: number = DEFAULT_T
|
|
|
215
215
|
return count;
|
|
216
216
|
}
|
|
217
217
|
|
|
218
|
+
/** This function gives the length of the part of the segment `ab` inside a circle. A radius of 0 or less gives 0. */
|
|
219
|
+
function partInCircle(a: Point, b: Point, cx: number, cy: number, r: number): number {
|
|
220
|
+
if (r <= 0) return 0;
|
|
221
|
+
const dx = b.x - a.x;
|
|
222
|
+
const dy = b.y - a.y;
|
|
223
|
+
const fx = a.x - cx;
|
|
224
|
+
const fy = a.y - cy;
|
|
225
|
+
const l2 = dx * dx + dy * dy;
|
|
226
|
+
if (l2 === 0) return 0;
|
|
227
|
+
const half = fx * dx + fy * dy;
|
|
228
|
+
const inner = half * half - l2 * (fx * fx + fy * fy - r * r);
|
|
229
|
+
if (inner <= 0) return 0;
|
|
230
|
+
const root = Math.sqrt(inner);
|
|
231
|
+
const t0 = Math.max(0, (-half - root) / l2);
|
|
232
|
+
const t1 = Math.min(1, (-half + root) / l2);
|
|
233
|
+
return t1 > t0 ? (t1 - t0) * Math.sqrt(l2) : 0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** This function gives the length of the part of the segment `ab` inside the open box from (`x0`, `y0`) to (`x1`, `y1`). A box with no inside gives 0. */
|
|
237
|
+
function partInBox(a: Point, b: Point, x0: number, y0: number, x1: number, y1: number): number {
|
|
238
|
+
if (x1 <= x0 || y1 <= y0) return 0;
|
|
239
|
+
const dx = b.x - a.x;
|
|
240
|
+
const dy = b.y - a.y;
|
|
241
|
+
let t0 = 0;
|
|
242
|
+
let t1 = 1;
|
|
243
|
+
for (const [from, d, lo, hi] of [[a.x, dx, x0, x1], [a.y, dy, y0, y1]] as const) {
|
|
244
|
+
if (d === 0) {
|
|
245
|
+
if (from <= lo || from >= hi) return 0;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
const ta = (lo - from) / d;
|
|
249
|
+
const tb = (hi - from) / d;
|
|
250
|
+
t0 = Math.max(t0, Math.min(ta, tb));
|
|
251
|
+
t1 = Math.min(t1, Math.max(ta, tb));
|
|
252
|
+
if (t0 > t1) return 0;
|
|
253
|
+
}
|
|
254
|
+
return (t1 - t0) * Math.sqrt(dx * dx + dy * dy);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* This function tells if the segment `ab` goes through a placed box, by the rule of `Tolerance.through`.
|
|
259
|
+
* A part of the segment longer than `tolerance` is inside the shape made `tolerance` smaller.
|
|
260
|
+
*
|
|
261
|
+
* - A circle box reads its circle. Each other kind reads its bounding box, so on a polygon or a free shape the answer is an upper bound.
|
|
262
|
+
* - The margin of the box does not count. A box without a position gives `false`.
|
|
263
|
+
*/
|
|
264
|
+
export function segmentThroughShape(a: Point, b: Point, box: LayoutBox, tolerance: number = DEFAULT_TOLERANCE.through): boolean {
|
|
265
|
+
const disc = placedCircle(box);
|
|
266
|
+
if (disc !== undefined) return partInCircle(a, b, disc.cx, disc.cy, disc.r - tolerance) > tolerance;
|
|
267
|
+
const r = placedRect(box);
|
|
268
|
+
if (r === undefined) return false;
|
|
269
|
+
return partInBox(a, b, r.x + tolerance, r.y + tolerance, r.x + r.w - tolerance, r.y + r.h - tolerance) > tolerance;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** A route between two boxes: the ids of its ends and its points. `countLinksThroughShapes` reads it. */
|
|
273
|
+
export interface ShapeLink {
|
|
274
|
+
readonly source: string;
|
|
275
|
+
readonly target: string;
|
|
276
|
+
readonly points: readonly Point[];
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* This function counts the routes that go through a placed box that is not one of their two ends, by `segmentThroughShape`.
|
|
281
|
+
* A route counts once, also when it goes through more than one box.
|
|
282
|
+
*/
|
|
283
|
+
export function countLinksThroughShapes(boxes: readonly LayoutBox[], links: readonly ShapeLink[], tolerance: number = DEFAULT_TOLERANCE.through): number {
|
|
284
|
+
const placed = boxes.flatMap((box) => {
|
|
285
|
+
const r = placedRect(box);
|
|
286
|
+
return r === undefined ? [] : [{ box, r }];
|
|
287
|
+
});
|
|
288
|
+
placed.sort((p, q) => p.r.x - q.r.x);
|
|
289
|
+
const widest = placed.reduce((m, p) => Math.max(m, p.r.w), 0);
|
|
290
|
+
const firstAt = (x: number): number => {
|
|
291
|
+
let lo = 0;
|
|
292
|
+
let hi = placed.length;
|
|
293
|
+
while (lo < hi) {
|
|
294
|
+
const mid = (lo + hi) >>> 1;
|
|
295
|
+
if (placed[mid]!.r.x < x) lo = mid + 1;
|
|
296
|
+
else hi = mid;
|
|
297
|
+
}
|
|
298
|
+
return lo;
|
|
299
|
+
};
|
|
300
|
+
let count = 0;
|
|
301
|
+
for (const link of links) {
|
|
302
|
+
let hit = false;
|
|
303
|
+
for (let i = 1; i < link.points.length && !hit; i++) {
|
|
304
|
+
const a = link.points[i - 1]!;
|
|
305
|
+
const b = link.points[i]!;
|
|
306
|
+
const left = Math.min(a.x, b.x);
|
|
307
|
+
const right = Math.max(a.x, b.x);
|
|
308
|
+
const top = Math.min(a.y, b.y);
|
|
309
|
+
const bottom = Math.max(a.y, b.y);
|
|
310
|
+
for (let k = firstAt(left - widest); k < placed.length && placed[k]!.r.x < right && !hit; k++) {
|
|
311
|
+
const { box, r } = placed[k]!;
|
|
312
|
+
if (r.x + r.w <= left || r.y >= bottom || r.y + r.h <= top) continue;
|
|
313
|
+
if (box.id !== link.source && box.id !== link.target && segmentThroughShape(a, b, box, tolerance)) hit = true;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (hit) count++;
|
|
317
|
+
}
|
|
318
|
+
return count;
|
|
319
|
+
}
|
|
320
|
+
|
|
218
321
|
/**
|
|
219
322
|
* This function counts the placed boxes that go out of the container.
|
|
220
323
|
* Each box with its margin must stay inside the container made smaller by `inset`. Only the fixed sides of the size count. A shaped container counts as its bounding box.
|
package/src/strategy-catalog.ts
CHANGED
|
@@ -4,8 +4,14 @@ import type { Supports } from "./record.ts";
|
|
|
4
4
|
import { type AnyOption, declarationIssues, type ListItem, type NoDefaultReason, optionIssue, type OptionScale, type ValueOption } from "./strategy-options.ts";
|
|
5
5
|
import type { ParamValue, StrategySpace } from "./strategy-space.ts";
|
|
6
6
|
|
|
7
|
-
/** The schema of a strategy catalog. A new field is a new version of the schema. */
|
|
8
|
-
export const STRATEGY_CATALOG_SCHEMA = "strategy-catalog/
|
|
7
|
+
/** The newest schema of a strategy catalog. A new field is a new version of the schema. */
|
|
8
|
+
export const STRATEGY_CATALOG_SCHEMA = "strategy-catalog/2";
|
|
9
|
+
|
|
10
|
+
/** The schemas that `parseStrategyCatalog` reads, oldest first. Schema 2 adds the field `output` of an entry. */
|
|
11
|
+
export const STRATEGY_CATALOG_SCHEMAS: readonly ["strategy-catalog/1", "strategy-catalog/2"] = ["strategy-catalog/1", STRATEGY_CATALOG_SCHEMA];
|
|
12
|
+
|
|
13
|
+
/** The name of one schema of a strategy catalog. */
|
|
14
|
+
export type StrategyCatalogSchema = (typeof STRATEGY_CATALOG_SCHEMAS)[number];
|
|
9
15
|
|
|
10
16
|
/**
|
|
11
17
|
* The dimension of `supports` that tells who owns the positions of the nodes. The value `host` means that the strategy keeps the positions that the host gives.
|
|
@@ -26,6 +32,60 @@ export interface CatalogLimit {
|
|
|
26
32
|
/** The kind of a strategy: it places on its own, it wraps other strategies, or it is for one shape of item. */
|
|
27
33
|
export type StrategyKind = "single" | "composite" | "shape";
|
|
28
34
|
|
|
35
|
+
/** The output properties that tell if the numbers of a result are whole: the positions and the sizes. The parser examines the class of these properties. */
|
|
36
|
+
export const OUTPUT_GRID_PROPERTIES: readonly string[] = ["position-grid", "size-grid"];
|
|
37
|
+
|
|
38
|
+
/** The class of a grid property of an output. */
|
|
39
|
+
export type OutputGridClass = "whole" | "near-whole" | "fractional" | "untested";
|
|
40
|
+
|
|
41
|
+
/** The largest distance from a whole number for a near-whole value. A value at a larger distance is fractional. */
|
|
42
|
+
export const OUTPUT_NEAR_WHOLE = 1e-9;
|
|
43
|
+
|
|
44
|
+
/** The fewest packed inputs for the class `whole` or `near-whole`. A fraction needs no such count, because one counterexample is a proof. */
|
|
45
|
+
export const OUTPUT_CLASS_MIN_INPUTS = 30;
|
|
46
|
+
|
|
47
|
+
/** What one read of an output covers. The row states no promise beyond this evidence. */
|
|
48
|
+
export interface OutputEvidence {
|
|
49
|
+
/** The count of inputs that the strategy packed in this read. */
|
|
50
|
+
readonly inputs: number;
|
|
51
|
+
/** The count of inputs with a fractional value of the property. A grid property must have this field. */
|
|
52
|
+
readonly fractionalInputs?: number;
|
|
53
|
+
/** The count of inputs with a near-whole value and no fractional value of the property. A grid property must have this field. */
|
|
54
|
+
readonly nearWholeInputs?: number;
|
|
55
|
+
/** The input family or the study of the read. */
|
|
56
|
+
readonly source: string;
|
|
57
|
+
/** The name of the options of the read, for example `default`. */
|
|
58
|
+
readonly optionSet: string;
|
|
59
|
+
/** The version of the catalog that the read used. */
|
|
60
|
+
readonly catalogVersion: string;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** One property of the output of a strategy, as a read found it. An entry has one row for each pair of `property` and `optionSet`. */
|
|
64
|
+
export interface OutputRow {
|
|
65
|
+
/** The property, for example `position-grid`. */
|
|
66
|
+
readonly property: string;
|
|
67
|
+
/** The class of the property. For a grid property, `outputGridClass` gives the only class that the evidence permits. */
|
|
68
|
+
readonly class: string;
|
|
69
|
+
readonly evidence: OutputEvidence;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The counts that decide the class of a grid property. */
|
|
73
|
+
export interface OutputGridCounts {
|
|
74
|
+
readonly inputs: number;
|
|
75
|
+
readonly fractionalInputs: number;
|
|
76
|
+
readonly nearWholeInputs: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* This function gives the class of a grid property from its counts. One fractional input gives `fractional` at any count.
|
|
81
|
+
* With no fraction, fewer than `OUTPUT_CLASS_MIN_INPUTS` inputs give `untested`. Each other read gives `near-whole` or `whole`.
|
|
82
|
+
*/
|
|
83
|
+
export function outputGridClass(counts: OutputGridCounts): OutputGridClass {
|
|
84
|
+
if (counts.fractionalInputs > 0) return "fractional";
|
|
85
|
+
if (counts.inputs < OUTPUT_CLASS_MIN_INPUTS) return "untested";
|
|
86
|
+
return counts.nearWholeInputs > 0 ? "near-whole" : "whole";
|
|
87
|
+
}
|
|
88
|
+
|
|
29
89
|
/** One strategy of a catalog. */
|
|
30
90
|
export interface CatalogEntry {
|
|
31
91
|
/** The key is unique in the catalog: `<step>/<id>` with a step, else `id`. */
|
|
@@ -51,6 +111,8 @@ export interface CatalogEntry {
|
|
|
51
111
|
readonly optIn: boolean;
|
|
52
112
|
/** The value `true` tells that the strategy is a baseline of a bench. It has `optIn` true, and a chooser does not offer it. */
|
|
53
113
|
readonly baseline?: true;
|
|
114
|
+
/** The reads of the output of the strategy. A file with this field has the schema 2. */
|
|
115
|
+
readonly output?: readonly OutputRow[];
|
|
54
116
|
/** The declarations of the options, in the order of a form. */
|
|
55
117
|
readonly options: Readonly<Record<string, AnyOption>>;
|
|
56
118
|
}
|
|
@@ -64,7 +126,7 @@ export interface StrategyCatalogSource {
|
|
|
64
126
|
|
|
65
127
|
/** A catalog document: the form that `writeStrategyCatalog` writes and `parseStrategyCatalog` reads. */
|
|
66
128
|
export interface StrategyCatalog extends StrategyCatalogSource {
|
|
67
|
-
readonly schema:
|
|
129
|
+
readonly schema: StrategyCatalogSchema;
|
|
68
130
|
/** The npm version of the engine. */
|
|
69
131
|
readonly version: string;
|
|
70
132
|
}
|
|
@@ -148,13 +210,15 @@ const STRATEGY_KINDS: readonly string[] = ["single", "composite", "shape"];
|
|
|
148
210
|
const NO_DEFAULT_REASONS: readonly NoDefaultReason[] = ["unset", "required", "derived", "function"];
|
|
149
211
|
const SCALES: readonly OptionScale[] = ["linear", "log"];
|
|
150
212
|
const TOP_FIELDS: readonly string[] = ["schema", "engine", "version", "strategies"];
|
|
151
|
-
const ENTRY_FIELDS: readonly string[] = ["key", "id", "step", "family", "kind", "describe", "supports", "limits", "deterministic", "determinismNote", "optIn", "baseline", "options"];
|
|
213
|
+
const ENTRY_FIELDS: readonly string[] = ["key", "id", "step", "family", "kind", "describe", "supports", "limits", "deterministic", "determinismNote", "optIn", "baseline", "output", "options"];
|
|
152
214
|
const COMMON_FIELDS: readonly string[] = ["type", "describe", "default", "noDefault", "when"];
|
|
153
215
|
const NUMBER_FIELDS: readonly string[] = [...COMMON_FIELDS, "min", "max", "step", "scale", "special", "marks", "values"];
|
|
154
216
|
const ENUM_FIELDS: readonly string[] = [...COMMON_FIELDS, "values", "custom"];
|
|
155
217
|
const STRATEGY_FIELDS: readonly string[] = [...COMMON_FIELDS, "step", "kinds", "optionsKey", "inlineOptions", "optIn", "needs"];
|
|
156
218
|
const MEMBER_FIELDS: readonly string[] = ["strategy", "options", "id"];
|
|
157
219
|
const LIST_FIELDS: readonly string[] = [...COMMON_FIELDS, "of", "min", "max"];
|
|
220
|
+
const ROW_FIELDS: readonly string[] = ["property", "class", "evidence"];
|
|
221
|
+
const EVIDENCE_FIELDS: readonly string[] = ["inputs", "fractionalInputs", "nearWholeInputs", "source", "optionSet", "catalogVersion"];
|
|
158
222
|
/** The fields of a declaration, in the order of the file. */
|
|
159
223
|
const OPTION_ORDER: readonly string[] = ["type", "describe", "min", "max", "step", "scale", "special", "marks", "values", "custom", "kinds", "optionsKey", "inlineOptions", "optIn", "needs", "of", "default", "noDefault", "when"];
|
|
160
224
|
|
|
@@ -394,8 +458,51 @@ function checkLimits(at: string, limits: unknown, faults: string[]): void {
|
|
|
394
458
|
}
|
|
395
459
|
}
|
|
396
460
|
|
|
461
|
+
/** This function adds the faults of the evidence of one output row. */
|
|
462
|
+
function checkEvidence(at: string, property: unknown, rowClass: unknown, raw: unknown, faults: string[]): void {
|
|
463
|
+
if (!isFields(raw)) {
|
|
464
|
+
faults.push(`${at}: "evidence" is not an object.`);
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
refuseUnknown(`${at}: the evidence`, raw, EVIDENCE_FIELDS, faults);
|
|
468
|
+
for (const f of ["source", "optionSet", "catalogVersion"]) if (!isText(raw[f])) faults.push(`${at}: the evidence field "${f}" is not a text.`);
|
|
469
|
+
const count = (v: unknown): v is number => Number.isInteger(v) && (v as number) >= 0;
|
|
470
|
+
if (!count(raw["inputs"])) faults.push(`${at}: the evidence field "inputs" is not a count.`);
|
|
471
|
+
const grid = OUTPUT_GRID_PROPERTIES.includes(property as string);
|
|
472
|
+
for (const f of ["fractionalInputs", "nearWholeInputs"]) if (grid ? !count(raw[f]) : raw[f] !== undefined && !count(raw[f])) faults.push(`${at}: the evidence field "${f}" is not a count.`);
|
|
473
|
+
const [inputs, fractionalInputs, nearWholeInputs] = [raw["inputs"], raw["fractionalInputs"], raw["nearWholeInputs"]];
|
|
474
|
+
if (!grid || !count(inputs) || !count(fractionalInputs) || !count(nearWholeInputs)) return;
|
|
475
|
+
if (fractionalInputs + nearWholeInputs > inputs) faults.push(`${at}: the fractional and near-whole counts are more than the inputs.`);
|
|
476
|
+
const expected = outputGridClass({ inputs, fractionalInputs, nearWholeInputs });
|
|
477
|
+
if (rowClass !== expected) faults.push(`${at}: the evidence gives the class "${expected}".`);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** This function adds the faults of the field `output` of one entry. */
|
|
481
|
+
function checkOutput(at: string, raw: unknown, schema: unknown, faults: string[]): void {
|
|
482
|
+
if (schema === STRATEGY_CATALOG_SCHEMAS[0]) faults.push(`${at}: the field "output" needs the schema ${STRATEGY_CATALOG_SCHEMA}.`);
|
|
483
|
+
if (!Array.isArray(raw) || raw.length === 0) {
|
|
484
|
+
faults.push(`${at}: "output" is not a list with rows.`);
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
const seen = new Set<string>();
|
|
488
|
+
(raw as readonly unknown[]).forEach((row, i) => {
|
|
489
|
+
const place = `${at}: the output row ${i}`;
|
|
490
|
+
if (!isFields(row)) {
|
|
491
|
+
faults.push(`${place} is not an object.`);
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
refuseUnknown(place, row, ROW_FIELDS, faults);
|
|
495
|
+
for (const f of ["property", "class"]) if (!isText(row[f])) faults.push(`${place}: "${f}" is not a text.`);
|
|
496
|
+
checkEvidence(place, row["property"], row["class"], row["evidence"], faults);
|
|
497
|
+
const optionSet = isFields(row["evidence"]) ? row["evidence"]["optionSet"] : undefined;
|
|
498
|
+
const name = JSON.stringify([row["property"], optionSet]);
|
|
499
|
+
if (seen.has(name)) faults.push(`${place}: the property and the option set are not unique.`);
|
|
500
|
+
seen.add(name);
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
|
|
397
504
|
/** This function restores one entry of a file. */
|
|
398
|
-
function readEntry(at: string, raw: unknown, faults: string[]): CatalogEntry | undefined {
|
|
505
|
+
function readEntry(at: string, raw: unknown, schema: unknown, faults: string[]): CatalogEntry | undefined {
|
|
399
506
|
if (!isFields(raw)) {
|
|
400
507
|
faults.push(`${at} is not an object.`);
|
|
401
508
|
return undefined;
|
|
@@ -411,6 +518,7 @@ function readEntry(at: string, raw: unknown, faults: string[]): CatalogEntry | u
|
|
|
411
518
|
if (raw["baseline"] === true && raw["optIn"] !== true) faults.push(`${at}: a baseline has "optIn" true.`);
|
|
412
519
|
if (!isFields(raw["supports"]) || !Object.values(raw["supports"]).every(isTextList)) faults.push(`${at}: "supports" is not a record of text lists.`);
|
|
413
520
|
checkLimits(at, raw["limits"], faults);
|
|
521
|
+
if (raw["output"] !== undefined) checkOutput(at, raw["output"], schema, faults);
|
|
414
522
|
if (isText(raw["id"]) && raw["key"] !== catalogKey(raw["id"], raw["step"] as string | undefined)) faults.push(`${at}: the key does not follow from "step" and "id".`);
|
|
415
523
|
if (!isFields(raw["options"])) {
|
|
416
524
|
faults.push(`${at}: "options" is not an object.`);
|
|
@@ -443,6 +551,7 @@ function readEntry(at: string, raw: unknown, faults: string[]): CatalogEntry | u
|
|
|
443
551
|
...(determinismNote === undefined ? {} : { determinismNote }),
|
|
444
552
|
optIn: raw["optIn"] as boolean,
|
|
445
553
|
...(raw["baseline"] === true ? { baseline: true as const } : {}),
|
|
554
|
+
...(raw["output"] === undefined ? {} : { output: raw["output"] as readonly OutputRow[] }),
|
|
446
555
|
options,
|
|
447
556
|
};
|
|
448
557
|
}
|
|
@@ -512,20 +621,21 @@ function checkReferences(entries: readonly CatalogEntry[], faults: string[]): vo
|
|
|
512
621
|
}
|
|
513
622
|
|
|
514
623
|
/**
|
|
515
|
-
* This function reads a catalog from parsed JSON
|
|
624
|
+
* This function reads a catalog from parsed JSON, in each schema of `STRATEGY_CATALOG_SCHEMAS`. It refuses each unknown field and each other schema.
|
|
516
625
|
* It gives the catalog with its `Infinity` values restored, or the list of all faults.
|
|
517
626
|
*/
|
|
518
627
|
export function parseStrategyCatalog(value: unknown): StrategyCatalogParse {
|
|
519
628
|
const faults: string[] = [];
|
|
520
629
|
if (!isFields(value)) return { ok: false, faults: ["The catalog is not an object."] };
|
|
521
630
|
refuseUnknown("The catalog", value, TOP_FIELDS, faults);
|
|
522
|
-
|
|
631
|
+
const schema = value["schema"];
|
|
632
|
+
if (!(STRATEGY_CATALOG_SCHEMAS as readonly unknown[]).includes(schema)) faults.push(`The schema is not one of ${STRATEGY_CATALOG_SCHEMAS.join(", ")}.`);
|
|
523
633
|
if (!isText(value["engine"])) faults.push(`"engine" is not a text.`);
|
|
524
634
|
if (!isText(value["version"])) faults.push(`"version" is not a text.`);
|
|
525
635
|
if (!Array.isArray(value["strategies"])) return { ok: false, faults: [...faults, `"strategies" is not a list.`] };
|
|
526
636
|
const entries: CatalogEntry[] = [];
|
|
527
637
|
(value["strategies"] as readonly unknown[]).forEach((raw, i) => {
|
|
528
|
-
const entry = readEntry(`The strategy ${isFields(raw) && isText(raw["key"]) ? raw["key"] : i}`, raw, faults);
|
|
638
|
+
const entry = readEntry(`The strategy ${isFields(raw) && isText(raw["key"]) ? raw["key"] : i}`, raw, schema, faults);
|
|
529
639
|
if (entry !== undefined) entries.push(entry);
|
|
530
640
|
});
|
|
531
641
|
for (let i = 1; i < entries.length; i++) {
|
|
@@ -536,7 +646,7 @@ export function parseStrategyCatalog(value: unknown): StrategyCatalogParse {
|
|
|
536
646
|
// A bad entry is not in the list, so each reference to it would give a false fault.
|
|
537
647
|
if (faults.length === 0) checkReferences(entries, faults);
|
|
538
648
|
if (faults.length > 0) return { ok: false, faults };
|
|
539
|
-
return { ok: true, catalog: { schema:
|
|
649
|
+
return { ok: true, catalog: { schema: schema as StrategyCatalogSchema, engine: value["engine"] as string, version: value["version"] as string, strategies: entries } };
|
|
540
650
|
}
|
|
541
651
|
|
|
542
652
|
/** This function gives the file form of a number. A value that is not finite becomes its name, and `NaN` stays for the writer to refuse. */
|
|
@@ -582,6 +692,23 @@ function writeOption(option: AnyOption | ListItem): Fields {
|
|
|
582
692
|
return out;
|
|
583
693
|
}
|
|
584
694
|
|
|
695
|
+
/** This function gives the file form of one output row, with the fields in the order of the file. */
|
|
696
|
+
function writeRow(row: OutputRow): unknown {
|
|
697
|
+
const e = row.evidence;
|
|
698
|
+
return {
|
|
699
|
+
property: row.property,
|
|
700
|
+
class: row.class,
|
|
701
|
+
evidence: {
|
|
702
|
+
inputs: e.inputs,
|
|
703
|
+
...(e.fractionalInputs === undefined ? {} : { fractionalInputs: e.fractionalInputs }),
|
|
704
|
+
...(e.nearWholeInputs === undefined ? {} : { nearWholeInputs: e.nearWholeInputs }),
|
|
705
|
+
source: e.source,
|
|
706
|
+
optionSet: e.optionSet,
|
|
707
|
+
catalogVersion: e.catalogVersion,
|
|
708
|
+
},
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
|
|
585
712
|
/** This function gives the file form of an entry, with its fields in the order of the file. */
|
|
586
713
|
function writeEntry(e: CatalogEntry): Fields {
|
|
587
714
|
const options = Object.fromEntries(Object.entries(e.options).map(([key, o]) => [key, writeOption(o)]));
|
|
@@ -598,6 +725,7 @@ function writeEntry(e: CatalogEntry): Fields {
|
|
|
598
725
|
...(e.determinismNote === undefined ? {} : { determinismNote: e.determinismNote }),
|
|
599
726
|
optIn: e.optIn,
|
|
600
727
|
...(e.baseline === true ? { baseline: true } : {}),
|
|
728
|
+
...(e.output === undefined ? {} : { output: e.output.map(writeRow) }),
|
|
601
729
|
options,
|
|
602
730
|
};
|
|
603
731
|
}
|
|
@@ -613,11 +741,13 @@ function unsafeValues(v: unknown, at: string): string[] {
|
|
|
613
741
|
|
|
614
742
|
/**
|
|
615
743
|
* This function gives the text of the catalog file, sorted by key. The same source and version give the same bytes.
|
|
744
|
+
* The file has the oldest schema that holds it, so a catalog with no `output` stays valid for a reader of the schema 1.
|
|
616
745
|
* The caller gives the version from `package.json`. The function throws an error if the file breaks a rule of the format.
|
|
617
746
|
*/
|
|
618
747
|
export function writeStrategyCatalog(source: StrategyCatalogSource, version: string): string {
|
|
619
748
|
const strategies = [...source.strategies].sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0)).map(writeEntry);
|
|
620
|
-
const
|
|
749
|
+
const schema = source.strategies.some((e) => e.output !== undefined) ? STRATEGY_CATALOG_SCHEMA : STRATEGY_CATALOG_SCHEMAS[0];
|
|
750
|
+
const doc = { schema, engine: source.engine, version, strategies };
|
|
621
751
|
const unsafe = unsafeValues(doc, "catalog");
|
|
622
752
|
if (unsafe.length > 0) throw new Error(`The catalog of ${source.engine} has values that JSON cannot hold: ${unsafe.join(" ")}`);
|
|
623
753
|
const text = `${JSON.stringify(doc, null, 2)}\n`;
|
package/src/tolerance.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
/** The epsilon for the float compares in an engine. */
|
|
2
2
|
export const EPS = 1e-6;
|
|
3
3
|
|
|
4
|
+
/** The count of `EPS` steps in one layout unit. It is the integer equal to `1 / EPS`. */
|
|
5
|
+
export const EPS_STEPS = 1_000_000;
|
|
6
|
+
|
|
4
7
|
/**
|
|
5
8
|
* The count of grid steps in one unit of a written file. A file rounds each number `v` to `Math.round(v * RECORD_SCALE) / RECORD_SCALE`.
|
|
6
9
|
* Use a multiplication by this value, not a division by `RECORD_EPS`. The two differ at some half steps.
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"measures-KWg_was6.d.ts","names":[],"sources":["../src/geometry.ts","../src/corpus.ts","../src/input.ts","../src/shapes.ts","../src/measures.ts"],"mappings":";KACY;;cAGC,gBAAgB;UAEZ;WACN;WACA;;;UAIM;WACN;WACA;WACA;WACA;;;;;;UAOM;WACN;WACA;WACA;WACA;;;iBAIK,OAAO,MAAM,YAAY;;iBAKzB,YAAY,MAAM,OAAO;;UAKxB;WACN;WACA;WACA;WACA;;;;;;KAOC;WACG;WAAuB;WAAoB;WAAoB;WAAoB;;WAEnF;WACA;WACA;;WAEA;WAAyB;WAAqB;WAAqB;;WACnE;WAA0B;WAAqB;WAAqB;WAAqB;;;KAG5F,kBAAkB;;iBAGd,YAAY,QAAQ,qBAAqB;;iBAOzC,KAAK,MAAM,MAAM,QAAQ,qBAAqB;;iBAM9C,SAAS,QAAQ,SAAS;;;;;iBA4B1B,WAAW,QAAQ;;iBAyBnB,UAAU,QAAQ,QAAQ,YAAY,aAAa;;;;;iBAoBnD,UAAU,GAAG,MAAM,GAAG;WAAkB;WAAoB;;;;;;iBAqB5D,cAAc,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG;;iBAK/C,qBAAqB,GAAG,OAAO,GAAG,OAAO,GAAG;;UC9K3C;;WAEN;;WAEA;;WAEA;;;UAIM;;WAEN;;WAEA;;WAEA,QAAQ,SAAS;;WAEjB,WAAW;;WAEX;;;;;;;;KASC;;;;KAGA,aAAa,SAAS;;cAGrB;;;;;;iBA4CG,SAAS,MAAM,YAAY,UAAU;;KAazC,SAAS,MAAM,OAAO;;;;;iBAMlB,eAAe,GAAG,QAAQ,SAAS,IAAI,UAAU,SAAS,IAAI,0BAA8B;;;;;iBAa5F,gBAAgB,mBAAmB;;;;;;;;;;;;;;;iBAuCnC,cAAc,GAC5B,QAAQ,SAAS,IACjB,UAAU,SAAS,IACnB,OAAO,eACP,yBACA,UAAS,OAAO,MAAM,yBACrB;;UAiEc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