Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor GfxReplayTest::testSimulatorIntegration so that separate sim…
Browse files Browse the repository at this point in the history
…ulator instance record and play the playback file.
0mdc committed Sep 6, 2022
1 parent 23aad33 commit 4678aa7
Showing 1 changed file with 76 additions and 55 deletions.
131 changes: 76 additions & 55 deletions src/tests/GfxReplayTest.cpp
Original file line number Diff line number Diff line change
@@ -381,65 +381,86 @@ void GfxReplayTest::testPlayerReadInvalidFile() {

// test recording and playback through the simulator interface
void GfxReplayTest::testSimulatorIntegration() {
std::string boxFile =
const std::string boxFile =
Cr::Utility::Path::join(TEST_ASSETS, "objects/transform_box.glb");
auto testFilepath =
const auto testFilepath =
Corrade::Utility::Path::join(DATA_DIR, "./gfx_replay_test.json");

SimulatorConfiguration simConfig{};
simConfig.activeSceneName = boxFile;
simConfig.enableGfxReplaySave = true;
simConfig.createRenderer = false;

auto sim = Simulator::create_unique(simConfig);
auto objAttrMgr = sim->getObjectAttributesManager();
objAttrMgr->loadAllJSONConfigsFromPath(
Cr::Utility::Path::join(TEST_ASSETS, "objects/nested_box"), true);

auto handles = objAttrMgr->getObjectHandlesBySubstring("nested_box");
CORRADE_VERIFY(!handles.empty());
auto rigidObj =
sim->getRigidObjectManager()->addBulletObjectByHandle(handles[0]);
auto rigidObjTranslation = Mn::Vector3(1.f, 2.f, 3.f);
auto rigidObjRotation = Mn::Quaternion::rotation(
const auto rigidObjTranslation = Mn::Vector3(1.f, 2.f, 3.f);
const auto rigidObjRotation = Mn::Quaternion::rotation(
Mn::Deg(45.f), Mn::Vector3(1.f, 1.f, 0.f).normalized());
rigidObj->setTranslation(rigidObjTranslation);
rigidObj->setRotation(rigidObjRotation);

auto& sceneGraph = sim->getActiveSceneGraph();
auto& rootNode = sceneGraph.getRootNode();
auto prevNumberOfChildrenOfRoot = getNumberOfChildrenOfRoot(rootNode);

const auto recorder = sim->getGfxReplayManager()->getRecorder();
CORRADE_VERIFY(recorder);
recorder->saveKeyframe();
recorder->writeSavedKeyframesToFile(testFilepath);

auto player = sim->getGfxReplayManager()->readKeyframesFromFile(testFilepath);
CORRADE_VERIFY(player);
CORRADE_COMPARE(player->getNumKeyframes(), 1);
player->setKeyframeIndex(0);
// second copies of transform_box and nested_box were loaded
CORRADE_COMPARE(getNumberOfChildrenOfRoot(rootNode),
prevNumberOfChildrenOfRoot + 2);

const auto& keyframes = player->debugGetKeyframes();
// we expect state updates for the state and the object instance
CORRADE_COMPARE(keyframes[0].stateUpdates.size(), 2);
// check the pose for nested_box
const auto& stateUpdate = keyframes[0].stateUpdates[1];
const auto transform = stateUpdate.second.absTransform;
CORRADE_COMPARE_AS((transform.translation - rigidObjTranslation).length(),
1.0e-5, Cr::TestSuite::Compare::LessOrEqual);
CORRADE_COMPARE_AS(
(transform.rotation.vector() - rigidObjRotation.vector()).length(),
1.0e-5, Cr::TestSuite::Compare::LessOrEqual);

player = nullptr;
// second copies of transform_box and nested_box are removed when Player
// is deleted
CORRADE_COMPARE(getNumberOfChildrenOfRoot(rootNode),
prevNumberOfChildrenOfRoot);
int prevNumberOfChildrenOfRoot = 0;

// record a playback file
{
SimulatorConfiguration simConfig{};
simConfig.activeSceneName = boxFile;
simConfig.enableGfxReplaySave = true;
simConfig.createRenderer = false;
simConfig.enablePhysics = false;
auto sim = Simulator::create_unique(simConfig);

auto& sceneGraph = sim->getActiveSceneGraph();
auto& rootNode = sceneGraph.getRootNode();

auto objAttrMgr = sim->getObjectAttributesManager();
objAttrMgr->loadAllJSONConfigsFromPath(
Cr::Utility::Path::join(TEST_ASSETS, "objects/nested_box"), true);
prevNumberOfChildrenOfRoot = getNumberOfChildrenOfRoot(rootNode);

auto handles = objAttrMgr->getObjectHandlesBySubstring("nested_box");
CORRADE_VERIFY(!handles.empty());
auto rigidObj =
sim->getRigidObjectManager()->addBulletObjectByHandle(handles[0]);
rigidObj->setTranslation(rigidObjTranslation);
rigidObj->setRotation(rigidObjRotation);

const auto recorder = sim->getGfxReplayManager()->getRecorder();
CORRADE_VERIFY(recorder);
recorder->saveKeyframe();
recorder->writeSavedKeyframesToFile(testFilepath);
}

// read the playback file
{
SimulatorConfiguration simConfig{};
simConfig.enableGfxReplaySave = false;
simConfig.createRenderer = false;
simConfig.enablePhysics = false;

auto sim = Simulator::create_unique(simConfig);
auto& sceneGraph = sim->getActiveSceneGraph();
auto& rootNode = sceneGraph.getRootNode();

CORRADE_COMPARE(getNumberOfChildrenOfRoot(rootNode),
1); // static stage object

auto player =
sim->getGfxReplayManager()->readKeyframesFromFile(testFilepath);
CORRADE_VERIFY(player);
CORRADE_COMPARE(player->getNumKeyframes(), 1);
player->setKeyframeIndex(0);

CORRADE_COMPARE(getNumberOfChildrenOfRoot(rootNode),
prevNumberOfChildrenOfRoot + 1);

const auto& keyframes = player->debugGetKeyframes();
// we expect state updates for the state and the object instance
CORRADE_COMPARE(keyframes[0].stateUpdates.size(), 2);
// check the pose for nested_box
const auto& stateUpdate = keyframes[0].stateUpdates[1];
const auto transform = stateUpdate.second.absTransform;
CORRADE_COMPARE_AS((transform.translation - rigidObjTranslation).length(),
1.0e-5, Cr::TestSuite::Compare::LessOrEqual);
CORRADE_COMPARE_AS(
(transform.rotation.vector() - rigidObjRotation.vector()).length(),
1.0e-5, Cr::TestSuite::Compare::LessOrEqual);

// instances of transform_box and nested_box are removed when Player
// is deleted. The static stage object remains.
player = nullptr;
CORRADE_COMPARE(getNumberOfChildrenOfRoot(rootNode), 1);
}

// remove file created for this test
bool success = Corrade::Utility::Path::remove(testFilepath);

0 comments on commit 4678aa7

Please sign in to comment.