@biolibs/bionet-space 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/find.js ADDED
@@ -0,0 +1,102 @@
1
+ /**
2
+ * How close a look at a place is.
3
+ *
4
+ * @param arranged - The place, or `null` for the whole lab.
5
+ * @returns `lab` for everything, `room` for a room, `unit` for whatever stands in a room (a freezer, a
6
+ * bench, an instrument), `box` for a place with positions, and `inside` for anything between.
7
+ */
8
+ export function levelOf(arranged) {
9
+ if (arranged === null)
10
+ return "lab";
11
+ if (arranged.place.grid !== undefined)
12
+ return "box";
13
+ if (arranged.path.length === 1)
14
+ return "room";
15
+ if (arranged.path.length === 2)
16
+ return "unit";
17
+ return "inside";
18
+ }
19
+ /**
20
+ * The space some places take up together.
21
+ *
22
+ * @param places - The places.
23
+ * @returns The smallest box around all of them, or `null` if there are none.
24
+ */
25
+ export function boundsOf(places) {
26
+ if (places.length === 0)
27
+ return null;
28
+ const low = [Infinity, Infinity, Infinity];
29
+ const high = [-Infinity, -Infinity, -Infinity];
30
+ for (const { center, size } of places) {
31
+ for (let axis = 0; axis < 3; axis++) {
32
+ low[axis] = Math.min(low[axis], center[axis] - size[axis] / 2);
33
+ high[axis] = Math.max(high[axis], center[axis] + size[axis] / 2);
34
+ }
35
+ }
36
+ return {
37
+ center: [(low[0] + high[0]) / 2, (low[1] + high[1]) / 2, (low[2] + high[2]) / 2],
38
+ size: [high[0] - low[0], high[1] - low[1], high[2] - low[2]],
39
+ };
40
+ }
41
+ /** Which way to look from, by level, for something facing front: across, up, towards the viewer. */
42
+ const FROM = {
43
+ lab: [0.45, 0.62, 1],
44
+ room: [0.4, 0.6, 1],
45
+ unit: [0.42, 0.28, 1],
46
+ inside: [0.3, 0.2, 1],
47
+ box: [0, 1, 0.42],
48
+ };
49
+ /**
50
+ * Where a camera should stand to see a place whole.
51
+ *
52
+ * It looks from in front and a little above, closer the smaller the thing is, and from nearly
53
+ * straight above for a box, since a box is read like a page. A place that is turned is looked at
54
+ * from its own front.
55
+ *
56
+ * @param arranged - Every place, arranged.
57
+ * @param id - The place to look at, or `null` for the whole lab.
58
+ * @param fov - The camera's vertical field of view, in degrees.
59
+ * @returns Where to stand and what to look at.
60
+ */
61
+ export function poseFor(arranged, id, fov = 45) {
62
+ const one = id === null ? null : arranged.find((each) => each.place.id === id) ?? null;
63
+ const bounds = one ?? boundsOf(arranged.filter((each) => each.place.parent === null)) ?? { center: [0, 0, 0], size: [4, 3, 4] };
64
+ const level = levelOf(one);
65
+ const [x, y, z] = FROM[level];
66
+ // Turn the direction with the place, so a freezer facing a side wall is still seen from its front.
67
+ const turn = one?.turn ?? 0;
68
+ const [dx, dz] = turn === 90 ? [-z, x] : turn === 180 ? [-x, -z] : turn === 270 ? [z, -x] : [x, z];
69
+ const length = Math.hypot(dx, y, dz);
70
+ const reach = Math.max(bounds.size[0], bounds.size[1], bounds.size[2]);
71
+ const distance = (reach * (level === "lab" || level === "room" ? 0.68 : level === "box" ? 1.25 : 1.0)) / Math.tan((fov * Math.PI) / 360);
72
+ return {
73
+ target: [...bounds.center],
74
+ position: [bounds.center[0] + (dx / length) * distance, bounds.center[1] + (y / length) * distance, bounds.center[2] + (dz / length) * distance],
75
+ };
76
+ }
77
+ /**
78
+ * Everything stored in a place or in anything inside it.
79
+ *
80
+ * @param layout - The lab.
81
+ * @param arranged - Its places, arranged.
82
+ * @param id - The place.
83
+ * @returns What is stored there, however deep.
84
+ */
85
+ export function storedWithin(layout, arranged, id) {
86
+ const inside = new Set(arranged.filter((one) => one.path.includes(id)).map((one) => one.place.id));
87
+ return layout.stored.filter((one) => inside.has(one.place));
88
+ }
89
+ /**
90
+ * Search what is stored, by the name of the part.
91
+ *
92
+ * @param layout - The lab.
93
+ * @param words - What was typed. Case does not matter, and every word has to appear.
94
+ * @returns What matched, in the order it is stored. Nothing, if nothing was typed.
95
+ */
96
+ export function search(layout, words) {
97
+ const wanted = words.toLowerCase().split(/\s+/).filter((word) => word !== "");
98
+ if (wanted.length === 0)
99
+ return [];
100
+ return layout.stored.filter((one) => wanted.every((word) => one.name.toLowerCase().includes(word)));
101
+ }
102
+ //# sourceMappingURL=find.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"find.js","sourceRoot":"","sources":["../src/find.ts"],"names":[],"mappings":"AASA;;;;;;GAMG;AACH,MAAM,UAAU,OAAO,CAAC,QAAyB;IAC/C,IAAI,QAAQ,KAAK,IAAI;QAAE,OAAO,KAAK,CAAC;IACpC,IAAI,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS;QAAE,OAAO,KAAK,CAAC;IACpD,IAAI,QAAQ,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,MAAM,CAAC;IAC9C,IAAI,QAAQ,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,MAAM,CAAC;IAC9C,OAAO,QAAQ,CAAC;AAClB,CAAC;AAQD;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAoD;IAC3E,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IACrC,MAAM,GAAG,GAAG,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAC3C,MAAM,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC;IAC/C,KAAK,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,MAAM,EAAE,CAAC;QACtC,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC;YACpC,GAAG,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAE,EAAE,MAAM,CAAC,IAAI,CAAE,GAAG,IAAI,CAAC,IAAI,CAAE,GAAG,CAAC,CAAC,CAAC;YAClE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;;GAMG;AACH,MAAM,UAAU,MAAM,CAAC,MAA8B,EAAE,KAAa;IAClE,MAAM,MAAM,GAAG,KAAK,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,EAAE,CAAC,CAAC;IAC9E,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IACnC,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACtG,CAAC"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * `@biolibs/bionet-space`: where things are in a lab.
4
+ *
5
+ * A lab is a tree of places with things stored at the ends of it. This package says what that tree
6
+ * is, gives every place somewhere to stand, and answers the questions a view of a lab asks: what is
7
+ * inside what, where a position in a box is, what a search turns up, where a camera should look.
8
+ *
9
+ * It draws nothing and depends on nothing. It is the contract between whatever knows what a lab
10
+ * holds and whatever shows it, so a 3D scene, a flat grid of a box and a list can all be views of
11
+ * the same data.
12
+ *
13
+ * - `model.ts` — places, what is stored in them, and a place with somewhere to stand
14
+ * - `arrange.ts` — giving every place somewhere to stand, and the positions inside a box
15
+ * - `find.ts` — levels, bounds, camera poses, searching
16
+ * - `example.ts` — a lab to look at
17
+ */
18
+ export * from "./model.js";
19
+ export * from "./arrange.js";
20
+ export * from "./find.js";
21
+ export * from "./example.js";
22
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * `@biolibs/bionet-space`: where things are in a lab.
4
+ *
5
+ * A lab is a tree of places with things stored at the ends of it. This package says what that tree
6
+ * is, gives every place somewhere to stand, and answers the questions a view of a lab asks: what is
7
+ * inside what, where a position in a box is, what a search turns up, where a camera should look.
8
+ *
9
+ * It draws nothing and depends on nothing. It is the contract between whatever knows what a lab
10
+ * holds and whatever shows it, so a 3D scene, a flat grid of a box and a list can all be views of
11
+ * the same data.
12
+ *
13
+ * - `model.ts` — places, what is stored in them, and a place with somewhere to stand
14
+ * - `arrange.ts` — giving every place somewhere to stand, and the positions inside a box
15
+ * - `find.ts` — levels, bounds, camera poses, searching
16
+ * - `example.ts` — a lab to look at
17
+ */
18
+ export * from "./model.js";
19
+ export * from "./arrange.js";
20
+ export * from "./find.js";
21
+ export * from "./example.js";
22
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC"}
@@ -0,0 +1,101 @@
1
+ /**
2
+ * What a lab is made of, as far as finding things goes: places, and what is stored in them.
3
+ *
4
+ * A lab is a tree of places. A room holds a freezer, which holds shelves, which hold racks, which
5
+ * hold boxes. Nothing here says a tree must have those levels: a place has a kind and a parent, and
6
+ * that is all a place has to have. A liquid nitrogen dewar with canes in it is as much a lab as a
7
+ * freezer with racks.
8
+ *
9
+ * **Where a place is in the tree is required. Where it stands in the room is not.** A lab can say
10
+ * "this tube is in box 3, rack B, the −80" without anybody drawing a floor plan, and `arrange` will
11
+ * give every place somewhere sensible to stand. A place that does say where it stands is put there.
12
+ *
13
+ * Everything is plain data, so it can be made from signed entries, sent to another process, or
14
+ * written by hand.
15
+ */
16
+ /**
17
+ * The kinds of place this package knows how to arrange. Any other string is allowed and is treated
18
+ * as a plain container.
19
+ */
20
+ export type PlaceKind = "room" | "freezer" | "fridge" | "bench" | "instrument" | "shelf" | "rack" | "box";
21
+ /** A quarter turn clockwise, seen from above. */
22
+ export type Turn = 0 | 90 | 180 | 270;
23
+ /** Where a place stands and how big it is, in metres. */
24
+ export interface Standing {
25
+ /**
26
+ * The centre of its base, measured from the centre of its parent's base: across (x), up (y) and
27
+ * towards the front (z). A room has no parent and is measured from the lab's origin.
28
+ */
29
+ position: [number, number, number];
30
+ /** How wide (x), tall (y) and deep (z) it is, before it is turned. */
31
+ size: [number, number, number];
32
+ /** How far it is turned. Its front faces +z when it is not turned at all. */
33
+ turn?: Turn;
34
+ }
35
+ /** One place. */
36
+ export interface Place {
37
+ /** Its ID, which never changes. */
38
+ id: string;
39
+ /** What kind of place it is. One of `PlaceKind`, or anything else. */
40
+ kind: string;
41
+ /** What the lab calls it. */
42
+ name: string;
43
+ /** The place it is in, or `null` for a room. */
44
+ parent: string | null;
45
+ /** Where it stands, if anybody has said. Otherwise `arrange` decides. */
46
+ standing?: Standing;
47
+ /** For a box: how many rows and columns of positions it has. */
48
+ grid?: {
49
+ rows: number;
50
+ columns: number;
51
+ };
52
+ /** For an instrument: which one, like `thermocycler`. */
53
+ model?: string;
54
+ }
55
+ /** One position in a box. Rows and columns count from 0, from the back left. */
56
+ export interface Slot {
57
+ row: number;
58
+ column: number;
59
+ }
60
+ /** One physical item, and where it is. */
61
+ export interface Stored {
62
+ /** The item's ID. */
63
+ item: string;
64
+ /** The place it is in. */
65
+ place: string;
66
+ /** Its position there, if the place has positions and somebody said which. */
67
+ slot?: Slot | null;
68
+ /** The checksum of the part it is an item of. */
69
+ part: string;
70
+ /** What the part is called. */
71
+ name: string;
72
+ /** How much there is. */
73
+ amount: number;
74
+ /** What one of that is. */
75
+ unit: string | null;
76
+ /** Whether the log has lost track of it and it needs counting. */
77
+ needsRecount?: boolean;
78
+ }
79
+ /** A lab, as far as finding things goes. */
80
+ export interface Layout {
81
+ /** Every place. */
82
+ places: Place[];
83
+ /** Everything stored, and where. */
84
+ stored: Stored[];
85
+ }
86
+ /** A place with somewhere to stand worked out, in the lab's own coordinates. */
87
+ export interface Arranged {
88
+ /** The place. */
89
+ place: Place;
90
+ /** The centre of the space it takes up, in metres from the lab's origin. */
91
+ center: [number, number, number];
92
+ /** How wide (x), tall (y) and deep (z) that space is, after any turning. */
93
+ size: [number, number, number];
94
+ /** How far it is turned in all, counting everything it is inside. */
95
+ turn: Turn;
96
+ /** Every place it is inside, outermost first, ending with itself. */
97
+ path: string[];
98
+ /** Whether anybody said where it stands, or `arrange` chose. */
99
+ placed: "by hand" | "by default";
100
+ }
101
+ //# sourceMappingURL=model.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,SAAS,GAAG,QAAQ,GAAG,OAAO,GAAG,YAAY,GAAG,OAAO,GAAG,MAAM,GAAG,KAAK,CAAC;AAE1G,iDAAiD;AACjD,MAAM,MAAM,IAAI,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,GAAG,CAAC;AAEtC,yDAAyD;AACzD,MAAM,WAAW,QAAQ;IACvB;;;OAGG;IACH,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IACnC,sEAAsE;IACtE,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,6EAA6E;IAC7E,IAAI,CAAC,EAAE,IAAI,CAAC;CACb;AAED,iBAAiB;AACjB,MAAM,WAAW,KAAK;IACpB,mCAAmC;IACnC,EAAE,EAAE,M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A4E;IAC5E,IAAI,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,qEAAqE;IACrE,IAAI,EAAE,IAAI,CAAC;IACX,qEAAqE;IACrE,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,gEAAgE;IAChE,MAAM,EAAE,SAAS,GAAG,YAAY,CAAC;CAClC"}
package/dist/model.js ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ * What a lab is made of, as far as finding things goes: places, and what is stored in them.
3
+ *
4
+ * A lab is a tree of places. A room holds a freezer, which holds shelves, which hold racks, which
5
+ * hold boxes. Nothing here says a tree must have those levels: a place has a kind and a parent, and
6
+ * that is all a place has to have. A liquid nitrogen dewar with canes in it is as much a lab as a
7
+ * freezer with racks.
8
+ *
9
+ * **Where a place is in the tree is required. Where it stands in the room is not.** A lab can say
10
+ * "this tube is in box 3, rack B, the −80" without anybody drawing a floor plan, and `arrange` will
11
+ * give every place somewhere sensible to stand. A place that does say where it stands is put there.
12
+ *
13
+ * Everything is plain data, so it can be made from signed entries, sent to another process, or
14
+ * written by hand.
15
+ */
16
+ export {};
17
+ //# sourceMappingURL=model.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"model.js","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG"}
package/package.json CHANGED
@@ -1,6 +1,43 @@
1
1
  {
2
+ "bugs": {
3
+ "url": "https://github.com/matthewlovestocode/biolibs/issues"
4
+ },
5
+ "description": "Where things are in a lab: a tree of places, where each one stands and how big it is, the positions inside a box, and where a camera should look. Plain data and arithmetic, with nothing drawn",
6
+ "engines": {
7
+ "node": ">=20"
8
+ },
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "src"
18
+ ],
19
+ "homepage": "https://github.com/matthewlovestocode/biolibs/tree/main/packages/bionet-space#readme",
20
+ "keywords": [
21
+ "bionet",
22
+ "lab",
23
+ "layout",
24
+ "geometry",
25
+ "freezer",
26
+ "inventory",
27
+ "biology"
28
+ ],
29
+ "license": "MIT",
30
+ "main": "dist/index.js",
2
31
  "name": "@biolibs/bionet-space",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/matthewlovestocode/biolibs.git",
38
+ "directory": "packages/bionet-space"
39
+ },
40
+ "type": "module",
41
+ "types": "dist/index.d.ts",
42
+ "version": "0.1.0"
43
+ }
package/src/arrange.ts ADDED
@@ -0,0 +1,241 @@
1
+ /**
2
+ * Giving every place somewhere to stand.
3
+ *
4
+ * A place that says where it stands is put there. One that does not is arranged by what it is:
5
+ * shelves split a freezer's height between them, racks share a shelf side by side, boxes stack in a
6
+ * rack. So a lab that has only ever said what is inside what still has a shape that can be drawn,
7
+ * and one that has been measured is drawn as measured.
8
+ *
9
+ * The result is in the lab's own coordinates, with turning already worked in. Every place comes out
10
+ * as a box with its sides along the axes, because places turn only by quarter turns — which is what
11
+ * keeps this arithmetic and not geometry.
12
+ */
13
+ import type { Arranged, Layout, Place, Slot, Standing, Turn } from "./model.js";
14
+
15
+ /** How big something is when nobody has said, by kind, in metres: wide, tall, deep. */
16
+ export const DEFAULT_SIZE: Readonly<Record<string, readonly [number, number, number]>> = {
17
+ room: [6, 2.8, 5],
18
+ freezer: [0.9, 2.0, 0.85],
19
+ fridge: [0.7, 1.8, 0.7],
20
+ bench: [1.8, 0.9, 0.75],
21
+ instrument: [0.4, 0.3, 0.4],
22
+ box: [0.134, 0.052, 0.134],
23
+ };
24
+
25
+ /** How big an instrument is by model, where that differs from the default. */
26
+ export const INSTRUMENT_SIZE: Readonly<Record<string, readonly [number, number, number]>> = {
27
+ thermocycler: [0.35, 0.25, 0.45],
28
+ "gel-station": [0.45, 0.18, 0.3],
29
+ centrifuge: [0.4, 0.3, 0.45],
30
+ computer: [0.5, 0.4, 0.25],
31
+ microscope: [0.3, 0.45, 0.4],
32
+ };
33
+
34
+ /** How much of a place's inside is kept clear of its walls, as a fraction of each side. */
35
+ const INSET = 0.06;
36
+ /** The gap left between things arranged side by side, in metres. */
37
+ const GAP = 0.02;
38
+
39
+ const sizeOf = (place: Place): [number, number, number] => {
40
+ if (place.standing !== undefined) return [...place.standing.size];
41
+ const size = (place.kind === "instrument" && place.model !== undefined ? INSTRUMENT_SIZE[place.model] : undefined)
42
+ ?? DEFAULT_SIZE[place.kind] ?? [0.5, 0.5, 0.5];
43
+ return [...size];
44
+ };
45
+
46
+ /** Turn an offset measured in a turned place into the lab's directions. */
47
+ function turned([x, y, z]: [number, number, number], turn: Turn): [number, number, number] {
48
+ switch (turn) {
49
+ case 90: return [-z, y, x];
50
+ case 180: return [-x, y, -z];
51
+ case 270: return [z, y, -x];
52
+ default: return [x, y, z];
53
+ }
54
+ }
55
+
56
+ /** A size as it lies once turned: a quarter turn swaps wide and deep. */
57
+ const lying = ([w, h, d]: [number, number, number], turn: Turn): [number, number, number] =>
58
+ (turn === 90 || turn === 270 ? [d, h, w] : [w, h, d]);
59
+
60
+ /**
61
+ * Where each child stands inside a parent that has not been told, measured from the centre of the
62
+ * parent's base, with the parent facing front.
63
+ *
64
+ * @param parent - The size of the parent: wide, tall, deep.
65
+ * @param kind - What the parent is.
66
+ * @param children - The children with no standing of their own, in order.
67
+ * @returns A standing for each, in the same order.
68
+ */
69
+ function byDefault(parent: [number, number, number], kind: string, children: Place[]): Standing[] {
70
+ const [W, H, D] = parent;
71
+ const inside: [number, number, number] = [W * (1 - 2 * INSET), H * (1 - 2 * INSET), D * (1 - 2 * INSET)];
72
+ const floor = H * INSET;
73
+ const count = children.length;
74
+ const sample = children[0]!.kind;
75
+
76
+ if (sample === "shelf") {
77
+ // Shelves split the height between them, the first at the top.
78
+ const each = inside[1] / count;
79
+ return children.map((_, at) => ({ position: [0, floor + (count - 1 - at) * each, 0], size: [inside[0], each - GAP, inside[2]] }));
80
+ }
81
+ if (sample === "box") {
82
+ // Boxes keep their own size and stack: upwards first, then across, then towards the back.
83
+ const [w, h, d] = sizeOf(children[0]!);
84
+ const high = Math.max(1, Math.floor((inside[1] + GAP) / (h + GAP)));
85
+ const across = Math.max(1, Math.floor((inside[0] + GAP) / (w + GAP)));
86
+ return children.map((_, at) => {
87
+ const level = at % high;
88
+ const column = Math.floor(at / high) % across;
89
+ const back = Math.floor(at / (high * across));
90
+ return {
91
+ position: [-inside[0] / 2 + w / 2 + column * (w + GAP), floor + level * (h + GAP), inside[2] / 2 - d / 2 - back * (d + GAP)],
92
+ size: [w, h, d],
93
+ };
94
+ });
95
+ }
96
+ if (kind === "room") {
97
+ // Whatever stands on a room's floor lines up along the back wall, at its own size.
98
+ const sizes = children.map(sizeOf);
99
+ const total = sizes.reduce((sum, size) => sum + size[0], 0) + 0.4 * (count - 1);
100
+ let x = -total / 2;
101
+ return sizes.map((size) => {
102
+ const standing: Standing = { position: [x + size[0] / 2, 0, -D / 2 + size[2] / 2 + 0.1], size };
103
+ x += size[0] + 0.4;
104
+ return standing;
105
+ });
106
+ }
107
+ if (kind === "bench") {
108
+ // Things on a bench stand on its top, spread along it.
109
+ const sizes = children.map(sizeOf);
110
+ const step = W / count;
111
+ return sizes.map((size, at) => ({ position: [-W / 2 + step * (at + 0.5), H, 0], size }));
112
+ }
113
+ // Anything else shares the width side by side: racks on a shelf, or whatever a lab has invented.
114
+ const each = (inside[0] - GAP * (count - 1)) / count;
115
+ return children.map((_, at) => ({ position: [-inside[0] / 2 + each / 2 + at * (each + GAP), floor, 0], size: [each, inside[1], inside[2]] }));
116
+ }
117
+
118
+ /**
119
+ * Give every place somewhere to stand.
120
+ *
121
+ * @param layout - The lab. Only its places are used.
122
+ * @returns Every place, with the space it takes up in the lab's coordinates, in an order where a
123
+ * place always comes after the place it is in. A place whose parent does not exist is left out.
124
+ *
125
+ * @example
126
+ * ```ts
127
+ * import { arrange, exampleLab } from "@biolibs/bionet-space";
128
+ *
129
+ * const arranged = arrange(exampleLab());
130
+ * arranged.find((one) => one.place.kind === "freezer")?.size; // [ 0.9, 2, 0.85 ]
131
+ * ```
132
+ */
133
+ export function arrange(layout: Pick<Layout, "places">): Arranged[] {
134
+ const children = new Map<string | null, Place[]>();
135
+ for (const place of layout.places) children.set(place.parent, [...(children.get(place.parent) ?? []), place]);
136
+ const out: Arranged[] = [];
137
+
138
+ /**
139
+ * Put `places` inside a parent whose base is centred at `base`, with size `size` and turn `turn`.
140
+ * `given` holds standings already chosen for places that did not say where they stand.
141
+ */
142
+ const settle = (places: Place[], base: [number, number, number], size: [number, number, number], kind: string, turn: Turn,
143
+ path: string[], given: Map<Place, Standing> = new Map()): void => {
144
+ const chosen = new Map(given);
145
+ const unplaced = places.filter((place) => place.standing === undefined && !chosen.has(place));
146
+ // Shelves and boxes each have a rule of their own. Everything else in the same place is arranged
147
+ // together, so a freezer and a fridge in one room stand side by side and not on top of each other.
148
+ const sort = (place: Place): string => (place.kind === "shelf" || place.kind === "box" ? place.kind : "other");
149
+ for (const which of new Set(unplaced.map(sort))) {
150
+ const group = unplaced.filter((place) => sort(place) === which);
151
+ byDefault(size, kind, group).forEach((standing, at) => chosen.set(group[at]!, standing));
152
+ }
153
+ for (const place of places) {
154
+ const standing = place.standing ?? chosen.get(place)!;
155
+ const whole = ((turn + (standing.turn ?? 0)) % 360) as Turn;
156
+ const offset = turned(standing.position, turn);
157
+ const at: [number, number, number] = [base[0] + offset[0], base[1] + offset[1], base[2] + offset[2]];
158
+ const space = lying([...standing.size], whole);
159
+ const here = [...path, place.id];
160
+ out.push({ place, center: [at[0], at[1] + space[1] / 2, at[2]], size: space, turn: whole, path: here,
161
+ placed: place.standing === undefined ? "by default" : "by hand" });
162
+ const inside = children.get(place.id);
163
+ if (inside !== undefined) settle(inside, at, [...standing.size], place.kind, whole, here);
164
+ }
165
+ };
166
+
167
+ // Rooms that have not been told where to stand line up in a row, a little apart.
168
+ const rooms = children.get(null) ?? [];
169
+ const row = new Map<Place, Standing>();
170
+ let x = 0;
171
+ for (const room of rooms) {
172
+ if (room.standing !== undefined) continue;
173
+ const size = sizeOf(room);
174
+ row.set(room, { position: [x + size[0] / 2, 0, 0], size });
175
+ x += size[0] + 1.5;
176
+ }
177
+ settle(rooms, [0, 0, 0], [0, 0, 0], "lab", 0, [], row);
178
+ return out;
179
+ }
180
+
181
+ /**
182
+ * Where one position in a box is, in the lab's coordinates.
183
+ *
184
+ * @param box - The box, arranged.
185
+ * @param slot - Which position.
186
+ * @returns The centre of that position on the floor of the box, or `null` if the box has no such position.
187
+ */
188
+ export function slotCenter(box: Arranged, slot: Slot): [number, number, number] | null {
189
+ const grid = box.place.grid;
190
+ if (grid === undefined || slot.row < 0 || slot.column < 0 || slot.row >= grid.rows || slot.column >= grid.columns) return null;
191
+ // Rows run from the back to the front and columns from left to right, in the box's own facing.
192
+ const [w, , d] = lying([...box.size], box.turn); // undo the turn to get the box's own wide and deep
193
+ const across = -w / 2 + (w / grid.columns) * (slot.column + 0.5);
194
+ const forward = -d / 2 + (d / grid.rows) * (slot.row + 0.5);
195
+ const offset = turned([across, 0, forward], box.turn);
196
+ return [box.center[0] + offset[0], box.center[1] - box.size[1] / 2, box.center[2] + offset[2]];
197
+ }
198
+
199
+ /**
200
+ * How big one position in a box is.
201
+ *
202
+ * @param box - The box, arranged.
203
+ * @returns The side of the square each position gets, in metres, or 0 if the box has no positions.
204
+ */
205
+ export function slotSize(box: Arranged): number {
206
+ const grid = box.place.grid;
207
+ if (grid === undefined) return 0;
208
+ const [w, , d] = lying([...box.size], box.turn);
209
+ return Math.min(w / grid.columns, d / grid.rows);
210
+ }
211
+
212
+ /**
213
+ * The first position in a box that nothing is stored in.
214
+ *
215
+ * @param box - The box.
216
+ * @param taken - The positions already used.
217
+ * @returns A free position, row by row from the back left, or `null` if the box is full or has none.
218
+ */
219
+ export function firstFreeSlot(box: Place, taken: readonly Slot[]): Slot | null {
220
+ if (box.grid === undefined) return null;
221
+ const used = new Set(taken.map((slot) => `${slot.row},${slot.column}`));
222
+ for (let row = 0; row < box.grid.rows; row++) {
223
+ for (let column = 0; column < box.grid.columns; column++) {
224
+ if (!used.has(`${row},${column}`)) return { row, column };
225
+ }
226
+ }
227
+ return null;
228
+ }
229
+
230
+ /**
231
+ * What a position is called, the way it is written on a box lid: a letter for the row, a number for
232
+ * the column.
233
+ *
234
+ * @param slot - The position.
235
+ * @returns Something like `C4`.
236
+ */
237
+ export function slotName(slot: Slot): string {
238
+ let row = "";
239
+ for (let at = slot.row; at >= 0; at = Math.floor(at / 26) - 1) row = String.fromCharCode(65 + (at % 26)) + row;
240
+ return `${row}${slot.column + 1}`;
241
+ }