Geoms of type mesh with zero density don't trigger the check for valid mesh volume.

PiperOrigin-RevId: 629416817
Change-Id: I23eaa0d61a703988a4eb96a3cad09e562384035e
This commit is contained in:
Yuval Tassa
2024-04-30 08:11:30 -07:00
committed by Copybara-Service
parent 26f23066d1
commit 6f97f42458
2 changed files with 38 additions and 3 deletions
+12 -3
View File
@@ -2344,6 +2344,7 @@ void mjCGeom::Compile(void) {
// compute geom mass and inertia
if (inferinertia) {
// mass is defined
if (mjuu_defined(mass)) {
if (mass==0) {
mass_ = 0;
@@ -2353,11 +2354,19 @@ void mjCGeom::Compile(void) {
density = mass / GetVolume();
SetInertia();
}
} else {
mass_ = density * GetVolume();
SetInertia();
}
// mass is not defined
else {
if (density == 0) {
mass_ = 0;
} else {
mass_ = density * GetVolume();
SetInertia();
}
}
// check for negative values
if (mass_<0 || inertia[0]<0 || inertia[1]<0 || inertia[2]<0 || density<0)
throw mjCError(this, "mass, inertia or density are negative in geom");
+26
View File
@@ -755,6 +755,32 @@ TEST_F(MjCGeomTest, NanSize) {
EXPECT_THAT(error.data(), HasSubstr("line 5"));
}
TEST_F(MjCGeomTest, BadMeshZeroMassDensityDoesntError) {
static constexpr char xml[] = R"(
<mujoco>
<asset>
<mesh name="bad_mesh"
vertex="0 0 0 1 0 0 0 1 0 0 0 1"
face="0 2 1" />
</asset>
<worldbody>
<body>
<geom type="mesh" mesh="bad_mesh" mass="0"/>
</body>
<body>
<geom type="mesh" mesh="bad_mesh" density="0"/>
</body>
</worldbody>
</mujoco>
)";
std::array<char, 1024> error;
mjModel* model = LoadModelFromString(xml, error.data(), error.size());
ASSERT_THAT(model, NotNull()) << error.data();
EXPECT_EQ(model->body_mass[1], 0);
EXPECT_EQ(model->body_mass[2], 0);
mj_deleteModel(model);
}
// ------------- test height fields --------------------------------------------
using MjCHFieldTest = MujocoTest;