@viamrobotics/motion-tools 1.19.0 → 1.19.1

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.
@@ -31,6 +31,7 @@
31
31
  */
32
32
  const worldStateEntities = useQuery(
33
33
  traits.WorldStateStoreAPI,
34
+ Not(traits.Points, traits.LinePositions, traits.GLTF),
34
35
  Or(traits.Box, traits.Capsule, traits.Sphere, traits.BufferGeometry, traits.ReferenceFrame)
35
36
  )
36
37
 
package/dist/draw.js CHANGED
@@ -109,11 +109,10 @@ export const updateMetadata = (entity, metadata, { pointCloud = false } = {}) =>
109
109
  const { colors, opacities } = metadata;
110
110
  if (colors) {
111
111
  if (pointCloud) {
112
- updateColors(entity, metadata);
113
- }
114
- else {
115
- setColorTraits(entity, colors);
112
+ updatePointCloudColors(entity, metadata);
116
113
  }
114
+ // Always set color traits so any subsequent async work can read them
115
+ setColorTraits(entity, colors);
117
116
  }
118
117
  entity.set(traits.Opacity, asOpacity(opacities, DEFAULT_OPACITY));
119
118
  };
@@ -315,7 +314,7 @@ const parsePointCloud = (world, entity, pointCloud, metadata) => {
315
314
  entity.add(traits.Points);
316
315
  });
317
316
  };
318
- const updateColors = (entity, metadata) => {
317
+ const updatePointCloudColors = (entity, metadata) => {
319
318
  const buffer = entity.get(traits.BufferGeometry);
320
319
  if (!buffer) {
321
320
  if (metadata.colors)
@@ -1,7 +1,10 @@
1
1
  import { Geometry as ViamGeometry } from '@viamrobotics/sdk';
2
2
  import { trait } from 'koota';
3
3
  import { BufferGeometry as ThreeBufferGeometry } from 'three';
4
+ import { createBufferGeometry, updateBufferGeometry } from '../attribute';
5
+ import { ColorFormat } from '../buf/draw/v1/metadata_pb';
4
6
  import { createBox, createCapsule, createSphere } from '../geometry';
7
+ import { parsePcdInWorker } from '../loaders/pcd';
5
8
  import { parsePlyInput } from '../ply';
6
9
  export const Name = trait(() => '');
7
10
  export const Parent = trait(() => 'world');
@@ -203,4 +206,62 @@ export const updateGeometryTrait = (entity, geometry) => {
203
206
  entity.add(BufferGeometry(parsePlyInput(geometry.geometryType.value.mesh)));
204
207
  }
205
208
  }
209
+ else if (geometry.geometryType.case === 'pointcloud') {
210
+ updatePointCloud(entity, geometry.geometryType.value.pointCloud);
211
+ }
212
+ };
213
+ const updatePointCloud = (entity, pointCloud) => {
214
+ parsePcdInWorker(new Uint8Array(pointCloud))
215
+ .then((parsed) => {
216
+ if (!entity.isAlive())
217
+ return;
218
+ const buffer = entity.get(BufferGeometry);
219
+ let colors = parsed.colors;
220
+ if (buffer) {
221
+ // Reapply single color trait if the point count changed
222
+ if (parsed.colors === undefined) {
223
+ const color = entity.get(Color);
224
+ if (color) {
225
+ const newCount = parsed.positions.length / 3;
226
+ colors = new Uint8Array(newCount * 3);
227
+ const r = Math.round(color.r * 255);
228
+ const g = Math.round(color.g * 255);
229
+ const b = Math.round(color.b * 255);
230
+ for (let i = 0; i < newCount; i++) {
231
+ colors[i * 3] = r;
232
+ colors[i * 3 + 1] = g;
233
+ colors[i * 3 + 2] = b;
234
+ }
235
+ }
236
+ }
237
+ // When the point count changes, attributes must be reallocated.
238
+ const oldCount = buffer.getAttribute('position').count;
239
+ const newCount = parsed.positions.length / 3;
240
+ if (oldCount === newCount) {
241
+ updateBufferGeometry(buffer, parsed.positions, {
242
+ colors,
243
+ colorFormat: ColorFormat.RGB,
244
+ });
245
+ }
246
+ else {
247
+ const fresh = createBufferGeometry(parsed.positions, {
248
+ colors,
249
+ colorFormat: ColorFormat.RGB,
250
+ });
251
+ buffer.dispose();
252
+ entity.set(BufferGeometry, fresh);
253
+ }
254
+ return;
255
+ }
256
+ entity.remove(Box, Capsule, Sphere);
257
+ entity.add(BufferGeometry(createBufferGeometry(parsed.positions, {
258
+ colors: parsed.colors,
259
+ colorFormat: ColorFormat.RGB,
260
+ })));
261
+ if (!entity.has(Points))
262
+ entity.add(Points);
263
+ })
264
+ .catch((error) => {
265
+ console.error('Failed to update pointcloud buffer geometry:', error);
266
+ });
206
267
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@viamrobotics/motion-tools",
3
- "version": "1.19.0",
3
+ "version": "1.19.1",
4
4
  "description": "Motion visualization with Viam",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",