Skip to content

Commit 1994fa6

Browse files
committed
added primitive scaling
1 parent 2fa3d62 commit 1994fa6

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Runtime/Scripts/Util/Extensions/ZOROSConversionExtensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using UnityEngine;
2+
using System.Collections.Generic;
23

34
namespace ZO.Util.Extensions {
45
public static class ZOROSConversionExtensions {
@@ -36,6 +37,29 @@ public static string ToXMLString(this Vector3 v) {
3637
return $"{v.x} {v.y} {v.z}";
3738
}
3839

40+
/// <summary>
41+
/// Converts URDF Xml string vector "1 2 3" to Vector3
42+
/// </summary>
43+
/// <param name="s">URDF XML string in format "1.0 2.0 3.0" 3 numbers separated by spaces</param>
44+
/// <returns></returns>
45+
public static Vector3 FromURDFStringToVector3(this string s) {
46+
string[] splits = s.Split(' ');
47+
List<float> numbers = new List<float>();
48+
foreach (string snum in splits) {
49+
if (float.TryParse(snum, out float v)) {
50+
numbers.Add(v);
51+
}
52+
}
53+
54+
if (numbers.Count == 3) {
55+
return new Vector3(numbers[0], numbers[1], numbers[2]);
56+
}
57+
58+
Debug.LogWarning($"Could not parse string: {s}");
59+
60+
return Vector3.zero;
61+
}
62+
3963

4064
}
4165

Runtime/Scripts/Util/ImportExport/ZOImportURDF.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,28 +61,47 @@ public static ZOSimDocumentRoot Import(XmlDocument xmlDocument) {
6161
XmlNode[] xmlVisuals = xmlLink.GetChildrenByName("visual");
6262

6363
foreach (XmlNode xmlVisual in xmlVisuals) {
64+
65+
6466
XmlNode[] xmlGeometries = xmlVisual.GetChildrenByName("geometry");
6567

6668
foreach (XmlNode xmlGeom in xmlGeometries) {
67-
XmlNode xmlBox = xmlGeom.GetChildByName("box");
69+
6870
GameObject visualGeo = null;
71+
72+
XmlNode xmlBox = xmlGeom.GetChildByName("box");
6973
if (xmlBox != null) {
7074
visualGeo = GameObject.CreatePrimitive(PrimitiveType.Cube);
75+
Vector3 size = xmlBox.Attributes["size"].Value.FromURDFStringToVector3();
76+
77+
visualGeo.transform.localScale = size.Ros2UnityScale();
7178
}
79+
7280
XmlNode xmlCylinder = xmlGeom.GetChildByName("cylinder");
7381
if (xmlCylinder != null) {
7482
visualGeo = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
83+
float radius = float.Parse(xmlCylinder.Attributes["radius"].Value);
84+
float length = float.Parse(xmlCylinder.Attributes["length"].Value);
85+
visualGeo.transform.localScale = new Vector3(radius * 2.0f, length * 0.5f, radius * 2.0f);
7586
}
87+
7688
XmlNode xmlSphere = xmlGeom.GetChildByName("sphere");
7789
if (xmlSphere != null) {
7890
visualGeo = GameObject.CreatePrimitive(PrimitiveType.Sphere);
91+
float radius = float.Parse(xmlSphere.Attributes["radius"].Value);
92+
visualGeo.transform.localScale = new Vector3(radius * 2.0f, radius * 2.0f, radius * 2.0f);
7993
}
94+
8095
XmlNode xmlMesh = xmlGeom.GetChildByName("mesh");
8196
if (xmlMesh != null) {
8297
// TODO
8398
}
8499
if (visualGeo != null) {
85100
visualGeo.transform.SetParent(goVisualsEmpty.transform);
101+
string visualName = xmlVisual.Attributes["name"].Value;
102+
if (visualName != null) {
103+
visualGeo.name = visualName;
104+
}
86105
}
87106

88107
}

0 commit comments

Comments
 (0)