Add tree element support to mjs_firstElement() and mjs_nextElement().

PiperOrigin-RevId: 671003046
Change-Id: I9e07d7c85c7218e9bded0f50af9db62d0f1e21e1
This commit is contained in:
Alessio Quaglino
2024-09-04 09:45:35 -07:00
committed by Copybara-Service
parent d3dfa6f970
commit ae4987026c
4 changed files with 79 additions and 24 deletions
+8
View File
@@ -892,6 +892,14 @@ mjsElement* mjCModel::NextObject(mjsElement* object, mjtObj type) {
}
switch (type) {
case mjOBJ_BODY:
case mjOBJ_SITE:
case mjOBJ_GEOM:
case mjOBJ_JOINT:
case mjOBJ_CAMERA:
case mjOBJ_LIGHT:
case mjOBJ_FRAME:
return bodies_[0]->NextChild(object, type, /*recursive=*/true);
case mjOBJ_ACTUATOR:
return GetNext(actuators_, object);
case mjOBJ_SENSOR:
+44 -13
View File
@@ -1253,26 +1253,38 @@ mjCBase* mjCBody::FindObject(mjtObj type, std::string _name, bool recursive) {
template <class T>
static mjsElement* GetNext(std::vector<T*>& list, mjsElement* child) {
mjsElement* mjCBody::GetNext(std::vector<T*>& list, const mjsElement* child, bool recursive) {
if (list.empty()) {
// no children
return nullptr;
}
if (!child) {
if (list.empty()) {
return nullptr;
}
// first child
return list[0]->spec.element;
}
for (unsigned int i = 0; i < list.size()-1; i++) {
// next child is in this body
if (list[i]->spec.element == child) {
return list[i+1]->spec.element;
}
}
if (recursive && list.back()->spec.element == child) {
// next child is in next body
for (int i=0; i<(int)bodies.size(); i++) {
return bodies[i]->NextChild(NULL, child->elemtype, true);
}
}
return nullptr;
}
// get next child of given type
mjsElement* mjCBody::NextChild(mjsElement* child, mjtObj type) {
mjsElement* mjCBody::NextChild(mjsElement* child, mjtObj type, bool recursive) {
if (type == mjOBJ_UNKNOWN) {
if (!child) {
throw mjCError(this, "child type must be specified if no child element is given");
@@ -1283,25 +1295,44 @@ mjsElement* mjCBody::NextChild(mjsElement* child, mjtObj type) {
throw mjCError(this, "child element is not of requested type");
}
mjsElement* candidate = nullptr;
switch (type) {
case mjOBJ_BODY:
case mjOBJ_XBODY:
return GetNext(bodies, child);
candidate = GetNext(bodies, child, recursive);
break;
case mjOBJ_JOINT:
return GetNext(joints, child);
candidate = GetNext(joints, child, recursive);
break;
case mjOBJ_GEOM:
return GetNext(geoms, child);
candidate = GetNext(geoms, child, recursive);
break;
case mjOBJ_SITE:
return GetNext(sites, child);
candidate = GetNext(sites, child, recursive);
break;
case mjOBJ_CAMERA:
return GetNext(cameras, child);
candidate = GetNext(cameras, child, recursive);
break;
case mjOBJ_LIGHT:
return GetNext(lights, child);
candidate = GetNext(lights, child, recursive);
break;
case mjOBJ_FRAME:
return GetNext(frames, child);
candidate = GetNext(frames, child, recursive);
break;
default:
return nullptr;
break;
}
if (!candidate && recursive) {
for (int i=0; i<(int)bodies.size(); i++) {
candidate = bodies[i]->NextChild(child, type, true);
if (candidate) {
return candidate;
}
}
}
return candidate;
}
+5 -1
View File
@@ -322,7 +322,7 @@ class mjCBody : public mjCBody_, private mjsBody {
const std::vector<double>& get_userdata() { return userdata_; }
// get next child of given type
mjsElement* NextChild(mjsElement* child, mjtObj type = mjOBJ_UNKNOWN);
mjsElement* NextChild(mjsElement* child, mjtObj type = mjOBJ_UNKNOWN, bool recursive = false);
// reset keyframe references for allowing self-attach
void ForgetKeyframes() const;
@@ -355,6 +355,10 @@ class mjCBody : public mjCBody_, private mjsBody {
template <typename T>
void CopyList(std::vector<T*>& dst, const std::vector<T*>& src,
std::map<mjCFrame*, int>& fmap, const mjCFrame* pframe = nullptr);
// gets next child of the same type in this body
template <class T>
mjsElement* GetNext(std::vector<T*>& list, const mjsElement* child, bool recursive = false);
};