Skip to content

Commit e7212d8

Browse files
havesscopybara-github
authored andcommitted
Add maxhullvert attribute to mjcPhysics MeshCollisionAPI.
PiperOrigin-RevId: 781482346 Change-Id: I0d53f9ad99ac842629243b0375907b2f91bd3b61
1 parent 243c011 commit e7212d8

File tree

9 files changed

+69
-1
lines changed

9 files changed

+69
-1
lines changed

include/mujoco/experimental/usd/mjcPhysics/meshCollisionAPI.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class SdfAssetPath;
4040

4141
/// \class MjcPhysicsMeshCollisionAPI
4242
///
43-
/// API describing a Mujoco mesh collider.
43+
/// API describing a MuJoCo mesh collider.
4444
///
4545
/// For any described attribute \em Fallback \em Value or \em Allowed \em Values
4646
/// below that are text/tokens, the actual token is published and defined in
@@ -173,6 +173,31 @@ class MjcPhysicsMeshCollisionAPI : public UsdAPISchemaBase {
173173
UsdAttribute CreateInertiaAttr(VtValue const &defaultValue = VtValue(),
174174
bool writeSparsely = false) const;
175175

176+
public:
177+
// --------------------------------------------------------------------- //
178+
// MAXHULLVERT
179+
// --------------------------------------------------------------------- //
180+
/// Sets an upper limit on the number of vertices in the meshes convex hull.
181+
/// The default value of -1 means unlimited.
182+
///
183+
/// | ||
184+
/// | -- | -- |
185+
/// | Declaration | `uniform int mjc:maxhullvert = -1` |
186+
/// | C++ Type | int |
187+
/// | \ref Usd_Datatypes "Usd Type" | SdfValueTypeNames->Int |
188+
/// | \ref SdfVariability "Variability" | SdfVariabilityUniform |
189+
MJCPHYSICS_API
190+
UsdAttribute GetMaxHullVertAttr() const;
191+
192+
/// See GetMaxHullVertAttr(), and also
193+
/// \ref Usd_Create_Or_Get_Property for when to use Get vs Create.
194+
/// If specified, author \p defaultValue as the attribute's default,
195+
/// sparsely (when it makes sense to do so) if \p writeSparsely is \c true -
196+
/// the default for \p writeSparsely is \c false.
197+
MJCPHYSICS_API
198+
UsdAttribute CreateMaxHullVertAttr(VtValue const &defaultValue = VtValue(),
199+
bool writeSparsely = false) const;
200+
176201
public:
177202
// ===================================================================== //
178203
// Feel free to add custom code below this line, it will be preserved by

include/mujoco/experimental/usd/mjcPhysics/tokens.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ struct MjcPhysicsTokensType {
356356
///
357357
/// MjcPhysicsJointAPI
358358
const TfToken mjcMargin;
359+
/// \brief "mjc:maxhullvert"
360+
///
361+
/// MjcPhysicsMeshCollisionAPI
362+
const TfToken mjcMaxhullvert;
359363
/// \brief "mjc:mpos"
360364
///
361365
/// MjcPhysicsKeyframe

src/experimental/usd/mjcPhysics/generatedSchema.usda

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ class "MjcMeshCollisionAPI" (
245245
displayName = "Inertia"
246246
doc = "Controls how a mesh is used when mass and inertia are inferred from geometry."
247247
)
248+
uniform int mjc:maxhullvert = -1 (
249+
displayName = "Maximum Hull Vertices"
250+
doc = "Sets an upper limit on the number of vertices in the meshes convex hull. The default value of -1 means unlimited."
251+
)
248252
}
249253

250254
class "MjcActuatorAPI" (

src/experimental/usd/mjcPhysics/meshCollisionAPI.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ UsdAttribute MjcPhysicsMeshCollisionAPI::CreateInertiaAttr(
8888
/* custom = */ false, SdfVariabilityUniform, defaultValue, writeSparsely);
8989
}
9090

91+
UsdAttribute MjcPhysicsMeshCollisionAPI::GetMaxHullVertAttr() const {
92+
return GetPrim().GetAttribute(MjcPhysicsTokens->mjcMaxhullvert);
93+
}
94+
95+
UsdAttribute MjcPhysicsMeshCollisionAPI::CreateMaxHullVertAttr(
96+
VtValue const &defaultValue, bool writeSparsely) const {
97+
return UsdSchemaBase::_CreateAttr(
98+
MjcPhysicsTokens->mjcMaxhullvert, SdfValueTypeNames->Int,
99+
/* custom = */ false, SdfVariabilityUniform, defaultValue, writeSparsely);
100+
}
101+
91102
namespace {
92103
static inline TfTokenVector _ConcatenateAttributeNames(
93104
const TfTokenVector &left, const TfTokenVector &right) {
@@ -104,6 +115,7 @@ const TfTokenVector &MjcPhysicsMeshCollisionAPI::GetSchemaAttributeNames(
104115
bool includeInherited) {
105116
static TfTokenVector localNames = {
106117
MjcPhysicsTokens->mjcInertia,
118+
MjcPhysicsTokens->mjcMaxhullvert,
107119
};
108120
static TfTokenVector allNames = _ConcatenateAttributeNames(
109121
UsdAPISchemaBase::GetSchemaAttributeNames(true), localNames);

src/experimental/usd/mjcPhysics/schema.usda

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,14 @@ class "MjcMeshCollisionAPI"
559559
displayName = "Inertia"
560560
doc = """Controls how a mesh is used when mass and inertia are inferred from geometry."""
561561
)
562+
563+
uniform int mjc:maxhullvert = -1 (
564+
customData = {
565+
string apiName = "MaxHullVert"
566+
}
567+
displayName = "Maximum Hull Vertices"
568+
doc = """Sets an upper limit on the number of vertices in the meshes convex hull. The default value of -1 means unlimited."""
569+
)
562570
}
563571

564572
class "MjcActuatorAPI"

src/experimental/usd/mjcPhysics/tokens.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ MjcPhysicsTokensType::MjcPhysicsTokensType()
8989
mjcLengthRangeMax("mjc:lengthRange:max", TfToken::Immortal),
9090
mjcLengthRangeMin("mjc:lengthRange:min", TfToken::Immortal),
9191
mjcMargin("mjc:margin", TfToken::Immortal),
92+
mjcMaxhullvert("mjc:maxhullvert", TfToken::Immortal),
9293
mjcMpos("mjc:mpos", TfToken::Immortal),
9394
mjcMquat("mjc:mquat", TfToken::Immortal),
9495
mjcOptionActuatorgroupdisable("mjc:option:actuatorgroupdisable",
@@ -222,6 +223,7 @@ MjcPhysicsTokensType::MjcPhysicsTokensType()
222223
mjcLengthRangeMax,
223224
mjcLengthRangeMin,
224225
mjcMargin,
226+
mjcMaxhullvert,
225227
mjcMpos,
226228
mjcMquat,
227229
mjcOptionActuatorgroupdisable,

src/experimental/usd/plugins/mjcf/mujoco_to_usd.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ class ModelWriter {
360360

361361
WriteUniformAttribute(mesh_path, pxr::SdfValueTypeNames->Token,
362362
MjcPhysicsTokens->mjcInertia, inertia);
363+
364+
WriteUniformAttribute(mesh_path, pxr::SdfValueTypeNames->Int,
365+
MjcPhysicsTokens->mjcMaxhullvert,
366+
mesh->maxhullvert);
363367
}
364368

365369
// NOTE: The geometry data taken from the spec is the post-compilation

src/experimental/usd/usd_to_mjspec.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,11 @@ void ParseMjcPhysicsMeshCollisionAPI(
546546
mesh->inertia = mjtMeshInertia::mjMESH_INERTIA_LEGACY;
547547
}
548548
}
549+
550+
auto maxhullvert_attr = mesh_collision_api.GetMaxHullVertAttr();
551+
if (maxhullvert_attr.HasAuthoredValue()) {
552+
maxhullvert_attr.Get(&mesh->maxhullvert);
553+
}
549554
}
550555

551556
void ParseMjcPhysicsGeneralActuatorAPI(mjSpec* spec,

test/experimental/usd/plugins/mjcf/mjcf_file_format_test.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,13 +1440,15 @@ TEST_F(MjcfSdfFileFormatPluginTest, TestMjcPhysicsMeshCollisionAPI) {
14401440
<mesh name="tet_exact" inertia="exact" vertex="0 0 0 1 0 0 0 1 0 0 0 1"/>
14411441
<mesh name="tet_convex" inertia="convex" vertex="0 0 0 1 0 0 0 1 0 0 0 1"/>
14421442
<mesh name="tet_shell" inertia="shell" vertex="0 0 0 1 0 0 0 1 0 0 0 1"/>
1443+
<mesh name="tet_max_vert" inertia="shell" maxhullvert="12" vertex="0 0 0 1 0 0 0 1 0 0 0 1"/>
14431444
</asset>
14441445
<worldbody>
14451446
<body name="body">
14461447
<geom name="tet_legacy" type="mesh" mesh="tet_legacy"/>
14471448
<geom name="tet_exact" type="mesh" mesh="tet_exact"/>
14481449
<geom name="tet_convex" type="mesh" mesh="tet_convex"/>
14491450
<geom name="tet_shell" type="mesh" mesh="tet_shell"/>
1451+
<geom name="tet_max_vert" type="mesh" mesh="tet_max_vert"/>
14501452
</body>
14511453
</worldbody>
14521454
</mujoco>
@@ -1461,6 +1463,8 @@ TEST_F(MjcfSdfFileFormatPluginTest, TestMjcPhysicsMeshCollisionAPI) {
14611463
MjcPhysicsTokens->convex);
14621464
ExpectAttributeEqual(stage, "/test/body/tet_shell/Mesh.mjc:inertia",
14631465
MjcPhysicsTokens->shell);
1466+
ExpectAttributeEqual(stage, "/test/body/tet_max_vert/Mesh.mjc:maxhullvert",
1467+
12);
14641468
}
14651469

14661470
TEST_F(MjcfSdfFileFormatPluginTest, TestMassAPIApplied) {

0 commit comments

Comments
 (0)