@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/src/example.ts ADDED
@@ -0,0 +1,95 @@
1
+ /**
2
+ * A lab to look at: one room, a −80 freezer full of boxes, a fridge, two benches and their
3
+ * instruments.
4
+ *
5
+ * It is here so that anything that draws a lab has one to draw before a real one exists, and so that
6
+ * tests have something of a realistic size. The room, the freezer, the fridge and the benches say
7
+ * where they stand; the shelves, racks and boxes do not, and are arranged for them — which is the
8
+ * mix a real lab will have.
9
+ *
10
+ * It is the same every time it is made. The tubes are scattered by a fixed sequence, not by chance.
11
+ */
12
+ import type { Layout, Place, Stored } from "./model.js";
13
+
14
+ /** What the example freezer is stocked with. The first seven are the parts the Wren Lab publishes. */
15
+ const PARTS = ["f1", "f2", "acceptor", "part1", "part2", "template", "backbone", "pUC19", "pET-28a", "pBR322", "pGEX-4T-1",
16
+ "GFP insert", "mCherry insert", "lacZα", "T7 promoter", "Cas9", "sgRNA scaffold", "AmpR cassette", "KanR cassette",
17
+ "pSB1C3", "RFP reporter"];
18
+
19
+ /** A sequence of numbers between 0 and 1 that is the same every time. */
20
+ function sequence(seed: number): () => number {
21
+ let state = seed >>> 0;
22
+ return () => {
23
+ state = (state + 0x6d2b79f5) >>> 0;
24
+ let mixed = Math.imul(state ^ (state >>> 15), 1 | state);
25
+ mixed = (mixed + Math.imul(mixed ^ (mixed >>> 7), 61 | mixed)) ^ mixed;
26
+ return ((mixed ^ (mixed >>> 14)) >>> 0) / 4294967296;
27
+ };
28
+ }
29
+
30
+ /**
31
+ * Make the example lab.
32
+ *
33
+ * @returns Its places and what is stored in them.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { arrange, exampleLab, search } from "@biolibs/bionet-space";
38
+ *
39
+ * const lab = exampleLab();
40
+ * lab.places.filter((place) => place.kind === "box").length; // 72
41
+ * search(lab, "puc19").length > 0; // true
42
+ * arrange(lab).length === lab.places.length; // true: everything has somewhere to stand
43
+ * ```
44
+ */
45
+ export function exampleLab(): Layout {
46
+ const next = sequence(20261115);
47
+ const places: Place[] = [
48
+ { id: "main", kind: "room", name: "Main lab", parent: null, standing: { position: [0, 0, 0], size: [7, 2.8, 5] } },
49
+ { id: "minus80", kind: "freezer", name: "−80 freezer", parent: "main", standing: { position: [-2.7, 0, -1.975], size: [0.9, 2.0, 0.85] } },
50
+ { id: "fridge", kind: "fridge", name: "4 °C fridge", parent: "main", standing: { position: [-1.6, 0, -2.05], size: [0.7, 1.8, 0.7] } },
51
+ { id: "bench1", kind: "bench", name: "Bench 1", parent: "main", standing: { position: [0.7, 0, -2.0], size: [2.4, 0.9, 0.75] } },
52
+ { id: "bench2", kind: "bench", name: "Bench 2", parent: "main", standing: { position: [3.0, 0, 0.3], size: [2.2, 0.9, 0.75], turn: 90 } },
53
+ { id: "thermocycler", kind: "instrument", model: "thermocycler", name: "Thermocycler", parent: "bench1" },
54
+ { id: "gel", kind: "instrument", model: "gel-station", name: "Gel station", parent: "bench1" },
55
+ { id: "centrifuge", kind: "instrument", model: "centrifuge", name: "Centrifuge", parent: "bench1" },
56
+ { id: "computer", kind: "instrument", model: "computer", name: "Computer", parent: "bench2" },
57
+ { id: "microscope", kind: "instrument", model: "microscope", name: "Microscope", parent: "bench2" },
58
+ ];
59
+ const stored: Stored[] = [];
60
+ let tubes = 0;
61
+
62
+ /** Add a box, and scatter tubes in it. */
63
+ const box = (id: string, name: string, parent: string, rows: number, columns: number, fullness: number): void => {
64
+ places.push({ id, kind: "box", name, parent, grid: { rows, columns } });
65
+ for (let row = 0; row < rows; row++) {
66
+ for (let column = 0; column < columns; column++) {
67
+ if (next() >= fullness) continue;
68
+ const part = PARTS[Math.floor(next() * PARTS.length)]!;
69
+ tubes++;
70
+ stored.push({ item: `tube-${String(tubes).padStart(4, "0")}`, place: id, slot: { row, column }, part: `example:${part}`, name: part,
71
+ amount: 5 + Math.floor(next() * 46), unit: "µL", ...(next() < 0.012 ? { needsRecount: true } : {}) });
72
+ }
73
+ }
74
+ };
75
+
76
+ // The freezer: four shelves, three racks on each, five boxes of 81 in each rack.
77
+ for (let shelf = 1; shelf <= 4; shelf++) {
78
+ places.push({ id: `minus80-s${shelf}`, kind: "shelf", name: `Shelf ${shelf}`, parent: "minus80" });
79
+ for (const rack of ["A", "B", "C"]) {
80
+ const rackId = `minus80-s${shelf}-${rack}`;
81
+ places.push({ id: rackId, kind: "rack", name: `Rack ${shelf}${rack}`, parent: `minus80-s${shelf}` });
82
+ for (let at = 1; at <= 5; at++) {
83
+ // Some boxes are nearly full, some nearly empty, and a few have nothing in them yet.
84
+ const fullness = next() < 0.15 ? 0 : 0.15 + next() * 0.8;
85
+ box(`${rackId}-${at}`, `Box ${shelf}${rack}${at}`, rackId, 9, 9, fullness);
86
+ }
87
+ }
88
+ }
89
+ // The fridge: three shelves with four boxes of 25 standing on each.
90
+ for (let shelf = 1; shelf <= 3; shelf++) {
91
+ places.push({ id: `fridge-s${shelf}`, kind: "shelf", name: `Shelf ${shelf}`, parent: "fridge" });
92
+ for (let at = 1; at <= 4; at++) box(`fridge-s${shelf}-${at}`, `Box F${shelf}-${at}`, `fridge-s${shelf}`, 5, 5, 0.2 + next() * 0.6);
93
+ }
94
+ return { places, stored };
95
+ }
package/src/find.ts ADDED
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Finding your way around an arranged lab: what is inside what, what a search turns up, and where a
3
+ * camera should look to see a place.
4
+ */
5
+ import type { Arranged, Layout, Stored } from "./model.js";
6
+
7
+ /** How close a place is to the things in it: a room, something standing in one, or a box of positions. */
8
+ export type Level = "lab" | "room" | "unit" | "inside" | "box";
9
+
10
+ /**
11
+ * How close a look at a place is.
12
+ *
13
+ * @param arranged - The place, or `null` for the whole lab.
14
+ * @returns `lab` for everything, `room` for a room, `unit` for whatever stands in a room (a freezer, a
15
+ * bench, an instrument), `box` for a place with positions, and `inside` for anything between.
16
+ */
17
+ export function levelOf(arranged: Arranged | null): Level {
18
+ if (arranged === null) return "lab";
19
+ if (arranged.place.grid !== undefined) return "box";
20
+ if (arranged.path.length === 1) return "room";
21
+ if (arranged.path.length === 2) return "unit";
22
+ return "inside";
23
+ }
24
+
25
+ /** A box that everything fits in. */
26
+ export interface Bounds {
27
+ center: [number, number, number];
28
+ size: [number, number, number];
29
+ }
30
+
31
+ /**
32
+ * The space some places take up together.
33
+ *
34
+ * @param places - The places.
35
+ * @returns The smallest box around all of them, or `null` if there are none.
36
+ */
37
+ export function boundsOf(places: readonly Pick<Arranged, "center" | "size">[]): Bounds | null {
38
+ if (places.length === 0) return null;
39
+ const low = [Infinity, Infinity, Infinity];
40
+ const high = [-Infinity, -Infinity, -Infinity];
41
+ for (const { center, size } of places) {
42
+ for (let axis = 0; axis < 3; axis++) {
43
+ low[axis] = Math.min(low[axis]!, center[axis]! - size[axis]! / 2);
44
+ high[axis] = Math.max(high[axis]!, center[axis]! + size[axis]! / 2);
45
+ }
46
+ }
47
+ return {
48
+ center: [(low[0]! + high[0]!) / 2, (low[1]! + high[1]!) / 2, (low[2]! + high[2]!) / 2],
49
+ size: [high[0]! - low[0]!, high[1]! - low[1]!, high[2]! - low[2]!],
50
+ };
51
+ }
52
+
53
+ /** Where a camera stands and what it looks at. */
54
+ export interface Pose {
55
+ /** Where the camera is. */
56
+ position: [number, number, number];
57
+ /** What it looks at. */
58
+ target: [number, number, number];
59
+ }
60
+
61
+ /** Which way to look from, by level, for something facing front: across, up, towards the viewer. */
62
+ const FROM: Readonly<Record<Level, readonly [number, number, number]>> = {
63
+ lab: [0.45, 0.62, 1],
64
+ room: [0.4, 0.6, 1],
65
+ unit: [0.42, 0.28, 1],
66
+ inside: [0.3, 0.2, 1],
67
+ box: [0, 1, 0.42],
68
+ };
69
+
70
+ /**
71
+ * Where a camera should stand to see a place whole.
72
+ *
73
+ * It looks from in front and a little above, closer the smaller the thing is, and from nearly
74
+ * straight above for a box, since a box is read like a page. A place that is turned is looked at
75
+ * from its own front.
76
+ *
77
+ * @param arranged - Every place, arranged.
78
+ * @param id - The place to look at, or `null` for the whole lab.
79
+ * @param fov - The camera's vertical field of view, in degrees.
80
+ * @returns Where to stand and what to look at.
81
+ */
82
+ export function poseFor(arranged: readonly Arranged[], id: string | null, fov = 45): Pose {
83
+ const one = id === null ? null : arranged.find((each) => each.place.id === id) ?? null;
84
+ const bounds = one ?? boundsOf(arranged.filter((each) => each.place.parent === null)) ?? { center: [0, 0, 0], size: [4, 3, 4] };
85
+ const level = levelOf(one);
86
+ const [x, y, z] = FROM[level];
87
+ // Turn the direction with the place, so a freezer facing a side wall is still seen from its front.
88
+ const turn = one?.turn ?? 0;
89
+ const [dx, dz] = turn === 90 ? [-z, x] : turn === 180 ? [-x, -z] : turn === 270 ? [z, -x] : [x, z];
90
+ const length = Math.hypot(dx, y, dz);
91
+ const reach = Math.max(bounds.size[0], bounds.size[1], bounds.size[2]);
92
+ const distance = (reach * (level === "lab" || level === "room" ? 0.68 : level === "box" ? 1.25 : 1.0)) / Math.tan((fov * Math.PI) / 360);
93
+ return {
94
+ target: [...bounds.center] as [number, number, number],
95
+ position: [bounds.center[0] + (dx / length) * distance, bounds.center[1] + (y / length) * distance, bounds.center[2] + (dz / length) * distance],
96
+ };
97
+ }
98
+
99
+ /**
100
+ * Everything stored in a place or in anything inside it.
101
+ *
102
+ * @param layout - The lab.
103
+ * @param arranged - Its places, arranged.
104
+ * @param id - The place.
105
+ * @returns What is stored there, however deep.
106
+ */
107
+ export function storedWithin(layout: Pick<Layout, "stored">, arranged: readonly Arranged[], id: string): Stored[] {
108
+ const inside = new Set(arranged.filter((one) => one.path.includes(id)).map((one) => one.place.id));
109
+ return layout.stored.filter((one) => inside.has(one.place));
110
+ }
111
+
112
+ /**
113
+ * Search what is stored, by the name of the part.
114
+ *
115
+ * @param layout - The lab.
116
+ * @param words - What was typed. Case does not matter, and every word has to appear.
117
+ * @returns What matched, in the order it is stored. Nothing, if nothing was typed.
118
+ */
119
+ export function search(layout: Pick<Layout, "stored">, words: string): Stored[] {
120
+ const wanted = words.toLowerCase().split(/\s+/).filter((word) => word !== "");
121
+ if (wanted.length === 0) return [];
122
+ return layout.stored.filter((one) => wanted.every((word) => one.name.toLowerCase().includes(word)));
123
+ }
package/src/index.ts ADDED
@@ -0,0 +1,21 @@
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";
package/src/model.ts ADDED
@@ -0,0 +1,105 @@
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
+ /**
18
+ * The kinds of place this package knows how to arrange. Any other string is allowed and is treated
19
+ * as a plain container.
20
+ */
21
+ export type PlaceKind = "room" | "freezer" | "fridge" | "bench" | "instrument" | "shelf" | "rack" | "box";
22
+
23
+ /** A quarter turn clockwise, seen from above. */
24
+ export type Turn = 0 | 90 | 180 | 270;
25
+
26
+ /** Where a place stands and how big it is, in metres. */
27
+ export interface Standing {
28
+ /**
29
+ * The centre of its base, measured from the centre of its parent's base: across (x), up (y) and
30
+ * towards the front (z). A room has no parent and is measured from the lab's origin.
31
+ */
32
+ position: [number, number, number];
33
+ /** How wide (x), tall (y) and deep (z) it is, before it is turned. */
34
+ size: [number, number, number];
35
+ /** How far it is turned. Its front faces +z when it is not turned at all. */
36
+ turn?: Turn;
37
+ }
38
+
39
+ /** One place. */
40
+ export interface Place {
41
+ /** Its ID, which never changes. */
42
+ id: string;
43
+ /** What kind of place it is. One of `PlaceKind`, or anything else. */
44
+ kind: string;
45
+ /** What the lab calls it. */
46
+ name: string;
47
+ /** The place it is in, or `null` for a room. */
48
+ parent: string | null;
49
+ /** Where it stands, if anybody has said. Otherwise `arrange` decides. */
50
+ standing?: Standing;
51
+ /** For a box: how many rows and columns of positions it has. */
52
+ grid?: { rows: number; columns: number };
53
+ /** For an instrument: which one, like `thermocycler`. */
54
+ model?: string;
55
+ }
56
+
57
+ /** One position in a box. Rows and columns count from 0, from the back left. */
58
+ export interface Slot {
59
+ row: number;
60
+ column: number;
61
+ }
62
+
63
+ /** One physical item, and where it is. */
64
+ export interface Stored {
65
+ /** The item's ID. */
66
+ item: string;
67
+ /** The place it is in. */
68
+ place: string;
69
+ /** Its position there, if the place has positions and somebody said which. */
70
+ slot?: Slot | null;
71
+ /** The checksum of the part it is an item of. */
72
+ part: string;
73
+ /** What the part is called. */
74
+ name: string;
75
+ /** How much there is. */
76
+ amount: number;
77
+ /** What one of that is. */
78
+ unit: string | null;
79
+ /** Whether the log has lost track of it and it needs counting. */
80
+ needsRecount?: boolean;
81
+ }
82
+
83
+ /** A lab, as far as finding things goes. */
84
+ export interface Layout {
85
+ /** Every place. */
86
+ places: Place[];
87
+ /** Everything stored, and where. */
88
+ stored: Stored[];
89
+ }
90
+
91
+ /** A place with somewhere to stand worked out, in the lab's own coordinates. */
92
+ export interface Arranged {
93
+ /** The place. */
94
+ place: Place;
95
+ /** The centre of the space it takes up, in metres from the lab's origin. */
96
+ center: [number, number, number];
97
+ /** How wide (x), tall (y) and deep (z) that space is, after any turning. */
98
+ size: [number, number, number];
99
+ /** How far it is turned in all, counting everything it is inside. */
100
+ turn: Turn;
101
+ /** Every place it is inside, outermost first, ending with itself. */
102
+ path: string[];
103
+ /** Whether anybody said where it stands, or `arrange` chose. */
104
+ placed: "by hand" | "by default";
105
+ }