Clean up mjCModel definition.
PiperOrigin-RevId: 611102699 Change-Id: I64a3927c30f16764742966f4c5e1e0e579af5f50
This commit is contained in:
committed by
Copybara-Service
parent
789188d51d
commit
b2bd377bad
+138
-143
@@ -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
|
int NumObjects(mjtObj type); // number of objects in specified list
|
||||||
|
mjCBase* GetObject(mjtObj type, int id); // pointer to specified object
|
||||||
|
|
||||||
//------------------------ API for access to model elements (outside tree)
|
// API for access to other variables
|
||||||
int NumObjects(mjtObj type); // number of objects in specified list
|
bool IsCompiled(); // is model already compiled
|
||||||
mjCBase* GetObject(mjtObj type, int id); // pointer to specified object
|
int GetFixed(); // number of fixed massless bodies
|
||||||
|
mjCError GetError(); // copy of error object
|
||||||
|
mjCBody* GetWorld(); // pointer to world body
|
||||||
|
mjCDef* FindDef(std::string name); // find default class name
|
||||||
|
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
|
||||||
|
bool IsNullPose(const mjtNum* pos, const mjtNum* quat); // detect null pose
|
||||||
|
|
||||||
//------------------------ API for access to other variables
|
// accessors
|
||||||
bool IsCompiled(void); // is model already compiled
|
|
||||||
int GetFixed(void); // number of fixed massless bodies
|
|
||||||
mjCError GetError(void); // copy of error object
|
|
||||||
mjCBody* GetWorld(void); // pointer to world body
|
|
||||||
mjCDef* FindDef(std::string name); // find default class name
|
|
||||||
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
|
|
||||||
bool IsNullPose(const mjtNum* pos, const mjtNum* quat); // detect null pose
|
|
||||||
|
|
||||||
//------------------------ getters
|
|
||||||
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,71 +160,69 @@ 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
|
||||||
int ngeom; // number of geoms
|
int ngeom; // number of geoms
|
||||||
int nsite; // number of sites
|
int nsite; // number of sites
|
||||||
int ncam; // number of cameras
|
int ncam; // number of cameras
|
||||||
int nlight; // number of lights
|
int nlight; // number of lights
|
||||||
int nflex; // number of flexes
|
int nflex; // number of flexes
|
||||||
int nmesh; // number of meshes
|
int nmesh; // number of meshes
|
||||||
int nskin; // number of skins
|
int nskin; // number of skins
|
||||||
int nhfield; // number of height fields
|
int nhfield; // number of height fields
|
||||||
int ntex; // number of textures
|
int ntex; // number of textures
|
||||||
int nmat; // number of materials
|
int nmat; // number of materials
|
||||||
int npair; // number of geom pairs in pair array
|
int npair; // number of geom pairs in pair array
|
||||||
int nexclude; // number of excluded body pairs
|
int nexclude; // number of excluded body pairs
|
||||||
int neq; // number of equality constraints
|
int neq; // number of equality constraints
|
||||||
int ntendon; // number of tendons
|
int ntendon; // number of tendons
|
||||||
int nsensor; // number of sensors
|
int nsensor; // number of sensors
|
||||||
int nnumeric; // number of numeric fields
|
int nnumeric; // number of numeric fields
|
||||||
int ntext; // number of text fields
|
int ntext; // number of text fields
|
||||||
int ntuple; // number of tuple fields
|
int ntuple; // number of tuple fields
|
||||||
int nmocap; // number of mocap bodies
|
int nmocap; // number of mocap bodies
|
||||||
int nplugin; // number of plugin instances
|
int nplugin; // number of plugin instances
|
||||||
|
|
||||||
// sizes computed by Compile
|
// sizes computed by Compile
|
||||||
int nq; // number of generalized coordinates = dim(qpos)
|
int nq; // number of generalized coordinates = dim(qpos)
|
||||||
int nv; // number of degrees of freedom = dim(qvel)
|
int nv; // number of degrees of freedom = dim(qvel)
|
||||||
int nu; // number of actuators/controls
|
int nu; // number of actuators/controls
|
||||||
int na; // number of activation variables
|
int na; // number of activation variables
|
||||||
int nbvh; // number of total boundary volume hierarchies
|
int nbvh; // number of total boundary volume hierarchies
|
||||||
int nbvhstatic; // number of static boundary volume hierarchies
|
int nbvhstatic; // number of static boundary volume hierarchies
|
||||||
int nbvhdynamic; // number of dynamic boundary volume hierarchies
|
int nbvhdynamic; // number of dynamic boundary volume hierarchies
|
||||||
int nflexvert; // number of vertices in all flexes
|
int nflexvert; // number of vertices in all flexes
|
||||||
int nflexedge; // number of edges in all flexes
|
int nflexedge; // number of edges in all flexes
|
||||||
int nflexelem; // number of elements in all flexes
|
int nflexelem; // number of elements in all flexes
|
||||||
int nflexelemdata; // number of element vertex ids in all flexes
|
int nflexelemdata; // number of element vertex ids in all flexes
|
||||||
int nflexshelldata; // number of shell fragment vertex ids in all flexes
|
int nflexshelldata; // number of shell fragment vertex ids in all flexes
|
||||||
int nflexevpair; // number of element-vertex pairs in all flexes
|
int nflexevpair; // number of element-vertex pairs in all flexes
|
||||||
int nflextexcoord; // number of vertex texture coordinates in all flexes
|
int nflextexcoord; // number of vertex texture coordinates in all flexes
|
||||||
int nmeshvert; // number of vertices in all meshes
|
int nmeshvert; // number of vertices in all meshes
|
||||||
int nmeshnormal; // number of normals in all meshes
|
int nmeshnormal; // number of normals in all meshes
|
||||||
int nmeshtexcoord; // number of texture coordinates in all meshes
|
int nmeshtexcoord; // number of texture coordinates in all meshes
|
||||||
int nmeshface; // number of triangular faces in all meshes
|
int nmeshface; // number of triangular faces in all meshes
|
||||||
int nmeshgraph; // number of shorts in mesh auxiliary data
|
int nmeshgraph; // number of shorts in mesh auxiliary data
|
||||||
int nskinvert; // number of vertices in all skins
|
int nskinvert; // number of vertices in all skins
|
||||||
int nskintexvert; // number of vertices with texcoord in all skins
|
int nskintexvert; // number of vertices with texcoord in all skins
|
||||||
int nskinface; // number of faces in all skins
|
int nskinface; // number of faces in all skins
|
||||||
int nskinbone; // number of bones in all skins
|
int nskinbone; // number of bones in all skins
|
||||||
int nskinbonevert; // number of vertices in all skins
|
int nskinbonevert; // number of vertices in all skins
|
||||||
int nhfielddata; // number of data points in all hfields
|
int nhfielddata; // number of data points in all hfields
|
||||||
int ntexdata; // number of texture bytes
|
int ntexdata; // number of texture bytes
|
||||||
int nwrap; // number of wrap objects in all tendon paths
|
int nwrap; // number of wrap objects in all tendon paths
|
||||||
int nsensordata; // number of mjtNums in sensor data vector
|
int nsensordata; // number of mjtNums in sensor data vector
|
||||||
int nnumericdata; // number of mjtNums in all custom fields
|
int nnumericdata; // number of mjtNums in all custom fields
|
||||||
int ntextdata; // number of chars in all text fields, including 0
|
int ntextdata; // number of chars in all text fields, including 0
|
||||||
int ntupledata; // number of objects in all tuple fields
|
int ntupledata; // number of objects in all tuple fields
|
||||||
int npluginattr; // number of chars in all plugin config attributes
|
int npluginattr; // number of chars in all plugin config attributes
|
||||||
int nnames; // number of chars in all names
|
int nnames; // number of chars in all names
|
||||||
int npaths; // number of chars in all paths
|
int npaths; // number of chars in all paths
|
||||||
int nM; // number of non-zeros in sparse inertia matrix
|
int nM; // number of non-zeros in sparse inertia matrix
|
||||||
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,25 +250,24 @@ 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;
|
||||||
|
|
||||||
// statistics, as computed by mj_setConst
|
// statistics, as computed by mj_setConst
|
||||||
double meaninertia_auto; // mean diagonal inertia, as computed by mj_setConst
|
double meaninertia_auto; // mean diagonal inertia, as computed by mj_setConst
|
||||||
double meanmass_auto; // mean body mass, as computed by mj_setConst
|
double meanmass_auto; // mean body mass, as computed by mj_setConst
|
||||||
double meansize_auto; // mean body size, as computed by mj_setConst
|
double meansize_auto; // mean body size, as computed by mj_setConst
|
||||||
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;
|
|
||||||
|
|
||||||
bool compiled; // already compiled flag (cannot be compiled again)
|
mjListKeyMap ids; // map from object names to ids
|
||||||
mjCError errInfo; // last error info
|
bool compiled; // already compiled flag (cannot be compiled again)
|
||||||
int fixCount; // how many bodies have been fixed
|
mjCError errInfo; // last error info
|
||||||
std::vector<mjtNum> qpos0; // save qpos0, to recognize changed key_qpos in write
|
int fixCount; // how many bodies have been fixed
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|||||||
Reference in New Issue
Block a user