From 9f729cffee831eaee148e3a08e2e7c2394be39ab Mon Sep 17 00:00:00 2001 From: Andreas Reich Date: Tue, 2 Jul 2024 09:26:23 +0200 Subject: [PATCH] add tests & comments for point/scene distinction --- crates/re_renderer/src/size.rs | 38 ++++++++++++++++++-- crates/re_types/src/components/radius_ext.rs | 34 ++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/crates/re_renderer/src/size.rs b/crates/re_renderer/src/size.rs index 591988f65d33..6de8f69526a2 100644 --- a/crates/re_renderer/src/size.rs +++ b/crates/re_renderer/src/size.rs @@ -37,13 +37,15 @@ impl Size { /// Get the scene-size of this, if stored as a scene size. #[inline] - pub fn scene(&self) -> Option { + pub fn scene_units(&self) -> Option { + // 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 { + pub fn ui_points(&self) -> Option { + // Ensure negative zero is treated as a point size. self.0.is_sign_negative().then_some(-self.0) } } @@ -86,3 +88,35 @@ impl From 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)); + } +} diff --git a/crates/re_types/src/components/radius_ext.rs b/crates/re_types/src/components/radius_ext.rs index 4f2a53c42dc3..6bf920283bf3 100644 --- a/crates/re_types/src/components/radius_ext.rs +++ b/crates/re_types/src/components/radius_ext.rs @@ -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 { + // 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 { + // 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)); + } +}