-
Notifications
You must be signed in to change notification settings - Fork 1
app-16292: shared ECS traits and helpers for entity pose plugins #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
faf59c0
Add shared ECS traits and helpers for entity pose plugins
DTCurrie 9e637c6
Add changeset
DTCurrie 77eed59
Address review feedback on writeMatrix and useMouseRaycaster
github-actions[bot] c18c0e7
Merge branch 'main' into app-16292/1-ecs-utils
DTCurrie dfef837
Address review feedback: writeMatrix early exit, undefined tests, fir…
github-actions[bot] c00b48a
Update src/lib/ecs/traits.ts
DTCurrie 1bc584f
Merge branch 'main' into app-16292/1-ecs-utils
DTCurrie 4c7ab2f
default to first hit
DTCurrie d076b3f
Merge branch 'main' into app-16292/1-ecs-utils
DTCurrie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@viamrobotics/motion-tools': patch | ||
| --- | ||
|
|
||
| Add `writeMatrix` and `CustomDetails` ECS traits, a `firstHitOnly` raycaster option, and a `'gizmo'` interaction mode |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { createWorld } from 'koota' | ||
| import { Matrix4 } from 'three' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { traits } from '$lib/ecs' | ||
| import { createPose, matrixToPose, poseToMatrix } from '$lib/transform' | ||
|
|
||
| import { writeMatrix } from '../traits' | ||
|
|
||
| const matrix = () => | ||
| poseToMatrix( | ||
| createPose({ | ||
| x: 10, | ||
| y: 20, | ||
| z: 30, | ||
| oX: 0.6, | ||
| oY: 0.8, | ||
| oZ: 0, | ||
| theta: 45, | ||
| }), | ||
| new Matrix4() | ||
| ) | ||
|
|
||
| describe('writeMatrix', () => { | ||
| it('no-ops when the entity has no Matrix trait', () => { | ||
| const world = createWorld() | ||
| const entity = world.spawn() | ||
| expect(() => writeMatrix(entity, { x: 1 })).not.toThrow() | ||
| }) | ||
|
|
||
| it('overwrites only the supplied position fields', () => { | ||
| const world = createWorld() | ||
| const entity = world.spawn(traits.Matrix(matrix())) | ||
| writeMatrix(entity, { x: 99 }) | ||
| const pose = matrixToPose(entity.get(traits.Matrix)!, createPose()) | ||
| expect(pose.x).toBeCloseTo(99) | ||
| expect(pose.y).toBeCloseTo(20) | ||
| expect(pose.z).toBeCloseTo(30) | ||
| }) | ||
|
|
||
| it('overwrites only the supplied orientation fields', () => { | ||
| const world = createWorld() | ||
| const entity = world.spawn(traits.Matrix(matrix())) | ||
| writeMatrix(entity, { theta: 90 }) | ||
| const pose = matrixToPose(entity.get(traits.Matrix)!, createPose()) | ||
| expect(pose.x).toBeCloseTo(10) | ||
| expect(pose.theta).toBeCloseTo(90) | ||
| expect(pose.oX).toBeCloseTo(0.6) | ||
| expect(pose.oY).toBeCloseTo(0.8) | ||
| expect(pose.oZ).toBeCloseTo(0) | ||
| }) | ||
|
|
||
| it('notifies subscribers via entity.changed', () => { | ||
| const world = createWorld() | ||
| const entity = world.spawn(traits.Matrix(matrix())) | ||
| const onChange = vi.fn() | ||
| world.onChange(traits.Matrix, onChange) | ||
| writeMatrix(entity, { x: 5 }) | ||
| expect(onChange).toHaveBeenCalledWith(entity) | ||
| }) | ||
|
|
||
| it('mutates the existing Matrix4 in place (does not allocate a new one)', () => { | ||
| const world = createWorld() | ||
| const entity = world.spawn(traits.Matrix(matrix())) | ||
| const before = entity.get(traits.Matrix) | ||
| writeMatrix(entity, { x: 5 }) | ||
| const after = entity.get(traits.Matrix) | ||
| expect(after).toBe(before) | ||
| }) | ||
|
DTCurrie marked this conversation as resolved.
|
||
|
|
||
| it('ignores explicitly undefined fields', () => { | ||
| const world = createWorld() | ||
| const entity = world.spawn(traits.Matrix(matrix())) | ||
| writeMatrix(entity, { x: undefined }) | ||
| const pose = matrixToPose(entity.get(traits.Matrix)!, createPose()) | ||
| expect(pose.x).toBeCloseTo(10) | ||
| expect(pose.y).toBeCloseTo(20) | ||
| expect(pose.z).toBeCloseTo(30) | ||
| }) | ||
|
|
||
| it('does not notify subscribers when patch is empty', () => { | ||
| const world = createWorld() | ||
| const entity = world.spawn(traits.Matrix(matrix())) | ||
| const onChange = vi.fn() | ||
| world.onChange(traits.Matrix, onChange) | ||
| writeMatrix(entity, {}) | ||
| expect(onChange).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not notify subscribers when all patch fields are undefined', () => { | ||
| const world = createWorld() | ||
| const entity = world.spawn(traits.Matrix(matrix())) | ||
| const onChange = vi.fn() | ||
| world.onChange(traits.Matrix, onChange) | ||
| writeMatrix(entity, { x: undefined, y: undefined }) | ||
| expect(onChange).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import type { GLTF as ThreeGltf } from 'three/examples/jsm/loaders/GLTFLoader.js' | ||
|
|
||
| import { Geometry as ViamGeometry } from '@viamrobotics/sdk' | ||
| import { Pose, Geometry as ViamGeometry } from '@viamrobotics/sdk' | ||
|
DTCurrie marked this conversation as resolved.
Outdated
|
||
| import { type Entity, trait } from 'koota' | ||
| import { Matrix4, BufferGeometry as ThreeBufferGeometry } from 'three' | ||
|
|
||
|
|
@@ -9,6 +9,7 @@ import { ColorFormat } from '$lib/buf/draw/v1/metadata_pb' | |
| import { createBox, createCapsule, createSphere } from '$lib/geometry' | ||
| import { parsePcdInWorker } from '$lib/loaders/pcd' | ||
| import { parsePlyInput } from '$lib/ply' | ||
| import { createPose, matrixToPose, poseToMatrix } from '$lib/transform' | ||
|
|
||
| export const Name = trait(() => '') | ||
| export const UUID = trait(() => '') | ||
|
|
@@ -74,6 +75,14 @@ export const InstancedMatrix = trait(() => ({ | |
| export const Hovered = trait(() => true) | ||
| export const Invisible = trait(() => true) | ||
|
|
||
| /** | ||
| * Suppresses the default frame-style world/local pose and parent-frame blocks | ||
| * in the details panel. Entities that render their own pose UI via the | ||
| * `details-extensions` portal target (e.g. gizmo plugin entities) opt in by | ||
| * adding this trait. | ||
| */ | ||
| export const CustomDetails = trait(() => true) | ||
|
Comment on lines
+78
to
+84
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can probably be removed when frame editing is made a plugin |
||
|
|
||
| /** | ||
| * True when the entity itself, or any of its parents up the `ChildOf` | ||
| * chain, has `Invisible`. Maintained by `provideInheritedInvisible`; | ||
|
|
@@ -291,6 +300,25 @@ export const updateGeometryTrait = (entity: Entity, geometry?: ViamGeometry) => | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Patches an entity's `Matrix` trait in-place via the `Pose` round-trip | ||
| * (`matrixToPose` → merge → `poseToMatrix`), then signals `entity.changed(Matrix)`. | ||
| * No-ops silently if the entity has no `Matrix` trait. | ||
| */ | ||
| export const writeMatrix = (entity: Entity, patch: Partial<Pose>) => { | ||
| const matrix = entity.get(Matrix) | ||
| if (!matrix) return | ||
|
|
||
| const pose = matrixToPose(matrix, createPose()) | ||
| const filtered = Object.fromEntries( | ||
| Object.entries(patch).filter(([, v]) => v !== undefined) | ||
| ) as Partial<Pose> | ||
| if (Object.keys(filtered).length === 0) return | ||
| Object.assign(pose, filtered) | ||
| poseToMatrix(pose, matrix) | ||
| entity.changed(Matrix) | ||
|
DTCurrie marked this conversation as resolved.
|
||
| } | ||
|
|
||
| const updatePointCloud = (entity: Entity, pointCloud: Uint8Array): void => { | ||
| parsePcdInWorker(new Uint8Array(pointCloud)) | ||
| .then((parsed) => { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.