Skip to content

Commit 3577470

Browse files
quaglacopybara-github
authored andcommitted
Set parent frame of new frames after copying all frames.
PiperOrigin-RevId: 737620918 Change-Id: I8f49570cab39bd84b2da908d3683dddc3b29cb3c
1 parent b10b0b7 commit 3577470

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/user/user_objects.cc

+12-4
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ mjCBody& mjCBody::operator+=(const mjCBody& other) {
847847
// map other frames to indices
848848
std::map<mjCFrame*, int> fmap;
849849
for (int i=0; i<other.frames.size(); i++) {
850-
fmap[other.frames[i]] = i;
850+
fmap[other.frames[i]] = i + frames.size();
851851
}
852852

853853
// copy frames, needs to happen first
@@ -977,6 +977,7 @@ template <typename T>
977977
void mjCBody::CopyList(std::vector<T*>& dst, const std::vector<T*>& src,
978978
std::map<mjCFrame*, int>& fmap, const mjCFrame* pframe) {
979979
int nsrc = (int)src.size();
980+
int ndst = (int)dst.size();
980981
for (int i=0; i<nsrc; i++) {
981982
if (pframe && !pframe->IsAncestor(src[i]->frame)) {
982983
continue; // skip if the element is not inside pframe
@@ -996,12 +997,19 @@ void mjCBody::CopyList(std::vector<T*>& dst, const std::vector<T*>& src,
996997
dst.back()->AddRef();
997998
}
998999

999-
// assign dst frame to src frame
1000-
dst.back()->frame = src[i]->frame ? frames[fmap[src[i]->frame]] : nullptr;
1001-
10021000
// set namespace
10031001
dst.back()->NameSpace(src[i]->model);
10041002
}
1003+
1004+
// assign dst frame to src frame
1005+
// needs to be done after the copy in case T is an mjCFrame
1006+
int j = 0;
1007+
for (int i = 0; i < src.size(); i++) {
1008+
if (pframe && !pframe->IsAncestor(src[i]->frame)) {
1009+
continue; // skip if the element is not inside pframe
1010+
}
1011+
dst[ndst + j++]->frame = src[i]->frame ? frames[fmap[src[i]->frame]] : nullptr;
1012+
}
10051013
}
10061014

10071015

test/user/user_api_test.cc

+16
Original file line numberDiff line numberDiff line change
@@ -2569,5 +2569,21 @@ TEST_F(MujocoTest, ErrorWhenCompilingOrphanedSpec) {
25692569
mj_deleteSpec(child);
25702570
}
25712571

2572+
TEST_F(MujocoTest, SetFrameReverseOrder) {
2573+
mjSpec* spec = mj_makeSpec();
2574+
mjsBody* world = mjs_findBody(spec, "world");
2575+
mjsFrame* child = mjs_addFrame(world, nullptr);
2576+
mjsFrame* parent = mjs_addFrame(world, nullptr);
2577+
mjs_setString(child->name, "child");
2578+
mjs_setString(parent->name, "parent");
2579+
mjs_setFrame(child->element, parent);
2580+
mjSpec* copy = mj_copySpec(spec);
2581+
EXPECT_THAT(copy, NotNull());
2582+
EXPECT_THAT(mjs_findFrame(copy, "child"), NotNull());
2583+
EXPECT_THAT(mjs_findFrame(copy, "parent"), NotNull());
2584+
mj_deleteSpec(spec);
2585+
mj_deleteSpec(copy);
2586+
}
2587+
25722588
} // namespace
25732589
} // namespace mujoco

0 commit comments

Comments
 (0)