Attach the tree of defaults recursively and with namespacing.

PiperOrigin-RevId: 650664018
Change-Id: I17751a02a406dcb2a2ca218c316b5eb0370071b8
This commit is contained in:
Alessio Quaglino
2024-07-09 09:53:05 -07:00
committed by Copybara-Service
parent d0f86bc2c5
commit 462cfa5ceb
6 changed files with 111 additions and 50 deletions
+60 -16
View File
@@ -564,28 +564,58 @@ void mjCDef::Compile(const mjCModel* model) {
// assignment operator
mjCDef& mjCDef::operator=(const mjCDef& other) {
if (this != &other) {
name = other.name;
parent = other.parent;
child = other.child;
joint_ = other.joint_;
geom_ = other.geom_;
site_ = other.site_;
camera_ = other.camera_;
light_ = other.light_;
flex_ = other.flex_;
mesh_ = other.mesh_;
material_ = other.material_;
pair_ = other.pair_;
equality_ = other.equality_;
tendon_ = other.tendon_;
actuator_ = other.actuator_;
CopyWithoutChildren(other);
// copy the rest of the default tree
*this += other;
}
PointToLocal();
return *this;
}
mjCDef& mjCDef::operator+=(const mjCDef& other) {
for (unsigned int i=0; i<other.child.size(); i++) {
child.push_back(new mjCDef(*other.child[i])); // triggers recursive call
child.back()->parent = this;
}
return *this;
}
void mjCDef::NameSpace(const mjCModel* m) {
if (!name.empty()) {
name = m->prefix + name + m->suffix;
}
for (auto c : child) {
c->NameSpace(m);
}
}
void mjCDef::CopyWithoutChildren(const mjCDef& other) {
name = other.name;
parent = nullptr;
child.clear();
joint_ = other.joint_;
geom_ = other.geom_;
site_ = other.site_;
camera_ = other.camera_;
light_ = other.light_;
flex_ = other.flex_;
mesh_ = other.mesh_;
material_ = other.material_;
pair_ = other.pair_;
equality_ = other.equality_;
tendon_ = other.tendon_;
actuator_ = other.actuator_;
PointToLocal();
}
void mjCDef::PointToLocal() {
joint_.PointToLocal();
geom_.PointToLocal();
@@ -817,6 +847,13 @@ mjCBody& mjCBody::operator+=(const mjCFrame& other) {
other.model->prefix = other.prefix;
other.model->suffix = other.suffix;
// attach defaults
if (other.model != model) {
mjCDef* subdef = new mjCDef(*other.model->Default());
subdef->NameSpace(other.model);
*model += *subdef;
}
// copy input frame
frames.push_back(new mjCFrame(other));
frames.back()->body = this;
@@ -1591,6 +1628,13 @@ mjCFrame& mjCFrame::operator+=(const mjCBody& other) {
subtree->SetFrame(this);
subtree->NameSpace(other.model);
// attach defaults
if (other.model != model) {
mjCDef* subdef = new mjCDef(*other.model->Default());
subdef->NameSpace(other.model);
*model += *subdef;
}
// add to body children
body->bodies.push_back(subtree);