re_types/datatypes/
view_coordinates.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/view_coordinates.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**: How we interpret the coordinate system of an entity/space.
22///
23/// For instance: What is "up"? What does the Z axis mean?
24///
25/// The three coordinates are always ordered as [x, y, z].
26///
27/// For example [Right, Down, Forward] means that the X axis points to the right, the Y axis points
28/// down, and the Z axis points forward.
29///
30/// ⚠ [Rerun does not yet support left-handed coordinate systems](https://github.com/rerun-io/rerun/issues/5032).
31///
32/// The following constants are used to represent the different directions:
33///  * Up = 1
34///  * Down = 2
35///  * Right = 3
36///  * Left = 4
37///  * Forward = 5
38///  * Back = 6
39///
40/// ⚠️ **This type is _unstable_ and may change significantly in a way that the data won't be backwards compatible.**
41#[derive(Clone, Debug, Copy, PartialEq, Eq, bytemuck::Pod, bytemuck::Zeroable)]
42#[repr(transparent)]
43pub struct ViewCoordinates(
44    /// The directions of the [x, y, z] axes.
45    pub [u8; 3usize],
46);
47
48::re_types_core::macros::impl_into_cow!(ViewCoordinates);
49
50impl ::re_types_core::Loggable for ViewCoordinates {
51    #[inline]
52    fn arrow_datatype() -> arrow::datatypes::DataType {
53        #![allow(clippy::wildcard_imports)]
54        use arrow::datatypes::*;
55        DataType::FixedSizeList(
56            std::sync::Arc::new(Field::new("item", DataType::UInt8, false)),
57            3,
58        )
59    }
60
61    fn to_arrow_opt<'a>(
62        data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
63    ) -> SerializationResult<arrow::array::ArrayRef>
64    where
65        Self: Clone + 'a,
66    {
67        #![allow(clippy::wildcard_imports)]
68        #![allow(clippy::manual_is_variant_and)]
69        use ::re_types_core::{arrow_helpers::as_array_ref, Loggable as _, ResultExt as _};
70        use arrow::{array::*, buffer::*, datatypes::*};
71        Ok({
72            let (somes, data0): (Vec<_>, Vec<_>) = data
73                .into_iter()
74                .map(|datum| {
75                    let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
76                    let datum = datum.map(|datum| datum.into_owned().0);
77                    (datum.is_some(), datum)
78                })
79                .unzip();
80            let data0_validity: Option<arrow::buffer::NullBuffer> = {
81                let any_nones = somes.iter().any(|some| !*some);
82                any_nones.then(|| somes.into())
83            };
84            {
85                let data0_inner_data: Vec<_> = data0
86                    .into_iter()
87                    .flat_map(|v| match v {
88                        Some(v) => itertools::Either::Left(v.into_iter()),
89                        None => itertools::Either::Right(
90                            std::iter::repeat(Default::default()).take(3usize),
91                        ),
92                    })
93                    .collect();
94                let data0_inner_validity: Option<arrow::buffer::NullBuffer> =
95                    data0_validity.as_ref().map(|validity| {
96                        validity
97                            .iter()
98                            .map(|b| std::iter::repeat(b).take(3usize))
99                            .flatten()
100                            .collect::<Vec<_>>()
101                            .into()
102                    });
103                as_array_ref(FixedSizeListArray::new(
104                    std::sync::Arc::new(Field::new("item", DataType::UInt8, false)),
105                    3,
106                    as_array_ref(PrimitiveArray::<UInt8Type>::new(
107                        ScalarBuffer::from(data0_inner_data.into_iter().collect::<Vec<_>>()),
108                        data0_inner_validity,
109                    )),
110                    data0_validity,
111                ))
112            }
113        })
114    }
115
116    fn from_arrow_opt(
117        arrow_data: &dyn arrow::array::Array,
118    ) -> DeserializationResult<Vec<Option<Self>>>
119    where
120        Self: Sized,
121    {
122        #![allow(clippy::wildcard_imports)]
123        use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
124        use arrow::{array::*, buffer::*, datatypes::*};
125        Ok({
126            let arrow_data = arrow_data
127                .as_any()
128                .downcast_ref::<arrow::array::FixedSizeListArray>()
129                .ok_or_else(|| {
130                    let expected = Self::arrow_datatype();
131                    let actual = arrow_data.data_type().clone();
132                    DeserializationError::datatype_mismatch(expected, actual)
133                })
134                .with_context("rerun.datatypes.ViewCoordinates#coordinates")?;
135            if arrow_data.is_empty() {
136                Vec::new()
137            } else {
138                let offsets = (0..)
139                    .step_by(3usize)
140                    .zip((3usize..).step_by(3usize).take(arrow_data.len()));
141                let arrow_data_inner = {
142                    let arrow_data_inner = &**arrow_data.values();
143                    arrow_data_inner
144                        .as_any()
145                        .downcast_ref::<UInt8Array>()
146                        .ok_or_else(|| {
147                            let expected = DataType::UInt8;
148                            let actual = arrow_data_inner.data_type().clone();
149                            DeserializationError::datatype_mismatch(expected, actual)
150                        })
151                        .with_context("rerun.datatypes.ViewCoordinates#coordinates")?
152                        .into_iter()
153                        .collect::<Vec<_>>()
154                };
155                ZipValidity::new_with_validity(offsets, arrow_data.nulls())
156                    .map(|elem| {
157                        elem.map(|(start, end): (usize, usize)| {
158                            debug_assert!(end - start == 3usize);
159                            if arrow_data_inner.len() < end {
160                                return Err(DeserializationError::offset_slice_oob(
161                                    (start, end),
162                                    arrow_data_inner.len(),
163                                ));
164                            }
165
166                            #[allow(unsafe_code, clippy::undocumented_unsafe_blocks)]
167                            let data = unsafe { arrow_data_inner.get_unchecked(start..end) };
168                            let data = data.iter().cloned().map(Option::unwrap_or_default);
169
170                            // NOTE: Unwrapping cannot fail: the length must be correct.
171                            #[allow(clippy::unwrap_used)]
172                            Ok(array_init::from_iter(data).unwrap())
173                        })
174                        .transpose()
175                    })
176                    .collect::<DeserializationResult<Vec<Option<_>>>>()?
177            }
178            .into_iter()
179        }
180        .map(|v| v.ok_or_else(DeserializationError::missing_data))
181        .map(|res| res.map(|v| Some(Self(v))))
182        .collect::<DeserializationResult<Vec<Option<_>>>>()
183        .with_context("rerun.datatypes.ViewCoordinates#coordinates")
184        .with_context("rerun.datatypes.ViewCoordinates")?)
185    }
186
187    #[inline]
188    fn from_arrow(arrow_data: &dyn arrow::array::Array) -> DeserializationResult<Vec<Self>>
189    where
190        Self: Sized,
191    {
192        #![allow(clippy::wildcard_imports)]
193        use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
194        use arrow::{array::*, buffer::*, datatypes::*};
195        if let Some(nulls) = arrow_data.nulls() {
196            if nulls.null_count() != 0 {
197                return Err(DeserializationError::missing_data());
198            }
199        }
200        Ok({
201            let slice = {
202                let arrow_data = arrow_data
203                    .as_any()
204                    .downcast_ref::<arrow::array::FixedSizeListArray>()
205                    .ok_or_else(|| {
206                        let expected = DataType::FixedSizeList(
207                            std::sync::Arc::new(Field::new("item", DataType::UInt8, false)),
208                            3,
209                        );
210                        let actual = arrow_data.data_type().clone();
211                        DeserializationError::datatype_mismatch(expected, actual)
212                    })
213                    .with_context("rerun.datatypes.ViewCoordinates#coordinates")?;
214                let arrow_data_inner = &**arrow_data.values();
215                bytemuck::cast_slice::<_, [_; 3usize]>(
216                    arrow_data_inner
217                        .as_any()
218                        .downcast_ref::<UInt8Array>()
219                        .ok_or_else(|| {
220                            let expected = DataType::UInt8;
221                            let actual = arrow_data_inner.data_type().clone();
222                            DeserializationError::datatype_mismatch(expected, actual)
223                        })
224                        .with_context("rerun.datatypes.ViewCoordinates#coordinates")?
225                        .values()
226                        .as_ref(),
227                )
228            };
229            {
230                slice.iter().copied().map(Self).collect::<Vec<_>>()
231            }
232        })
233    }
234}
235
236impl From<[u8; 3usize]> for ViewCoordinates {
237    #[inline]
238    fn from(coordinates: [u8; 3usize]) -> Self {
239        Self(coordinates)
240    }
241}
242
243impl From<ViewCoordinates> for [u8; 3usize] {
244    #[inline]
245    fn from(value: ViewCoordinates) -> Self {
246        value.0
247    }
248}
249
250impl ::re_byte_size::SizeBytes for ViewCoordinates {
251    #[inline]
252    fn heap_size_bytes(&self) -> u64 {
253        self.0.heap_size_bytes()
254    }
255
256    #[inline]
257    fn is_pod() -> bool {
258        <[u8; 3usize]>::is_pod()
259    }
260}