re_types/datatypes/
quaternion.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/quaternion.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**: A Quaternion represented by 4 real numbers.
22///
23/// Note: although the x,y,z,w components of the quaternion will be passed through to the
24/// datastore as provided, when used in the Viewer Quaternions will always be normalized.
25#[derive(Clone, Debug, Copy, PartialEq, PartialOrd, bytemuck::Pod, bytemuck::Zeroable)]
26#[repr(C)]
27pub struct Quaternion(pub [f32; 4usize]);
28
29::re_types_core::macros::impl_into_cow!(Quaternion);
30
31impl ::re_types_core::Loggable for Quaternion {
32    #[inline]
33    fn arrow_datatype() -> arrow::datatypes::DataType {
34        #![allow(clippy::wildcard_imports)]
35        use arrow::datatypes::*;
36        DataType::FixedSizeList(
37            std::sync::Arc::new(Field::new("item", DataType::Float32, false)),
38            4,
39        )
40    }
41
42    fn to_arrow_opt<'a>(
43        data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
44    ) -> SerializationResult<arrow::array::ArrayRef>
45    where
46        Self: Clone + 'a,
47    {
48        #![allow(clippy::wildcard_imports)]
49        #![allow(clippy::manual_is_variant_and)]
50        use ::re_types_core::{arrow_helpers::as_array_ref, Loggable as _, ResultExt as _};
51        use arrow::{array::*, buffer::*, datatypes::*};
52        Ok({
53            let (somes, data0): (Vec<_>, Vec<_>) = data
54                .into_iter()
55                .map(|datum| {
56                    let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
57                    let datum = datum.map(|datum| datum.into_owned().0);
58                    (datum.is_some(), datum)
59                })
60                .unzip();
61            let data0_validity: Option<arrow::buffer::NullBuffer> = {
62                let any_nones = somes.iter().any(|some| !*some);
63                any_nones.then(|| somes.into())
64            };
65            {
66                let data0_inner_data: Vec<_> = data0
67                    .into_iter()
68                    .flat_map(|v| match v {
69                        Some(v) => itertools::Either::Left(v.into_iter()),
70                        None => itertools::Either::Right(
71                            std::iter::repeat(Default::default()).take(4usize),
72                        ),
73                    })
74                    .collect();
75                let data0_inner_validity: Option<arrow::buffer::NullBuffer> =
76                    data0_validity.as_ref().map(|validity| {
77                        validity
78                            .iter()
79                            .map(|b| std::iter::repeat(b).take(4usize))
80                            .flatten()
81                            .collect::<Vec<_>>()
82                            .into()
83                    });
84                as_array_ref(FixedSizeListArray::new(
85                    std::sync::Arc::new(Field::new("item", DataType::Float32, false)),
86                    4,
87                    as_array_ref(PrimitiveArray::<Float32Type>::new(
88                        ScalarBuffer::from(data0_inner_data.into_iter().collect::<Vec<_>>()),
89                        data0_inner_validity,
90                    )),
91                    data0_validity,
92                ))
93            }
94        })
95    }
96
97    fn from_arrow_opt(
98        arrow_data: &dyn arrow::array::Array,
99    ) -> DeserializationResult<Vec<Option<Self>>>
100    where
101        Self: Sized,
102    {
103        #![allow(clippy::wildcard_imports)]
104        use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
105        use arrow::{array::*, buffer::*, datatypes::*};
106        Ok({
107            let arrow_data = arrow_data
108                .as_any()
109                .downcast_ref::<arrow::array::FixedSizeListArray>()
110                .ok_or_else(|| {
111                    let expected = Self::arrow_datatype();
112                    let actual = arrow_data.data_type().clone();
113                    DeserializationError::datatype_mismatch(expected, actual)
114                })
115                .with_context("rerun.datatypes.Quaternion#xyzw")?;
116            if arrow_data.is_empty() {
117                Vec::new()
118            } else {
119                let offsets = (0..)
120                    .step_by(4usize)
121                    .zip((4usize..).step_by(4usize).take(arrow_data.len()));
122                let arrow_data_inner = {
123                    let arrow_data_inner = &**arrow_data.values();
124                    arrow_data_inner
125                        .as_any()
126                        .downcast_ref::<Float32Array>()
127                        .ok_or_else(|| {
128                            let expected = DataType::Float32;
129                            let actual = arrow_data_inner.data_type().clone();
130                            DeserializationError::datatype_mismatch(expected, actual)
131                        })
132                        .with_context("rerun.datatypes.Quaternion#xyzw")?
133                        .into_iter()
134                        .collect::<Vec<_>>()
135                };
136                ZipValidity::new_with_validity(offsets, arrow_data.nulls())
137                    .map(|elem| {
138                        elem.map(|(start, end): (usize, usize)| {
139                            debug_assert!(end - start == 4usize);
140                            if arrow_data_inner.len() < end {
141                                return Err(DeserializationError::offset_slice_oob(
142                                    (start, end),
143                                    arrow_data_inner.len(),
144                                ));
145                            }
146
147                            #[allow(unsafe_code, clippy::undocumented_unsafe_blocks)]
148                            let data = unsafe { arrow_data_inner.get_unchecked(start..end) };
149                            let data = data.iter().cloned().map(Option::unwrap_or_default);
150
151                            // NOTE: Unwrapping cannot fail: the length must be correct.
152                            #[allow(clippy::unwrap_used)]
153                            Ok(array_init::from_iter(data).unwrap())
154                        })
155                        .transpose()
156                    })
157                    .collect::<DeserializationResult<Vec<Option<_>>>>()?
158            }
159            .into_iter()
160        }
161        .map(|v| v.ok_or_else(DeserializationError::missing_data))
162        .map(|res| res.map(|v| Some(Self(v))))
163        .collect::<DeserializationResult<Vec<Option<_>>>>()
164        .with_context("rerun.datatypes.Quaternion#xyzw")
165        .with_context("rerun.datatypes.Quaternion")?)
166    }
167
168    #[inline]
169    fn from_arrow(arrow_data: &dyn arrow::array::Array) -> DeserializationResult<Vec<Self>>
170    where
171        Self: Sized,
172    {
173        #![allow(clippy::wildcard_imports)]
174        use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
175        use arrow::{array::*, buffer::*, datatypes::*};
176        if let Some(nulls) = arrow_data.nulls() {
177            if nulls.null_count() != 0 {
178                return Err(DeserializationError::missing_data());
179            }
180        }
181        Ok({
182            let slice = {
183                let arrow_data = arrow_data
184                    .as_any()
185                    .downcast_ref::<arrow::array::FixedSizeListArray>()
186                    .ok_or_else(|| {
187                        let expected = DataType::FixedSizeList(
188                            std::sync::Arc::new(Field::new("item", DataType::Float32, false)),
189                            4,
190                        );
191                        let actual = arrow_data.data_type().clone();
192                        DeserializationError::datatype_mismatch(expected, actual)
193                    })
194                    .with_context("rerun.datatypes.Quaternion#xyzw")?;
195                let arrow_data_inner = &**arrow_data.values();
196                bytemuck::cast_slice::<_, [_; 4usize]>(
197                    arrow_data_inner
198                        .as_any()
199                        .downcast_ref::<Float32Array>()
200                        .ok_or_else(|| {
201                            let expected = DataType::Float32;
202                            let actual = arrow_data_inner.data_type().clone();
203                            DeserializationError::datatype_mismatch(expected, actual)
204                        })
205                        .with_context("rerun.datatypes.Quaternion#xyzw")?
206                        .values()
207                        .as_ref(),
208                )
209            };
210            {
211                slice.iter().copied().map(Self).collect::<Vec<_>>()
212            }
213        })
214    }
215}
216
217impl From<[f32; 4usize]> for Quaternion {
218    #[inline]
219    fn from(xyzw: [f32; 4usize]) -> Self {
220        Self(xyzw)
221    }
222}
223
224impl From<Quaternion> for [f32; 4usize] {
225    #[inline]
226    fn from(value: Quaternion) -> Self {
227        value.0
228    }
229}
230
231impl ::re_byte_size::SizeBytes for Quaternion {
232    #[inline]
233    fn heap_size_bytes(&self) -> u64 {
234        self.0.heap_size_bytes()
235    }
236
237    #[inline]
238    fn is_pod() -> bool {
239        <[f32; 4usize]>::is_pod()
240    }
241}