Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pinhole functionality for depth mapping #3

Merged
merged 10 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/.gitignore
makeecat marked this conversation as resolved.
Show resolved Hide resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/Peng.iml
makeecat marked this conversation as resolved.
Show resolved Hide resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml
makeecat marked this conversation as resolved.
Show resolved Hide resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml
makeecat marked this conversation as resolved.
Show resolved Hide resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/quad.yaml
makeecat marked this conversation as resolved.
Show resolved Hide resolved
makeecat marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ camera:
fov: 90.0 # Field of view (degrees)
near: 0.1 # Near clipping plane (m)
far: 5.0 # Far clipping plane (m)
rotation_transform: [0.0, 0.0, 1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0] # Rotates camera to positive x-axis

mesh:
division: 7 # Number of divisions in the mesh grid
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ pub struct CameraConfig {
pub near: f32,
/// Camera far clipping plane in meters
pub far: f32,
/// Camera transform matrix for depth
pub rotation_transform: [f32; 9]
}

#[derive(serde::Deserialize)]
Expand Down
65 changes: 65 additions & 0 deletions src/lib.rs
makeecat marked this conversation as resolved.
Show resolved Hide resolved
makeecat marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2582,6 +2582,71 @@ pub fn log_depth_image(
rec.log("world/quad/cam/depth", &rerun_image)?;
Ok(())
}
/// creates pinhole camera
/// # Arguments
/// * `rec` - The rerun::RecordingStream instance
/// * `width` - The width component of the camera resolution
/// * `height` - The height component of the camera resolution
/// * `fov` - The fov of the camera
/// * `cam_position` - The position vector of the camera (aligns with the quad)
/// * `cam_orientation` - The orientation quaternion of quad
/// * `cam_transform` - The transform matrix between quad and camera alignment
/// * `depth_image` - The depth image data
/// # Errors
/// * If the data cannot be logged to the recording stream
/// # Example
/// ```no_run
/// use peng_quad::pinhole_depth;
/// use nalgebra::{Vector3, UnitQuaternion};
/// let rec = rerun::RecordingStreamBuilder::new("log.rerun").connect().unwrap();
/// let depth_image = vec![ 0.0f32 ; 640 * 480];
/// let cam_position = Vector3::new(0.0,0.0,0.0);
/// let cam_orientation = UnitQuaternion::identity();
/// let cam_transform = [0.0, 0.0, 1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0];
/// pinhole_depth(&rec, 640, 480, 90.0, cam_position, cam_orientation, cam_transform, &depth_image).unwrap();

pub fn pinhole_depth(
rec: &rerun::RecordingStream,
width: usize,
height: usize,
fov: f32,
cam_position: Vector3<f32>,
cam_orientation: UnitQuaternion<f32>,
cam_transform: [f32; 9],
depth_image: &[f32],
) -> Result<(), SimulationError> {
let pinhole_camera =
rerun::Pinhole::from_fov_and_aspect_ratio(fov / 180.0 * PI, width as f32 / height as f32)
.with_camera_xyz(rerun::components::ViewCoordinates::RDF)
.with_resolution((width as f32, height as f32))
.with_principal_point((width as f32 / 2.0, height as f32 / 2.0));
let rotated_camera_orientation = UnitQuaternion::from_rotation_matrix(
&(cam_orientation.to_rotation_matrix()
* Rotation3::from_matrix_unchecked(Matrix3::from_row_slice(&cam_transform))),
);
let cam_transform = rerun::Transform3D::from_translation_rotation(
rerun::Vec3D::new(cam_position.x, cam_position.y, cam_position.z),
rerun::Quaternion::from_xyzw([
rotated_camera_orientation.i,
rotated_camera_orientation.j,
rotated_camera_orientation.k,
rotated_camera_orientation.w,
]),
);
rec.log("world/quad/cam", &cam_transform)?;
rec.log("world/quad/cam", &pinhole_camera)?;
let depth_image_rerun =
rerun::external::ndarray::Array::from_shape_vec((height, width), depth_image.to_vec())
.unwrap();
rec.log(
"world/quad/cam/rerun_depth",
&rerun::DepthImage::try_from(depth_image_rerun)
.unwrap()
.with_meter(1.0),
)?;

Ok(())
}
/// turbo color map function
/// # Arguments
/// * `gray` - The gray value in the range [0, 255]
Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
makeecat marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ fn main() -> Result<(), SimulationError> {
camera.near,
camera.far,
)?;
pinhole_depth(
rec,
camera.resolution.0,
camera.resolution.1,
camera.fov,
quad.position,
quad.orientation,
config.camera.rotation_transform,
&depth_buffer,
)?;
}
log_maze_obstacles(rec, &maze)?;
}
Expand Down