@luma.gl/graph 10.0.0-beta.3 → 10.0.0-beta.4
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/dist/arrow/gpu/arrow-gpu-table-conversion.cjs +66 -30
- package/dist/arrow/gpu/arrow-gpu-table-conversion.d.ts +7 -1
- package/dist/arrow/gpu/arrow-gpu-table-conversion.d.ts.map +1 -1
- package/dist/arrow/gpu/arrow-gpu-table-conversion.js +78 -32
- package/dist/arrow/gpu/arrow-gpu-table-conversion.js.map +1 -1
- package/dist/arrow/gpu/arrow-gpu-vector-conversion.cjs +7 -0
- package/dist/arrow/gpu/arrow-gpu-vector-conversion.d.ts +2 -0
- package/dist/arrow/gpu/arrow-gpu-vector-conversion.d.ts.map +1 -1
- package/dist/arrow/gpu/arrow-gpu-vector-conversion.js +14 -0
- package/dist/arrow/gpu/arrow-gpu-vector-conversion.js.map +1 -1
- package/dist/arrow/index.cjs.map +2 -2
- package/dist/arrow/vectors/arrow-temporal-gpu-vector.cjs +1 -2
- package/dist/arrow/vectors/arrow-temporal-gpu-vector.js +2 -2
- package/dist/arrow/vectors/arrow-temporal-gpu-vector.js.map +1 -1
- package/dist/dist.dev.js +62 -27
- package/dist/dist.min.js +48 -47
- package/dist/gpu-core/gpu-command-graph-budget-controller.cjs +10 -2
- package/dist/gpu-core/gpu-command-graph-budget-controller.d.ts +4 -0
- package/dist/gpu-core/gpu-command-graph-budget-controller.d.ts.map +1 -1
- package/dist/gpu-core/gpu-command-graph-budget-controller.js +10 -2
- package/dist/gpu-core/gpu-command-graph-budget-controller.js.map +1 -1
- package/dist/gpu-core/gpu-group-aggregation.cjs +45 -21
- package/dist/gpu-core/gpu-group-aggregation.d.ts +4 -0
- package/dist/gpu-core/gpu-group-aggregation.d.ts.map +1 -1
- package/dist/gpu-core/gpu-group-aggregation.js +58 -24
- package/dist/gpu-core/gpu-group-aggregation.js.map +1 -1
- package/dist/gpu-core/index.cjs.map +1 -1
- package/dist/gpu-dataframe/gpu-global-aggregation-compiler.cjs +6 -2
- package/dist/gpu-dataframe/gpu-global-aggregation-compiler.d.ts +2 -1
- package/dist/gpu-dataframe/gpu-global-aggregation-compiler.d.ts.map +1 -1
- package/dist/gpu-dataframe/gpu-global-aggregation-compiler.js +6 -2
- package/dist/gpu-dataframe/gpu-global-aggregation-compiler.js.map +1 -1
- package/dist/gpu-dataframe/gpu-global-sort-compiler.cjs +61 -54
- package/dist/gpu-dataframe/gpu-global-sort-compiler.d.ts +6 -1
- package/dist/gpu-dataframe/gpu-global-sort-compiler.d.ts.map +1 -1
- package/dist/gpu-dataframe/gpu-global-sort-compiler.js +61 -55
- package/dist/gpu-dataframe/gpu-global-sort-compiler.js.map +1 -1
- package/dist/gpu-dataframe/gpu-group-aggregation-compiler.cjs +88 -13
- package/dist/gpu-dataframe/gpu-group-aggregation-compiler.d.ts +11 -4
- package/dist/gpu-dataframe/gpu-group-aggregation-compiler.d.ts.map +1 -1
- package/dist/gpu-dataframe/gpu-group-aggregation-compiler.js +96 -16
- package/dist/gpu-dataframe/gpu-group-aggregation-compiler.js.map +1 -1
- package/dist/gpu-dataframe/gpu-group-by-query.cjs +12 -3
- package/dist/gpu-dataframe/gpu-group-by-query.d.ts +8 -1
- package/dist/gpu-dataframe/gpu-group-by-query.d.ts.map +1 -1
- package/dist/gpu-dataframe/gpu-group-by-query.js +13 -3
- package/dist/gpu-dataframe/gpu-group-by-query.js.map +1 -1
- package/dist/gpu-dataframe/gpu-join-compiler.cjs +53 -2
- package/dist/gpu-dataframe/gpu-join-compiler.d.ts +9 -1
- package/dist/gpu-dataframe/gpu-join-compiler.d.ts.map +1 -1
- package/dist/gpu-dataframe/gpu-join-compiler.js +51 -0
- package/dist/gpu-dataframe/gpu-join-compiler.js.map +1 -1
- package/dist/gpu-dataframe/gpu-query-compiler.cjs +24 -0
- package/dist/gpu-dataframe/gpu-query-compiler.d.ts +3 -1
- package/dist/gpu-dataframe/gpu-query-compiler.d.ts.map +1 -1
- package/dist/gpu-dataframe/gpu-query-compiler.js +25 -0
- package/dist/gpu-dataframe/gpu-query-compiler.js.map +1 -1
- package/dist/gpu-dataframe/index.cjs.map +3 -3
- package/dist/gpu-graph/index.cjs.map +1 -1
- package/dist/gpu-graph-algorithms/benchmarks.cjs.map +2 -2
- package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.cjs +182 -62
- package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.d.ts +29 -2
- package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.d.ts.map +1 -1
- package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.js +200 -64
- package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.js.map +1 -1
- package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.cjs +135 -9
- package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.d.ts +55 -8
- package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.d.ts.map +1 -1
- package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.js +145 -14
- package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.js.map +1 -1
- package/dist/gpu-graph-algorithms/index.cjs.map +3 -3
- package/dist/gpu-physics/articulation-tree-shader.cjs +177 -0
- package/dist/gpu-physics/articulation-tree-shader.d.ts +3 -0
- package/dist/gpu-physics/articulation-tree-shader.d.ts.map +1 -0
- package/dist/gpu-physics/articulation-tree-shader.js +159 -0
- package/dist/gpu-physics/articulation-tree-shader.js.map +1 -0
- package/dist/gpu-physics/gpu-articulation-tree.cjs +203 -0
- package/dist/gpu-physics/gpu-articulation-tree.d.ts +81 -0
- package/dist/gpu-physics/gpu-articulation-tree.d.ts.map +1 -0
- package/dist/gpu-physics/gpu-articulation-tree.js +205 -0
- package/dist/gpu-physics/gpu-articulation-tree.js.map +1 -0
- package/dist/gpu-physics/gpu-rigid-body-world.cjs +62 -0
- package/dist/gpu-physics/gpu-rigid-body-world.d.ts +12 -0
- package/dist/gpu-physics/gpu-rigid-body-world.d.ts.map +1 -1
- package/dist/gpu-physics/gpu-rigid-body-world.js +76 -0
- package/dist/gpu-physics/gpu-rigid-body-world.js.map +1 -1
- package/dist/gpu-physics/index.cjs +3 -0
- package/dist/gpu-physics/index.cjs.map +3 -3
- package/dist/gpu-physics/index.d.ts +1 -0
- package/dist/gpu-physics/index.d.ts.map +1 -1
- package/dist/gpu-physics/index.js +1 -0
- package/dist/gpu-physics/index.js.map +1 -1
- package/dist/gpu-physics/rigid-body-joints.d.ts +2 -0
- package/dist/gpu-physics/rigid-body-joints.d.ts.map +1 -1
- package/dist/gpu-physics/rigid-body-joints.js.map +1 -1
- package/dist/gpu-project/graph.cjs.map +2 -2
- package/dist/gpu-simple-graph/index.cjs.map +2 -2
- package/dist/gpu-sql/gpu-sql.cjs +167 -47
- package/dist/gpu-sql/gpu-sql.d.ts +6 -1
- package/dist/gpu-sql/gpu-sql.d.ts.map +1 -1
- package/dist/gpu-sql/gpu-sql.js +187 -47
- package/dist/gpu-sql/gpu-sql.js.map +1 -1
- package/dist/gpu-sql/index.cjs.map +3 -3
- package/dist/index.cjs.map +1 -1
- package/package.json +8 -8
- package/src/arrow/gpu/arrow-gpu-table-conversion.ts +94 -40
- package/src/arrow/gpu/arrow-gpu-vector-conversion.ts +19 -0
- package/src/arrow/vectors/arrow-temporal-gpu-vector.ts +2 -2
- package/src/gpu-core/gpu-command-graph-budget-controller.ts +22 -2
- package/src/gpu-core/gpu-group-aggregation.ts +68 -30
- package/src/gpu-dataframe/gpu-global-aggregation-compiler.ts +9 -2
- package/src/gpu-dataframe/gpu-global-sort-compiler.ts +95 -78
- package/src/gpu-dataframe/gpu-group-aggregation-compiler.ts +191 -24
- package/src/gpu-dataframe/gpu-group-by-query.ts +27 -4
- package/src/gpu-dataframe/gpu-join-compiler.ts +75 -0
- package/src/gpu-dataframe/gpu-query-compiler.ts +41 -1
- package/src/gpu-graph-algorithms/README.md +4 -2
- package/src/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.ts +248 -66
- package/src/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.ts +218 -20
- package/src/gpu-physics/articulation-tree-shader.ts +169 -0
- package/src/gpu-physics/gpu-articulation-tree.ts +279 -0
- package/src/gpu-physics/gpu-rigid-body-world.ts +89 -0
- package/src/gpu-physics/index.ts +7 -0
- package/src/gpu-physics/rigid-body-joints.ts +2 -0
- package/src/gpu-sql/gpu-sql.ts +248 -57
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {Buffer, type ShaderLayout, type Binding, type BindingDeclaration} from '@luma.gl/core';
|
|
6
|
+
import type {GPUCommandGraph, GraphBufferUse} from '../gpu-core/gpu-command-graph';
|
|
7
|
+
import type {GPUCommandGraphComputeExecutable} from '../gpu-core/gpu-command-graph-types';
|
|
8
|
+
import {
|
|
9
|
+
createBoundKernel,
|
|
10
|
+
createBoundKernelAsync,
|
|
11
|
+
type BoundKernel
|
|
12
|
+
} from '../gpu-core/utils/bound-kernel';
|
|
13
|
+
import {getArticulationTreeShader} from './articulation-tree-shader';
|
|
14
|
+
|
|
15
|
+
type Vector = readonly [number, number, number];
|
|
16
|
+
/** Link origins are centers of mass. Both anchor offsets use their respective local frames. */
|
|
17
|
+
export type GPUArticulationLink = {
|
|
18
|
+
id: string;
|
|
19
|
+
/** Parent link index, or -1 for the fixed base. Parents precede children. */
|
|
20
|
+
parent: number;
|
|
21
|
+
parentAnchor: Vector;
|
|
22
|
+
childAnchor: Vector;
|
|
23
|
+
/** Unit revolute axis in the parent frame. Omit for a welded attachment. */
|
|
24
|
+
axis?: Vector;
|
|
25
|
+
/** Child-to-parent orientation at zero angle. */
|
|
26
|
+
restOrientation?: readonly [number, number, number, number];
|
|
27
|
+
mass: number;
|
|
28
|
+
/** Principal moments about the COM in the child frame. */
|
|
29
|
+
inertia: Vector;
|
|
30
|
+
angle?: number;
|
|
31
|
+
speed?: number;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** Position control in radians, N·m/rad, N·m·s/rad and N·m respectively. */
|
|
35
|
+
export type GPUArticulationDrive = {
|
|
36
|
+
angle: number;
|
|
37
|
+
stiffness: number;
|
|
38
|
+
damping: number;
|
|
39
|
+
maximumTorque: number;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** Frictionless unilateral contact between a link-centered sphere and a static world-space box. */
|
|
43
|
+
export type GPUArticulationContact = {
|
|
44
|
+
link: string;
|
|
45
|
+
radius: number;
|
|
46
|
+
center: Vector;
|
|
47
|
+
halfExtents: Vector;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** Small fixed-base trees. The graph owns the kernel and the tree owns the buffers. */
|
|
51
|
+
export class GPUArticulationTree {
|
|
52
|
+
readonly timeStep: number;
|
|
53
|
+
readonly id: string;
|
|
54
|
+
private readonly links: GPUArticulationLink[];
|
|
55
|
+
private readonly indices = new Map<string, number>();
|
|
56
|
+
private readonly buffers: Buffer[] = [];
|
|
57
|
+
private readonly base: Vector;
|
|
58
|
+
private readonly gravity: Vector;
|
|
59
|
+
private readonly contact?: GPUArticulationContact;
|
|
60
|
+
private readonly publishMatrices: boolean;
|
|
61
|
+
/** Column-major world transforms in link order, available after registration with publishMatrices. */
|
|
62
|
+
get poseMatrices() {
|
|
63
|
+
return this.buffers[6] ?? null;
|
|
64
|
+
}
|
|
65
|
+
private destroyed = false;
|
|
66
|
+
|
|
67
|
+
constructor(props: {
|
|
68
|
+
id?: string;
|
|
69
|
+
links: readonly GPUArticulationLink[];
|
|
70
|
+
base?: Vector;
|
|
71
|
+
gravity?: Vector;
|
|
72
|
+
timeStep?: number;
|
|
73
|
+
contact?: GPUArticulationContact;
|
|
74
|
+
publishMatrices?: boolean;
|
|
75
|
+
}) {
|
|
76
|
+
this.id = props.id ?? 'articulation-tree';
|
|
77
|
+
this.timeStep = props.timeStep ?? 1 / 120;
|
|
78
|
+
this.contact = structuredClone(props.contact);
|
|
79
|
+
this.publishMatrices = Boolean(props.publishMatrices);
|
|
80
|
+
this.links = structuredClone([...props.links]);
|
|
81
|
+
this.base = [...(props.base ?? [0, 0, 0])];
|
|
82
|
+
this.gravity = [...(props.gravity ?? [0, 0, -9.81])];
|
|
83
|
+
const finite = (x: number) => Number.isFinite(Math.fround(x));
|
|
84
|
+
if (
|
|
85
|
+
!this.links.length ||
|
|
86
|
+
this.links.length > 16 ||
|
|
87
|
+
!finite(this.timeStep) ||
|
|
88
|
+
Math.fround(this.timeStep) <= 0 ||
|
|
89
|
+
![...this.base, ...this.gravity].every(finite)
|
|
90
|
+
)
|
|
91
|
+
throw new Error('Invalid articulation settings');
|
|
92
|
+
for (const [index, link] of this.links.entries()) {
|
|
93
|
+
if (
|
|
94
|
+
!link.id ||
|
|
95
|
+
this.indices.has(link.id) ||
|
|
96
|
+
!Number.isInteger(link.parent) ||
|
|
97
|
+
link.parent < -1 ||
|
|
98
|
+
link.parent >= index ||
|
|
99
|
+
!finite(link.mass) ||
|
|
100
|
+
Math.fround(link.mass) <= 0 ||
|
|
101
|
+
!link.inertia.every(value => finite(value) && Math.fround(value) > 0) ||
|
|
102
|
+
![...link.parentAnchor, ...link.childAnchor, link.angle ?? 0, link.speed ?? 0].every(
|
|
103
|
+
finite
|
|
104
|
+
) ||
|
|
105
|
+
(link.axis
|
|
106
|
+
? !(Math.abs(Math.hypot(...link.axis) - 1) < 1e-6)
|
|
107
|
+
: Boolean(link.angle || link.speed)) ||
|
|
108
|
+
(link.restOrientation && !(Math.abs(Math.hypot(...link.restOrientation) - 1) < 1e-6))
|
|
109
|
+
)
|
|
110
|
+
throw new Error('Invalid articulation link');
|
|
111
|
+
this.indices.set(link.id, index);
|
|
112
|
+
}
|
|
113
|
+
const contact = this.contact;
|
|
114
|
+
if (
|
|
115
|
+
contact &&
|
|
116
|
+
(!this.indices.has(contact.link) ||
|
|
117
|
+
!contact.center.every(finite) ||
|
|
118
|
+
![contact.radius, ...contact.halfExtents].every(
|
|
119
|
+
value => finite(value) && Math.fround(value) > 0
|
|
120
|
+
))
|
|
121
|
+
)
|
|
122
|
+
throw new Error('Invalid articulation contact');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Register one simulation step. The output pose buffer uses four vec4s per link: COM, quaternion, velocity, spin. */
|
|
126
|
+
addToGraph<P>(graph: GPUCommandGraph<P>) {
|
|
127
|
+
if (this.destroyed || this.buffers.length)
|
|
128
|
+
throw new Error('Inactive or already registered articulation');
|
|
129
|
+
const names = ['links', 'drives', 'coordinates', 'poses', 'counters', 'settings'];
|
|
130
|
+
const data = [
|
|
131
|
+
new Float32Array(
|
|
132
|
+
this.links.flatMap(link => [
|
|
133
|
+
...link.parentAnchor,
|
|
134
|
+
link.parent,
|
|
135
|
+
...link.childAnchor,
|
|
136
|
+
0,
|
|
137
|
+
...(link.axis ?? [0, 0, 0]),
|
|
138
|
+
link.mass,
|
|
139
|
+
...link.inertia,
|
|
140
|
+
0,
|
|
141
|
+
...(link.restOrientation ?? [0, 0, 0, 1])
|
|
142
|
+
])
|
|
143
|
+
),
|
|
144
|
+
new Float32Array(this.links.length * 4),
|
|
145
|
+
new Float32Array(this.links.flatMap(link => [link.angle ?? 0, link.speed ?? 0, 0, 0])),
|
|
146
|
+
new Float32Array(this.links.length * 16),
|
|
147
|
+
new Uint32Array(4),
|
|
148
|
+
new Float32Array([
|
|
149
|
+
...this.base,
|
|
150
|
+
0,
|
|
151
|
+
...this.gravity,
|
|
152
|
+
this.timeStep,
|
|
153
|
+
...(this.contact?.center ?? [0, 0, 0]),
|
|
154
|
+
this.contact?.radius ?? 0,
|
|
155
|
+
...(this.contact?.halfExtents ?? [0, 0, 0]),
|
|
156
|
+
this.contact ? this.indices.get(this.contact.link)! : 0
|
|
157
|
+
])
|
|
158
|
+
];
|
|
159
|
+
if (this.publishMatrices) {
|
|
160
|
+
names.push('matrices');
|
|
161
|
+
data.push(new Float32Array(this.links.length * 16));
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
const handles = data.map((values, index) => {
|
|
165
|
+
const usage =
|
|
166
|
+
(index === 5 ? Buffer.UNIFORM : Buffer.STORAGE) | Buffer.COPY_SRC | Buffer.COPY_DST;
|
|
167
|
+
const buffer = graph.device.createBuffer({
|
|
168
|
+
id: this.id + '-' + names[index],
|
|
169
|
+
data: values,
|
|
170
|
+
usage
|
|
171
|
+
});
|
|
172
|
+
this.buffers.push(buffer);
|
|
173
|
+
return graph.importBuffer({id: buffer.id, byteLength: buffer.byteLength, usage}, buffer);
|
|
174
|
+
});
|
|
175
|
+
const resources: GraphBufferUse[] = handles.map((buffer, index) => ({
|
|
176
|
+
buffer,
|
|
177
|
+
usage: index === 5 ? 'uniform' : index < 2 ? 'storage-read' : 'storage-read-write'
|
|
178
|
+
}));
|
|
179
|
+
const shaderLayout: ShaderLayout = {
|
|
180
|
+
attributes: [],
|
|
181
|
+
bindings: names.map(
|
|
182
|
+
(name, location): BindingDeclaration =>
|
|
183
|
+
location === 5
|
|
184
|
+
? {name, location, group: 0, type: 'uniform'}
|
|
185
|
+
: {name, location, group: 0, type: location < 2 ? 'read-only-storage' : 'storage'}
|
|
186
|
+
)
|
|
187
|
+
};
|
|
188
|
+
const props = {
|
|
189
|
+
source: getArticulationTreeShader(
|
|
190
|
+
this.links.length,
|
|
191
|
+
Boolean(this.contact),
|
|
192
|
+
this.publishMatrices
|
|
193
|
+
),
|
|
194
|
+
shaderLayout
|
|
195
|
+
};
|
|
196
|
+
const executable = (bound: BoundKernel): GPUCommandGraphComputeExecutable<P> => ({
|
|
197
|
+
encode: ({computePass, getBuffer}) => {
|
|
198
|
+
const bindings: Record<string, Binding> = {};
|
|
199
|
+
names.forEach((name, index) => {
|
|
200
|
+
bindings[name] = getBuffer(handles[index]);
|
|
201
|
+
});
|
|
202
|
+
bound.kernel.dispatch(computePass, {bindings, x: 1});
|
|
203
|
+
},
|
|
204
|
+
destroy: () => bound.kernel.destroy()
|
|
205
|
+
});
|
|
206
|
+
graph.addComputePass({
|
|
207
|
+
id: handles[0].id + '-joint-space',
|
|
208
|
+
description: 'Integrate joint-space dynamics and publish articulated link poses.',
|
|
209
|
+
resources,
|
|
210
|
+
compile: ({device}) => executable(createBoundKernel(device, props)),
|
|
211
|
+
compileAsync: async ({device}) => executable(await createBoundKernelAsync(device, props))
|
|
212
|
+
});
|
|
213
|
+
return handles[3];
|
|
214
|
+
} catch (error) {
|
|
215
|
+
this.destroy();
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
private driveIndex(id: string) {
|
|
221
|
+
const index = this.indices.get(id);
|
|
222
|
+
if (this.destroyed || !this.buffers.length || index === undefined || !this.links[index].axis)
|
|
223
|
+
throw new Error('Inactive or missing revolute drive');
|
|
224
|
+
return index;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Update the named revolute drive between submitted steps. */
|
|
228
|
+
setDrive(id: string, drive: GPUArticulationDrive) {
|
|
229
|
+
const index = this.driveIndex(id);
|
|
230
|
+
const values = new Float32Array([
|
|
231
|
+
drive.angle,
|
|
232
|
+
drive.stiffness,
|
|
233
|
+
drive.damping,
|
|
234
|
+
drive.maximumTorque
|
|
235
|
+
]);
|
|
236
|
+
if (!values.every(Number.isFinite) || values.slice(1).some(value => value < 0))
|
|
237
|
+
throw new Error('Invalid articulation drive');
|
|
238
|
+
this.buffers[1].write(values, index * 16);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Signed motor torque during the most recently accepted step. */
|
|
242
|
+
async readTorque(id: string) {
|
|
243
|
+
const index = this.driveIndex(id);
|
|
244
|
+
const bytes = await this.buffers[2].readAsync(index * 16 + 8, 4);
|
|
245
|
+
return new DataView(bytes.buffer, bytes.byteOffset, 4).getFloat32(0, true);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Read COM poses and world-space velocities in link order after stepping. */
|
|
249
|
+
async readState() {
|
|
250
|
+
const bytes = await this.buffers[3].readAsync();
|
|
251
|
+
const values = new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4);
|
|
252
|
+
return this.links.map((_, index) => {
|
|
253
|
+
const start = index * 16;
|
|
254
|
+
return {
|
|
255
|
+
position: Array.from(values.subarray(start, start + 3)),
|
|
256
|
+
orientation: Array.from(values.subarray(start + 4, start + 8)),
|
|
257
|
+
velocity: Array.from(values.subarray(start + 8, start + 11)),
|
|
258
|
+
angularVelocity: Array.from(values.subarray(start + 12, start + 15))
|
|
259
|
+
};
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
/** Numerical failure latches and preserves the last accepted state. */
|
|
263
|
+
async readSimulationDiagnostics() {
|
|
264
|
+
const bytes = await this.buffers[4].readAsync();
|
|
265
|
+
const values = new Uint32Array(bytes.buffer, bytes.byteOffset, 2);
|
|
266
|
+
return {
|
|
267
|
+
steps: values[0],
|
|
268
|
+
complete: values[1] === 0,
|
|
269
|
+
...(this.contact
|
|
270
|
+
? {normalForce: new DataView(bytes.buffer, bytes.byteOffset).getFloat32(8, true)}
|
|
271
|
+
: {})
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
destroy() {
|
|
275
|
+
this.destroyed = true;
|
|
276
|
+
for (const buffer of this.buffers) buffer.destroy();
|
|
277
|
+
this.buffers.length = 0;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
@@ -3265,6 +3265,95 @@ struct Command { impulse: vec4f, point: vec4f, orientation: vec4f, bodyIndex: ve
|
|
|
3265
3265
|
return structuredClone(joint);
|
|
3266
3266
|
}
|
|
3267
3267
|
|
|
3268
|
+
/** Command a named angular force drive. Angles are radians and maximum torque is N·m. */
|
|
3269
|
+
setAngularActuator(
|
|
3270
|
+
id: string,
|
|
3271
|
+
axis: 0 | 1 | 2,
|
|
3272
|
+
command: {angle: number; stiffness?: number; damping?: number; maximumTorque?: number}
|
|
3273
|
+
): void {
|
|
3274
|
+
const index = this.findAngularActuator(id, axis);
|
|
3275
|
+
const joint = this.props.joints![index];
|
|
3276
|
+
if (joint.type !== 'general' || !joint.drive) throw new Error('Expected angular drive');
|
|
3277
|
+
if (
|
|
3278
|
+
command.stiffness === undefined &&
|
|
3279
|
+
command.damping === undefined &&
|
|
3280
|
+
command.maximumTorque === undefined
|
|
3281
|
+
) {
|
|
3282
|
+
this.setAngularDriveTargets(new Map([[index, command.angle]]));
|
|
3283
|
+
return;
|
|
3284
|
+
}
|
|
3285
|
+
this.setJointParameters(index, {
|
|
3286
|
+
...joint,
|
|
3287
|
+
drive: {
|
|
3288
|
+
...joint.drive,
|
|
3289
|
+
positionTarget: command.angle,
|
|
3290
|
+
stiffness: command.stiffness ?? joint.drive.stiffness,
|
|
3291
|
+
damping: command.damping ?? joint.drive.damping,
|
|
3292
|
+
maximumForce: command.maximumTorque ?? joint.drive.maximumForce
|
|
3293
|
+
}
|
|
3294
|
+
});
|
|
3295
|
+
}
|
|
3296
|
+
|
|
3297
|
+
/** Read the signed mean motor torque from the most recently submitted step. */
|
|
3298
|
+
async readAngularActuatorTorque(id: string, axis: 0 | 1 | 2): Promise<number> {
|
|
3299
|
+
const index = this.findAngularActuator(id, axis);
|
|
3300
|
+
if (!this.targets || this.destroyed) throw new Error('Inactive world');
|
|
3301
|
+
const bytes = await this.targets.readAsync(
|
|
3302
|
+
this.getJointStateOffset() + index * RIGID_BODY_JOINT_STATE_BYTES,
|
|
3303
|
+
4
|
|
3304
|
+
);
|
|
3305
|
+
return new DataView(bytes.buffer, bytes.byteOffset, 4).getFloat32(0, true) / this.timeStep;
|
|
3306
|
+
}
|
|
3307
|
+
|
|
3308
|
+
private findAngularActuator(id: string, axis: number): number {
|
|
3309
|
+
const indices = (this.props.joints ?? []).flatMap((joint, index) =>
|
|
3310
|
+
joint.id === id &&
|
|
3311
|
+
joint.type === 'general' &&
|
|
3312
|
+
joint.angular &&
|
|
3313
|
+
joint.drive &&
|
|
3314
|
+
joint.drive.mode !== 'acceleration' &&
|
|
3315
|
+
joint.axes[0] === axis
|
|
3316
|
+
? [index]
|
|
3317
|
+
: []
|
|
3318
|
+
);
|
|
3319
|
+
if (indices.length !== 1) throw new Error('Missing or ambiguous angular force drive');
|
|
3320
|
+
return indices[0];
|
|
3321
|
+
}
|
|
3322
|
+
|
|
3323
|
+
/** Retarget authored angular drive rows between steps while preserving the other joints' settings. */
|
|
3324
|
+
setAngularDriveTargets(targets: ReadonlyMap<number, number>): void {
|
|
3325
|
+
/** Validate the entire batch before publishing writes to the GPU. */
|
|
3326
|
+
const joints = [...(this.props.joints ?? [])];
|
|
3327
|
+
if (!this.targets || !this.sleepStates || this.destroyed) throw new Error('Inactive world');
|
|
3328
|
+
for (const [index, angle] of targets) {
|
|
3329
|
+
const joint = joints[index];
|
|
3330
|
+
if (
|
|
3331
|
+
joint?.type !== 'general' ||
|
|
3332
|
+
!joint.angular ||
|
|
3333
|
+
!joint.drive ||
|
|
3334
|
+
!Number.isFinite(Math.fround(angle))
|
|
3335
|
+
) {
|
|
3336
|
+
throw new Error('Invalid angular drive target');
|
|
3337
|
+
}
|
|
3338
|
+
if (Math.fround(joint.drive.positionTarget ?? 0) === Math.fround(angle)) continue;
|
|
3339
|
+
joints[index] = {...joint, drive: {...joint.drive, positionTarget: angle}};
|
|
3340
|
+
}
|
|
3341
|
+
const drivenBodies = new Set<number>();
|
|
3342
|
+
for (const [index, angle] of targets) {
|
|
3343
|
+
if (joints[index] === this.props.joints?.[index]) continue;
|
|
3344
|
+
drivenBodies.add(joints[index].first);
|
|
3345
|
+
drivenBodies.add(joints[index].second);
|
|
3346
|
+
// Word 40 is general.drive.x in packRigidBodyJointState.
|
|
3347
|
+
this.targets.write(
|
|
3348
|
+
new Float32Array([angle]),
|
|
3349
|
+
this.getJointStateOffset() + index * RIGID_BODY_JOINT_STATE_BYTES + 40 * 4
|
|
3350
|
+
);
|
|
3351
|
+
}
|
|
3352
|
+
this.props.joints = joints;
|
|
3353
|
+
// Wake the endpoints while preserving warm starts for contacts already at rest.
|
|
3354
|
+
for (const body of drivenBodies) this.sleepStates.write(new Float32Array(2), body * 16);
|
|
3355
|
+
}
|
|
3356
|
+
|
|
3268
3357
|
/** Update an existing constraint's response without recompiling or changing attachment topology. */
|
|
3269
3358
|
setJointParameters(index: number, parameters: GPURigidBodyJoint): void {
|
|
3270
3359
|
this.setJointParameterEdits([{index, parameters}]);
|
package/src/gpu-physics/index.ts
CHANGED
|
@@ -163,3 +163,10 @@ export type {GPURigidBodyJointLimit, GPURigidBodyJointDrive} from './joint-types
|
|
|
163
163
|
export {rotateRigidBodyVector, normalizeRigidBodyRotation} from './rigid-body-properties';
|
|
164
164
|
export type {GPURigidBodyGeneralConstraint} from './rigid-body-general-joints';
|
|
165
165
|
export type {GPURigidBodyVehicle} from './rigid-body-vehicle';
|
|
166
|
+
|
|
167
|
+
export {
|
|
168
|
+
GPUArticulationTree,
|
|
169
|
+
type GPUArticulationLink,
|
|
170
|
+
type GPUArticulationContact,
|
|
171
|
+
type GPUArticulationDrive
|
|
172
|
+
} from './gpu-articulation-tree';
|
|
@@ -17,6 +17,8 @@ export type GPURigidBodyJointFrame = {
|
|
|
17
17
|
orientation?: readonly [number, number, number, number];
|
|
18
18
|
};
|
|
19
19
|
type JointBase = {
|
|
20
|
+
/** Instance-local name of the authored joint. All compiled rows share this name. */
|
|
21
|
+
id?: string;
|
|
20
22
|
/** Can be toggled between submitted steps without rebuilding the world. */
|
|
21
23
|
enabled?: boolean;
|
|
22
24
|
first: number;
|