re_types/archetypes/
boxes3d_ext.rs

1use crate::{
2    components::{HalfSize3D, PoseTranslation3D},
3    datatypes::Vec3D,
4};
5
6use super::Boxes3D;
7
8impl Boxes3D {
9    /// Creates new [`Boxes3D`] with [`Self::half_sizes`] centered around the local origin.
10    #[inline]
11    pub fn from_half_sizes(half_sizes: impl IntoIterator<Item = impl Into<HalfSize3D>>) -> Self {
12        Self::new(half_sizes)
13    }
14
15    /// Creates new [`Boxes3D`] with [`Self::centers`] and [`Self::half_sizes`].
16    #[inline]
17    pub fn from_centers_and_half_sizes(
18        centers: impl IntoIterator<Item = impl Into<PoseTranslation3D>>,
19        half_sizes: impl IntoIterator<Item = impl Into<HalfSize3D>>,
20    ) -> Self {
21        Self::new(half_sizes).with_centers(centers)
22    }
23
24    /// Creates new [`Boxes3D`] with [`Self::half_sizes`] created from (full) sizes.
25    ///
26    /// TODO(#3285): Does *not* preserve data as-is and instead creates half-sizes from the input data.
27    #[inline]
28    pub fn from_sizes(sizes: impl IntoIterator<Item = impl Into<Vec3D>>) -> Self {
29        Self::new(sizes.into_iter().map(|size| {
30            let size = size.into();
31            HalfSize3D::new(size.x() / 2.0, size.y() / 2.0, size.z() / 2.0)
32        }))
33    }
34
35    /// Creates new [`Boxes3D`] with [`Self::centers`] and [`Self::half_sizes`] created from centers and (full) sizes.
36    ///
37    /// TODO(#3285): Does *not* preserve data as-is and instead creates half-sizes from the input data.
38    #[inline]
39    pub fn from_centers_and_sizes(
40        centers: impl IntoIterator<Item = impl Into<PoseTranslation3D>>,
41        sizes: impl IntoIterator<Item = impl Into<Vec3D>>,
42    ) -> Self {
43        Self::from_sizes(sizes).with_centers(centers)
44    }
45
46    /// Creates new [`Boxes3D`] with [`Self::centers`] and [`Self::half_sizes`] created from minimums and (full) sizes.
47    ///
48    /// TODO(#3285): Does *not* preserve data as-is and instead creates centers and half-sizes from the input data.
49    pub fn from_mins_and_sizes(
50        mins: impl IntoIterator<Item = impl Into<Vec3D>>,
51        sizes: impl IntoIterator<Item = impl Into<Vec3D>>,
52    ) -> Self {
53        let half_sizes: Vec<_> = sizes
54            .into_iter()
55            .map(|size| {
56                let size = size.into();
57                HalfSize3D::new(size.x() / 2.0, size.y() / 2.0, size.z() / 2.0)
58            })
59            .collect();
60
61        // The box semantics are such that the last half-size is used for all remaining boxes.
62        if let Some(last_half_size) = half_sizes.last() {
63            let centers: Vec<_> = mins
64                .into_iter()
65                .zip(half_sizes.iter().chain(std::iter::repeat(last_half_size)))
66                .map(|(min, half_size)| {
67                    let min = min.into();
68                    PoseTranslation3D::new(
69                        min.x() + half_size.x(),
70                        min.y() + half_size.y(),
71                        min.z() + half_size.z(),
72                    )
73                })
74                .collect();
75            Self::from_half_sizes(half_sizes).with_centers(centers)
76        } else {
77            if mins.into_iter().next().is_some() {
78                re_log::warn_once!("Must provide at least one size to create boxes.");
79            }
80            Self::from_half_sizes(half_sizes)
81                .with_centers(std::iter::empty::<crate::components::PoseTranslation3D>())
82        }
83    }
84}