Add missing global fields to mjmModel.

PiperOrigin-RevId: 610415124
Change-Id: I07f284804bf73cb67703597973b50b8c00ea8deb
This commit is contained in:
Alessio Quaglino
2024-02-26 08:24:10 -08:00
committed by Copybara-Service
parent 8679c9fc59
commit 1f2d673c7b
10 changed files with 42 additions and 22 deletions
+3
View File
@@ -510,6 +510,9 @@ void mjm_setDouble(mjDoubleVec dest, const double* array, int size) {
// get string // get string
const char* mjm_getString(const mjString source) { const char* mjm_getString(const mjString source) {
std::string* str = reinterpret_cast<std::string*>(source); std::string* str = reinterpret_cast<std::string*>(source);
if (!str) {
return nullptr;
}
return str->c_str(); return str->c_str();
} }
+7
View File
@@ -125,6 +125,13 @@ typedef struct _mjmModel { // model specification
// sizes // sizes
int nkey; // number of keyframes int nkey; // number of keyframes
// global data
mjString comment; // comment at top of XML
mjString modelfiledir; // path to model file
// other
bool hasImplicitPluginElem; // already encountered an implicit plugin sensor/actuator
} mjmModel; } mjmModel;
+4 -2
View File
@@ -837,7 +837,8 @@ bool mjCFlexcomp::MakeMesh(mjCModel* model, char* error, int error_sz) {
} }
// load resource // load resource
string filename = mjuu_makefullname(model->modelfiledir, mjm_getString(model->spec.meshdir), file); string filename = mjuu_makefullname(mjm_getString(model->spec.modelfiledir),
mjm_getString(model->spec.meshdir), file);
mjResource* resource = nullptr; mjResource* resource = nullptr;
try { try {
@@ -943,7 +944,8 @@ bool mjCFlexcomp::MakeGMSH(mjCModel* model, char* error, int error_sz) {
} }
// open resource // open resource
string filename = mjuu_makefullname(model->modelfiledir, mjm_getString(model->spec.meshdir), file); string filename = mjuu_makefullname(mjm_getString(model->spec.modelfiledir),
mjm_getString(model->spec.meshdir), file);
mjResource* resource = nullptr; mjResource* resource = nullptr;
try { try {
+2 -2
View File
@@ -348,7 +348,7 @@ void mjCMesh::Compile(const mjVFS* vfs) {
throw mjCError(this, "unsupported content type: '%s'", asset_type.c_str()); throw mjCError(this, "unsupported content type: '%s'", asset_type.c_str());
} }
string filename = mjuu_makefullname(model->modelfiledir, model->meshdir_, file_); string filename = mjuu_makefullname(model->modelfiledir_, model->meshdir_, file_);
mjResource* resource = LoadResource(filename, vfs); mjResource* resource = LoadResource(filename, vfs);
try { try {
@@ -1951,7 +1951,7 @@ void mjCSkin::Compile(const mjVFS* vfs) {
throw mjCError(this, "Unknown skin file type: %s", file_.c_str()); throw mjCError(this, "Unknown skin file type: %s", file_.c_str());
} }
string filename = mjuu_makefullname(model->modelfiledir, model->meshdir_, file_); string filename = mjuu_makefullname(model->modelfiledir_, model->meshdir_, file_);
mjResource* resource = LoadResource(filename, vfs); mjResource* resource = LoadResource(filename, vfs);
try { try {
+8 -2
View File
@@ -84,8 +84,8 @@ static void copyvec(T1* dest, T2* src, int n) {
// constructor // constructor
mjCModel::mjCModel() { mjCModel::mjCModel() {
mjm_defaultModel(spec); mjm_defaultModel(spec);
comment.clear(); spec_comment_.clear();
modelfiledir.clear(); spec_modelfiledir_.clear();
spec_meshdir_.clear(); spec_meshdir_.clear();
spec_texturedir_.clear(); spec_texturedir_.clear();
spec_modelname_ = "MuJoCo Model"; spec_modelname_ = "MuJoCo Model";
@@ -177,6 +177,8 @@ mjCModel::mjCModel() {
void mjCModel::PointToLocal() { void mjCModel::PointToLocal() {
spec.element = (mjElement)this; spec.element = (mjElement)this;
spec.comment = (mjString)&spec_comment_;
spec.modelfiledir = (mjString)&spec_modelfiledir_;
spec.modelname = (mjString)&spec_modelname_; spec.modelname = (mjString)&spec_modelname_;
spec.meshdir = (mjString)&spec_meshdir_; spec.meshdir = (mjString)&spec_meshdir_;
spec.texturedir = (mjString)&spec_texturedir_; spec.texturedir = (mjString)&spec_texturedir_;
@@ -186,9 +188,13 @@ void mjCModel::PointToLocal() {
void mjCModel::CopyFromSpec() { void mjCModel::CopyFromSpec() {
*static_cast<mjmModel*>(this) = spec; *static_cast<mjmModel*>(this) = spec;
comment_ = spec_comment_;
modelfiledir_ = spec_modelfiledir_;
modelname_ = spec_modelname_; modelname_ = spec_modelname_;
meshdir_ = spec_meshdir_; meshdir_ = spec_meshdir_;
texturedir_ = spec_texturedir_; texturedir_ = spec_texturedir_;
comment = (mjString)&comment_;
modelfiledir = (mjString)&modelfiledir_;
modelname = (mjString)&modelname_; modelname = (mjString)&modelname_;
meshdir = (mjString)&meshdir_; meshdir = (mjString)&meshdir_;
texturedir = (mjString)&texturedir_; texturedir = (mjString)&texturedir_;
+5 -5
View File
@@ -129,9 +129,8 @@ class mjCModel : private mjmModel {
//------------------------ global data //------------------------ global data
std::string comment; // comment at top of XML
std::string modelfiledir; // path to model file
std::vector<mjCDef*> defaults; // settings for each defaults class std::vector<mjCDef*> defaults; // settings for each defaults class
std::vector<std::pair<const mjpPlugin*, int>> active_plugins; // list of active plugins
private: private:
void TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs); void TryCompile(mjModel*& m, mjData*& d, const mjVFS* vfs);
@@ -245,8 +244,6 @@ class mjCModel : private mjmModel {
std::vector<mjCText*> texts; // list of text fields std::vector<mjCText*> texts; // list of text fields
std::vector<mjCTuple*> tuples; // list of tuple fields std::vector<mjCTuple*> tuples; // list of tuple fields
std::vector<mjCKey*> keys; // list of keyframe fields std::vector<mjCKey*> keys; // list of keyframe fields
std::vector<std::pair<const mjpPlugin*, int>> active_plugins; // list of active plugins
std::vector<mjCPlugin*> plugins; // list of plugin instances std::vector<mjCPlugin*> plugins; // list of plugin instances
// pointers to objects created inside kinematic tree // pointers to objects created inside kinematic tree
@@ -272,16 +269,19 @@ class mjCModel : private mjmModel {
// map from object names to ids // map from object names to ids
mjListKeyMap ids; mjListKeyMap ids;
bool hasImplicitPluginElem; // already encountered an implicit plugin sensor/actuator
bool compiled; // already compiled flag (cannot be compiled again) bool compiled; // already compiled flag (cannot be compiled again)
mjCError errInfo; // last error info mjCError errInfo; // last error info
int fixCount; // how many bodies have been fixed int fixCount; // how many bodies have been fixed
std::vector<mjtNum> qpos0; // save qpos0, to recognize changed key_qpos in write std::vector<mjtNum> qpos0; // save qpos0, to recognize changed key_qpos in write
// variable-size attributes // variable-size attributes
std::string comment_; // comment at top of XML
std::string modelfiledir_; // path to model file
std::string modelname_; std::string modelname_;
std::string meshdir_; std::string meshdir_;
std::string texturedir_; std::string texturedir_;
std::string spec_comment_;
std::string spec_modelfiledir_;
std::string spec_modelname_; std::string spec_modelname_;
std::string spec_meshdir_; std::string spec_meshdir_;
std::string spec_texturedir_; std::string spec_texturedir_;
+3 -3
View File
@@ -2654,7 +2654,7 @@ void mjCHField::Compile(const mjVFS* vfs) {
throw mjCError(this, "unsupported content type: '%s'", asset_type.c_str()); throw mjCError(this, "unsupported content type: '%s'", asset_type.c_str());
} }
string filename = mjuu_makefullname(model->modelfiledir, model->meshdir_, file_); string filename = mjuu_makefullname(model->modelfiledir_, model->meshdir_, file_);
mjResource* resource = LoadResource(filename, vfs); mjResource* resource = LoadResource(filename, vfs);
try { try {
@@ -3273,7 +3273,7 @@ void mjCTexture::LoadCubeSeparate(const mjVFS* vfs) {
} }
// make filename // make filename
string filename = mjuu_makefullname(model->modelfiledir, model->texturedir_, cubefiles_[i]); string filename = mjuu_makefullname(model->modelfiledir_, model->texturedir_, cubefiles_[i]);
// load PNG or custom // load PNG or custom
unsigned int w, h; unsigned int w, h;
@@ -3373,7 +3373,7 @@ void mjCTexture::Compile(const mjVFS* vfs) {
} }
// make filename // make filename
string filename = mjuu_makefullname(model->modelfiledir, model->texturedir_, file_); string filename = mjuu_makefullname(model->modelfiledir_, model->texturedir_, file_);
// dispatch // dispatch
if (type==mjTEXTURE_2D) { if (type==mjTEXTURE_2D) {
+5 -4
View File
@@ -15,6 +15,7 @@
#include "xml/xml.h" #include "xml/xml.h"
#include <locale.h> #include <locale.h>
#include "user/user_api.h"
#if defined(__APPLE__) || defined(__FreeBSD__) #if defined(__APPLE__) || defined(__FreeBSD__)
#include <xlocale.h> #include <xlocale.h>
@@ -348,9 +349,9 @@ mjCModel* mjParseXML(const char* filename, const mjVFS* vfs,
int ndir = 0; int ndir = 0;
mju_getResourceDir(resource, &dir, &ndir); mju_getResourceDir(resource, &dir, &ndir);
if (dir != nullptr) { if (dir != nullptr) {
model->modelfiledir = std::string(dir, ndir); mjm_setString(model->spec.modelfiledir, std::string(dir, ndir).c_str());
} else { } else {
model->modelfiledir = ""; mjm_setString(model->spec.modelfiledir, "");
} }
// close resource // close resource
@@ -362,8 +363,8 @@ mjCModel* 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(model->modelfiledir); parser.SetModelFileDir(mjm_getString(model->spec.modelfiledir));
mjIncludeXML(parser, root, model->modelfiledir, vfs, included); mjIncludeXML(parser, root, mjm_getString(model->spec.modelfiledir), vfs, included);
// parse MuJoCo model // parse MuJoCo model
parser.SetModel(model); parser.SetModel(model);
+2 -2
View File
@@ -811,9 +811,9 @@ void mjXReader::Parse(XMLElement* root) {
// get comment // get comment
if (root->FirstChild() && root->FirstChild()->ToComment()) { if (root->FirstChild() && root->FirstChild()->ToComment()) {
model->comment = root->FirstChild()->Value(); mjm_setString(model->spec.comment, root->FirstChild()->Value());
} else { } else {
model->comment.clear(); mjm_setString(model->spec.comment, "");
} }
//------------------- parse MuJoCo sections embedded in all XML formats //------------------- parse MuJoCo sections embedded in all XML formats
+3 -2
View File
@@ -781,8 +781,9 @@ string mjXWriter::Write(char *error, size_t error_sz) {
doc.InsertFirstChild(root); doc.InsertFirstChild(root);
// write comment if present // write comment if present
if (!model->comment.empty()) { string text = mjm_getString(model->comment);
XMLComment* comment = doc.NewComment(model->comment.c_str()); if (!text.empty()) {
XMLComment* comment = doc.NewComment(text.c_str());
root->LinkEndChild(comment); root->LinkEndChild(comment);
} }