Clean up mjCModel definition.

PiperOrigin-RevId: 611102699
Change-Id: I64a3927c30f16764742966f4c5e1e0e579af5f50
This commit is contained in:
Kyle Bayes
2024-02-28 07:57:30 -08:00
committed by Copybara-Service
parent 789188d51d
commit b2bd377bad
+60 -65
View File
@@ -15,6 +15,7 @@
#ifndef MUJOCO_SRC_USER_USER_MODEL_H_ #ifndef MUJOCO_SRC_USER_USER_MODEL_H_
#define MUJOCO_SRC_USER_USER_MODEL_H_ #define MUJOCO_SRC_USER_USER_MODEL_H_
#include <array>
#include <functional> #include <functional>
#include <map> #include <map>
#include <string> #include <string>
@@ -32,16 +33,12 @@
typedef std::map<std::string, int, std::less<> > mjKeyMap; typedef std::map<std::string, int, std::less<> > mjKeyMap;
typedef std::array<mjKeyMap, mjNOBJECT> mjListKeyMap; typedef std::array<mjKeyMap, mjNOBJECT> mjListKeyMap;
//---------------------------------- class mjCModel ------------------------------------------------
// mjCModel contains everything needed to generate the low-level model. // mjCModel contains everything needed to generate the low-level model.
// It can be constructed manually by calling 'Add' functions and setting // It can be constructed manually by calling 'Add' functions and setting
// the public fields of the various objects. Alternatively it can constructed // the public fields of the various objects. Alternatively it can constructed
// by loading an XML file via mjCXML. Once an mjCModel object is // by loading an XML file via mjCXML. Once an mjCModel object is
// constructed, 'Compile' can be called to generate the corresponding mjModel object // constructed, 'Compile' can be called to generate the corresponding mjModel object
// (which is the low-level model). The mjCModel object can then be deleted. // (which is the low-level model). The mjCModel object can then be deleted.
class mjCModel : private mjmModel { class mjCModel : private mjmModel {
friend class mjCBody; friend class mjCBody;
friend class mjCCamera; friend class mjCCamera;
@@ -63,98 +60,99 @@ class mjCModel : private mjmModel {
friend class mjXWriter; friend class mjXWriter;
public: public:
mjCModel(); // constructor mjCModel();
~mjCModel(); // destructor ~mjCModel();
void CopyFromSpec(); // copy spec to private attributes void CopyFromSpec(); // copy spec to private attributes
void PointToLocal(); void PointToLocal();
mjmModel spec; mjmModel spec;
mjModel* Compile(const mjVFS* vfs = 0); // COMPILER: construct mjModel mjModel* Compile(const mjVFS* vfs = nullptr); // construct mjModel
bool CopyBack(const mjModel*); // DECOMPILER: copy numeric back bool CopyBack(const mjModel*); // DECOMPILER: copy numeric back
void FuseStatic(void); // fuse static bodies with parent void FuseStatic(); // fuse static bodies with parent
void FuseReindex(mjCBody* body); // reindex elements during fuse void FuseReindex(mjCBody* body); // reindex elements during fuse
// API for adding model elements
mjCFlex* AddFlex();
mjCMesh* AddMesh(mjCDef* def = nullptr);
mjCSkin* AddSkin();
mjCHField* AddHField();
mjCTexture* AddTexture();
mjCMaterial* AddMaterial(mjCDef* def = nullptr);
mjCPair* AddPair(mjCDef* def = nullptr); // geom pair for inclusion
mjCBodyPair* AddExclude(); // body pair for exclusion
mjCEquality* AddEquality(mjCDef* def = nullptr); // equality constraint
mjCTendon* AddTendon(mjCDef* def = nullptr);
mjCActuator* AddActuator(mjCDef* def = nullptr);
mjCSensor* AddSensor();
mjCNumeric* AddNumeric();
mjCText* AddText();
mjCTuple* AddTuple();
mjCKey* AddKey();
mjCPlugin* AddPlugin();
//------------------------ API for adding model elements // delete elements marked as discard=true
mjCFlex* AddFlex(void); // flex template <class T> void Delete(std::vector<T*>& elements,
mjCMesh* AddMesh(mjCDef* def = 0); // mesh const std::vector<bool>& discard);
mjCSkin* AddSkin(void); // skin
mjCHField* AddHField(void); // heightfield
mjCTexture* AddTexture(void); // texture
mjCMaterial* AddMaterial(mjCDef* def = 0); // material
mjCPair* AddPair(mjCDef* def = 0); // geom pair for inclusion
mjCBodyPair* AddExclude(void); // body pair for exclusion
mjCEquality* AddEquality(mjCDef* def = 0); // equality constraint
mjCTendon* AddTendon(mjCDef* def = 0); // tendon
mjCActuator* AddActuator(mjCDef* def = 0); // actuator
mjCSensor* AddSensor(void); // sensor
mjCNumeric* AddNumeric(void); // custom numeric
mjCText* AddText(void); // custom text
mjCTuple* AddTuple(void); // custom tuple
mjCKey* AddKey(void); // keyframe
mjCPlugin* AddPlugin(void); // plugin instance
//------------------------ API for deleting model elements // delete all elements
template <class T> template <class T> void DeleteAll(std::vector<T*>& elements);
void Delete(std::vector<T*>& elements,
const std::vector<bool>& discard); // delete elements marked as discard=true
template <class T> // API for access to model elements (outside tree)
void DeleteAll(std::vector<T*>& elements); // delete all elements
//------------------------ API for access to model elements (outside tree)
int NumObjects(mjtObj type); // number of objects in specified list int NumObjects(mjtObj type); // number of objects in specified list
mjCBase* GetObject(mjtObj type, int id); // pointer to specified object mjCBase* GetObject(mjtObj type, int id); // pointer to specified object
//------------------------ API for access to other variables // API for access to other variables
bool IsCompiled(void); // is model already compiled bool IsCompiled(); // is model already compiled
int GetFixed(void); // number of fixed massless bodies int GetFixed(); // number of fixed massless bodies
mjCError GetError(void); // copy of error object mjCError GetError(); // copy of error object
mjCBody* GetWorld(void); // pointer to world body mjCBody* GetWorld(); // pointer to world body
mjCDef* FindDef(std::string name); // find default class name mjCDef* FindDef(std::string name); // find default class name
mjCDef* AddDef(std::string name, int parentid); // add default class to array mjCDef* AddDef(std::string name, int parentid); // add default class to array
mjCBase* FindObject(mjtObj type, std::string name); // find object given type and name mjCBase* FindObject(mjtObj type, std::string name); // find object given type and name
bool IsNullPose(const mjtNum* pos, const mjtNum* quat); // detect null pose bool IsNullPose(const mjtNum* pos, const mjtNum* quat); // detect null pose
//------------------------ getters // accessors
std::string get_meshdir(void) const { return meshdir_; } std::string get_meshdir(void) const { return meshdir_; }
std::string get_texturedir(void) const { return texturedir_; } std::string get_texturedir(void) const { return texturedir_; }
//------------------------ API for plugins // resolve plugin instance, create a new one if needed
void ResolvePlugin(mjCBase* obj, // resolve plugin instance, create a new one if needed void ResolvePlugin(mjCBase* obj, const std::string& plugin_name,
const std::string& plugin_name,
const std::string& plugin_instance_name, const std::string& plugin_instance_name,
mjCPlugin** plugin_instance); mjCPlugin** plugin_instance);
// settings for each defaults class
std::vector<mjCDef*> defaults;
//------------------------ global data // list of active plugins
std::vector<mjCDef*> defaults; // settings for each defaults class std::vector<std::pair<const mjpPlugin*, int>> active_plugins;
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);
mjModel* _Compile(const mjVFS* vfs); mjModel* _Compile(const mjVFS* vfs);
void Clear(void); // clear objects allocated by Compile // clear objects allocated by Compile
void Clear(void);
template <class T> // add object of any type // add object of any type
T* AddObject(std::vector<T*>& list, std::string type); template <class T> T* AddObject(std::vector<T*>& list, std::string type);
template <class T> // add object of any type, with def parameter // add object of any type, with def parameter
T* AddObjectDef(std::vector<T*>& list, std::string type, mjCDef* def); template <class T> T* AddObjectDef(std::vector<T*>& list, std::string type,
mjCDef* def);
template<class T> // if asset name is missing, set to filename // if asset name is missing, set to filename
void SetDefaultNames(std::vector<T*>& assets); template<class T> void SetDefaultNames(std::vector<T*>& assets);
template <class T> // delete material from object // delete material from object
void DeleteMaterial(std::vector<T*>& list, std::string_view name = ""); template <class T> void DeleteMaterial(std::vector<T*>& list,
std::string_view name = "");
//------------------------ compile phases // compile phases
void MakeLists(mjCBody* body); // make lists of bodies, geoms, joints, sites void MakeLists(mjCBody* body); // make lists of bodies, geoms, joints, sites
void IndexAssets(bool discard); // convert asset names into indices void IndexAssets(bool discard); // convert asset names into indices
void CheckEmptyNames(void); // check empty names void CheckEmptyNames(); // check empty names
void SetSizes(void); // compute sizes void SetSizes(); // compute sizes
void AutoSpringDamper(mjModel*); // automatic stiffness and damping computation void AutoSpringDamper(mjModel*); // automatic stiffness and damping computation
void LengthRange(mjModel*, mjData*); // compute actuator lengthrange void LengthRange(mjModel*, mjData*); // compute actuator lengthrange
void CopyNames(mjModel*); // copy names, compute name addresses void CopyNames(mjModel*); // copy names, compute name addresses
@@ -162,7 +160,6 @@ class mjCModel : private mjmModel {
void CopyObjects(mjModel*); // copy objects outside kinematic tree void CopyObjects(mjModel*); // copy objects outside kinematic tree
void CopyTree(mjModel*); // copy objects inside kinematic tree void CopyTree(mjModel*); // copy objects inside kinematic tree
//------------------------ sizes
// sizes set from object list lengths // sizes set from object list lengths
int nbody; // number of bodies int nbody; // number of bodies
int njnt; // number of joints int njnt; // number of joints
@@ -226,7 +223,6 @@ class mjCModel : private mjmModel {
int nD; // number of non-zeros in sparse dof-dof matrix int nD; // number of non-zeros in sparse dof-dof matrix
int nB; // number of non-zeros in sparse body-dof matrix int nB; // number of non-zeros in sparse body-dof matrix
//------------------------ object lists
// objects created here // objects created here
std::vector<mjCFlex*> flexes; // list of flexes std::vector<mjCFlex*> flexes; // list of flexes
std::vector<mjCMesh*> meshes; // list of meshes std::vector<mjCMesh*> meshes; // list of meshes
@@ -254,8 +250,6 @@ class mjCModel : private mjmModel {
std::vector<mjCCamera*> cameras; // list of cameras std::vector<mjCCamera*> cameras; // list of cameras
std::vector<mjCLight*> lights; // list of lights std::vector<mjCLight*> lights; // list of lights
//------------------------ internal variables
// array of pointers to each object list (enumerated by type) // array of pointers to each object list (enumerated by type)
std::array<std::vector<mjCBase*>*, mjNOBJECT> object_lists; std::array<std::vector<mjCBase*>*, mjNOBJECT> object_lists;
@@ -266,13 +260,14 @@ class mjCModel : private mjmModel {
double extent_auto; // spatial extent, as computed by mj_setConst double extent_auto; // spatial extent, as computed by mj_setConst
double center_auto[3]; // center of model, as computed by mj_setConst double center_auto[3]; // center of model, as computed by mj_setConst
// map from object names to ids
mjListKeyMap ids;
mjListKeyMap ids; // map from object names to ids
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
// save qpos0, to recognize changed key_qpos in write
std::vector<mjtNum> qpos0;
// variable-size attributes // variable-size attributes
std::string comment_; // comment at top of XML std::string comment_; // comment at top of XML