Skip to content

Commit bce6867

Browse files
authored
APP-8006 add world state hook (#105)
* add world state hook * trying out the world state hook * Add use world state hook * bump and do targeted updates * fix * remove test setup * Add world state connection
1 parent 4e4cb54 commit bce6867

File tree

10 files changed

+279
-16
lines changed

10 files changed

+279
-16
lines changed

.changeset/happy-parks-say.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@viamrobotics/motion-tools': patch
3+
---
4+
5+
Add useWorldState hook

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@tanstack/svelte-query-devtools": "5.84.0",
4444
"@testing-library/jest-dom": "6.6.4",
4545
"@testing-library/svelte": "5.2.8",
46+
"@thi.ng/paths": "^5.2.21",
4647
"@threlte/core": "8.1.4",
4748
"@threlte/extras": "9.4.4",
4849
"@threlte/rapier": "3.1.5",
@@ -53,8 +54,8 @@
5354
"@typescript-eslint/eslint-plugin": "8.39.0",
5455
"@typescript-eslint/parser": "8.39.0",
5556
"@viamrobotics/prime-core": "0.1.5",
56-
"@viamrobotics/sdk": "0.46.0",
57-
"@viamrobotics/svelte-sdk": "0.4.5",
57+
"@viamrobotics/sdk": "0.50.0",
58+
"@viamrobotics/svelte-sdk": "0.6.0",
5859
"@vitejs/plugin-basic-ssl": "2.1.0",
5960
"@zag-js/svelte": "1.21.1",
6061
"@zag-js/tree-view": "1.21.1",

pnpm-lock.yaml

Lines changed: 53 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/WorldObject.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Geometry, Pose } from '@viamrobotics/sdk'
1+
import type { Geometry, Pose, TransformWithUUID } from '@viamrobotics/sdk'
22
import { BatchedMesh, Box3, MathUtils, Object3D, Vector3, type ColorRepresentation } from 'three'
33
import { createPose } from './transform'
44

@@ -39,3 +39,15 @@ export class WorldObject<T extends Geometries = Geometries> {
3939
this.metadata = metadata ?? {}
4040
}
4141
}
42+
43+
export const fromTransform = (transform: TransformWithUUID) => {
44+
const metadata: Metadata = { ...transform.metadata?.fields }
45+
const worldObject = new WorldObject(
46+
transform.referenceFrame,
47+
transform.poseInObserverFrame?.pose,
48+
transform.poseInObserverFrame?.referenceFrame,
49+
transform.physicalObject?.geometryType,
50+
metadata
51+
)
52+
return worldObject
53+
}

src/lib/components/SceneProviders.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
providePointclouds(() => partID.current)
3838
provideMotionClient(() => partID.current)
3939
provideObjects()
40+
4041
const { focus } = provideSelection()
4142
</script>
4243

src/lib/components/Tree/TreeContainer.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import Settings from './Settings.svelte'
1010
import Logs from './Logs.svelte'
1111
import { useDraggable } from '$lib/hooks/useDraggable.svelte'
12+
import { useWorldStates } from '$lib/hooks/useWorldState.svelte'
1213
1314
const { ...rest } = $props()
1415
@@ -17,6 +18,7 @@
1718
const selected = useSelected()
1819
const objects = useObjects()
1920
const draggable = useDraggable('treeview')
21+
const worldStates = useWorldStates()
2022
2123
let rootNode = $state<TreeNode>({
2224
id: 'world',
@@ -25,7 +27,7 @@
2527
href: '/',
2628
})
2729
28-
const nodes = $derived(buildTreeNodes(objects.current))
30+
const nodes = $derived(buildTreeNodes(objects.current, worldStates.current))
2931
3032
$effect.pre(() => {
3133
if (!isEqual(rootNode.children, nodes)) {

src/lib/components/Tree/buildTree.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ export interface TreeNode {
1010
/**
1111
* Creates a tree representing parent child / relationships from a set of frames.
1212
*/
13-
export const buildTreeNodes = (objects: WorldObject[]): TreeNode[] => {
13+
export const buildTreeNodes = (
14+
objects: WorldObject[],
15+
worldStates: { name: string; objects: WorldObject[] }[]
16+
): TreeNode[] => {
1417
const nodeMap = new Map<string, TreeNode>()
1518
const rootNodes = []
1619

@@ -39,5 +42,32 @@ export const buildTreeNodes = (objects: WorldObject[]): TreeNode[] => {
3942
}
4043
}
4144

45+
for (const worldState of worldStates) {
46+
const node: TreeNode = {
47+
name: worldState.name,
48+
id: worldState.name,
49+
children: [],
50+
href: `/world-state/${worldState.name}`,
51+
}
52+
53+
console.log('worldState', worldState)
54+
55+
for (const object of worldState.objects) {
56+
const child: TreeNode = {
57+
name: object.name,
58+
id: object.uuid,
59+
children: [],
60+
href: `/world-state/${worldState.name}/${object.name}`,
61+
}
62+
63+
nodeMap.set(object.name, child)
64+
node.children?.push(child)
65+
console.log('child', child)
66+
}
67+
68+
nodeMap.set(worldState.name, node)
69+
rootNodes.push(node)
70+
}
71+
4272
return rootNodes
4373
}

src/lib/components/WorldObjects.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
import Pointcloud from './Pointcloud.svelte'
1212
import Model from './WorldObject.svelte'
1313
import Label from './Label.svelte'
14+
import { useWorldStates } from '$lib/hooks/useWorldState.svelte'
15+
import WorldState from './WorldState.svelte'
1416
1517
const points = usePointClouds()
1618
const drawAPI = useDrawAPI()
1719
const frames = useFrames()
1820
const geometries = useGeometries()
21+
const worldStates = useWorldStates()
1922
</script>
2023

2124
{#each frames.current as object (object.uuid)}
@@ -65,6 +68,10 @@
6568
</Portal>
6669
{/each}
6770

71+
{#each worldStates.current as { name, objects } (name)}
72+
<WorldState {objects} />
73+
{/each}
74+
6875
{#each points.current as object (object.uuid)}
6976
<Portal id={object.referenceFrame}>
7077
<Pointcloud {object}>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script lang="ts">
2+
import Frame from './Frame.svelte'
3+
import Label from './Label.svelte'
4+
import Portal from './portal/Portal.svelte'
5+
import PortalTarget from './portal/PortalTarget.svelte'
6+
import { WorldObject } from '$lib/WorldObject'
7+
8+
interface Props {
9+
objects: WorldObject[]
10+
}
11+
12+
let { objects }: Props = $props()
13+
</script>
14+
15+
{#each objects as object (object.uuid)}
16+
<Portal id={object.referenceFrame}>
17+
<Frame
18+
uuid={object.uuid}
19+
name={object.name}
20+
pose={object.pose}
21+
geometry={object.geometry}
22+
metadata={object.metadata}
23+
>
24+
<PortalTarget id={object.name} />
25+
<Label text={object.name} />
26+
</Frame>
27+
</Portal>
28+
{/each}

0 commit comments

Comments
 (0)