b362cb4972
This change also requires user scripts to explicitly synchronize changes to physics state to the viewer. The Simulate class was reconfigured so that certain UI events are handled during this sync operation, outside of the render loop on the main thread. These correspond to operations that require access to the full mjModel/mjData. To support other, more interactive operations (e.g. camera movements), a new mjvSceneState struct is introduced which captures only the portion of the physics state required for scene re-rendering. The mjvSceneState is updated from mjModel/mjData during the viewer sync operation, and is significantly cheaper than a full mj_copyModel and mj_copyData. Fixes https://github.com/deepmind/mujoco/issues/796 PiperOrigin-RevId: 525723636 Change-Id: Id08d0210a2c067d5afe85e2bf104f276aeddd75e
117 lines
3.5 KiB
C++
117 lines
3.5 KiB
C++
// Copyright 2023 DeepMind Technologies Limited
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include <cstring>
|
|
#include <string>
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
#include <mujoco/mjvisualize.h>
|
|
#include <mujoco/mujoco.h>
|
|
#include "test/fixture.h"
|
|
|
|
namespace mujoco {
|
|
namespace {
|
|
|
|
using ::testing::NotNull;
|
|
using MjvSceneStateTest = MujocoTest;
|
|
|
|
constexpr int kMaxGeom = 10000;
|
|
|
|
TEST_F(MjvSceneStateTest, CanUpdateFromState) {
|
|
constexpr char path[] = "engine/testdata/hammock/hammock.xml";
|
|
const std::string xml_path = GetTestDataFilePath(path);
|
|
mjModel* model = mj_loadXML(xml_path.c_str(), nullptr, 0, 0);
|
|
ASSERT_THAT(model, NotNull());
|
|
mjData* data = mj_makeData(model);
|
|
|
|
while (data->time < 2) {
|
|
mj_step(model, data);
|
|
}
|
|
ASSERT_GT(data->ncon, 10);
|
|
|
|
mjvScene scn1;
|
|
mjv_defaultScene(&scn1);
|
|
mjv_makeScene(model, &scn1, kMaxGeom);
|
|
|
|
mjvOption opt;
|
|
mjv_defaultOption(&opt);
|
|
|
|
mjvPerturb pert;
|
|
mjv_defaultPerturb(&pert);
|
|
|
|
mjvCamera cam;
|
|
mjv_defaultFreeCamera(model, &cam);
|
|
|
|
// Enable all flags to exercise all code paths
|
|
for (int i = 0; i < mjNVISFLAG; ++i) {
|
|
opt.flags[i] = 1;
|
|
}
|
|
|
|
mjv_updateScene(model, data, &opt, &pert, &cam, mjCAT_ALL, &scn1);
|
|
EXPECT_GT(scn1.ngeom, 0);
|
|
EXPECT_GT(scn1.nskin, 0);
|
|
EXPECT_GT(scn1.nlight, 0);
|
|
|
|
mjvSceneState scnstate;
|
|
mjv_defaultSceneState(&scnstate);
|
|
mjv_makeSceneState(model, data, &scnstate, kMaxGeom);
|
|
mjv_updateSceneState(model, data, &scnstate);
|
|
|
|
mjvScene scn2;
|
|
mjv_defaultScene(&scn2);
|
|
mjv_makeScene(model, &scn2, kMaxGeom);
|
|
mjv_updateSceneFromState(&scnstate, &opt, &pert, &cam, mjCAT_ALL, &scn2);
|
|
|
|
EXPECT_EQ(scn1.ngeom, scn2.ngeom);
|
|
for (int i = 0; i < scn1.ngeom; ++i) {
|
|
EXPECT_EQ(std::memcmp(&scn1.geoms[i], &scn2.geoms[i], sizeof(mjvGeom)), 0);
|
|
}
|
|
// NB: scn->geomorder is a scratch space for use by mjr_render, so we don't
|
|
// need to compare them here.
|
|
|
|
EXPECT_LE(scn1.nskin, scn2.nskin);
|
|
EXPECT_EQ(std::memcmp(scn1.skinfacenum, scn2.skinfacenum,
|
|
sizeof(*scn2.skinfacenum) * scn2.nskin),
|
|
0);
|
|
EXPECT_EQ(std::memcmp(scn1.skinvertadr, scn2.skinvertadr,
|
|
sizeof(*scn2.skinvertadr) * scn2.nskin),
|
|
0);
|
|
EXPECT_EQ(std::memcmp(scn1.skinvertnum, scn2.skinvertnum,
|
|
sizeof(*scn2.skinvertnum) * scn2.nskin),
|
|
0);
|
|
EXPECT_EQ(std::memcmp(scn1.skinvert, scn2.skinvert,
|
|
sizeof(*scn2.skinvert) * scn2.nskin),
|
|
0);
|
|
EXPECT_EQ(std::memcmp(scn1.skinnormal, scn2.skinnormal,
|
|
sizeof(*scn2.skinnormal) * scn2.nskin),
|
|
0);
|
|
|
|
auto scn1_cmp_begin = reinterpret_cast<char*>(&scn1.nlight);
|
|
auto scn2_cmp_begin = reinterpret_cast<char*>(&scn2.nlight);
|
|
auto cmp_bytes =
|
|
sizeof(mjvScene) - (scn2_cmp_begin - reinterpret_cast<char*>(&scn2));
|
|
EXPECT_EQ(std::memcmp(scn1_cmp_begin, scn2_cmp_begin, cmp_bytes), 0);
|
|
|
|
mjv_freeScene(&scn1);
|
|
mjv_freeScene(&scn2);
|
|
mjv_freeSceneState(&scnstate);
|
|
|
|
mj_deleteData(data);
|
|
mj_deleteModel(model);
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace mujoco
|