diff --git a/Cargo.lock b/Cargo.lock index f6211650f901..c063508b4723 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4510,6 +4510,7 @@ dependencies = [ "macaw", "re_build_tools", "re_types_builder", + "similar-asserts", "xshell", ] diff --git a/crates/re_types/Cargo.toml b/crates/re_types/Cargo.toml index 4da294c84c50..d596ff1b9347 100644 --- a/crates/re_types/Cargo.toml +++ b/crates/re_types/Cargo.toml @@ -49,6 +49,7 @@ macaw = { workspace = true, optional = true } # External glam.workspace = true itertools.workspace = true +similar-asserts = "1.4.2" [build-dependencies] diff --git a/crates/re_types/definitions/rerun/components/draw_order.fbs b/crates/re_types/definitions/rerun/components/draw_order.fbs index 819fb82f26e6..5286937a80bf 100644 --- a/crates/re_types/definitions/rerun/components/draw_order.fbs +++ b/crates/re_types/definitions/rerun/components/draw_order.fbs @@ -20,7 +20,7 @@ struct DrawOrder ( "attr.arrow.transparent", "attr.python.aliases": "float", "attr.python.array_aliases": "npt.NDArray[np.float32]", - "attr.rust.derive": "Debug, Clone, Copy, PartialEq, PartialOrd", + "attr.rust.derive": "Debug, Clone, Copy", "attr.rust.repr": "transparent", "attr.rust.tuple_struct", order: 100 diff --git a/crates/re_types/src/components/class_id_ext.rs b/crates/re_types/src/components/class_id_ext.rs new file mode 100644 index 000000000000..60da05d4a0e1 --- /dev/null +++ b/crates/re_types/src/components/class_id_ext.rs @@ -0,0 +1,7 @@ +use super::ClassId; + +impl From for ClassId { + fn from(value: u16) -> Self { + Self(value) + } +} diff --git a/crates/re_types/src/components/color_ext.rs b/crates/re_types/src/components/color_ext.rs new file mode 100644 index 000000000000..ea0703868e7e --- /dev/null +++ b/crates/re_types/src/components/color_ext.rs @@ -0,0 +1,50 @@ +use super::Color; + +impl Color { + #[inline] + pub fn from_rgb(r: u8, g: u8, b: u8) -> Self { + Self::from([r, g, b, 255]) + } + + #[inline] + pub fn from_unmultiplied_rgba(r: u8, g: u8, b: u8, a: u8) -> Self { + Self::from([r, g, b, a]) + } + + #[inline] + pub fn to_array(&self) -> [u8; 4] { + [ + (self.0 >> 24) as u8, + (self.0 >> 16) as u8, + (self.0 >> 8) as u8, + self.0 as u8, + ] + } +} + +impl From for Color { + #[inline] + fn from(c: u32) -> Self { + Self(c) + } +} + +impl From<[u8; 4]> for Color { + #[inline] + fn from(bytes: [u8; 4]) -> Self { + Self( + (bytes[0] as u32) << 24 + | (bytes[1] as u32) << 16 + | (bytes[2] as u32) << 8 + | (bytes[3] as u32), + ) + } +} + +#[cfg(feature = "ecolor")] +impl From for ecolor::Color32 { + fn from(color: Color) -> Self { + let [r, g, b, a] = color.to_array(); + Self::from_rgba_premultiplied(r, g, b, a) + } +} diff --git a/crates/re_types/src/components/draw_order.rs b/crates/re_types/src/components/draw_order.rs index 3ccf929240b7..16cb87bce10d 100644 --- a/crates/re_types/src/components/draw_order.rs +++ b/crates/re_types/src/components/draw_order.rs @@ -7,7 +7,7 @@ /// Within an entity draw order is governed by the order of the components. /// /// Draw order for entities with the same draw order is generally undefined. -#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] +#[derive(Debug, Clone, Copy)] #[repr(transparent)] pub struct DrawOrder(pub f32); diff --git a/crates/re_types/src/components/draw_order_ext.rs b/crates/re_types/src/components/draw_order_ext.rs new file mode 100644 index 000000000000..f99f10213c56 --- /dev/null +++ b/crates/re_types/src/components/draw_order_ext.rs @@ -0,0 +1,60 @@ +use super::DrawOrder; + +// TODO(cmc): come up with some DSL in our flatbuffers definitions so that we can declare these +// constants directly in there. +impl DrawOrder { + /// Draw order used for images if no draw order was specified. + pub const DEFAULT_IMAGE: DrawOrder = DrawOrder(-10.0); + + /// Draw order used for 2D boxes if no draw order was specified. + pub const DEFAULT_BOX2D: DrawOrder = DrawOrder(10.0); + + /// Draw order used for 2D lines if no draw order was specified. + pub const DEFAULT_LINES2D: DrawOrder = DrawOrder(20.0); + + /// Draw order used for 2D points if no draw order was specified. + pub const DEFAULT_POINTS2D: DrawOrder = DrawOrder(30.0); +} + +impl std::cmp::PartialEq for DrawOrder { + #[inline] + fn eq(&self, other: &Self) -> bool { + self.0.is_nan() && other.0.is_nan() || self.0 == other.0 + } +} + +impl std::cmp::Eq for DrawOrder {} + +impl std::cmp::PartialOrd for DrawOrder { + #[inline] + fn partial_cmp(&self, other: &Self) -> Option { + if other == self { + Some(std::cmp::Ordering::Equal) + } else if other.0.is_nan() || self.0 < other.0 { + Some(std::cmp::Ordering::Less) + } else { + Some(std::cmp::Ordering::Greater) + } + } +} + +impl std::cmp::Ord for DrawOrder { + #[inline] + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.partial_cmp(other).unwrap() + } +} + +impl From for DrawOrder { + #[inline] + fn from(value: f32) -> Self { + Self(value) + } +} + +impl From for f32 { + #[inline] + fn from(value: DrawOrder) -> Self { + value.0 + } +} diff --git a/crates/re_types/src/components/instance_key_ext.rs b/crates/re_types/src/components/instance_key_ext.rs new file mode 100644 index 000000000000..c7a40974ebec --- /dev/null +++ b/crates/re_types/src/components/instance_key_ext.rs @@ -0,0 +1,22 @@ +use super::InstanceKey; + +// TODO(cmc): come up with some DSL in our flatbuffers definitions so that we can declare these +// constants directly in there. +impl InstanceKey { + /// Draw order used for images if no draw order was specified. + pub const SPLAT: Self = Self(u64::MAX); +} + +impl From for InstanceKey { + #[inline] + fn from(value: u64) -> Self { + Self(value) + } +} + +impl From for u64 { + #[inline] + fn from(value: InstanceKey) -> Self { + value.0 + } +} diff --git a/crates/re_types/src/components/keypoint_id_ext.rs b/crates/re_types/src/components/keypoint_id_ext.rs new file mode 100644 index 000000000000..e7293dcdaf72 --- /dev/null +++ b/crates/re_types/src/components/keypoint_id_ext.rs @@ -0,0 +1,7 @@ +use super::KeypointId; + +impl From for KeypointId { + fn from(value: u16) -> Self { + Self(value) + } +} diff --git a/crates/re_types/src/components/label_ext.rs b/crates/re_types/src/components/label_ext.rs new file mode 100644 index 000000000000..679284f2b05d --- /dev/null +++ b/crates/re_types/src/components/label_ext.rs @@ -0,0 +1,51 @@ +use super::Label; + +impl Label { + #[inline] + pub fn as_str(&self) -> &str { + self.0.as_str() + } +} + +impl From for Label { + #[inline] + fn from(value: String) -> Self { + Self(value) + } +} + +impl From<&str> for Label { + #[inline] + fn from(value: &str) -> Self { + Self(value.to_owned()) + } +} + +impl From