re_types/datatypes/
class_description.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/class_description.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/// **Datatype**: The description of a semantic Class.
23///
24/// If an entity is annotated with a corresponding [`components::ClassId`][crate::components::ClassId], Rerun will use
25/// the attached [`datatypes::AnnotationInfo`][crate::datatypes::AnnotationInfo] to derive labels and colors.
26///
27/// Keypoints within an annotation class can similarly be annotated with a
28/// [`components::KeypointId`][crate::components::KeypointId] in which case we should defer to the label and color for the
29/// [`datatypes::AnnotationInfo`][crate::datatypes::AnnotationInfo] specifically associated with the Keypoint.
30///
31/// Keypoints within the class can also be decorated with skeletal edges.
32/// Keypoint-connections are pairs of [`components::KeypointId`][crate::components::KeypointId]s. If an edge is
33/// defined, and both keypoints exist within the instance of the class, then the
34/// keypoints should be connected with an edge. The edge should be labeled and
35/// colored as described by the class's [`datatypes::AnnotationInfo`][crate::datatypes::AnnotationInfo].
36#[derive(Clone, Debug, Default, Eq, PartialEq)]
37pub struct ClassDescription {
38    /// The [`datatypes::AnnotationInfo`][crate::datatypes::AnnotationInfo] for the class.
39    pub info: crate::datatypes::AnnotationInfo,
40
41    /// The [`datatypes::AnnotationInfo`][crate::datatypes::AnnotationInfo] for all of the keypoints.
42    pub keypoint_annotations: Vec<crate::datatypes::AnnotationInfo>,
43
44    /// The connections between keypoints.
45    pub keypoint_connections: Vec<crate::datatypes::KeypointPair>,
46}
47
48::re_types_core::macros::impl_into_cow!(ClassDescription);
49
50impl ::re_types_core::Loggable for ClassDescription {
51    #[inline]
52    fn arrow_datatype() -> arrow::datatypes::DataType {
53        #![allow(clippy::wildcard_imports)]
54        use arrow::datatypes::*;
55        DataType::Struct(Fields::from(vec![
56            Field::new(
57                "info",
58                <crate::datatypes::AnnotationInfo>::arrow_datatype(),
59                false,
60            ),
61            Field::new(
62                "keypoint_annotations",
63                DataType::List(std::sync::Arc::new(Field::new(
64                    "item",
65                    <crate::datatypes::AnnotationInfo>::arrow_datatype(),
66                    false,
67                ))),
68                false,
69            ),
70            Field::new(
71                "keypoint_connections",
72                DataType::List(std::sync::Arc::new(Field::new(
73                    "item",
74                    <crate::datatypes::KeypointPair>::arrow_datatype(),
75                    false,
76                ))),
77                false,
78            ),
79        ]))
80    }
81
82    fn to_arrow_opt<'a>(
83        data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
84    ) -> SerializationResult<arrow::array::ArrayRef>
85    where
86        Self: Clone + 'a,
87    {
88        #![allow(clippy::wildcard_imports)]
89        #![allow(clippy::manual_is_variant_and)]
90        use ::re_types_core::{arrow_helpers::as_array_ref, Loggable as _, ResultExt as _};
91        use arrow::{array::*, buffer::*, datatypes::*};
92        Ok({
93            let fields = Fields::from(vec![
94                Field::new(
95                    "info",
96                    <crate::datatypes::AnnotationInfo>::arrow_datatype(),
97                    false,
98                ),
99                Field::new(
100                    "keypoint_annotations",
101                    DataType::List(std::sync::Arc::new(Field::new(
102                        "item",
103                        <crate::datatypes::AnnotationInfo>::arrow_datatype(),
104                        false,
105                    ))),
106                    false,
107                ),
108                Field::new(
109                    "keypoint_connections",
110                    DataType::List(std::sync::Arc::new(Field::new(
111                        "item",
112                        <crate::datatypes::KeypointPair>::arrow_datatype(),
113                        false,
114                    ))),
115                    false,
116                ),
117            ]);
118            let (somes, data): (Vec<_>, Vec<_>) = data
119                .into_iter()
120                .map(|datum| {
121                    let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
122                    (datum.is_some(), datum)
123                })
124                .unzip();
125            let validity: Option<arrow::buffer::NullBuffer> = {
126                let any_nones = somes.iter().any(|some| !*some);
127                any_nones.then(|| somes.into())
128            };
129            as_array_ref(StructArray::new(
130                fields,
131                vec![
132                    {
133                        let (somes, info): (Vec<_>, Vec<_>) = data
134                            .iter()
135                            .map(|datum| {
136                                let datum = datum.as_ref().map(|datum| datum.info.clone());
137                                (datum.is_some(), datum)
138                            })
139                            .unzip();
140                        let info_validity: Option<arrow::buffer::NullBuffer> = {
141                            let any_nones = somes.iter().any(|some| !*some);
142                            any_nones.then(|| somes.into())
143                        };
144                        {
145                            _ = info_validity;
146                            crate::datatypes::AnnotationInfo::to_arrow_opt(info)?
147                        }
148                    },
149                    {
150                        let (somes, keypoint_annotations): (Vec<_>, Vec<_>) = data
151                            .iter()
152                            .map(|datum| {
153                                let datum = datum
154                                    .as_ref()
155                                    .map(|datum| datum.keypoint_annotations.clone());
156                                (datum.is_some(), datum)
157                            })
158                            .unzip();
159                        let keypoint_annotations_validity: Option<arrow::buffer::NullBuffer> = {
160                            let any_nones = somes.iter().any(|some| !*some);
161                            any_nones.then(|| somes.into())
162                        };
163                        {
164                            let offsets = arrow::buffer::OffsetBuffer::<i32>::from_lengths(
165                                keypoint_annotations
166                                    .iter()
167                                    .map(|opt| opt.as_ref().map_or(0, |datum| datum.len())),
168                            );
169                            let keypoint_annotations_inner_data: Vec<_> = keypoint_annotations
170                                .into_iter()
171                                .flatten()
172                                .flatten()
173                                .collect();
174                            let keypoint_annotations_inner_validity: Option<
175                                arrow::buffer::NullBuffer,
176                            > = None;
177                            as_array_ref(ListArray::try_new(
178                                std::sync::Arc::new(Field::new(
179                                    "item",
180                                    <crate::datatypes::AnnotationInfo>::arrow_datatype(),
181                                    false,
182                                )),
183                                offsets,
184                                {
185                                    _ = keypoint_annotations_inner_validity;
186                                    crate::datatypes::AnnotationInfo::to_arrow_opt(
187                                        keypoint_annotations_inner_data.into_iter().map(Some),
188                                    )?
189                                },
190                                keypoint_annotations_validity,
191                            )?)
192                        }
193                    },
194                    {
195                        let (somes, keypoint_connections): (Vec<_>, Vec<_>) = data
196                            .iter()
197                            .map(|datum| {
198                                let datum = datum
199                                    .as_ref()
200                                    .map(|datum| datum.keypoint_connections.clone());
201                                (datum.is_some(), datum)
202                            })
203                            .unzip();
204                        let keypoint_connections_validity: Option<arrow::buffer::NullBuffer> = {
205                            let any_nones = somes.iter().any(|some| !*some);
206                            any_nones.then(|| somes.into())
207                        };
208                        {
209                            let offsets = arrow::buffer::OffsetBuffer::<i32>::from_lengths(
210                                keypoint_connections
211                                    .iter()
212                                    .map(|opt| opt.as_ref().map_or(0, |datum| datum.len())),
213                            );
214                            let keypoint_connections_inner_data: Vec<_> = keypoint_connections
215                                .into_iter()
216                                .flatten()
217                                .flatten()
218                                .collect();
219                            let keypoint_connections_inner_validity: Option<
220                                arrow::buffer::NullBuffer,
221                            > = None;
222                            as_array_ref(ListArray::try_new(
223                                std::sync::Arc::new(Field::new(
224                                    "item",
225                                    <crate::datatypes::KeypointPair>::arrow_datatype(),
226                                    false,
227                                )),
228                                offsets,
229                                {
230                                    _ = keypoint_connections_inner_validity;
231                                    crate::datatypes::KeypointPair::to_arrow_opt(
232                                        keypoint_connections_inner_data.into_iter().map(Some),
233                                    )?
234                                },
235                                keypoint_connections_validity,
236                            )?)
237                        }
238                    },
239                ],
240                validity,
241            ))
242        })
243    }
244
245    fn from_arrow_opt(
246        arrow_data: &dyn arrow::array::Array,
247    ) -> DeserializationResult<Vec<Option<Self>>>
248    where
249        Self: Sized,
250    {
251        #![allow(clippy::wildcard_imports)]
252        use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
253        use arrow::{array::*, buffer::*, datatypes::*};
254        Ok({
255            let arrow_data = arrow_data
256                .as_any()
257                .downcast_ref::<arrow::array::StructArray>()
258                .ok_or_else(|| {
259                    let expected = Self::arrow_datatype();
260                    let actual = arrow_data.data_type().clone();
261                    DeserializationError::datatype_mismatch(expected, actual)
262                })
263                .with_context("rerun.datatypes.ClassDescription")?;
264            if arrow_data.is_empty() {
265                Vec::new()
266            } else {
267                let (arrow_data_fields, arrow_data_arrays) =
268                    (arrow_data.fields(), arrow_data.columns());
269                let arrays_by_name: ::std::collections::HashMap<_, _> = arrow_data_fields
270                    .iter()
271                    .map(|field| field.name().as_str())
272                    .zip(arrow_data_arrays)
273                    .collect();
274                let info = {
275                    if !arrays_by_name.contains_key("info") {
276                        return Err(DeserializationError::missing_struct_field(
277                            Self::arrow_datatype(),
278                            "info",
279                        ))
280                        .with_context("rerun.datatypes.ClassDescription");
281                    }
282                    let arrow_data = &**arrays_by_name["info"];
283                    crate::datatypes::AnnotationInfo::from_arrow_opt(arrow_data)
284                        .with_context("rerun.datatypes.ClassDescription#info")?
285                        .into_iter()
286                };
287                let keypoint_annotations = {
288                    if !arrays_by_name.contains_key("keypoint_annotations") {
289                        return Err(DeserializationError::missing_struct_field(
290                            Self::arrow_datatype(),
291                            "keypoint_annotations",
292                        ))
293                        .with_context("rerun.datatypes.ClassDescription");
294                    }
295                    let arrow_data = &**arrays_by_name["keypoint_annotations"];
296                    {
297                        let arrow_data = arrow_data
298                            .as_any()
299                            .downcast_ref::<arrow::array::ListArray>()
300                            .ok_or_else(|| {
301                                let expected = DataType::List(std::sync::Arc::new(Field::new(
302                                    "item",
303                                    <crate::datatypes::AnnotationInfo>::arrow_datatype(),
304                                    false,
305                                )));
306                                let actual = arrow_data.data_type().clone();
307                                DeserializationError::datatype_mismatch(expected, actual)
308                            })
309                            .with_context(
310                                "rerun.datatypes.ClassDescription#keypoint_annotations",
311                            )?;
312                        if arrow_data.is_empty() {
313                            Vec::new()
314                        } else {
315                            let arrow_data_inner = {
316                                let arrow_data_inner = &**arrow_data.values();
317                                crate::datatypes::AnnotationInfo::from_arrow_opt(arrow_data_inner)
318                                    .with_context(
319                                        "rerun.datatypes.ClassDescription#keypoint_annotations",
320                                    )?
321                                    .into_iter()
322                                    .collect::<Vec<_>>()
323                            };
324                            let offsets = arrow_data.offsets();
325                            ZipValidity::new_with_validity(offsets.windows(2), arrow_data.nulls())
326                                .map(|elem| {
327                                    elem.map(|window| {
328                                        let start = window[0] as usize;
329                                        let end = window[1] as usize;
330                                        if arrow_data_inner.len() < end {
331                                            return Err(DeserializationError::offset_slice_oob(
332                                                (start, end),
333                                                arrow_data_inner.len(),
334                                            ));
335                                        }
336
337                                        #[allow(unsafe_code, clippy::undocumented_unsafe_blocks)]
338                                        let data =
339                                            unsafe { arrow_data_inner.get_unchecked(start..end) };
340                                        let data = data
341                                            .iter()
342                                            .cloned()
343                                            .map(Option::unwrap_or_default)
344                                            .collect();
345                                        Ok(data)
346                                    })
347                                    .transpose()
348                                })
349                                .collect::<DeserializationResult<Vec<Option<_>>>>()?
350                        }
351                        .into_iter()
352                    }
353                };
354                let keypoint_connections = {
355                    if !arrays_by_name.contains_key("keypoint_connections") {
356                        return Err(DeserializationError::missing_struct_field(
357                            Self::arrow_datatype(),
358                            "keypoint_connections",
359                        ))
360                        .with_context("rerun.datatypes.ClassDescription");
361                    }
362                    let arrow_data = &**arrays_by_name["keypoint_connections"];
363                    {
364                        let arrow_data = arrow_data
365                            .as_any()
366                            .downcast_ref::<arrow::array::ListArray>()
367                            .ok_or_else(|| {
368                                let expected = DataType::List(std::sync::Arc::new(Field::new(
369                                    "item",
370                                    <crate::datatypes::KeypointPair>::arrow_datatype(),
371                                    false,
372                                )));
373                                let actual = arrow_data.data_type().clone();
374                                DeserializationError::datatype_mismatch(expected, actual)
375                            })
376                            .with_context(
377                                "rerun.datatypes.ClassDescription#keypoint_connections",
378                            )?;
379                        if arrow_data.is_empty() {
380                            Vec::new()
381                        } else {
382                            let arrow_data_inner = {
383                                let arrow_data_inner = &**arrow_data.values();
384                                crate::datatypes::KeypointPair::from_arrow_opt(arrow_data_inner)
385                                    .with_context(
386                                        "rerun.datatypes.ClassDescription#keypoint_connections",
387                                    )?
388                                    .into_iter()
389                                    .collect::<Vec<_>>()
390                            };
391                            let offsets = arrow_data.offsets();
392                            ZipValidity::new_with_validity(offsets.windows(2), arrow_data.nulls())
393                                .map(|elem| {
394                                    elem.map(|window| {
395                                        let start = window[0] as usize;
396                                        let end = window[1] as usize;
397                                        if arrow_data_inner.len() < end {
398                                            return Err(DeserializationError::offset_slice_oob(
399                                                (start, end),
400                                                arrow_data_inner.len(),
401                                            ));
402                                        }
403
404                                        #[allow(unsafe_code, clippy::undocumented_unsafe_blocks)]
405                                        let data =
406                                            unsafe { arrow_data_inner.get_unchecked(start..end) };
407                                        let data = data
408                                            .iter()
409                                            .cloned()
410                                            .map(Option::unwrap_or_default)
411                                            .collect();
412                                        Ok(data)
413                                    })
414                                    .transpose()
415                                })
416                                .collect::<DeserializationResult<Vec<Option<_>>>>()?
417                        }
418                        .into_iter()
419                    }
420                };
421                ZipValidity::new_with_validity(
422                    ::itertools::izip!(info, keypoint_annotations, keypoint_connections),
423                    arrow_data.nulls(),
424                )
425                .map(|opt| {
426                    opt.map(|(info, keypoint_annotations, keypoint_connections)| {
427                        Ok(Self {
428                            info: info
429                                .ok_or_else(DeserializationError::missing_data)
430                                .with_context("rerun.datatypes.ClassDescription#info")?,
431                            keypoint_annotations: keypoint_annotations
432                                .ok_or_else(DeserializationError::missing_data)
433                                .with_context(
434                                    "rerun.datatypes.ClassDescription#keypoint_annotations",
435                                )?,
436                            keypoint_connections: keypoint_connections
437                                .ok_or_else(DeserializationError::missing_data)
438                                .with_context(
439                                    "rerun.datatypes.ClassDescription#keypoint_connections",
440                                )?,
441                        })
442                    })
443                    .transpose()
444                })
445                .collect::<DeserializationResult<Vec<_>>>()
446                .with_context("rerun.datatypes.ClassDescription")?
447            }
448        })
449    }
450}
451
452impl ::re_byte_size::SizeBytes for ClassDescription {
453    #[inline]
454    fn heap_size_bytes(&self) -> u64 {
455        self.info.heap_size_bytes()
456            + self.keypoint_annotations.heap_size_bytes()
457            + self.keypoint_connections.heap_size_bytes()
458    }
459
460    #[inline]
461    fn is_pod() -> bool {
462        <crate::datatypes::AnnotationInfo>::is_pod()
463            && <Vec<crate::datatypes::AnnotationInfo>>::is_pod()
464            && <Vec<crate::datatypes::KeypointPair>>::is_pod()
465    }
466}