Rename remaining model to spec in xml/

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