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