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

Fix projected ray length #2482

Merged
merged 4 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions crates/re_components/src/pinhole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ impl Pinhole {
pub fn aspect_ratio(&self) -> Option<f32> {
self.resolution.map(|r| r[0] / r[1])
}

/// Project camera-space coordinates into pixel coordinates,
/// returning the same z/depth.
#[cfg(feature = "glam")]
#[inline]
pub fn project(&self, pixel: glam::Vec3) -> glam::Vec3 {
((pixel.truncate() * glam::Vec2::from(self.focal_length_in_pixels())) / pixel.z
+ self.principal_point())
.extend(pixel.z)
}

/// Given pixel coordinates and a world-space depth,
/// return a position in the camera space.
///
/// The returned z is the same as the input z (depth).
#[cfg(feature = "glam")]
#[inline]
pub fn unproject(&self, pixel: glam::Vec3) -> glam::Vec3 {
((pixel.truncate() - self.principal_point()) * pixel.z
/ glam::Vec2::from(self.focal_length_in_pixels()))
.extend(pixel.z)
}
}

#[test]
Expand Down
43 changes: 12 additions & 31 deletions crates/re_space_view_spatial/src/space_camera_3d.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use glam::{vec3, Affine3A, Mat3, Quat, Vec2, Vec3};
use glam::{Quat, Vec2, Vec3};
use macaw::{IsoTransform, Ray3};

use re_components::{Pinhole, ViewCoordinates};
Expand Down Expand Up @@ -52,41 +52,22 @@ impl SpaceCamera3D {
}
}

/// Projects image coordinates into world coordinates
pub fn world_from_image(&self) -> Option<Affine3A> {
/// Returns x, y, and depth in image/pixel coordinates.
pub fn project_onto_2d(&self, point_in_world: Vec3) -> Option<Vec3> {
let pinhole = self.pinhole?;
let world_from_cam = self.world_from_cam();
let image_from_cam: Mat3 = pinhole.image_from_cam.into();
let cam_from_image = Affine3A::from_mat3(image_from_cam.inverse());
Some(world_from_cam * cam_from_image)
}

/// Projects world coordinates onto 2D image coordinates
pub fn image_from_world(&self) -> Option<Affine3A> {
let pinhole = self.pinhole?;
let cam_from_world = self.cam_from_world();

let image_from_cam = pinhole.image_from_cam.into();
let image_from_cam = Affine3A::from_mat3(image_from_cam);
Some(image_from_cam * cam_from_world)
}
Comment on lines -55 to -72
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two functions were just big footguns. These were not affine transforms, nor even normal 4x4 projection matrices. Instead they where matrices with projection in the Z component (not W), so just… wrong. Better to be explicit than try to combine two very different kind of transforms like this.


/// Returns x, y, and depth in image coordinates.
pub fn project_onto_2d(&self, pos3d: Vec3) -> Option<Vec3> {
self.image_from_world().map(|pixel_from_world| {
let point = pixel_from_world.transform_point3(pos3d);
vec3(point.x / point.z, point.y / point.z, point.z)
})
let point_in_cam = self.cam_from_world().transform_point3(point_in_world);
let point_in_image = pinhole.project(point_in_cam);
Some(point_in_image)
}

/// Unproject a 2D image coordinate as a ray in 3D space
pub fn unproject_as_ray(&self, pos2d: Vec2) -> Option<Ray3> {
self.world_from_image().map(|world_from_pixel| {
let origin = self.position();
let stop = world_from_pixel.transform_point3(pos2d.extend(1.0));
let dir = (stop - origin).normalize();
Ray3::from_origin_dir(origin, dir)
})
let pinhole = self.pinhole?;

let depth = 1.0; // whatever will do
let stop = pinhole.unproject(pos2d.extend(depth));
let ray_in_camera = Ray3::from_origin_dir(Vec3::ZERO, stop);
Some(self.world_from_camera * ray_in_camera)
}
}

Expand Down
12 changes: 10 additions & 2 deletions crates/re_space_view_spatial/src/ui_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,15 +571,23 @@ fn show_projections_from_2d_space(
match ctx.selection_state().hovered_space() {
HoveredSpace::TwoD { space_2d, pos } => {
if let Some(cam) = space_cameras.iter().find(|cam| &cam.ent_path == space_2d) {
if let Some(ray) = cam.unproject_as_ray(glam::vec2(pos.x, pos.y)) {
if let Some(pinhole) = cam.pinhole {
// Render a thick line to the actual z value if any and a weaker one as an extension
// If we don't have a z value, we only render the thick one.
let thick_ray_length = if pos.z.is_finite() && pos.z > 0.0 {
let depth = if 0.0 < pos.z && pos.z.is_finite() {
pos.z
} else {
cam.picture_plane_distance
};

let stop_in_cam = pinhole.unproject(glam::vec3(pos.x, pos.y, depth));
let stop_in_world = cam.world_from_cam().transform_point3(stop_in_cam);

let origin = cam.position();
let ray =
macaw::Ray3::from_origin_dir(origin, (stop_in_world - origin).normalize());

let thick_ray_length = (stop_in_world - origin).length();
add_picking_ray(line_builder, ray, scene_bbox_accum, thick_ray_length);
}
}
Expand Down