/**
* @author Benjamin Pitzer - ben.pitzer@gmail.com
* @author Russell Toris - rctoris@wpi.edu
*/
var UrdfMaterial = require('./UrdfMaterial');
var UrdfLink = require('./UrdfLink');
var DOMParser = require('../util/DOMParser');
// See https://developer.mozilla.org/docs/XPathResult#Constants
var XPATH_FIRST_ORDERED_NODE_TYPE = 9;
/**
* A URDF Model can be used to parse a given URDF into the appropriate elements.
*
* @constructor
* @param options - object with following keys:
* * xml - the XML element to parse
* * string - the XML element to parse as a string
*/
function UrdfModel(options) {
options = options || {};
var xmlDoc = options.xml;
var string = options.string;
this.materials = {};
this.links = {};
// Check if we are using a string or an XML element
if (string) {
// Parse the string
var parser = new DOMParser();
xmlDoc = parser.parseFromString(string, 'text/xml');
}
// Initialize the model with the given XML node.
// Get the robot tag
var robotXml = xmlDoc.evaluate('//robot', xmlDoc, null, XPATH_FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
// Get the robot name
this.name = robotXml.getAttribute('name');
// Parse all the visual elements we need
for (var nodes = robotXml.childNodes, i = 0; i < nodes.length; i++) {
var node = nodes[i];
if (node.tagName === 'material') {
var material = new UrdfMaterial({
xml : node
});
// Make sure this is unique
if (this.materials[material.name] !== void 0) {
console.warn('Material ' + material.name + 'is not unique.');
} else {
this.materials[material.name] = material;
}
} else if (node.tagName === 'link') {
var link = new UrdfLink({
xml : node
});
// Make sure this is unique
if (this.links[link.name] !== void 0) {
console.warn('Link ' + link.name + ' is not unique.');
} else {
// Check for a material
if (link.visual && link.visual.material) {
if (this.materials[link.visual.material.name] !== void 0) {
link.visual.material = this.materials[link.visual.material.name];
} else {
this.materials[link.visual.material.name] = link.visual.material;
}
}
// Add the link
this.links[link.name] = link;
}
}
}
}
module.exports = UrdfModel;