From 714163e5bfd7c39c611e95704dacc24a8171ee79 Mon Sep 17 00:00:00 2001 From: Jeremy Leibs Date: Tue, 27 Aug 2024 17:17:14 -0400 Subject: [PATCH] Handle proper half-size splatting semantics in from_mins_and_sizes --- .../re_types/src/archetypes/boxes2d_ext.rs | 32 +++++++++++----- .../re_types/src/archetypes/boxes3d_ext.rs | 38 +++++++++++++------ 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/crates/store/re_types/src/archetypes/boxes2d_ext.rs b/crates/store/re_types/src/archetypes/boxes2d_ext.rs index 5b8a1923ef9c..fcdeb0160c71 100644 --- a/crates/store/re_types/src/archetypes/boxes2d_ext.rs +++ b/crates/store/re_types/src/archetypes/boxes2d_ext.rs @@ -51,14 +51,28 @@ impl Boxes2D { sizes: impl IntoIterator>, ) -> Self { let boxes = Self::from_sizes(sizes); - let centers: Vec<_> = mins - .into_iter() - .zip(boxes.half_sizes.iter()) - .map(|(min, half_size)| { - let min = min.into(); - Position2D::new(min.x() + half_size.x(), min.y() + half_size.y()) - }) - .collect(); - boxes.with_centers(centers) + + // The box semantics are such that the last half-size is used for all remaining boxes. + if let Some(last_half_size) = boxes.half_sizes.last() { + let centers: Vec<_> = mins + .into_iter() + .zip( + boxes + .half_sizes + .iter() + .chain(std::iter::repeat(last_half_size)), + ) + .map(|(min, half_size)| { + let min = min.into(); + Position2D::new(min.x() + half_size.x(), min.y() + half_size.y()) + }) + .collect(); + boxes.with_centers(centers) + } else { + if mins.into_iter().next().is_some() { + re_log::warn_once!("Must provide at least one size to create boxes."); + } + boxes.with_centers(std::iter::empty::()) + } } } diff --git a/crates/store/re_types/src/archetypes/boxes3d_ext.rs b/crates/store/re_types/src/archetypes/boxes3d_ext.rs index 39c92ad6349b..39df661d7958 100644 --- a/crates/store/re_types/src/archetypes/boxes3d_ext.rs +++ b/crates/store/re_types/src/archetypes/boxes3d_ext.rs @@ -51,18 +51,32 @@ impl Boxes3D { sizes: impl IntoIterator>, ) -> Self { let boxes = Self::from_sizes(sizes); - let centers: Vec<_> = mins - .into_iter() - .zip(boxes.half_sizes.iter()) - .map(|(min, half_size)| { - let min = min.into(); - PoseTranslation3D::new( - min.x() + half_size.x(), - min.y() + half_size.y(), - min.z() + half_size.z(), + + // The box semantics are such that the last half-size is used for all remaining boxes. + if let Some(last_half_size) = boxes.half_sizes.last() { + let centers: Vec<_> = mins + .into_iter() + .zip( + boxes + .half_sizes + .iter() + .chain(std::iter::repeat(last_half_size)), ) - }) - .collect(); - boxes.with_centers(centers) + .map(|(min, half_size)| { + let min = min.into(); + PoseTranslation3D::new( + min.x() + half_size.x(), + min.y() + half_size.y(), + min.z() + half_size.z(), + ) + }) + .collect(); + boxes.with_centers(centers) + } else { + if mins.into_iter().next().is_some() { + re_log::warn_once!("Must provide at least one size to create boxes."); + } + boxes.with_centers(std::iter::empty::()) + } } }