Skip to content

Commit 723960a

Browse files
ChuangTseucopybara-github
authored andcommitted
Fix plane size
Mujoco uses half-sizes so we must divide USD sizes by 2. Also added a test for geom primitive sizes. PiperOrigin-RevId: 781595650 Change-Id: If8a84dd942905c367590eafffac61cc23b1c407d
1 parent 58c9521 commit 723960a

File tree

1 file changed

+34
-43
lines changed

1 file changed

+34
-43
lines changed

src/experimental/usd/usd_to_mjspec.cc

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,6 @@ bool IsUniformScale(const pxr::GfVec3d& scale) {
126126
fabs(scale[1] - scale[2]) < epsilon;
127127
}
128128

129-
template <typename T>
130-
void SetScale(T* element, const pxr::GfMatrix4d& world_transform,
131-
const pxr::GfVec3d& scale) {
132-
pxr::GfVec3d transform_scale =
133-
pxr::GfCompMult(GetScale(world_transform), scale);
134-
element->size[0] = transform_scale[0];
135-
element->size[1] = transform_scale[1];
136-
element->size[2] = transform_scale[2];
137-
}
138-
139129
template <typename T>
140130
bool MaybeParseGeomPrimitive(const pxr::UsdPrim& prim, T* element,
141131
pxr::UsdGeomXformCache& xform_cache) {
@@ -222,9 +212,41 @@ bool MaybeParseGeomPrimitive(const pxr::UsdPrim& prim, T* element,
222212
return false;
223213
}
224214
// MuJoCo uses half-length for box size.
225-
SetScale(element, xform_cache.GetLocalToWorldTransform(prim),
226-
pxr::GfVec3d(size / 2));
215+
size = size / 2;
216+
element->size[0] = scale[0] * size;
217+
element->size[1] = scale[1] * size;
218+
element->size[2] = scale[2] * size;
219+
} else if (prim.IsA<pxr::UsdGeomPlane>()) {
220+
element->type = mjGEOM_PLANE;
221+
222+
pxr::UsdGeomPlane plane(prim);
223+
TfToken axis;
224+
if (!plane.GetAxisAttr().Get(&axis)) {
225+
mju_error("Could not get plane axis attr.");
226+
return false;
227+
}
228+
if (axis != pxr::UsdGeomTokens->z) {
229+
mju_error("Only z-axis planes are supported.");
230+
return false;
231+
}
227232

233+
double length;
234+
if (!plane.GetLengthAttr().Get(&length)) {
235+
mju_error("Could not get plane length attr.");
236+
return false;
237+
}
238+
double width;
239+
if (!plane.GetWidthAttr().Get(&width)) {
240+
mju_error("Could not get plane width attr.");
241+
return false;
242+
}
243+
// MuJoCo uses half-length for plane size.
244+
width = width / 2;
245+
length = length / 2;
246+
// Plane geoms in mjc are always infinite. Scale is used for visualization.
247+
element->size[0] = scale[0] * width;
248+
element->size[1] = scale[1] * length;
249+
element->size[2] = scale[2];
228250
} else {
229251
return false;
230252
}
@@ -1040,37 +1062,6 @@ void ParseUsdPhysicsCollider(mjSpec* spec,
10401062
mjs_setInt(mesh->userface, userface.data(), userface.size());
10411063

10421064
mjs_setString(geom->meshname, mesh_name.c_str());
1043-
} else if (prim.IsA<pxr::UsdGeomPlane>()) {
1044-
geom->type = mjGEOM_PLANE;
1045-
1046-
pxr::UsdGeomPlane plane(prim);
1047-
TfToken axis;
1048-
if (!plane.GetAxisAttr().Get(&axis)) {
1049-
mju_error("Could not get plane axis attr.");
1050-
return;
1051-
}
1052-
if (axis != pxr::UsdGeomTokens->z) {
1053-
mju_error("Only z-axis planes are supported.");
1054-
return;
1055-
}
1056-
1057-
// This block of code distributes the plane length and width along the
1058-
// scale as per the specification here:
1059-
// https://openusd.org/dev/api/class_usd_geom_plane.html#a89fa6076111984682db77fc8a4e57496.
1060-
double length;
1061-
if (!plane.GetLengthAttr().Get(&length)) {
1062-
mju_error("Could not get plane length attr.");
1063-
return;
1064-
}
1065-
double width;
1066-
if (!plane.GetWidthAttr().Get(&width)) {
1067-
mju_error("Could not get plane width attr.");
1068-
return;
1069-
}
1070-
// Plane geoms in mjc are always infinite. We set the scale here just
1071-
// for visualization.
1072-
SetScale(geom, xform_cache.GetLocalToWorldTransform(prim),
1073-
pxr::GfVec3d(width, length, 1));
10741065
}
10751066
}
10761067
}

0 commit comments

Comments
 (0)