@arkitektio/bsx 3.0.0 → 4.0.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.
Files changed (74) hide show
  1. package/dist/bsx/language.d.ts.map +1 -1
  2. package/dist/bsx/language.js +4 -3
  3. package/dist/bsx/language.js.map +1 -1
  4. package/dist/bsx/references.d.ts +4 -2
  5. package/dist/bsx/references.d.ts.map +1 -1
  6. package/dist/bsx/references.js +34 -6
  7. package/dist/bsx/references.js.map +1 -1
  8. package/dist/catalogs/index.d.ts +27 -14
  9. package/dist/catalogs/index.d.ts.map +1 -1
  10. package/dist/catalogs/parts/index.d.ts +16 -3
  11. package/dist/catalogs/parts/index.d.ts.map +1 -1
  12. package/dist/catalogs/parts/run.d.ts +44 -4
  13. package/dist/catalogs/parts/run.d.ts.map +1 -1
  14. package/dist/catalogs/parts/run.js +33 -19
  15. package/dist/catalogs/parts/run.js.map +1 -1
  16. package/dist/catalogs/parts/scene.js +7 -7
  17. package/dist/catalogs/parts/scene.js.map +1 -1
  18. package/dist/catalogs/scene.d.ts +27 -14
  19. package/dist/catalogs/scene.d.ts.map +1 -1
  20. package/dist/catalogs/scene.js +3 -3
  21. package/dist/catalogs/scene.js.map +1 -1
  22. package/dist/renderer/bloks/run/components.d.ts +15 -2
  23. package/dist/renderer/bloks/run/components.d.ts.map +1 -1
  24. package/dist/renderer/bloks/run/components.js +114 -43
  25. package/dist/renderer/bloks/run/components.js.map +1 -1
  26. package/dist/renderer/bloks/run/host.d.ts +27 -8
  27. package/dist/renderer/bloks/run/host.d.ts.map +1 -1
  28. package/dist/renderer/bloks/run/host.js +8 -4
  29. package/dist/renderer/bloks/run/host.js.map +1 -1
  30. package/dist/renderer/bloks/run/index.d.ts +1 -1
  31. package/dist/renderer/bloks/run/index.d.ts.map +1 -1
  32. package/dist/renderer/bloks/run/index.js +1 -1
  33. package/dist/renderer/bloks/run/index.js.map +1 -1
  34. package/dist/renderer/bloks/run/math.d.ts +3 -20
  35. package/dist/renderer/bloks/run/math.d.ts.map +1 -1
  36. package/dist/renderer/bloks/run/math.js +4 -15
  37. package/dist/renderer/bloks/run/math.js.map +1 -1
  38. package/dist/renderer/bloks/scene/Scene.d.ts +16 -2
  39. package/dist/renderer/bloks/scene/Scene.d.ts.map +1 -1
  40. package/dist/renderer/bloks/scene/Scene.js +11 -3
  41. package/dist/renderer/bloks/scene/Scene.js.map +1 -1
  42. package/dist/scene.js +2 -2
  43. package/dist/scene.js.map +1 -1
  44. package/dist/spec/bsx_v2.json +277 -62
  45. package/dist/spec/catalogs/scene@1.json +93 -41
  46. package/dist/spec/catalogs/scene@1.json.js +93 -41
  47. package/dist/spec/catalogs.js +1 -1
  48. package/dist/spec/catalogs.js.map +1 -1
  49. package/dist/spec/vocabulary.d.ts +1 -1
  50. package/dist/spec/vocabulary.d.ts.map +1 -1
  51. package/dist/spec/vocabulary.js +1 -1
  52. package/dist/spec/vocabulary.js.map +1 -1
  53. package/dist/spec/vocabulary.json +4 -4
  54. package/dist/spec/vocabulary.json.js +4 -4
  55. package/package.json +1 -1
  56. package/src/bsx/language.test.ts +1 -0
  57. package/src/bsx/language.ts +4 -3
  58. package/src/bsx/references.ts +42 -7
  59. package/src/catalogs/parts/run.ts +39 -19
  60. package/src/catalogs/parts/scene.ts +7 -7
  61. package/src/catalogs/scene.ts +3 -3
  62. package/src/renderer/bloks/run/components.tsx +227 -103
  63. package/src/renderer/bloks/run/host.tsx +38 -12
  64. package/src/renderer/bloks/run/index.ts +1 -1
  65. package/src/renderer/bloks/run/math.test.ts +1 -24
  66. package/src/renderer/bloks/run/math.ts +5 -33
  67. package/src/renderer/bloks/run/run.test.tsx +44 -38
  68. package/src/renderer/bloks/scene/Scene.tsx +22 -4
  69. package/src/scene.ts +2 -2
  70. package/src/spec/bsx_v2.json +277 -62
  71. package/src/spec/catalogs/scene@1.json +93 -41
  72. package/src/spec/catalogs.ts +1 -1
  73. package/src/spec/vocabulary.json +4 -4
  74. package/src/spec/vocabulary.ts +2 -2
@@ -1,21 +1,25 @@
1
1
  import * as React from 'react';
2
2
  import * as THREE from 'three';
3
- import {Line} from '@react-three/drei';
4
- import {anchorSchema, routeSchema, type runContracts} from '../../../catalogs/parts/run';
3
+ import {Line, TransformControls} from '@react-three/drei';
4
+ import {Move3d} from 'lucide-react';
5
+ import {placedSchema, routeSchema, spaceSchema, type runContracts} from '../../../catalogs/parts/run';
6
+ import {fromMatrix4, toMatrix4} from '../../../vendor/core/model/affine';
7
+ import {Button} from '../../../vendor/core/ui/button';
5
8
  import {useBlok, useValue, type BlokRender, type BlokRenderArgs} from '../../runtime';
6
- import {ModelProvider, useOwnedModel, useSceneNotice, useSceneOverlay, type ModelContextValue} from '../scene/context';
9
+ import {ModelProvider, SceneOwnerProvider, useOwnedModel, useSceneNotice, type ModelContextValue} from '../scene/context';
7
10
  import {FALLBACK_SCENE_RGB, useThemeColour} from '../scene/colour';
8
- import {useBlokSceneEmbedded} from '../scene/host';
9
- import {BlokTrayProvider, useBlokRunHost, useBlokSpaceHost, type BlokRunAgent} from './host';
10
- import {progressFraction, resolveAnchors, routePoints, splitRoute, throughKeys} from './math';
11
+ import {BlokSceneInertProvider, useBlokSceneEmbedded, useBlokSceneStill} from '../scene/host';
12
+ import {SceneFrame} from '../scene/Scene';
13
+ import {useBlokRunHost, useBlokSpaceHost, type BlokPose, type BlokRoom, type BlokRunAgent} from './host';
14
+ import {progressFraction, routePoints, splitRoute, throughKeys} from './math';
11
15
 
12
16
  type Contracts = typeof runContracts;
13
17
 
14
18
  /**
15
- * The components only an action's own blok holds: they read the run it is
16
- * shown for (scene@1). `Overlay` is the root of one drawn into the space
17
- * hosting the run, `Anchor` anchors at the agent a dependency is bound to,
18
- * and `Route` draws a line through several.
19
+ * The components that place an action's dependencies in 3D (scene@1). `Space`
20
+ * is a scene whose layout is a shared room, `Placed` draws the agent a
21
+ * dependency is bound to with that agent's own scene blok where the room keeps
22
+ * it, and `Route` draws a line through several.
19
23
  */
20
24
 
21
25
  const Children = ({component, buildChild}: Pick<BlokRenderArgs, 'component' | 'buildChild'>) => (
@@ -26,14 +30,97 @@ const Children = ({component, buildChild}: Pick<BlokRenderArgs, 'component' | 'b
26
30
  </>
27
31
  );
28
32
 
29
- const OverlayRender: BlokRender<Contracts['Overlay']> = ({component, buildChild}) =>
30
- useBlokSceneEmbedded() ? (
31
- <Children component={component} buildChild={buildChild} />
32
- ) : (
33
- <div className="flex min-h-24 items-center justify-center rounded-xl border border-dashed border-border/70 bg-muted/30 px-6 text-center text-sm text-muted-foreground">
34
- An overlay is drawn into the space that hosts a run of its action. Open the run in a space to see it.
35
- </div>
33
+ /* -------------------------------------------------------------------------- */
34
+ /* The room */
35
+ /* -------------------------------------------------------------------------- */
36
+
37
+ /** What a `Space` tells what it holds: the room, whether it is being arranged, and where each device stands now. */
38
+ type RoomContextValue = {
39
+ room: BlokRoom;
40
+ /** Whether the viewer is arranging the room: devices then move by gizmo, and clicks do not reach them. */
41
+ arranging: boolean;
42
+ /** Where each placed agent stands now (its kept pose, else its `Placed`'s): what a `Route` runs through. */
43
+ standing: ReadonlyMap<string, BlokPose>;
44
+ stand: (agentId: string, pose: BlokPose | null) => void;
45
+ };
46
+
47
+ const NO_ROOM_CONTEXT: RoomContextValue = {
48
+ room: {poseOf: () => null},
49
+ arranging: false,
50
+ standing: new Map(),
51
+ stand: () => undefined,
52
+ };
53
+
54
+ const RoomContext = React.createContext<RoomContextValue>(NO_ROOM_CONTEXT);
55
+
56
+ const useRoom = (): RoomContextValue => React.useContext(RoomContext);
57
+
58
+ const samePose = (a: BlokPose | undefined, b: BlokPose | null): boolean =>
59
+ JSON.stringify(a ?? null) === JSON.stringify(b ?? null);
60
+
61
+ const RoomProvider = ({room, arranging, children}: {room: BlokRoom; arranging: boolean; children: React.ReactNode}) => {
62
+ const [standing, setStanding] = React.useState<ReadonlyMap<string, BlokPose>>(() => new Map());
63
+ const stand = React.useCallback((agentId: string, pose: BlokPose | null) => {
64
+ setStanding(previous => {
65
+ if (samePose(previous.get(agentId), pose)) return previous;
66
+ const next = new Map(previous);
67
+ if (pose === null) next.delete(agentId);
68
+ else next.set(agentId, pose);
69
+ return next;
70
+ });
71
+ }, []);
72
+ const value = React.useMemo(() => ({room, arranging, standing, stand}), [room, arranging, standing, stand]);
73
+ return <RoomContext.Provider value={value}>{children}</RoomContext.Provider>;
74
+ };
75
+
76
+ const ArrangeToggle = ({arranging, onChange}: {arranging: boolean; onChange: (arranging: boolean) => void}) => (
77
+ <div className="pointer-events-auto absolute bottom-2 left-2 z-10">
78
+ <Button
79
+ size="sm"
80
+ variant={arranging ? 'default' : 'outline'}
81
+ onClick={() => onChange(!arranging)}
82
+ title={arranging ? 'Done: devices stay where you left them, for everyone' : 'Move the devices of this room'}
83
+ >
84
+ <Move3d className="size-4" />
85
+ {arranging ? 'Done' : 'Arrange'}
86
+ </Button>
87
+ </div>
88
+ );
89
+
90
+ const SpaceRender: BlokRender<Contracts['Space']> = ({component, buildChild}) => {
91
+ const blok = useBlok(component, spaceSchema);
92
+ const reference = useValue(blok.room) ?? '';
93
+ const room = useBlokSpaceHost().useRoom(reference);
94
+ const still = useBlokSceneStill();
95
+ const [arranging, setArranging] = React.useState(false);
96
+ // Only a room the viewer may keep can be arranged, and never while viewing the past.
97
+ const canArrange = room.save !== undefined && !still;
98
+ const moving = arranging && canArrange;
99
+ useSceneNotice(reference ? null : 'A space without a room', 'Name the room (`room="room:lab"`): where devices stand is kept by it.');
100
+
101
+ const children = (
102
+ <RoomProvider room={room} arranging={moving}>
103
+ <BlokSceneInertProvider value={moving}>
104
+ <Children component={component} buildChild={buildChild} />
105
+ </BlokSceneInertProvider>
106
+ </RoomProvider>
36
107
  );
108
+ // Inside a scene that is already open, this one only adds what it holds.
109
+ if (useBlokSceneEmbedded()) return children;
110
+ return (
111
+ <SceneFrame
112
+ component={component}
113
+ buildChild={buildChild}
114
+ overlay={canArrange ? <ArrangeToggle arranging={moving} onChange={setArranging} /> : null}
115
+ >
116
+ {children}
117
+ </SceneFrame>
118
+ );
119
+ };
120
+
121
+ /* -------------------------------------------------------------------------- */
122
+ /* Placed devices */
123
+ /* -------------------------------------------------------------------------- */
37
124
 
38
125
  const EMPTY_REPORT = {
39
126
  ref: '',
@@ -47,85 +134,121 @@ const EMPTY_REPORT = {
47
134
 
48
135
  /**
49
136
  * What stands in for a device whose model is not in the scene (yet): its
50
- * anchor, with no nodes to bind and nothing to click. A binding without a
51
- * `node` then works on the anchor; one naming a node reports it missing.
137
+ * group, with no nodes to bind and nothing to click. A binding without a
138
+ * `node` then works on the group; one naming a node reports it missing.
52
139
  */
53
140
  const standInModel = (root: THREE.Object3D): ModelContextValue => ({
54
141
  instance: {root, nodes: new Map(), rest: new Map(), clips: [], report: EMPTY_REPORT},
55
142
  addClickable: () => () => undefined,
56
143
  });
57
144
 
58
- /** One agent the space places: its children at its placement, bound to its model as its scene blok loaded it. */
59
- const PlacedAt = ({
145
+ const isPoseRows = (value: unknown): value is BlokPose =>
146
+ Array.isArray(value) &&
147
+ value.length === 4 &&
148
+ value.every(row => Array.isArray(row) && row.length === 4 && row.every(cell => typeof cell === 'number' && Number.isFinite(cell)));
149
+
150
+ const IDENTITY: BlokPose = [
151
+ [1, 0, 0, 0],
152
+ [0, 1, 0, 0],
153
+ [0, 0, 1, 0],
154
+ [0, 0, 0, 1],
155
+ ];
156
+
157
+ /** One agent a dependency is bound to: drawn by its own scene blok where the room keeps it, its children bound to its model. */
158
+ const PlacedAgent = ({
60
159
  agent,
61
- matrix,
160
+ dependency,
161
+ declared,
162
+ blokName,
62
163
  children,
63
164
  }: {
64
165
  agent: BlokRunAgent;
65
- matrix: THREE.Matrix4;
166
+ dependency: string;
167
+ declared: unknown;
168
+ blokName: string | null;
66
169
  children: React.ReactNode;
67
170
  }) => {
171
+ const {room, arranging, stand} = useRoom();
172
+ const space = useBlokSpaceHost();
173
+ const kept = room.poseOf(agent.id);
174
+ const pose: BlokPose = kept ?? (isPoseRows(declared) ? declared : IDENTITY);
175
+ const poseKey = JSON.stringify(pose);
176
+ // Keyed by value: a room re-read around the same pose moves nothing.
177
+ // eslint-disable-next-line react-hooks/exhaustive-deps
178
+ const matrix = React.useMemo(() => toMatrix4(pose), [poseKey]);
179
+ // eslint-disable-next-line react-hooks/exhaustive-deps
180
+ React.useEffect(() => stand(agent.id, pose), [agent.id, poseKey, stand]);
181
+ React.useEffect(() => () => stand(agent.id, null), [agent.id, stand]);
182
+
68
183
  const [group, setGroup] = React.useState<THREE.Group | null>(null);
184
+ const dragging = React.useRef(false);
69
185
  React.useLayoutEffect(() => {
70
- if (group) matrix.decompose(group.position, group.quaternion, group.scale);
186
+ // While the gizmo holds the group it is the gizmo's; the save on release brings the pose back.
187
+ if (!group || dragging.current) return;
188
+ matrix.decompose(group.position, group.quaternion, group.scale);
71
189
  }, [group, matrix]);
190
+
72
191
  const device = useOwnedModel(agent.id);
73
192
  const standIn = React.useMemo(() => (group ? standInModel(group) : null), [group]);
74
193
  const model = device ?? standIn;
194
+
75
195
  return (
76
- <group ref={setGroup} name={`at:${agent.id}`}>
77
- {model ? <ModelProvider value={model}>{children}</ModelProvider> : null}
78
- </group>
196
+ <>
197
+ <group ref={setGroup} name={`placed:${dependency}:${agent.id}`}>
198
+ <SceneOwnerProvider value={agent.id}>{space.renderAgent(agent.id, blokName)}</SceneOwnerProvider>
199
+ {model ? <ModelProvider value={model}>{children}</ModelProvider> : null}
200
+ </group>
201
+ {arranging && room.save && group ? (
202
+ <TransformControls
203
+ object={group}
204
+ mode="translate"
205
+ size={0.7}
206
+ onMouseDown={() => {
207
+ dragging.current = true;
208
+ }}
209
+ onMouseUp={() => {
210
+ dragging.current = false;
211
+ group.updateMatrix();
212
+ // A click on the gizmo that moved nothing has nothing to keep.
213
+ if (group.matrix.equals(matrix)) return;
214
+ room.save?.(agent, dependency, fromMatrix4(group.matrix));
215
+ }}
216
+ />
217
+ ) : null}
218
+ </>
79
219
  );
80
220
  };
81
221
 
82
- /** One agent the space does not place: a card in the side tray, where what would point at it shows. */
83
- const TrayAt = ({agent, dependency, children}: {agent: BlokRunAgent; dependency: string; children: React.ReactNode}) => {
84
- const [root] = React.useState(() => new THREE.Object3D());
85
- const model = React.useMemo(() => standInModel(root), [root]);
86
- const anchor = React.useCallback(() => null, []);
87
- useSceneOverlay({
88
- visible: true,
89
- anchor,
90
- mode: 'panel',
91
- title: agent.name,
92
- description: `Not placed in this space (${dependency})`,
93
- content: null,
94
- });
222
+ const PlacedRender: BlokRender<Contracts['Placed']> = ({component, buildChild}) => {
223
+ const blok = useBlok(component, placedSchema);
224
+ const dependency = useValue(blok.dep);
225
+ const blokName = useValue(blok.blok) ?? null;
226
+ const declared = useValue(blok.matrix);
227
+ const run = useBlokRunHost();
228
+ const agents = React.useMemo(() => (dependency ? run.agentsFor(dependency) : []), [dependency, run]);
229
+ useSceneNotice(
230
+ dependency && agents.length === 0 ? `Nothing is bound to "${dependency}"` : null,
231
+ 'The run has no agent for this dependency, so nothing is drawn for it.',
232
+ );
233
+ useSceneNotice(
234
+ declared !== undefined && declared !== null && !isPoseRows(declared) ? 'A placement that is not a matrix' : null,
235
+ `"${dependency}" stands at the room's origin: its matrix is not four rows of four numbers.`,
236
+ );
237
+ const children = <Children component={component} buildChild={buildChild} />;
95
238
  return (
96
- <BlokTrayProvider value={agent.name}>
97
- <ModelProvider value={model}>{children}</ModelProvider>
98
- </BlokTrayProvider>
239
+ <>
240
+ {agents.map(agent => (
241
+ <PlacedAgent key={agent.id} agent={agent} dependency={dependency ?? ''} declared={declared} blokName={blokName}>
242
+ {children}
243
+ </PlacedAgent>
244
+ ))}
245
+ </>
99
246
  );
100
247
  };
101
248
 
102
- const AnchorRender: BlokRender<Contracts['Anchor']> = ({component, buildChild}) => {
103
- const blok = useBlok(component, anchorSchema);
104
- const dependency = useValue(blok.dep);
105
- const run = useBlokRunHost();
106
- const space = useBlokSpaceHost();
107
- const agents = React.useMemo(() => (dependency ? run.agentsFor(dependency) : []), [dependency, run]);
108
- const anchors = React.useMemo(() => resolveAnchors(agents, space.placementOf), [agents, space]);
109
- useSceneNotice(
110
- dependency && agents.length === 0 ? `Nothing is bound to "${dependency}"` : null,
111
- 'The run has no agent for this dependency, so nothing is drawn for it.',
112
- );
113
- const children = <Children component={component} buildChild={buildChild} />;
114
- return (
115
- <>
116
- {anchors.placed.map(({agent, matrix}) => (
117
- <PlacedAt key={agent.id} agent={agent} matrix={matrix}>
118
- {children}
119
- </PlacedAt>
120
- ))}
121
- {anchors.unplaced.map(agent => (
122
- <TrayAt key={agent.id} agent={agent} dependency={dependency ?? ''}>
123
- {children}
124
- </TrayAt>
125
- ))}
126
- </>
127
- );
128
- };
249
+ /* -------------------------------------------------------------------------- */
250
+ /* Route */
251
+ /* -------------------------------------------------------------------------- */
129
252
 
130
253
  const ROUTE_COLOR = 'chart-1';
131
254
 
@@ -134,41 +257,42 @@ const triples = (points: readonly THREE.Vector3[]): Array<[number, number, numbe
134
257
  points.map(point => [point.x, point.y, point.z]);
135
258
 
136
259
  const RouteRender: BlokRender<Contracts['Route']> = ({component}) => {
137
- const blok = useBlok(component, routeSchema);
138
- const through = useValue(blok.through);
139
- const progress = useValue(blok.progress) ?? undefined;
140
- const color = useThemeColour(useValue(blok.color) || ROUTE_COLOR) ?? FALLBACK_SCENE_RGB;
141
- const run = useBlokRunHost();
142
- const space = useBlokSpaceHost();
143
- const keys = React.useMemo(() => throughKeys(through), [through]);
144
- const points = React.useMemo(() => routePoints(keys, run.agentsFor, space.placementOf), [keys, run, space]);
145
- const split = React.useMemo(
146
- () => splitRoute(points, progress === undefined ? 1 : progressFraction(progress)),
147
- [points, progress],
148
- );
149
- useSceneNotice(
150
- keys.length >= 2 && points.length < 2 ? 'A route with nowhere to go' : null,
151
- `Fewer than two of ${keys.join(', ')} stand in this space.`,
152
- );
153
- if (!split.head) return null;
154
- return (
155
- <group name="route">
156
- {split.travelled.length >= 2 ? <Line points={triples(split.travelled)} color={color} lineWidth={3} /> : null}
157
- {split.ahead.length >= 2 && progress !== undefined ? (
158
- <Line points={triples(split.ahead)} color={color} lineWidth={2} dashed dashSize={0.04} gapSize={0.03} transparent opacity={0.5} />
159
- ) : null}
160
- {progress !== undefined ? (
161
- <mesh position={split.head} raycast={() => null}>
162
- <sphereGeometry args={[0.02, 16, 16]} />
163
- <meshBasicMaterial color={color} toneMapped={false} />
164
- </mesh>
165
- ) : null}
166
- </group>
167
- );
260
+ const blok = useBlok(component, routeSchema);
261
+ const through = useValue(blok.through);
262
+ const progress = useValue(blok.progress) ?? undefined;
263
+ const color = useThemeColour(useValue(blok.color) || ROUTE_COLOR) ?? FALLBACK_SCENE_RGB;
264
+ const run = useBlokRunHost();
265
+ const {standing} = useRoom();
266
+ const keys = React.useMemo(() => throughKeys(through), [through]);
267
+ const placementOf = React.useCallback((agentId: string) => standing.get(agentId) ?? null, [standing]);
268
+ const points = React.useMemo(() => routePoints(keys, run.agentsFor, placementOf), [keys, run, placementOf]);
269
+ const split = React.useMemo(
270
+ () => splitRoute(points, progress === undefined ? 1 : progressFraction(progress)),
271
+ [points, progress],
272
+ );
273
+ useSceneNotice(
274
+ keys.length >= 2 && points.length < 2 ? 'A route with nowhere to go' : null,
275
+ `Fewer than two of ${keys.join(', ')} are placed in this space.`,
276
+ );
277
+ if (!split.head) return null;
278
+ return (
279
+ <group name="route">
280
+ {split.travelled.length >= 2 ? <Line points={triples(split.travelled)} color={color} lineWidth={3} /> : null}
281
+ {split.ahead.length >= 2 && progress !== undefined ? (
282
+ <Line points={triples(split.ahead)} color={color} lineWidth={2} dashed dashSize={0.04} gapSize={0.03} transparent opacity={0.5} />
283
+ ) : null}
284
+ {progress !== undefined ? (
285
+ <mesh position={split.head} raycast={() => null}>
286
+ <sphereGeometry args={[0.02, 16, 16]} />
287
+ <meshBasicMaterial color={color} toneMapped={false} />
288
+ </mesh>
289
+ ) : null}
290
+ </group>
291
+ );
168
292
  };
169
293
 
170
294
  export const runRenders = {
171
- Overlay: OverlayRender,
172
- Anchor: AnchorRender,
295
+ Space: SpaceRender,
296
+ Placed: PlacedRender,
173
297
  Route: RouteRender,
174
298
  };
@@ -3,10 +3,11 @@ import * as React from 'react';
3
3
  /**
4
4
  * What an action's own blok needs from the app that shows a run of it.
5
5
  *
6
- * The blok core draws `Anchor`, `Route` and `ask`; it does not know which
7
- * agents a run got, where they stand, or how an ask is answered. The host
8
- * answers those through these contexts: the run (rekuest, from the task or a
9
- * dry run) and the space it is drawn into (rekuest's space scene).
6
+ * The blok core draws `Space`, `Placed`, `Route` and `ask`; it does not know
7
+ * which agents a run got, how an agent's own scene blok is found, where a room
8
+ * keeps its devices, or how an ask is answered. The host answers those through
9
+ * these contexts: the run (rekuest, from the task or a dry run) and the rooms
10
+ * (rekuest's spaces, kept on the server).
10
11
  *
11
12
  * Both are provided around the scene surface, not inside the canvas: a
12
13
  * `Popout`'s card is rendered by the scene's HTML half, outside the blok.
@@ -35,26 +36,51 @@ export const BlokRunHostProvider = BlokRunHostContext.Provider;
35
36
 
36
37
  export const useBlokRunHost = (): BlokRunHost => React.useContext(BlokRunHostContext);
37
38
 
39
+ /** A 4x4 affine as four rows, in meters with Y up: what a room keeps per device. */
40
+ export type BlokPose = number[][];
41
+
38
42
  /**
39
- * The space an overlay is drawn into: where each agent stands in it, as the
40
- * 4x4 affine its placement keeps (rows, meters, Y up).
43
+ * One room as the host keeps it: where people left its devices. Shared, so a
44
+ * device moved by one viewer stands there for every other.
41
45
  */
46
+ export type BlokRoom = {
47
+ /** Where someone left the agent in this room; null where nobody moved it (its `Placed` decides). */
48
+ poseOf: (agentId: string) => BlokPose | null;
49
+ /**
50
+ * Keep where the agent stands now, for everyone; `dependency` is the key it
51
+ * was placed under. Absent where the viewer may not arrange the room.
52
+ */
53
+ save?: (agent: BlokRunAgent, dependency: string, pose: BlokPose) => void;
54
+ /** Forget where the agent was left, so its `Placed` decides again. */
55
+ reset?: (agent: BlokRunAgent) => void;
56
+ };
57
+
42
58
  export type BlokSpaceHost = {
43
- /** The stored affine of the agent's placement; null where the space does not place it. */
44
- placementOf: (agentId: string) => unknown;
59
+ /** The room a `Space` names (`room="room:helsinki-lab"`). A hook. */
60
+ useRoom: (room: string) => BlokRoom;
61
+ /**
62
+ * Draw the agent with its own scene blok, inside the scene that is open: its
63
+ * default scene blok, or the one named. Nothing where it has none.
64
+ */
65
+ renderAgent: (agentId: string, blok: string | null) => React.ReactNode;
45
66
  };
46
67
 
47
- const NO_SPACE: BlokSpaceHost = {placementOf: () => null};
68
+ const NO_ROOM: BlokRoom = {poseOf: () => null};
69
+
70
+ const DETACHED_SPACE: BlokSpaceHost = {
71
+ useRoom: () => NO_ROOM,
72
+ renderAgent: () => null,
73
+ };
48
74
 
49
- const BlokSpaceHostContext = React.createContext<BlokSpaceHost>(NO_SPACE);
75
+ const BlokSpaceHostContext = React.createContext<BlokSpaceHost>(DETACHED_SPACE);
50
76
 
51
77
  export const BlokSpaceHostProvider = BlokSpaceHostContext.Provider;
52
78
 
53
79
  export const useBlokSpaceHost = (): BlokSpaceHost => React.useContext(BlokSpaceHostContext);
54
80
 
55
81
  /**
56
- * Set below an `At` whose agent the space does not place: what would point at
57
- * the device (a `Popout`) shows in the side tray instead, under this title.
82
+ * Set where what would point at a device has nothing to point at: a `Popout`
83
+ * then shows in the side tray instead, under this title.
58
84
  */
59
85
  const BlokTrayContext = React.createContext<string | null>(null);
60
86
 
@@ -1,3 +1,3 @@
1
1
  export {runRenders} from './components';
2
2
  export * from './host';
3
- export {progressFraction, resolveAnchors, routePoints, splitRoute, throughKeys} from './math';
3
+ export {progressFraction, routePoints, splitRoute, throughKeys} from './math';
@@ -1,7 +1,7 @@
1
1
  import {Matrix4, Quaternion, Vector3} from 'three';
2
2
  import {describe, expect, it} from 'vitest';
3
3
  import {fromMatrix4} from '../../../vendor/core/model/affine';
4
- import {progressFraction, resolveAnchors, ROUTE_LIFT, routePoints, splitRoute, throughKeys} from './math';
4
+ import {progressFraction, ROUTE_LIFT, routePoints, splitRoute, throughKeys} from './math';
5
5
 
6
6
  const arm = {id: 'a1', name: 'Arm'};
7
7
  const ot2 = {id: 'a2', name: 'OT-2'};
@@ -14,29 +14,6 @@ const turned = fromMatrix4(
14
14
  const placements: Record<string, unknown> = {a1: turned, a2: fromMatrix4(new Matrix4().setPosition(3, 0, 0))};
15
15
  const placementOf = (id: string) => placements[id] ?? null;
16
16
 
17
- describe('where an At draws', () => {
18
- it('stands at the placement of each placed agent, turned as it is', () => {
19
- const {placed, unplaced} = resolveAnchors([arm], placementOf);
20
- expect(unplaced).toEqual([]);
21
- expect(placed).toHaveLength(1);
22
- const position = new Vector3();
23
- const rotation = new Quaternion();
24
- placed[0].matrix.decompose(position, rotation, new Vector3());
25
- expect(position.toArray()).toEqual([1, 0, 2]);
26
- expect(rotation.angleTo(new Quaternion().setFromAxisAngle(new Vector3(0, 1, 0), Math.PI / 2))).toBeCloseTo(0);
27
- });
28
-
29
- it('sends an agent the space does not place to the tray', () => {
30
- const {placed, unplaced} = resolveAnchors([arm, scope], placementOf);
31
- expect(placed.map(anchor => anchor.agent)).toEqual([arm]);
32
- expect(unplaced).toEqual([scope]);
33
- });
34
-
35
- it('draws at every agent of a key bound to several', () => {
36
- expect(resolveAnchors([arm, ot2], placementOf).placed.map(anchor => anchor.agent.id)).toEqual(['a1', 'a2']);
37
- });
38
- });
39
-
40
17
  describe('the keys a Route runs through', () => {
41
18
  it('reads them as BSX writes a list, as JSON, as plain text, or as a list', () => {
42
19
  expect(throughKeys("['robot', 'opentrons', 'frame', 'robot']")).toEqual(['robot', 'opentrons', 'frame', 'robot']);
@@ -1,42 +1,12 @@
1
- import {Matrix4, Vector3} from 'three';
1
+ import {Vector3} from 'three';
2
2
  import {toMatrix4} from '../../../vendor/core/model/affine';
3
3
  import type {BlokRunAgent} from './host';
4
4
 
5
5
  /**
6
- * Where the agents of a run stand in the space it is drawn into: pure, so the
7
- * placement of an `At` and the line of a `Route` are worked out (and tested)
8
- * without a canvas.
6
+ * Where the agents of a run stand in the room a `Space` keeps: pure, so the
7
+ * line of a `Route` is worked out (and tested) without a canvas.
9
8
  */
10
9
 
11
- /** An agent the space places, at its placement's matrix. */
12
- export type PlacedRunAgent = {agent: BlokRunAgent; matrix: Matrix4};
13
-
14
- export type RunAnchors = {
15
- /** The agents bound to the key that stand in the space, in binding order. */
16
- placed: PlacedRunAgent[];
17
- /** The agents bound to the key the space does not place: shown in the side tray. */
18
- unplaced: BlokRunAgent[];
19
- };
20
-
21
- const isPlaced = (affine: unknown): boolean => affine !== null && affine !== undefined;
22
-
23
- /**
24
- * Where an `<At dependency=key>` draws: at every agent the run binds the key
25
- * to that the space places, and in the tray for every one it does not.
26
- */
27
- export const resolveAnchors = (
28
- agents: readonly BlokRunAgent[],
29
- placementOf: (agentId: string) => unknown,
30
- ): RunAnchors => {
31
- const anchors: RunAnchors = {placed: [], unplaced: []};
32
- for (const agent of agents) {
33
- const affine = placementOf(agent.id);
34
- if (isPlaced(affine)) anchors.placed.push({agent, matrix: toMatrix4(affine)});
35
- else anchors.unplaced.push(agent);
36
- }
37
- return anchors;
38
- };
39
-
40
10
  /**
41
11
  * The dependency keys a `through` names: a list, or the text BSX writes for
42
12
  * one (`"['robot', 'frame']"`, `'["robot"]'`, `"robot, frame"`), read as the
@@ -53,6 +23,8 @@ export const throughKeys = (value: unknown): string[] => {
53
23
  .filter(item => item !== '');
54
24
  };
55
25
 
26
+ const isPlaced = (affine: unknown): boolean => affine !== null && affine !== undefined;
27
+
56
28
  /** How far above a placement's origin the route runs, in meters: over the floor, under most devices. */
57
29
  export const ROUTE_LIFT = 0.05;
58
30