diff --git a/doc/changelog.rst b/doc/changelog.rst index e809f83e..76e3c878 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -60,6 +60,10 @@ General CMake utility project. - ``USD_DIR`` is no longer used by the MuJoCo CMake project, instead use ``pxr_DIR`` if you have a pre-built USD library. - Users no longer have to set ``PXR_PLUGINPATH_NAME`` environment variable, MuJoCo should load USD plugins automatically. +- Non-breaking ABI changes: + + - Allcating sizes in :ref:`mjModel` now use 64-bit rather than 32-bit integers to accommodate larger + scenes. MJX ^^^ diff --git a/doc/includes/references.h b/doc/includes/references.h index 7f2d46b9..6b9abd51 100644 --- a/doc/includes/references.h +++ b/doc/includes/references.h @@ -1006,95 +1006,96 @@ struct mjModel_ { // ------------------------------- sizes // sizes needed at mjModel construction - int nq; // number of generalized coordinates = dim(qpos) - int nv; // number of degrees of freedom = dim(qvel) - int nu; // number of actuators/controls = dim(ctrl) - int na; // number of activation states = dim(act) - int nbody; // number of bodies - int nbvh; // number of total bounding volumes in all bodies - int nbvhstatic; // number of static bounding volumes (aabb stored in mjModel) - int nbvhdynamic; // number of dynamic bounding volumes (aabb stored in mjData) - int noct; // number of total octree cells in all meshes - int njnt; // number of joints - int ntree; // number of kinematic trees under world body - int nM; // number of non-zeros in sparse inertia matrix - int nB; // number of non-zeros in sparse body-dof matrix - int nC; // number of non-zeros in sparse reduced dof-dof matrix - int nD; // number of non-zeros in sparse dof-dof matrix - int ngeom; // number of geoms - int nsite; // number of sites - int ncam; // number of cameras - int nlight; // number of lights - int nflex; // number of flexes - int nflexnode; // number of dofs in all flexes - int nflexvert; // number of vertices in all flexes - int nflexedge; // number of edges in all flexes - int nflexelem; // number of elements in all flexes - int nflexelemdata; // number of element vertex ids in all flexes - int nflexelemedge; // number of element edge 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 nflextexcoord; // number of vertices with texture coordinates - int nJfe; // number of non-zeros in sparse flexedge Jacobian matrix - int nJfv; // number of non-zeros in sparse flexvert Jacobian matrix - int nmesh; // number of meshes - int nmeshvert; // number of vertices in all meshes - int nmeshnormal; // number of normals in all meshes - int nmeshtexcoord; // number of texcoords in all meshes - int nmeshface; // number of triangular faces in all meshes - int nmeshgraph; // number of ints in mesh auxiliary data - int nmeshpoly; // number of polygons in all meshes - int nmeshpolyvert; // number of vertices in all polygons - int nmeshpolymap; // number of polygons in vertex map - int nskin; // number of skins - int nskinvert; // number of vertices in all skins - int nskintexvert; // number of vertices with texcoords in all skins - int nskinface; // number of triangular faces in all skins - int nskinbone; // number of bones in all skins - int nskinbonevert; // number of vertices in all skin bones - int nhfield; // number of heightfields - int nhfielddata; // number of data points in all heightfields - int ntex; // number of textures - int ntexdata; // number of bytes in texture rgb data - int nmat; // number of materials - int npair; // number of predefined geom pairs - int nexclude; // number of excluded geom pairs - int neq; // number of equality constraints - int ntendon; // number of tendons - int nwrap; // number of wrap objects in all tendon paths - int nsensor; // number of sensors - int nnumeric; // number of numeric custom fields - int nnumericdata; // number of mjtNums in all numeric fields - int ntext; // number of text custom fields - int ntextdata; // number of mjtBytes in all text fields - int ntuple; // number of tuple custom fields - int ntupledata; // number of objects in all tuple fields - int nkey; // number of keyframes - int nmocap; // number of mocap bodies - int nplugin; // number of plugin instances - int npluginattr; // number of chars in all plugin config attributes - int nuser_body; // number of mjtNums in body_user - int nuser_jnt; // number of mjtNums in jnt_user - int nuser_geom; // number of mjtNums in geom_user - int nuser_site; // number of mjtNums in site_user - int nuser_cam; // number of mjtNums in cam_user - int nuser_tendon; // number of mjtNums in tendon_user - int nuser_actuator; // number of mjtNums in actuator_user - int nuser_sensor; // number of mjtNums in sensor_user - int nnames; // number of chars in all names - int npaths; // number of chars in all paths + mjtSize nq; // number of generalized coordinates = dim(qpos) + mjtSize nv; // number of degrees of freedom = dim(qvel) + mjtSize nu; // number of actuators/controls = dim(ctrl) + mjtSize na; // number of activation states = dim(act) + mjtSize nbody; // number of bodies + mjtSize nbvh; // number of total bounding volumes in all bodies + mjtSize nbvhstatic; // number of static bounding volumes (aabb stored in mjModel) + mjtSize nbvhdynamic; // number of dynamic bounding volumes (aabb stored in mjData) + mjtSize noct; // number of total octree cells in all meshes + mjtSize njnt; // number of joints + mjtSize ntree; // number of kinematic trees under world body + mjtSize nM; // number of non-zeros in sparse inertia matrix + mjtSize nB; // number of non-zeros in sparse body-dof matrix + mjtSize nC; // number of non-zeros in sparse reduced dof-dof matrix + mjtSize nD; // number of non-zeros in sparse dof-dof matrix + mjtSize ngeom; // number of geoms + mjtSize nsite; // number of sites + mjtSize ncam; // number of cameras + mjtSize nlight; // number of lights + mjtSize nflex; // number of flexes + mjtSize nflexnode; // number of dofs in all flexes + mjtSize nflexvert; // number of vertices in all flexes + mjtSize nflexedge; // number of edges in all flexes + mjtSize nflexelem; // number of elements in all flexes + mjtSize nflexelemdata; // number of element vertex ids in all flexes + mjtSize nflexelemedge; // number of element edge ids in all flexes + mjtSize nflexshelldata; // number of shell fragment vertex ids in all flexes + mjtSize nflexevpair; // number of element-vertex pairs in all flexes + mjtSize nflextexcoord; // number of vertices with texture coordinates + mjtSize nJfe; // number of non-zeros in sparse flexedge Jacobian matrix + mjtSize nJfv; // number of non-zeros in sparse flexvert Jacobian matrix + mjtSize nmesh; // number of meshes + mjtSize nmeshvert; // number of vertices in all meshes + mjtSize nmeshnormal; // number of normals in all meshes + mjtSize nmeshtexcoord; // number of texcoords in all meshes + mjtSize nmeshface; // number of triangular faces in all meshes + mjtSize nmeshgraph; // number of ints in mesh auxiliary data + mjtSize nmeshpoly; // number of polygons in all meshes + mjtSize nmeshpolyvert; // number of vertices in all polygons + mjtSize nmeshpolymap; // number of polygons in vertex map + mjtSize nskin; // number of skins + mjtSize nskinvert; // number of vertices in all skins + mjtSize nskintexvert; // number of vertices with texcoords in all skins + mjtSize nskinface; // number of triangular faces in all skins + mjtSize nskinbone; // number of bones in all skins + mjtSize nskinbonevert; // number of vertices in all skin bones + mjtSize nhfield; // number of heightfields + mjtSize nhfielddata; // number of data points in all heightfields + mjtSize ntex; // number of textures + mjtSize ntexdata; // number of bytes in texture rgb data + mjtSize nmat; // number of materials + mjtSize npair; // number of predefined geom pairs + mjtSize nexclude; // number of excluded geom pairs + mjtSize neq; // number of equality constraints + mjtSize ntendon; // number of tendons + mjtSize nwrap; // number of wrap objects in all tendon paths + mjtSize nsensor; // number of sensors + mjtSize nnumeric; // number of numeric custom fields + mjtSize nnumericdata; // number of mjtNums in all numeric fields + mjtSize ntext; // number of text custom fields + mjtSize ntextdata; // number of mjtBytes in all text fields + mjtSize ntuple; // number of tuple custom fields + mjtSize ntupledata; // number of objects in all tuple fields + mjtSize nkey; // number of keyframes + mjtSize nmocap; // number of mocap bodies + mjtSize nplugin; // number of plugin instances + mjtSize npluginattr; // number of chars in all plugin config attributes + mjtSize nuser_body; // number of mjtNums in body_user + mjtSize nuser_jnt; // number of mjtNums in jnt_user + mjtSize nuser_geom; // number of mjtNums in geom_user + mjtSize nuser_site; // number of mjtNums in site_user + mjtSize nuser_cam; // number of mjtNums in cam_user + mjtSize nuser_tendon; // number of mjtNums in tendon_user + mjtSize nuser_actuator; // number of mjtNums in actuator_user + mjtSize nuser_sensor; // number of mjtNums in sensor_user + mjtSize nnames; // number of chars in all names + mjtSize npaths; // number of chars in all paths // sizes set after mjModel construction - int nnames_map; // number of slots in the names hash map - int nJmom; // number of non-zeros in sparse actuator_moment matrix - int ngravcomp; // number of bodies with nonzero gravcomp - int nemax; // number of potential equality-constraint rows - int njmax; // number of available rows in constraint Jacobian (legacy) - int nconmax; // number of potential contacts in contact list (legacy) - int nuserdata; // number of mjtNums reserved for the user - int nsensordata; // number of mjtNums in sensor data vector - int npluginstate; // number of mjtNums in plugin state vector + mjtSize nnames_map; // number of slots in the names hash map + mjtSize nJmom; // number of non-zeros in sparse actuator_moment matrix + mjtSize ngravcomp; // number of bodies with nonzero gravcomp + mjtSize nemax; // number of potential equality-constraint rows + mjtSize njmax; // number of available rows in constraint Jacobian (legacy) + mjtSize nconmax; // number of potential contacts in contact list (legacy) + mjtSize nuserdata; // number of mjtNums reserved for the user + mjtSize nsensordata; // number of mjtNums in sensor data vector + mjtSize npluginstate; // number of mjtNums in plugin state vector + // buffer sizes mjtSize narena; // number of bytes in the mjData arena (inclusive of stack) mjtSize nbuffer; // number of bytes in buffer diff --git a/include/mujoco/mjmodel.h b/include/mujoco/mjmodel.h index ef9b390c..af81166e 100644 --- a/include/mujoco/mjmodel.h +++ b/include/mujoco/mjmodel.h @@ -672,95 +672,96 @@ struct mjModel_ { // ------------------------------- sizes // sizes needed at mjModel construction - int nq; // number of generalized coordinates = dim(qpos) - int nv; // number of degrees of freedom = dim(qvel) - int nu; // number of actuators/controls = dim(ctrl) - int na; // number of activation states = dim(act) - int nbody; // number of bodies - int nbvh; // number of total bounding volumes in all bodies - int nbvhstatic; // number of static bounding volumes (aabb stored in mjModel) - int nbvhdynamic; // number of dynamic bounding volumes (aabb stored in mjData) - int noct; // number of total octree cells in all meshes - int njnt; // number of joints - int ntree; // number of kinematic trees under world body - int nM; // number of non-zeros in sparse inertia matrix - int nB; // number of non-zeros in sparse body-dof matrix - int nC; // number of non-zeros in sparse reduced dof-dof matrix - int nD; // number of non-zeros in sparse dof-dof matrix - int ngeom; // number of geoms - int nsite; // number of sites - int ncam; // number of cameras - int nlight; // number of lights - int nflex; // number of flexes - int nflexnode; // number of dofs in all flexes - int nflexvert; // number of vertices in all flexes - int nflexedge; // number of edges in all flexes - int nflexelem; // number of elements in all flexes - int nflexelemdata; // number of element vertex ids in all flexes - int nflexelemedge; // number of element edge 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 nflextexcoord; // number of vertices with texture coordinates - int nJfe; // number of non-zeros in sparse flexedge Jacobian matrix - int nJfv; // number of non-zeros in sparse flexvert Jacobian matrix - int nmesh; // number of meshes - int nmeshvert; // number of vertices in all meshes - int nmeshnormal; // number of normals in all meshes - int nmeshtexcoord; // number of texcoords in all meshes - int nmeshface; // number of triangular faces in all meshes - int nmeshgraph; // number of ints in mesh auxiliary data - int nmeshpoly; // number of polygons in all meshes - int nmeshpolyvert; // number of vertices in all polygons - int nmeshpolymap; // number of polygons in vertex map - int nskin; // number of skins - int nskinvert; // number of vertices in all skins - int nskintexvert; // number of vertices with texcoords in all skins - int nskinface; // number of triangular faces in all skins - int nskinbone; // number of bones in all skins - int nskinbonevert; // number of vertices in all skin bones - int nhfield; // number of heightfields - int nhfielddata; // number of data points in all heightfields - int ntex; // number of textures - int ntexdata; // number of bytes in texture rgb data - int nmat; // number of materials - int npair; // number of predefined geom pairs - int nexclude; // number of excluded geom pairs - int neq; // number of equality constraints - int ntendon; // number of tendons - int nwrap; // number of wrap objects in all tendon paths - int nsensor; // number of sensors - int nnumeric; // number of numeric custom fields - int nnumericdata; // number of mjtNums in all numeric fields - int ntext; // number of text custom fields - int ntextdata; // number of mjtBytes in all text fields - int ntuple; // number of tuple custom fields - int ntupledata; // number of objects in all tuple fields - int nkey; // number of keyframes - int nmocap; // number of mocap bodies - int nplugin; // number of plugin instances - int npluginattr; // number of chars in all plugin config attributes - int nuser_body; // number of mjtNums in body_user - int nuser_jnt; // number of mjtNums in jnt_user - int nuser_geom; // number of mjtNums in geom_user - int nuser_site; // number of mjtNums in site_user - int nuser_cam; // number of mjtNums in cam_user - int nuser_tendon; // number of mjtNums in tendon_user - int nuser_actuator; // number of mjtNums in actuator_user - int nuser_sensor; // number of mjtNums in sensor_user - int nnames; // number of chars in all names - int npaths; // number of chars in all paths + mjtSize nq; // number of generalized coordinates = dim(qpos) + mjtSize nv; // number of degrees of freedom = dim(qvel) + mjtSize nu; // number of actuators/controls = dim(ctrl) + mjtSize na; // number of activation states = dim(act) + mjtSize nbody; // number of bodies + mjtSize nbvh; // number of total bounding volumes in all bodies + mjtSize nbvhstatic; // number of static bounding volumes (aabb stored in mjModel) + mjtSize nbvhdynamic; // number of dynamic bounding volumes (aabb stored in mjData) + mjtSize noct; // number of total octree cells in all meshes + mjtSize njnt; // number of joints + mjtSize ntree; // number of kinematic trees under world body + mjtSize nM; // number of non-zeros in sparse inertia matrix + mjtSize nB; // number of non-zeros in sparse body-dof matrix + mjtSize nC; // number of non-zeros in sparse reduced dof-dof matrix + mjtSize nD; // number of non-zeros in sparse dof-dof matrix + mjtSize ngeom; // number of geoms + mjtSize nsite; // number of sites + mjtSize ncam; // number of cameras + mjtSize nlight; // number of lights + mjtSize nflex; // number of flexes + mjtSize nflexnode; // number of dofs in all flexes + mjtSize nflexvert; // number of vertices in all flexes + mjtSize nflexedge; // number of edges in all flexes + mjtSize nflexelem; // number of elements in all flexes + mjtSize nflexelemdata; // number of element vertex ids in all flexes + mjtSize nflexelemedge; // number of element edge ids in all flexes + mjtSize nflexshelldata; // number of shell fragment vertex ids in all flexes + mjtSize nflexevpair; // number of element-vertex pairs in all flexes + mjtSize nflextexcoord; // number of vertices with texture coordinates + mjtSize nJfe; // number of non-zeros in sparse flexedge Jacobian matrix + mjtSize nJfv; // number of non-zeros in sparse flexvert Jacobian matrix + mjtSize nmesh; // number of meshes + mjtSize nmeshvert; // number of vertices in all meshes + mjtSize nmeshnormal; // number of normals in all meshes + mjtSize nmeshtexcoord; // number of texcoords in all meshes + mjtSize nmeshface; // number of triangular faces in all meshes + mjtSize nmeshgraph; // number of ints in mesh auxiliary data + mjtSize nmeshpoly; // number of polygons in all meshes + mjtSize nmeshpolyvert; // number of vertices in all polygons + mjtSize nmeshpolymap; // number of polygons in vertex map + mjtSize nskin; // number of skins + mjtSize nskinvert; // number of vertices in all skins + mjtSize nskintexvert; // number of vertices with texcoords in all skins + mjtSize nskinface; // number of triangular faces in all skins + mjtSize nskinbone; // number of bones in all skins + mjtSize nskinbonevert; // number of vertices in all skin bones + mjtSize nhfield; // number of heightfields + mjtSize nhfielddata; // number of data points in all heightfields + mjtSize ntex; // number of textures + mjtSize ntexdata; // number of bytes in texture rgb data + mjtSize nmat; // number of materials + mjtSize npair; // number of predefined geom pairs + mjtSize nexclude; // number of excluded geom pairs + mjtSize neq; // number of equality constraints + mjtSize ntendon; // number of tendons + mjtSize nwrap; // number of wrap objects in all tendon paths + mjtSize nsensor; // number of sensors + mjtSize nnumeric; // number of numeric custom fields + mjtSize nnumericdata; // number of mjtNums in all numeric fields + mjtSize ntext; // number of text custom fields + mjtSize ntextdata; // number of mjtBytes in all text fields + mjtSize ntuple; // number of tuple custom fields + mjtSize ntupledata; // number of objects in all tuple fields + mjtSize nkey; // number of keyframes + mjtSize nmocap; // number of mocap bodies + mjtSize nplugin; // number of plugin instances + mjtSize npluginattr; // number of chars in all plugin config attributes + mjtSize nuser_body; // number of mjtNums in body_user + mjtSize nuser_jnt; // number of mjtNums in jnt_user + mjtSize nuser_geom; // number of mjtNums in geom_user + mjtSize nuser_site; // number of mjtNums in site_user + mjtSize nuser_cam; // number of mjtNums in cam_user + mjtSize nuser_tendon; // number of mjtNums in tendon_user + mjtSize nuser_actuator; // number of mjtNums in actuator_user + mjtSize nuser_sensor; // number of mjtNums in sensor_user + mjtSize nnames; // number of chars in all names + mjtSize npaths; // number of chars in all paths // sizes set after mjModel construction - int nnames_map; // number of slots in the names hash map - int nJmom; // number of non-zeros in sparse actuator_moment matrix - int ngravcomp; // number of bodies with nonzero gravcomp - int nemax; // number of potential equality-constraint rows - int njmax; // number of available rows in constraint Jacobian (legacy) - int nconmax; // number of potential contacts in contact list (legacy) - int nuserdata; // number of mjtNums reserved for the user - int nsensordata; // number of mjtNums in sensor data vector - int npluginstate; // number of mjtNums in plugin state vector + mjtSize nnames_map; // number of slots in the names hash map + mjtSize nJmom; // number of non-zeros in sparse actuator_moment matrix + mjtSize ngravcomp; // number of bodies with nonzero gravcomp + mjtSize nemax; // number of potential equality-constraint rows + mjtSize njmax; // number of available rows in constraint Jacobian (legacy) + mjtSize nconmax; // number of potential contacts in contact list (legacy) + mjtSize nuserdata; // number of mjtNums reserved for the user + mjtSize nsensordata; // number of mjtNums in sensor data vector + mjtSize npluginstate; // number of mjtNums in plugin state vector + // buffer sizes mjtSize narena; // number of bytes in the mjData arena (inclusive of stack) mjtSize nbuffer; // number of bytes in buffer diff --git a/include/mujoco/mjxmacro.h b/include/mujoco/mjxmacro.h index 606ce95b..f0c3f64a 100644 --- a/include/mujoco/mjxmacro.h +++ b/include/mujoco/mjxmacro.h @@ -65,8 +65,8 @@ //-------------------------------- mjModel --------------------------------------------------------- -// int fields of mjModel -#define MJMODEL_INTS \ +// size fields of mjModel +#define MJMODEL_SIZES \ X( nq ) \ X( nv ) \ X( nu ) \ diff --git a/python/mujoco/indexers.cc b/python/mujoco/indexers.cc index 158f4cae..3bcfb44e 100644 --- a/python/mujoco/indexers.cc +++ b/python/mujoco/indexers.cc @@ -261,7 +261,7 @@ MJDATA_VIEW_GROUPS if constexpr (std::string_view(#dim) == std::string_view("1")) { \ return {}; \ } else { \ - return {n}; \ + return {(int)n}; \ } \ }() \ MUJOCO_DIAG_UNIGNORE_UNUSED_LAMBDA_CAPTURE diff --git a/python/mujoco/introspect/structs.py b/python/mujoco/introspect/structs.py index be3f54a8..ce548f11 100644 --- a/python/mujoco/introspect/structs.py +++ b/python/mujoco/introspect/structs.py @@ -849,432 +849,432 @@ STRUCTS: Mapping[str, StructDecl] = dict([ fields=( StructFieldDecl( name='nq', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of generalized coordinates = dim(qpos)', ), StructFieldDecl( name='nv', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of degrees of freedom = dim(qvel)', ), StructFieldDecl( name='nu', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of actuators/controls = dim(ctrl)', ), StructFieldDecl( name='na', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of activation states = dim(act)', ), StructFieldDecl( name='nbody', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of bodies', ), StructFieldDecl( name='nbvh', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of total bounding volumes in all bodies', ), StructFieldDecl( name='nbvhstatic', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of static bounding volumes (aabb stored in mjModel)', # pylint: disable=line-too-long ), StructFieldDecl( name='nbvhdynamic', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of dynamic bounding volumes (aabb stored in mjData)', # pylint: disable=line-too-long ), StructFieldDecl( name='noct', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of total octree cells in all meshes', ), StructFieldDecl( name='njnt', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of joints', ), StructFieldDecl( name='ntree', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of kinematic trees under world body', ), StructFieldDecl( name='nM', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of non-zeros in sparse inertia matrix', ), StructFieldDecl( name='nB', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of non-zeros in sparse body-dof matrix', ), StructFieldDecl( name='nC', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of non-zeros in sparse reduced dof-dof matrix', ), StructFieldDecl( name='nD', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of non-zeros in sparse dof-dof matrix', ), StructFieldDecl( name='ngeom', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of geoms', ), StructFieldDecl( name='nsite', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of sites', ), StructFieldDecl( name='ncam', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of cameras', ), StructFieldDecl( name='nlight', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of lights', ), StructFieldDecl( name='nflex', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of flexes', ), StructFieldDecl( name='nflexnode', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of dofs in all flexes', ), StructFieldDecl( name='nflexvert', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of vertices in all flexes', ), StructFieldDecl( name='nflexedge', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of edges in all flexes', ), StructFieldDecl( name='nflexelem', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of elements in all flexes', ), StructFieldDecl( name='nflexelemdata', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of element vertex ids in all flexes', ), StructFieldDecl( name='nflexelemedge', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of element edge ids in all flexes', ), StructFieldDecl( name='nflexshelldata', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of shell fragment vertex ids in all flexes', ), StructFieldDecl( name='nflexevpair', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of element-vertex pairs in all flexes', ), StructFieldDecl( name='nflextexcoord', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of vertices with texture coordinates', ), StructFieldDecl( name='nJfe', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of non-zeros in sparse flexedge Jacobian matrix', ), StructFieldDecl( name='nJfv', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of non-zeros in sparse flexvert Jacobian matrix', ), StructFieldDecl( name='nmesh', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of meshes', ), StructFieldDecl( name='nmeshvert', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of vertices in all meshes', ), StructFieldDecl( name='nmeshnormal', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of normals in all meshes', ), StructFieldDecl( name='nmeshtexcoord', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of texcoords in all meshes', ), StructFieldDecl( name='nmeshface', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of triangular faces in all meshes', ), StructFieldDecl( name='nmeshgraph', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of ints in mesh auxiliary data', ), StructFieldDecl( name='nmeshpoly', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of polygons in all meshes', ), StructFieldDecl( name='nmeshpolyvert', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of vertices in all polygons', ), StructFieldDecl( name='nmeshpolymap', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of polygons in vertex map', ), StructFieldDecl( name='nskin', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of skins', ), StructFieldDecl( name='nskinvert', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of vertices in all skins', ), StructFieldDecl( name='nskintexvert', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of vertices with texcoords in all skins', ), StructFieldDecl( name='nskinface', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of triangular faces in all skins', ), StructFieldDecl( name='nskinbone', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of bones in all skins', ), StructFieldDecl( name='nskinbonevert', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of vertices in all skin bones', ), StructFieldDecl( name='nhfield', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of heightfields', ), StructFieldDecl( name='nhfielddata', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of data points in all heightfields', ), StructFieldDecl( name='ntex', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of textures', ), StructFieldDecl( name='ntexdata', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of bytes in texture rgb data', ), StructFieldDecl( name='nmat', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of materials', ), StructFieldDecl( name='npair', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of predefined geom pairs', ), StructFieldDecl( name='nexclude', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of excluded geom pairs', ), StructFieldDecl( name='neq', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of equality constraints', ), StructFieldDecl( name='ntendon', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of tendons', ), StructFieldDecl( name='nwrap', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of wrap objects in all tendon paths', ), StructFieldDecl( name='nsensor', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of sensors', ), StructFieldDecl( name='nnumeric', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of numeric custom fields', ), StructFieldDecl( name='nnumericdata', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtNums in all numeric fields', ), StructFieldDecl( name='ntext', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of text custom fields', ), StructFieldDecl( name='ntextdata', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtBytes in all text fields', ), StructFieldDecl( name='ntuple', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of tuple custom fields', ), StructFieldDecl( name='ntupledata', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of objects in all tuple fields', ), StructFieldDecl( name='nkey', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of keyframes', ), StructFieldDecl( name='nmocap', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mocap bodies', ), StructFieldDecl( name='nplugin', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of plugin instances', ), StructFieldDecl( name='npluginattr', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of chars in all plugin config attributes', ), StructFieldDecl( name='nuser_body', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtNums in body_user', ), StructFieldDecl( name='nuser_jnt', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtNums in jnt_user', ), StructFieldDecl( name='nuser_geom', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtNums in geom_user', ), StructFieldDecl( name='nuser_site', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtNums in site_user', ), StructFieldDecl( name='nuser_cam', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtNums in cam_user', ), StructFieldDecl( name='nuser_tendon', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtNums in tendon_user', ), StructFieldDecl( name='nuser_actuator', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtNums in actuator_user', ), StructFieldDecl( name='nuser_sensor', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtNums in sensor_user', ), StructFieldDecl( name='nnames', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of chars in all names', ), StructFieldDecl( name='npaths', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of chars in all paths', ), StructFieldDecl( name='nnames_map', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of slots in the names hash map', ), StructFieldDecl( name='nJmom', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of non-zeros in sparse actuator_moment matrix', ), StructFieldDecl( name='ngravcomp', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of bodies with nonzero gravcomp', ), StructFieldDecl( name='nemax', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of potential equality-constraint rows', ), StructFieldDecl( name='njmax', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of available rows in constraint Jacobian (legacy)', ), StructFieldDecl( name='nconmax', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of potential contacts in contact list (legacy)', ), StructFieldDecl( name='nuserdata', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtNums reserved for the user', ), StructFieldDecl( name='nsensordata', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtNums in sensor data vector', ), StructFieldDecl( name='npluginstate', - type=ValueType(name='int'), + type=ValueType(name='mjtSize'), doc='number of mjtNums in plugin state vector', ), StructFieldDecl( diff --git a/python/mujoco/structs.cc b/python/mujoco/structs.cc index 8c036e7d..67993b4c 100644 --- a/python/mujoco/structs.cc +++ b/python/mujoco/structs.cc @@ -56,9 +56,13 @@ namespace { // (dim0, dim1). #define X_ARRAY_SHAPE(dim0, dim1) XArrayShapeImpl(#dim1)((dim0), (dim1)) -std::vector XArrayShapeImpl1D(int dim0, int dim1) { return {dim0}; } +std::vector XArrayShapeImpl1D(mjtSize dim0, mjtSize dim1) { + return {dim0}; +} -std::vector XArrayShapeImpl2D(int dim0, int dim1) { return {dim0, dim1}; } +std::vector XArrayShapeImpl2D(mjtSize dim0, mjtSize dim1) { + return {dim0, dim1}; +} constexpr auto XArrayShapeImpl(const std::string_view dim1_str) { if (dim1_str == "1") { @@ -401,20 +405,20 @@ This is useful for example when the MJB is not available as a file on disk.)")); #define X(var) \ mjModel.def_property_readonly( \ #var, [](const MjModelWrapper& m) { return m.get()->var; }); - MJMODEL_INTS + MJMODEL_SIZES #undef X mjModel.def_property_readonly("_sizes", [](const MjModelWrapper& m) { int nint = 0; #define X(var) ++nint; - MJMODEL_INTS + MJMODEL_SIZES #undef X py::array_t sizes(nint); { int i = 0; auto data = sizes.mutable_unchecked(); #define X(var) data[i++] = m.get()->var; - MJMODEL_INTS + MJMODEL_SIZES #undef X } py::detail::array_proxy(sizes.ptr())->flags &= @@ -425,7 +429,7 @@ This is useful for example when the MJB is not available as a file on disk.)")); mjModel.def_property_readonly_static("_size_fields", [](py::object) { std::vector fields; #define X(var) fields.push_back(#var); - MJMODEL_INTS + MJMODEL_SIZES #undef X return py::tuple(py::cast(fields)); }); @@ -433,7 +437,7 @@ This is useful for example when the MJB is not available as a file on disk.)")); mjModel.def_property_readonly_static("_all_fields", [](py::object) { std::vector fields; #define X(var) fields.push_back(#var); - MJMODEL_INTS + MJMODEL_SIZES #undef X #define X(type, name, nr, nc) fields.push_back(#name); MJMODEL_POINTERS diff --git a/python/mujoco/structs.h b/python/mujoco/structs.h index 18d306ad..06d8ffa6 100644 --- a/python/mujoco/structs.h +++ b/python/mujoco/structs.h @@ -1006,7 +1006,7 @@ using _impl::MjSolverStatList; template std::enable_if_t, pybind11::array_t> static InitPyArray(Shape&& shape, T* buf, pybind11::handle owner) { - int size = 1; + mjtSize size = 1; for (const auto& i : shape) { size *= i; } @@ -1043,7 +1043,7 @@ template std::enable_if_t && !is_mj_struct_list_v, pybind11::tuple> static InitPyArray(Shape&& shape, T* buf, pybind11::handle owner) { - int size = 1; + mjtSize size = 1; for (const auto& i : shape) { size *= i; } diff --git a/python/mujoco/structs_wrappers.cc b/python/mujoco/structs_wrappers.cc index b909cc2f..8da1b115 100644 --- a/python/mujoco/structs_wrappers.cc +++ b/python/mujoco/structs_wrappers.cc @@ -60,9 +60,13 @@ namespace { // (dim0, dim1). #define X_ARRAY_SHAPE(dim0, dim1) XArrayShapeImpl(#dim1)((dim0), (dim1)) -std::vector XArrayShapeImpl1D(int dim0, int dim1) { return {dim0}; } +std::vector XArrayShapeImpl1D(mjtSize dim0, mjtSize dim1) { + return {dim0}; +} -std::vector XArrayShapeImpl2D(int dim0, int dim1) { return {dim0, dim1}; } +std::vector XArrayShapeImpl2D(mjtSize dim0, mjtSize dim1) { + return {dim0, dim1}; +} constexpr auto XArrayShapeImpl(const std::string_view dim1_str) { if (dim1_str == "1") { diff --git a/sample/testspeed.cc b/sample/testspeed.cc index 486651be..f14e6572 100644 --- a/sample/testspeed.cc +++ b/sample/testspeed.cc @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include #include @@ -283,7 +284,7 @@ int main(int argc, char** argv) { solver[m->opt.solver], solto6[m->opt.solver], iterations[0]/nstep); std::printf(" Contacts / step : %.2f\n", static_cast(contacts[0])/nstep); std::printf(" Constraints / step : %.2f\n", static_cast(constraints[0])/nstep); - std::printf(" Degrees of freedom : %d\n", m->nv); + std::printf(" Degrees of freedom : %" PRId64 "\n", m->nv); std::printf(" Dynamic memory usage : %.1f%% of %s\n\n", 100 * d[0]->maxuse_arena / (double)(d[0]->narena), mju_writeNumBytes(d[0]->narena)); diff --git a/src/engine/engine_io.c b/src/engine/engine_io.c index ed728f91..504011bf 100644 --- a/src/engine/engine_io.c +++ b/src/engine/engine_io.c @@ -50,10 +50,7 @@ #pragma warning (disable: 4305) // disable MSVC warning: truncation from 'double' to 'float' #endif -static const int MAX_ARRAY_SIZE = INT_MAX / 4; - - -//----------------------------------- static utility functions ------------------------------------- +static const int MAX_ARRAY_SIZE = INT_MAX; //----------------------------------- static utility functions ------------------------------------- @@ -70,24 +67,12 @@ static const int ID = 54321; #define MJMODEL_MEMBER(name) (((mjModel*) NULL)->name) -// count ints in mjModel -static int getnint(void) { - int cnt = 0; - -#define X(name) cnt += _Generic(MJMODEL_MEMBER(name), int: 1, default: 0); - MJMODEL_INTS -#undef X - - return cnt; -} - - -// count buffer members in mjModel (mjtSize) -static int getnbuffer(void) { +// count mjtSize members in mjModel +static int getnsize(void) { int cnt = 0; #define X(name) cnt += _Generic(MJMODEL_MEMBER(name), mjtSize: 1, default: 0); - MJMODEL_INTS + MJMODEL_SIZES #undef X return cnt; @@ -107,7 +92,7 @@ static int getnptr(void) { // write to memory buffer -static void bufwrite(const void* src, int num, int szbuf, void* buf, int* ptrbuf) { +static void bufwrite(const void* src, int num, mjtSize szbuf, void* buf, mjtSize* ptrbuf) { // check pointers if (!src || !buf || !ptrbuf) { mjERROR("NULL pointer passed to bufwrite"); @@ -125,7 +110,7 @@ static void bufwrite(const void* src, int num, int szbuf, void* buf, int* ptrbuf // read from memory buffer -static void bufread(void* dest, int num, int szbuf, const void* buf, int* ptrbuf) { +static void bufread(void* dest, int num, mjtSize szbuf, const void* buf, mjtSize* ptrbuf) { // check pointers if (!dest || !buf || !ptrbuf) { mjERROR("NULL pointer passed to bufread"); @@ -184,8 +169,8 @@ static void mj_setPtrModel(mjModel* m) { // performs the following operations: // *nbuffer += SKIP(*offset) + type_size*nr*nc; // *offset += SKIP(*offset) + type_size*nr*nc; -static int safeAddToBufferSize(intptr_t* offset, mjtSize* nbuffer, - size_t type_size, int nr, int nc) { +static mjtSize safeAddToBufferSize(intptr_t* offset, mjtSize* nbuffer, + size_t type_size, mjtSize nr, mjtSize nc) { if (type_size < 0 || nr < 0 || nc < 0) { return 0; } @@ -216,25 +201,58 @@ static void freeModelBuffers(mjModel* m) { // allocate and initialize mjModel structure void mj_makeModel(mjModel** dest, - int nq, int nv, int nu, int na, int nbody, int nbvh, - int nbvhstatic, int nbvhdynamic, int noct, int njnt, int ntree, - int nM, int nB, int nC, int nD, int ngeom, int nsite, int ncam, int nlight, - int nflex, int nflexnode, int nflexvert, int nflexedge, int nflexelem, int nflexelemdata, - int nflexelemedge, int nflexshelldata, int nflexevpair, int nflextexcoord, int nJfe, int nJfv, - int nmesh, int nmeshvert, int nmeshnormal, int nmeshtexcoord, int nmeshface, - int nmeshgraph, int nmeshpoly, int nmeshpolyvert, int nmeshpolymap, int nskin, int nskinvert, - int nskintexvert, int nskinface, - int nskinbone, int nskinbonevert, int nhfield, int nhfielddata, int ntex, - int ntexdata, int nmat, int npair, int nexclude, int neq, int ntendon, - int nwrap, int nsensor, int nnumeric, int nnumericdata, int ntext, - int ntextdata, int ntuple, int ntupledata, int nkey, int nmocap, - int nplugin, int npluginattr, int nuser_body, int nuser_jnt, int nuser_geom, - int nuser_site, int nuser_cam, int nuser_tendon, int nuser_actuator, - int nuser_sensor, int nnames, int npaths) { + mjtSize nq, mjtSize nv, mjtSize nu, mjtSize na, mjtSize nbody, mjtSize nbvh, mjtSize nbvhstatic, + mjtSize nbvhdynamic, mjtSize noct, mjtSize njnt, mjtSize ntree, mjtSize nM, mjtSize nB, + mjtSize nC, mjtSize nD, mjtSize ngeom, mjtSize nsite, mjtSize ncam, mjtSize nlight, + mjtSize nflex, mjtSize nflexnode, mjtSize nflexvert, mjtSize nflexedge, mjtSize nflexelem, + mjtSize nflexelemdata, mjtSize nflexelemedge, mjtSize nflexshelldata, mjtSize nflexevpair, + mjtSize nflextexcoord, mjtSize nJfe, mjtSize nJfv, mjtSize nmesh, mjtSize nmeshvert, + mjtSize nmeshnormal, mjtSize nmeshtexcoord, mjtSize nmeshface, mjtSize nmeshgraph, + mjtSize nmeshpoly, mjtSize nmeshpolyvert, mjtSize nmeshpolymap, mjtSize nskin, + mjtSize nskinvert, mjtSize nskintexvert, mjtSize nskinface, mjtSize nskinbone, + mjtSize nskinbonevert, mjtSize nhfield, mjtSize nhfielddata, mjtSize ntex, mjtSize ntexdata, + mjtSize nmat, mjtSize npair, mjtSize nexclude, mjtSize neq, mjtSize ntendon, mjtSize nwrap, + mjtSize nsensor, mjtSize nnumeric, mjtSize nnumericdata, mjtSize ntext, mjtSize ntextdata, + mjtSize ntuple, mjtSize ntupledata, mjtSize nkey, mjtSize nmocap, mjtSize nplugin, + mjtSize npluginattr, mjtSize nuser_body, mjtSize nuser_jnt, mjtSize nuser_geom, + mjtSize nuser_site, mjtSize nuser_cam, mjtSize nuser_tendon, mjtSize nuser_actuator, + mjtSize nuser_sensor, mjtSize nnames, mjtSize npaths) { intptr_t offset = 0; int allocate = *dest ? 0 : 1; mjModel* m = NULL; + // CHECK SIZE PARAMETERS + { + // dummy variables for MJMODEL_SIZES set after mjModel construction + int nnames_map=0, nJmom=0, ngravcomp=0, nemax=0, njmax=0; + int nconmax=0, nuserdata=0, nsensordata=0, npluginstate=0, narena=0, nbuffer=0; + + // sizes must be non-negative and fit in int, except for the byte arrays texdata and textdata + #define X(name) \ + if (name < 0) { \ + mju_warning("Invalid model: %s is negative (%lld).", #name, (long long)name); \ + return; \ + } \ + if (name >= MAX_ARRAY_SIZE && \ + strcmp(#name, "ntexdata") != 0 && strcmp(#name, "ntextdata") != 0) { \ + mju_warning("Invalid model: %s is too large. Expected < %d. Got %lld.", \ + #name, MAX_ARRAY_SIZE, (long long)name); \ + return; \ + } + MJMODEL_SIZES + #undef X + + // suppress unused variable warnings + (void)nnames_map; (void)nJmom; (void)ngravcomp; (void)nemax; (void)njmax; + (void)nconmax; (void)nuserdata; (void)nsensordata; (void)npluginstate; (void)narena; (void)nbuffer; + } + + // nbody should always be positive + if (nbody == 0) { + mju_warning("Invalid model: nbody == 0"); + return; + } + // allocate mjModel if (!allocate) { m = *dest; @@ -336,29 +354,6 @@ void mj_makeModel(mjModel** dest, m->nnames_map = mjLOAD_MULTIPLE * nnames_map; m->npaths = npaths; -#define X(name) \ - if ((m->name) < 0) { \ - if (allocate) mju_free(m); \ - mju_warning("Invalid model: negative " #name); \ - return; \ - } - MJMODEL_INTS; -#undef X - - // nbody should always be positive - if (m->nbody == 0) { - if (allocate) mju_free(m); - mju_warning("Invalid model: nbody == 0"); - return; - } - - // nmocap is going to get multiplied by 4, and shouldn't overflow - if (m->nmocap >= MAX_ARRAY_SIZE) { - if (allocate) mju_free(m); - mju_warning("Invalid model: nmocap too large"); - return; - } - // compute buffer size m->nbuffer = 0; #define X(type, name, nr, nc) \ @@ -484,10 +479,10 @@ void mjv_copyModel(mjModel* dest, const mjModel* src) { // save model to binary file, or memory buffer of szbuf>0 void mj_saveModel(const mjModel* m, const char* filename, void* buffer, int buffer_sz) { FILE* fp = 0; - int ptrbuf = 0; + mjtSize ptrbuf = 0; // standard header - int header[NHEADER] = {ID, sizeof(mjtNum), getnint(), getnbuffer(), getnptr()}; + int header[NHEADER] = {ID, sizeof(mjtNum), getnsize(), mj_version(), getnptr()}; // open file for writing if no buffer if (!buffer) { @@ -502,7 +497,7 @@ void mj_saveModel(const mjModel* m, const char* filename, void* buffer, int buff if (fp) { fwrite(header, sizeof(int), NHEADER, fp); #define X(name) fwrite(&m->name, sizeof(m->name), 1, fp); - MJMODEL_INTS + MJMODEL_SIZES #undef X fwrite((void*)&m->opt, sizeof(mjOption), 1, fp); fwrite((void*)&m->vis, sizeof(mjVisual), 1, fp); @@ -517,7 +512,7 @@ void mj_saveModel(const mjModel* m, const char* filename, void* buffer, int buff } else { bufwrite(header, sizeof(header), buffer_sz, buffer, &ptrbuf); #define X(name) bufwrite(&m->name, sizeof(m->name), buffer_sz, buffer, &ptrbuf); - MJMODEL_INTS + MJMODEL_SIZES #undef X bufwrite((void*)&m->opt, sizeof(mjOption), buffer_sz, buffer, &ptrbuf); bufwrite((void*)&m->vis, sizeof(mjVisual), buffer_sz, buffer, &ptrbuf); @@ -539,7 +534,7 @@ void mj_saveModel(const mjModel* m, const char* filename, void* buffer, int buff // load binary MJB model mjModel* mj_loadModelBuffer(const void* buffer, int buffer_sz) { - int ptrbuf = 0; + mjtSize ptrbuf = 0; mjModel *m = 0; if (buffer_sz < NHEADER*sizeof(int)) { @@ -551,7 +546,7 @@ mjModel* mj_loadModelBuffer(const void* buffer, int buffer_sz) { bufread(header, NHEADER*sizeof(int), buffer_sz, buffer, &ptrbuf); // check header - int expected_header[NHEADER] = {ID, sizeof(mjtNum), getnint(), getnbuffer(), getnptr()}; + int expected_header[NHEADER] = {ID, sizeof(mjtNum), getnsize(), mj_version(), getnptr()}; for (int i=0; i < NHEADER; i++) { if (header[i] != expected_header[i]) { switch (i) { @@ -564,11 +559,11 @@ mjModel* mj_loadModelBuffer(const void* buffer, int buffer_sz) { return NULL; case 2: - mju_warning("Model and executable have different number of ints in mjModel"); + mju_warning("Model and executable have different number of sizes in mjModel"); return NULL; case 3: - mju_warning("Model and executable have different number of size_t members in mjModel"); + mju_warning("Model and executable use different MuJoCo version"); return NULL; default: @@ -578,35 +573,39 @@ mjModel* mj_loadModelBuffer(const void* buffer, int buffer_sz) { } } - if (ptrbuf + sizeof(int)*getnint() + sizeof(mjtSize)*getnbuffer() > buffer_sz) { + int nsize = getnsize(); // number of mjtSize fields in mjModel + + if (ptrbuf + sizeof(mjtSize)*nsize > buffer_sz) { mju_warning("Truncated model file - ran out of data while reading sizes"); return NULL; } // read mjModel construction fields - int ints[256]; - bufread(ints, sizeof(int)*getnint(), buffer_sz, buffer, &ptrbuf); + mjtSize sizes[256]; + bufread(sizes, sizeof(mjtSize)*nsize, buffer_sz, buffer, &ptrbuf); // allocate new mjModel mj_makeModel(&m, - ints[0], ints[1], ints[2], ints[3], ints[4], ints[5], ints[6], - ints[7], ints[8], ints[9], ints[10], ints[11], ints[12], ints[13], - ints[14], ints[15], ints[16], ints[17], ints[18], ints[19], ints[20], - ints[21], ints[22], ints[23], ints[24], ints[25], ints[26], ints[27], - ints[28], ints[29], ints[30], ints[31], ints[32], ints[33], ints[34], - ints[35], ints[36], ints[37], ints[38], ints[39], ints[40], ints[41], - ints[42], ints[43], ints[44], ints[45], ints[46], ints[47], ints[48], - ints[49], ints[50], ints[51], ints[52], ints[53], ints[54], ints[55], - ints[56], ints[57], ints[58], ints[59], ints[60], ints[61], ints[62], - ints[63], ints[64], ints[65], ints[66], ints[67], ints[68], ints[69], - ints[70], ints[71], ints[72], ints[73], ints[74], ints[75], ints[76]); + sizes[0], sizes[1], sizes[2], sizes[3], sizes[4], sizes[5], sizes[6], + sizes[7], sizes[8], sizes[9], sizes[10], sizes[11], sizes[12], sizes[13], + sizes[14], sizes[15], sizes[16], sizes[17], sizes[18], sizes[19], sizes[20], + sizes[21], sizes[22], sizes[23], sizes[24], sizes[25], sizes[26], sizes[27], + sizes[28], sizes[29], sizes[30], sizes[31], sizes[32], sizes[33], sizes[34], + sizes[35], sizes[36], sizes[37], sizes[38], sizes[39], sizes[40], sizes[41], + sizes[42], sizes[43], sizes[44], sizes[45], sizes[46], sizes[47], sizes[48], + sizes[49], sizes[50], sizes[51], sizes[52], sizes[53], sizes[54], sizes[55], + sizes[56], sizes[57], sizes[58], sizes[59], sizes[60], sizes[61], sizes[62], + sizes[63], sizes[64], sizes[65], sizes[66], sizes[67], sizes[68], sizes[69], + sizes[70], sizes[71], sizes[72], sizes[73], sizes[74], sizes[75], sizes[76]); - // read mjModel mjtSize fields - mjtSize sizes[8]; - bufread(sizes, sizeof(mjtSize)*getnbuffer(), buffer_sz, buffer, &ptrbuf); + // mj_makeModel may fail if the input buffer has invalid sizes + if (!m) { + mju_warning("Invalid sizes, unable to load model"); + return NULL; + } - // check mjtSize fields - if (!m || m->nbuffer != sizes[getnbuffer()-1]) { + // check buffer size (last mjtSize field is nbuffer) + if (m->nbuffer != sizes[nsize-1]) { mju_warning("Corrupted model, wrong nbuffer field"); mj_deleteModel(m); return NULL; @@ -615,10 +614,9 @@ mjModel* mj_loadModelBuffer(const void* buffer, int buffer_sz) { // set integer fields { int int_idx = 0; - int size_idx = 0; #define X(name) \ - m->name = _Generic(m->name, mjtSize: sizes[size_idx++], default: ints[int_idx++]); - MJMODEL_INTS + m->name = sizes[int_idx++]; + MJMODEL_SIZES #undef X } @@ -675,8 +673,8 @@ void mj_deleteModel(mjModel* m) { // size of buffer needed to hold model mjtSize mj_sizeModel(const mjModel* m) { mjtSize size = ( - sizeof(int)*(NHEADER+getnint()) - + sizeof(mjtSize)*getnbuffer() + sizeof(int)*NHEADER + + sizeof(mjtSize)*getnsize() + sizeof(mjOption) + sizeof(mjVisual) + sizeof(mjStatistic)); @@ -1832,14 +1830,14 @@ const char* mj_validateReferences(const mjModel* m) { } } for (int i=0; i < m->nhfield; i++) { - int hfield_adr = m->hfield_adr[i] + m->hfield_nrow[i]*m->hfield_ncol[i]; + mjtSize hfield_adr = m->hfield_adr[i] + ((mjtSize) m->hfield_nrow[i]) * m->hfield_ncol[i]; if (hfield_adr > m->nhfielddata || m->hfield_adr[i] < 0) { return "Invalid model: hfield_adr out of bounds."; } } for (int i=0; i < m->ntex; i++) { - int tex_adr = m->tex_adr[i] + m->tex_nchannel[i]*m->tex_height[i]*m->tex_width[i]; - if (tex_adr > m->ntexdata || m->tex_adr[i] < 0) { + mjtSize nbytes = ((mjtSize) m->tex_nchannel[i]) * m->tex_height[i] * m->tex_width[i]; + if (m->tex_adr[i] + nbytes > m->ntexdata || m->tex_adr[i] < 0) { return "Invalid model: tex_adr out of bounds."; } } diff --git a/src/engine/engine_io.h b/src/engine/engine_io.h index d7101e69..918ad5a3 100644 --- a/src/engine/engine_io.h +++ b/src/engine/engine_io.h @@ -48,18 +48,22 @@ void mj_defaultStatistic(mjStatistic* stat); // allocate mjModel void mj_makeModel(mjModel** dest, - int nq, int nv, int nu, int na, int nbody, int nbvh, int nbvhstatic, int nbvhdynamic, int noct, - int njnt, int ntree, int nM, int nB, int nC, int nD, int ngeom, int nsite, int ncam, int nlight, - int nflex, int nflexnode, int nflexvert, int nflexedge, int nflexelem, int nflexelemdata, - int nflexelemedge, int nflexshelldata, int nflexevpair, int nflextexcoord, int nJfe, int nJfv, - int nmesh, int nmeshvert, int nmeshnormal, int nmeshtexcoord, int nmeshface, int nmeshgraph, - int nmeshpoly, int nmeshpolyvert, int nmeshpolymap, int nskin, int nskinvert, int nskintexvert, - int nskinface, int nskinbone, int nskinbonevert, int nhfield, int nhfielddata, int ntex, - int ntexdata, int nmat, int npair, int nexclude, int neq, int ntendon, int nwrap, int nsensor, - int nnumeric, int nnumericdata, int ntext, int ntextdata, int ntuple, int ntupledata, int nkey, - int nmocap, int nplugin, int npluginattr, int nuser_body, int nuser_jnt, int nuser_geom, - int nuser_site, int nuser_cam, int nuser_tendon, int nuser_actuator, int nuser_sensor, - int nnames, int npaths); + mjtSize nq, mjtSize nv, mjtSize nu, mjtSize na, mjtSize nbody, mjtSize nbvh, mjtSize nbvhstatic, + mjtSize nbvhdynamic, mjtSize noct, mjtSize njnt, mjtSize ntree, mjtSize nM, mjtSize nB, + mjtSize nC, mjtSize nD, mjtSize ngeom, mjtSize nsite, mjtSize ncam, mjtSize nlight, + mjtSize nflex, mjtSize nflexnode, mjtSize nflexvert, mjtSize nflexedge, mjtSize nflexelem, + mjtSize nflexelemdata, mjtSize nflexelemedge, mjtSize nflexshelldata, mjtSize nflexevpair, + mjtSize nflextexcoord, mjtSize nJfe, mjtSize nJfv, mjtSize nmesh, mjtSize nmeshvert, + mjtSize nmeshnormal, mjtSize nmeshtexcoord, mjtSize nmeshface, mjtSize nmeshgraph, + mjtSize nmeshpoly, mjtSize nmeshpolyvert, mjtSize nmeshpolymap, mjtSize nskin, + mjtSize nskinvert, mjtSize nskintexvert, mjtSize nskinface, mjtSize nskinbone, + mjtSize nskinbonevert, mjtSize nhfield, mjtSize nhfielddata, mjtSize ntex, mjtSize ntexdata, + mjtSize nmat, mjtSize npair, mjtSize nexclude, mjtSize neq, mjtSize ntendon, mjtSize nwrap, + mjtSize nsensor, mjtSize nnumeric, mjtSize nnumericdata, mjtSize ntext, mjtSize ntextdata, + mjtSize ntuple, mjtSize ntupledata, mjtSize nkey, mjtSize nmocap, mjtSize nplugin, + mjtSize npluginattr, mjtSize nuser_body, mjtSize nuser_jnt, mjtSize nuser_geom, + mjtSize nuser_site, mjtSize nuser_cam, mjtSize nuser_tendon, mjtSize nuser_actuator, + mjtSize nuser_sensor, mjtSize nnames, mjtSize npaths); // copy mjModel; allocate new if dest is NULL MJAPI mjModel* mj_copyModel(mjModel* dest, const mjModel* src); diff --git a/src/engine/engine_print.c b/src/engine/engine_print.c index 90c04b06..08e2af3e 100644 --- a/src/engine/engine_print.c +++ b/src/engine/engine_print.c @@ -551,7 +551,7 @@ void mj_printFormattedModel(const mjModel* m, const char* filename, const char* fprintf(fp, "\n"); \ } - MJMODEL_INTS + MJMODEL_SIZES #undef X fprintf(fp, "\n"); @@ -636,7 +636,7 @@ void mj_printFormattedModel(const mjModel* m, const char* filename, const char* (void)nu; (void)nmocap; - const int* object_class; + const mjtSize* object_class; #define X(type, name, num, sz) \ if (&m->num == object_class && sz > 0) { \ @@ -645,6 +645,7 @@ void mj_printFormattedModel(const mjModel* m, const char* filename, const char* float: float_format, \ int: INT_FORMAT, \ mjtByte: INT_FORMAT, \ + mjtSize: SIZE_FORMAT, \ default: NULL); \ if (format) { \ fprintf(fp, " "); \ diff --git a/src/engine/engine_support.c b/src/engine/engine_support.c index e54ae1ce..91a36e82 100644 --- a/src/engine/engine_support.c +++ b/src/engine/engine_support.c @@ -14,6 +14,7 @@ #include "engine/engine_support.h" +#include // IWYU pragma: keep #include #include @@ -343,7 +344,7 @@ void mj_copyState(const mjModel* m, const mjData* src, mjData* dst, int sig) { void mj_setKeyframe(mjModel* m, const mjData* d, int k) { // check keyframe index if (k >= m->nkey) { - mjERROR("index must be smaller than %d (keyframes allocated in model)", m->nkey); + mjERROR("index must be smaller than %" PRId64 " (keyframes allocated in model)", m->nkey); } if (k < 0) { mjERROR("keyframe index cannot be negative"); diff --git a/src/render/render_context.c b/src/render/render_context.c index 4a47f1df..faebe866 100644 --- a/src/render/render_context.c +++ b/src/render/render_context.c @@ -14,6 +14,7 @@ #include "render/render_context.h" +#include // IWYU pragma: keep #include #include #include @@ -1308,7 +1309,7 @@ static void makeMaterial(const mjModel* m, mjrContext* con) { for (int i=0; i < m->ntex; i++) { if (m->tex_type[i] == mjTEXTURE_SKYBOX) { if (m->nmat >= mjMAXMATERIAL-2) { - mju_error("With skybox, maximum number of materials is %d, got %d", + mju_error("With skybox, maximum number of materials is %d, got %" PRId64, mjMAXMATERIAL-1, m->nmat); } for (int j=0; j < mjNTEXROLE; j++) { @@ -1321,7 +1322,7 @@ static void makeMaterial(const mjModel* m, mjrContext* con) { } if (m->nmat >= mjMAXMATERIAL-1) { - mju_error("Maximum number of materials is %d, got %d", mjMAXMATERIAL, m->nmat); + mju_error("Maximum number of materials is %d, got %" PRId64, mjMAXMATERIAL, m->nmat); } for (int i=0; i < m->nmat; i++) { if (m->mat_texid[i*mjNTEXROLE + mjTEXROLE_RGB] >= 0) { diff --git a/src/user/user_model.cc b/src/user/user_model.cc index 4c2c8677..be61c7b8 100644 --- a/src/user/user_model.cc +++ b/src/user/user_model.cc @@ -2218,28 +2218,45 @@ void mjCModel::SetSizes() { } // nhfielddata - for (int i=0; i < nhfield; i++)nhfielddata += hfields_[i]->nrow * hfields_[i]->ncol; + for (int i=0; i < nhfield; i++) { + nhfielddata += static_cast(hfields_[i]->nrow) * hfields_[i]->ncol; + } // ntexdata - for (int i=0; i < ntex; i++)ntexdata += textures_[i]->nchannel * textures_[i]->width * textures_[i]->height; + for (int i=0; i < ntex; i++) { + const mjCTexture* tex = textures_[i]; + ntexdata += static_cast(tex->nchannel) * tex->width * tex->height; + } // nwrap - for (int i=0; i < ntendon; i++)nwrap += (int)tendons_[i]->path.size(); + for (int i=0; i < ntendon; i++) { + nwrap += (int)tendons_[i]->path.size(); + } // nsensordata - for (int i=0; i < nsensor; i++)nsensordata += sensors_[i]->dim; + for (int i=0; i < nsensor; i++) { + nsensordata += sensors_[i]->dim; + } // nnumericdata - for (int i=0; i < nnumeric; i++)nnumericdata += numerics_[i]->size; + for (int i=0; i < nnumeric; i++) { + nnumericdata += numerics_[i]->size; + } // ntextdata - for (int i=0; i < ntext; i++)ntextdata += (int)texts_[i]->data_.size() + 1; + for (int i=0; i < ntext; i++) { + ntextdata += (int)texts_[i]->data_.size() + 1; + } // ntupledata - for (int i=0; i < ntuple; i++)ntupledata += (int)tuples_[i]->objtype_.size(); + for (int i=0; i < ntuple; i++) { + ntupledata += (int)tuples_[i]->objtype_.size(); + } // npluginattr - for (int i=0; i < nplugin; i++)npluginattr += (int)plugins_[i]->flattened_attributes.size(); + for (int i=0; i < nplugin; i++) { + npluginattr += (int)plugins_[i]->flattened_attributes.size(); + } // nnames nnames = (int)modelname_.size() + 1; @@ -3185,10 +3202,10 @@ int mjCModel::CountNJmom(const mjModel* m) { // copy objects outside kinematic tree void mjCModel::CopyObjects(mjModel* m) { - int adr, bone_adr, vert_adr, node_adr, normal_adr, face_adr, texcoord_adr, oct_adr; - int edge_adr, elem_adr, elemdata_adr, elemedge_adr, shelldata_adr, evpair_adr; - int bonevert_adr, graph_adr, data_adr, bvh_adr; - int poly_adr, polymap_adr, polyvert_adr; + mjtSize adr, bone_adr, vert_adr, node_adr, normal_adr, face_adr, texcoord_adr, oct_adr; + mjtSize edge_adr, elem_adr, elemdata_adr, elemedge_adr, shelldata_adr, evpair_adr; + mjtSize bonevert_adr, graph_adr, data_adr, bvh_adr; + mjtSize poly_adr, polymap_adr, polyvert_adr; // sizes outside call to mj_makeModel m->nemax = nemax; @@ -3556,7 +3573,8 @@ void mjCModel::CopyObjects(mjModel* m) { m->hfield_adr[i] = data_adr; // copy elevation data - memcpy(m->hfield_data + data_adr, phf->data.data(), phf->nrow*phf->ncol*sizeof(float)); + memcpy(m->hfield_data + data_adr, phf->data.data(), + static_cast(phf->nrow) * phf->ncol * sizeof(float)); // advance counter data_adr += phf->nrow*phf->ncol; @@ -3577,11 +3595,11 @@ void mjCModel::CopyObjects(mjModel* m) { m->tex_adr[i] = data_adr; // copy rgb data - memcpy(m->tex_data + data_adr, ptex->data_.data(), - ptex->nchannel * ptex->width * ptex->height); + mjtSize nbytes = static_cast(ptex->nchannel) * ptex->width * ptex->height; + memcpy(m->tex_data + data_adr, ptex->data_.data(), nbytes); // advance counter - data_adr += ptex->nchannel * ptex->width * ptex->height; + data_adr += nbytes; } // materials diff --git a/src/user/user_model.h b/src/user/user_model.h index 27b9ee48..66da0ec5 100644 --- a/src/user/user_model.h +++ b/src/user/user_model.h @@ -54,81 +54,81 @@ class mjCModel_ : public mjsElement { std::string suffix; protected: - bool compiled; // already compiled flag + bool compiled; // already compiled flag // sizes set from object list lengths - int nbody; // number of bodies - int njnt; // number of joints - int ngeom; // number of geoms - int nsite; // number of sites - int ncam; // number of cameras - int nlight; // number of lights - int nflex; // number of flexes - int nmesh; // number of meshes - int nskin; // number of skins - int nhfield; // number of height fields - int ntex; // number of textures - int nmat; // number of materials - int npair; // number of geom pairs in pair array - int nexclude; // number of excluded body pairs - int neq; // number of equality constraints - int ntendon; // number of tendons - int nsensor; // number of sensors - int nnumeric; // number of numeric fields - int ntext; // number of text fields - int ntuple; // number of tuple fields - int nmocap; // number of mocap bodies - int nplugin; // number of plugin instances + mjtSize nbody; // number of bodies + mjtSize njnt; // number of joints + mjtSize ngeom; // number of geoms + mjtSize nsite; // number of sites + mjtSize ncam; // number of cameras + mjtSize nlight; // number of lights + mjtSize nflex; // number of flexes + mjtSize nmesh; // number of meshes + mjtSize nskin; // number of skins + mjtSize nhfield; // number of height fields + mjtSize ntex; // number of textures + mjtSize nmat; // number of materials + mjtSize npair; // number of geom pairs in pair array + mjtSize nexclude; // number of excluded body pairs + mjtSize neq; // number of equality constraints + mjtSize ntendon; // number of tendons + mjtSize nsensor; // number of sensors + mjtSize nnumeric; // number of numeric fields + mjtSize ntext; // number of text fields + mjtSize ntuple; // number of tuple fields + mjtSize nmocap; // number of mocap bodies + mjtSize nplugin; // number of plugin instances // sizes computed by Compile - int nq; // number of generalized coordinates = dim(qpos) - int nv; // number of degrees of freedom = dim(qvel) - int nu; // number of actuators/controls - int na; // number of activation variables - int ntree; // number of trees - int nbvh; // number of total boundary volume hierarchies - int nbvhstatic; // number of static boundary volume hierarchies - int nbvhdynamic; // number of dynamic boundary volume hierarchies - int noct; // number of total octree cells - int nflexnode; // number of nodes in all flexes - int nflexvert; // number of vertices in all flexes - int nflexedge; // number of edges in all flexes - int nflexelem; // number of elements in all flexes - int nflexelemdata; // number of element vertex ids in all flexes - int nflexelemedge; // number of element edges 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 nflextexcoord; // number of vertex texture coordinates in all flexes - int nJfe; // number of non-zeros in sparse flex edge constraint Jacobian - int nJfv; // number of non-zeros in sparse flex vertex constraint Jacobian - int nmeshvert; // number of vertices in all meshes - int nmeshnormal; // number of normals in all meshes - int nmeshtexcoord; // number of texture coordinates in all meshes - int nmeshface; // number of triangular faces in all meshes - int nmeshpoly; // number of polygon faces in all meshes - int nmeshgraph; // number of ints in mesh auxiliary data - int nmeshpolyvert; // number of vertices in all polygon faces - int nmeshpolymap; // number of polygons in vertex map - int nskinvert; // number of vertices in all skins - int nskintexvert; // number of vertices with texcoord in all skins - int nskinface; // number of faces in all skins - int nskinbone; // number of bones in all skins - int nskinbonevert; // number of vertices in all skins - int nhfielddata; // number of data points in all hfields - int ntexdata; // number of texture bytes - int nwrap; // number of wrap objects in all tendon paths - int nsensordata; // number of mjtNums in sensor data vector - int nnumericdata; // number of mjtNums in all custom fields - int ntextdata; // number of chars in all text fields, including 0 - int ntupledata; // number of objects in all tuple fields - int npluginattr; // number of chars in all plugin config attributes - int nnames; // number of chars in all names - int npaths; // number of chars in all paths - int nM; // number of non-zeros in sparse inertia matrix - int nB; // number of non-zeros in sparse body-dof matrix - int nC; // number of non-zeros in reduced sparse dof-dof matrix - int nD; // number of non-zeros in sparse dof-dof matrix - int nJmom; // number of non-zeros in sparse actuator_moment matrix + mjtSize nq; // number of generalized coordinates = dim(qpos) + mjtSize nv; // number of degrees of freedom = dim(qvel) + mjtSize nu; // number of actuators/controls + mjtSize na; // number of activation variables + mjtSize ntree; // number of trees + mjtSize nbvh; // number of total boundary volume hierarchies + mjtSize nbvhstatic; // number of static boundary volume hierarchies + mjtSize nbvhdynamic; // number of dynamic boundary volume hierarchies + mjtSize noct; // number of total octree cells + mjtSize nflexnode; // number of nodes in all flexes + mjtSize nflexvert; // number of vertices in all flexes + mjtSize nflexedge; // number of edges in all flexes + mjtSize nflexelem; // number of elements in all flexes + mjtSize nflexelemdata; // number of element vertex ids in all flexes + mjtSize nflexelemedge; // number of element edges in all flexes + mjtSize nflexshelldata; // number of shell fragment vertex ids in all flexes + mjtSize nflexevpair; // number of element-vertex pairs in all flexes + mjtSize nflextexcoord; // number of vertex texture coordinates in all flexes + mjtSize nJfe; // number of non-zeros in sparse flex edge constraint Jacobian + mjtSize nJfv; // number of non-zeros in sparse flex vertex constraint Jacobian + mjtSize nmeshvert; // number of vertices in all meshes + mjtSize nmeshnormal; // number of normals in all meshes + mjtSize nmeshtexcoord; // number of texture coordinates in all meshes + mjtSize nmeshface; // number of triangular faces in all meshes + mjtSize nmeshpoly; // number of polygon faces in all meshes + mjtSize nmeshgraph; // number of ints in mesh auxiliary data + mjtSize nmeshpolyvert; // number of vertices in all polygon faces + mjtSize nmeshpolymap; // number of polygons in vertex map + mjtSize nskinvert; // number of vertices in all skins + mjtSize nskintexvert; // number of vertices with texcoord in all skins + mjtSize nskinface; // number of faces in all skins + mjtSize nskinbone; // number of bones in all skins + mjtSize nskinbonevert; // number of vertices in all skins + mjtSize nhfielddata; // number of data points in all hfields + mjtSize ntexdata; // number of texture bytes + mjtSize nwrap; // number of wrap objects in all tendon paths + mjtSize nsensordata; // number of mjtNums in sensor data vector + mjtSize nnumericdata; // number of mjtNums in all custom fields + mjtSize ntextdata; // number of chars in all text fields, including 0 + mjtSize ntupledata; // number of objects in all tuple fields + mjtSize npluginattr; // number of chars in all plugin config attributes + mjtSize nnames; // number of chars in all names + mjtSize npaths; // number of chars in all paths + mjtSize nM; // number of non-zeros in sparse inertia matrix + mjtSize nB; // number of non-zeros in sparse body-dof matrix + mjtSize nC; // number of non-zeros in reduced sparse dof-dof matrix + mjtSize nD; // number of non-zeros in sparse dof-dof matrix + mjtSize nJmom; // number of non-zeros in sparse actuator_moment matrix // statistics, as computed by mj_setConst double meaninertia_auto; // mean diagonal inertia, as computed by mj_setConst diff --git a/src/user/user_objects.cc b/src/user/user_objects.cc index e60f9d93..124c8261 100644 --- a/src/user/user_objects.cc +++ b/src/user/user_objects.cc @@ -4825,7 +4825,7 @@ void mjCTexture::BuiltinCube(void) { if (w > std::numeric_limits::max() / w) { throw mjCError(this, "Cube texture width is too large."); } - int ww = width*width; + mjtSize ww = width*width; // convert fixed colors for (int j = 0; j < 3; j++) { @@ -4838,7 +4838,7 @@ void mjCTexture::BuiltinCube(void) { // gradient if (builtin == mjBUILTIN_GRADIENT) { - if (ww > std::numeric_limits::max() / 18) { + if (ww > std::numeric_limits::max() / 18) { throw mjCError(this, "Gradient texture width is too large."); } for (int r = 0; r < w; r++) { @@ -5176,7 +5176,7 @@ void mjCTexture::LoadCubeSingle(std::string filename, const mjVFS* vfs) { // allocate data std::int64_t size = static_cast(width)*height; - if (size >= std::numeric_limits::max() / 3 || size <= 0) { + if (size >= std::numeric_limits::max() / 3 || size <= 0) { throw mjCError(this, "Cube texture too large"); } try { @@ -5293,7 +5293,7 @@ void mjCTexture::LoadCubeSeparate(const mjVFS* vfs) { } height = 6*width; std::int64_t size = static_cast(width)*height; - if (size >= std::numeric_limits::max() / 3 || size <= 0) { + if (size >= std::numeric_limits::max() / 3 || size <= 0) { throw mjCError(this, "PNG texture too large"); } try { @@ -5378,7 +5378,7 @@ void mjCTexture::Compile(const mjVFS* vfs) { } std::int64_t size = static_cast(width)*height; - if (size >= std::numeric_limits::max() / nchannel || size <= 0) { + if (size >= std::numeric_limits::max() / nchannel || size <= 0) { throw mjCError(this, "Builtin texture too large"); } // allocate data diff --git a/test/fixture.cc b/test/fixture.cc index 4229578e..0e4f14bc 100644 --- a/test/fixture.cc +++ b/test/fixture.cc @@ -203,6 +203,10 @@ auto Compare(unsigned char val1, unsigned char val2) { return val1 != val2; } +auto Compare(mjtSize val1, mjtSize val2) { + return val1 > val2 ? val1 - val2 : val2 - val1; +} + // The maximum spacing between a normalised floating point number x and an // adjacent normalised number is 2 epsilon |x|; a factor 10 is added accounting // for losses during non-idempotent operations such as vector normalizations. @@ -245,7 +249,7 @@ mjtNum CompareModel(const mjModel* m1, const mjModel* m2, field = #name; \ } \ } - MJMODEL_INTS + MJMODEL_SIZES #undef X if (maxdif > 0) return maxdif; diff --git a/test/header_test.cc b/test/header_test.cc index 83c79fcc..48f6e74d 100644 --- a/test/header_test.cc +++ b/test/header_test.cc @@ -126,7 +126,7 @@ TEST_F(HeaderTest, MjModelIntsOrdered) { std::vector> ints; #define X(name) ints.push_back({static_cast(&m.name), #name}); - MJMODEL_INTS + MJMODEL_SIZES #undef X CheckAddressOrdering(ints, "MJMODEL_INT"); diff --git a/test/xml/xml_native_reader_test.cc b/test/xml/xml_native_reader_test.cc index 0932f1d3..3df1656d 100644 --- a/test/xml/xml_native_reader_test.cc +++ b/test/xml/xml_native_reader_test.cc @@ -892,11 +892,11 @@ TEST_F(XMLReaderTest, LargeTextureTest) { @@ -906,30 +906,7 @@ TEST_F(XMLReaderTest, LargeTextureTest) { std::array error; mjModel* model = LoadModelFromString(xml, error.data(), error.size()); - EXPECT_THAT(model, IsNull()); - mj_deleteModel(model); -} - -TEST_F(XMLReaderTest, HugeTextureTest) { - static constexpr char xml[] = R"( - - - - - - - )"; - - std::array error; - mjModel* model = LoadModelFromString(xml, error.data(), error.size()); - - EXPECT_THAT(model, IsNull()); + EXPECT_THAT(model, NotNull()); mj_deleteModel(model); } diff --git a/unity/Runtime/Bindings/MjBindings.cs b/unity/Runtime/Bindings/MjBindings.cs index fe515c12..9406207e 100644 --- a/unity/Runtime/Bindings/MjBindings.cs +++ b/unity/Runtime/Bindings/MjBindings.cs @@ -5286,92 +5286,92 @@ public unsafe struct mjStatistic_ { [StructLayout(LayoutKind.Sequential)] public unsafe struct mjModel_ { - public int nq; - public int nv; - public int nu; - public int na; - public int nbody; - public int nbvh; - public int nbvhstatic; - public int nbvhdynamic; - public int noct; - public int njnt; - public int ntree; - public int nM; - public int nB; - public int nC; - public int nD; - public int ngeom; - public int nsite; - public int ncam; - public int nlight; - public int nflex; - public int nflexnode; - public int nflexvert; - public int nflexedge; - public int nflexelem; - public int nflexelemdata; - public int nflexelemedge; - public int nflexshelldata; - public int nflexevpair; - public int nflextexcoord; - public int nJfe; - public int nJfv; - public int nmesh; - public int nmeshvert; - public int nmeshnormal; - public int nmeshtexcoord; - public int nmeshface; - public int nmeshgraph; - public int nmeshpoly; - public int nmeshpolyvert; - public int nmeshpolymap; - public int nskin; - public int nskinvert; - public int nskintexvert; - public int nskinface; - public int nskinbone; - public int nskinbonevert; - public int nhfield; - public int nhfielddata; - public int ntex; - public int ntexdata; - public int nmat; - public int npair; - public int nexclude; - public int neq; - public int ntendon; - public int nwrap; - public int nsensor; - public int nnumeric; - public int nnumericdata; - public int ntext; - public int ntextdata; - public int ntuple; - public int ntupledata; - public int nkey; - public int nmocap; - public int nplugin; - public int npluginattr; - public int nuser_body; - public int nuser_jnt; - public int nuser_geom; - public int nuser_site; - public int nuser_cam; - public int nuser_tendon; - public int nuser_actuator; - public int nuser_sensor; - public int nnames; - public int npaths; - public int nnames_map; - public int nJmom; - public int ngravcomp; - public int nemax; - public int njmax; - public int nconmax; - public int nuserdata; - public int nsensordata; - public int npluginstate; + public Int64 nq; + public Int64 nv; + public Int64 nu; + public Int64 na; + public Int64 nbody; + public Int64 nbvh; + public Int64 nbvhstatic; + public Int64 nbvhdynamic; + public Int64 noct; + public Int64 njnt; + public Int64 ntree; + public Int64 nM; + public Int64 nB; + public Int64 nC; + public Int64 nD; + public Int64 ngeom; + public Int64 nsite; + public Int64 ncam; + public Int64 nlight; + public Int64 nflex; + public Int64 nflexnode; + public Int64 nflexvert; + public Int64 nflexedge; + public Int64 nflexelem; + public Int64 nflexelemdata; + public Int64 nflexelemedge; + public Int64 nflexshelldata; + public Int64 nflexevpair; + public Int64 nflextexcoord; + public Int64 nJfe; + public Int64 nJfv; + public Int64 nmesh; + public Int64 nmeshvert; + public Int64 nmeshnormal; + public Int64 nmeshtexcoord; + public Int64 nmeshface; + public Int64 nmeshgraph; + public Int64 nmeshpoly; + public Int64 nmeshpolyvert; + public Int64 nmeshpolymap; + public Int64 nskin; + public Int64 nskinvert; + public Int64 nskintexvert; + public Int64 nskinface; + public Int64 nskinbone; + public Int64 nskinbonevert; + public Int64 nhfield; + public Int64 nhfielddata; + public Int64 ntex; + public Int64 ntexdata; + public Int64 nmat; + public Int64 npair; + public Int64 nexclude; + public Int64 neq; + public Int64 ntendon; + public Int64 nwrap; + public Int64 nsensor; + public Int64 nnumeric; + public Int64 nnumericdata; + public Int64 ntext; + public Int64 ntextdata; + public Int64 ntuple; + public Int64 ntupledata; + public Int64 nkey; + public Int64 nmocap; + public Int64 nplugin; + public Int64 npluginattr; + public Int64 nuser_body; + public Int64 nuser_jnt; + public Int64 nuser_geom; + public Int64 nuser_site; + public Int64 nuser_cam; + public Int64 nuser_tendon; + public Int64 nuser_actuator; + public Int64 nuser_sensor; + public Int64 nnames; + public Int64 npaths; + public Int64 nnames_map; + public Int64 nJmom; + public Int64 ngravcomp; + public Int64 nemax; + public Int64 njmax; + public Int64 nconmax; + public Int64 nuserdata; + public Int64 nsensordata; + public Int64 npluginstate; public Int64 narena; public Int64 nbuffer; public mjOption_ opt; diff --git a/wasm/codegen/generated/bindings.cc b/wasm/codegen/generated/bindings.cc index b22474da..d27a4487 100644 --- a/wasm/codegen/generated/bindings.cc +++ b/wasm/codegen/generated/bindings.cc @@ -3410,532 +3410,532 @@ struct MjModel { mjModel* get() const; void set(mjModel* ptr); int nq() const { - return ptr_->nq; + return static_cast(ptr_->nq); } void set_nq(int value) { - ptr_->nq = value; + ptr_->nq = static_cast(value); } int nv() const { - return ptr_->nv; + return static_cast(ptr_->nv); } void set_nv(int value) { - ptr_->nv = value; + ptr_->nv = static_cast(value); } int nu() const { - return ptr_->nu; + return static_cast(ptr_->nu); } void set_nu(int value) { - ptr_->nu = value; + ptr_->nu = static_cast(value); } int na() const { - return ptr_->na; + return static_cast(ptr_->na); } void set_na(int value) { - ptr_->na = value; + ptr_->na = static_cast(value); } int nbody() const { - return ptr_->nbody; + return static_cast(ptr_->nbody); } void set_nbody(int value) { - ptr_->nbody = value; + ptr_->nbody = static_cast(value); } int nbvh() const { - return ptr_->nbvh; + return static_cast(ptr_->nbvh); } void set_nbvh(int value) { - ptr_->nbvh = value; + ptr_->nbvh = static_cast(value); } int nbvhstatic() const { - return ptr_->nbvhstatic; + return static_cast(ptr_->nbvhstatic); } void set_nbvhstatic(int value) { - ptr_->nbvhstatic = value; + ptr_->nbvhstatic = static_cast(value); } int nbvhdynamic() const { - return ptr_->nbvhdynamic; + return static_cast(ptr_->nbvhdynamic); } void set_nbvhdynamic(int value) { - ptr_->nbvhdynamic = value; + ptr_->nbvhdynamic = static_cast(value); } int noct() const { - return ptr_->noct; + return static_cast(ptr_->noct); } void set_noct(int value) { - ptr_->noct = value; + ptr_->noct = static_cast(value); } int njnt() const { - return ptr_->njnt; + return static_cast(ptr_->njnt); } void set_njnt(int value) { - ptr_->njnt = value; + ptr_->njnt = static_cast(value); } int ntree() const { - return ptr_->ntree; + return static_cast(ptr_->ntree); } void set_ntree(int value) { - ptr_->ntree = value; + ptr_->ntree = static_cast(value); } int nM() const { - return ptr_->nM; + return static_cast(ptr_->nM); } void set_nM(int value) { - ptr_->nM = value; + ptr_->nM = static_cast(value); } int nB() const { - return ptr_->nB; + return static_cast(ptr_->nB); } void set_nB(int value) { - ptr_->nB = value; + ptr_->nB = static_cast(value); } int nC() const { - return ptr_->nC; + return static_cast(ptr_->nC); } void set_nC(int value) { - ptr_->nC = value; + ptr_->nC = static_cast(value); } int nD() const { - return ptr_->nD; + return static_cast(ptr_->nD); } void set_nD(int value) { - ptr_->nD = value; + ptr_->nD = static_cast(value); } int ngeom() const { - return ptr_->ngeom; + return static_cast(ptr_->ngeom); } void set_ngeom(int value) { - ptr_->ngeom = value; + ptr_->ngeom = static_cast(value); } int nsite() const { - return ptr_->nsite; + return static_cast(ptr_->nsite); } void set_nsite(int value) { - ptr_->nsite = value; + ptr_->nsite = static_cast(value); } int ncam() const { - return ptr_->ncam; + return static_cast(ptr_->ncam); } void set_ncam(int value) { - ptr_->ncam = value; + ptr_->ncam = static_cast(value); } int nlight() const { - return ptr_->nlight; + return static_cast(ptr_->nlight); } void set_nlight(int value) { - ptr_->nlight = value; + ptr_->nlight = static_cast(value); } int nflex() const { - return ptr_->nflex; + return static_cast(ptr_->nflex); } void set_nflex(int value) { - ptr_->nflex = value; + ptr_->nflex = static_cast(value); } int nflexnode() const { - return ptr_->nflexnode; + return static_cast(ptr_->nflexnode); } void set_nflexnode(int value) { - ptr_->nflexnode = value; + ptr_->nflexnode = static_cast(value); } int nflexvert() const { - return ptr_->nflexvert; + return static_cast(ptr_->nflexvert); } void set_nflexvert(int value) { - ptr_->nflexvert = value; + ptr_->nflexvert = static_cast(value); } int nflexedge() const { - return ptr_->nflexedge; + return static_cast(ptr_->nflexedge); } void set_nflexedge(int value) { - ptr_->nflexedge = value; + ptr_->nflexedge = static_cast(value); } int nflexelem() const { - return ptr_->nflexelem; + return static_cast(ptr_->nflexelem); } void set_nflexelem(int value) { - ptr_->nflexelem = value; + ptr_->nflexelem = static_cast(value); } int nflexelemdata() const { - return ptr_->nflexelemdata; + return static_cast(ptr_->nflexelemdata); } void set_nflexelemdata(int value) { - ptr_->nflexelemdata = value; + ptr_->nflexelemdata = static_cast(value); } int nflexelemedge() const { - return ptr_->nflexelemedge; + return static_cast(ptr_->nflexelemedge); } void set_nflexelemedge(int value) { - ptr_->nflexelemedge = value; + ptr_->nflexelemedge = static_cast(value); } int nflexshelldata() const { - return ptr_->nflexshelldata; + return static_cast(ptr_->nflexshelldata); } void set_nflexshelldata(int value) { - ptr_->nflexshelldata = value; + ptr_->nflexshelldata = static_cast(value); } int nflexevpair() const { - return ptr_->nflexevpair; + return static_cast(ptr_->nflexevpair); } void set_nflexevpair(int value) { - ptr_->nflexevpair = value; + ptr_->nflexevpair = static_cast(value); } int nflextexcoord() const { - return ptr_->nflextexcoord; + return static_cast(ptr_->nflextexcoord); } void set_nflextexcoord(int value) { - ptr_->nflextexcoord = value; + ptr_->nflextexcoord = static_cast(value); } int nJfe() const { - return ptr_->nJfe; + return static_cast(ptr_->nJfe); } void set_nJfe(int value) { - ptr_->nJfe = value; + ptr_->nJfe = static_cast(value); } int nJfv() const { - return ptr_->nJfv; + return static_cast(ptr_->nJfv); } void set_nJfv(int value) { - ptr_->nJfv = value; + ptr_->nJfv = static_cast(value); } int nmesh() const { - return ptr_->nmesh; + return static_cast(ptr_->nmesh); } void set_nmesh(int value) { - ptr_->nmesh = value; + ptr_->nmesh = static_cast(value); } int nmeshvert() const { - return ptr_->nmeshvert; + return static_cast(ptr_->nmeshvert); } void set_nmeshvert(int value) { - ptr_->nmeshvert = value; + ptr_->nmeshvert = static_cast(value); } int nmeshnormal() const { - return ptr_->nmeshnormal; + return static_cast(ptr_->nmeshnormal); } void set_nmeshnormal(int value) { - ptr_->nmeshnormal = value; + ptr_->nmeshnormal = static_cast(value); } int nmeshtexcoord() const { - return ptr_->nmeshtexcoord; + return static_cast(ptr_->nmeshtexcoord); } void set_nmeshtexcoord(int value) { - ptr_->nmeshtexcoord = value; + ptr_->nmeshtexcoord = static_cast(value); } int nmeshface() const { - return ptr_->nmeshface; + return static_cast(ptr_->nmeshface); } void set_nmeshface(int value) { - ptr_->nmeshface = value; + ptr_->nmeshface = static_cast(value); } int nmeshgraph() const { - return ptr_->nmeshgraph; + return static_cast(ptr_->nmeshgraph); } void set_nmeshgraph(int value) { - ptr_->nmeshgraph = value; + ptr_->nmeshgraph = static_cast(value); } int nmeshpoly() const { - return ptr_->nmeshpoly; + return static_cast(ptr_->nmeshpoly); } void set_nmeshpoly(int value) { - ptr_->nmeshpoly = value; + ptr_->nmeshpoly = static_cast(value); } int nmeshpolyvert() const { - return ptr_->nmeshpolyvert; + return static_cast(ptr_->nmeshpolyvert); } void set_nmeshpolyvert(int value) { - ptr_->nmeshpolyvert = value; + ptr_->nmeshpolyvert = static_cast(value); } int nmeshpolymap() const { - return ptr_->nmeshpolymap; + return static_cast(ptr_->nmeshpolymap); } void set_nmeshpolymap(int value) { - ptr_->nmeshpolymap = value; + ptr_->nmeshpolymap = static_cast(value); } int nskin() const { - return ptr_->nskin; + return static_cast(ptr_->nskin); } void set_nskin(int value) { - ptr_->nskin = value; + ptr_->nskin = static_cast(value); } int nskinvert() const { - return ptr_->nskinvert; + return static_cast(ptr_->nskinvert); } void set_nskinvert(int value) { - ptr_->nskinvert = value; + ptr_->nskinvert = static_cast(value); } int nskintexvert() const { - return ptr_->nskintexvert; + return static_cast(ptr_->nskintexvert); } void set_nskintexvert(int value) { - ptr_->nskintexvert = value; + ptr_->nskintexvert = static_cast(value); } int nskinface() const { - return ptr_->nskinface; + return static_cast(ptr_->nskinface); } void set_nskinface(int value) { - ptr_->nskinface = value; + ptr_->nskinface = static_cast(value); } int nskinbone() const { - return ptr_->nskinbone; + return static_cast(ptr_->nskinbone); } void set_nskinbone(int value) { - ptr_->nskinbone = value; + ptr_->nskinbone = static_cast(value); } int nskinbonevert() const { - return ptr_->nskinbonevert; + return static_cast(ptr_->nskinbonevert); } void set_nskinbonevert(int value) { - ptr_->nskinbonevert = value; + ptr_->nskinbonevert = static_cast(value); } int nhfield() const { - return ptr_->nhfield; + return static_cast(ptr_->nhfield); } void set_nhfield(int value) { - ptr_->nhfield = value; + ptr_->nhfield = static_cast(value); } int nhfielddata() const { - return ptr_->nhfielddata; + return static_cast(ptr_->nhfielddata); } void set_nhfielddata(int value) { - ptr_->nhfielddata = value; + ptr_->nhfielddata = static_cast(value); } int ntex() const { - return ptr_->ntex; + return static_cast(ptr_->ntex); } void set_ntex(int value) { - ptr_->ntex = value; + ptr_->ntex = static_cast(value); } int ntexdata() const { - return ptr_->ntexdata; + return static_cast(ptr_->ntexdata); } void set_ntexdata(int value) { - ptr_->ntexdata = value; + ptr_->ntexdata = static_cast(value); } int nmat() const { - return ptr_->nmat; + return static_cast(ptr_->nmat); } void set_nmat(int value) { - ptr_->nmat = value; + ptr_->nmat = static_cast(value); } int npair() const { - return ptr_->npair; + return static_cast(ptr_->npair); } void set_npair(int value) { - ptr_->npair = value; + ptr_->npair = static_cast(value); } int nexclude() const { - return ptr_->nexclude; + return static_cast(ptr_->nexclude); } void set_nexclude(int value) { - ptr_->nexclude = value; + ptr_->nexclude = static_cast(value); } int neq() const { - return ptr_->neq; + return static_cast(ptr_->neq); } void set_neq(int value) { - ptr_->neq = value; + ptr_->neq = static_cast(value); } int ntendon() const { - return ptr_->ntendon; + return static_cast(ptr_->ntendon); } void set_ntendon(int value) { - ptr_->ntendon = value; + ptr_->ntendon = static_cast(value); } int nwrap() const { - return ptr_->nwrap; + return static_cast(ptr_->nwrap); } void set_nwrap(int value) { - ptr_->nwrap = value; + ptr_->nwrap = static_cast(value); } int nsensor() const { - return ptr_->nsensor; + return static_cast(ptr_->nsensor); } void set_nsensor(int value) { - ptr_->nsensor = value; + ptr_->nsensor = static_cast(value); } int nnumeric() const { - return ptr_->nnumeric; + return static_cast(ptr_->nnumeric); } void set_nnumeric(int value) { - ptr_->nnumeric = value; + ptr_->nnumeric = static_cast(value); } int nnumericdata() const { - return ptr_->nnumericdata; + return static_cast(ptr_->nnumericdata); } void set_nnumericdata(int value) { - ptr_->nnumericdata = value; + ptr_->nnumericdata = static_cast(value); } int ntext() const { - return ptr_->ntext; + return static_cast(ptr_->ntext); } void set_ntext(int value) { - ptr_->ntext = value; + ptr_->ntext = static_cast(value); } int ntextdata() const { - return ptr_->ntextdata; + return static_cast(ptr_->ntextdata); } void set_ntextdata(int value) { - ptr_->ntextdata = value; + ptr_->ntextdata = static_cast(value); } int ntuple() const { - return ptr_->ntuple; + return static_cast(ptr_->ntuple); } void set_ntuple(int value) { - ptr_->ntuple = value; + ptr_->ntuple = static_cast(value); } int ntupledata() const { - return ptr_->ntupledata; + return static_cast(ptr_->ntupledata); } void set_ntupledata(int value) { - ptr_->ntupledata = value; + ptr_->ntupledata = static_cast(value); } int nkey() const { - return ptr_->nkey; + return static_cast(ptr_->nkey); } void set_nkey(int value) { - ptr_->nkey = value; + ptr_->nkey = static_cast(value); } int nmocap() const { - return ptr_->nmocap; + return static_cast(ptr_->nmocap); } void set_nmocap(int value) { - ptr_->nmocap = value; + ptr_->nmocap = static_cast(value); } int nplugin() const { - return ptr_->nplugin; + return static_cast(ptr_->nplugin); } void set_nplugin(int value) { - ptr_->nplugin = value; + ptr_->nplugin = static_cast(value); } int npluginattr() const { - return ptr_->npluginattr; + return static_cast(ptr_->npluginattr); } void set_npluginattr(int value) { - ptr_->npluginattr = value; + ptr_->npluginattr = static_cast(value); } int nuser_body() const { - return ptr_->nuser_body; + return static_cast(ptr_->nuser_body); } void set_nuser_body(int value) { - ptr_->nuser_body = value; + ptr_->nuser_body = static_cast(value); } int nuser_jnt() const { - return ptr_->nuser_jnt; + return static_cast(ptr_->nuser_jnt); } void set_nuser_jnt(int value) { - ptr_->nuser_jnt = value; + ptr_->nuser_jnt = static_cast(value); } int nuser_geom() const { - return ptr_->nuser_geom; + return static_cast(ptr_->nuser_geom); } void set_nuser_geom(int value) { - ptr_->nuser_geom = value; + ptr_->nuser_geom = static_cast(value); } int nuser_site() const { - return ptr_->nuser_site; + return static_cast(ptr_->nuser_site); } void set_nuser_site(int value) { - ptr_->nuser_site = value; + ptr_->nuser_site = static_cast(value); } int nuser_cam() const { - return ptr_->nuser_cam; + return static_cast(ptr_->nuser_cam); } void set_nuser_cam(int value) { - ptr_->nuser_cam = value; + ptr_->nuser_cam = static_cast(value); } int nuser_tendon() const { - return ptr_->nuser_tendon; + return static_cast(ptr_->nuser_tendon); } void set_nuser_tendon(int value) { - ptr_->nuser_tendon = value; + ptr_->nuser_tendon = static_cast(value); } int nuser_actuator() const { - return ptr_->nuser_actuator; + return static_cast(ptr_->nuser_actuator); } void set_nuser_actuator(int value) { - ptr_->nuser_actuator = value; + ptr_->nuser_actuator = static_cast(value); } int nuser_sensor() const { - return ptr_->nuser_sensor; + return static_cast(ptr_->nuser_sensor); } void set_nuser_sensor(int value) { - ptr_->nuser_sensor = value; + ptr_->nuser_sensor = static_cast(value); } int nnames() const { - return ptr_->nnames; + return static_cast(ptr_->nnames); } void set_nnames(int value) { - ptr_->nnames = value; + ptr_->nnames = static_cast(value); } int npaths() const { - return ptr_->npaths; + return static_cast(ptr_->npaths); } void set_npaths(int value) { - ptr_->npaths = value; + ptr_->npaths = static_cast(value); } int nnames_map() const { - return ptr_->nnames_map; + return static_cast(ptr_->nnames_map); } void set_nnames_map(int value) { - ptr_->nnames_map = value; + ptr_->nnames_map = static_cast(value); } int nJmom() const { - return ptr_->nJmom; + return static_cast(ptr_->nJmom); } void set_nJmom(int value) { - ptr_->nJmom = value; + ptr_->nJmom = static_cast(value); } int ngravcomp() const { - return ptr_->ngravcomp; + return static_cast(ptr_->ngravcomp); } void set_ngravcomp(int value) { - ptr_->ngravcomp = value; + ptr_->ngravcomp = static_cast(value); } int nemax() const { - return ptr_->nemax; + return static_cast(ptr_->nemax); } void set_nemax(int value) { - ptr_->nemax = value; + ptr_->nemax = static_cast(value); } int njmax() const { - return ptr_->njmax; + return static_cast(ptr_->njmax); } void set_njmax(int value) { - ptr_->njmax = value; + ptr_->njmax = static_cast(value); } int nconmax() const { - return ptr_->nconmax; + return static_cast(ptr_->nconmax); } void set_nconmax(int value) { - ptr_->nconmax = value; + ptr_->nconmax = static_cast(value); } int nuserdata() const { - return ptr_->nuserdata; + return static_cast(ptr_->nuserdata); } void set_nuserdata(int value) { - ptr_->nuserdata = value; + ptr_->nuserdata = static_cast(value); } int nsensordata() const { - return ptr_->nsensordata; + return static_cast(ptr_->nsensordata); } void set_nsensordata(int value) { - ptr_->nsensordata = value; + ptr_->nsensordata = static_cast(value); } int npluginstate() const { - return ptr_->npluginstate; + return static_cast(ptr_->npluginstate); } void set_npluginstate(int value) { - ptr_->npluginstate = value; + ptr_->npluginstate = static_cast(value); } - mjtSize narena() const { - return ptr_->narena; + int narena() const { + return static_cast(ptr_->narena); } - void set_narena(mjtSize value) { - ptr_->narena = value; + void set_narena(int value) { + ptr_->narena = static_cast(value); } - mjtSize nbuffer() const { - return ptr_->nbuffer; + int nbuffer() const { + return static_cast(ptr_->nbuffer); } - void set_nbuffer(mjtSize value) { - ptr_->nbuffer = value; + void set_nbuffer(int value) { + ptr_->nbuffer = static_cast(value); } emscripten::val buffer() const { return emscripten::val(emscripten::typed_memory_view(ptr_->nbuffer, static_cast(ptr_->buffer))); @@ -5337,11 +5337,11 @@ struct MjSpec { void set_strippath(mjtByte value) { ptr_->strippath = value; } - mjtSize memory() const { - return ptr_->memory; + int memory() const { + return static_cast(ptr_->memory); } - void set_memory(mjtSize value) { - ptr_->memory = value; + void set_memory(int value) { + ptr_->memory = static_cast(value); } int nemax() const { return ptr_->nemax; @@ -5421,11 +5421,11 @@ struct MjSpec { void set_nconmax(int value) { ptr_->nconmax = value; } - mjtSize nstack() const { - return ptr_->nstack; + int nstack() const { + return static_cast(ptr_->nstack); } - void set_nstack(mjtSize value) { - ptr_->nstack = value; + void set_nstack(int value) { + ptr_->nstack = static_cast(value); } mjString comment() const { return (ptr_ && ptr_->comment) ? *(ptr_->comment) : ""; @@ -6043,17 +6043,17 @@ struct MjData { std::unique_ptr copy(); mjData* get() const; void set(mjData* ptr); - mjtSize narena() const { - return ptr_->narena; + int narena() const { + return static_cast(ptr_->narena); } - void set_narena(mjtSize value) { - ptr_->narena = value; + void set_narena(int value) { + ptr_->narena = static_cast(value); } - mjtSize nbuffer() const { - return ptr_->nbuffer; + int nbuffer() const { + return static_cast(ptr_->nbuffer); } - void set_nbuffer(mjtSize value) { - ptr_->nbuffer = value; + void set_nbuffer(int value) { + ptr_->nbuffer = static_cast(value); } int nplugin() const { return ptr_->nplugin; @@ -6079,20 +6079,20 @@ struct MjData { void set_parena(size_t value) { ptr_->parena = value; } - mjtSize maxuse_stack() const { - return ptr_->maxuse_stack; + int maxuse_stack() const { + return static_cast(ptr_->maxuse_stack); } - void set_maxuse_stack(mjtSize value) { - ptr_->maxuse_stack = value; + void set_maxuse_stack(int value) { + ptr_->maxuse_stack = static_cast(value); } emscripten::val maxuse_threadstack() const { return emscripten::val(emscripten::typed_memory_view(128, ptr_->maxuse_threadstack)); } - mjtSize maxuse_arena() const { - return ptr_->maxuse_arena; + int maxuse_arena() const { + return static_cast(ptr_->maxuse_arena); } - void set_maxuse_arena(mjtSize value) { - ptr_->maxuse_arena = value; + void set_maxuse_arena(int value) { + ptr_->maxuse_arena = static_cast(value); } int maxuse_con() const { return ptr_->maxuse_con; diff --git a/wasm/codegen/generators/structs.py b/wasm/codegen/generators/structs.py index ce4659b3..5cb91fc9 100644 --- a/wasm/codegen/generators/structs.py +++ b/wasm/codegen/generators/structs.py @@ -97,10 +97,21 @@ def _generate_field_data( ): builder = code_builder.CodeBuilder() - with builder.function(f"{f.type.name} {f.name}() const"): - builder.line(f"return ptr_->{f.name};") - with builder.function(f"void set_{f.name}({f.type.name} value)"): - builder.line(f"ptr_->{f.name} = value;") + + # `mjtSize` is a 64-bit signed integer type and would be represented in + # JS/TS a `bigint` because it doesn't fit in the number primitive (a 64-bit + # float). By casting int, we avoid "TS2345: Argument of type 'bigint' is not + # assignable to parameter of type 'number'" errors. + if f.type.name == "mjtSize": + with builder.function(f"int {f.name}() const"): + builder.line(f"return static_cast(ptr_->{f.name});") + with builder.function(f"void set_{f.name}(int value)"): + builder.line(f"ptr_->{f.name} = static_cast(value);") + else: + with builder.function(f"{f.type.name} {f.name}() const"): + builder.line(f"return ptr_->{f.name};") + with builder.function(f"void set_{f.name}({f.type.name} value)"): + builder.line(f"ptr_->{f.name} = value;") return WrappedFieldData( declaration=builder.to_string(),