re_types/datatypes/
annotation_info.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/datatypes/annotation_info.fbs".
3
4#![allow(unused_braces)]
5#![allow(unused_imports)]
6#![allow(unused_parens)]
7#![allow(clippy::allow_attributes)]
8#![allow(clippy::clone_on_copy)]
9#![allow(clippy::cloned_instead_of_copied)]
10#![allow(clippy::map_flatten)]
11#![allow(clippy::needless_question_mark)]
12#![allow(clippy::new_without_default)]
13#![allow(clippy::redundant_closure)]
14#![allow(clippy::too_many_arguments)]
15#![allow(clippy::too_many_lines)]
16#![allow(clippy::wildcard_imports)]
17
18use ::re_types_core::SerializationResult;
19use ::re_types_core::try_serialize_field;
20use ::re_types_core::{ComponentBatch as _, SerializedComponentBatch};
21use ::re_types_core::{ComponentDescriptor, ComponentType};
22use ::re_types_core::{DeserializationError, DeserializationResult};
23
24/// **Datatype**: Annotation info annotating a class id or key-point id.
25///
26/// Color and label will be used to annotate entities/keypoints which reference the id.
27/// The id refers either to a class or key-point id
28#[derive(Clone, Debug, Default, Eq, PartialEq)]
29pub struct AnnotationInfo {
30    /// [`datatypes::ClassId`][crate::datatypes::ClassId] or [`datatypes::KeypointId`][crate::datatypes::KeypointId] to which this annotation info belongs.
31    pub id: u16,
32
33    /// The label that will be shown in the UI.
34    pub label: Option<crate::datatypes::Utf8>,
35
36    /// The color that will be applied to the annotated entity.
37    pub color: Option<crate::datatypes::Rgba32>,
38}
39
40::re_types_core::macros::impl_into_cow!(AnnotationInfo);
41
42impl ::re_types_core::Loggable for AnnotationInfo {
43    #[inline]
44    fn arrow_datatype() -> arrow::datatypes::DataType {
45        use arrow::datatypes::*;
46        DataType::Struct(Fields::from(vec![
47            Field::new("id", DataType::UInt16, false),
48            Field::new("label", <crate::datatypes::Utf8>::arrow_datatype(), true),
49            Field::new("color", <crate::datatypes::Rgba32>::arrow_datatype(), true),
50        ]))
51    }
52
53    fn to_arrow_opt<'a>(
54        data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
55    ) -> SerializationResult<arrow::array::ArrayRef>
56    where
57        Self: Clone + 'a,
58    {
59        #![allow(clippy::manual_is_variant_and)]
60        use ::re_types_core::{Loggable as _, ResultExt as _, arrow_helpers::as_array_ref};
61        use arrow::{array::*, buffer::*, datatypes::*};
62        Ok({
63            let fields = Fields::from(vec![
64                Field::new("id", DataType::UInt16, false),
65                Field::new("label", <crate::datatypes::Utf8>::arrow_datatype(), true),
66                Field::new("color", <crate::datatypes::Rgba32>::arrow_datatype(), true),
67            ]);
68            let (somes, data): (Vec<_>, Vec<_>) = data
69                .into_iter()
70                .map(|datum| {
71                    let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
72                    (datum.is_some(), datum)
73                })
74                .unzip();
75            let validity: Option<arrow::buffer::NullBuffer> = {
76                let any_nones = somes.iter().any(|some| !*some);
77                any_nones.then(|| somes.into())
78            };
79            as_array_ref(StructArray::new(
80                fields,
81                vec![
82                    {
83                        let (somes, id): (Vec<_>, Vec<_>) = data
84                            .iter()
85                            .map(|datum| {
86                                let datum = datum.as_ref().map(|datum| datum.id.clone());
87                                (datum.is_some(), datum)
88                            })
89                            .unzip();
90                        let id_validity: Option<arrow::buffer::NullBuffer> = {
91                            let any_nones = somes.iter().any(|some| !*some);
92                            any_nones.then(|| somes.into())
93                        };
94                        as_array_ref(PrimitiveArray::<UInt16Type>::new(
95                            ScalarBuffer::from(
96                                id.into_iter()
97                                    .map(|v| v.unwrap_or_default())
98                                    .collect::<Vec<_>>(),
99                            ),
100                            id_validity,
101                        ))
102                    },
103                    {
104                        let (somes, label): (Vec<_>, Vec<_>) = data
105                            .iter()
106                            .map(|datum| {
107                                let datum =
108                                    datum.as_ref().map(|datum| datum.label.clone()).flatten();
109                                (datum.is_some(), datum)
110                            })
111                            .unzip();
112                        let label_validity: Option<arrow::buffer::NullBuffer> = {
113                            let any_nones = somes.iter().any(|some| !*some);
114                            any_nones.then(|| somes.into())
115                        };
116                        {
117                            let offsets = arrow::buffer::OffsetBuffer::from_lengths(
118                                label.iter().map(|opt| {
119                                    opt.as_ref().map(|datum| datum.0.len()).unwrap_or_default()
120                                }),
121                            );
122                            #[expect(clippy::unwrap_used)]
123                            let capacity = offsets.last().copied().unwrap() as usize;
124                            let mut buffer_builder =
125                                arrow::array::builder::BufferBuilder::<u8>::new(capacity);
126                            for data in label.iter().flatten() {
127                                buffer_builder.append_slice(data.0.as_bytes());
128                            }
129                            let inner_data: arrow::buffer::Buffer = buffer_builder.finish();
130
131                            #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
132                            as_array_ref(unsafe {
133                                StringArray::new_unchecked(offsets, inner_data, label_validity)
134                            })
135                        }
136                    },
137                    {
138                        let (somes, color): (Vec<_>, Vec<_>) = data
139                            .iter()
140                            .map(|datum| {
141                                let datum =
142                                    datum.as_ref().map(|datum| datum.color.clone()).flatten();
143                                (datum.is_some(), datum)
144                            })
145                            .unzip();
146                        let color_validity: Option<arrow::buffer::NullBuffer> = {
147                            let any_nones = somes.iter().any(|some| !*some);
148                            any_nones.then(|| somes.into())
149                        };
150                        as_array_ref(PrimitiveArray::<UInt32Type>::new(
151                            ScalarBuffer::from(
152                                color
153                                    .into_iter()
154                                    .map(|datum| datum.map(|datum| datum.0).unwrap_or_default())
155                                    .collect::<Vec<_>>(),
156                            ),
157                            color_validity,
158                        ))
159                    },
160                ],
161                validity,
162            ))
163        })
164    }
165
166    fn from_arrow_opt(
167        arrow_data: &dyn arrow::array::Array,
168    ) -> DeserializationResult<Vec<Option<Self>>>
169    where
170        Self: Sized,
171    {
172        use ::re_types_core::{Loggable as _, ResultExt as _, arrow_zip_validity::ZipValidity};
173        use arrow::{array::*, buffer::*, datatypes::*};
174        Ok({
175            let arrow_data = arrow_data
176                .as_any()
177                .downcast_ref::<arrow::array::StructArray>()
178                .ok_or_else(|| {
179                    let expected = Self::arrow_datatype();
180                    let actual = arrow_data.data_type().clone();
181                    DeserializationError::datatype_mismatch(expected, actual)
182                })
183                .with_context("rerun.datatypes.AnnotationInfo")?;
184            if arrow_data.is_empty() {
185                Vec::new()
186            } else {
187                let (arrow_data_fields, arrow_data_arrays) =
188                    (arrow_data.fields(), arrow_data.columns());
189                let arrays_by_name: ::std::collections::HashMap<_, _> = arrow_data_fields
190                    .iter()
191                    .map(|field| field.name().as_str())
192                    .zip(arrow_data_arrays)
193                    .collect();
194                let id = {
195                    if !arrays_by_name.contains_key("id") {
196                        return Err(DeserializationError::missing_struct_field(
197                            Self::arrow_datatype(),
198                            "id",
199                        ))
200                        .with_context("rerun.datatypes.AnnotationInfo");
201                    }
202                    let arrow_data = &**arrays_by_name["id"];
203                    arrow_data
204                        .as_any()
205                        .downcast_ref::<UInt16Array>()
206                        .ok_or_else(|| {
207                            let expected = DataType::UInt16;
208                            let actual = arrow_data.data_type().clone();
209                            DeserializationError::datatype_mismatch(expected, actual)
210                        })
211                        .with_context("rerun.datatypes.AnnotationInfo#id")?
212                        .into_iter()
213                };
214                let label = {
215                    if !arrays_by_name.contains_key("label") {
216                        return Err(DeserializationError::missing_struct_field(
217                            Self::arrow_datatype(),
218                            "label",
219                        ))
220                        .with_context("rerun.datatypes.AnnotationInfo");
221                    }
222                    let arrow_data = &**arrays_by_name["label"];
223                    {
224                        let arrow_data = arrow_data
225                            .as_any()
226                            .downcast_ref::<StringArray>()
227                            .ok_or_else(|| {
228                                let expected = DataType::Utf8;
229                                let actual = arrow_data.data_type().clone();
230                                DeserializationError::datatype_mismatch(expected, actual)
231                            })
232                            .with_context("rerun.datatypes.AnnotationInfo#label")?;
233                        let arrow_data_buf = arrow_data.values();
234                        let offsets = arrow_data.offsets();
235                        ZipValidity::new_with_validity(offsets.windows(2), arrow_data.nulls())
236                            .map(|elem| {
237                                elem.map(|window| {
238                                    let start = window[0] as usize;
239                                    let end = window[1] as usize;
240                                    let len = end - start;
241                                    if arrow_data_buf.len() < end {
242                                        return Err(DeserializationError::offset_slice_oob(
243                                            (start, end),
244                                            arrow_data_buf.len(),
245                                        ));
246                                    }
247                                    let data = arrow_data_buf.slice_with_length(start, len);
248                                    Ok(data)
249                                })
250                                .transpose()
251                            })
252                            .map(|res_or_opt| {
253                                res_or_opt.map(|res_or_opt| {
254                                    res_or_opt.map(|v| {
255                                        crate::datatypes::Utf8(::re_types_core::ArrowString::from(
256                                            v,
257                                        ))
258                                    })
259                                })
260                            })
261                            .collect::<DeserializationResult<Vec<Option<_>>>>()
262                            .with_context("rerun.datatypes.AnnotationInfo#label")?
263                            .into_iter()
264                    }
265                };
266                let color = {
267                    if !arrays_by_name.contains_key("color") {
268                        return Err(DeserializationError::missing_struct_field(
269                            Self::arrow_datatype(),
270                            "color",
271                        ))
272                        .with_context("rerun.datatypes.AnnotationInfo");
273                    }
274                    let arrow_data = &**arrays_by_name["color"];
275                    arrow_data
276                        .as_any()
277                        .downcast_ref::<UInt32Array>()
278                        .ok_or_else(|| {
279                            let expected = DataType::UInt32;
280                            let actual = arrow_data.data_type().clone();
281                            DeserializationError::datatype_mismatch(expected, actual)
282                        })
283                        .with_context("rerun.datatypes.AnnotationInfo#color")?
284                        .into_iter()
285                        .map(|res_or_opt| res_or_opt.map(crate::datatypes::Rgba32))
286                };
287                ZipValidity::new_with_validity(
288                    ::itertools::izip!(id, label, color),
289                    arrow_data.nulls(),
290                )
291                .map(|opt| {
292                    opt.map(|(id, label, color)| {
293                        Ok(Self {
294                            id: id
295                                .ok_or_else(DeserializationError::missing_data)
296                                .with_context("rerun.datatypes.AnnotationInfo#id")?,
297                            label,
298                            color,
299                        })
300                    })
301                    .transpose()
302                })
303                .collect::<DeserializationResult<Vec<_>>>()
304                .with_context("rerun.datatypes.AnnotationInfo")?
305            }
306        })
307    }
308}
309
310impl ::re_byte_size::SizeBytes for AnnotationInfo {
311    #[inline]
312    fn heap_size_bytes(&self) -> u64 {
313        self.id.heap_size_bytes() + self.label.heap_size_bytes() + self.color.heap_size_bytes()
314    }
315
316    #[inline]
317    fn is_pod() -> bool {
318        <u16>::is_pod()
319            && <Option<crate::datatypes::Utf8>>::is_pod()
320            && <Option<crate::datatypes::Rgba32>>::is_pod()
321    }
322}