Skip to content

Commit

Permalink
add tests & comments for point/scene distinction
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Jul 2, 2024
1 parent 79991c7 commit 9f729cf
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
38 changes: 36 additions & 2 deletions crates/re_renderer/src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ impl Size {

/// Get the scene-size of this, if stored as a scene size.
#[inline]
pub fn scene(&self) -> Option<f32> {
pub fn scene_units(&self) -> Option<f32> {
// Ensure negative zero is treated as a point size.
self.0.is_sign_positive().then_some(self.0)
}

/// Get the point size of this, if stored as a point size.
#[inline]
pub fn points(&self) -> Option<f32> {
pub fn ui_points(&self) -> Option<f32> {
// Ensure negative zero is treated as a point size.
self.0.is_sign_negative().then_some(-self.0)
}
}
Expand Down Expand Up @@ -86,3 +88,35 @@ impl From<Size> for SizeHalf {
Self(half::f16::from_f32(size.0))
}
}

#[cfg(test)]
mod tests {
use crate::Size;

#[test]
fn scene_point_distinction() {
let size = Size(1.0);
assert_eq!(size.scene_units(), Some(1.0));
assert_eq!(size.ui_points(), None);

let size = Size(-1.0);
assert_eq!(size.scene_units(), None);
assert_eq!(size.ui_points(), Some(1.0));

let size = Size(f32::INFINITY);
assert_eq!(size.scene_units(), Some(f32::INFINITY));
assert_eq!(size.ui_points(), None);

let size = Size(f32::NEG_INFINITY);
assert_eq!(size.scene_units(), None);
assert_eq!(size.ui_points(), Some(f32::INFINITY));

let size = Size(0.0);
assert_eq!(size.scene_units(), Some(0.0));
assert_eq!(size.ui_points(), None);

let size = Size(-0.0);
assert_eq!(size.scene_units(), None);
assert_eq!(size.ui_points(), Some(0.0));
}
}
34 changes: 34 additions & 0 deletions crates/re_types/src/components/radius_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,46 @@ impl Radius {
/// If this radius is in scene units, returns the radius in scene units.
#[inline]
pub fn scene_units(&self) -> Option<f32> {
// Ensure negative zero is treated as a point size.
self.0.is_sign_positive().then_some(self.0)
}

/// If this radius is in ui points, returns the radius in ui points.
#[inline]
pub fn ui_points(&self) -> Option<f32> {
// Ensure negative zero is treated as a point size.
self.0.is_sign_negative().then_some(-self.0)
}
}

#[cfg(test)]
mod tests {
use super::Radius;

#[test]
fn scene_point_distinction() {
let radius = Radius(1.0);
assert_eq!(radius.scene_units(), Some(1.0));
assert_eq!(radius.ui_points(), None);

let radius = Radius(-1.0);
assert_eq!(radius.scene_units(), None);
assert_eq!(radius.ui_points(), Some(1.0));

let radius = Radius(f32::INFINITY);
assert_eq!(radius.scene_units(), Some(f32::INFINITY));
assert_eq!(radius.ui_points(), None);

let radius = Radius(f32::NEG_INFINITY);
assert_eq!(radius.scene_units(), None);
assert_eq!(radius.ui_points(), Some(f32::INFINITY));

let radius = Radius(0.0);
assert_eq!(radius.scene_units(), Some(0.0));
assert_eq!(radius.ui_points(), None);

let radius = Radius(-0.0);
assert_eq!(radius.scene_units(), None);
assert_eq!(radius.ui_points(), Some(0.0));
}
}

0 comments on commit 9f729cf

Please sign in to comment.