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