franka_interface/
sim_camera.rs

1// Copyright (c) 2021 Marco Boneberger
2// Licensed under the EUPL-1.2-or-later
3//! Contains a struct for creating images inside the simulation
4
5use rubullet::image::RgbaImage;
6use rubullet::nalgebra::Matrix4;
7use rubullet::{CameraImageOptions, PhysicsClient};
8use std::cell::RefCell;
9use std::rc::Rc;
10
11/// Camera for the Simulation. This struct should make it easier to get camera images from the simulation.
12pub struct SimCamera {
13    view_matrix: Matrix4<f32>,
14    projection_matrix: Matrix4<f32>,
15    width: usize,
16    height: usize,
17}
18
19impl SimCamera {
20    /// creates a new SimCamera
21    /// # Arguments
22    /// * `width` - width of the image in pixel
23    /// * `height` - height of the image in pixel
24    /// * `view_matrix` - can be calculated using [`PhysicsClient::compute_view_matrix`](`rubullet::PhysicsClient::compute_view_matrix`)
25    /// or [`PhysicsClient::compute_view_matrix_from_yaw_pitch_roll`](`rubullet::PhysicsClient::compute_view_matrix_from_yaw_pitch_roll`)
26    /// * `projection_matrix` - can be calculated using [`PhysicsClient::compute_projection_matrix`](`rubullet::PhysicsClient::compute_projection_matrix`)
27    /// or [`PhysicsClient::compute_projection_matrix_fov`](`rubullet::PhysicsClient::compute_projection_matrix_fov`)
28    pub fn new(
29        width: usize,
30        height: usize,
31        view_matrix: Matrix4<f32>,
32        projection_matrix: Matrix4<f32>,
33    ) -> SimCamera {
34        SimCamera {
35            view_matrix,
36            projection_matrix,
37            width,
38            height,
39        }
40    }
41    /// returns an image from the simulation. Note that taking pictures has a significant impact on
42    /// the performance of the simulation.
43    pub fn get_image(&self, client: Rc<RefCell<PhysicsClient>>) -> RgbaImage {
44        client
45            .borrow_mut()
46            .get_camera_image(
47                self.width,
48                self.height,
49                CameraImageOptions {
50                    view_matrix: Some(self.view_matrix),
51                    projection_matrix: Some(self.projection_matrix),
52                    ..Default::default()
53                },
54            )
55            .unwrap()
56            .rgba
57    }
58}