pub struct MjRenderer<'m> { /* private fields */ }
Expand description
A renderer for rendering 3D scenes. By default, RGB rendering is enabled and depth rendering is disabled.
Implementations§
Source§impl<'m> MjRenderer<'m>
impl<'m> MjRenderer<'m>
Sourcepub fn new(
model: &'m MjModel,
width: usize,
height: usize,
max_geom: usize,
) -> Result<Self, RendererError>
pub fn new( model: &'m MjModel, width: usize, height: usize, max_geom: usize, ) -> Result<Self, RendererError>
Construct a new renderer.
The max_geom
parameter
defines how much space will be allocated for additional, user-defined visual-only geoms.
It can thus be set to 0 if no additional geoms will be drawn by the user.
§Scene allocation
The renderer uses two scenes:
- the internal scene: used by the renderer to draw the model’s state.
- the user scene: used by the user to add additional geoms to the internal scene
The internal scene allocates the amount of space needed to fit every pre-existing
model geom + user visual-only geoms + additional visual-only geoms that aren’t from the user (e.g., tendons).
By default, the renderer reserves 100 extra geom slots for drawing the additional visual-only geoms.
If that is not enough or it is too much, you can construct MjRenderer
via its builder
(MjRenderer::builder
), which allows more configuration.
Parameters width
and height
must be less or equal to the offscreen buffer size,
which can be configured at the top of the model’s XML like so:
<visual>
<global offwidth="1920" offheight="1080"/>
</visual>
Sourcepub fn builder() -> MjRendererBuilder
pub fn builder() -> MjRendererBuilder
Create a MjRendererBuilder
to configure MjRenderer
.
Sourcepub fn user_scene(&self) -> &MjvScene<'m>
pub fn user_scene(&self) -> &MjvScene<'m>
Return an immutable reference to a user scene for drawing custom visual-only geoms.
Sourcepub fn user_scene_mut(&mut self) -> &mut MjvScene<'m>
pub fn user_scene_mut(&mut self) -> &mut MjvScene<'m>
Return a mutable reference to a user scene for drawing custom visual-only geoms.
Sourcepub fn opts_mut(&mut self) -> &mut MjvOption
pub fn opts_mut(&mut self) -> &mut MjvOption
Return a mutable reference to visualization options.
Sourcepub fn camera_mut(&mut self) -> &mut MjvCamera
pub fn camera_mut(&mut self) -> &mut MjvCamera
Return a mutable reference to the camera.
Sourcepub fn rgb_enabled(&self) -> bool
pub fn rgb_enabled(&self) -> bool
Check if RGB rendering is enabled.
Sourcepub fn depth_enabled(&self) -> bool
pub fn depth_enabled(&self) -> bool
Check if depth rendering is enabled.
Sourcepub fn set_font_scale(&mut self, font_scale: MjtFontScale)
pub fn set_font_scale(&mut self, font_scale: MjtFontScale)
Sets the font size.
Sourcepub fn set_opts(&mut self, options: MjvOption)
pub fn set_opts(&mut self, options: MjvOption)
Update the visualization options and return a reference to self.
Sourcepub fn set_camera(&mut self, camera: MjvCamera)
pub fn set_camera(&mut self, camera: MjvCamera)
Set the camera used for rendering.
Sourcepub fn set_rgb_rendering(&mut self, enable: bool)
pub fn set_rgb_rendering(&mut self, enable: bool)
Enables/disables RGB rendering.
Sourcepub fn set_depth_rendering(&mut self, enable: bool)
pub fn set_depth_rendering(&mut self, enable: bool)
Enables/disables depth rendering.
Sourcepub fn with_font_scale(self, font_scale: MjtFontScale) -> Self
pub fn with_font_scale(self, font_scale: MjtFontScale) -> Self
Sets the font size. To be used on construction.
Sourcepub fn with_opts(self, options: MjvOption) -> Self
pub fn with_opts(self, options: MjvOption) -> Self
Update the visualization options and return a reference to self. To be used on construction.
Sourcepub fn with_camera(self, camera: MjvCamera) -> Self
pub fn with_camera(self, camera: MjvCamera) -> Self
Set the camera used for rendering. To be used on construction.
Sourcepub fn with_rgb_rendering(self, enable: bool) -> Self
pub fn with_rgb_rendering(self, enable: bool) -> Self
Enables/disables RGB rendering. To be used on construction.
Sourcepub fn with_depth_rendering(self, enable: bool) -> Self
pub fn with_depth_rendering(self, enable: bool) -> Self
Enables/disables depth rendering. To be used on construction.
Sourcepub fn rgb<const WIDTH: usize, const HEIGHT: usize>(
&self,
) -> Result<&[[[u8; 3]; WIDTH]; HEIGHT]>
pub fn rgb<const WIDTH: usize, const HEIGHT: usize>( &self, ) -> Result<&[[[u8; 3]; WIDTH]; HEIGHT]>
Return an RGB image of the scene. This methods accepts two generic parameters <WIDTH, HEIGHT> that define the shape of the output slice.
Sourcepub fn depth_flat(&self) -> Option<&[f32]>
pub fn depth_flat(&self) -> Option<&[f32]>
Return a flattened depth image of the scene.
Sourcepub fn depth<const WIDTH: usize, const HEIGHT: usize>(
&self,
) -> Result<&[[f32; WIDTH]; HEIGHT]>
pub fn depth<const WIDTH: usize, const HEIGHT: usize>( &self, ) -> Result<&[[f32; WIDTH]; HEIGHT]>
Return a depth image of the scene. This methods accepts two generic parameters <WIDTH, HEIGHT> that define the shape of the output slice.
Sourcepub fn save_rgb<T: AsRef<Path>>(&self, path: T) -> Result<()>
pub fn save_rgb<T: AsRef<Path>>(&self, path: T) -> Result<()>
Save an RGB image of the scene to a path.
§Errors
ErrorKind::NotFound
when RGB rendering is disabled,- other errors related to write.
Sourcepub fn save_depth<T: AsRef<Path>>(
&self,
path: T,
normalize: bool,
) -> Result<(f32, f32)>
pub fn save_depth<T: AsRef<Path>>( &self, path: T, normalize: bool, ) -> Result<(f32, f32)>
Save a depth image of the scene to a path. The image is 16-bit PNG, which
can be converted into depth (distance) data by dividing the grayscale values by
65535.0 and applying inverse normalization: depth = min + (grayscale / 65535.0) * (max - min)
.
If normalize
is true
, then the data is normalized with min-max normalization.
Use of MjRenderer::save_depth_raw
is recommended if performance is critical, as
it skips PNG encoding and also saves the true depth values directly.
§Returns
An Ok
((min, max))
is returned, where min and max represent the normalization parameters.
§Errors
ErrorKind::NotFound
when depth rendering is disabled,- other errors related to write.
Sourcepub fn save_depth_raw<T: AsRef<Path>>(&self, path: T) -> Result<()>
pub fn save_depth_raw<T: AsRef<Path>>(&self, path: T) -> Result<()>
Save the raw depth data to the path
. The data is encoded
as a sequence of bytes, where groups of four represent a single f32 value.
The lower bytes of individual f32 appear first (low-endianness).
§Errors
ErrorKind::NotFound
when depth rendering is disabled,- other errors related to write.