Skip to content

Commit eac2e4b

Browse files
committed
using lossyscale. implicit fixed joints.
1 parent 1a4d31b commit eac2e4b

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

Runtime/Scripts/Util/ImportExport/ZOExportURDF.cs

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,18 @@ protected void BuildURDFJoints(XElement robot, ZOSimOccurrence parent, ZOSimOccu
176176

177177
ZOJointInterface[] zoJoints = parent.GetComponents<ZOJointInterface>();
178178

179+
// find if child is explicitly linked to a joint of the parent
180+
bool isChildExplicitlyInAParentJoint = false;
181+
foreach (ZOJointInterface joint in zoJoints) {
182+
if (joint.ConnectedOccurrence == child) {
183+
isChildExplicitlyInAParentJoint = true;
184+
break;
185+
}
186+
}
187+
188+
179189
// even if there are no explicit fixed joint children of the parent are "automatically" set as fixed.
180-
if (zoJoints.Length == 0) {
190+
if (zoJoints.Length == 0 || isChildExplicitlyInAParentJoint == false) {
181191
XElement jointX = new XElement("joint");
182192
jointX.SetAttributeValue("name", $"Fixed_{parent.Name}_to_{child.Name}");
183193
// Transform jointTransform = this.transform;
@@ -384,25 +394,25 @@ protected void BuildLink(ZOSimOccurrence simOccurrence, XElement robot, Vector3
384394
// go through the children of the visuals and get all the models
385395
foreach (Transform visualsChild in child) {
386396
// check if it is a primitive type (cube, sphere, cylinder, etc)
387-
BuildLinkVisuals(visualsChild, link, anchorOffset);
397+
BuildLinkVisuals(simOccurrence, visualsChild, link, anchorOffset);
388398

389399
// we will do any collider that are attached to the visual
390-
BuildLinkCollisions(visualsChild, link, anchorOffset);
400+
BuildLinkCollisions(simOccurrence, visualsChild, link, anchorOffset);
391401
}
392402
}
393403

394404
// build collisions
395405
if (child.name.ToLower() == "collisions") {
396406
// go through the children of the collisions and get all the models
397407
foreach (Transform collisionChild in child) {
398-
BuildLinkCollisions(collisionChild, link, anchorOffset);
408+
BuildLinkCollisions(simOccurrence, collisionChild, link, anchorOffset);
399409
}
400410
}
401411
}
402412
}
403413

404414

405-
protected void BuildLinkCollisions(Transform collisionTransform, XElement link, Vector3 anchorOffset) {
415+
protected void BuildLinkCollisions(ZOSimOccurrence simOccurrence, Transform collisionTransform, XElement link, Vector3 anchorOffset) {
406416
Collider[] colliders = collisionTransform.GetComponentsInChildren<Collider>();
407417
foreach (Collider collider in colliders) {
408418
XElement collision = new XElement("collision");
@@ -487,36 +497,34 @@ protected void BuildLinkCollisions(Transform collisionTransform, XElement link,
487497
}
488498
}
489499

490-
protected void BuildLinkVisuals(Transform visualTransform, XElement link, Vector3 anchorOffset) {
500+
protected void BuildLinkVisuals(ZOSimOccurrence simOccurrence, Transform visualTransform, XElement link, Vector3 anchorOffset) {
491501
// build 3d primitive if exists
492502
// Check children
493503
MeshFilter[] meshFilters = visualTransform.GetComponentsInChildren<MeshFilter>();
494504
foreach (MeshFilter meshFilter in meshFilters) {
495505
if (meshFilter) {
496506

497-
MeshRenderer meshRenderer = visualTransform.GetComponent<MeshRenderer>();
498-
Collider collider = null;
499507
XElement visual = new XElement("visual");
500-
visual.SetAttributeValue("name", visualTransform.name);
508+
visual.SetAttributeValue("name", meshFilter.name);
501509
XElement geometry = new XElement("geometry");
502510

503511
if (meshFilter.sharedMesh.name.Contains("Cube")) {
504512
XElement box = new XElement("box");
505513

506-
Vector3 boxSize = visualTransform.lossyScale.Unity2RosScale();
514+
Vector3 boxSize = meshFilter.transform.lossyScale.Unity2RosScale();
507515
box.SetAttributeValue("size", boxSize.ToXMLString());
508516
geometry.Add(box);
509517

510518
} else if (meshFilter.sharedMesh.name.Contains("Sphere")) {
511519
XElement sphere = new XElement("sphere");
512-
float radius = visualTransform.lossyScale.x / 2.0f;
520+
float radius = meshFilter.transform.lossyScale.x / 2.0f;
513521
sphere.SetAttributeValue("radius", radius);
514522
geometry.Add(sphere);
515523

516524
} else if (meshFilter.sharedMesh.name.Contains("Cylinder")) {
517525
XElement cylinder = new XElement("cylinder");
518-
float radius = visualTransform.lossyScale.x / 2.0f;
519-
float height = visualTransform.lossyScale.y * 2.0f;
526+
float radius = meshFilter.transform.lossyScale.x / 2.0f;
527+
float height = meshFilter.transform.lossyScale.y * 2.0f;
520528
cylinder.SetAttributeValue("radius", radius);
521529
cylinder.SetAttributeValue("length", height);
522530

@@ -525,21 +533,44 @@ protected void BuildLinkVisuals(Transform visualTransform, XElement link, Vector
525533

526534
} else { // regular mesh so export meshes as OBJ
527535
XElement mesh = new XElement("mesh");
528-
mesh.SetAttributeValue("filename", $"{visualTransform.name}.obj");
529-
Vector3 scale = visualTransform.lossyScale.Unity2RosScale();
536+
mesh.SetAttributeValue("filename", $"{meshFilter.name}.obj");
537+
Vector3 scale = meshFilter.transform.lossyScale.Unity2RosScale();
530538
mesh.SetAttributeValue("scale", scale.ToXMLString());
531539
geometry.Add(mesh);
532540

533-
_visualMeshesToExport.Add(visualTransform);
541+
_visualMeshesToExport.Add(meshFilter.transform);
534542
}
535543

536544
if (geometry.HasElements) {
545+
//Matrix4x4 simOccurrenceWorldMatrix = Matrix4x4.TRS(simOccurrence.transform.parent.position, simOccurrence.transform.parent.rotation, simOccurrence.transform.parent.lossyScale);// simOccurrence.transform.parent.WorldTranslationRotationMatrix();
546+
// Matrix4x4 simOccurrenceWorldMatrix = simOccurrence.transform.parent.WorldTranslationRotationMatrix();
547+
// Matrix4x4 visualWorldMatrix = visualTransform.WorldTranslationRotationMatrix();
548+
// Matrix4x4 visualWorldMatrix = Matrix4x4.TRS(visualTransform.position, visualTransform.rotation, visualTransform.lossyScale);// visualTransform.WorldTranslationRotationMatrix();
549+
// Matrix4x4 visualRelativeToSimOccurrence = simOccurrenceWorldMatrix.inverse * visualWorldMatrix;
550+
// Matrix4x4 visualRelativeToSimOccurrence = simOccurrence.transform.LocalTRSMatrix() * visualTransform.LocalTRSMatrix();
551+
552+
// Vector3 position = visualRelativeToSimOccurrence.Position() + anchorOffset;
553+
554+
// Vector3 position = new Vector3(visualRelativeToSimOccurrence.Position().x * simOccurrence.transform.parent.lossyScale.x,
555+
// visualRelativeToSimOccurrence.Position().y * simOccurrence.transform.parent.lossyScale.y,
556+
// visualRelativeToSimOccurrence.Position().z * simOccurrence.transform.parent.lossyScale.z) + anchorOffset;
557+
// Vector3 xyz = position.Unity2Ros();
558+
// Quaternion rotation = visualRelativeToSimOccurrence.rotation;
559+
// Vector3 rpy = new Vector3(-rotation.eulerAngles.z * Mathf.Deg2Rad,
560+
// rotation.eulerAngles.x * Mathf.Deg2Rad,
561+
// -rotation.eulerAngles.y * Mathf.Deg2Rad);
562+
563+
Quaternion rotation = visualTransform.localRotation;
564+
537565
// build origin
538566
Vector3 position = visualTransform.localPosition + anchorOffset;
539567
Vector3 xyz = position.Unity2Ros();
540-
Vector3 rpy = new Vector3(-visualTransform.localEulerAngles.z * Mathf.Deg2Rad,
541-
visualTransform.localEulerAngles.x * Mathf.Deg2Rad,
542-
-visualTransform.localEulerAngles.y * Mathf.Deg2Rad);
568+
// Vector3 rpy = new Vector3(-visualTransform.localEulerAngles.z * Mathf.Deg2Rad,
569+
// visualTransform.localEulerAngles.x * Mathf.Deg2Rad,
570+
// -visualTransform.localEulerAngles.y * Mathf.Deg2Rad);
571+
Vector3 rpy = new Vector3(-rotation.eulerAngles.z * Mathf.Deg2Rad,
572+
rotation.eulerAngles.x * Mathf.Deg2Rad,
573+
-rotation.eulerAngles.y * Mathf.Deg2Rad);
543574

544575
XElement origin = new XElement("origin");
545576
origin.SetAttributeValue("xyz", xyz.ToXMLString());

0 commit comments

Comments
 (0)