-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathURDFLoader.test.js
More file actions
659 lines (471 loc) · 20.6 KB
/
Copy pathURDFLoader.test.js
File metadata and controls
659 lines (471 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import { JSDOM } from 'jsdom';
import { Mesh, Color } from 'three';
import fetch from 'node-fetch';
import URDFLoader from '../src/URDFLoader.js';
const jsdom = new JSDOM();
const window = jsdom.window;
global.DOMParser = window.DOMParser;
global.XMLSerializer = window.XMLSerializer;
global.Document = window.Document;
global.Element = window.Element;
global.XMLHttpRequest = window.XMLHttpRequest;
global.fetch = fetch;
function emptyLoadMeshCallback(url, manager, done) {
done(new Mesh());
}
function compareRobots(ra, rb) {
if (ra.isURDFRobot) {
expect(Object.keys(ra.links).sort()).toEqual(Object.keys(rb.links).sort());
expect(Object.keys(ra.joints).sort()).toEqual(Object.keys(rb.joints).sort());
expect(Object.keys(ra.colliders).sort()).toEqual(Object.keys(rb.colliders).sort());
expect(Object.keys(ra.visual).sort()).toEqual(Object.keys(rb.visual).sort());
}
expect(ra.name).toEqual(rb.name);
expect(ra.type).toEqual(rb.type);
expect(ra.geometry).toEqual(rb.geometry);
expect(ra.material).toEqual(rb.material);
expect(ra.urdfNode).toEqual(rb.urdfNode);
expect(ra.urdfName).toEqual(rb.urdfName);
expect(ra.isMesh).toEqual(rb.isMesh);
expect(ra.isURDFLink).toEqual(rb.isURDFLink);
expect(ra.isURDFRobot).toEqual(rb.isURDFRobot);
expect(ra.isURDFJoint).toEqual(rb.isURDFJoint);
expect(ra.isURDFCollider).toEqual(rb.isURDFCollider);
switch (ra.type) {
case 'URDFRobot':
expect(Object.keys(ra.joints)).toEqual(Object.keys(rb.joints));
expect(Object.keys(ra.links)).toEqual(Object.keys(rb.links));
break;
case 'URDFJoint':
case 'URDFMimicJoint':
expect(ra.jointType).toEqual(rb.jointType);
expect(ra.axis).toEqual(rb.axis);
expect(ra.limit).toEqual(rb.limit);
expect(ra.ignoreLimits).toEqual(rb.ignoreLimits);
expect(ra.jointValue).toEqual(rb.jointValue);
expect(ra.origPosition).toEqual(rb.origPosition);
expect(ra.origQuaternion).toEqual(rb.origQuaternion);
// Just compare the names of the mimic joint list
expect(ra.mimicJoints.map(x => x.urdfName)).toEqual(rb.mimicJoints.map(x => x.urdfName));
if (ra.type === 'URDFMimicJoint') {
expect(ra.mimicJoint).toEqual(rb.mimicJoint);
expect(ra.offset).toEqual(rb.offset);
expect(ra.multiplier).toEqual(rb.multiplier);
}
break;
}
for (let i = 0; i < ra.children.length; i++) {
compareRobots(ra.children[i], rb.children[i]);
}
}
describe('File Argument', () => {
it('should work if the file is already parsed', async() => {
const loader = new URDFLoader();
loader.packages = 'https://raw.githubusercontent.com/gkjohnson/urdf-loaders/master/urdf/TriATHLETE_Climbing';
loader.workingPath = 'https://raw.githubusercontent.com/gkjohnson/urdf-loaders/master/urdf/TriATHLETE_Climbing/urdf/';
const req = await fetch('https://raw.githubusercontent.com/gkjohnson/urdf-loaders/master/urdf/TriATHLETE_Climbing/urdf/TriATHLETE.URDF');
const xmlContent = await req.text();
const parsedContent = new DOMParser().parseFromString(xmlContent, 'text/xml');
const documentRobot = loader.parse(parsedContent);
const rootRobot = loader.parse(parsedContent.children[0]);
expect(Object.keys(documentRobot.links).length).toEqual(28);
expect(Object.keys(rootRobot.links).length).toEqual(28);
});
});
describe('Options', () => {
describe('parseVisual, parseCollision', () => {
it('should exclude the elements if false', async() => {
const loader = new URDFLoader();
loader.packages = 'https://raw.githubusercontent.com/gkjohnson/nasa-urdf-robots/master/';
loader.loadMeshCb = emptyLoadMeshCallback;
loader.parseVisual = false;
loader.parseCollision = false;
let visTotal = 0;
let colTotal = 0;
const robot = await loader.loadAsync('https://raw.githubusercontent.com/gkjohnson/nasa-urdf-robots/master/r2_description/robots/r2b.urdf');
robot.traverse(c => {
if (c.isURDFCollider) {
colTotal++;
}
if (c.isURDFVisual) {
visTotal++;
}
});
expect(visTotal).toBe(0);
expect(colTotal).toBe(0);
});
it('should include the elements if true', async() => {
const loader = new URDFLoader();
loader.packages = 'https://raw.githubusercontent.com/gkjohnson/nasa-urdf-robots/master/';
loader.loadMeshCb = emptyLoadMeshCallback;
loader.parseVisual = true;
loader.parseCollision = true;
let visTotal = 0;
let colTotal = 0;
const robot = await loader.loadAsync('https://raw.githubusercontent.com/gkjohnson/nasa-urdf-robots/master/r2_description/robots/r2b.urdf');
robot.traverse(c => {
if (c.isURDFCollider) {
colTotal++;
}
if (c.isURDFVisual) {
visTotal++;
}
});
expect(visTotal).toBe(71);
expect(colTotal).toBe(71);
});
});
describe('loadMeshCb', () => {
it('should get called to load all meshes', async() => {
const loader = new URDFLoader();
loader.packages = 'https://raw.githubusercontent.com/gkjohnson/urdf-loaders/master/urdf/TriATHLETE_Climbing';
loader.loadMeshCb = (path, manager, done) => {
const mesh = new Mesh();
mesh.fromCallback = true;
done(mesh);
};
let fromCallbackCount = 0;
const robot = await loader.loadAsync('https://raw.githubusercontent.com/gkjohnson/urdf-loaders/master/urdf/TriATHLETE_Climbing/urdf/TriATHLETE.URDF');
robot.traverse(c => {
if (c.fromCallback) {
fromCallbackCount++;
}
});
expect(fromCallbackCount).toEqual(28);
});
it('should use correct workingPath to load meshes', async() => {
const loader = new URDFLoader();
loader.workingPath = 'https://raw.githubusercontent.com/mock-working-path';
loader.loadMeshCb = (path, manager, done) => {
const mesh = new Mesh();
expect(path).toContain('https://raw.githubusercontent.com/mock-working-path');
done(mesh);
};
await loader.loadAsync('https://raw.githubusercontent.com/gkjohnson/urdf-loaders/master/urdf/TriATHLETE_Climbing/urdf/TriATHLETE.URDF');
loader.workingPath = '';
loader.loadMeshCb = (path, manager, done) => {
const mesh = new Mesh();
expect(path).toContain('https://raw.githubusercontent.com/gkjohnson/urdf-loaders/master/urdf/TriATHLETE_Climbing/urdf');
done(mesh);
};
await loader.loadAsync('https://raw.githubusercontent.com/gkjohnson/urdf-loaders/master/urdf/TriATHLETE_Climbing/urdf/TriATHLETE.URDF');
expect(loader.workingPath).toBe('');
});
});
describe('packages', () => {
const urdf = `
<robot>
<link name="Body">
<visual>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<mesh filename="package://package1/path/to/model.stl" />
</geometry>
</visual>
</link>
<link name="Body">
<visual>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<mesh filename="package://package2/path/to/model2.stl" />
</geometry>
</visual>
</link>
</robot>
`;
it('should use the values from an object if set.', () => {
const loader = new URDFLoader();
loader.packages = {
'package1': 'path/to/package1',
'package2': 'path/to/package2',
};
const loaded = [];
loader.loadMeshCb = url => {
loaded.push(url);
};
loader.parse(urdf);
expect(loaded).toEqual([
'path/to/package1/path/to/model.stl',
'path/to/package2/path/to/model2.stl',
]);
});
it('should use the values from a function if set.', () => {
const loader = new URDFLoader();
loader.packages = pkg => {
switch (pkg) {
case 'package1':
return 'func/path/1';
case 'package2':
return 'func/path/2';
}
};
const loaded = [];
loader.loadMeshCb = url => {
loaded.push(url);
};
loader.parse(urdf);
expect(loaded).toEqual([
'func/path/1/path/to/model.stl',
'func/path/2/path/to/model2.stl',
]);
});
});
});
describe('Clone', () => {
it('should clone the robot exactly', async() => {
const loader = new URDFLoader();
loader.packages = 'https://raw.githubusercontent.com/gkjohnson/nasa-urdf-robots/master/';
loader.loadMeshCb = emptyLoadMeshCallback;
loader.parseVisual = true;
loader.parseCollision = true;
const robot = await loader.loadAsync('https://raw.githubusercontent.com/gkjohnson/nasa-urdf-robots/master/r2_description/robots/r2b.urdf');
compareRobots(robot, robot.clone());
});
it('should clone the robot exactly even when node names have been changed', async() => {
const loader = new URDFLoader();
loader.packages = 'https://raw.githubusercontent.com/gkjohnson/nasa-urdf-robots/master/';
loader.loadMeshCb = emptyLoadMeshCallback;
loader.parseVisual = true;
loader.parseCollision = true;
const robot = await loader.loadAsync('https://raw.githubusercontent.com/gkjohnson/nasa-urdf-robots/master/r2_description/robots/r2b.urdf');
robot.name = 'test 1';
compareRobots(robot, robot.clone());
});
it('should clone a robot with mimic joints exactly.', async() => {
const loader = new URDFLoader();
const robot = loader.parse(`
<robot name="TEST">
<link name="LINK1"/>
<joint name="A" type="continuous">
<origin xyz="0 0 0" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<parent link="LINK1"/>
<child link="LINK2"/>
</joint>
<link name="LINK2"/>
<joint name="B" type="continuous">
<origin xyz="0 0 0" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<parent link="LINK2"/>
<child link="LINK3"/>
<mimic joint="A" offset="-5" multiplier="23"/>
</joint>
<link name="LINK3"/>
</robot>
`);
compareRobots(robot, robot.clone());
});
});
describe('Load', () => {
it(`should call complete even if all meshes can't be loaded`, async() => {
const loader = new URDFLoader();
const urdf = `
<robot>
<link
name="Body">
<visual>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<mesh filename="../file/does/not/exist.stl" />
</geometry>
</visual>
</link>
</robot>
`;
loader.loadMeshCb = (path, manager, done) => done(null, new Error('Deliberate Test Error'));
loader.parse(urdf);
});
});
describe('Material Tags', () => {
it('should parse material colors and name.', () => {
const loader = new URDFLoader();
const res = loader.parse(`
<robot name="TEST">
<material name="Cyan">
<color rgba="0 1.0 1.0 1.0"/>
</material>
<link name="LINK">
<visual>
<geometry>
<box size="1 1 1"/>
</geometry>
<material name="Cyan"/>
</visual>
</link>
</robot>
`);
const material = res.children[0].children[0].material;
expect(material.name).toEqual('Cyan');
expect(material.color).toEqual(new Color(0, 1, 1).convertSRGBToLinear());
expect(material.transparent).toEqual(false);
expect(material.depthWrite).toEqual(true);
expect(material.opacity).toEqual(1.0);
});
it('should parse transparent materials correctly.', () => {
const loader = new URDFLoader();
const res = loader.parse(`
<robot name="TEST">
<material name="Cyan">
<color rgba="0 1.0 1.0 0.5"/>
</material>
<link name="LINK">
<visual>
<geometry>
<box size="1 1 1"/>
</geometry>
<material name="Cyan"/>
</visual>
</link>
</robot>
`);
const material = res.children[0].children[0].material;
expect(material.name).toEqual('Cyan');
expect(material.color).toEqual(new Color(0, 1, 1).convertSRGBToLinear());
expect(material.transparent).toEqual(true);
expect(material.depthWrite).toEqual(false);
expect(material.opacity).toEqual(0.5);
});
});
describe('TriATHLETE Climbing URDF', () => {
let robot;
beforeEach(async() => {
const loader = new URDFLoader();
loader.packages = 'https://raw.githubusercontent.com/gkjohnson/urdf-loaders/master/urdf/TriATHLETE_Climbing';
robot = await loader.loadAsync('https://raw.githubusercontent.com/gkjohnson/urdf-loaders/master/urdf/TriATHLETE_Climbing/urdf/TriATHLETE.URDF');
});
it('should have the correct number of links', async() => {
expect(Object.keys(robot.joints)).toHaveLength(27);
expect(Object.keys(robot.links)).toHaveLength(28);
});
it('should load the correct joint types', async() => {
for (const key in robot.joints) {
const joint = robot.joints[key];
const jointType = joint.jointType;
if (/^W/.test(key)) expect(jointType).toEqual('continuous');
else if (/^TC\d/.test(key)) expect(jointType).toEqual('prismatic');
else expect(jointType).toEqual('revolute');
}
});
it.todo('should respect joint limits for different joint types');
it('should load the robonaut model successfully.', async() => {
const loader = new URDFLoader();
loader.packages = 'https://raw.githubusercontent.com/gkjohnson/nasa-urdf-robots/master/';
loader.loadMeshCb = emptyLoadMeshCallback;
const robot = await loader.loadAsync('https://raw.githubusercontent.com/gkjohnson/nasa-urdf-robots/master/r2_description/robots/r2b.urdf');
expect(Object.keys(robot.links)).toHaveLength(128);
expect(Object.keys(robot.joints)).toHaveLength(127);
});
it('should load the valkyrie model successfully.', async() => {
const loader = new URDFLoader();
loader.packages = 'https://raw.githubusercontent.com/gkjohnson/nasa-urdf-robots/master/';
loader.loadMeshCb = emptyLoadMeshCallback;
const robot = await loader.loadAsync('https://raw.githubusercontent.com/gkjohnson/nasa-urdf-robots/master/val_description/model/robots/valkyrie_A.urdf');
expect(Object.keys(robot.links)).toHaveLength(69);
expect(Object.keys(robot.joints)).toHaveLength(68);
});
it('should load the a multipackage model successfully.', async() => {
const loader = new URDFLoader();
loader.packages = {
blending_end_effector:
'https://raw.githubusercontent.com/ros-industrial-consortium/godel/kinetic-devel/godel_robots/blending_end_effector',
abb_irb1200_support:
'https://raw.githubusercontent.com/ros-industrial/abb_experimental/kinetic-devel/abb_irb1200_support',
godel_irb1200_support:
'https://raw.githubusercontent.com/ros-industrial-consortium/godel/kinetic-devel/godel_robots/abb/godel_irb1200/godel_irb1200_support',
};
loader.loadMeshCb = emptyLoadMeshCallback;
const robot = await loader.loadAsync('https://raw.githubusercontent.com/ipa-jfh/urdf-loaders/2170f75bacaec933c17aeb2ee59d73643a4bab3a/multipkg_test.urdf');
expect(Object.keys(robot.links)).toHaveLength(30);
expect(Object.keys(robot.joints)).toHaveLength(29);
});
});
describe('Parsing Mimic Tags', () => {
it('should parse and link the mimicked joints.', () => {
const loader = new URDFLoader();
const res = loader.parse(`
<robot name="TEST">
<link name="LINK1"/>
<joint name="A" type="continuous">
<origin xyz="0 0 0" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<parent link="LINK1"/>
<child link="LINK2"/>
</joint>
<link name="LINK2"/>
<joint name="B" type="continuous">
<origin xyz="0 0 0" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<parent link="LINK2"/>
<child link="LINK3"/>
<mimic joint="A" offset="-5" multiplier="23"/>
</joint>
<link name="LINK3"/>
</robot>
`);
const jointA = res.joints['A'];
const jointB = res.joints['B'];
expect(jointA.mimicJoints).toEqual([jointB]);
expect(jointB.multiplier).toEqual(23);
expect(jointB.offset).toEqual(-5);
});
it('should use defaults for multiplier and offset attributes.', () => {
const loader = new URDFLoader();
const res = loader.parse(`
<robot name="TEST">
<link name="LINK1"/>
<joint name="A" type="continuous">
<origin xyz="0 0 0" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<parent link="LINK1"/>
<child link="LINK2"/>
</joint>
<link name="LINK2"/>
<joint name="B" type="continuous">
<origin xyz="0 0 0" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<parent link="LINK2"/>
<child link="LINK3"/>
<mimic joint="A"/>
</joint>
<link name="LINK3"/>
</robot>
`);
const jointB = res.joints['B'];
expect(jointB.multiplier).toEqual(1);
expect(jointB.offset).toEqual(0);
});
it('should detect infinite loops.', () => {
const loader = new URDFLoader();
expect(() => {
loader.parse(`
<robot name="TEST">
<link name="LINK1"/>
<joint name="A" type="continuous">
<origin xyz="0 0 0" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<parent link="LINK1"/>
<child link="LINK2"/>
<mimic joint="B" offset="-5" multiplier="23"/>
</joint>
<link name="LINK2"/>
<joint name="B" type="continuous">
<origin xyz="0 0 0" rpy="0 0 0"/>
<axis xyz="1 0 0"/>
<parent link="LINK2"/>
<child link="LINK3"/>
<mimic joint="A" offset="-5" multiplier="23"/>
</joint>
<link name="LINK3"/>
</robot>
`);
}).toThrow();
});
});
describe('Parsing', () => {
it('should throw an error if <robot> node is missing', () => {
const loader = new URDFLoader();
expect(() => {
loader.parse('<not-a-robot></not-a-robot>');
}).toThrow('URDFLoader: No <robot> node found in URDF.');
});
});