Skip to content

Commit 7f11a0d

Browse files
quaglacopybara-github
authored andcommitted
Add check for height fields size during compilation.
Fixes #2427. PiperOrigin-RevId: 727805729 Change-Id: Idf3192e5fb238249faaeae6b4fb9de4c55755d96
1 parent 78a4d2f commit 7f11a0d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

python/mujoco/specs_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,5 +1135,29 @@ def test_bind(self):
11351135
):
11361136
print(mj_model.bind(joints).invalid)
11371137

1138+
def test_incorrect_hfield_size(self):
1139+
nrow = 300
1140+
ncol = 400
1141+
hdata = np.random.uniform(size=(1, 1))
1142+
model_spec = mujoco.MjSpec()
1143+
model_spec.add_hfield(
1144+
name='hfield',
1145+
size=[1, 1, 1, 1e-3],
1146+
ncol=ncol,
1147+
nrow=nrow,
1148+
userdata=hdata.flatten(),
1149+
)
1150+
model_spec.worldbody.add_geom(
1151+
name='hfield',
1152+
type=mujoco.mjtGeom.mjGEOM_HFIELD,
1153+
pos=np.array([0, 0, 1]),
1154+
hfieldname='hfield',
1155+
)
1156+
with self.assertRaisesRegex(
1157+
ValueError, r"Error: elevation data length must match nrow\*ncol\n"
1158+
"Element name 'hfield', id 0",
1159+
):
1160+
model_spec.compile()
1161+
11381162
if __name__ == '__main__':
11391163
absltest.main()

src/user/user_objects.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3696,6 +3696,9 @@ void mjCHField::Compile(const mjVFS* vfs) {
36963696

36973697
// copy userdata into data
36983698
if (!userdata_.empty()) {
3699+
if (nrow*ncol != userdata_.size()) {
3700+
throw mjCError(this, "elevation data length must match nrow*ncol");
3701+
}
36993702
data.assign(nrow*ncol, 0);
37003703
if (data.empty()) {
37013704
throw mjCError(this, "could not allocate buffers in hfield");

0 commit comments

Comments
 (0)