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