|
4 | 4 | * @author Russell Toris - [email protected]
|
5 | 5 | */
|
6 | 6 |
|
| 7 | +import { DOMParser, MIME_TYPE } from '@xmldom/xmldom'; |
7 | 8 | import UrdfMaterial from './UrdfMaterial.js';
|
8 | 9 | import UrdfLink from './UrdfLink.js';
|
9 | 10 | import UrdfJoint from './UrdfJoint.js';
|
10 |
| -import { DOMParser, MIME_TYPE } from '@xmldom/xmldom'; |
| 11 | +import { isElement } from './UrdfUtils.js'; |
11 | 12 |
|
12 | 13 | // See https://developer.mozilla.org/docs/XPathResult#Constants
|
13 |
| -var XPATH_FIRST_ORDERED_NODE_TYPE = 9; |
| 14 | +// const XPATH_FIRST_ORDERED_NODE_TYPE = 9; |
| 15 | + |
| 16 | +export interface UrdfModelOptions { |
| 17 | + /** |
| 18 | + * The XML element to parse. |
| 19 | + */ |
| 20 | + xml?: Element; |
| 21 | + /** |
| 22 | + * The XML element to parse as a string. |
| 23 | + */ |
| 24 | + string: string; |
| 25 | +} |
14 | 26 |
|
15 | 27 | /**
|
16 | 28 | * A URDF Model can be used to parse a given URDF into the appropriate elements.
|
17 | 29 | */
|
18 | 30 | export default class UrdfModel {
|
19 |
| - materials = {}; |
20 |
| - links = {}; |
21 |
| - joints = {}; |
22 |
| - /** |
23 |
| - * @param {Object} options |
24 |
| - * @param {Element | null} [options.xml] - The XML element to parse. |
25 |
| - * @param {string} [options.string] - The XML element to parse as a string. |
26 |
| - */ |
27 |
| - constructor(options) { |
28 |
| - var xmlDoc = options.xml; |
29 |
| - var string = options.string; |
| 31 | + |
| 32 | + name: string | null; |
| 33 | + materials: Record<string, UrdfMaterial> = {}; |
| 34 | + links: Record<string, UrdfLink> = {}; |
| 35 | + joints: Record<string, UrdfJoint> = {}; |
| 36 | + |
| 37 | + constructor({ xml, string }: UrdfModelOptions) { |
| 38 | + let xmlDoc = xml; |
30 | 39 |
|
31 | 40 | // Check if we are using a string or an XML element
|
32 | 41 | if (string) {
|
33 | 42 | // Parse the string
|
34 |
| - var parser = new DOMParser(); |
35 |
| - xmlDoc = parser.parseFromString(string, MIME_TYPE.XML_TEXT).documentElement; |
| 43 | + xmlDoc = new DOMParser().parseFromString(string, MIME_TYPE.XML_TEXT).documentElement; |
36 | 44 | }
|
| 45 | + |
37 | 46 | if (!xmlDoc) {
|
38 | 47 | throw new Error('No URDF document parsed!');
|
39 | 48 | }
|
40 | 49 |
|
41 |
| - // Initialize the model with the given XML node. |
42 |
| - // Get the robot tag |
43 |
| - var robotXml = xmlDoc; |
44 |
| - |
45 | 50 | // Get the robot name
|
46 |
| - this.name = robotXml.getAttribute('name'); |
| 51 | + this.name = xmlDoc.getAttribute('name'); |
47 | 52 |
|
| 53 | + const childNodes = xmlDoc.childNodes; |
48 | 54 | // Parse all the visual elements we need
|
49 |
| - for (var nodes = robotXml.childNodes, i = 0; i < nodes.length; i++) { |
50 |
| - /** @type {Element} */ |
51 |
| - // @ts-expect-error -- unknown why this doesn't work properly. |
52 |
| - var node = nodes[i]; |
53 |
| - if (node.tagName === 'material') { |
54 |
| - var material = new UrdfMaterial({ |
55 |
| - xml: node |
56 |
| - }); |
57 |
| - // Make sure this is unique |
58 |
| - if (this.materials[material.name] !== void 0) { |
| 55 | + for (const node of childNodes) { |
| 56 | + |
| 57 | + // Safety check to make sure we're working with an element. |
| 58 | + if (!isElement(node)) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + switch (node.tagName) { |
| 63 | + case 'material': { |
| 64 | + const material = new UrdfMaterial({ xml: node }); |
| 65 | + // Make sure this is unique |
| 66 | + if (!Object.hasOwn(this.materials, material.name)) { |
| 67 | + this.materials[material.name] = material; |
| 68 | + break; |
| 69 | + } |
| 70 | + |
59 | 71 | if (this.materials[material.name].isLink()) {
|
60 | 72 | this.materials[material.name].assign(material);
|
61 | 73 | } else {
|
62 |
| - console.warn('Material ' + material.name + 'is not unique.'); |
| 74 | + console.warn(`Material ${material.name} is not unique.`); |
63 | 75 | }
|
64 |
| - } else { |
65 |
| - this.materials[material.name] = material; |
| 76 | + |
| 77 | + break; |
66 | 78 | }
|
67 |
| - } else if (node.tagName === 'link') { |
68 |
| - var link = new UrdfLink({ |
69 |
| - xml: node |
70 |
| - }); |
71 |
| - // Make sure this is unique |
72 |
| - if (this.links[link.name] !== void 0) { |
73 |
| - console.warn('Link ' + link.name + ' is not unique.'); |
74 |
| - } else { |
| 79 | + case 'link': { |
| 80 | + const link = new UrdfLink({ xml: node }); |
| 81 | + // Make sure this is unique |
| 82 | + if (Object.hasOwn(this.links, link.name)) { |
| 83 | + console.warn(`Link ${link.name} is not unique.`); |
| 84 | + break; |
| 85 | + } |
| 86 | + |
75 | 87 | // Check for a material
|
76 |
| - for (var j = 0; j < link.visuals.length; j++) { |
77 |
| - var mat = link.visuals[j].material; |
78 |
| - if (mat !== null && mat.name) { |
79 |
| - if (this.materials[mat.name] !== void 0) { |
80 |
| - link.visuals[j].material = this.materials[mat.name]; |
81 |
| - } else { |
82 |
| - this.materials[mat.name] = mat; |
83 |
| - } |
| 88 | + for (const item of link.visuals) { |
| 89 | + const mat = item.material; |
| 90 | + if (!mat?.name) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + if (Object.hasOwn(this.materials, mat.name)) { |
| 95 | + item.material = this.materials[mat.name]; |
| 96 | + } else { |
| 97 | + this.materials[mat.name] = mat; |
84 | 98 | }
|
85 | 99 | }
|
86 | 100 |
|
87 | 101 | // Add the link
|
88 | 102 | this.links[link.name] = link;
|
| 103 | + |
| 104 | + break; |
| 105 | + } |
| 106 | + case 'joint': { |
| 107 | + const joint = new UrdfJoint({ xml: node }); |
| 108 | + this.joints[joint.name] = joint; |
| 109 | + break; |
89 | 110 | }
|
90 |
| - } else if (node.tagName === 'joint') { |
91 |
| - var joint = new UrdfJoint({ |
92 |
| - xml: node |
93 |
| - }); |
94 |
| - this.joints[joint.name] = joint; |
95 | 111 | }
|
96 | 112 | }
|
97 | 113 | }
|
|
0 commit comments