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