re_types/archetypes/
boxes2d.rs

1// DO NOT EDIT! This file was auto-generated by crates/build/re_types_builder/src/codegen/rust/api.rs
2// Based on "crates/store/re_types/definitions/rerun/archetypes/boxes2d.fbs".
3
4#![allow(unused_imports)]
5#![allow(unused_parens)]
6#![allow(clippy::clone_on_copy)]
7#![allow(clippy::cloned_instead_of_copied)]
8#![allow(clippy::map_flatten)]
9#![allow(clippy::needless_question_mark)]
10#![allow(clippy::new_without_default)]
11#![allow(clippy::redundant_closure)]
12#![allow(clippy::too_many_arguments)]
13#![allow(clippy::too_many_lines)]
14
15use ::re_types_core::try_serialize_field;
16use ::re_types_core::SerializationResult;
17use ::re_types_core::{ComponentBatch as _, SerializedComponentBatch};
18use ::re_types_core::{ComponentDescriptor, ComponentName};
19use ::re_types_core::{DeserializationError, DeserializationResult};
20
21/// **Archetype**: 2D boxes with half-extents and optional center, colors etc.
22///
23/// ## Example
24///
25/// ### Simple 2D boxes
26/// ```ignore
27/// fn main() -> Result<(), Box<dyn std::error::Error>> {
28///     let rec = rerun::RecordingStreamBuilder::new("rerun_example_box2d").spawn()?;
29///
30///     rec.log(
31///         "simple",
32///         &rerun::Boxes2D::from_mins_and_sizes([(-1., -1.)], [(2., 2.)]),
33///     )?;
34///
35///     Ok(())
36/// }
37/// ```
38/// <center>
39/// <picture>
40///   <source media="(max-width: 480px)" srcset="https://static.rerun.io/box2d_simple/ac4424f3cf747382867649610cbd749c45b2020b/480w.png">
41///   <source media="(max-width: 768px)" srcset="https://static.rerun.io/box2d_simple/ac4424f3cf747382867649610cbd749c45b2020b/768w.png">
42///   <source media="(max-width: 1024px)" srcset="https://static.rerun.io/box2d_simple/ac4424f3cf747382867649610cbd749c45b2020b/1024w.png">
43///   <source media="(max-width: 1200px)" srcset="https://static.rerun.io/box2d_simple/ac4424f3cf747382867649610cbd749c45b2020b/1200w.png">
44///   <img src="https://static.rerun.io/box2d_simple/ac4424f3cf747382867649610cbd749c45b2020b/full.png" width="640">
45/// </picture>
46/// </center>
47#[derive(Clone, Debug, PartialEq, Default)]
48pub struct Boxes2D {
49    /// All half-extents that make up the batch of boxes.
50    pub half_sizes: Option<SerializedComponentBatch>,
51
52    /// Optional center positions of the boxes.
53    pub centers: Option<SerializedComponentBatch>,
54
55    /// Optional colors for the boxes.
56    pub colors: Option<SerializedComponentBatch>,
57
58    /// Optional radii for the lines that make up the boxes.
59    pub radii: Option<SerializedComponentBatch>,
60
61    /// Optional text labels for the boxes.
62    ///
63    /// If there's a single label present, it will be placed at the center of the entity.
64    /// Otherwise, each instance will have its own label.
65    pub labels: Option<SerializedComponentBatch>,
66
67    /// Optional choice of whether the text labels should be shown by default.
68    pub show_labels: Option<SerializedComponentBatch>,
69
70    /// An optional floating point value that specifies the 2D drawing order.
71    ///
72    /// Objects with higher values are drawn on top of those with lower values.
73    /// Defaults to `10.0`.
74    pub draw_order: Option<SerializedComponentBatch>,
75
76    /// Optional [`components::ClassId`][crate::components::ClassId]s for the boxes.
77    ///
78    /// The [`components::ClassId`][crate::components::ClassId] provides colors and labels if not specified explicitly.
79    pub class_ids: Option<SerializedComponentBatch>,
80}
81
82impl Boxes2D {
83    /// Returns the [`ComponentDescriptor`] for [`Self::half_sizes`].
84    #[inline]
85    pub fn descriptor_half_sizes() -> ComponentDescriptor {
86        ComponentDescriptor {
87            archetype_name: Some("rerun.archetypes.Boxes2D".into()),
88            component_name: "rerun.components.HalfSize2D".into(),
89            archetype_field_name: Some("half_sizes".into()),
90        }
91    }
92
93    /// Returns the [`ComponentDescriptor`] for [`Self::centers`].
94    #[inline]
95    pub fn descriptor_centers() -> ComponentDescriptor {
96        ComponentDescriptor {
97            archetype_name: Some("rerun.archetypes.Boxes2D".into()),
98            component_name: "rerun.components.Position2D".into(),
99            archetype_field_name: Some("centers".into()),
100        }
101    }
102
103    /// Returns the [`ComponentDescriptor`] for [`Self::colors`].
104    #[inline]
105    pub fn descriptor_colors() -> ComponentDescriptor {
106        ComponentDescriptor {
107            archetype_name: Some("rerun.archetypes.Boxes2D".into()),
108            component_name: "rerun.components.Color".into(),
109            archetype_field_name: Some("colors".into()),
110        }
111    }
112
113    /// Returns the [`ComponentDescriptor`] for [`Self::radii`].
114    #[inline]
115    pub fn descriptor_radii() -> ComponentDescriptor {
116        ComponentDescriptor {
117            archetype_name: Some("rerun.archetypes.Boxes2D".into()),
118            component_name: "rerun.components.Radius".into(),
119            archetype_field_name: Some("radii".into()),
120        }
121    }
122
123    /// Returns the [`ComponentDescriptor`] for [`Self::labels`].
124    #[inline]
125    pub fn descriptor_labels() -> ComponentDescriptor {
126        ComponentDescriptor {
127            archetype_name: Some("rerun.archetypes.Boxes2D".into()),
128            component_name: "rerun.components.Text".into(),
129            archetype_field_name: Some("labels".into()),
130        }
131    }
132
133    /// Returns the [`ComponentDescriptor`] for [`Self::show_labels`].
134    #[inline]
135    pub fn descriptor_show_labels() -> ComponentDescriptor {
136        ComponentDescriptor {
137            archetype_name: Some("rerun.archetypes.Boxes2D".into()),
138            component_name: "rerun.components.ShowLabels".into(),
139            archetype_field_name: Some("show_labels".into()),
140        }
141    }
142
143    /// Returns the [`ComponentDescriptor`] for [`Self::draw_order`].
144    #[inline]
145    pub fn descriptor_draw_order() -> ComponentDescriptor {
146        ComponentDescriptor {
147            archetype_name: Some("rerun.archetypes.Boxes2D".into()),
148            component_name: "rerun.components.DrawOrder".into(),
149            archetype_field_name: Some("draw_order".into()),
150        }
151    }
152
153    /// Returns the [`ComponentDescriptor`] for [`Self::class_ids`].
154    #[inline]
155    pub fn descriptor_class_ids() -> ComponentDescriptor {
156        ComponentDescriptor {
157            archetype_name: Some("rerun.archetypes.Boxes2D".into()),
158            component_name: "rerun.components.ClassId".into(),
159            archetype_field_name: Some("class_ids".into()),
160        }
161    }
162
163    /// Returns the [`ComponentDescriptor`] for the associated indicator component.
164    #[inline]
165    pub fn descriptor_indicator() -> ComponentDescriptor {
166        ComponentDescriptor {
167            archetype_name: Some("rerun.archetypes.Boxes2D".into()),
168            component_name: "rerun.components.Boxes2DIndicator".into(),
169            archetype_field_name: None,
170        }
171    }
172}
173
174static REQUIRED_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 1usize]> =
175    once_cell::sync::Lazy::new(|| [Boxes2D::descriptor_half_sizes()]);
176
177static RECOMMENDED_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 3usize]> =
178    once_cell::sync::Lazy::new(|| {
179        [
180            Boxes2D::descriptor_centers(),
181            Boxes2D::descriptor_colors(),
182            Boxes2D::descriptor_indicator(),
183        ]
184    });
185
186static OPTIONAL_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 5usize]> =
187    once_cell::sync::Lazy::new(|| {
188        [
189            Boxes2D::descriptor_radii(),
190            Boxes2D::descriptor_labels(),
191            Boxes2D::descriptor_show_labels(),
192            Boxes2D::descriptor_draw_order(),
193            Boxes2D::descriptor_class_ids(),
194        ]
195    });
196
197static ALL_COMPONENTS: once_cell::sync::Lazy<[ComponentDescriptor; 9usize]> =
198    once_cell::sync::Lazy::new(|| {
199        [
200            Boxes2D::descriptor_half_sizes(),
201            Boxes2D::descriptor_centers(),
202            Boxes2D::descriptor_colors(),
203            Boxes2D::descriptor_indicator(),
204            Boxes2D::descriptor_radii(),
205            Boxes2D::descriptor_labels(),
206            Boxes2D::descriptor_show_labels(),
207            Boxes2D::descriptor_draw_order(),
208            Boxes2D::descriptor_class_ids(),
209        ]
210    });
211
212impl Boxes2D {
213    /// The total number of components in the archetype: 1 required, 3 recommended, 5 optional
214    pub const NUM_COMPONENTS: usize = 9usize;
215}
216
217/// Indicator component for the [`Boxes2D`] [`::re_types_core::Archetype`]
218pub type Boxes2DIndicator = ::re_types_core::GenericIndicatorComponent<Boxes2D>;
219
220impl ::re_types_core::Archetype for Boxes2D {
221    type Indicator = Boxes2DIndicator;
222
223    #[inline]
224    fn name() -> ::re_types_core::ArchetypeName {
225        "rerun.archetypes.Boxes2D".into()
226    }
227
228    #[inline]
229    fn display_name() -> &'static str {
230        "Boxes 2D"
231    }
232
233    #[inline]
234    fn indicator() -> SerializedComponentBatch {
235        #[allow(clippy::unwrap_used)]
236        Boxes2DIndicator::DEFAULT.serialized().unwrap()
237    }
238
239    #[inline]
240    fn required_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
241        REQUIRED_COMPONENTS.as_slice().into()
242    }
243
244    #[inline]
245    fn recommended_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
246        RECOMMENDED_COMPONENTS.as_slice().into()
247    }
248
249    #[inline]
250    fn optional_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
251        OPTIONAL_COMPONENTS.as_slice().into()
252    }
253
254    #[inline]
255    fn all_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
256        ALL_COMPONENTS.as_slice().into()
257    }
258
259    #[inline]
260    fn from_arrow_components(
261        arrow_data: impl IntoIterator<Item = (ComponentDescriptor, arrow::array::ArrayRef)>,
262    ) -> DeserializationResult<Self> {
263        re_tracing::profile_function!();
264        use ::re_types_core::{Loggable as _, ResultExt as _};
265        let arrays_by_descr: ::nohash_hasher::IntMap<_, _> = arrow_data.into_iter().collect();
266        let half_sizes = arrays_by_descr
267            .get(&Self::descriptor_half_sizes())
268            .map(|array| {
269                SerializedComponentBatch::new(array.clone(), Self::descriptor_half_sizes())
270            });
271        let centers = arrays_by_descr
272            .get(&Self::descriptor_centers())
273            .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_centers()));
274        let colors = arrays_by_descr
275            .get(&Self::descriptor_colors())
276            .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_colors()));
277        let radii = arrays_by_descr
278            .get(&Self::descriptor_radii())
279            .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_radii()));
280        let labels = arrays_by_descr
281            .get(&Self::descriptor_labels())
282            .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_labels()));
283        let show_labels = arrays_by_descr
284            .get(&Self::descriptor_show_labels())
285            .map(|array| {
286                SerializedComponentBatch::new(array.clone(), Self::descriptor_show_labels())
287            });
288        let draw_order = arrays_by_descr
289            .get(&Self::descriptor_draw_order())
290            .map(|array| {
291                SerializedComponentBatch::new(array.clone(), Self::descriptor_draw_order())
292            });
293        let class_ids = arrays_by_descr
294            .get(&Self::descriptor_class_ids())
295            .map(|array| {
296                SerializedComponentBatch::new(array.clone(), Self::descriptor_class_ids())
297            });
298        Ok(Self {
299            half_sizes,
300            centers,
301            colors,
302            radii,
303            labels,
304            show_labels,
305            draw_order,
306            class_ids,
307        })
308    }
309}
310
311impl ::re_types_core::AsComponents for Boxes2D {
312    #[inline]
313    fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
314        use ::re_types_core::Archetype as _;
315        [
316            Some(Self::indicator()),
317            self.half_sizes.clone(),
318            self.centers.clone(),
319            self.colors.clone(),
320            self.radii.clone(),
321            self.labels.clone(),
322            self.show_labels.clone(),
323            self.draw_order.clone(),
324            self.class_ids.clone(),
325        ]
326        .into_iter()
327        .flatten()
328        .collect()
329    }
330}
331
332impl ::re_types_core::ArchetypeReflectionMarker for Boxes2D {}
333
334impl Boxes2D {
335    /// Create a new `Boxes2D`.
336    #[inline]
337    pub(crate) fn new(
338        half_sizes: impl IntoIterator<Item = impl Into<crate::components::HalfSize2D>>,
339    ) -> Self {
340        Self {
341            half_sizes: try_serialize_field(Self::descriptor_half_sizes(), half_sizes),
342            centers: None,
343            colors: None,
344            radii: None,
345            labels: None,
346            show_labels: None,
347            draw_order: None,
348            class_ids: None,
349        }
350    }
351
352    /// Update only some specific fields of a `Boxes2D`.
353    #[inline]
354    pub fn update_fields() -> Self {
355        Self::default()
356    }
357
358    /// Clear all the fields of a `Boxes2D`.
359    #[inline]
360    pub fn clear_fields() -> Self {
361        use ::re_types_core::Loggable as _;
362        Self {
363            half_sizes: Some(SerializedComponentBatch::new(
364                crate::components::HalfSize2D::arrow_empty(),
365                Self::descriptor_half_sizes(),
366            )),
367            centers: Some(SerializedComponentBatch::new(
368                crate::components::Position2D::arrow_empty(),
369                Self::descriptor_centers(),
370            )),
371            colors: Some(SerializedComponentBatch::new(
372                crate::components::Color::arrow_empty(),
373                Self::descriptor_colors(),
374            )),
375            radii: Some(SerializedComponentBatch::new(
376                crate::components::Radius::arrow_empty(),
377                Self::descriptor_radii(),
378            )),
379            labels: Some(SerializedComponentBatch::new(
380                crate::components::Text::arrow_empty(),
381                Self::descriptor_labels(),
382            )),
383            show_labels: Some(SerializedComponentBatch::new(
384                crate::components::ShowLabels::arrow_empty(),
385                Self::descriptor_show_labels(),
386            )),
387            draw_order: Some(SerializedComponentBatch::new(
388                crate::components::DrawOrder::arrow_empty(),
389                Self::descriptor_draw_order(),
390            )),
391            class_ids: Some(SerializedComponentBatch::new(
392                crate::components::ClassId::arrow_empty(),
393                Self::descriptor_class_ids(),
394            )),
395        }
396    }
397
398    /// Partitions the component data into multiple sub-batches.
399    ///
400    /// Specifically, this transforms the existing [`SerializedComponentBatch`]es data into [`SerializedComponentColumn`]s
401    /// instead, via [`SerializedComponentBatch::partitioned`].
402    ///
403    /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
404    ///
405    /// The specified `lengths` must sum to the total length of the component batch.
406    ///
407    /// [`SerializedComponentColumn`]: [::re_types_core::SerializedComponentColumn]
408    #[inline]
409    pub fn columns<I>(
410        self,
411        _lengths: I,
412    ) -> SerializationResult<impl Iterator<Item = ::re_types_core::SerializedComponentColumn>>
413    where
414        I: IntoIterator<Item = usize> + Clone,
415    {
416        let columns = [
417            self.half_sizes
418                .map(|half_sizes| half_sizes.partitioned(_lengths.clone()))
419                .transpose()?,
420            self.centers
421                .map(|centers| centers.partitioned(_lengths.clone()))
422                .transpose()?,
423            self.colors
424                .map(|colors| colors.partitioned(_lengths.clone()))
425                .transpose()?,
426            self.radii
427                .map(|radii| radii.partitioned(_lengths.clone()))
428                .transpose()?,
429            self.labels
430                .map(|labels| labels.partitioned(_lengths.clone()))
431                .transpose()?,
432            self.show_labels
433                .map(|show_labels| show_labels.partitioned(_lengths.clone()))
434                .transpose()?,
435            self.draw_order
436                .map(|draw_order| draw_order.partitioned(_lengths.clone()))
437                .transpose()?,
438            self.class_ids
439                .map(|class_ids| class_ids.partitioned(_lengths.clone()))
440                .transpose()?,
441        ];
442        Ok(columns
443            .into_iter()
444            .flatten()
445            .chain([::re_types_core::indicator_column::<Self>(
446                _lengths.into_iter().count(),
447            )?]))
448    }
449
450    /// Helper to partition the component data into unit-length sub-batches.
451    ///
452    /// This is semantically similar to calling [`Self::columns`] with `std::iter::take(1).repeat(n)`,
453    /// where `n` is automatically guessed.
454    #[inline]
455    pub fn columns_of_unit_batches(
456        self,
457    ) -> SerializationResult<impl Iterator<Item = ::re_types_core::SerializedComponentColumn>> {
458        let len_half_sizes = self.half_sizes.as_ref().map(|b| b.array.len());
459        let len_centers = self.centers.as_ref().map(|b| b.array.len());
460        let len_colors = self.colors.as_ref().map(|b| b.array.len());
461        let len_radii = self.radii.as_ref().map(|b| b.array.len());
462        let len_labels = self.labels.as_ref().map(|b| b.array.len());
463        let len_show_labels = self.show_labels.as_ref().map(|b| b.array.len());
464        let len_draw_order = self.draw_order.as_ref().map(|b| b.array.len());
465        let len_class_ids = self.class_ids.as_ref().map(|b| b.array.len());
466        let len = None
467            .or(len_half_sizes)
468            .or(len_centers)
469            .or(len_colors)
470            .or(len_radii)
471            .or(len_labels)
472            .or(len_show_labels)
473            .or(len_draw_order)
474            .or(len_class_ids)
475            .unwrap_or(0);
476        self.columns(std::iter::repeat(1).take(len))
477    }
478
479    /// All half-extents that make up the batch of boxes.
480    #[inline]
481    pub fn with_half_sizes(
482        mut self,
483        half_sizes: impl IntoIterator<Item = impl Into<crate::components::HalfSize2D>>,
484    ) -> Self {
485        self.half_sizes = try_serialize_field(Self::descriptor_half_sizes(), half_sizes);
486        self
487    }
488
489    /// Optional center positions of the boxes.
490    #[inline]
491    pub fn with_centers(
492        mut self,
493        centers: impl IntoIterator<Item = impl Into<crate::components::Position2D>>,
494    ) -> Self {
495        self.centers = try_serialize_field(Self::descriptor_centers(), centers);
496        self
497    }
498
499    /// Optional colors for the boxes.
500    #[inline]
501    pub fn with_colors(
502        mut self,
503        colors: impl IntoIterator<Item = impl Into<crate::components::Color>>,
504    ) -> Self {
505        self.colors = try_serialize_field(Self::descriptor_colors(), colors);
506        self
507    }
508
509    /// Optional radii for the lines that make up the boxes.
510    #[inline]
511    pub fn with_radii(
512        mut self,
513        radii: impl IntoIterator<Item = impl Into<crate::components::Radius>>,
514    ) -> Self {
515        self.radii = try_serialize_field(Self::descriptor_radii(), radii);
516        self
517    }
518
519    /// Optional text labels for the boxes.
520    ///
521    /// If there's a single label present, it will be placed at the center of the entity.
522    /// Otherwise, each instance will have its own label.
523    #[inline]
524    pub fn with_labels(
525        mut self,
526        labels: impl IntoIterator<Item = impl Into<crate::components::Text>>,
527    ) -> Self {
528        self.labels = try_serialize_field(Self::descriptor_labels(), labels);
529        self
530    }
531
532    /// Optional choice of whether the text labels should be shown by default.
533    #[inline]
534    pub fn with_show_labels(
535        mut self,
536        show_labels: impl Into<crate::components::ShowLabels>,
537    ) -> Self {
538        self.show_labels = try_serialize_field(Self::descriptor_show_labels(), [show_labels]);
539        self
540    }
541
542    /// This method makes it possible to pack multiple [`crate::components::ShowLabels`] in a single component batch.
543    ///
544    /// This only makes sense when used in conjunction with [`Self::columns`]. [`Self::with_show_labels`] should
545    /// be used when logging a single row's worth of data.
546    #[inline]
547    pub fn with_many_show_labels(
548        mut self,
549        show_labels: impl IntoIterator<Item = impl Into<crate::components::ShowLabels>>,
550    ) -> Self {
551        self.show_labels = try_serialize_field(Self::descriptor_show_labels(), show_labels);
552        self
553    }
554
555    /// An optional floating point value that specifies the 2D drawing order.
556    ///
557    /// Objects with higher values are drawn on top of those with lower values.
558    /// Defaults to `10.0`.
559    #[inline]
560    pub fn with_draw_order(mut self, draw_order: impl Into<crate::components::DrawOrder>) -> Self {
561        self.draw_order = try_serialize_field(Self::descriptor_draw_order(), [draw_order]);
562        self
563    }
564
565    /// This method makes it possible to pack multiple [`crate::components::DrawOrder`] in a single component batch.
566    ///
567    /// This only makes sense when used in conjunction with [`Self::columns`]. [`Self::with_draw_order`] should
568    /// be used when logging a single row's worth of data.
569    #[inline]
570    pub fn with_many_draw_order(
571        mut self,
572        draw_order: impl IntoIterator<Item = impl Into<crate::components::DrawOrder>>,
573    ) -> Self {
574        self.draw_order = try_serialize_field(Self::descriptor_draw_order(), draw_order);
575        self
576    }
577
578    /// Optional [`components::ClassId`][crate::components::ClassId]s for the boxes.
579    ///
580    /// The [`components::ClassId`][crate::components::ClassId] provides colors and labels if not specified explicitly.
581    #[inline]
582    pub fn with_class_ids(
583        mut self,
584        class_ids: impl IntoIterator<Item = impl Into<crate::components::ClassId>>,
585    ) -> Self {
586        self.class_ids = try_serialize_field(Self::descriptor_class_ids(), class_ids);
587        self
588    }
589}
590
591impl ::re_byte_size::SizeBytes for Boxes2D {
592    #[inline]
593    fn heap_size_bytes(&self) -> u64 {
594        self.half_sizes.heap_size_bytes()
595            + self.centers.heap_size_bytes()
596            + self.colors.heap_size_bytes()
597            + self.radii.heap_size_bytes()
598            + self.labels.heap_size_bytes()
599            + self.show_labels.heap_size_bytes()
600            + self.draw_order.heap_size_bytes()
601            + self.class_ids.heap_size_bytes()
602    }
603}