urdf-loader 0.10.3 → 0.10.5

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.
@@ -1,402 +1,395 @@
1
- import { Object3D, Vector3 } from 'three';
2
-
3
- class URDFBase extends Object3D {
4
-
5
- constructor(...args) {
6
-
7
- super(...args);
8
- this.urdfNode = null;
9
- this.urdfName = '';
10
-
11
- }
12
-
13
- copy(source, recursive) {
14
-
15
- super.copy(source, recursive);
16
-
17
- this.urdfNode = source.urdfNode;
18
- this.urdfName = source.urdfName;
19
-
20
- return this;
21
-
22
- }
23
-
24
- }
25
-
26
- class URDFCollider extends URDFBase {
27
-
28
- constructor(...args) {
29
-
30
- super(...args);
31
- this.isURDFCollider = true;
32
- this.type = 'URDFCollider';
33
-
34
- }
35
-
36
- }
37
-
38
- class URDFVisual extends URDFBase {
39
-
40
- constructor(...args) {
41
-
42
- super(...args);
43
- this.isURDFVisual = true;
44
- this.type = 'URDFVisual';
45
-
46
- }
47
-
48
- }
49
-
50
- class URDFLink extends URDFBase {
51
-
52
- constructor(...args) {
53
-
54
- super(...args);
55
- this.isURDFLink = true;
56
- this.type = 'URDFLink';
57
-
58
- }
59
-
60
- }
61
-
62
- class URDFJoint extends URDFBase {
63
-
64
- get jointType() {
65
-
66
- return this._jointType;
67
-
68
- }
69
-
70
- set jointType(v) {
71
-
72
- if (this.jointType === v) return;
73
- this._jointType = v;
74
- this.matrixWorldNeedsUpdate = true;
75
- switch (v) {
76
-
77
- case 'fixed':
78
- this.jointValue = [];
79
- break;
80
-
81
- case 'continuous':
82
- case 'revolute':
83
- case 'prismatic':
84
- this.jointValue = new Array(1).fill(0);
85
- break;
86
-
87
- case 'planar':
88
- this.jointValue = new Array(2).fill(0);
89
- break;
90
-
91
- case 'floating':
92
- this.jointValue = new Array(6).fill(0);
93
- break;
94
-
95
- }
96
-
97
- }
98
-
99
- get angle() {
100
-
101
- return this.jointValue[0];
102
-
103
- }
104
-
105
- constructor(...args) {
106
-
107
- super(...args);
108
-
109
- this.isURDFJoint = true;
110
- this.type = 'URDFJoint';
111
-
112
- this.jointValue = null;
113
- this.jointType = 'fixed';
114
- this.axis = new Vector3(1, 0, 0);
115
- this.limit = { lower: 0, upper: 0 };
116
- this.ignoreLimits = false;
117
-
118
- this.origPosition = null;
119
- this.origQuaternion = null;
120
-
121
- this.mimicJoints = [];
122
-
123
- }
124
-
125
- /* Overrides */
126
- copy(source, recursive) {
127
-
128
- super.copy(source, recursive);
129
-
130
- this.jointType = source.jointType;
131
- this.axis = source.axis.clone();
132
- this.limit.lower = source.limit.lower;
133
- this.limit.upper = source.limit.upper;
134
- this.ignoreLimits = false;
135
-
136
- this.jointValue = [...source.jointValue];
137
-
138
- this.origPosition = source.origPosition ? source.origPosition.clone() : null;
139
- this.origQuaternion = source.origQuaternion ? source.origQuaternion.clone() : null;
140
-
141
- this.mimicJoints = [...source.mimicJoints];
142
-
143
- return this;
144
-
145
- }
146
-
147
- /* Public Functions */
148
- setJointValue(...values) {
149
-
150
- values = values.map(v => parseFloat(v));
151
-
152
- if (!this.origPosition || !this.origQuaternion) {
153
-
154
- this.origPosition = this.position.clone();
155
- this.origQuaternion = this.quaternion.clone();
156
-
157
- }
158
-
159
- let didUpdate = false;
160
-
161
- this.mimicJoints.forEach(joint => {
162
-
163
- didUpdate = joint.updateFromMimickedJoint(...values) || didUpdate;
164
-
165
- });
166
-
167
- switch (this.jointType) {
168
-
169
- case 'fixed': {
170
-
171
- return didUpdate;
172
-
173
- }
174
- case 'continuous':
175
- case 'revolute': {
176
-
177
- let angle = values[0];
178
- if (angle == null) return didUpdate;
179
- if (angle === this.jointValue[0]) return didUpdate;
180
-
181
- if (!this.ignoreLimits && this.jointType === 'revolute') {
182
-
183
- angle = Math.min(this.limit.upper, angle);
184
- angle = Math.max(this.limit.lower, angle);
185
-
186
- }
187
-
188
- this.quaternion
189
- .setFromAxisAngle(this.axis, angle)
190
- .premultiply(this.origQuaternion);
191
-
192
- if (this.jointValue[0] !== angle) {
193
-
194
- this.jointValue[0] = angle;
195
- this.matrixWorldNeedsUpdate = true;
196
- return true;
197
-
198
- } else {
199
-
200
- return didUpdate;
201
-
202
- }
203
-
204
- }
205
-
206
- case 'prismatic': {
207
-
208
- let pos = values[0];
209
- if (pos == null) return didUpdate;
210
- if (pos === this.jointValue[0]) return didUpdate;
211
-
212
- if (!this.ignoreLimits) {
213
-
214
- pos = Math.min(this.limit.upper, pos);
215
- pos = Math.max(this.limit.lower, pos);
216
-
217
- }
218
-
219
- this.position.copy(this.origPosition);
220
- this.position.addScaledVector(this.axis, pos);
221
-
222
- if (this.jointValue[0] !== pos) {
223
-
224
- this.jointValue[0] = pos;
225
- this.matrixWorldNeedsUpdate = true;
226
- return true;
227
-
228
- } else {
229
-
230
- return didUpdate;
231
-
232
- }
233
-
234
- }
235
-
236
- case 'floating':
237
- case 'planar':
238
- // TODO: Support these joint types
239
- console.warn(`'${ this.jointType }' joint not yet supported`);
240
-
241
- }
242
-
243
- return didUpdate;
244
-
245
- }
246
-
247
- }
248
-
249
- class URDFMimicJoint extends URDFJoint {
250
-
251
- constructor(...args) {
252
-
253
- super(...args);
254
- this.type = 'URDFMimicJoint';
255
- this.mimicJoint = null;
256
- this.offset = 0;
257
- this.multiplier = 1;
258
-
259
- }
260
-
261
- updateFromMimickedJoint(...values) {
262
-
263
- const modifiedValues = values.map(x => x * this.multiplier + this.offset);
264
- return super.setJointValue(...modifiedValues);
265
-
266
- }
267
-
268
- /* Overrides */
269
- setJointValue(...values) {
270
-
271
- console.warn(`URDFMimicJoint: Setting the joint value of mimic joint "${ this.urdfName }" will cause it to be out of sync.`);
272
- return super.setJointValue(...values);
273
- }
274
-
275
- /* Overrides */
276
- copy(source, recursive) {
277
-
278
- super.copy(source, recursive);
279
-
280
- this.mimicJoint = source.mimicJoint;
281
- this.offset = source.offset;
282
- this.multiplier = source.multiplier;
283
-
284
- return this;
285
-
286
- }
287
-
288
- }
289
-
290
- class URDFRobot extends URDFLink {
291
-
292
- constructor(...args) {
293
-
294
- super(...args);
295
- this.isURDFRobot = true;
296
- this.urdfNode = null;
297
-
298
- this.urdfRobotNode = null;
299
- this.robotName = null;
300
-
301
- this.links = null;
302
- this.joints = null;
303
- this.colliders = null;
304
- this.visual = null;
305
- this.frames = null;
306
-
307
- }
308
-
309
- copy(source, recursive) {
310
-
311
- super.copy(source, recursive);
312
-
313
- this.urdfRobotNode = source.urdfRobotNode;
314
- this.robotName = source.robotName;
315
-
316
- this.links = {};
317
- this.joints = {};
318
- this.colliders = {};
319
- this.visual = {};
320
-
321
- this.traverse(c => {
322
-
323
- if (c.isURDFJoint && c.urdfName in source.joints) {
324
-
325
- this.joints[c.urdfName] = c;
326
-
327
- }
328
-
329
- if (c.isURDFLink && c.urdfName in source.links) {
330
-
331
- this.links[c.urdfName] = c;
332
-
333
- }
334
-
335
- if (c.isURDFCollider && c.urdfName in source.colliders) {
336
-
337
- this.colliders[c.urdfName] = c;
338
-
339
- }
340
-
341
- if (c.isURDFVisual && c.urdfName in source.visual) {
342
-
343
- this.visual[c.urdfName] = c;
344
-
345
- }
346
-
347
- });
348
-
349
- this.frames = {
350
- ...this.colliders,
351
- ...this.visual,
352
- ...this.links,
353
- ...this.joints,
354
- };
355
-
356
- return this;
357
-
358
- }
359
-
360
- getFrame(name) {
361
-
362
- return this.frames[name];
363
-
364
- }
365
-
366
- setJointValue(jointName, ...angle) {
367
-
368
- const joint = this.joints[jointName];
369
- if (joint) {
370
-
371
- return joint.setJointValue(...angle);
372
-
373
- }
374
-
375
- return false;
376
- }
377
-
378
- setJointValues(values) {
379
-
380
- let didChange = false;
381
- for (const name in values) {
382
-
383
- const value = values[name];
384
- if (Array.isArray(value)) {
385
-
386
- didChange = this.setJointValue(name, ...value) || didChange;
387
-
388
- } else {
389
-
390
- didChange = this.setJointValue(name, value) || didChange;
391
-
392
- }
393
-
394
- }
395
-
396
- return didChange;
397
-
398
- }
399
-
400
- }
401
-
402
- export { URDFRobot, URDFLink, URDFJoint, URDFMimicJoint, URDFVisual, URDFCollider };
1
+ import { Object3D, Vector3 } from 'three';
2
+
3
+ class URDFBase extends Object3D {
4
+
5
+ constructor(...args) {
6
+
7
+ super(...args);
8
+ this.urdfNode = null;
9
+ this.urdfName = '';
10
+
11
+ }
12
+
13
+ copy(source, recursive) {
14
+
15
+ super.copy(source, recursive);
16
+
17
+ this.urdfNode = source.urdfNode;
18
+ this.urdfName = source.urdfName;
19
+
20
+ return this;
21
+
22
+ }
23
+
24
+ }
25
+
26
+ class URDFCollider extends URDFBase {
27
+
28
+ constructor(...args) {
29
+
30
+ super(...args);
31
+ this.isURDFCollider = true;
32
+ this.type = 'URDFCollider';
33
+
34
+ }
35
+
36
+ }
37
+
38
+ class URDFVisual extends URDFBase {
39
+
40
+ constructor(...args) {
41
+
42
+ super(...args);
43
+ this.isURDFVisual = true;
44
+ this.type = 'URDFVisual';
45
+
46
+ }
47
+
48
+ }
49
+
50
+ class URDFLink extends URDFBase {
51
+
52
+ constructor(...args) {
53
+
54
+ super(...args);
55
+ this.isURDFLink = true;
56
+ this.type = 'URDFLink';
57
+
58
+ }
59
+
60
+ }
61
+
62
+ class URDFJoint extends URDFBase {
63
+
64
+ get jointType() {
65
+
66
+ return this._jointType;
67
+
68
+ }
69
+
70
+ set jointType(v) {
71
+
72
+ if (this.jointType === v) return;
73
+ this._jointType = v;
74
+ this.matrixWorldNeedsUpdate = true;
75
+ switch (v) {
76
+
77
+ case 'fixed':
78
+ this.jointValue = [];
79
+ break;
80
+
81
+ case 'continuous':
82
+ case 'revolute':
83
+ case 'prismatic':
84
+ this.jointValue = new Array(1).fill(0);
85
+ break;
86
+
87
+ case 'planar':
88
+ this.jointValue = new Array(2).fill(0);
89
+ break;
90
+
91
+ case 'floating':
92
+ this.jointValue = new Array(6).fill(0);
93
+ break;
94
+
95
+ }
96
+
97
+ }
98
+
99
+ get angle() {
100
+
101
+ return this.jointValue[0];
102
+
103
+ }
104
+
105
+ constructor(...args) {
106
+
107
+ super(...args);
108
+
109
+ this.isURDFJoint = true;
110
+ this.type = 'URDFJoint';
111
+
112
+ this.jointValue = null;
113
+ this.jointType = 'fixed';
114
+ this.axis = new Vector3(1, 0, 0);
115
+ this.limit = { lower: 0, upper: 0 };
116
+ this.ignoreLimits = false;
117
+
118
+ this.origPosition = null;
119
+ this.origQuaternion = null;
120
+
121
+ this.mimicJoints = [];
122
+
123
+ }
124
+
125
+ /* Overrides */
126
+ copy(source, recursive) {
127
+
128
+ super.copy(source, recursive);
129
+
130
+ this.jointType = source.jointType;
131
+ this.axis = source.axis.clone();
132
+ this.limit.lower = source.limit.lower;
133
+ this.limit.upper = source.limit.upper;
134
+ this.ignoreLimits = false;
135
+
136
+ this.jointValue = [...source.jointValue];
137
+
138
+ this.origPosition = source.origPosition ? source.origPosition.clone() : null;
139
+ this.origQuaternion = source.origQuaternion ? source.origQuaternion.clone() : null;
140
+
141
+ this.mimicJoints = [...source.mimicJoints];
142
+
143
+ return this;
144
+
145
+ }
146
+
147
+ /* Public Functions */
148
+ setJointValue(...values) {
149
+
150
+ values = values.map(v => parseFloat(v));
151
+
152
+ if (!this.origPosition || !this.origQuaternion) {
153
+
154
+ this.origPosition = this.position.clone();
155
+ this.origQuaternion = this.quaternion.clone();
156
+
157
+ }
158
+
159
+ let didUpdate = false;
160
+
161
+ this.mimicJoints.forEach(joint => {
162
+
163
+ didUpdate = joint.updateFromMimickedJoint(...values) || didUpdate;
164
+
165
+ });
166
+
167
+ switch (this.jointType) {
168
+
169
+ case 'fixed': {
170
+
171
+ return didUpdate;
172
+
173
+ }
174
+ case 'continuous':
175
+ case 'revolute': {
176
+
177
+ let angle = values[0];
178
+ if (angle == null) return didUpdate;
179
+ if (angle === this.jointValue[0]) return didUpdate;
180
+
181
+ if (!this.ignoreLimits && this.jointType === 'revolute') {
182
+
183
+ angle = Math.min(this.limit.upper, angle);
184
+ angle = Math.max(this.limit.lower, angle);
185
+
186
+ }
187
+
188
+ this.quaternion
189
+ .setFromAxisAngle(this.axis, angle)
190
+ .premultiply(this.origQuaternion);
191
+
192
+ if (this.jointValue[0] !== angle) {
193
+
194
+ this.jointValue[0] = angle;
195
+ this.matrixWorldNeedsUpdate = true;
196
+ return true;
197
+
198
+ } else {
199
+
200
+ return didUpdate;
201
+
202
+ }
203
+
204
+ }
205
+
206
+ case 'prismatic': {
207
+
208
+ let pos = values[0];
209
+ if (pos == null) return didUpdate;
210
+ if (pos === this.jointValue[0]) return didUpdate;
211
+
212
+ if (!this.ignoreLimits) {
213
+
214
+ pos = Math.min(this.limit.upper, pos);
215
+ pos = Math.max(this.limit.lower, pos);
216
+
217
+ }
218
+
219
+ this.position.copy(this.origPosition);
220
+ this.position.addScaledVector(this.axis, pos);
221
+
222
+ if (this.jointValue[0] !== pos) {
223
+
224
+ this.jointValue[0] = pos;
225
+ this.matrixWorldNeedsUpdate = true;
226
+ return true;
227
+
228
+ } else {
229
+
230
+ return didUpdate;
231
+
232
+ }
233
+
234
+ }
235
+
236
+ case 'floating':
237
+ case 'planar':
238
+ // TODO: Support these joint types
239
+ console.warn(`'${ this.jointType }' joint not yet supported`);
240
+
241
+ }
242
+
243
+ return didUpdate;
244
+
245
+ }
246
+
247
+ }
248
+
249
+ class URDFMimicJoint extends URDFJoint {
250
+
251
+ constructor(...args) {
252
+
253
+ super(...args);
254
+ this.type = 'URDFMimicJoint';
255
+ this.mimicJoint = null;
256
+ this.offset = 0;
257
+ this.multiplier = 1;
258
+
259
+ }
260
+
261
+ updateFromMimickedJoint(...values) {
262
+
263
+ const modifiedValues = values.map(x => x * this.multiplier + this.offset);
264
+ return super.setJointValue(...modifiedValues);
265
+
266
+ }
267
+
268
+ /* Overrides */
269
+ copy(source, recursive) {
270
+
271
+ super.copy(source, recursive);
272
+
273
+ this.mimicJoint = source.mimicJoint;
274
+ this.offset = source.offset;
275
+ this.multiplier = source.multiplier;
276
+
277
+ return this;
278
+
279
+ }
280
+
281
+ }
282
+
283
+ class URDFRobot extends URDFLink {
284
+
285
+ constructor(...args) {
286
+
287
+ super(...args);
288
+ this.isURDFRobot = true;
289
+ this.urdfNode = null;
290
+
291
+ this.urdfRobotNode = null;
292
+ this.robotName = null;
293
+
294
+ this.links = null;
295
+ this.joints = null;
296
+ this.colliders = null;
297
+ this.visual = null;
298
+ this.frames = null;
299
+
300
+ }
301
+
302
+ copy(source, recursive) {
303
+
304
+ super.copy(source, recursive);
305
+
306
+ this.urdfRobotNode = source.urdfRobotNode;
307
+ this.robotName = source.robotName;
308
+
309
+ this.links = {};
310
+ this.joints = {};
311
+ this.colliders = {};
312
+ this.visual = {};
313
+
314
+ this.traverse(c => {
315
+
316
+ if (c.isURDFJoint && c.urdfName in source.joints) {
317
+
318
+ this.joints[c.urdfName] = c;
319
+
320
+ }
321
+
322
+ if (c.isURDFLink && c.urdfName in source.links) {
323
+
324
+ this.links[c.urdfName] = c;
325
+
326
+ }
327
+
328
+ if (c.isURDFCollider && c.urdfName in source.colliders) {
329
+
330
+ this.colliders[c.urdfName] = c;
331
+
332
+ }
333
+
334
+ if (c.isURDFVisual && c.urdfName in source.visual) {
335
+
336
+ this.visual[c.urdfName] = c;
337
+
338
+ }
339
+
340
+ });
341
+
342
+ this.frames = {
343
+ ...this.colliders,
344
+ ...this.visual,
345
+ ...this.links,
346
+ ...this.joints,
347
+ };
348
+
349
+ return this;
350
+
351
+ }
352
+
353
+ getFrame(name) {
354
+
355
+ return this.frames[name];
356
+
357
+ }
358
+
359
+ setJointValue(jointName, ...angle) {
360
+
361
+ const joint = this.joints[jointName];
362
+ if (joint) {
363
+
364
+ return joint.setJointValue(...angle);
365
+
366
+ }
367
+
368
+ return false;
369
+ }
370
+
371
+ setJointValues(values) {
372
+
373
+ let didChange = false;
374
+ for (const name in values) {
375
+
376
+ const value = values[name];
377
+ if (Array.isArray(value)) {
378
+
379
+ didChange = this.setJointValue(name, ...value) || didChange;
380
+
381
+ } else {
382
+
383
+ didChange = this.setJointValue(name, value) || didChange;
384
+
385
+ }
386
+
387
+ }
388
+
389
+ return didChange;
390
+
391
+ }
392
+
393
+ }
394
+
395
+ export { URDFRobot, URDFLink, URDFJoint, URDFMimicJoint, URDFVisual, URDFCollider };