Report which body has too-small mass/inertia in compilation error.

PiperOrigin-RevId: 776196017
Change-Id: Ib81efb170347a57cdc4b446c14cd7b8f5357958c
This commit is contained in:
Yuval Tassa
2025-06-26 10:39:31 -07:00
committed by Copybara-Service
parent afc35451a0
commit 21bae3a388
4 changed files with 7 additions and 23 deletions
+3 -18
View File
@@ -4515,10 +4515,9 @@ void mjCModel::TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs) {
} }
// check mass and inertia of moving bodies // check mass and inertia of moving bodies
if (bodies_.size() > 1) { for (int i=1; i < bodies_.size(); i++) {
// we ignore the first body as it is the world body if (!bodies_[i]->joints.empty() && !CheckBodyMassInertia(bodies_[i])) {
if (!CheckBodiesMassInertia(std::vector<mjCBody*>(bodies_.begin()+1, bodies_.end()))) { throw mjCError(bodies_[i], "mass and inertia of moving bodies must be larger than mjMINVAL");
throw mjCError(0, "mass and inertia of moving bodies must be larger than mjMINVAL");
} }
} }
@@ -4763,20 +4762,6 @@ uint64_t mjCModel::Signature() {
bool mjCModel::CheckBodiesMassInertia(std::vector<mjCBody*> bodies) {
// check mass and inertia of moving bodies
for (int i=0; i < bodies.size(); i++) {
if (!bodies[i]->joints.empty()) {
if (!CheckBodyMassInertia(bodies[i])) {
return false;
}
}
}
return true;
}
bool mjCModel::CheckBodyMassInertia(mjCBody* body) { bool mjCModel::CheckBodyMassInertia(mjCBody* body) {
// check if body has valid mass and inertia // check if body has valid mass and inertia
if (body->mass >= mjMINVAL && if (body->mass >= mjMINVAL &&
-3
View File
@@ -425,9 +425,6 @@ class mjCModel : public mjCModel_, private mjSpec {
// compute qpos0 // compute qpos0
void ComputeReference(); void ComputeReference();
// return true if all bodies have valid mass and inertia
bool CheckBodiesMassInertia(std::vector<mjCBody*> bodies);
// return true if body has valid mass and inertia // return true if body has valid mass and inertia
bool CheckBodyMassInertia(mjCBody* body); bool CheckBodyMassInertia(mjCBody* body);
+2 -1
View File
@@ -529,7 +529,7 @@ TEST_F(MjCMeshTest, TinyInertiaFails) {
<mesh name="tiny" vertex="0 0 0 1e-4 0 0 0 1e-4 0 0 0 1e-4"/> <mesh name="tiny" vertex="0 0 0 1e-4 0 0 0 1e-4 0 0 0 1e-4"/>
</asset> </asset>
<worldbody> <worldbody>
<body> <body name="tiny_body">
<freejoint/> <freejoint/>
<geom type="mesh" mesh="tiny"/> <geom type="mesh" mesh="tiny"/>
</body> </body>
@@ -542,6 +542,7 @@ TEST_F(MjCMeshTest, TinyInertiaFails) {
error.data(), error.data(),
HasSubstr( HasSubstr(
"mass and inertia of moving bodies must be larger than mjMINVAL")); "mass and inertia of moving bodies must be larger than mjMINVAL"));
EXPECT_THAT(error.data(), HasSubstr("Element name 'tiny_body'"));
} }
TEST_F(MjCMeshTest, FlippedFaceAllowedLegacyInertia) { TEST_F(MjCMeshTest, FlippedFaceAllowedLegacyInertia) {
+2 -1
View File
@@ -196,7 +196,7 @@ TEST_F(UserCModelTest, NestedZeroMassBodiesFail) {
<worldbody> <worldbody>
<body> <body>
<geom size="1"/> <geom size="1"/>
<body> <body name="bad">
<freejoint/> <freejoint/>
<body> <body>
<body> <body>
@@ -214,6 +214,7 @@ TEST_F(UserCModelTest, NestedZeroMassBodiesFail) {
error, error,
HasSubstr( HasSubstr(
"mass and inertia of moving bodies must be larger than mjMINVAL")); "mass and inertia of moving bodies must be larger than mjMINVAL"));
EXPECT_THAT(error, HasSubstr("Element name 'bad'"));
mj_deleteModel(model); mj_deleteModel(model);
} }