MjRenderer

Struct MjRenderer 

Source
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>

Source

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>
Source

pub fn builder() -> MjRendererBuilder

Create a MjRendererBuilder to configure MjRenderer.

Source

pub fn scene(&self) -> &MjvScene<'m>

Return an immutable reference to the internal scene.

Source

pub fn user_scene(&self) -> &MjvScene<'m>

Return an immutable reference to a user scene for drawing custom visual-only geoms.

Source

pub fn user_scene_mut(&mut self) -> &mut MjvScene<'m>

Return a mutable reference to a user scene for drawing custom visual-only geoms.

Source

pub fn opts(&self) -> &MjvOption

Return an immutable reference to visualization options.

Source

pub fn opts_mut(&mut self) -> &mut MjvOption

Return a mutable reference to visualization options.

Source

pub fn camera(&self) -> &MjvCamera

Return an immutable reference to the camera.

Source

pub fn camera_mut(&mut self) -> &mut MjvCamera

Return a mutable reference to the camera.

Source

pub fn rgb_enabled(&self) -> bool

Check if RGB rendering is enabled.

Source

pub fn depth_enabled(&self) -> bool

Check if depth rendering is enabled.

Source

pub fn set_font_scale(&mut self, font_scale: MjtFontScale)

Sets the font size.

Source

pub fn set_opts(&mut self, options: MjvOption)

Update the visualization options and return a reference to self.

Source

pub fn set_camera(&mut self, camera: MjvCamera)

Set the camera used for rendering.

Source

pub fn set_rgb_rendering(&mut self, enable: bool)

Enables/disables RGB rendering.

Source

pub fn set_depth_rendering(&mut self, enable: bool)

Enables/disables depth rendering.

Source

pub fn with_font_scale(self, font_scale: MjtFontScale) -> Self

Sets the font size. To be used on construction.

Source

pub fn with_opts(self, options: MjvOption) -> Self

Update the visualization options and return a reference to self. To be used on construction.

Source

pub fn with_camera(self, camera: MjvCamera) -> Self

Set the camera used for rendering. To be used on construction.

Source

pub fn with_rgb_rendering(self, enable: bool) -> Self

Enables/disables RGB rendering. To be used on construction.

Source

pub fn with_depth_rendering(self, enable: bool) -> Self

Enables/disables depth rendering. To be used on construction.

Source

pub fn sync(&mut self, data: &mut MjData<'_>)

Update the scene with new data from data.

Source

pub fn rgb_flat(&self) -> Option<&[u8]>

Return a flattened RGB image of the scene.

Source

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.

Source

pub fn depth_flat(&self) -> Option<&[f32]>

Return a flattened depth image of the scene.

Source

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.

Source

pub fn save_rgb<T: AsRef<Path>>(&self, path: T) -> Result<()>

Save an RGB image of the scene to a path.

§Errors
Source

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
Source

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

Auto Trait Implementations§

§

impl<'m> Freeze for MjRenderer<'m>

§

impl<'m> RefUnwindSafe for MjRenderer<'m>

§

impl<'m> !Send for MjRenderer<'m>

§

impl<'m> !Sync for MjRenderer<'m>

§

impl<'m> Unpin for MjRenderer<'m>

§

impl<'m> UnwindSafe for MjRenderer<'m>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.