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
+5 -3
View File
@@ -98,9 +98,11 @@ mjModel* mj_loadXML(const char* filename, const mjVFS* vfs,
char* error, int error_sz) { char* error, int error_sz) {
// parse new model // parse new model
std::unique_ptr<mjSpec, std::function<void(mjSpec*)>> spec( std::unique_ptr<mjSpec, std::function<void(mjSpec*)> > spec(
ParseXML(filename, vfs, error, error_sz), ParseXML(filename, vfs, error, error_sz),
[](mjSpec* s) { mj_deleteSpec(s); }); [](mjSpec* s) {
mj_deleteSpec(s);
});
if (!spec) { if (!spec) {
return nullptr; return nullptr;
} }
@@ -166,7 +168,7 @@ int mj_printSchema(const char* filename, char* buffer, int buffer_sz, int flg_ht
// print to stringstream // print to stringstream
mjXReader reader; mjXReader reader;
std::stringstream str; 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 // filename given: write to file
if (filename) { if (filename) {
File diff suppressed because it is too large Load Diff
+64 -58
View File
@@ -56,7 +56,7 @@ class mj_XMLPrinter : public tinyxml2::XMLPrinter {
public: public:
void PrintSpace( int depth ) { void PrintSpace( int depth ) {
for (int i=0; i<depth; ++i) { for (int i=0; i < depth; ++i) {
Write( " " ); Write( " " );
} }
} }
@@ -290,7 +290,7 @@ void mjXWriter::OneSkin(XMLElement* elem, const mjCSkin* skin) {
WriteAttrTxt(elem, "face", text); WriteAttrTxt(elem, "face", text);
// bones // bones
for (size_t i=0; i<skin->get_bodyname().size(); i++) { for (size_t i=0; i < skin->get_bodyname().size(); i++) {
// make bone // make bone
XMLElement* bone = InsertEnd(elem, "bone"); XMLElement* bone = InsertEnd(elem, "bone");
@@ -325,7 +325,7 @@ void mjXWriter::OneMaterial(XMLElement* elem, const mjCMaterial* material, mjCDe
// defaults and regular // defaults and regular
// check if we have non-rgb textures // check if we have non-rgb textures
bool has_non_rgb = false; bool has_non_rgb = false;
for (int i=1; i<mjNTEXROLE; i++) { for (int i=1; i < mjNTEXROLE; i++) {
if (!material->textures_[i].empty()) { if (!material->textures_[i].empty()) {
if (i != mjTEXROLE_RGB) { if (i != mjTEXROLE_RGB) {
has_non_rgb = true; has_non_rgb = true;
@@ -335,7 +335,7 @@ void mjXWriter::OneMaterial(XMLElement* elem, const mjCMaterial* material, mjCDe
// if we have non-rgb textures, write them as layers // if we have non-rgb textures, write them as layers
if (has_non_rgb) { if (has_non_rgb) {
for (int i=1; i<mjNTEXROLE; i++) { for (int i=1; i < mjNTEXROLE; i++) {
if (!material->textures_[i].empty()) { if (!material->textures_[i].empty()) {
XMLElement * child_elem = InsertEnd(elem, "layer"); XMLElement * child_elem = InsertEnd(elem, "layer");
WriteAttrTxt(child_elem, "texture", material->textures_[i]); WriteAttrTxt(child_elem, "texture", material->textures_[i]);
@@ -432,7 +432,7 @@ void mjXWriter::OneGeom(XMLElement* elem, const mjCGeom* geom, mjCDef* def, stri
} }
// mesh geom // mesh geom
if (geom->type==mjGEOM_MESH || geom->type==mjGEOM_SDF) { if (geom->type == mjGEOM_MESH || geom->type == mjGEOM_SDF) {
mjCMesh* mesh = geom->mesh; mjCMesh* mesh = geom->mesh;
// write pos/quat if there is a difference // write pos/quat if there is a difference
@@ -474,7 +474,8 @@ void mjXWriter::OneGeom(XMLElement* elem, const mjCGeom* geom, mjCDef* def, stri
WriteAttr(elem, "margin", 1, &geom->margin, &def->Geom().margin); WriteAttr(elem, "margin", 1, &geom->margin, &def->Geom().margin);
WriteAttr(elem, "gap", 1, &geom->gap, &def->Geom().gap); WriteAttr(elem, "gap", 1, &geom->gap, &def->Geom().gap);
WriteAttr(elem, "gap", 1, &geom->gap, &def->Geom().gap); WriteAttr(elem, "gap", 1, &geom->gap, &def->Geom().gap);
WriteAttrKey(elem, "fluidshape", fluid_map, 2, geom->fluid_ellipsoid, def->Geom().fluid_ellipsoid); WriteAttrKey(elem, "fluidshape",
fluid_map, 2, geom->fluid_ellipsoid, def->Geom().fluid_ellipsoid);
WriteAttr(elem, "fluidcoef", 5, geom->fluid_coefs, def->Geom().fluid_coefs); WriteAttr(elem, "fluidcoef", 5, geom->fluid_coefs, def->Geom().fluid_coefs);
if (geom->type != mjGEOM_MESH) { if (geom->type != mjGEOM_MESH) {
WriteAttrKey(elem, "shellinertia", meshtype_map, 2, geom->typeinertia, WriteAttrKey(elem, "shellinertia", meshtype_map, 2, geom->typeinertia,
@@ -491,10 +492,10 @@ void mjXWriter::OneGeom(XMLElement* elem, const mjCGeom* geom, mjCDef* def, stri
WriteAttr(elem, "rgba", 4, geom->rgba, def->Geom().rgba); WriteAttr(elem, "rgba", 4, geom->rgba, def->Geom().rgba);
// hfield and mesh attributes // hfield and mesh attributes
if (geom->type==mjGEOM_HFIELD) { if (geom->type == mjGEOM_HFIELD) {
WriteAttrTxt(elem, "hfield", geom->get_hfieldname()); WriteAttrTxt(elem, "hfield", geom->get_hfieldname());
} }
if (geom->type==mjGEOM_MESH || geom->type==mjGEOM_SDF) { if (geom->type == mjGEOM_MESH || geom->type == mjGEOM_SDF) {
WriteAttrTxt(elem, "mesh", geom->get_meshname()); WriteAttrTxt(elem, "mesh", geom->get_meshname());
} }
@@ -569,7 +570,7 @@ void mjXWriter::OneCamera(XMLElement* elem, const mjCCamera* camera, mjCDef* def
WriteAttrKey(elem, "orthographic", bool_map, 2, camera->orthographic, def->Camera().orthographic); WriteAttrKey(elem, "orthographic", bool_map, 2, camera->orthographic, def->Camera().orthographic);
// camera intrinsics if specified // camera intrinsics if specified
if (camera->sensor_size[0]>0 && camera->sensor_size[1]>0) { if (camera->sensor_size[0] > 0 && camera->sensor_size[1] > 0) {
WriteAttr(elem, "sensorsize", 2, camera->sensor_size); WriteAttr(elem, "sensorsize", 2, camera->sensor_size);
WriteAttr(elem, "focal", 2, camera->focal_length, def->Camera().focal_length); WriteAttr(elem, "focal", 2, camera->focal_length, def->Camera().focal_length);
WriteAttr(elem, "focalpixel", 2, camera->focal_pixel, def->Camera().focal_pixel); WriteAttr(elem, "focalpixel", 2, camera->focal_pixel, def->Camera().focal_pixel);
@@ -705,7 +706,7 @@ void mjXWriter::OneEquality(XMLElement* elem, const mjCEquality* equality, mjCDe
// write tendon // write tendon
void mjXWriter::OneTendon(XMLElement* elem, const mjCTendon* tendon, mjCDef* def) { void mjXWriter::OneTendon(XMLElement* elem, const mjCTendon* tendon, mjCDef* def) {
bool fixed = (tendon->GetWrap(0) && tendon->GetWrap(0)->type==mjWRAP_JOINT); bool fixed = (tendon->GetWrap(0) && tendon->GetWrap(0)->type == mjWRAP_JOINT);
// regular // regular
if (!writingdefaults) { if (!writingdefaults) {
@@ -737,7 +738,7 @@ void mjXWriter::OneTendon(XMLElement* elem, const mjCTendon* tendon, mjCDef* def
} }
// spatial only // spatial only
if (!fixed) { if (!fixed) {
if (tendon->get_material()!=def->Tendon().get_material()) { if (tendon->get_material() != def->Tendon().get_material()) {
WriteAttrTxt(elem, "material", tendon->get_material()); WriteAttrTxt(elem, "material", tendon->get_material());
} }
WriteAttr(elem, "width", 1, &tendon->width, &def->Tendon().width); WriteAttr(elem, "width", 1, &tendon->width, &def->Tendon().width);
@@ -1122,7 +1123,8 @@ void mjXWriter::Visual(XMLElement* root) {
// global // global
elem = InsertEnd(section, "global"); elem = InsertEnd(section, "global");
WriteAttrKey(elem, "orthographic", bool_map, 2, vis->global.orthographic, visdef.global.orthographic); WriteAttrKey(elem, "orthographic",
bool_map, 2, vis->global.orthographic, visdef.global.orthographic);
WriteAttr(elem, "fovy", 1, &vis->global.fovy, &visdef.global.fovy); WriteAttr(elem, "fovy", 1, &vis->global.fovy, &visdef.global.fovy);
WriteAttr(elem, "ipd", 1, &vis->global.ipd, &visdef.global.ipd); WriteAttr(elem, "ipd", 1, &vis->global.ipd, &visdef.global.ipd);
WriteAttr(elem, "azimuth", 1, &vis->global.azimuth, &visdef.global.azimuth); WriteAttr(elem, "azimuth", 1, &vis->global.azimuth, &visdef.global.azimuth);
@@ -1132,7 +1134,8 @@ void mjXWriter::Visual(XMLElement* root) {
WriteAttr(elem, "realtime", 1, &vis->global.realtime, &visdef.global.realtime); WriteAttr(elem, "realtime", 1, &vis->global.realtime, &visdef.global.realtime);
WriteAttrInt(elem, "offwidth", vis->global.offwidth, visdef.global.offwidth); WriteAttrInt(elem, "offwidth", vis->global.offwidth, visdef.global.offwidth);
WriteAttrInt(elem, "offheight", vis->global.offheight, visdef.global.offheight); WriteAttrInt(elem, "offheight", vis->global.offheight, visdef.global.offheight);
WriteAttrKey(elem, "ellipsoidinertia", bool_map, 2, vis->global.ellipsoidinertia, visdef.global.ellipsoidinertia); WriteAttrKey(elem, "ellipsoidinertia",
bool_map, 2, vis->global.ellipsoidinertia, visdef.global.ellipsoidinertia);
WriteAttrKey(elem, "bvactive", bool_map, 2, vis->global.bvactive, visdef.global.bvactive); WriteAttrKey(elem, "bvactive", bool_map, 2, vis->global.bvactive, visdef.global.bvactive);
if (!elem->FirstAttribute()) { if (!elem->FirstAttribute()) {
section->DeleteChild(elem); section->DeleteChild(elem);
@@ -1322,7 +1325,7 @@ void mjXWriter::Default(XMLElement* root, mjCDef* def) {
} }
// add children recursively // add children recursively
for (int i=0; i<(int)def->child.size(); i++) { for (int i=0; i < (int)def->child.size(); i++) {
Default(section, def->child[i]); Default(section, def->child[i]);
} }
@@ -1407,7 +1410,7 @@ void mjXWriter::Custom(XMLElement* root) {
int ntup = model->NumObjects(mjOBJ_TUPLE); int ntup = model->NumObjects(mjOBJ_TUPLE);
// skip section if empty // skip section if empty
if (nnum==0 && ntxt==0 && ntup==0) { if (nnum == 0 && ntxt == 0 && ntup == 0) {
return; return;
} }
@@ -1415,7 +1418,7 @@ void mjXWriter::Custom(XMLElement* root) {
XMLElement* section = InsertEnd(root, "custom"); XMLElement* section = InsertEnd(root, "custom");
// write all numerics // write all numerics
for (int i=0; i<nnum; i++) { for (int i=0; i < nnum; i++) {
mjCNumeric* numeric = (mjCNumeric*)model->GetObject(mjOBJ_NUMERIC, i); mjCNumeric* numeric = (mjCNumeric*)model->GetObject(mjOBJ_NUMERIC, i);
elem = InsertEnd(section, "numeric"); elem = InsertEnd(section, "numeric");
WriteAttrTxt(elem, "name", numeric->name); WriteAttrTxt(elem, "name", numeric->name);
@@ -1424,7 +1427,7 @@ void mjXWriter::Custom(XMLElement* root) {
} }
// write all texts // write all texts
for (int i=0; i<ntxt; i++) { for (int i=0; i < ntxt; i++) {
mjCText* text = (mjCText*)model->GetObject(mjOBJ_TEXT, i); mjCText* text = (mjCText*)model->GetObject(mjOBJ_TEXT, i);
elem = InsertEnd(section, "text"); elem = InsertEnd(section, "text");
WriteAttrTxt(elem, "name", text->name); WriteAttrTxt(elem, "name", text->name);
@@ -1432,18 +1435,18 @@ void mjXWriter::Custom(XMLElement* root) {
} }
// write all tuples // write all tuples
for (int i=0; i<ntup; i++) { for (int i=0; i < ntup; i++) {
mjCTuple* tuple = (mjCTuple*)model->GetObject(mjOBJ_TUPLE, i); mjCTuple* tuple = (mjCTuple*)model->GetObject(mjOBJ_TUPLE, i);
elem = InsertEnd(section, "tuple"); elem = InsertEnd(section, "tuple");
WriteAttrTxt(elem, "name", tuple->name); WriteAttrTxt(elem, "name", tuple->name);
// write objects in tuple // write objects in tuple
for (int j=0; j<(int)tuple->objtype_.size(); j++) { for (int j=0; j < (int)tuple->objtype_.size(); j++) {
XMLElement* obj = InsertEnd(elem, "element"); XMLElement* obj = InsertEnd(elem, "element");
WriteAttrTxt(obj, "objtype", mju_type2Str((int)tuple->objtype_[j])); WriteAttrTxt(obj, "objtype", mju_type2Str((int)tuple->objtype_[j]));
WriteAttrTxt(obj, "objname", tuple->objname_[j].c_str()); WriteAttrTxt(obj, "objname", tuple->objname_[j].c_str());
double oprm = tuple->objprm_[j]; double oprm = tuple->objprm_[j];
if (oprm!=0) { if (oprm != 0) {
WriteAttr(obj, "prm", 1, &oprm); WriteAttr(obj, "prm", 1, &oprm);
} }
} }
@@ -1463,7 +1466,7 @@ void mjXWriter::Asset(XMLElement* root) {
int nhfield = model->NumObjects(mjOBJ_HFIELD); int nhfield = model->NumObjects(mjOBJ_HFIELD);
// return if empty // return if empty
if (ntex==0 && nmat==0 && nmesh==0 && nhfield==0) { if (ntex == 0 && nmat == 0 && nmesh == 0 && nhfield == 0) {
return; return;
} }
@@ -1472,7 +1475,7 @@ void mjXWriter::Asset(XMLElement* root) {
// write textures // write textures
mjCTexture deftex(0); mjCTexture deftex(0);
for (int i=0; i<ntex; i++) { for (int i=0; i < ntex; i++) {
// create element // create element
mjCTexture* texture = (mjCTexture*)model->GetObject(mjOBJ_TEXTURE, i); mjCTexture* texture = (mjCTexture*)model->GetObject(mjOBJ_TEXTURE, i);
elem = InsertEnd(section, "texture"); elem = InsertEnd(section, "texture");
@@ -1482,7 +1485,7 @@ void mjXWriter::Asset(XMLElement* root) {
WriteAttrTxt(elem, "name", texture->name); WriteAttrTxt(elem, "name", texture->name);
// write builtin // write builtin
if (texture->builtin!=mjBUILTIN_NONE) { if (texture->builtin != mjBUILTIN_NONE) {
WriteAttrKey(elem, "builtin", builtin_map, builtin_sz, texture->builtin); WriteAttrKey(elem, "builtin", builtin_map, builtin_sz, texture->builtin);
WriteAttrKey(elem, "mark", mark_map, mark_sz, texture->mark, deftex.mark); WriteAttrKey(elem, "mark", mark_map, mark_sz, texture->mark, deftex.mark);
WriteAttr(elem, "rgb1", 3, texture->rgb1, deftex.rgb1); WriteAttr(elem, "rgb1", 3, texture->rgb1, deftex.rgb1);
@@ -1531,7 +1534,7 @@ void mjXWriter::Asset(XMLElement* root) {
} }
// write materials // write materials
for (int i=0; i<nmat; i++) { for (int i=0; i < nmat; i++) {
// create element and write // create element and write
mjCMaterial* material = (mjCMaterial*)model->GetObject(mjOBJ_MATERIAL, i); mjCMaterial* material = (mjCMaterial*)model->GetObject(mjOBJ_MATERIAL, i);
elem = InsertEnd(section, "material"); elem = InsertEnd(section, "material");
@@ -1539,7 +1542,7 @@ void mjXWriter::Asset(XMLElement* root) {
} }
// write meshes // write meshes
for (int i=0; i<nmesh; i++) { for (int i=0; i < nmesh; i++) {
// create element and write // create element and write
mjCMesh* mesh = (mjCMesh*)model->GetObject(mjOBJ_MESH, i); mjCMesh* mesh = (mjCMesh*)model->GetObject(mjOBJ_MESH, i);
if (mesh->Plugin().active) { if (mesh->Plugin().active) {
@@ -1553,7 +1556,7 @@ void mjXWriter::Asset(XMLElement* root) {
} }
// write hfields // write hfields
for (int i=0; i<nhfield; i++) { for (int i=0; i < nhfield; i++) {
// create element // create element
mjCHField* hfield = (mjCHField*)model->GetObject(mjOBJ_HFIELD, i); mjCHField* hfield = (mjCHField*)model->GetObject(mjOBJ_HFIELD, i);
elem = InsertEnd(section, "hfield"); elem = InsertEnd(section, "hfield");
@@ -1606,7 +1609,7 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body, mjCFrame* frame, string_vi
} }
// write body attributes and inertial // write body attributes and inertial
else if (!frame && body!=model->GetWorld()) { else if (!frame && body != model->GetWorld()) {
WriteAttrTxt(elem, "name", body->name); WriteAttrTxt(elem, "name", body->name);
if (childclass != body->classname && body->classname != "main") { if (childclass != body->classname && body->classname != "main") {
WriteAttrTxt(elem, "childclass", body->classname); WriteAttrTxt(elem, "childclass", body->classname);
@@ -1629,7 +1632,7 @@ void mjXWriter::Body(XMLElement* elem, mjCBody* body, mjCFrame* frame, string_vi
WriteVector(elem, "user", body->get_userdata()); WriteVector(elem, "user", body->get_userdata());
// write inertial // write inertial
if (body->explicitinertial && model->compiler.inertiafromgeom!=mjINERTIAFROMGEOM_TRUE) { if (body->explicitinertial && model->compiler.inertiafromgeom != mjINERTIAFROMGEOM_TRUE) {
XMLElement* inertial = InsertEnd(elem, "inertial"); XMLElement* inertial = InsertEnd(elem, "inertial");
WriteAttr(inertial, "pos", 3, body->ipos); WriteAttr(inertial, "pos", 3, body->ipos);
WriteAttr(inertial, "quat", 4, body->iquat, unitq); WriteAttr(inertial, "quat", 4, body->iquat, unitq);
@@ -1760,7 +1763,7 @@ void mjXWriter::Contact(XMLElement* root) {
int nexclude = model->NumObjects(mjOBJ_EXCLUDE); int nexclude = model->NumObjects(mjOBJ_EXCLUDE);
// skip if section is empty // skip if section is empty
if (npair==0 && nexclude==0) { if (npair == 0 && nexclude == 0) {
return; return;
} }
@@ -1768,7 +1771,7 @@ void mjXWriter::Contact(XMLElement* root) {
XMLElement* section = InsertEnd(root, "contact"); XMLElement* section = InsertEnd(root, "contact");
// write all geom pairs // write all geom pairs
for (int i=0; i<npair; i++) { for (int i=0; i < npair; i++) {
// create element and write // create element and write
mjCPair* pair = (mjCPair*)model->GetObject(mjOBJ_PAIR, i); mjCPair* pair = (mjCPair*)model->GetObject(mjOBJ_PAIR, i);
elem = InsertEnd(section, "pair"); elem = InsertEnd(section, "pair");
@@ -1776,7 +1779,7 @@ void mjXWriter::Contact(XMLElement* root) {
} }
// write all exclude pairs // write all exclude pairs
for (int i=0; i<nexclude; i++) { for (int i=0; i < nexclude; i++) {
// create element // create element
mjCBodyPair* exclude = (mjCBodyPair*)model->GetObject(mjOBJ_EXCLUDE, i); mjCBodyPair* exclude = (mjCBodyPair*)model->GetObject(mjOBJ_EXCLUDE, i);
elem = InsertEnd(section, "exclude"); elem = InsertEnd(section, "exclude");
@@ -1794,7 +1797,7 @@ void mjXWriter::Contact(XMLElement* root) {
void mjXWriter::Equality(XMLElement* root) { void mjXWriter::Equality(XMLElement* root) {
// skip section if empty // skip section if empty
int num; int num;
if ((num=model->NumObjects(mjOBJ_EQUALITY))==0) { if ((num=model->NumObjects(mjOBJ_EQUALITY)) == 0) {
return; return;
} }
@@ -1802,9 +1805,10 @@ void mjXWriter::Equality(XMLElement* root) {
XMLElement* section = InsertEnd(root, "equality"); XMLElement* section = InsertEnd(root, "equality");
// write all constraints // write all constraints
for (int i=0; i<num; i++) { for (int i=0; i < num; i++) {
mjCEquality* equality = (mjCEquality*)model->GetObject(mjOBJ_EQUALITY, i); mjCEquality* equality = (mjCEquality*)model->GetObject(mjOBJ_EQUALITY, i);
XMLElement* elem = InsertEnd(section, FindValue(equality_map, equality_sz, equality->type).c_str()); XMLElement* elem = InsertEnd(section,
FindValue(equality_map, equality_sz, equality->type).c_str());
OneEquality(elem, equality, model->def_map[equality->classname]); OneEquality(elem, equality, model->def_map[equality->classname]);
} }
} }
@@ -1820,7 +1824,7 @@ void mjXWriter::Deformable(XMLElement* root) {
int nskin = model->NumObjects(mjOBJ_SKIN); int nskin = model->NumObjects(mjOBJ_SKIN);
// return if empty // return if empty
if (nflex==0 && nskin==0) { if (nflex == 0 && nskin == 0) {
return; return;
} }
@@ -1828,7 +1832,7 @@ void mjXWriter::Deformable(XMLElement* root) {
XMLElement* section = InsertEnd(root, "deformable"); XMLElement* section = InsertEnd(root, "deformable");
// write flexes // write flexes
for (int i=0; i<nflex; i++) { for (int i=0; i < nflex; i++) {
// create element and write // create element and write
mjCFlex* flex = (mjCFlex*)model->GetObject(mjOBJ_FLEX, i); mjCFlex* flex = (mjCFlex*)model->GetObject(mjOBJ_FLEX, i);
elem = InsertEnd(section, "flex"); elem = InsertEnd(section, "flex");
@@ -1836,7 +1840,7 @@ void mjXWriter::Deformable(XMLElement* root) {
} }
// write skins // write skins
for (int i=0; i<nskin; i++) { for (int i=0; i < nskin; i++) {
// create element and write // create element and write
mjCSkin* skin = (mjCSkin*)model->GetObject(mjOBJ_SKIN, i); mjCSkin* skin = (mjCSkin*)model->GetObject(mjOBJ_SKIN, i);
elem = InsertEnd(section, "skin"); elem = InsertEnd(section, "skin");
@@ -1850,7 +1854,7 @@ void mjXWriter::Deformable(XMLElement* root) {
void mjXWriter::Tendon(XMLElement* root) { void mjXWriter::Tendon(XMLElement* root) {
// skip section if empty // skip section if empty
int num; int num;
if ((num=model->NumObjects(mjOBJ_TENDON))==0) { if ((num=model->NumObjects(mjOBJ_TENDON)) == 0) {
return; return;
} }
@@ -1858,19 +1862,19 @@ void mjXWriter::Tendon(XMLElement* root) {
XMLElement* section = InsertEnd(root, "tendon"); XMLElement* section = InsertEnd(root, "tendon");
// write all tendons // write all tendons
for (int i=0; i<num; i++) { for (int i=0; i < num; i++) {
// write tendon element and attributes // write tendon element and attributes
mjCTendon* tendon = (mjCTendon*)model->GetObject(mjOBJ_TENDON, i); mjCTendon* tendon = (mjCTendon*)model->GetObject(mjOBJ_TENDON, i);
if (!tendon->NumWraps()) { // SHOULD NOT OCCUR if (!tendon->NumWraps()) { // SHOULD NOT OCCUR
continue; continue;
} }
XMLElement* elem = InsertEnd(section, XMLElement* elem = InsertEnd(section,
tendon->GetWrap(0)->type==mjWRAP_JOINT ? "fixed" : "spatial"); tendon->GetWrap(0)->type == mjWRAP_JOINT ? "fixed" : "spatial");
OneTendon(elem, tendon, model->def_map[tendon->classname]); OneTendon(elem, tendon, model->def_map[tendon->classname]);
// write wraps // write wraps
XMLElement* wrapelem; XMLElement* wrapelem;
for (int j=0; j<tendon->NumWraps(); j++) { for (int j=0; j < tendon->NumWraps(); j++) {
const mjCWrap* wrap = tendon->GetWrap(j); const mjCWrap* wrap = tendon->GetWrap(j);
switch (wrap->type) { switch (wrap->type) {
case mjWRAP_JOINT: case mjWRAP_JOINT:
@@ -1911,7 +1915,7 @@ void mjXWriter::Tendon(XMLElement* root) {
void mjXWriter::Actuator(XMLElement* root) { void mjXWriter::Actuator(XMLElement* root) {
// skip section if empty // skip section if empty
int num; int num;
if ((num=model->NumObjects(mjOBJ_ACTUATOR))==0) { if ((num=model->NumObjects(mjOBJ_ACTUATOR)) == 0) {
return; return;
} }
@@ -1919,7 +1923,7 @@ void mjXWriter::Actuator(XMLElement* root) {
XMLElement* section = InsertEnd(root, "actuator"); XMLElement* section = InsertEnd(root, "actuator");
// write all actuators // write all actuators
for (int i=0; i<num; i++) { for (int i=0; i < num; i++) {
mjCActuator* actuator = (mjCActuator*)model->GetObject(mjOBJ_ACTUATOR, i); mjCActuator* actuator = (mjCActuator*)model->GetObject(mjOBJ_ACTUATOR, i);
XMLElement* elem; XMLElement* elem;
if (actuator->plugin.active) { if (actuator->plugin.active) {
@@ -1939,7 +1943,7 @@ void mjXWriter::Sensor(XMLElement* root) {
// skip section if empty // skip section if empty
int num; int num;
if ((num=model->NumObjects(mjOBJ_SENSOR))==0) { if ((num=model->NumObjects(mjOBJ_SENSOR)) == 0) {
return; return;
} }
@@ -1947,7 +1951,7 @@ void mjXWriter::Sensor(XMLElement* root) {
XMLElement* section = InsertEnd(root, "sensor"); XMLElement* section = InsertEnd(root, "sensor");
// write all sensors // write all sensors
for (int i=0; i<num; i++) { for (int i=0; i < num; i++) {
XMLElement* elem = 0; XMLElement* elem = 0;
mjCSensor* sensor = model->Sensors()[i]; mjCSensor* sensor = model->Sensors()[i];
string instance_name = ""; string instance_name = "";
@@ -2201,7 +2205,9 @@ void mjXWriter::Sensor(XMLElement* root) {
// user-defined sensor // user-defined sensor
case mjSENS_USER: case mjSENS_USER:
elem = InsertEnd(section, "user"); elem = InsertEnd(section, "user");
if (mju_type2Str(sensor->objtype)) WriteAttrTxt(elem, "objtype", mju_type2Str(sensor->objtype)); if (mju_type2Str(sensor->objtype)) {
WriteAttrTxt(elem, "objtype", mju_type2Str(sensor->objtype));
}
WriteAttrTxt(elem, "objname", sensor->get_objname()); WriteAttrTxt(elem, "objname", sensor->get_objname());
WriteAttrInt(elem, "dim", sensor->dim); WriteAttrInt(elem, "dim", sensor->dim);
WriteAttrKey(elem, "needstage", stage_map, stage_sz, (int)sensor->needstage); WriteAttrKey(elem, "needstage", stage_map, stage_sz, (int)sensor->needstage);
@@ -2239,7 +2245,7 @@ void mjXWriter::Keyframe(XMLElement* root) {
} }
// write all keyframes // write all keyframes
for (int i=0; i<model->nkey; i++) { for (int i=0; i < model->nkey; i++) {
XMLElement* elem = InsertEnd(section, "key"); XMLElement* elem = InsertEnd(section, "key");
bool change = false; bool change = false;
@@ -2252,14 +2258,14 @@ void mjXWriter::Keyframe(XMLElement* root) {
} }
// check time and write // check time and write
if (key->time!=0) { if (key->time != 0) {
WriteAttr(elem, "time", 1, &key->time); WriteAttr(elem, "time", 1, &key->time);
change = true; change = true;
} }
// check qpos and write // check qpos and write
for (int j=0; j<model->nq; j++) { for (int j=0; j < model->nq; j++) {
if (key->qpos_[j]!=model->qpos0[j]) { if (key->qpos_[j] != model->qpos0[j]) {
WriteAttr(elem, "qpos", model->nq, key->qpos_.data()); WriteAttr(elem, "qpos", model->nq, key->qpos_.data());
change = true; change = true;
break; break;
@@ -2267,8 +2273,8 @@ void mjXWriter::Keyframe(XMLElement* root) {
} }
// check qvel and write // check qvel and write
for (int j=0; j<model->nv; j++) { for (int j=0; j < model->nv; j++) {
if (key->qvel_[j]!=0) { if (key->qvel_[j] != 0) {
WriteAttr(elem, "qvel", model->nv, key->qvel_.data()); WriteAttr(elem, "qvel", model->nv, key->qvel_.data());
change = true; change = true;
break; break;
@@ -2276,8 +2282,8 @@ void mjXWriter::Keyframe(XMLElement* root) {
} }
// check act and write // check act and write
for (int j=0; j<model->na; j++) { for (int j=0; j < model->na; j++) {
if (key->act_[j]!=0) { if (key->act_[j] != 0) {
WriteAttr(elem, "act", model->na, key->act_.data()); WriteAttr(elem, "act", model->na, key->act_.data());
change = true; change = true;
break; break;
@@ -2286,7 +2292,7 @@ void mjXWriter::Keyframe(XMLElement* root) {
// check mpos and write // check mpos and write
if (model->nmocap) { if (model->nmocap) {
for (int j=0; j<model->nbody; j++) { for (int j=0; j < model->nbody; j++) {
if (model->Bodies()[j]->mocap) { if (model->Bodies()[j]->mocap) {
mjCBody* body = model->Bodies()[j]; mjCBody* body = model->Bodies()[j];
int id = body->mocapid; int id = body->mocapid;
@@ -2303,7 +2309,7 @@ void mjXWriter::Keyframe(XMLElement* root) {
// check mquat and write // check mquat and write
if (model->nmocap) { if (model->nmocap) {
for (int j=0; j<model->nbody; j++) { for (int j=0; j < model->nbody; j++) {
if (model->Bodies()[j]->mocap) { if (model->Bodies()[j]->mocap) {
mjCBody* body = model->Bodies()[j]; mjCBody* body = model->Bodies()[j];
int id = body->mocapid; int id = body->mocapid;
@@ -2320,8 +2326,8 @@ void mjXWriter::Keyframe(XMLElement* root) {
} }
// check ctrl and write // check ctrl and write
for (int j=0; j<model->nu; j++) { for (int j=0; j < model->nu; j++) {
if (key->ctrl_[j]!=0) { if (key->ctrl_[j] != 0) {
WriteAttr(elem, "ctrl", model->nu, key->ctrl_.data()); WriteAttr(elem, "ctrl", model->nu, key->ctrl_.data());
change = true; change = true;
break; break;
+17 -17
View File
@@ -125,7 +125,7 @@ void mjXURDF::Parse(
while (elem) { while (elem) {
// identify link elements // identify link elements
name = elem->Value(); name = elem->Value();
if (name=="link") { if (name == "link") {
ReadAttrTxt(elem, "name", text, true); ReadAttrTxt(elem, "name", text, true);
text = GetPrefixedName(text); text = GetPrefixedName(text);
AddBody(text); AddBody(text);
@@ -140,7 +140,7 @@ void mjXURDF::Parse(
while (elem) { while (elem) {
// identify joint elements // identify joint elements
name = elem->Value(); name = elem->Value();
if (name=="joint") { if (name == "joint") {
// find parent, get name and id // find parent, get name and id
temp = FindSubElem(elem, "parent", true); temp = FindSubElem(elem, "parent", true);
ReadAttrTxt(temp, "link", text, true); ReadAttrTxt(temp, "link", text, true);
@@ -154,12 +154,12 @@ void mjXURDF::Parse(
id_child = FindName(text, urName); id_child = FindName(text, urName);
// make sure parent and child exist // 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"); throw mjXError(elem, "URDF joint parent or child missing");
} }
// check for multiple parents // 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()); 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 // 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) { if (urParent[i] < 0) {
AddToTree(i); AddToTree(i);
} }
@@ -184,7 +184,7 @@ void mjXURDF::Parse(
while (elem) { while (elem) {
// identify body/link elements // identify body/link elements
name = elem->Value(); name = elem->Value();
if (name=="link") { if (name == "link") {
Body(elem); Body(elem);
} }
@@ -197,7 +197,7 @@ void mjXURDF::Parse(
while (elem) { while (elem) {
// identify body/link elements // identify body/link elements
name = elem->Value(); name = elem->Value();
if (name=="joint") { if (name == "joint") {
Joint(elem); Joint(elem);
} }
@@ -297,7 +297,7 @@ void mjXURDF::Body(XMLElement* body_elem) {
name = elem->Value(); name = elem->Value();
// visual element // visual element
if (name=="visual") { if (name == "visual") {
// parse material // parse material
if ((temp = FindSubElem(elem, "material"))) { if ((temp = FindSubElem(elem, "material"))) {
// if color specified - use directly // if color specified - use directly
@@ -310,7 +310,7 @@ void mjXURDF::Body(XMLElement* body_elem) {
ReadAttrTxt(temp, "name", name, true); ReadAttrTxt(temp, "name", name, true);
name = GetPrefixedName(name); name = GetPrefixedName(name);
int imat = FindName(name, urMat); int imat = FindName(name, urMat);
if (imat>=0) { if (imat >= 0) {
std::memcpy(rgba, urRGBA[imat].val, 4*sizeof(float)); std::memcpy(rgba, urRGBA[imat].val, 4*sizeof(float));
} }
} }
@@ -320,7 +320,7 @@ void mjXURDF::Body(XMLElement* body_elem) {
pgeom = Geom(elem, pbody, false); pgeom = Geom(elem, pbody, false);
// save color // save color
if (rgba[0]>=0) { if (rgba[0] >= 0) {
std::memcpy(pgeom->rgba, rgba, 4*sizeof(float)); std::memcpy(pgeom->rgba, rgba, 4*sizeof(float));
} }
@@ -339,11 +339,11 @@ void mjXURDF::Body(XMLElement* body_elem) {
} }
// collision element // collision element
else if (name=="collision") { else if (name == "collision") {
pgeom = Geom(elem, pbody, true); pgeom = Geom(elem, pbody, true);
// use color from last visual // use color from last visual
if (rgba[0]>=0) { if (rgba[0] >= 0) {
std::memcpy(pgeom->rgba, rgba, 4*sizeof(float)); std::memcpy(pgeom->rgba, rgba, 4*sizeof(float));
} }
@@ -546,7 +546,7 @@ mjsGeom* mjXURDF::Geom(XMLElement* geom_elem, mjsBody* pbody, bool collision) {
// box // box
if ((temp = FindSubElem(elem, "box"))) { if ((temp = FindSubElem(elem, "box"))) {
ReadAttr(temp, "size", 3, pgeom->size, text, true, true); 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 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 // find body with given name in list, return -1 if not found
int mjXURDF::FindName(std::string name, std::vector<std::string>& list) { 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) { if (list[i] == name) {
return i; 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 // add name to list, error if name already exists
void mjXURDF::AddName(std::string name, std::vector<std::string>& list) { void mjXURDF::AddName(std::string name, std::vector<std::string>& list) {
// make sure name is unique // make sure name is unique
if (FindName(name, list)>=0) { if (FindName(name, list) >= 0) {
throw mjXError(0, "repeated URDF name: ", name.c_str()); throw mjXError(0, "repeated URDF name: ", name.c_str());
} }
@@ -713,7 +713,7 @@ void mjXURDF::AddBody(std::string name) {
void mjXURDF::AddToTree(int n) { void mjXURDF::AddToTree(int n) {
// get pointer to parent in mjCModel tree // get pointer to parent in mjCModel tree
mjsBody *parent = 0, *child = 0, *world = 0; mjsBody *parent = 0, *child = 0, *world = 0;
if (urParent[n]>=0) { if (urParent[n] >= 0) {
world = mjs_findBody(spec, "world"); world = mjs_findBody(spec, "world");
parent = mjs_findChild(world, urName[urParent[n]].c_str()); parent = mjs_findChild(world, urName[urParent[n]].c_str());
@@ -731,7 +731,7 @@ void mjXURDF::AddToTree(int n) {
} }
// add children recursively // 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]); AddToTree(urChildren[n][i]);
} }
} }
+22 -22
View File
@@ -112,7 +112,7 @@ FilePath ResolveFilePath(XMLElement* e, const FilePath& filename,
// error string copy // error string copy
void mjCopyError(char* dst, const char* src, int maxlen) { void mjCopyError(char* dst, const char* src, int maxlen) {
if (dst && maxlen>0) { if (dst && maxlen > 0) {
strncpy(dst, src, maxlen); strncpy(dst, src, maxlen);
dst[maxlen-1] = 0; dst[maxlen-1] = 0;
} }
@@ -243,7 +243,7 @@ std::string mjXSchema::GetError() {
// print spaces // print spaces
static void printspace(std::stringstream& str, int n, const char* space) { 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; str << space;
} }
} }
@@ -259,14 +259,14 @@ void mjXSchema::Print(std::stringstream& str, int level) const {
printspace(str, 3*level, " "); printspace(str, 3*level, " ");
str << name1 << " (" << type_ << ")"; str << name1 << " (" << type_ << ")";
int baselen = 3*level + (int)name1.size() + 4; int baselen = 3*level + (int)name1.size() + 4;
if (baselen<30) { if (baselen < 30) {
printspace(str, 30-baselen, " "); printspace(str, 30-baselen, " ");
} }
// attributes // attributes
int cnt = std::max(baselen, 30); int cnt = std::max(baselen, 30);
for (const std::string& attr : attr_) { for (const std::string& attr : attr_) {
if (cnt>60) { if (cnt > 60) {
str << "\n"; str << "\n";
printspace(str, (cnt = std::max(30, baselen)), " "); 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_); std::string name1 = (name_ == "body" ? "(world)body" : name_);
// open table // open table
if (level==0) { if (level == 0) {
str << "<table border=\"1\">\n"; str << "<table border=\"1\">\n";
} }
@@ -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) // find string in map, return corresponding integer (-1: not found)
int mjXUtil::FindKey(const mjMap* map, int mapsz, std::string key) { 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) { if (map[i].key == key) {
return map[i].value; 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) // find integer in map, return corresponding string ("": not found)
std::string mjXUtil::FindValue(const mjMap* map, int mapsz, int value) { 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) { if (map[i].value == value) {
return map[i].key; return map[i].key;
} }
@@ -565,7 +565,7 @@ std::string mjXUtil::FindValue(const mjMap* map, int mapsz, int value) {
// if attribute is present, return vector of numerical data // if attribute is present, return vector of numerical data
template<typename T> template<typename T>
std::optional<std::vector<T>> mjXUtil::ReadAttrVec(XMLElement* elem, const char* attr, std::optional<std::vector<T> > mjXUtil::ReadAttrVec(XMLElement* elem, const char* attr,
bool required) { bool required) {
std::vector<T> v; std::vector<T> v;
const char* raw_cstr = elem->Attribute(attr); const char* raw_cstr = elem->Attribute(attr);
@@ -593,13 +593,13 @@ std::optional<std::vector<T>> mjXUtil::ReadAttrVec(XMLElement* elem, const char*
return v; return v;
} }
template std::optional<std::vector<double>> template std::optional<std::vector<double> >
mjXUtil::ReadAttrVec(XMLElement* elem, const char* attr, bool required); 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); 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); 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); 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) { void mjXUtil::Vector2String(std::string& txt, const std::vector<float>& vec, int ncol) {
std::stringstream strm; 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) { if (ncol && (i % ncol) == 0) {
strm << "\n "; strm << "\n ";
} else if (i>0) { } else if (i > 0) {
strm << " "; strm << " ";
} }
strm << vec[i]; strm << vec[i];
@@ -839,7 +839,7 @@ void mjXUtil::WriteAttr(XMLElement* elem, std::string name, int n, const T* data
bool trim) { bool trim) {
// make sure all are defined // make sure all are defined
if constexpr (std::is_floating_point_v<T>) { 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])) { if (std::isnan(data[i])) {
return; 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()); stream.precision(mujoco::_mjPRIVATE__get_xml_precision());
// process all numbers // process all numbers
for (int i=0; i<n; i++) { for (int i=0; i < n; i++) {
// add space between numbers // add space between numbers
if (i>0) { if (i > 0) {
stream << " "; 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) { void mjXUtil::WriteVector(XMLElement* elem, std::string name, const std::vector<double>& vec) {
// proceed only if non-zero found // proceed only if non-zero found
bool ok = false; bool ok = false;
for (size_t i=0; i<vec.size(); i++) { for (size_t i=0; i < vec.size(); i++) {
if (vec[i]) { if (vec[i]) {
ok = true; ok = true;
break; break;
@@ -921,8 +921,8 @@ void mjXUtil::WriteVector(XMLElement* elem, std::string name, const std::vector<
const std::vector<double>& def) { const std::vector<double>& def) {
// proceed only if non-zero found // proceed only if non-zero found
bool ok = false; bool ok = false;
for (size_t i=0; i<vec.size(); i++) { for (size_t i=0; i < vec.size(); i++) {
if (vec[i]!=def[i]) { if (vec[i] != def[i]) {
ok = true; ok = true;
break; break;
} }
@@ -952,7 +952,7 @@ void mjXUtil::WriteAttrTxt(XMLElement* elem, std::string name, std::string value
// write attribute- single int // write attribute- single int
void mjXUtil::WriteAttrInt(XMLElement* elem, std::string name, int data, int def) { void mjXUtil::WriteAttrInt(XMLElement* elem, std::string name, int data, int def) {
// skip default // skip default
if (data==def) { if (data == def) {
return; 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, void mjXUtil::WriteAttrKey(XMLElement* elem, std::string name,
const mjMap* map, int mapsz, int data, int def) { const mjMap* map, int mapsz, int data, int def) {
// skip default // skip default
if (data==def) { if (data == def) {
return; return;
} }