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