Add check for height fields size during compilation.
Fixes #2427. PiperOrigin-RevId: 727805729 Change-Id: Idf3192e5fb238249faaeae6b4fb9de4c55755d96
This commit is contained in:
committed by
Copybara-Service
parent
78a4d2f389
commit
7f11a0df12
@@ -1135,5 +1135,29 @@ class SpecsTest(absltest.TestCase):
|
|||||||
):
|
):
|
||||||
print(mj_model.bind(joints).invalid)
|
print(mj_model.bind(joints).invalid)
|
||||||
|
|
||||||
|
def test_incorrect_hfield_size(self):
|
||||||
|
nrow = 300
|
||||||
|
ncol = 400
|
||||||
|
hdata = np.random.uniform(size=(1, 1))
|
||||||
|
model_spec = mujoco.MjSpec()
|
||||||
|
model_spec.add_hfield(
|
||||||
|
name='hfield',
|
||||||
|
size=[1, 1, 1, 1e-3],
|
||||||
|
ncol=ncol,
|
||||||
|
nrow=nrow,
|
||||||
|
userdata=hdata.flatten(),
|
||||||
|
)
|
||||||
|
model_spec.worldbody.add_geom(
|
||||||
|
name='hfield',
|
||||||
|
type=mujoco.mjtGeom.mjGEOM_HFIELD,
|
||||||
|
pos=np.array([0, 0, 1]),
|
||||||
|
hfieldname='hfield',
|
||||||
|
)
|
||||||
|
with self.assertRaisesRegex(
|
||||||
|
ValueError, r"Error: elevation data length must match nrow\*ncol\n"
|
||||||
|
"Element name 'hfield', id 0",
|
||||||
|
):
|
||||||
|
model_spec.compile()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
absltest.main()
|
absltest.main()
|
||||||
|
|||||||
@@ -3696,6 +3696,9 @@ void mjCHField::Compile(const mjVFS* vfs) {
|
|||||||
|
|
||||||
// copy userdata into data
|
// copy userdata into data
|
||||||
if (!userdata_.empty()) {
|
if (!userdata_.empty()) {
|
||||||
|
if (nrow*ncol != userdata_.size()) {
|
||||||
|
throw mjCError(this, "elevation data length must match nrow*ncol");
|
||||||
|
}
|
||||||
data.assign(nrow*ncol, 0);
|
data.assign(nrow*ncol, 0);
|
||||||
if (data.empty()) {
|
if (data.empty()) {
|
||||||
throw mjCError(this, "could not allocate buffers in hfield");
|
throw mjCError(this, "could not allocate buffers in hfield");
|
||||||
|
|||||||
Reference in New Issue
Block a user