jaxsim 0.4.1.dev6__py3-none-any.whl → 0.4.1.dev11__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
jaxsim/_version.py CHANGED
@@ -12,5 +12,5 @@ __version__: str
12
12
  __version_tuple__: VERSION_TUPLE
13
13
  version_tuple: VERSION_TUPLE
14
14
 
15
- __version__ = version = '0.4.1.dev6'
16
- __version_tuple__ = version_tuple = (0, 4, 1, 'dev6')
15
+ __version__ = version = '0.4.1.dev11'
16
+ __version_tuple__ = version_tuple = (0, 4, 1, 'dev11')
jaxsim/mujoco/loaders.py CHANGED
@@ -160,6 +160,7 @@ class RodModelToMjcf:
160
160
  considered_joints: list[str] | None = None,
161
161
  plane_normal: tuple[float, float, float] = (0, 0, 1),
162
162
  heightmap: bool | None = None,
163
+ heightmap_samples_xy: tuple[int, int] = (101, 101),
163
164
  cameras: list[dict[str, str]] | dict[str, str] | None = None,
164
165
  ) -> tuple[str, dict[str, Any]]:
165
166
  """
@@ -170,10 +171,11 @@ class RodModelToMjcf:
170
171
  considered_joints: The list of joint names to consider in the conversion.
171
172
  plane_normal: The normal vector of the plane.
172
173
  heightmap: Whether to generate a heightmap.
174
+ heightmap_samples_xy: The number of points in the heightmap grid.
173
175
  cameras: The list of cameras to add to the scene.
174
176
 
175
177
  Returns:
176
- tuple: A tuple containing the MJCF string and the assets dictionary.
178
+ tuple: A tuple containing the MJCF string and the dictionary of assets.
177
179
  """
178
180
 
179
181
  # -------------------------------------
@@ -404,9 +406,11 @@ class RodModelToMjcf:
404
406
  asset_element,
405
407
  "hfield",
406
408
  name="terrain",
407
- nrow="100",
408
- ncol="100",
409
- size="5 5 1 1",
409
+ nrow=f"{int(heightmap_samples_xy[0])}",
410
+ ncol=f"{int(heightmap_samples_xy[1])}",
411
+ # The following 'size' is a placeholder, it is updated dynamically
412
+ # when a hfield/heightmap is stored into MjData.
413
+ size="1 1 1 1",
410
414
  )
411
415
  if heightmap
412
416
  else None
jaxsim/mujoco/model.py CHANGED
@@ -7,6 +7,7 @@ from typing import Any, Callable
7
7
  import mujoco as mj
8
8
  import numpy as np
9
9
  import numpy.typing as npt
10
+ import xmltodict
10
11
  from scipy.spatial.transform import Rotation
11
12
 
12
13
  import jaxsim.typing as jtp
@@ -42,16 +43,27 @@ class MujocoModelHelper:
42
43
  mjcf_description: str | pathlib.Path,
43
44
  assets: dict[str, Any] | None = None,
44
45
  heightmap: HeightmapCallable | None = None,
46
+ heightmap_name: str = "terrain",
47
+ heightmap_radius_xy: tuple[float, float] = (1.0, 1.0),
45
48
  ) -> MujocoModelHelper:
46
49
  """
47
- Build a Mujoco model from an XML description and an optional assets dictionary.
50
+ Build a Mujoco model from an MJCF description.
48
51
 
49
52
  Args:
50
- mjcf_description: A string containing the XML description of the Mujoco model
53
+ mjcf_description:
54
+ A string containing the XML description of the Mujoco model
51
55
  or a path to a file containing the XML description.
52
56
  assets: An optional dictionary containing the assets of the model.
53
- heightmap: A function in two variables that returns the height of a terrain
57
+ heightmap:
58
+ A function in two variables that returns the height of a terrain
54
59
  in the specified coordinate point.
60
+ heightmap_name:
61
+ The default name of the heightmap in the MJCF description
62
+ to load the corresponding configuration.
63
+ heightmap_radius_xy:
64
+ The extension of the heightmap in the x-y surface corresponding to the
65
+ plane over which the grid of the sampled heightmap is generated.
66
+
55
67
  Returns:
56
68
  A MujocoModelHelper object.
57
69
  """
@@ -63,15 +75,61 @@ class MujocoModelHelper:
63
75
  else mjcf_description
64
76
  )
65
77
 
66
- # Create the Mujoco model from the XML and, optionally, the assets dictionary.
78
+ if heightmap is None:
79
+ hfield = None
80
+
81
+ else:
82
+
83
+ mjcf_description_dict = xmltodict.parse(xml_input=mjcf_description)
84
+
85
+ # Create a dictionary of all hfield configurations from the MJCF.
86
+ hfields = mjcf_description_dict["mujoco"]["asset"].get("hfield", [])
87
+ hfields = hfields if isinstance(hfields, list) else [hfields]
88
+ hfields_dict = {hfield["@name"]: hfield for hfield in hfields}
89
+
90
+ if heightmap_name not in hfields_dict:
91
+ raise ValueError(f"Heightmap '{heightmap_name}' not found in MJCF")
92
+
93
+ hfield_element = hfields_dict[heightmap_name]
94
+
95
+ # Generate the hfield by sampling the heightmap function.
96
+ hfield = generate_hfield(
97
+ heightmap=heightmap,
98
+ samples_xy=(int(hfield_element["@nrow"]), int(hfield_element["@ncol"])),
99
+ radius_xy=heightmap_radius_xy,
100
+ )
101
+
102
+ # Update dynamically the '/asset/hfield[@name=heightmap_name]@size' attribute
103
+ # with the information of the sampled points.
104
+ # This is necessary for correctly rendering the heightmap over the
105
+ # specified xy area with the correct z elevation.
106
+ size = [float(el) for el in hfield_element["@size"].split(" ")]
107
+ size[0], size[1] = heightmap_radius_xy
108
+ size[2] = 1.0
109
+ size[3] = max(0, -min(hfield))
110
+
111
+ # Replace the 'size' attribute.
112
+ hfields_dict[heightmap_name]["@size"] = " ".join(str(el) for el in size)
113
+
114
+ # Update the hfield elements of the original MJCF.
115
+ # Only the hfield corresponding to 'heightmap_name' was actually edited.
116
+ mjcf_description_dict["mujoco"]["asset"]["hfield"] = list(
117
+ hfields_dict.values()
118
+ )
119
+
120
+ # Serialize the updated MJCF to XML.
121
+ mjcf_description = xmltodict.unparse(
122
+ input_dict=mjcf_description_dict, pretty=True
123
+ )
124
+
125
+ # Create the Mujoco model from the XML and, optionally, the dictionary of assets.
67
126
  model = mj.MjModel.from_xml_string(xml=mjcf_description, assets=assets)
68
127
  data = mj.MjData(model)
69
128
 
70
- if heightmap:
71
- nrow = model.hfield_nrow.item()
72
- ncol = model.hfield_ncol.item()
73
- new_hfield = generate_hfield(heightmap, (nrow, ncol))
74
- model.hfield_data = new_hfield
129
+ # Store the sampled heightmap into the Mujoco model.
130
+ if heightmap is not None:
131
+ assert hfield is not None
132
+ model.hfield_data = hfield
75
133
 
76
134
  return MujocoModelHelper(model=model, data=data)
77
135
 
@@ -385,10 +443,13 @@ class MujocoModelHelper:
385
443
 
386
444
 
387
445
  def generate_hfield(
388
- heightmap: HeightmapCallable, size: tuple[int, int] = (10, 10)
446
+ heightmap: HeightmapCallable,
447
+ samples_xy: tuple[int, int] = (11, 11),
448
+ radius_xy: tuple[float, float] = (1.0, 1.0),
389
449
  ) -> npt.NDArray:
390
450
  """
391
- Generates a numpy array representing the heightmap of
451
+ Generate an array with elevation points sampled from a heightmap function.
452
+
392
453
  The map will have the following format:
393
454
  ```
394
455
  heightmap[0, 0] heightmap[0, 1] ... heightmap[0, size[1]-1]
@@ -398,17 +459,22 @@ def generate_hfield(
398
459
  ```
399
460
 
400
461
  Args:
401
- heightmap: A function that takes two arguments (x, y) and returns the height
462
+ heightmap:
463
+ A function that takes two arguments (x, y) and returns the height
402
464
  at that point.
403
- size: A tuple of two integers representing the size of the grid.
465
+ samples_xy: A tuple of two integers representing the size of the grid.
466
+ radius_xy:
467
+ A tuple of two floats representing extension of the heightmap in the
468
+ x-y surface corresponding to the area over which the grid of the sampled
469
+ heightmap is generated.
404
470
 
405
471
  Returns:
406
- np.ndarray: The terrain heightmap
472
+ A flat array of the sampled terrain heightmap.
407
473
  """
408
474
 
409
475
  # Generate the grid.
410
- x = np.linspace(0, 1, size[0])
411
- y = np.linspace(0, 1, size[1])
476
+ x = np.linspace(-radius_xy[0], radius_xy[0], samples_xy[0])
477
+ y = np.linspace(-radius_xy[1], radius_xy[1], samples_xy[1])
412
478
 
413
479
  # Generate the heightmap.
414
480
  return np.array([[heightmap(xi, yi) for xi in x] for yi in y]).flatten()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: jaxsim
3
- Version: 0.4.1.dev6
3
+ Version: 0.4.1.dev11
4
4
  Summary: A differentiable physics engine and multibody dynamics library for control and robot learning.
5
5
  Author-email: Diego Ferigo <dgferigo@gmail.com>
6
6
  Maintainer-email: Diego Ferigo <dgferigo@gmail.com>, Filippo Luca Ferretti <filippo.ferretti@iit.it>
@@ -1,5 +1,5 @@
1
1
  jaxsim/__init__.py,sha256=ixsS4dYMPex2wOUUp_rkPnwrPhYzkRh1xO_YuMj3Cr4,2626
2
- jaxsim/_version.py,sha256=bHDDEq643CuzU_d4ZEpTqbsQTlTvvqbGfOA9vrbclqI,424
2
+ jaxsim/_version.py,sha256=7ZFpPaz7M9u-cSnMJ8POHhkQ3_j7elMp-k9w58-1Rgw,426
3
3
  jaxsim/exceptions.py,sha256=8_h8iqL8DgNR754dR8SZiQ7361GR5V1sUk3ZuZCHw1Q,2069
4
4
  jaxsim/logging.py,sha256=c4zhwBKf9eAYAHVp62kTEllqdsZgh0K-kPKVy8L3elU,1584
5
5
  jaxsim/typing.py,sha256=IbFx3UkEXi-cm7UBqMPi58rJAFV_HbZ9E_K4JwfNvVM,753
@@ -31,8 +31,8 @@ jaxsim/math/skew.py,sha256=oOGSSR8PUGROl6IJFlrmu6K3gPH-u16hUPfKIkcVv9o,1177
31
31
  jaxsim/math/transform.py,sha256=_5kSnfkS6_vxvjxdw50KeXMjvW8e1OGaumUlk1iGJgc,2969
32
32
  jaxsim/mujoco/__init__.py,sha256=Zo5GAlN1DYKvX8s1hu1j6HntKIbBMLB9Puv9ouaNAZ8,158
33
33
  jaxsim/mujoco/__main__.py,sha256=GBmB7J-zj75ZnFyuAAmpSOpbxi_HhHhWJeot3ljGDJY,5291
34
- jaxsim/mujoco/loaders.py,sha256=FuGPPg9Iq2xLZSqIFzlY7YfPR5uy9rbGlb0cU4gio5A,21000
35
- jaxsim/mujoco/model.py,sha256=mObFS77EY97sTFdlFrl69_gf9S_0FHO0W3_hevxQNws,13511
34
+ jaxsim/mujoco/loaders.py,sha256=hwT8cjMEZmVaPMs4RzS4UEgC0_c9Od9817UcbNANPBc,21345
35
+ jaxsim/mujoco/model.py,sha256=EwUPg9BsNv1B7TdDfjZCpC022lDR16AyIAajPJGH7NU,16357
36
36
  jaxsim/mujoco/visualizer.py,sha256=9jfKXkuoaW7Ppo_0m8dogD3SxH13K2strFNDVLtG3hA,5154
37
37
  jaxsim/parsers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
38
38
  jaxsim/parsers/kinematic_graph.py,sha256=88d0EmndVJWdcyFJsW25S78Z8F04cUt08RQMyoil1Xw,34734
@@ -61,8 +61,8 @@ jaxsim/utils/__init__.py,sha256=Y5zyoRevl3EMVQadhZ4EtSwTEkDt2vcnFoRhPJjKTZ0,215
61
61
  jaxsim/utils/jaxsim_dataclass.py,sha256=fLl1tY3DDb3lpIhG6BPqA5W34hM84oFzL-5cuz8k-68,11379
62
62
  jaxsim/utils/tracing.py,sha256=KDMoyVPlu2NJvFkhtZwq5AkqMMgajt3munvJom-vEjQ,650
63
63
  jaxsim/utils/wrappers.py,sha256=GOJQCJc5zwzoEGZB62wnWWGvUUQlXvDxz_A2Q-hFv7c,4027
64
- jaxsim-0.4.1.dev6.dist-info/LICENSE,sha256=eaYdFmdeMbiIoIiPzEK0MjP1S9wtFXjXNR5er49uLR0,1546
65
- jaxsim-0.4.1.dev6.dist-info/METADATA,sha256=SHBMqYI6b449ER-BGP0ANpyrW84OjD41JgtMbfw41Ss,16778
66
- jaxsim-0.4.1.dev6.dist-info/WHEEL,sha256=Z4pYXqR_rTB7OWNDYFOm1qRk0RX6GFP2o8LgvP453Hk,91
67
- jaxsim-0.4.1.dev6.dist-info/top_level.txt,sha256=LxGMA8FLtXjQ6oI7N5gd_R_oSUHxpXxUEOfT1xS_ni0,7
68
- jaxsim-0.4.1.dev6.dist-info/RECORD,,
64
+ jaxsim-0.4.1.dev11.dist-info/LICENSE,sha256=eaYdFmdeMbiIoIiPzEK0MjP1S9wtFXjXNR5er49uLR0,1546
65
+ jaxsim-0.4.1.dev11.dist-info/METADATA,sha256=RQcmfKhS454m3fgEmDhJP00XtQFwnxWeXaBkTWiUKaQ,16779
66
+ jaxsim-0.4.1.dev11.dist-info/WHEEL,sha256=Z4pYXqR_rTB7OWNDYFOm1qRk0RX6GFP2o8LgvP453Hk,91
67
+ jaxsim-0.4.1.dev11.dist-info/top_level.txt,sha256=LxGMA8FLtXjQ6oI7N5gd_R_oSUHxpXxUEOfT1xS_ni0,7
68
+ jaxsim-0.4.1.dev11.dist-info/RECORD,,