From e885a3dc3de1877f1f69fe8ade292fe9e37cc021 Mon Sep 17 00:00:00 2001 From: Kyle Bayes Date: Thu, 16 May 2024 03:21:17 -0700 Subject: [PATCH] Make default class members private. PiperOrigin-RevId: 634288934 Change-Id: I1b7a5ec8dd51c65377b09bcb85c56a849573cac7 --- src/user/user_api.cc | 4 +- src/user/user_composite.cc | 26 +-- src/user/user_flexcomp.cc | 6 +- src/user/user_mesh.cc | 8 +- src/user/user_model.h | 3 +- src/user/user_objects.cc | 187 ++++++++++---------- src/user/user_objects.h | 128 ++++++++------ src/xml/xml_native_writer.cc | 318 +++++++++++++++++------------------ src/xml/xml_native_writer.h | 28 +-- 9 files changed, 362 insertions(+), 346 deletions(-) diff --git a/src/user/user_api.cc b/src/user/user_api.cc index 7fd88e36..d818f778 100644 --- a/src/user/user_api.cc +++ b/src/user/user_api.cc @@ -429,7 +429,7 @@ mjsPlugin* mjs_addPlugin(mjSpec* s) { // add default to model mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, int parentid, int* id) { mjCModel* modelC = static_cast(s->element); - *id = (int)modelC->Defaults().size(); + *id = (int)modelC->NumDefaults(); mjCDef* def = modelC->AddDefault(classname, parentid); if (def) { return &def->spec; @@ -469,7 +469,7 @@ mjsDefault* mjs_findDefault(mjSpec* s, const char* classname) { // get default[0] from model mjsDefault* mjs_getSpecDefault(mjSpec* s) { mjCModel* modelC = static_cast(s->element); - mjCDef* def = modelC->Defaults()[0]; + mjCDef* def = modelC->Defaults(0); if (!def) { return nullptr; } diff --git a/src/user/user_composite.cc b/src/user/user_composite.cc index 01bbcf20..0fa0ce08 100644 --- a/src/user/user_composite.cc +++ b/src/user/user_composite.cc @@ -561,7 +561,7 @@ bool mjCComposite::MakeParticle(mjCModel* model, mjsBody* body, char* error, int // create tendon mjsTendon* ten = mjs_addTendon(&model->spec, &def[mjCOMPKIND_TENDON].spec); - mjs_setDefault(ten->element, &model->Defaults()[0]->spec); + mjs_setDefault(ten->element, &model->Defaults(0)->spec); mjs_setString(ten->name, txt0); ten->group = 4; mjs_wrapSite(ten, txt1); @@ -569,7 +569,7 @@ bool mjCComposite::MakeParticle(mjCModel* model, mjsBody* body, char* error, int // add equality constraint mjsEquality* eq = mjs_addEquality(&model->spec, &def[mjCOMPKIND_TENDON].spec); - mjs_setDefault(eq->element, &model->Defaults()[0]->spec); + mjs_setDefault(eq->element, &model->Defaults(0)->spec); eq->type = mjEQ_TENDON; mjs_setString(eq->name1, mjs_getString(ten->name)); } @@ -675,7 +675,7 @@ bool mjCComposite::MakeGrid(mjCModel* model, mjsBody* body, char* error, int err // create tendon mjCTendon* ten = model->AddTendon(def + mjCOMPKIND_TENDON); - ten->def = model->Defaults()[0]; + ten->def = model->Defaults(0); mju::sprintf_arr(txt, "%sT%d_%d_%d", prefix.c_str(), i, ix, iy); ten->name = txt; ten->WrapSite(txt1); @@ -683,7 +683,7 @@ bool mjCComposite::MakeGrid(mjCModel* model, mjsBody* body, char* error, int err // add equality constraint mjsEquality* eq = mjs_addEquality(&model->spec, &def[mjCOMPKIND_TENDON].spec); - mjs_setDefault(eq->element, &model->Defaults()[0]->spec); + mjs_setDefault(eq->element, &model->Defaults(0)->spec); eq->type = mjEQ_TENDON; mjs_setString(eq->name1, ten->name.c_str()); } @@ -1045,7 +1045,7 @@ mjsBody* mjCComposite::AddRopeBody(mjCModel* model, mjsBody* body, int ix, int i // add constraint mjsEquality* eq = mjs_addEquality(&model->spec, &def[mjCOMPKIND_TWIST].spec); - mjs_setDefault(eq->element, &model->Defaults()[0]->spec); + mjs_setDefault(eq->element, &model->Defaults(0)->spec); eq->type = mjEQ_JOINT; mjs_setString(eq->name1, mjs_getString(jnt->name)); } @@ -1063,7 +1063,7 @@ mjsBody* mjCComposite::AddRopeBody(mjCModel* model, mjsBody* body, int ix, int i // add constraint mjsEquality* eq = mjs_addEquality(&model->spec, &def[mjCOMPKIND_STRETCH].spec); - mjs_setDefault(eq->element, &model->Defaults()[0]->spec); + mjs_setDefault(eq->element, &model->Defaults(0)->spec); eq->type = mjEQ_JOINT; mjs_setString(eq->name1, mjs_getString(jnt->name)); } @@ -1131,7 +1131,7 @@ bool mjCComposite::MakeBox(mjCModel* model, mjsBody* body, char* error, int erro // fixed tendon for all joints mjCTendon* ten = model->AddTendon(def + mjCOMPKIND_TENDON); - ten->def = model->Defaults()[0]; + ten->def = model->Defaults(0); mju::sprintf_arr(txt, "%sT", prefix.c_str()); ten->name = txt; @@ -1184,7 +1184,7 @@ bool mjCComposite::MakeBox(mjCModel* model, mjsBody* body, char* error, int erro // add fix constraint mjsEquality* eq = mjs_addEquality(&model->spec, &def[mjCOMPKIND_JOINT].spec); - mjs_setDefault(eq->element, &model->Defaults()[0]->spec); + mjs_setDefault(eq->element, &model->Defaults(0)->spec); eq->type = mjEQ_JOINT; mjs_setString(eq->name1, mjs_getString(jnt->name)); @@ -1218,7 +1218,7 @@ bool mjCComposite::MakeBox(mjCModel* model, mjsBody* body, char* error, int erro // finalize fixed tendon mjsEquality* eqt = mjs_addEquality(&model->spec, &def[mjCOMPKIND_TENDON].spec); - mjs_setDefault(eqt->element, &model->Defaults()[0]->spec); + mjs_setDefault(eqt->element, &model->Defaults(0)->spec); eqt->type = mjEQ_TENDON; mjs_setString(eqt->name1, ten->name.c_str()); @@ -1244,7 +1244,7 @@ void mjCComposite::MakeShear(mjCModel* model) { // create tendon mjCTendon* ten = model->AddTendon(def + mjCOMPKIND_SHEAR); - ten->def = model->Defaults()[0]; + ten->def = model->Defaults(0); ten->WrapSite(txt1); ten->WrapSite(txt2); @@ -1254,7 +1254,7 @@ void mjCComposite::MakeShear(mjCModel* model) { // equality constraint mjsEquality* eq = mjs_addEquality(&model->spec, &def[mjCOMPKIND_SHEAR].spec); - mjs_setDefault(eq->element, &model->Defaults()[0]->spec); + mjs_setDefault(eq->element, &model->Defaults(0)->spec); eq->type = mjEQ_TENDON; mjs_setString(eq->name1, txt); } @@ -1552,11 +1552,11 @@ void mjCComposite::MakeCableBonesSubgrid(mjCModel* model, mjsSkin* skin) { // bind pose if (iy==0) { bindpos.push_back((ix==count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); - bindpos.push_back(-def[0].geom.spec.size[1]); + bindpos.push_back(-def[0].Geom().spec.size[1]); bindpos.push_back(0); } else if (iy==2) { bindpos.push_back((ix==count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); - bindpos.push_back(def[0].geom.spec.size[1]); + bindpos.push_back(def[0].Geom().spec.size[1]); bindpos.push_back(0); } else { bindpos.push_back((ix==count[0]-1) ? -2*def[0].spec.geom->size[0] : 0); diff --git a/src/user/user_flexcomp.cc b/src/user/user_flexcomp.cc index bd5b9e53..0f2af4b8 100644 --- a/src/user/user_flexcomp.cc +++ b/src/user/user_flexcomp.cc @@ -400,7 +400,7 @@ bool mjCFlexcomp::Make(mjSpec* spec, mjsBody* body, char* error, int error_sz) { mjsFlex* pf = &flex->spec; int id = flex->id; - *flex = def.flex; + *flex = def.Flex(); flex->PointToLocal(); flex->model = model; @@ -521,7 +521,7 @@ bool mjCFlexcomp::Make(mjSpec* spec, mjsBody* body, char* error, int error_sz) { // create edge equality constraint if (equality) { mjsEquality* pe = mjs_addEquality(&model->spec, &def.spec); - mjs_setDefault(pe->element, &model->Defaults()[0]->spec); + mjs_setDefault(pe->element, &model->Defaults(0)->spec); pe->type = mjEQ_FLEX; pe->active = true; mjs_setString(pe->name1, name.c_str()); @@ -544,7 +544,7 @@ int mjCFlexcomp::GridID(int ix, int iy, int iz) { // make grid bool mjCFlexcomp::MakeGrid(char* error, int error_sz) { - int dim = def.flex.spec.dim; + int dim = def.Flex().spec.dim; bool hastex = texcoord.empty(); // 1D diff --git a/src/user/user_mesh.cc b/src/user/user_mesh.cc index c61bbdba..eb722ab8 100644 --- a/src/user/user_mesh.cc +++ b/src/user/user_mesh.cc @@ -146,12 +146,12 @@ mjCMesh::mjCMesh(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - *this = _def->mesh; + *this = _def->Mesh(); } // set model, def model = _model; - def = (_def ? _def : (_model ? _model->Defaults()[0] : 0)); + def = (_def ? _def : (_model ? _model->Defaults(0) : 0)); // in case this body is not compiled CopyFromSpec(); @@ -1426,7 +1426,7 @@ void mjCMesh::Process() { // apply formula, accumulate GetVolumeRef(type) += vol; for (int j=0; j<6; j++) { - P[j] += def->geom.density*vol / + P[j] += def->Geom().density*vol / (type==mjINERTIA_SHELL ? 12 : 20) * ( 2*(D[k[j][0]] * D[k[j][1]] + E[k[j][0]] * E[k[j][1]] + @@ -1467,7 +1467,7 @@ void mjCMesh::Process() { } // compute sizes of equivalent inertia box - double mass = GetVolumeRef(type) * def->geom.density; + double mass = GetVolumeRef(type) * def->Geom().density; double* boxsz = GetInertiaBoxPtr(type); boxsz[0] = sqrt(6*(eigval[1]+eigval[2]-eigval[0])/mass)/2; boxsz[1] = sqrt(6*(eigval[0]+eigval[2]-eigval[1])/mass)/2; diff --git a/src/user/user_model.h b/src/user/user_model.h index 1138bc45..135510af 100644 --- a/src/user/user_model.h +++ b/src/user/user_model.h @@ -222,7 +222,8 @@ class mjCModel : public mjCModel_, private mjSpec { std::string get_meshdir() const { return meshdir_; } std::string get_texturedir() const { return texturedir_; } - const std::vector& Defaults() const { return defaults_; } + mjCDef* Defaults(int i) const { return defaults_[i]; } + int NumDefaults() const { return defaults_.size(); } const std::vector>& ActivePlugins() const { return active_plugins_; diff --git a/src/user/user_objects.cc b/src/user/user_objects.cc index 8ed146e1..eaf2dfaf 100644 --- a/src/user/user_objects.cc +++ b/src/user/user_objects.cc @@ -557,22 +557,22 @@ int mjCBoundingVolumeHierarchy::MakeBVH( //------------------------- class mjCDef implementation -------------------------------------------- // constructor -mjCDef::mjCDef(void) { +mjCDef::mjCDef() { name.clear(); parentid = -1; childid.clear(); - mjs_defaultJoint(&joint.spec); - mjs_defaultGeom(&geom.spec); - mjs_defaultSite(&site.spec); - mjs_defaultCamera(&camera.spec); - mjs_defaultLight(&light.spec); - mjs_defaultFlex(&flex.spec); - mjs_defaultMesh(&mesh.spec); - mjs_defaultMaterial(&material.spec); - mjs_defaultPair(&pair.spec); - mjs_defaultEquality(&equality.spec); - mjs_defaultTendon(&tendon.spec); - mjs_defaultActuator(&actuator.spec); + mjs_defaultJoint(&joint_.spec); + mjs_defaultGeom(&geom_.spec); + mjs_defaultSite(&site_.spec); + mjs_defaultCamera(&camera_.spec); + mjs_defaultLight(&light_.spec); + mjs_defaultFlex(&flex_.spec); + mjs_defaultMesh(&mesh_.spec); + mjs_defaultMaterial(&material_.spec); + mjs_defaultPair(&pair_.spec); + mjs_defaultEquality(&equality_.spec); + mjs_defaultTendon(&tendon_.spec); + mjs_defaultActuator(&actuator_.spec); // make sure all the pointers are local PointToLocal(); @@ -592,12 +592,12 @@ void mjCDef::Compile(const mjCModel* model) { CopyFromSpec(); // enforce length of all default userdata arrays - joint.userdata_.resize(model->nuser_jnt); - geom.userdata_.resize(model->nuser_geom); - site.userdata_.resize(model->nuser_site); - camera.userdata_.resize(model->nuser_cam); - tendon.userdata_.resize(model->nuser_tendon); - actuator.userdata_.resize(model->nuser_actuator); + joint_.userdata_.resize(model->nuser_jnt); + geom_.userdata_.resize(model->nuser_geom); + site_.userdata_.resize(model->nuser_site); + camera_.userdata_.resize(model->nuser_cam); + tendon_.userdata_.resize(model->nuser_tendon); + actuator_.userdata_.resize(model->nuser_actuator); } @@ -608,18 +608,18 @@ mjCDef& mjCDef::operator=(const mjCDef& other) { name = other.name; parentid = other.parentid; childid = other.childid; - joint = other.joint; - geom = other.geom; - site = other.site; - camera = other.camera; - light = other.light; - flex = other.flex; - mesh = other.mesh; - material = other.material; - pair = other.pair; - equality = other.equality; - tendon = other.tendon; - actuator = other.actuator; + joint_ = other.joint_; + geom_ = other.geom_; + site_ = other.site_; + camera_ = other.camera_; + light_ = other.light_; + flex_ = other.flex_; + mesh_ = other.mesh_; + material_ = other.material_; + pair_ = other.pair_; + equality_ = other.equality_; + tendon_ = other.tendon_; + actuator_ = other.actuator_; } PointToLocal(); return *this; @@ -628,49 +628,49 @@ mjCDef& mjCDef::operator=(const mjCDef& other) { void mjCDef::PointToLocal() { - joint.PointToLocal(); - geom.PointToLocal(); - site.PointToLocal(); - camera.PointToLocal(); - light.PointToLocal(); - flex.PointToLocal(); - mesh.PointToLocal(); - material.PointToLocal(); - pair.PointToLocal(); - equality.PointToLocal(); - tendon.PointToLocal(); - actuator.PointToLocal(); + joint_.PointToLocal(); + geom_.PointToLocal(); + site_.PointToLocal(); + camera_.PointToLocal(); + light_.PointToLocal(); + flex_.PointToLocal(); + mesh_.PointToLocal(); + material_.PointToLocal(); + pair_.PointToLocal(); + equality_.PointToLocal(); + tendon_.PointToLocal(); + actuator_.PointToLocal(); spec.element = static_cast(this); spec.name = (mjString)&name; - spec.joint = &joint.spec; - spec.geom = &geom.spec; - spec.site = &site.spec; - spec.camera = &camera.spec; - spec.light = &light.spec; - spec.flex = &flex.spec; - spec.mesh = &mesh.spec; - spec.material = &material.spec; - spec.pair = &pair.spec; - spec.equality = &equality.spec; - spec.tendon = &tendon.spec; - spec.actuator = &actuator.spec; + spec.joint = &joint_.spec; + spec.geom = &geom_.spec; + spec.site = &site_.spec; + spec.camera = &camera_.spec; + spec.light = &light_.spec; + spec.flex = &flex_.spec; + spec.mesh = &mesh_.spec; + spec.material = &material_.spec; + spec.pair = &pair_.spec; + spec.equality = &equality_.spec; + spec.tendon = &tendon_.spec; + spec.actuator = &actuator_.spec; } void mjCDef::CopyFromSpec() { - joint.CopyFromSpec(); - geom.CopyFromSpec(); - site.CopyFromSpec(); - camera.CopyFromSpec(); - light.CopyFromSpec(); - flex.CopyFromSpec(); - mesh.CopyFromSpec(); - material.CopyFromSpec(); - pair.CopyFromSpec(); - equality.CopyFromSpec(); - tendon.CopyFromSpec(); - actuator.CopyFromSpec(); + joint_.CopyFromSpec(); + geom_.CopyFromSpec(); + site_.CopyFromSpec(); + camera_.CopyFromSpec(); + light_.CopyFromSpec(); + flex_.CopyFromSpec(); + mesh_.CopyFromSpec(); + material_.CopyFromSpec(); + pair_.CopyFromSpec(); + equality_.CopyFromSpec(); + tendon_.CopyFromSpec(); + actuator_.CopyFromSpec(); } @@ -1674,12 +1674,12 @@ mjCJoint::mjCJoint(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - *this = _def->joint; + *this = _def->Joint(); } // set model, def model = _model; - def = (_def ? _def : (_model ? _model->Defaults()[0] : 0)); + def = (_def ? _def : (_model ? _model->Defaults(0) : 0)); // point to local PointToLocal(); @@ -1878,12 +1878,12 @@ mjCGeom::mjCGeom(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - *this = _def->geom; + *this = _def->Geom(); } // set model, def model = _model; - def = (_def ? _def : (_model ? _model->Defaults()[0] : 0)); + def = (_def ? _def : (_model ? _model->Defaults(0) : 0)); // point to local PointToLocal(); @@ -1964,7 +1964,7 @@ void mjCGeom::NameSpace(const mjCModel* m) { // compute geom volume -double mjCGeom::GetVolume(void) { +double mjCGeom::GetVolume() const { double height; // get from mesh @@ -2512,7 +2512,7 @@ mjCSite::mjCSite(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - *this = _def->site; + *this = _def->Site(); } // point to local @@ -2523,7 +2523,7 @@ mjCSite::mjCSite(mjCModel* _model, mjCDef* _def) { // set model, def model = _model; - def = (_def ? _def : (_model ? _model->Defaults()[0] : 0)); + def = (_def ? _def : (_model ? _model->Defaults(0) : 0)); } @@ -2664,12 +2664,12 @@ mjCCamera::mjCCamera(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - *this = _def->camera; + *this = _def->Camera(); } // set model, def model = _model; - def = (_def ? _def : (_model ? _model->Defaults()[0] : 0)); + def = (_def ? _def : (_model ? _model->Defaults(0) : 0)); // point to local PointToLocal(); @@ -2818,12 +2818,12 @@ mjCLight::mjCLight(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - *this = _def->light; + *this = _def->Light(); } // set model, def model = _model; - def = (_def ? _def : (_model ? _model->Defaults()[0] : 0)); + def = (_def ? _def : (_model ? _model->Defaults(0) : 0)); PointToLocal(); CopyFromSpec(); @@ -3855,12 +3855,12 @@ mjCMaterial::mjCMaterial(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - *this = _def->material; + *this = _def->Material(); } // set model, def model = _model; - def = (_def ? _def : (_model ? _model->Defaults()[0] : 0)); + def = (_def ? _def : (_model ? _model->Defaults(0) : 0)); // point to local PointToLocal(); @@ -3943,12 +3943,12 @@ mjCPair::mjCPair(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - *this = _def->pair; + *this = _def->Pair(); } // set model, def model = _model; - def = (_def ? _def : (_model ? _model->Defaults()[0] : 0)); + def = (_def ? _def : (_model ? _model->Defaults(0) : 0)); // point to local PointToLocal(); @@ -4304,12 +4304,12 @@ mjCEquality::mjCEquality(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - *this = _def->equality; + *this = _def->Equality(); } // set model, def model = _model; - def = (_def ? _def : (_model ? _model->Defaults()[0] : 0)); + def = (_def ? _def : (_model ? _model->Defaults(0) : 0)); // point to local PointToLocal(); @@ -4466,12 +4466,12 @@ mjCTendon::mjCTendon(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - *this = _def->tendon; + *this = _def->Tendon(); } // set model, def model = _model; - def = (_def ? _def : (_model ? _model->Defaults()[0] : 0)); + def = (_def ? _def : (_model ? _model->Defaults(0) : 0)); // point to local PointToLocal(); @@ -4629,19 +4629,18 @@ void mjCTendon::WrapPulley(double divisor, std::string_view info) { // get number of wraps -int mjCTendon::NumWraps(void) { +int mjCTendon::NumWraps() const { return (int)path.size(); } // get pointer to specified wrap -mjCWrap* mjCTendon::GetWrap(int id) { - if (id>=0 && id<(int)path.size()) { - return path[id]; - } else { - return 0; +const mjCWrap* mjCTendon::GetWrap(int i) const { + if (i >= 0 && i < (int)path.size()) { + return path[i]; } + return nullptr; } @@ -4945,12 +4944,12 @@ mjCActuator::mjCActuator(mjCModel* _model, mjCDef* _def) { // reset to default if given if (_def) { - *this = _def->actuator; + *this = _def->Actuator(); } // set model, def model = _model; - def = (_def ? _def : (_model ? _model->Defaults()[0] : 0)); + def = (_def ? _def : (_model ? _model->Defaults(0) : 0)); // in case this actuator is not compiled CopyFromSpec(); diff --git a/src/user/user_objects.h b/src/user/user_objects.h index f6df07b6..b173d1e0 100644 --- a/src/user/user_objects.h +++ b/src/user/user_objects.h @@ -413,8 +413,8 @@ class mjCJoint : public mjCJoint_, private mjsJoint { void CopyFromSpec(void); // used by mjXWriter and mjCModel - const std::vector& get_userdata() { return userdata_; } - const double* get_range() { return range; } + const std::vector& get_userdata() const { return userdata_; } + const double* get_range() const { return range; } bool is_limited() const; bool is_actfrclimited() const; @@ -475,7 +475,7 @@ class mjCGeom : public mjCGeom_, private mjsGeom { using mjCBase::name; mjsGeom spec; // variables set by user - double GetVolume(void); // compute geom volume + double GetVolume() const; // compute geom volume void SetInertia(void); // compute and set geom inertia bool IsVisual(void) const { return visual_; } void SetNotVisual(void) { visual_ = false; } @@ -489,10 +489,10 @@ class mjCGeom : public mjCGeom_, private mjsGeom { void SetBoundingVolume(mjCBoundingVolume* bv) const; // used by mjXWriter and mjCModel - const std::vector& get_userdata() { return userdata_; } - const std::string& get_hfieldname() { return spec_hfieldname_; } - const std::string& get_meshname() { return spec_meshname_; } - const std::string& get_material() { return spec_material_; } + const std::vector& get_userdata() const { return userdata_; } + const std::string& get_hfieldname() const { return spec_hfieldname_; } + const std::string& get_meshname() const { return spec_meshname_; } + const std::string& get_material() const { return spec_material_; } void del_material() { spec_material_.clear(); } private: @@ -546,8 +546,8 @@ class mjCSite : public mjCSite_, private mjsSite { using mjCBase::info; // used by mjXWriter and mjCModel - const std::vector& get_userdata() { return userdata_; } - const std::string& get_material() { return material_; } + const std::vector& get_userdata() const { return userdata_; } + const std::string& get_material() const { return material_; } void del_material() { material_.clear(); } private: @@ -589,8 +589,8 @@ class mjCCamera : public mjCCamera_, private mjsCamera { using mjCBase::info; // used by mjXWriter and mjCModel - const std::string& get_targetbody() { return targetbody_; } - const std::vector& get_userdata() { return userdata_; } + const std::string& get_targetbody() const { return targetbody_; } + const std::vector& get_userdata() const { return userdata_; } private: void Compile(void); // compiler @@ -629,7 +629,7 @@ class mjCLight : public mjCLight_, private mjsLight { using mjCBase::info; // used by mjXWriter and mjCModel - const std::string& get_targetbody() { return targetbody_; } + const std::string& get_targetbody() const { return targetbody_; } private: void Compile(void); // compiler @@ -697,12 +697,12 @@ class mjCFlex: public mjCFlex_, private mjsFlex { void NameSpace(const mjCModel* m); // used by mjXWriter and mjCModel - const std::string& get_material() { return material_; } - const std::vector& get_vertbody() { return vertbody_; } - const std::vector& get_vert() { return vert_; } - const std::vector& get_elemaabb() { return elemaabb_; } - const std::vector& get_elem() { return elem_; } - const std::vector& get_texcoord() { return texcoord_; } + const std::string& get_material() const { return material_; } + const std::vector& get_vertbody() const { return vertbody_; } + const std::vector& get_vert() const { return vert_; } + const std::vector& get_elemaabb() const { return elemaabb_; } + const std::vector& get_elem() const { return elem_; } + const std::vector& get_texcoord() const { return texcoord_; } bool HasTexcoord() const; // texcoord not null void DelTexcoord(); // delete texcoord @@ -921,16 +921,16 @@ class mjCSkin: public mjCSkin_, private mjsSkin { using mjCBase::classname; using mjCBase::info; - std::string get_file() const { return file_; } - std::string& get_material() { return material_; } - std::vector& get_vert() { return vert_; } - std::vector& get_texcoord() { return texcoord_; } - std::vector& get_face() { return face_; } - std::vector& get_bodyname() { return bodyname_; } - std::vector& get_bindpos() { return bindpos_; } - std::vector& get_bindquat() { return bindquat_; } - std::vector>& get_vertid() { return vertid_; } - std::vector>& get_vertweight() { return vertweight_; } + const std::string& get_file() const { return file_; } + const std::string& get_material() const { return material_; } + const std::vector& get_vert() const { return vert_; } + const std::vector& get_texcoord() const { return texcoord_; } + const std::vector& get_face() const { return face_; } + const std::vector& get_bodyname() const { return bodyname_; } + const std::vector& get_bindpos() const { return bindpos_; } + const std::vector& get_bindquat() const { return bindquat_; } + const std::vector>& get_vertid() const { return vertid_; } + const std::vector>& get_vertweight() const { return vertweight_; } void del_material() { material_.clear(); } void CopyFromSpec(); @@ -1082,7 +1082,7 @@ class mjCMaterial : public mjCMaterial_, private mjsMaterial { void PointToLocal(); void NameSpace(const mjCModel* m); - std::string get_texture() { return texture_; } + const std::string& get_texture() const { return texture_; } void del_texture() { texture_.clear(); } private: @@ -1124,8 +1124,8 @@ class mjCPair : public mjCPair_, private mjsPair { void ResolveReferences(const mjCModel* m); void NameSpace(const mjCModel* m); - std::string get_geomname1() { return geomname1_; } - std::string get_geomname2() { return geomname2_; } + const std::string& get_geomname1() const { return geomname1_; } + const std::string& get_geomname2() const { return geomname2_; } int GetSignature(void) { return signature; @@ -1258,7 +1258,7 @@ class mjCTendon : public mjCTendon_, private mjsTendon { using mjCBase::info; void set_material(std::string _material) { material_ = _material; } - std::string& get_material() { return material_; } + const std::string& get_material() const { return material_; } void del_material() { material_.clear(); } // API for adding wrapping objects @@ -1268,12 +1268,12 @@ class mjCTendon : public mjCTendon_, private mjsTendon { void WrapPulley(double divisor, std::string_view info = ""); // pulley // API for access to wrapping objects - int NumWraps(void); // number of wraps - mjCWrap* GetWrap(int); // pointer to wrap + int NumWraps() const; // number of wraps + const mjCWrap* GetWrap(int i) const; // pointer to wrap std::vector path; // wrapping objects // used by mjXWriter and mjCModel - const std::vector& get_userdata() { return userdata_; } + const std::vector& get_userdata() const { return userdata_; } const double* get_range() { return range; } void CopyFromSpec(); @@ -1391,10 +1391,10 @@ class mjCActuator : public mjCActuator_, private mjsActuator { using mjCBase::info; // used by mjXWriter and mjCModel - const std::vector& get_userdata() { return userdata_; } - const std::string& get_target() { return spec_target_; } - const std::string& get_slidersite() { return spec_slidersite_; } - const std::string& get_refsite() { return spec_refsite_; } + const std::vector& get_userdata() const { return userdata_; } + const std::string& get_target() const { return spec_target_; } + const std::string& get_slidersite() const { return spec_slidersite_; } + const std::string& get_refsite() const { return spec_refsite_; } bool is_ctrllimited() const; bool is_forcelimited() const; @@ -1615,13 +1615,29 @@ class mjCDef : public mjElement { friend class mjXWriter; public: - mjCDef(void); // constructor - mjCDef(const mjCDef& other); // copy constructor - void Compile(const mjCModel* model); // compiler - mjCDef& operator=(const mjCDef& other); // copy assignment + mjCDef(); + mjCDef(const mjCDef& other); + mjCDef& operator=(const mjCDef& other); + void PointToLocal(void); void CopyFromSpec(void); + void Compile(const mjCModel* model); + + // accessors + mjCJoint& Joint() { return joint_; } + mjCGeom& Geom() { return geom_; } + mjCSite& Site() { return site_; } + mjCCamera& Camera() { return camera_; } + mjCLight& Light() { return light_; } + mjCFlex& Flex() { return flex_; } + mjCMesh& Mesh() { return mesh_; } + mjCMaterial& Material() { return material_; } + mjCPair& Pair() { return pair_; } + mjCEquality& Equality() { return equality_; } + mjCTendon& Tendon() { return tendon_; } + mjCActuator& Actuator() { return actuator_; } + // identifiers std::string name; // class name int parentid; // id of parent class @@ -1629,19 +1645,19 @@ class mjCDef : public mjElement { mjsDefault spec; - // default objects (TODO: they should become private) - mjCJoint joint; - mjCGeom geom; - mjCSite site; - mjCCamera camera; - mjCLight light; - mjCFlex flex; - mjCMesh mesh; - mjCMaterial material; - mjCPair pair; - mjCEquality equality; - mjCTendon tendon; - mjCActuator actuator; + private: + mjCJoint joint_; + mjCGeom geom_; + mjCSite site_; + mjCCamera camera_; + mjCLight light_; + mjCFlex flex_; + mjCMesh mesh_; + mjCMaterial material_; + mjCPair pair_; + mjCEquality equality_; + mjCTendon tendon_; + mjCActuator actuator_; }; #endif // MUJOCO_SRC_USER_USER_OBJECTS_H_ diff --git a/src/xml/xml_native_writer.cc b/src/xml/xml_native_writer.cc index 65e21c02..97daccd4 100644 --- a/src/xml/xml_native_writer.cc +++ b/src/xml/xml_native_writer.cc @@ -84,7 +84,7 @@ XMLElement* mjXWriter::InsertEnd(XMLElement* parent, const char* name) { //---------------------------------- class mjXWriter: one-element writers -------------------------- // write flex -void mjXWriter::OneFlex(XMLElement* elem, mjCFlex* pflex) { +void mjXWriter::OneFlex(XMLElement* elem, const mjCFlex* pflex) { string text; mjCFlex defflex; @@ -152,7 +152,7 @@ void mjXWriter::OneFlex(XMLElement* elem, mjCFlex* pflex) { // write mesh -void mjXWriter::OneMesh(XMLElement* elem, mjCMesh* pmesh, mjCDef* def) { +void mjXWriter::OneMesh(XMLElement* elem, const mjCMesh* pmesh, mjCDef* def) { string text; // regular @@ -188,17 +188,17 @@ void mjXWriter::OneMesh(XMLElement* elem, mjCMesh* pmesh, mjCDef* def) { } // defaults and regular - WriteAttr(elem, "refpos", 3, pmesh->refpos, def->mesh.refpos); - WriteAttr(elem, "refquat", 4, pmesh->refquat, def->mesh.refquat); - WriteAttr(elem, "scale", 3, pmesh->scale, def->mesh.scale); + WriteAttr(elem, "refpos", 3, pmesh->refpos, def->Mesh().refpos); + WriteAttr(elem, "refquat", 4, pmesh->refquat, def->Mesh().refquat); + WriteAttr(elem, "scale", 3, pmesh->scale, def->Mesh().scale); WriteAttrKey(elem, "smoothnormal", bool_map, 2, pmesh->get_smoothnormal(), - def->mesh.get_smoothnormal()); + def->Mesh().get_smoothnormal()); } // write skin -void mjXWriter::OneSkin(XMLElement* elem, mjCSkin* pskin) { +void mjXWriter::OneSkin(XMLElement* elem, const mjCSkin* pskin) { string text; mjCDef mydef; float zero = 0; @@ -208,7 +208,7 @@ void mjXWriter::OneSkin(XMLElement* elem, mjCSkin* pskin) { WriteAttrTxt(elem, "file", pskin->get_file()); WriteAttrTxt(elem, "material", pskin->get_material()); WriteAttrInt(elem, "group", pskin->group, 0); - WriteAttr(elem, "rgba", 4, pskin->rgba, mydef.geom.rgba); + WriteAttr(elem, "rgba", 4, pskin->rgba, mydef.Geom().rgba); WriteAttr(elem, "inflate", 1, &pskin->inflate, &zero); // write data if no file @@ -251,7 +251,7 @@ void mjXWriter::OneSkin(XMLElement* elem, mjCSkin* pskin) { // write material -void mjXWriter::OneMaterial(XMLElement* elem, mjCMaterial* pmat, mjCDef* def) { +void mjXWriter::OneMaterial(XMLElement* elem, const mjCMaterial* pmat, mjCDef* def) { // regular if (!writingdefaults) { WriteAttrTxt(elem, "name", pmat->name); @@ -259,24 +259,24 @@ void mjXWriter::OneMaterial(XMLElement* elem, mjCMaterial* pmat, mjCDef* def) { } // defaults and regular - if (pmat->texture != def->material.texture) { + if (pmat->texture != def->Material().texture) { WriteAttrTxt(elem, "texture", pmat->get_texture()); } - WriteAttrKey(elem, "texuniform", bool_map, 2, pmat->texuniform, def->material.texuniform); - WriteAttr(elem, "texrepeat", 2, pmat->texrepeat, def->material.texrepeat); - WriteAttr(elem, "emission", 1, &pmat->emission, &def->material.emission); - WriteAttr(elem, "specular", 1, &pmat->specular, &def->material.specular); - WriteAttr(elem, "shininess", 1, &pmat->shininess, &def->material.shininess); - WriteAttr(elem, "reflectance", 1, &pmat->reflectance, &def->material.reflectance); - WriteAttr(elem, "metallic", 1, &pmat->metallic, &def->material.metallic); - WriteAttr(elem, "roughness", 1, &pmat->roughness, &def->material.roughness); - WriteAttr(elem, "rgba", 4, pmat->rgba, def->material.rgba); + WriteAttrKey(elem, "texuniform", bool_map, 2, pmat->texuniform, def->Material().texuniform); + WriteAttr(elem, "texrepeat", 2, pmat->texrepeat, def->Material().texrepeat); + WriteAttr(elem, "emission", 1, &pmat->emission, &def->Material().emission); + WriteAttr(elem, "specular", 1, &pmat->specular, &def->Material().specular); + WriteAttr(elem, "shininess", 1, &pmat->shininess, &def->Material().shininess); + WriteAttr(elem, "reflectance", 1, &pmat->reflectance, &def->Material().reflectance); + WriteAttr(elem, "metallic", 1, &pmat->metallic, &def->Material().metallic); + WriteAttr(elem, "roughness", 1, &pmat->roughness, &def->Material().roughness); + WriteAttr(elem, "rgba", 4, pmat->rgba, def->Material().rgba); } // write joint -void mjXWriter::OneJoint(XMLElement* elem, mjCJoint* pjoint, mjCDef* def) { +void mjXWriter::OneJoint(XMLElement* elem, const mjCJoint* pjoint, mjCDef* def) { double zero = 0; // regular @@ -292,42 +292,42 @@ void mjXWriter::OneJoint(XMLElement* elem, mjCJoint* pjoint, mjCDef* def) { } // defaults and regular - if (pjoint->type != def->joint.type) { + if (pjoint->type != def->Joint().type) { WriteAttrTxt(elem, "type", FindValue(joint_map, joint_sz, pjoint->type)); } - WriteAttrInt(elem, "group", pjoint->group, def->joint.group); + WriteAttrInt(elem, "group", pjoint->group, def->Joint().group); WriteAttr(elem, "ref", 1, &pjoint->ref, &zero); WriteAttr(elem, "springref", 1, &pjoint->springref, &zero); - WriteAttr(elem, "solreflimit", mjNREF, pjoint->solref_limit, def->joint.solref_limit, true); - WriteAttr(elem, "solimplimit", mjNIMP, pjoint->solimp_limit, def->joint.solimp_limit, true); - WriteAttr(elem, "solreffriction", mjNREF, pjoint->solref_friction, def->joint.solref_friction, + WriteAttr(elem, "solreflimit", mjNREF, pjoint->solref_limit, def->Joint().solref_limit, true); + WriteAttr(elem, "solimplimit", mjNIMP, pjoint->solimp_limit, def->Joint().solimp_limit, true); + WriteAttr(elem, "solreffriction", mjNREF, pjoint->solref_friction, def->Joint().solref_friction, true); - WriteAttr(elem, "solimpfriction", mjNIMP, pjoint->solimp_friction, def->joint.solimp_friction, + WriteAttr(elem, "solimpfriction", mjNIMP, pjoint->solimp_friction, def->Joint().solimp_friction, true); - WriteAttr(elem, "stiffness", 1, &pjoint->stiffness, &def->joint.stiffness); - WriteAttrKey(elem, "limited", TFAuto_map, 3, pjoint->limited, def->joint.limited); - WriteAttr(elem, "range", 2, pjoint->range, def->joint.range); + WriteAttr(elem, "stiffness", 1, &pjoint->stiffness, &def->Joint().stiffness); + WriteAttrKey(elem, "limited", TFAuto_map, 3, pjoint->limited, def->Joint().limited); + WriteAttr(elem, "range", 2, pjoint->range, def->Joint().range); WriteAttrKey(elem, "actuatorfrclimited", TFAuto_map, 3, pjoint->actfrclimited, - def->joint.actfrclimited); - WriteAttrKey(elem, "actuatorgravcomp", bool_map, 2, pjoint->actgravcomp, def->joint.actgravcomp); - WriteAttr(elem, "actuatorfrcrange", 2, pjoint->actfrcrange, def->joint.actfrcrange); - WriteAttr(elem, "margin", 1, &pjoint->margin, &def->joint.margin); - WriteAttr(elem, "armature", 1, &pjoint->armature, &def->joint.armature); - WriteAttr(elem, "damping", 1, &pjoint->damping, &def->joint.damping); - WriteAttr(elem, "frictionloss", 1, &pjoint->frictionloss, &def->joint.frictionloss); + def->Joint().actfrclimited); + WriteAttrKey(elem, "actuatorgravcomp", bool_map, 2, pjoint->actgravcomp, def->Joint().actgravcomp); + WriteAttr(elem, "actuatorfrcrange", 2, pjoint->actfrcrange, def->Joint().actfrcrange); + WriteAttr(elem, "margin", 1, &pjoint->margin, &def->Joint().margin); + WriteAttr(elem, "armature", 1, &pjoint->armature, &def->Joint().armature); + WriteAttr(elem, "damping", 1, &pjoint->damping, &def->Joint().damping); + WriteAttr(elem, "frictionloss", 1, &pjoint->frictionloss, &def->Joint().frictionloss); // userdata if (writingdefaults) { WriteVector(elem, "user", pjoint->get_userdata()); } else { - WriteVector(elem, "user", pjoint->get_userdata(), def->joint.get_userdata()); + WriteVector(elem, "user", pjoint->get_userdata(), def->Joint().get_userdata()); } } // write geom -void mjXWriter::OneGeom(XMLElement* elem, mjCGeom* pgeom, mjCDef* def) { +void mjXWriter::OneGeom(XMLElement* elem, const mjCGeom* pgeom, mjCDef* def) { double unitq[4] = {1, 0, 0, 0}; double mass = 0; @@ -336,10 +336,10 @@ void mjXWriter::OneGeom(XMLElement* elem, mjCGeom* pgeom, mjCDef* def) { WriteAttrTxt(elem, "name", pgeom->name); WriteAttrTxt(elem, "class", pgeom->classname); if (mjGEOMINFO[pgeom->type]) { - WriteAttr(elem, "size", mjGEOMINFO[pgeom->type], pgeom->size, def->geom.size); + WriteAttr(elem, "size", mjGEOMINFO[pgeom->type], pgeom->size, def->Geom().size); } if (mjuu_defined(pgeom->mass)) { - mass = pgeom->GetVolume() * def->geom.density; + mass = pgeom->GetVolume() * def->Geom().density; } // mesh geom @@ -368,35 +368,35 @@ void mjXWriter::OneGeom(XMLElement* elem, mjCGeom* pgeom, mjCDef* def) { WriteAttr(elem, "quat", 4, pgeom->quat, unitq); } } else { - WriteAttr(elem, "size", 3, pgeom->size, def->geom.size); + WriteAttr(elem, "size", 3, pgeom->size, def->Geom().size); } // defaults and regular - WriteAttrKey(elem, "type", geom_map, mjNGEOMTYPES, pgeom->type, def->geom.type); - WriteAttrInt(elem, "contype", pgeom->contype, def->geom.contype); - WriteAttrInt(elem, "conaffinity", pgeom->conaffinity, def->geom.conaffinity); - WriteAttrInt(elem, "condim", pgeom->condim, def->geom.condim); - WriteAttrInt(elem, "group", pgeom->group, def->geom.group); - WriteAttrInt(elem, "priority", pgeom->priority, def->geom.priority); - WriteAttr(elem, "friction", 3, pgeom->friction, def->geom.friction, true); - WriteAttr(elem, "solmix", 1, &pgeom->solmix, &def->geom.solmix); - WriteAttr(elem, "solref", mjNREF, pgeom->solref, def->geom.solref, true); - WriteAttr(elem, "solimp", mjNIMP, pgeom->solimp, def->geom.solimp, true); - WriteAttr(elem, "margin", 1, &pgeom->margin, &def->geom.margin); - WriteAttr(elem, "gap", 1, &pgeom->gap, &def->geom.gap); - WriteAttr(elem, "gap", 1, &pgeom->gap, &def->geom.gap); - WriteAttrKey(elem, "fluidshape", fluid_map, 2, pgeom->fluid_ellipsoid, def->geom.fluid_ellipsoid); - WriteAttr(elem, "fluidcoef", 5, pgeom->fluid_coefs, def->geom.fluid_coefs); - WriteAttrKey(elem, "shellinertia", meshtype_map, 2, pgeom->typeinertia, def->geom.typeinertia); + WriteAttrKey(elem, "type", geom_map, mjNGEOMTYPES, pgeom->type, def->Geom().type); + WriteAttrInt(elem, "contype", pgeom->contype, def->Geom().contype); + WriteAttrInt(elem, "conaffinity", pgeom->conaffinity, def->Geom().conaffinity); + WriteAttrInt(elem, "condim", pgeom->condim, def->Geom().condim); + WriteAttrInt(elem, "group", pgeom->group, def->Geom().group); + WriteAttrInt(elem, "priority", pgeom->priority, def->Geom().priority); + WriteAttr(elem, "friction", 3, pgeom->friction, def->Geom().friction, true); + WriteAttr(elem, "solmix", 1, &pgeom->solmix, &def->Geom().solmix); + WriteAttr(elem, "solref", mjNREF, pgeom->solref, def->Geom().solref, true); + WriteAttr(elem, "solimp", mjNIMP, pgeom->solimp, def->Geom().solimp, true); + WriteAttr(elem, "margin", 1, &pgeom->margin, &def->Geom().margin); + WriteAttr(elem, "gap", 1, &pgeom->gap, &def->Geom().gap); + WriteAttr(elem, "gap", 1, &pgeom->gap, &def->Geom().gap); + WriteAttrKey(elem, "fluidshape", fluid_map, 2, pgeom->fluid_ellipsoid, def->Geom().fluid_ellipsoid); + WriteAttr(elem, "fluidcoef", 5, pgeom->fluid_coefs, def->Geom().fluid_coefs); + WriteAttrKey(elem, "shellinertia", meshtype_map, 2, pgeom->typeinertia, def->Geom().typeinertia); if (mjuu_defined(pgeom->mass)) { WriteAttr(elem, "mass", 1, &pgeom->mass_, &mass); } else { - WriteAttr(elem, "density", 1, &pgeom->density, &def->geom.density); + WriteAttr(elem, "density", 1, &pgeom->density, &def->Geom().density); } - if (pgeom->get_material() != def->geom.get_material()) { + if (pgeom->get_material() != def->Geom().get_material()) { WriteAttrTxt(elem, "material", pgeom->get_material()); } - WriteAttr(elem, "rgba", 4, pgeom->rgba, def->geom.rgba); + WriteAttr(elem, "rgba", 4, pgeom->rgba, def->Geom().rgba); // hfield and mesh attributes if (pgeom->type==mjGEOM_HFIELD) { @@ -410,7 +410,7 @@ void mjXWriter::OneGeom(XMLElement* elem, mjCGeom* pgeom, mjCDef* def) { if (writingdefaults) { WriteVector(elem, "user", pgeom->get_userdata()); } else { - WriteVector(elem, "user", pgeom->get_userdata(), def->geom.get_userdata()); + WriteVector(elem, "user", pgeom->get_userdata(), def->Geom().get_userdata()); } // write plugin @@ -422,7 +422,7 @@ void mjXWriter::OneGeom(XMLElement* elem, mjCGeom* pgeom, mjCDef* def) { // write site -void mjXWriter::OneSite(XMLElement* elem, mjCSite* psite, mjCDef* def) { +void mjXWriter::OneSite(XMLElement* elem, const mjCSite* psite, mjCDef* def) { double unitq[4] = {1, 0, 0, 0}; // regular @@ -432,32 +432,32 @@ void mjXWriter::OneSite(XMLElement* elem, mjCSite* psite, mjCDef* def) { WriteAttr(elem, "pos", 3, psite->pos); WriteAttr(elem, "quat", 4, psite->quat, unitq); if (mjGEOMINFO[psite->type]) { - WriteAttr(elem, "size", mjGEOMINFO[psite->type], psite->size, def->site.size); + WriteAttr(elem, "size", mjGEOMINFO[psite->type], psite->size, def->Site().size); } } else { - WriteAttr(elem, "size", 3, psite->size, def->site.size); + WriteAttr(elem, "size", 3, psite->size, def->Site().size); } // defaults and regular - WriteAttrInt(elem, "group", psite->group, def->site.group); - WriteAttrKey(elem, "type", geom_map, mjNGEOMTYPES, psite->type, def->site.type); - if (psite->get_material() != def->site.get_material()) { + WriteAttrInt(elem, "group", psite->group, def->Site().group); + WriteAttrKey(elem, "type", geom_map, mjNGEOMTYPES, psite->type, def->Site().type); + if (psite->get_material() != def->Site().get_material()) { WriteAttrTxt(elem, "material", psite->get_material()); } - WriteAttr(elem, "rgba", 4, psite->rgba, def->site.rgba); + WriteAttr(elem, "rgba", 4, psite->rgba, def->Site().rgba); // userdata if (writingdefaults) { WriteVector(elem, "user", psite->get_userdata()); } else { - WriteVector(elem, "user", psite->get_userdata(), def->site.get_userdata()); + WriteVector(elem, "user", psite->get_userdata(), def->Site().get_userdata()); } } // write camera -void mjXWriter::OneCamera(XMLElement* elem, mjCCamera* pcam, mjCDef* def) { +void mjXWriter::OneCamera(XMLElement* elem, const mjCCamera* pcam, mjCDef* def) { double unitq[4] = {1, 0, 0, 0}; // regular @@ -470,36 +470,36 @@ void mjXWriter::OneCamera(XMLElement* elem, mjCCamera* pcam, mjCDef* def) { } // defaults and regular - WriteAttr(elem, "ipd", 1, &pcam->ipd, &def->camera.ipd); - WriteAttrKey(elem, "mode", camlight_map, camlight_sz, pcam->mode, def->camera.mode); - WriteAttr(elem, "resolution", 2, pcam->resolution, def->camera.resolution); + WriteAttr(elem, "ipd", 1, &pcam->ipd, &def->Camera().ipd); + WriteAttrKey(elem, "mode", camlight_map, camlight_sz, pcam->mode, def->Camera().mode); + WriteAttr(elem, "resolution", 2, pcam->resolution, def->Camera().resolution); // resolution if positive - WriteAttr(elem, "resolution", 2, pcam->resolution, def->camera.resolution); + WriteAttr(elem, "resolution", 2, pcam->resolution, def->Camera().resolution); // camera intrinsics if specified if (pcam->sensor_size[0]>0 && pcam->sensor_size[1]>0) { WriteAttr(elem, "sensorsize", 2, pcam->sensor_size); - WriteAttr(elem, "focal", 2, pcam->focal_length, def->camera.focal_length); - WriteAttr(elem, "focalpixel", 2, pcam->focal_pixel, def->camera.focal_pixel); - WriteAttr(elem, "principal", 2, pcam->principal_length, def->camera.principal_length); - WriteAttr(elem, "principalpixel", 2, pcam->principal_pixel, def->camera.principal_pixel); + WriteAttr(elem, "focal", 2, pcam->focal_length, def->Camera().focal_length); + WriteAttr(elem, "focalpixel", 2, pcam->focal_pixel, def->Camera().focal_pixel); + WriteAttr(elem, "principal", 2, pcam->principal_length, def->Camera().principal_length); + WriteAttr(elem, "principalpixel", 2, pcam->principal_pixel, def->Camera().principal_pixel); } else { - WriteAttr(elem, "fovy", 1, &pcam->fovy, &def->camera.fovy); + WriteAttr(elem, "fovy", 1, &pcam->fovy, &def->Camera().fovy); } // userdata if (writingdefaults) { WriteVector(elem, "user", pcam->get_userdata()); } else { - WriteVector(elem, "user", pcam->get_userdata(), def->camera.get_userdata()); + WriteVector(elem, "user", pcam->get_userdata(), def->Camera().get_userdata()); } } // write light -void mjXWriter::OneLight(XMLElement* elem, mjCLight* plight, mjCDef* def) { +void mjXWriter::OneLight(XMLElement* elem, const mjCLight* plight, mjCDef* def) { // regular if (!writingdefaults) { WriteAttrTxt(elem, "name", plight->name); @@ -510,23 +510,23 @@ void mjXWriter::OneLight(XMLElement* elem, mjCLight* plight, mjCDef* def) { } // defaults and regular - WriteAttr(elem, "bulbradius", 1, &plight->bulbradius, &def->light.bulbradius); - WriteAttrKey(elem, "directional", bool_map, 2, plight->directional, def->light.directional); - WriteAttrKey(elem, "castshadow", bool_map, 2, plight->castshadow, def->light.castshadow); - WriteAttrKey(elem, "active", bool_map, 2, plight->active, def->light.active); - WriteAttr(elem, "attenuation", 3, plight->attenuation, def->light.attenuation); - WriteAttr(elem, "cutoff", 1, &plight->cutoff, &def->light.cutoff); - WriteAttr(elem, "exponent", 1, &plight->exponent, &def->light.exponent); - WriteAttr(elem, "ambient", 3, plight->ambient, def->light.ambient); - WriteAttr(elem, "diffuse", 3, plight->diffuse, def->light.diffuse); - WriteAttr(elem, "specular", 3, plight->specular, def->light.specular); - WriteAttrKey(elem, "mode", camlight_map, camlight_sz, plight->mode, def->light.mode); + WriteAttr(elem, "bulbradius", 1, &plight->bulbradius, &def->Light().bulbradius); + WriteAttrKey(elem, "directional", bool_map, 2, plight->directional, def->Light().directional); + WriteAttrKey(elem, "castshadow", bool_map, 2, plight->castshadow, def->Light().castshadow); + WriteAttrKey(elem, "active", bool_map, 2, plight->active, def->Light().active); + WriteAttr(elem, "attenuation", 3, plight->attenuation, def->Light().attenuation); + WriteAttr(elem, "cutoff", 1, &plight->cutoff, &def->Light().cutoff); + WriteAttr(elem, "exponent", 1, &plight->exponent, &def->Light().exponent); + WriteAttr(elem, "ambient", 3, plight->ambient, def->Light().ambient); + WriteAttr(elem, "diffuse", 3, plight->diffuse, def->Light().diffuse); + WriteAttr(elem, "specular", 3, plight->specular, def->Light().specular); + WriteAttrKey(elem, "mode", camlight_map, camlight_sz, plight->mode, def->Light().mode); } // write pair -void mjXWriter::OnePair(XMLElement* elem, mjCPair* ppair, mjCDef* def) { +void mjXWriter::OnePair(XMLElement* elem, const mjCPair* ppair, mjCDef* def) { // regular if (!writingdefaults) { WriteAttrTxt(elem, "class", ppair->classname); @@ -536,20 +536,20 @@ void mjXWriter::OnePair(XMLElement* elem, mjCPair* ppair, mjCDef* def) { // defaults and regular WriteAttrTxt(elem, "name", ppair->name); - WriteAttrInt(elem, "condim", ppair->condim, def->pair.spec.condim); - WriteAttr(elem, "margin", 1, &ppair->margin, &def->pair.spec.margin); - WriteAttr(elem, "gap", 1, &ppair->gap, &def->pair.spec.gap); - WriteAttr(elem, "solref", mjNREF, ppair->solref, def->pair.spec.solref, true); - WriteAttr(elem, "solreffriction", mjNREF, ppair->solreffriction, def->pair.spec.solreffriction, + WriteAttrInt(elem, "condim", ppair->condim, def->Pair().spec.condim); + WriteAttr(elem, "margin", 1, &ppair->margin, &def->Pair().spec.margin); + WriteAttr(elem, "gap", 1, &ppair->gap, &def->Pair().spec.gap); + WriteAttr(elem, "solref", mjNREF, ppair->solref, def->Pair().spec.solref, true); + WriteAttr(elem, "solreffriction", mjNREF, ppair->solreffriction, def->Pair().spec.solreffriction, true); - WriteAttr(elem, "solimp", mjNIMP, ppair->solimp, def->pair.spec.solimp, true); - WriteAttr(elem, "friction", 5, ppair->friction, def->pair.spec.friction); // all 5 values + WriteAttr(elem, "solimp", mjNIMP, ppair->solimp, def->Pair().spec.solimp, true); + WriteAttr(elem, "friction", 5, ppair->friction, def->Pair().spec.friction); // all 5 values } // write equality -void mjXWriter::OneEquality(XMLElement* elem, mjCEquality* peq, mjCDef* def) { +void mjXWriter::OneEquality(XMLElement* elem, const mjCEquality* peq, mjCDef* def) { // regular if (!writingdefaults) { WriteAttrTxt(elem, "name", peq->name); @@ -592,15 +592,15 @@ void mjXWriter::OneEquality(XMLElement* elem, mjCEquality* peq, mjCDef* def) { } // defaults and regular - WriteAttrKey(elem, "active", bool_map, 2, peq->active, def->equality.active); - WriteAttr(elem, "solref", mjNREF, peq->solref, def->equality.solref, true); - WriteAttr(elem, "solimp", mjNIMP, peq->solimp, def->equality.solimp, true); + WriteAttrKey(elem, "active", bool_map, 2, peq->active, def->Equality().active); + WriteAttr(elem, "solref", mjNREF, peq->solref, def->Equality().solref, true); + WriteAttr(elem, "solimp", mjNIMP, peq->solimp, def->Equality().solimp, true); } // write tendon -void mjXWriter::OneTendon(XMLElement* elem, mjCTendon* pten, mjCDef* def) { +void mjXWriter::OneTendon(XMLElement* elem, const mjCTendon* pten, mjCDef* def) { bool fixed = (pten->GetWrap(0) && pten->GetWrap(0)->type==mjWRAP_JOINT); // regular @@ -610,46 +610,46 @@ void mjXWriter::OneTendon(XMLElement* elem, mjCTendon* pten, mjCDef* def) { } // defaults and regular - WriteAttrInt(elem, "group", pten->group, def->tendon.group); - WriteAttr(elem, "solreflimit", mjNREF, pten->solref_limit, def->tendon.solref_limit, true); - WriteAttr(elem, "solimplimit", mjNIMP, pten->solimp_limit, def->tendon.solimp_limit, true); - WriteAttr(elem, "solreffriction", mjNREF, pten->solref_friction, def->tendon.solref_friction, + WriteAttrInt(elem, "group", pten->group, def->Tendon().group); + WriteAttr(elem, "solreflimit", mjNREF, pten->solref_limit, def->Tendon().solref_limit, true); + WriteAttr(elem, "solimplimit", mjNIMP, pten->solimp_limit, def->Tendon().solimp_limit, true); + WriteAttr(elem, "solreffriction", mjNREF, pten->solref_friction, def->Tendon().solref_friction, true); - WriteAttr(elem, "solimpfriction", mjNIMP, pten->solimp_friction, def->tendon.solimp_friction, + WriteAttr(elem, "solimpfriction", mjNIMP, pten->solimp_friction, def->Tendon().solimp_friction, true); - WriteAttrKey(elem, "limited", TFAuto_map, 3, pten->limited, def->tendon.limited); - WriteAttr(elem, "range", 2, pten->range, def->tendon.range); - WriteAttr(elem, "margin", 1, &pten->margin, &def->tendon.margin); - WriteAttr(elem, "stiffness", 1, &pten->stiffness, &def->tendon.stiffness); - WriteAttr(elem, "damping", 1, &pten->damping, &def->tendon.damping); - WriteAttr(elem, "frictionloss", 1, &pten->frictionloss, &def->tendon.frictionloss); + WriteAttrKey(elem, "limited", TFAuto_map, 3, pten->limited, def->Tendon().limited); + WriteAttr(elem, "range", 2, pten->range, def->Tendon().range); + WriteAttr(elem, "margin", 1, &pten->margin, &def->Tendon().margin); + WriteAttr(elem, "stiffness", 1, &pten->stiffness, &def->Tendon().stiffness); + WriteAttr(elem, "damping", 1, &pten->damping, &def->Tendon().damping); + WriteAttr(elem, "frictionloss", 1, &pten->frictionloss, &def->Tendon().frictionloss); if (pten->springlength[0] != pten->springlength[1] || - def->tendon.springlength[0] != def->tendon.springlength[1]) { - WriteAttr(elem, "springlength", 2, pten->springlength, def->tendon.springlength); + def->Tendon().springlength[0] != def->Tendon().springlength[1]) { + WriteAttr(elem, "springlength", 2, pten->springlength, def->Tendon().springlength); } else { - WriteAttr(elem, "springlength", 1, pten->springlength, def->tendon.springlength); + WriteAttr(elem, "springlength", 1, pten->springlength, def->Tendon().springlength); } // spatial only if (!fixed) { - if (pten->get_material()!=def->tendon.get_material()) { + if (pten->get_material()!=def->Tendon().get_material()) { WriteAttrTxt(elem, "material", pten->get_material()); } - WriteAttr(elem, "width", 1, &pten->width, &def->tendon.width); - WriteAttr(elem, "rgba", 4, pten->rgba, def->tendon.rgba); + WriteAttr(elem, "width", 1, &pten->width, &def->Tendon().width); + WriteAttr(elem, "rgba", 4, pten->rgba, def->Tendon().rgba); } // userdata if (writingdefaults) { WriteVector(elem, "user", pten->get_userdata()); } else { - WriteVector(elem, "user", pten->get_userdata(), def->tendon.get_userdata()); + WriteVector(elem, "user", pten->get_userdata(), def->Tendon().get_userdata()); } } // write actuator -void mjXWriter::OneActuator(XMLElement* elem, mjCActuator* pact, mjCDef* def) { +void mjXWriter::OneActuator(XMLElement* elem, const mjCActuator* pact, mjCDef* def) { // regular if (!writingdefaults) { WriteAttrTxt(elem, "name", pact->name); @@ -689,20 +689,20 @@ void mjXWriter::OneActuator(XMLElement* elem, mjCActuator* pact, mjCDef* def) { } // defaults and regular - WriteAttrInt(elem, "group", pact->group, def->actuator.group); - WriteAttrKey(elem, "ctrllimited", TFAuto_map, 3, pact->ctrllimited, def->actuator.ctrllimited); - WriteAttr(elem, "ctrlrange", 2, pact->ctrlrange, def->actuator.ctrlrange); - WriteAttrKey(elem, "forcelimited", TFAuto_map, 3, pact->forcelimited, def->actuator.forcelimited); - WriteAttr(elem, "forcerange", 2, pact->forcerange, def->actuator.forcerange); - WriteAttrKey(elem, "actlimited", TFAuto_map, 3, pact->actlimited, def->actuator.actlimited); - WriteAttr(elem, "actrange", 2, pact->actrange, def->actuator.actrange); - WriteAttr(elem, "lengthrange", 2, pact->lengthrange, def->actuator.lengthrange); - WriteAttr(elem, "gear", 6, pact->gear, def->actuator.gear); - WriteAttr(elem, "cranklength", 1, &pact->cranklength, &def->actuator.cranklength); + WriteAttrInt(elem, "group", pact->group, def->Actuator().group); + WriteAttrKey(elem, "ctrllimited", TFAuto_map, 3, pact->ctrllimited, def->Actuator().ctrllimited); + WriteAttr(elem, "ctrlrange", 2, pact->ctrlrange, def->Actuator().ctrlrange); + WriteAttrKey(elem, "forcelimited", TFAuto_map, 3, pact->forcelimited, def->Actuator().forcelimited); + WriteAttr(elem, "forcerange", 2, pact->forcerange, def->Actuator().forcerange); + WriteAttrKey(elem, "actlimited", TFAuto_map, 3, pact->actlimited, def->Actuator().actlimited); + WriteAttr(elem, "actrange", 2, pact->actrange, def->Actuator().actrange); + WriteAttr(elem, "lengthrange", 2, pact->lengthrange, def->Actuator().lengthrange); + WriteAttr(elem, "gear", 6, pact->gear, def->Actuator().gear); + WriteAttr(elem, "cranklength", 1, &pact->cranklength, &def->Actuator().cranklength); WriteAttrKey(elem, "actearly", bool_map, 2, pact->actearly, - def->actuator.actearly); - WriteAttrKey(elem, "dyntype", dyn_map, dyn_sz, pact->dyntype, def->actuator.dyntype); - WriteAttr(elem, "dynprm", mjNDYN, pact->dynprm, def->actuator.dynprm); + def->Actuator().actearly); + WriteAttrKey(elem, "dyntype", dyn_map, dyn_sz, pact->dyntype, def->Actuator().dyntype); + WriteAttr(elem, "dynprm", mjNDYN, pact->dynprm, def->Actuator().dynprm); // plugins: write config attributes if (pact->plugin.active) { @@ -713,29 +713,29 @@ void mjXWriter::OneActuator(XMLElement* elem, mjCActuator* pact, mjCDef* def) { else { // special handling of actdim which has default value of -1 if (writingdefaults) { - WriteAttrInt(elem, "actdim", pact->actdim, def->actuator.actdim); + WriteAttrInt(elem, "actdim", pact->actdim, def->Actuator().actdim); } else { int default_actdim = pact->dyntype == mjDYN_NONE ? 0 : 1; WriteAttrInt(elem, "actdim", pact->actdim, default_actdim); } - WriteAttrKey(elem, "gaintype", gain_map, gain_sz, pact->gaintype, def->actuator.gaintype); - WriteAttrKey(elem, "biastype", bias_map, bias_sz, pact->biastype, def->actuator.biastype); - WriteAttr(elem, "gainprm", mjNGAIN, pact->gainprm, def->actuator.gainprm, true); - WriteAttr(elem, "biasprm", mjNBIAS, pact->biasprm, def->actuator.biasprm, true); + WriteAttrKey(elem, "gaintype", gain_map, gain_sz, pact->gaintype, def->Actuator().gaintype); + WriteAttrKey(elem, "biastype", bias_map, bias_sz, pact->biastype, def->Actuator().biastype); + WriteAttr(elem, "gainprm", mjNGAIN, pact->gainprm, def->Actuator().gainprm, true); + WriteAttr(elem, "biasprm", mjNBIAS, pact->biasprm, def->Actuator().biasprm, true); } // userdata if (writingdefaults) { WriteVector(elem, "user", pact->get_userdata()); } else { - WriteVector(elem, "user", pact->get_userdata(), def->actuator.get_userdata()); + WriteVector(elem, "user", pact->get_userdata(), def->Actuator().get_userdata()); } } // write plugin -void mjXWriter::OnePlugin(XMLElement* elem, mjsPlugin* plugin) { +void mjXWriter::OnePlugin(XMLElement* elem, const mjsPlugin* plugin) { const std::string instance_name = std::string(mjs_getString(plugin->instance_name)); const std::string plugin_name = std::string(mjs_getString(plugin->name)); if (!instance_name.empty()) { @@ -806,7 +806,7 @@ string mjXWriter::Write(char *error, size_t error_sz) { Visual(root); Statistic(root); writingdefaults = true; - Default(root, model->Defaults()[0]); + Default(root, model->Defaults(0)); writingdefaults = false; Extension(root); Custom(root); @@ -1136,7 +1136,7 @@ void mjXWriter::Default(XMLElement* root, mjCDef* def) { // pointer to parent defaults mjCDef* par; if (def->parentid>=0) { - par = model->Defaults()[def->parentid]; + par = model->Defaults(def->parentid); } else { par = new mjCDef; } @@ -1147,57 +1147,57 @@ void mjXWriter::Default(XMLElement* root, mjCDef* def) { // mesh elem = InsertEnd(section, "mesh"); - OneMesh(elem, &def->mesh, par); + OneMesh(elem, &def->Mesh(), par); if (!elem->FirstAttribute()) section->DeleteChild(elem); // material elem = InsertEnd(section, "material"); - OneMaterial(elem, &def->material, par); + OneMaterial(elem, &def->Material(), par); if (!elem->FirstAttribute()) section->DeleteChild(elem); // joint elem = InsertEnd(section, "joint"); - OneJoint(elem, &def->joint, par); + OneJoint(elem, &def->Joint(), par); if (!elem->FirstAttribute()) section->DeleteChild(elem); // geom elem = InsertEnd(section, "geom"); - OneGeom(elem, &def->geom, par); + OneGeom(elem, &def->Geom(), par); if (!elem->FirstAttribute()) section->DeleteChild(elem); // site elem = InsertEnd(section, "site"); - OneSite(elem, &def->site, par); + OneSite(elem, &def->Site(), par); if (!elem->FirstAttribute()) section->DeleteChild(elem); // camera elem = InsertEnd(section, "camera"); - OneCamera(elem, &def->camera, par); + OneCamera(elem, &def->Camera(), par); if (!elem->FirstAttribute()) section->DeleteChild(elem); // light elem = InsertEnd(section, "light"); - OneLight(elem, &def->light, par); + OneLight(elem, &def->Light(), par); if (!elem->FirstAttribute()) section->DeleteChild(elem); // pair elem = InsertEnd(section, "pair"); - OnePair(elem, &def->pair, par); + OnePair(elem, &def->Pair(), par); if (!elem->FirstAttribute()) section->DeleteChild(elem); // equality elem = InsertEnd(section, "equality"); - OneEquality(elem, &def->equality, par); + OneEquality(elem, &def->Equality(), par); if (!elem->FirstAttribute()) section->DeleteChild(elem); // tendon elem = InsertEnd(section, "tendon"); - OneTendon(elem, &def->tendon, par); + OneTendon(elem, &def->Tendon(), par); if (!elem->FirstAttribute()) section->DeleteChild(elem); // actuator elem = InsertEnd(section, "general"); - OneActuator(elem, &def->actuator, par); + OneActuator(elem, &def->Actuator(), par); if (!elem->FirstAttribute()) section->DeleteChild(elem); // if top-level class has no members or children, delete it and return @@ -1209,7 +1209,7 @@ void mjXWriter::Default(XMLElement* root, mjCDef* def) { // add children recursively for (int i=0; i<(int)def->childid.size(); i++) { - Default(section, model->Defaults()[def->childid[i]]); + Default(section, model->Defaults(def->childid[i])); } // delete parent defaults if allocated here @@ -1677,7 +1677,7 @@ void mjXWriter::Tendon(XMLElement* root) { // write wraps XMLElement* wrap; for (int j=0; jNumWraps(); j++) { - mjCWrap* pw = pten->GetWrap(j); + const mjCWrap* pw = pten->GetWrap(j); switch (pw->type) { case mjWRAP_JOINT: wrap = InsertEnd(elem, "joint"); diff --git a/src/xml/xml_native_writer.h b/src/xml/xml_native_writer.h index f2881e18..063fddca 100644 --- a/src/xml/xml_native_writer.h +++ b/src/xml/xml_native_writer.h @@ -59,20 +59,20 @@ class mjXWriter : public mjXBase { void Keyframe(tinyxml2::XMLElement* root); // keyframe section // single element writers, used in defaults and main body - void OneFlex(tinyxml2::XMLElement* elem, mjCFlex* pflex); - void OneMesh(tinyxml2::XMLElement* elem, mjCMesh* pmesh, mjCDef* def); - void OneSkin(tinyxml2::XMLElement* elem, mjCSkin* pskin); - void OneMaterial(tinyxml2::XMLElement* elem, mjCMaterial* pmaterial, mjCDef* def); - void OneJoint(tinyxml2::XMLElement* elem, mjCJoint* pjoint, mjCDef* def); - void OneGeom(tinyxml2::XMLElement* elem, mjCGeom* pgeom, mjCDef* def); - void OneSite(tinyxml2::XMLElement* elem, mjCSite* psite, mjCDef* def); - void OneCamera(tinyxml2::XMLElement* elem, mjCCamera* pcamera, mjCDef* def); - void OneLight(tinyxml2::XMLElement* elem, mjCLight* plight, mjCDef* def); - void OnePair(tinyxml2::XMLElement* elem, mjCPair* ppair, mjCDef* def); - void OneEquality(tinyxml2::XMLElement* elem, mjCEquality* pequality, mjCDef* def); - void OneTendon(tinyxml2::XMLElement* elem, mjCTendon* ptendon, mjCDef* def); - void OneActuator(tinyxml2::XMLElement* elem, mjCActuator* pactuator, mjCDef* def); - void OnePlugin(tinyxml2::XMLElement* elem, mjsPlugin* plugin); + void OneFlex(tinyxml2::XMLElement* elem, const mjCFlex* pflex); + void OneMesh(tinyxml2::XMLElement* elem, const mjCMesh* pmesh, mjCDef* def); + void OneSkin(tinyxml2::XMLElement* elem, const mjCSkin* pskin); + void OneMaterial(tinyxml2::XMLElement* elem, const mjCMaterial* pmaterial, mjCDef* def); + void OneJoint(tinyxml2::XMLElement* elem, const mjCJoint* pjoint, mjCDef* def); + void OneGeom(tinyxml2::XMLElement* elem, const mjCGeom* pgeom, mjCDef* def); + void OneSite(tinyxml2::XMLElement* elem, const mjCSite* psite, mjCDef* def); + void OneCamera(tinyxml2::XMLElement* elem, const mjCCamera* pcamera, mjCDef* def); + void OneLight(tinyxml2::XMLElement* elem, const mjCLight* plight, mjCDef* def); + void OnePair(tinyxml2::XMLElement* elem, const mjCPair* ppair, mjCDef* def); + void OneEquality(tinyxml2::XMLElement* elem, const mjCEquality* pequality, mjCDef* def); + void OneTendon(tinyxml2::XMLElement* elem, const mjCTendon* ptendon, mjCDef* def); + void OneActuator(tinyxml2::XMLElement* elem, const mjCActuator* pactuator, mjCDef* def); + void OnePlugin(tinyxml2::XMLElement* elem, const mjsPlugin* plugin); tinyxml2::XMLElement* OneFrame(tinyxml2::XMLElement* elem, mjCFrame* frame); bool writingdefaults; // true during defaults write