| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | using System; |
| | using System.Linq; |
| | using System.Xml; |
| | using System.Globalization; |
| | using UnityEngine; |
| |
|
| | namespace Mujoco { |
| | |
| | |
| | public static class XmlElementExtensions { |
| |
|
| | public static bool GetBoolAttribute( |
| | this XmlElement element, string name, bool defaultValue = false) { |
| | if (!element.HasAttribute(name)) { |
| | return defaultValue; |
| | } |
| | var strValue = element.GetAttribute(name); |
| | bool parsedValue; |
| | if (bool.TryParse(strValue, out parsedValue)) { |
| | return parsedValue; |
| | } else { |
| | throw new ArgumentException($"'{strValue}' is not a bool."); |
| | } |
| | } |
| |
|
| | public static bool GetLimitedAttribute( |
| | this XmlElement element, string name, bool rangeDefined) { |
| | var strValue = element.GetStringAttribute(name, "auto"); |
| | if (strValue == "auto" && rangeDefined && element.GetAutolimitsEnabled()) return true; |
| | if (strValue == "auto") return false; |
| |
|
| | bool parsedValue; |
| | if (bool.TryParse(strValue, out parsedValue)) { |
| | return parsedValue; |
| | } else { |
| | throw new ArgumentException($"'{strValue}' is not a bool."); |
| | } |
| | } |
| |
|
| | public static bool GetAutolimitsEnabled( |
| | this XmlElement element) { |
| | bool autolimits = (element.OwnerDocument?.GetElementsByTagName("compiler")[0]?["compiler"]) |
| | ?.GetBoolAttribute("autolimits", true) ?? |
| | true; |
| | return autolimits; |
| | } |
| |
|
| | public static float GetFloatAttribute( |
| | this XmlElement element, string name, float defaultValue = 0.0f) { |
| | if (!element.HasAttribute(name)) { |
| | return defaultValue; |
| | } |
| | var strValue = element.GetAttribute(name); |
| | float parsedValue; |
| | if (float.TryParse(strValue, NumberStyles.Any, CultureInfo.InvariantCulture, out parsedValue)) { |
| | return parsedValue; |
| | } else { |
| | throw new ArgumentException($"'{strValue}' is not a float."); |
| | } |
| | } |
| |
|
| | |
| | public static T GetEnumAttribute<T>( |
| | this XmlElement element, string name, T defaultValue, |
| | bool ignoreCase = false) where T : struct, IConvertible { |
| | if (!typeof(T).IsEnum) { |
| | throw new ArgumentException("T must be an enumerated type."); |
| | } |
| | if (!element.HasAttribute(name)) { |
| | return defaultValue; |
| | } |
| | var strValue = element.GetAttribute(name); |
| | T parsedValue; |
| | if (Enum.TryParse<T>(strValue, ignoreCase, out parsedValue)) { |
| | return parsedValue; |
| | } else { |
| | throw new ArgumentException( |
| | $"Reading attribute {name}: '{strValue}' is not a value of enum {typeof(T)}."); |
| | } |
| | } |
| |
|
| | public static string GetStringAttribute( |
| | this XmlElement element, string name, string defaultValue = null) { |
| | if (!element.HasAttribute(name)) { |
| | return defaultValue; |
| | } |
| | return element.GetAttribute(name); |
| | } |
| |
|
| | public static T GetObjectReferenceAttribute<T>( |
| | this XmlElement mjcf, string attributeName) where T : MjComponent { |
| | var objectName = mjcf.GetStringAttribute(attributeName); |
| | if (string.IsNullOrEmpty(objectName)) { |
| | return null; |
| | } |
| | T foundObject = MjHierarchyTool.FindComponentOfTypeAndName<T>(objectName); |
| | if (foundObject == null) { |
| | throw new ArgumentException( |
| | $"Object {objectName} of type {typeof(T).ToString()} referred by {attributeName} " + |
| | "doesn't exist."); |
| | } |
| | return foundObject; |
| | } |
| |
|
| | public static Vector2 GetVector2Attribute( |
| | this XmlElement element, string name, Vector2 defaultValue) { |
| | var defaultValues = new float[] {defaultValue.x, defaultValue.y}; |
| | var components = element.GetFloatArrayAttribute(name, defaultValues, fillMissingValues: false); |
| | if (components.Length != 2) { |
| | throw new ArgumentException("Invalid Vector2 string representation."); |
| | } |
| | return new Vector2(components[0], components[1]); |
| | } |
| |
|
| | public static Vector3 GetVector3Attribute( |
| | this XmlElement element, string name, Vector3 defaultValue) { |
| | var defaultValues = new float[] { |
| | defaultValue.x, defaultValue.y, defaultValue.z}; |
| | var components = element.GetFloatArrayAttribute(name, defaultValues, fillMissingValues: false); |
| | if (components.Length != 3) { |
| | throw new ArgumentException("Invalid Vector3 string representation."); |
| | } |
| | return new Vector3(components[0], components[1], components[2]); |
| | } |
| |
|
| | public static Quaternion GetQuaternionAttribute( |
| | this XmlElement element, string name, Quaternion defaultValue) { |
| | var mjDefault = new float[] { |
| | defaultValue.w, defaultValue.x, defaultValue.y, defaultValue.z }; |
| | var components = element.GetFloatArrayAttribute(name, mjDefault, fillMissingValues: false); |
| | if (components.Length != 4) { |
| | throw new ArgumentException("Invalid Quaternion string representation."); |
| | } |
| | return new Quaternion(w:components[0], x:components[1], y:components[2], z:components[3]); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | public static float[] GetFloatArrayAttribute( |
| | this XmlElement element, string name, float[] defaultValue, bool fillMissingValues = true) { |
| | if (!element.HasAttribute(name)) { |
| | return defaultValue; |
| | } |
| | var strValue = element.GetAttribute(name); |
| | var components = strValue.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries); |
| | var resultLength = components.Length; |
| | if (fillMissingValues && defaultValue != null) { |
| | |
| | |
| | |
| | resultLength = Math.Max(resultLength, defaultValue.Length); |
| | } |
| | var result = new float[resultLength]; |
| | for (var i = 0; i < components.Length; ++i) { |
| | float componentValue; |
| | if (float.TryParse(components[i], NumberStyles.Any, CultureInfo.InvariantCulture, out componentValue)) { |
| | result[i] = componentValue; |
| | } else { |
| | throw new ArgumentException($"'{components[i]}' is not a float."); |
| | } |
| | } |
| | for (var i = components.Length; i < resultLength; ++i) { |
| | |
| | result[i] = defaultValue[i]; |
| | } |
| | return result; |
| | } |
| | } |
| | } |
| |
|