pub struct MjRenderer<M: Deref<Target = MjModel> + Clone> { /* 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: Deref<Target = MjModel> + Clone> MjRenderer<M>
impl<M: Deref<Target = MjModel> + Clone> MjRenderer<M>
Sourcepub fn new(
model: M,
width: usize,
height: usize,
max_user_geom: usize,
) -> Result<Self, RendererError>
pub fn new( model: M, width: usize, height: usize, max_user_geom: usize, ) -> Result<Self, RendererError>
Construct a new renderer.
The max_user_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<M>
pub fn builder() -> MjRendererBuilder<M>
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::NotFoundwhen 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 (if it was enabled with normalize): 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::NotFoundwhen 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::NotFoundwhen depth rendering is disabled,- other errors related to write.
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>where
M: Unpin,
impl<M> !UnwindSafe for MjRenderer<M>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.