From 62bc837b4cec7bf19d7b70cc27e3224813428971 Mon Sep 17 00:00:00 2001 From: Yuval Tassa Date: Sun, 5 May 2024 12:02:48 -0700 Subject: [PATCH] Rename remaining model to spec in xml/ PiperOrigin-RevId: 630855094 Change-Id: Ib921fe43b9b5a5a64867ef4c8c1cca217c27a5aa --- src/xml/xml.cc | 32 +++++------ src/xml/xml.h | 2 +- src/xml/xml_api.cc | 34 ++++++------ src/xml/xml_native_reader.cc | 100 +++++++++++++++++------------------ src/xml/xml_native_reader.h | 4 +- src/xml/xml_native_writer.cc | 6 +-- src/xml/xml_native_writer.h | 2 +- 7 files changed, 90 insertions(+), 90 deletions(-) diff --git a/src/xml/xml.cc b/src/xml/xml.cc index 064d52f3..b765f01d 100644 --- a/src/xml/xml.cc +++ b/src/xml/xml.cc @@ -100,17 +100,17 @@ class LocaleOverride { } // namespace // Main writer function - calls mjXWrite -std::string mjWriteXML(mjSpec* model, char* error, int error_sz) { +std::string mjWriteXML(mjSpec* spec, char* error, int error_sz) { LocaleOverride locale_override; // check for empty model - if (!model) { + if (!spec) { mjCopyError(error, "Cannot write empty model", error_sz); return ""; } mjXWriter writer; - writer.SetModel(model); + writer.SetModel(spec); return writer.Write(error, error_sz); } @@ -286,7 +286,7 @@ mjSpec* mjParseXML(const char* filename, const mjVFS* vfs, } // clear - mjSpec* model = nullptr; + mjSpec* spec = nullptr; if (error) { error[0] = '\0'; } @@ -345,14 +345,14 @@ mjSpec* mjParseXML(const char* filename, const mjVFS* vfs, } // create model, set filedir - model = mjs_createSpec(); + spec = mjs_createSpec(); const char* dir; int ndir = 0; mju_getResourceDir(resource, &dir, &ndir); if (dir != nullptr) { - mjs_setString(model->modelfiledir, std::string(dir, ndir).c_str()); + mjs_setString(spec->modelfiledir, std::string(dir, ndir).c_str()); } else { - mjs_setString(model->modelfiledir, ""); + mjs_setString(spec->modelfiledir, ""); } // close resource @@ -364,11 +364,11 @@ mjSpec* mjParseXML(const char* filename, const mjVFS* vfs, // find include elements, replace them with subtree from xml file std::unordered_set included = {filename}; mjXReader parser; - parser.SetModelFileDir(mjs_getString(model->modelfiledir)); - mjIncludeXML(parser, root, mjs_getString(model->modelfiledir), vfs, included); + parser.SetModelFileDir(mjs_getString(spec->modelfiledir)); + mjIncludeXML(parser, root, mjs_getString(spec->modelfiledir), vfs, included); // parse MuJoCo model - parser.SetModel(model); + parser.SetModel(spec); parser.Parse(root); } @@ -378,11 +378,11 @@ mjSpec* mjParseXML(const char* filename, const mjVFS* vfs, // set reasonable default for parsing a URDF // this is separate from the Parser to allow multiple URDFs to be loaded. - model->strippath = true; - model->fusestatic = true; - model->discardvisual = true; + spec->strippath = true; + spec->fusestatic = true; + spec->discardvisual = true; - parser.SetModel(model); + parser.SetModel(spec); parser.Parse(root); } @@ -394,11 +394,11 @@ mjSpec* mjParseXML(const char* filename, const mjVFS* vfs, // catch known errors catch (mjXError err) { mjCopyError(error, err.message, error_sz); - mjs_deleteSpec(model); + mjs_deleteSpec(spec); return nullptr; } - return model; + return spec; } diff --git a/src/xml/xml.h b/src/xml/xml.h index 71104a6c..10b5e4a3 100644 --- a/src/xml/xml.h +++ b/src/xml/xml.h @@ -24,7 +24,7 @@ // Top level API // Main writer function -std::string mjWriteXML(mjSpec* model, char* error, int error_sz); +std::string mjWriteXML(mjSpec* spec, char* error, int error_sz); // Main parser function MJAPI mjSpec* mjParseXML(const char* filename, const mjVFS* vfs, char* error, int error_sz); diff --git a/src/xml/xml_api.cc b/src/xml/xml_api.cc index a827ddef..c3cab08d 100644 --- a/src/xml/xml_api.cc +++ b/src/xml/xml_api.cc @@ -37,7 +37,7 @@ class GlobalModel { public: // deletes current model and takes ownership of model - void Set(mjSpec* model = nullptr); + void Set(mjSpec* spec = nullptr); // writes XML to string std::optional ToXML(const mjModel* m, char* error, @@ -46,30 +46,30 @@ class GlobalModel { private: // using raw pointers as GlobalModel needs to be trivially destructible std::mutex* mutex_ = new std::mutex(); - mjSpec* model_ = nullptr; + mjSpec* spec_ = nullptr; }; std::optional GlobalModel::ToXML(const mjModel* m, char* error, int error_sz) { std::lock_guard lock(*mutex_); - if (!model_) { + if (!spec_) { mjCopyError(error, "No XML model loaded", error_sz); return std::nullopt; } - mjs_copyBack(model_, m); - std::string result = mjWriteXML(model_, error, error_sz); + mjs_copyBack(spec_, m); + std::string result = mjWriteXML(spec_, error, error_sz); if (result.empty()) { return std::nullopt; } return result; } -void GlobalModel::Set(mjSpec* model) { +void GlobalModel::Set(mjSpec* spec) { std::lock_guard lock(*mutex_); - if (model_ != nullptr) { - mjs_deleteSpec(model_); + if (spec_ != nullptr) { + mjs_deleteSpec(spec_); } - model_ = model; + spec_ = spec; } @@ -91,29 +91,29 @@ mjModel* mj_loadXML(const char* filename, const mjVFS* vfs, char* error, int error_sz) { // parse new model - std::unique_ptr> model( + std::unique_ptr> spec( mjParseXML(filename, vfs, error, error_sz), - [](mjSpec* m) { mjs_deleteSpec(m); }); - if (!model) { + [](mjSpec* s) { mjs_deleteSpec(s); }); + if (!spec) { return nullptr; } // compile new model - mjModel* m = mjs_compile(model.get(), vfs); + mjModel* m = mjs_compile(spec.get(), vfs); if (!m) { - mjCopyError(error, mjs_getError(model.get()), error_sz); + mjCopyError(error, mjs_getError(spec.get()), error_sz); return nullptr; } // handle compile warning - if (mjs_isWarning(model.get())) { - mjCopyError(error, mjs_getError(model.get()), error_sz); + if (mjs_isWarning(spec.get())) { + mjCopyError(error, mjs_getError(spec.get()), error_sz); } else if (error) { error[0] = '\0'; } // clear old and assign new - GetGlobalModel().Set(model.release()); + GetGlobalModel().Set(spec.release()); return m; } diff --git a/src/xml/xml_native_reader.cc b/src/xml/xml_native_reader.cc index 1a12d3d8..da9f41e5 100644 --- a/src/xml/xml_native_reader.cc +++ b/src/xml/xml_native_reader.cc @@ -924,25 +924,25 @@ void mjXReader::Parse(XMLElement* root) { // compiler section parser -void mjXReader::Compiler(XMLElement* section, mjSpec* mod) { +void mjXReader::Compiler(XMLElement* section, mjSpec* spec) { string text; int n; // top-level attributes if (MapValue(section, "autolimits", &n, bool_map, 2)) { - mod->autolimits = (n==1); + spec->autolimits = (n==1); } - ReadAttr(section, "boundmass", 1, &mod->boundmass, text); - ReadAttr(section, "boundinertia", 1, &mod->boundinertia, text); - ReadAttr(section, "settotalmass", 1, &mod->settotalmass, text); + ReadAttr(section, "boundmass", 1, &spec->boundmass, text); + ReadAttr(section, "boundinertia", 1, &spec->boundinertia, text); + ReadAttr(section, "settotalmass", 1, &spec->settotalmass, text); if (MapValue(section, "balanceinertia", &n, bool_map, 2)) { - mod->balanceinertia = (n==1); + spec->balanceinertia = (n==1); } if (MapValue(section, "strippath", &n, bool_map, 2)) { - mod->strippath = (n==1); + spec->strippath = (n==1); } if (MapValue(section, "fitaabb", &n, bool_map, 2)) { - mod->fitaabb = (n==1); + spec->fitaabb = (n==1); } if (MapValue(section, "coordinate", &n, coordinate_map, 2)) { if (n==1) { @@ -951,48 +951,48 @@ void mjXReader::Compiler(XMLElement* section, mjSpec* mod) { } } if (MapValue(section, "angle", &n, angle_map, 2)) { - mod->degree = (n==1); + spec->degree = (n==1); } if (ReadAttrTxt(section, "eulerseq", text)) { if (text.size()!=3) { throw mjXError(section, "euler format must have length 3"); } - memcpy(mod->euler, text.c_str(), 3); + memcpy(spec->euler, text.c_str(), 3); } if (ReadAttrTxt(section, "assetdir", text)) { - mjs_setString(mod->meshdir, text.c_str()); - mjs_setString(mod->texturedir, text.c_str()); + mjs_setString(spec->meshdir, text.c_str()); + mjs_setString(spec->texturedir, text.c_str()); } // meshdir and texturedir take precedence over assetdir std::string meshdir, texturedir; if (ReadAttrTxt(section, "meshdir", meshdir)) { - mjs_setString(mod->meshdir, meshdir.c_str()); + mjs_setString(spec->meshdir, meshdir.c_str()); }; if (ReadAttrTxt(section, "texturedir", texturedir)) { - mjs_setString(mod->texturedir, texturedir.c_str()); + mjs_setString(spec->texturedir, texturedir.c_str()); } if (MapValue(section, "discardvisual", &n, bool_map, 2)) { - mod->discardvisual = (n==1); + spec->discardvisual = (n==1); } if (MapValue(section, "convexhull", &n, bool_map, 2)) { - mod->convexhull = (n==1); + spec->convexhull = (n==1); } if (MapValue(section, "usethread", &n, bool_map, 2)) { - mod->usethread = (n==1); + spec->usethread = (n==1); } if (MapValue(section, "fusestatic", &n, bool_map, 2)) { - mod->fusestatic = (n==1); + spec->fusestatic = (n==1); } - MapValue(section, "inertiafromgeom", &mod->inertiafromgeom, TFAuto_map, 3); - ReadAttr(section, "inertiagrouprange", 2, mod->inertiagrouprange, text); + MapValue(section, "inertiafromgeom", &spec->inertiafromgeom, TFAuto_map, 3); + ReadAttr(section, "inertiagrouprange", 2, spec->inertiagrouprange, text); if (MapValue(section, "exactmeshinertia", &n, bool_map, 2)){ - mod->exactmeshinertia = (n==1); + spec->exactmeshinertia = (n==1); } // lengthrange subelement XMLElement* elem = FindSubElem(section, "lengthrange"); if (elem) { - mjLROpt* opt = &(mod->LRopt); + mjLROpt* opt = &(spec->LRopt); // flags MapValue(elem, "mode", &opt->mode, lrmode_map, lrmode_sz); @@ -1110,7 +1110,7 @@ void mjXReader::Option(XMLElement* section, mjOption* opt) { // size section parser -void mjXReader::Size(XMLElement* section, mjSpec* mod) { +void mjXReader::Size(XMLElement* section, mjSpec* spec) { // read memory bytes { constexpr char err_msg[] = @@ -1199,69 +1199,69 @@ void mjXReader::Size(XMLElement* section, mjSpec* mod) { if (*memory / sizeof(mjtNum) > std::numeric_limits::max()) { throw mjXError(section, "%s", err_msg); } - mod->memory = *memory; + spec->memory = *memory; } } // read sizes - ReadAttrInt(section, "nuserdata", &mod->nuserdata); - ReadAttrInt(section, "nkey", &mod->nkey); + ReadAttrInt(section, "nuserdata", &spec->nuserdata); + ReadAttrInt(section, "nkey", &spec->nkey); - ReadAttrInt(section, "nconmax", &mod->nconmax); - if (mod->nconmax < -1) throw mjXError(section, "nconmax must be >= -1"); + ReadAttrInt(section, "nconmax", &spec->nconmax); + if (spec->nconmax < -1) throw mjXError(section, "nconmax must be >= -1"); { int nstack = -1; const bool has_nstack = ReadAttrInt(section, "nstack", &nstack); if (has_nstack) { - if (mod->nstack < -1) { + if (spec->nstack < -1) { throw mjXError(section, "nstack must be >= -1"); } - if (mod->memory != -1 && nstack != -1) { + if (spec->memory != -1 && nstack != -1) { throw mjXError(section, "either 'memory' and 'nstack' attribute can be specified, not both"); } - mod->nstack = nstack; + spec->nstack = nstack; } } { int njmax = -1; const bool has_njmax = ReadAttrInt(section, "njmax", &njmax); if (has_njmax) { - if (mod->njmax < -1) { + if (spec->njmax < -1) { throw mjXError(section, "njmax must be >= -1"); } - if (mod->memory != -1 && njmax != -1) { + if (spec->memory != -1 && njmax != -1) { throw mjXError(section, "either 'memory' and 'njmax' attribute can be specified, not both"); } - mod->njmax = njmax; + spec->njmax = njmax; } } - ReadAttrInt(section, "nuser_body", &mod->nuser_body); - if (mod->nuser_body < -1) throw mjXError(section, "nuser_body must be >= -1"); + ReadAttrInt(section, "nuser_body", &spec->nuser_body); + if (spec->nuser_body < -1) throw mjXError(section, "nuser_body must be >= -1"); - ReadAttrInt(section, "nuser_jnt", &mod->nuser_jnt); - if (mod->nuser_jnt < -1) throw mjXError(section, "nuser_jnt must be >= -1"); + ReadAttrInt(section, "nuser_jnt", &spec->nuser_jnt); + if (spec->nuser_jnt < -1) throw mjXError(section, "nuser_jnt must be >= -1"); - ReadAttrInt(section, "nuser_geom", &mod->nuser_geom); - if (mod->nuser_geom < -1) throw mjXError(section, "nuser_geom must be >= -1"); + ReadAttrInt(section, "nuser_geom", &spec->nuser_geom); + if (spec->nuser_geom < -1) throw mjXError(section, "nuser_geom must be >= -1"); - ReadAttrInt(section, "nuser_site", &mod->nuser_site); - if (mod->nuser_site < -1) throw mjXError(section, "nuser_site must be >= -1"); + ReadAttrInt(section, "nuser_site", &spec->nuser_site); + if (spec->nuser_site < -1) throw mjXError(section, "nuser_site must be >= -1"); - ReadAttrInt(section, "nuser_cam", &mod->nuser_cam); - if (mod->nuser_cam < -1) throw mjXError(section, "nuser_cam must be >= -1"); + ReadAttrInt(section, "nuser_cam", &spec->nuser_cam); + if (spec->nuser_cam < -1) throw mjXError(section, "nuser_cam must be >= -1"); - ReadAttrInt(section, "nuser_tendon", &mod->nuser_tendon); - if (mod->nuser_tendon < -1) throw mjXError(section, "nuser_tendon must be >= -1"); + ReadAttrInt(section, "nuser_tendon", &spec->nuser_tendon); + if (spec->nuser_tendon < -1) throw mjXError(section, "nuser_tendon must be >= -1"); - ReadAttrInt(section, "nuser_actuator", &mod->nuser_actuator); - if (mod->nuser_actuator < -1) throw mjXError(section, "nuser_actuator must be >= -1"); + ReadAttrInt(section, "nuser_actuator", &spec->nuser_actuator); + if (spec->nuser_actuator < -1) throw mjXError(section, "nuser_actuator must be >= -1"); - ReadAttrInt(section, "nuser_sensor", &mod->nuser_sensor); - if (mod->nuser_sensor < -1) throw mjXError(section, "nuser_sensor must be >= -1"); + ReadAttrInt(section, "nuser_sensor", &spec->nuser_sensor); + if (spec->nuser_sensor < -1) throw mjXError(section, "nuser_sensor must be >= -1"); } diff --git a/src/xml/xml_native_reader.h b/src/xml/xml_native_reader.h index c0b36748..69059900 100644 --- a/src/xml/xml_native_reader.h +++ b/src/xml/xml_native_reader.h @@ -42,9 +42,9 @@ class mjXReader : public mjXBase { void SetTextureDir(std::string texturedir); // XML sections embedded in all formats - static void Compiler(tinyxml2::XMLElement* section, mjSpec* mod); // compiler section + static void Compiler(tinyxml2::XMLElement* section, mjSpec* spec); // compiler section static void Option(tinyxml2::XMLElement* section, mjOption* opt); // option section - static void Size(tinyxml2::XMLElement* section, mjSpec* mod); // size section + static void Size(tinyxml2::XMLElement* section, mjSpec* spec); // size section private: // XML section specific to MJCF diff --git a/src/xml/xml_native_writer.cc b/src/xml/xml_native_writer.cc index a5505982..b24a6824 100644 --- a/src/xml/xml_native_writer.cc +++ b/src/xml/xml_native_writer.cc @@ -769,9 +769,9 @@ mjXWriter::mjXWriter(void) { // cast model -void mjXWriter::SetModel(mjSpec* modelspec) { - if (modelspec) { - model = (mjCModel*)modelspec->element; +void mjXWriter::SetModel(mjSpec* spec) { + if (spec) { + model = (mjCModel*)spec->element; } } diff --git a/src/xml/xml_native_writer.h b/src/xml/xml_native_writer.h index 5b8cd38d..f2881e18 100644 --- a/src/xml/xml_native_writer.h +++ b/src/xml/xml_native_writer.h @@ -27,7 +27,7 @@ class mjXWriter : public mjXBase { public: mjXWriter(); // constructor virtual ~mjXWriter() = default; // destructor - void SetModel(mjSpec* modelspec); + void SetModel(mjSpec* spec); // write XML document to string std::string Write(char *error, std::size_t error_sz);