@mark1russell7/packing 0.1.5 → 0.1.6

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@mark1russell7/packing",
4
- "version": "0.1.5",
4
+ "version": "0.1.6",
5
5
  "description": "Rectangle packing: shelf, guillotine and skyline strategies, push cascades and measures of a layout.",
6
6
  "keywords": [
7
7
  "rectangle-packing",
@@ -50,6 +50,7 @@
50
50
  },
51
51
  "devDependencies": {
52
52
  "@mark1russell7/cue": "^1.0.2",
53
+ "@mark1russell7/layout-contract": "github:mark1russell7/layout-contract#69933a61b0270c5915b82ab563d24908ade708af&path:packages/layout-contract",
53
54
  "@types/node": "^26.6.4",
54
55
  "tsdown": "^0.23.1",
55
56
  "typescript": "^7.0.2",
package/src/check.ts CHANGED
@@ -26,11 +26,13 @@ function overlap(a: Box, b: Box, tolerance: number): boolean {
26
26
  * This function counts the breaks of the 4 hard rules of the packer for an output.
27
27
  *
28
28
  * - `no-overlap`: each pair of items with their margins, and each pair of an item and an obstacle, that overlap by more than `tolerance.overlap`.
29
- * - `in-container`: each item with its margin that goes out of a fixed side by more than `tolerance.overlap`.
29
+ * - `in-container`: each item with its margin that goes out of a fixed side by more than `tolerance.overlap`. The inset of
30
+ * the container makes each fixed side smaller (decision 3 of `docs/PACKER.md`).
30
31
  * - `locked-kept`: each locked item that moved by more than `RECORD_EPS`, or that has no position.
31
32
  * - `complete`: each item with no position that is not in the overflow.
32
33
  *
33
- * The rules read a shape of the container or of an obstacle as its bounding box. The result has no tolerance in it.
34
+ * The rules read a shape of the container, of an obstacle or of an item as its bounding box (decision 5). The bounding box
35
+ * of an item shape is relative to the position of the item. The result has no tolerance in it.
34
36
  */
35
37
  export function checkHard(input: PackInput, output: PackOutput, tolerance: Pick<Tolerance, "overlap"> = DEFAULT_TOLERANCE): HardBreaks {
36
38
  const tol = tolerance.overlap;
@@ -52,7 +54,10 @@ export function checkHard(input: PackInput, output: PackOutput, tolerance: Pick<
52
54
  }
53
55
  const placed: Item = p.turn === 90 ? turnItem(item) : item;
54
56
  const m = marginOf(placed);
55
- boxes.push({ l: p.x - m.left, t: p.y - m.top, r: p.x + placed.w + m.right, b: p.y + placed.h + m.bottom });
57
+ const area = placed.shape === undefined ? { x: 0, y: 0, w: placed.w, h: placed.h } : boundsOf(placed.shape);
58
+ const l = p.x + area.x;
59
+ const t = p.y + area.y;
60
+ boxes.push({ l: l - m.left, t: t - m.top, r: l + area.w + m.right, b: t + area.h + m.bottom });
56
61
  }
57
62
 
58
63
  const moved = new Map(output.moved.map((m) => [m.id, m]));
@@ -73,11 +78,12 @@ export function checkHard(input: PackInput, output: PackOutput, tolerance: Pick<
73
78
  const y = shape?.y ?? 0;
74
79
  const w = shape?.w ?? container.w ?? Infinity;
75
80
  const h = shape?.h ?? container.h ?? Infinity;
81
+ const inset = container.inset ?? 0;
76
82
  const fixedW = container.fixed === "size" || container.fixed === "width";
77
83
  const fixedH = container.fixed === "size" || container.fixed === "height";
78
84
  for (const box of boxes) {
79
- const outX = fixedW && (box.l < x - tol || box.r > x + w + tol);
80
- const outY = fixedH && (box.t < y - tol || box.b > y + h + tol);
85
+ const outX = fixedW && (box.l < x + inset - tol || box.r > x + w - inset + tol);
86
+ const outY = fixedH && (box.t < y + inset - tol || box.b > y + h - inset + tol);
81
87
  if (outX || outY) breaks["in-container"] += 1;
82
88
  }
83
89
  return breaks;
package/src/strategy.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  import type { Container, PackInput, PackOutput, Region } from "./contract.ts";
2
2
 
3
- /** For each dimension of the taxonomy, the values that a strategy supports. `docs/research/TAXONOMY.md` gives the values. */
4
- export interface Supports {
3
+ /**
4
+ * For each dimension of the taxonomy, the values that a strategy supports. `docs/research/TAXONOMY.md` gives the values.
5
+ * The type is an alias, not an interface. Thus it fits the record `Supports` of the contract.
6
+ */
7
+ export type Supports = {
5
8
  containerSize: ("fixed" | "freeHeight" | "freeWidth" | "freeAspect" | "freeArea")[];
6
9
  containerShape: ("rect" | "convex" | "polygon" | "circle" | "ellipse" | "holes")[];
7
10
  /** The movement of the obstacles. An empty list supports only an input with no obstacle and no locked item. */
@@ -11,7 +14,7 @@ export interface Supports {
11
14
  order: ("free" | "soft" | "hard")[];
12
15
  groups: ("none" | "flat" | "nested")[];
13
16
  mode: ("offline" | "online" | "incremental")[];
14
- }
17
+ };
15
18
 
16
19
  /** A dimension of `Supports`. */
17
20
  export type Dimension = keyof Supports;