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