Skip to content

Commit 62fad9c

Browse files
committed
Make RDF the default, and add helper for converting between coordinates
1 parent 5f756f8 commit 62fad9c

4 files changed

Lines changed: 68 additions & 45 deletions

File tree

crates/re_components/src/coordinates.rs

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,12 @@ impl re_log_types::Component for ViewCoordinates {
105105
}
106106

107107
impl ViewCoordinates {
108-
/// Default right-handed view coordinates of `re_renderer`: X=Right, Y=Up, Z=Back.
109-
pub const RUB: Self = Self([ViewDir::Right, ViewDir::Up, ViewDir::Back]);
110-
111108
/// Default right-handed pinhole, camera, and image coordinates: X=Right, Y=Down, Z=Forward.
112109
pub const RDF: Self = Self([ViewDir::Right, ViewDir::Down, ViewDir::Forward]);
113110

111+
/// Default right-handed view coordinates of `re_renderer`: X=Right, Y=Up, Z=Back.
112+
pub const RUB: Self = Self([ViewDir::Right, ViewDir::Up, ViewDir::Back]);
113+
114114
/// Choses a coordinate system based on just an up-axis.
115115
pub fn from_up_and_handedness(up: SignedAxis3, handedness: Handedness) -> Self {
116116
use ViewDir::{Back, Down, Forward, Right, Up};
@@ -209,13 +209,31 @@ impl ViewCoordinates {
209209
)
210210
}
211211

212-
/// Returns a matrix that transforms from RUB to this coordinate system.
212+
/// Returns a matrix that transforms from another coordinate system to this (self) one.
213+
#[cfg(feature = "glam")]
214+
#[inline]
215+
pub fn from_other(&self, other: &Self) -> glam::Mat3 {
216+
self.from_rdf() * other.to_rdf()
217+
}
218+
219+
/// Returns a matrix that transforms this coordinate system to RDF.
213220
///
214-
/// (RUB: X=Right, Y=Up, Z=Back)
221+
/// (RDF: X=Right, Y=Down, Z=Forward)
215222
#[cfg(feature = "glam")]
216223
#[inline]
217-
pub fn from_rub(&self) -> glam::Mat3 {
218-
self.to_rub().transpose()
224+
pub fn to_rdf(&self) -> glam::Mat3 {
225+
fn rdf(dir: ViewDir) -> [f32; 3] {
226+
match dir {
227+
ViewDir::Right => [1.0, 0.0, 0.0],
228+
ViewDir::Left => [-1.0, 0.0, 0.0],
229+
ViewDir::Up => [0.0, -1.0, 0.0],
230+
ViewDir::Down => [0.0, 1.0, 0.0],
231+
ViewDir::Back => [0.0, 0.0, -1.0],
232+
ViewDir::Forward => [0.0, 0.0, 1.0],
233+
}
234+
}
235+
236+
glam::Mat3::from_cols_array_2d(&[rdf(self.0[0]), rdf(self.0[1]), rdf(self.0[2])])
219237
}
220238

221239
/// Returns a matrix that transforms from RDF to this coordinate system.
@@ -224,8 +242,36 @@ impl ViewCoordinates {
224242
#[cfg(feature = "glam")]
225243
#[inline]
226244
pub fn from_rdf(&self) -> glam::Mat3 {
227-
let rub_from_rdf = Self::RDF.to_rub();
228-
self.from_rub() * rub_from_rdf
245+
self.to_rdf().transpose()
246+
}
247+
248+
/// Returns a matrix that transforms this coordinate system to RUB.
249+
///
250+
/// (RUB: X=Right, Y=Up, Z=Back)
251+
#[cfg(feature = "glam")]
252+
#[inline]
253+
pub fn to_rub(&self) -> glam::Mat3 {
254+
fn rub(dir: ViewDir) -> [f32; 3] {
255+
match dir {
256+
ViewDir::Right => [1.0, 0.0, 0.0],
257+
ViewDir::Left => [-1.0, 0.0, 0.0],
258+
ViewDir::Up => [0.0, 1.0, 0.0],
259+
ViewDir::Down => [0.0, -1.0, 0.0],
260+
ViewDir::Back => [0.0, 0.0, 1.0],
261+
ViewDir::Forward => [0.0, 0.0, -1.0],
262+
}
263+
}
264+
265+
glam::Mat3::from_cols_array_2d(&[rub(self.0[0]), rub(self.0[1]), rub(self.0[2])])
266+
}
267+
268+
/// Returns a matrix that transforms from RUB to this coordinate system.
269+
///
270+
/// (RUB: X=Right, Y=Up, Z=Back)
271+
#[cfg(feature = "glam")]
272+
#[inline]
273+
pub fn from_rub(&self) -> glam::Mat3 {
274+
self.to_rub().transpose()
229275
}
230276

231277
/// Returns a quaternion that rotates from RUB to this coordinate system.
@@ -254,31 +300,11 @@ impl ViewCoordinates {
254300
}
255301
}
256302

257-
/// Returns a matrix that transforms this coordinate system to RUB.
258-
///
259-
/// (RUB: X=Right, Y=Up, Z=Back)
260-
#[cfg(feature = "glam")]
261-
#[inline]
262-
pub fn to_rub(&self) -> glam::Mat3 {
263-
fn rub(dir: ViewDir) -> [f32; 3] {
264-
match dir {
265-
ViewDir::Right => [1.0, 0.0, 0.0],
266-
ViewDir::Left => [-1.0, 0.0, 0.0],
267-
ViewDir::Up => [0.0, 1.0, 0.0],
268-
ViewDir::Down => [0.0, -1.0, 0.0],
269-
ViewDir::Back => [0.0, 0.0, 1.0],
270-
ViewDir::Forward => [0.0, 0.0, -1.0],
271-
}
272-
}
273-
274-
glam::Mat3::from_cols_array_2d(&[rub(self.0[0]), rub(self.0[1]), rub(self.0[2])])
275-
}
276-
277303
#[cfg(feature = "glam")]
278304
#[inline]
279305
pub fn handedness(&self) -> Option<Handedness> {
280-
let to_rub = self.to_rub();
281-
let det = to_rub.determinant();
306+
let to_rdf = self.to_rdf();
307+
let det = to_rdf.determinant();
282308
if det == -1.0 {
283309
Some(Handedness::Left)
284310
} else if det == 0.0 {

crates/re_space_view_spatial/src/scene/contexts/transform_context.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,7 @@ fn transform_at(
276276
// determine how the image plane is oriented.
277277
// (see also `CamerasPart` where the frustum lines are set up)
278278
let view_coordinates = pinhole_camera_view_coordinates(store, query, entity_path);
279-
let world_from_image_plane3d =
280-
// Convert from RUB setup to what we'are actually using.
281-
view_coordinates.from_rub() *
282-
// Account for the orientation of the image plane.
283-
image_view_coordinates().to_rub();
279+
let world_from_image_plane3d = view_coordinates.from_other(&image_view_coordinates());
284280

285281
glam::Affine3A::from_mat3(world_from_image_plane3d) * image_plane3d_from_2d_content
286282

crates/re_space_view_spatial/src/space_camera_3d.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@ impl SpaceCamera3D {
6565
//
6666
// Because the [`Pinhole`] component currently assumes an input in the default `image_view_coordinates`
6767
// we need to pre-transform the data from the user-defined `pinhole_view_coordiantes` to the required
68-
// `image_view_coordinates`. We do this by converting the points into and out of RUB.
69-
//
68+
// `image_view_coordinates`.
69+
//
7070
// TODO(...): When Pinhole is an archetype instead of a component, `pinhole.project` should do this
7171
// internally.
72-
let point_in_image_unprojected = image_view_coordinates().from_rub()
73-
* self.pinhole_view_coordinates.to_rub()
74-
* point_in_cam;
72+
let point_in_image_unprojected =
73+
image_view_coordinates().from_other(&self.pinhole_view_coordinates) * point_in_cam;
7574

7675
let point_in_image = pinhole.project(point_in_image_unprojected);
7776
Some(point_in_image)

crates/re_space_view_spatial/src/ui_3d.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,13 @@ fn show_projections_from_2d_space(
575575
cam.picture_plane_distance
576576
};
577577
let stop_in_image_plane = pinhole.unproject(glam::vec3(pos.x, pos.y, depth));
578-
let stop_in_rub_view = image_view_coordinates().to_rub() * stop_in_image_plane;
579578

580-
let world_from_rub_view = glam::Affine3A::from(cam.world_from_camera)
581-
* glam::Affine3A::from_mat3(cam.pinhole_view_coordinates.from_rub());
582-
let stop_in_world = world_from_rub_view.transform_point3(stop_in_rub_view);
579+
let world_from_image = glam::Affine3A::from(cam.world_from_camera)
580+
* glam::Affine3A::from_mat3(
581+
cam.pinhole_view_coordinates
582+
.from_other(&image_view_coordinates()),
583+
);
584+
let stop_in_world = world_from_image.transform_point3(stop_in_image_plane);
583585

584586
let origin = cam.position();
585587
let ray =

0 commit comments

Comments
 (0)