Cosmetic improvements to src/xml/ source files.

PiperOrigin-RevId: 738010419
Change-Id: Idb90a42a374682450737684e67388329c184c777
This commit is contained in:
Yuval Tassa
2025-03-18 08:11:40 -07:00
committed by Copybara-Service
parent 205599ac1a
commit efe213a7b3
5 changed files with 743 additions and 733 deletions
+6 -4
View File
@@ -98,9 +98,11 @@ mjModel* mj_loadXML(const char* filename, const mjVFS* vfs,
char* error, int error_sz) {
// parse new model
std::unique_ptr<mjSpec, std::function<void(mjSpec*)>> spec(
ParseXML(filename, vfs, error, error_sz),
[](mjSpec* s) { mj_deleteSpec(s); });
std::unique_ptr<mjSpec, std::function<void(mjSpec*)> > spec(
ParseXML(filename, vfs, error, error_sz),
[](mjSpec* s) {
mj_deleteSpec(s);
});
if (!spec) {
return nullptr;
}
@@ -166,7 +168,7 @@ int mj_printSchema(const char* filename, char* buffer, int buffer_sz, int flg_ht
// print to stringstream
mjXReader reader;
std::stringstream str;
reader.PrintSchema(str, flg_html!=0, flg_pad!=0);
reader.PrintSchema(str, flg_html != 0, flg_pad != 0);
// filename given: write to file
if (filename) {
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+74 -74
View File
@@ -125,7 +125,7 @@ void mjXURDF::Parse(
while (elem) {
// identify link elements
name = elem->Value();
if (name=="link") {
if (name == "link") {
ReadAttrTxt(elem, "name", text, true);
text = GetPrefixedName(text);
AddBody(text);
@@ -140,7 +140,7 @@ void mjXURDF::Parse(
while (elem) {
// identify joint elements
name = elem->Value();
if (name=="joint") {
if (name == "joint") {
// find parent, get name and id
temp = FindSubElem(elem, "parent", true);
ReadAttrTxt(temp, "link", text, true);
@@ -154,12 +154,12 @@ void mjXURDF::Parse(
id_child = FindName(text, urName);
// make sure parent and child exist
if (id_parent<0 || id_child<0) {
if (id_parent < 0 || id_child < 0) {
throw mjXError(elem, "URDF joint parent or child missing");
}
// check for multiple parents
if (urParent[id_child]>=0) {
if (urParent[id_child] >= 0) {
throw mjXError(elem, "URDF body has multiple parents:", urName[id_child].c_str());
}
@@ -173,7 +173,7 @@ void mjXURDF::Parse(
}
// find all top-level bodies, call recursive tree constructor
for (int i=0; i<(int)urName.size(); i++) {
for (int i=0; i < (int)urName.size(); i++) {
if (urParent[i] < 0) {
AddToTree(i);
}
@@ -184,7 +184,7 @@ void mjXURDF::Parse(
while (elem) {
// identify body/link elements
name = elem->Value();
if (name=="link") {
if (name == "link") {
Body(elem);
}
@@ -197,7 +197,7 @@ void mjXURDF::Parse(
while (elem) {
// identify body/link elements
name = elem->Value();
if (name=="joint") {
if (name == "joint") {
Joint(elem);
}
@@ -297,7 +297,7 @@ void mjXURDF::Body(XMLElement* body_elem) {
name = elem->Value();
// visual element
if (name=="visual") {
if (name == "visual") {
// parse material
if ((temp = FindSubElem(elem, "material"))) {
// if color specified - use directly
@@ -310,7 +310,7 @@ void mjXURDF::Body(XMLElement* body_elem) {
ReadAttrTxt(temp, "name", name, true);
name = GetPrefixedName(name);
int imat = FindName(name, urMat);
if (imat>=0) {
if (imat >= 0) {
std::memcpy(rgba, urRGBA[imat].val, 4*sizeof(float));
}
}
@@ -320,7 +320,7 @@ void mjXURDF::Body(XMLElement* body_elem) {
pgeom = Geom(elem, pbody, false);
// save color
if (rgba[0]>=0) {
if (rgba[0] >= 0) {
std::memcpy(pgeom->rgba, rgba, 4*sizeof(float));
}
@@ -339,11 +339,11 @@ void mjXURDF::Body(XMLElement* body_elem) {
}
// collision element
else if (name=="collision") {
else if (name == "collision") {
pgeom = Geom(elem, pbody, true);
// use color from last visual
if (rgba[0]>=0) {
if (rgba[0] >= 0) {
std::memcpy(pgeom->rgba, rgba, 4*sizeof(float));
}
@@ -418,71 +418,71 @@ void mjXURDF::Joint(XMLElement* joint_elem) {
// create joint (unless fixed)
double mat[9], quat[4], tmpaxis[3];
switch (jointtype) {
case 0: // revolute
case 1: // continuous
pjoint = mjs_addJoint(pbody, 0);
mjs_setString(pjoint->name, jntname.c_str());
pjoint->type = mjJNT_HINGE;
mjuu_setvec(pjoint->pos, 0, 0, 0);
mjuu_copyvec(pjoint->axis, axis, 3);
break;
case 0: // revolute
case 1: // continuous
pjoint = mjs_addJoint(pbody, 0);
mjs_setString(pjoint->name, jntname.c_str());
pjoint->type = mjJNT_HINGE;
mjuu_setvec(pjoint->pos, 0, 0, 0);
mjuu_copyvec(pjoint->axis, axis, 3);
break;
case 2: // prismatic
pjoint = mjs_addJoint(pbody, 0);
mjs_setString(pjoint->name, jntname.c_str());
pjoint->type = mjJNT_SLIDE;
mjuu_setvec(pjoint->pos, 0, 0, 0);
mjuu_copyvec(pjoint->axis, axis, 3);
break;
case 2: // prismatic
pjoint = mjs_addJoint(pbody, 0);
mjs_setString(pjoint->name, jntname.c_str());
pjoint->type = mjJNT_SLIDE;
mjuu_setvec(pjoint->pos, 0, 0, 0);
mjuu_copyvec(pjoint->axis, axis, 3);
break;
case 3: // fixed- no joint, return
return;
case 3: // fixed- no joint, return
return;
case 4: // floating
pjoint = mjs_addJoint(pbody, 0);
mjs_setString(pjoint->name, jntname.c_str());
pjoint->type = mjJNT_FREE;
break;
case 4: // floating
pjoint = mjs_addJoint(pbody, 0);
mjs_setString(pjoint->name, jntname.c_str());
pjoint->type = mjJNT_FREE;
break;
case 5: // planar- construct complex joint
// make frame with axis = z
mjuu_z2quat(quat, axis);
mjuu_quat2mat(mat, quat);
case 5: // planar- construct complex joint
// make frame with axis = z
mjuu_z2quat(quat, axis);
mjuu_quat2mat(mat, quat);
// construct slider along x
pjoint = mjs_addJoint(pbody, 0);
mjs_setString(pjoint->name, (jntname + "_TX").c_str());
pjoint->type = mjJNT_SLIDE;
tmpaxis[0] = mat[0];
tmpaxis[1] = mat[3];
tmpaxis[2] = mat[6];
mjuu_setvec(pjoint->pos, 0, 0, 0);
mjuu_copyvec(pjoint->axis, tmpaxis, 3);
// construct slider along x
pjoint = mjs_addJoint(pbody, 0);
mjs_setString(pjoint->name, (jntname + "_TX").c_str());
pjoint->type = mjJNT_SLIDE;
tmpaxis[0] = mat[0];
tmpaxis[1] = mat[3];
tmpaxis[2] = mat[6];
mjuu_setvec(pjoint->pos, 0, 0, 0);
mjuu_copyvec(pjoint->axis, tmpaxis, 3);
// construct slider along y
pjoint1 = mjs_addJoint(pbody, 0);
mjs_setString(pjoint1->name, (jntname + "_TY").c_str());
pjoint1->type = mjJNT_SLIDE;
tmpaxis[0] = mat[1];
tmpaxis[1] = mat[4];
tmpaxis[2] = mat[7];
mjuu_setvec(pjoint1->pos, 0, 0, 0);
mjuu_copyvec(pjoint1->axis, tmpaxis, 3);
// construct slider along y
pjoint1 = mjs_addJoint(pbody, 0);
mjs_setString(pjoint1->name, (jntname + "_TY").c_str());
pjoint1->type = mjJNT_SLIDE;
tmpaxis[0] = mat[1];
tmpaxis[1] = mat[4];
tmpaxis[2] = mat[7];
mjuu_setvec(pjoint1->pos, 0, 0, 0);
mjuu_copyvec(pjoint1->axis, tmpaxis, 3);
// construct hinge around z = locaxis
pjoint2 = mjs_addJoint(pbody, 0);
mjs_setString(pjoint2->name, (jntname + "_RZ").c_str());
pjoint2->type = mjJNT_HINGE;
mjuu_setvec(pjoint2->pos, 0, 0, 0);
mjuu_copyvec(pjoint2->axis, axis, 3);
break;
// construct hinge around z = locaxis
pjoint2 = mjs_addJoint(pbody, 0);
mjs_setString(pjoint2->name, (jntname + "_RZ").c_str());
pjoint2->type = mjJNT_HINGE;
mjuu_setvec(pjoint2->pos, 0, 0, 0);
mjuu_copyvec(pjoint2->axis, axis, 3);
break;
case 6: // ball joint
pjoint = mjs_addJoint(pbody, 0);
mjs_setString(pjoint->name, jntname.c_str());
pjoint->type = mjJNT_BALL;
mjuu_setvec(pjoint->pos, 0, 0, 0);
mjuu_copyvec(pjoint->axis, axis, 3);
case 6: // ball joint
pjoint = mjs_addJoint(pbody, 0);
mjs_setString(pjoint->name, jntname.c_str());
pjoint->type = mjJNT_BALL;
mjuu_setvec(pjoint->pos, 0, 0, 0);
mjuu_copyvec(pjoint->axis, axis, 3);
}
// dynamics element
@@ -546,7 +546,7 @@ mjsGeom* mjXURDF::Geom(XMLElement* geom_elem, mjsBody* pbody, bool collision) {
// box
if ((temp = FindSubElem(elem, "box"))) {
ReadAttr(temp, "size", 3, pgeom->size, text, true, true);
for (int i=0; i<3; i++) {
for (int i=0; i < 3; i++) {
pgeom->size[i] /= 2; // MuJoCo uses half-length
}
}
@@ -673,7 +673,7 @@ void mjXURDF::Origin(XMLElement* origin_elem, double* pos, double* quat) {
// find body with given name in list, return -1 if not found
int mjXURDF::FindName(std::string name, std::vector<std::string>& list) {
for (unsigned int i=0; i<list.size(); i++)
for (unsigned int i=0; i < list.size(); i++)
if (list[i] == name) {
return i;
}
@@ -686,7 +686,7 @@ int mjXURDF::FindName(std::string name, std::vector<std::string>& list) {
// add name to list, error if name already exists
void mjXURDF::AddName(std::string name, std::vector<std::string>& list) {
// make sure name is unique
if (FindName(name, list)>=0) {
if (FindName(name, list) >= 0) {
throw mjXError(0, "repeated URDF name: ", name.c_str());
}
@@ -713,7 +713,7 @@ void mjXURDF::AddBody(std::string name) {
void mjXURDF::AddToTree(int n) {
// get pointer to parent in mjCModel tree
mjsBody *parent = 0, *child = 0, *world = 0;
if (urParent[n]>=0) {
if (urParent[n] >= 0) {
world = mjs_findBody(spec, "world");
parent = mjs_findChild(world, urName[urParent[n]].c_str());
@@ -731,7 +731,7 @@ void mjXURDF::AddToTree(int n) {
}
// add children recursively
for (int i=0; i<(int)urChildren[n].size(); i++) {
for (int i=0; i < (int)urChildren[n].size(); i++) {
AddToTree(urChildren[n][i]);
}
}
+41 -41
View File
@@ -112,7 +112,7 @@ FilePath ResolveFilePath(XMLElement* e, const FilePath& filename,
// error string copy
void mjCopyError(char* dst, const char* src, int maxlen) {
if (dst && maxlen>0) {
if (dst && maxlen > 0) {
strncpy(dst, src, maxlen);
dst[maxlen-1] = 0;
}
@@ -243,7 +243,7 @@ std::string mjXSchema::GetError() {
// print spaces
static void printspace(std::stringstream& str, int n, const char* space) {
for (int i=0; i<n; i++) {
for (int i=0; i < n; i++) {
str << space;
}
}
@@ -259,14 +259,14 @@ void mjXSchema::Print(std::stringstream& str, int level) const {
printspace(str, 3*level, " ");
str << name1 << " (" << type_ << ")";
int baselen = 3*level + (int)name1.size() + 4;
if (baselen<30) {
if (baselen < 30) {
printspace(str, 30-baselen, " ");
}
// attributes
int cnt = std::max(baselen, 30);
for (const std::string& attr : attr_) {
if (cnt>60) {
if (cnt > 60) {
str << "\n";
printspace(str, (cnt = std::max(30, baselen)), " ");
}
@@ -290,7 +290,7 @@ void mjXSchema::PrintHTML(std::stringstream& str, int level, bool pad) const {
std::string name1 = (name_ == "body" ? "(world)body" : name_);
// open table
if (level==0) {
if (level == 0) {
str << "<table border=\"1\">\n";
}
@@ -341,9 +341,9 @@ bool mjXSchema::NameMatch(XMLElement* elem, int level) {
// special handling of body, worldbody, and frame
if (name_ == "body" &&
((level == 1 && !strcmp(elem->Value(), "worldbody")) ||
(level != 1 && !strcmp(elem->Value(), "body")) ||
(level >= 1 && !strcmp(elem->Value(), "frame")) ||
(level >= 1 && !strcmp(elem->Value(), "replicate")))) {
(level != 1 && !strcmp(elem->Value(), "body")) ||
(level >= 1 && !strcmp(elem->Value(), "frame")) ||
(level >= 1 && !strcmp(elem->Value(), "replicate")))) {
return true;
}
@@ -426,23 +426,23 @@ XMLElement* mjXSchema::Check(XMLElement* elem, int level) {
msg[0] = '\0';
for (mjXSchema& subschema : subschema_) {
switch (subschema.type_) {
case '!':
if (subschema.refcnt_ > 1)
mju::sprintf_arr(msg, "unique element '%s' found %d times",
subschema.name_.c_str(), subschema.refcnt_);
else if (subschema.refcnt_ < 1)
mju::sprintf_arr(msg, "element '%s' is required",
subschema.name_.c_str());
break;
case '!':
if (subschema.refcnt_ > 1)
mju::sprintf_arr(msg, "unique element '%s' found %d times",
subschema.name_.c_str(), subschema.refcnt_);
else if (subschema.refcnt_ < 1)
mju::sprintf_arr(msg, "element '%s' is required",
subschema.name_.c_str());
break;
case '?':
if (subschema.refcnt_ > 1)
mju::sprintf_arr(msg, "unique element '%s' found %d times",
subschema.name_.c_str(), subschema.refcnt_);
break;
case '?':
if (subschema.refcnt_ > 1)
mju::sprintf_arr(msg, "unique element '%s' found %d times",
subschema.name_.c_str(), subschema.refcnt_);
break;
default:
break;
default:
break;
}
}
@@ -539,7 +539,7 @@ template bool mjXUtil::SameVector(const unsigned char* vec1, const unsigned char
// find string in map, return corresponding integer (-1: not found)
int mjXUtil::FindKey(const mjMap* map, int mapsz, std::string key) {
for (int i=0; i<mapsz; i++) {
for (int i=0; i < mapsz; i++) {
if (map[i].key == key) {
return map[i].value;
}
@@ -552,7 +552,7 @@ int mjXUtil::FindKey(const mjMap* map, int mapsz, std::string key) {
// find integer in map, return corresponding string ("": not found)
std::string mjXUtil::FindValue(const mjMap* map, int mapsz, int value) {
for (int i=0; i<mapsz; i++) {
for (int i=0; i < mapsz; i++) {
if (map[i].value == value) {
return map[i].key;
}
@@ -565,8 +565,8 @@ std::string mjXUtil::FindValue(const mjMap* map, int mapsz, int value) {
// if attribute is present, return vector of numerical data
template<typename T>
std::optional<std::vector<T>> mjXUtil::ReadAttrVec(XMLElement* elem, const char* attr,
bool required) {
std::optional<std::vector<T> > mjXUtil::ReadAttrVec(XMLElement* elem, const char* attr,
bool required) {
std::vector<T> v;
const char* raw_cstr = elem->Attribute(attr);
if (raw_cstr) {
@@ -593,13 +593,13 @@ std::optional<std::vector<T>> mjXUtil::ReadAttrVec(XMLElement* elem, const char*
return v;
}
template std::optional<std::vector<double>>
template std::optional<std::vector<double> >
mjXUtil::ReadAttrVec(XMLElement* elem, const char* attr, bool required);
template std::optional<std::vector<float>>
template std::optional<std::vector<float> >
mjXUtil::ReadAttrVec(XMLElement* elem, const char* attr, bool required);
template std::optional<std::vector<int>>
template std::optional<std::vector<int> >
mjXUtil::ReadAttrVec(XMLElement* elem, const char* attr, bool required);
template std::optional<std::vector<unsigned char>>
template std::optional<std::vector<unsigned char> >
mjXUtil::ReadAttrVec(XMLElement* elem, const char* attr, bool required);
@@ -751,10 +751,10 @@ bool mjXUtil::ReadAttrInt(XMLElement* elem, const char* attr, int* data, bool re
void mjXUtil::Vector2String(std::string& txt, const std::vector<float>& vec, int ncol) {
std::stringstream strm;
for (size_t i=0; i<vec.size(); i++) {
for (size_t i=0; i < vec.size(); i++) {
if (ncol && (i % ncol) == 0) {
strm << "\n ";
} else if (i>0) {
} else if (i > 0) {
strm << " ";
}
strm << vec[i];
@@ -839,7 +839,7 @@ void mjXUtil::WriteAttr(XMLElement* elem, std::string name, int n, const T* data
bool trim) {
// make sure all are defined
if constexpr (std::is_floating_point_v<T>) {
for (int i=0; i<n; i++) {
for (int i=0; i < n; i++) {
if (std::isnan(data[i])) {
return;
}
@@ -863,9 +863,9 @@ void mjXUtil::WriteAttr(XMLElement* elem, std::string name, int n, const T* data
stream.precision(mujoco::_mjPRIVATE__get_xml_precision());
// process all numbers
for (int i=0; i<n; i++) {
for (int i=0; i < n; i++) {
// add space between numbers
if (i>0) {
if (i > 0) {
stream << " ";
}
@@ -901,7 +901,7 @@ template void mjXUtil::WriteAttr(XMLElement* elem, std::string name, int n,
void mjXUtil::WriteVector(XMLElement* elem, std::string name, const std::vector<double>& vec) {
// proceed only if non-zero found
bool ok = false;
for (size_t i=0; i<vec.size(); i++) {
for (size_t i=0; i < vec.size(); i++) {
if (vec[i]) {
ok = true;
break;
@@ -921,8 +921,8 @@ void mjXUtil::WriteVector(XMLElement* elem, std::string name, const std::vector<
const std::vector<double>& def) {
// proceed only if non-zero found
bool ok = false;
for (size_t i=0; i<vec.size(); i++) {
if (vec[i]!=def[i]) {
for (size_t i=0; i < vec.size(); i++) {
if (vec[i] != def[i]) {
ok = true;
break;
}
@@ -952,7 +952,7 @@ void mjXUtil::WriteAttrTxt(XMLElement* elem, std::string name, std::string value
// write attribute- single int
void mjXUtil::WriteAttrInt(XMLElement* elem, std::string name, int data, int def) {
// skip default
if (data==def) {
if (data == def) {
return;
}
@@ -965,7 +965,7 @@ void mjXUtil::WriteAttrInt(XMLElement* elem, std::string name, int data, int def
void mjXUtil::WriteAttrKey(XMLElement* elem, std::string name,
const mjMap* map, int mapsz, int data, int def) {
// skip default
if (data==def) {
if (data == def) {
return;
}