re_types/components/
line_strip2d.rs1#![allow(unused_braces)]
5#![allow(unused_imports)]
6#![allow(unused_parens)]
7#![allow(clippy::allow_attributes)]
8#![allow(clippy::clone_on_copy)]
9#![allow(clippy::cloned_instead_of_copied)]
10#![allow(clippy::map_flatten)]
11#![allow(clippy::needless_question_mark)]
12#![allow(clippy::new_without_default)]
13#![allow(clippy::redundant_closure)]
14#![allow(clippy::too_many_arguments)]
15#![allow(clippy::too_many_lines)]
16#![allow(clippy::wildcard_imports)]
17
18use ::re_types_core::SerializationResult;
19use ::re_types_core::try_serialize_field;
20use ::re_types_core::{ComponentBatch as _, SerializedComponentBatch};
21use ::re_types_core::{ComponentDescriptor, ComponentType};
22use ::re_types_core::{DeserializationError, DeserializationResult};
23
24#[derive(Clone, Debug, Default, PartialEq)]
37pub struct LineStrip2D(pub Vec<crate::datatypes::Vec2D>);
38
39impl ::re_types_core::Component for LineStrip2D {
40 #[inline]
41 fn name() -> ComponentType {
42 "rerun.components.LineStrip2D".into()
43 }
44}
45
46::re_types_core::macros::impl_into_cow!(LineStrip2D);
47
48impl ::re_types_core::Loggable for LineStrip2D {
49 #[inline]
50 fn arrow_datatype() -> arrow::datatypes::DataType {
51 use arrow::datatypes::*;
52 DataType::List(std::sync::Arc::new(Field::new(
53 "item",
54 <crate::datatypes::Vec2D>::arrow_datatype(),
55 false,
56 )))
57 }
58
59 fn to_arrow_opt<'a>(
60 data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
61 ) -> SerializationResult<arrow::array::ArrayRef>
62 where
63 Self: Clone + 'a,
64 {
65 #![allow(clippy::manual_is_variant_and)]
66 use ::re_types_core::{Loggable as _, ResultExt as _, arrow_helpers::as_array_ref};
67 use arrow::{array::*, buffer::*, datatypes::*};
68 Ok({
69 let (somes, data0): (Vec<_>, Vec<_>) = data
70 .into_iter()
71 .map(|datum| {
72 let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
73 let datum = datum.map(|datum| datum.into_owned().0);
74 (datum.is_some(), datum)
75 })
76 .unzip();
77 let data0_validity: Option<arrow::buffer::NullBuffer> = {
78 let any_nones = somes.iter().any(|some| !*some);
79 any_nones.then(|| somes.into())
80 };
81 {
82 let offsets = arrow::buffer::OffsetBuffer::<i32>::from_lengths(
83 data0
84 .iter()
85 .map(|opt| opt.as_ref().map_or(0, |datum| datum.len())),
86 );
87 let data0_inner_data: Vec<_> = data0.into_iter().flatten().flatten().collect();
88 let data0_inner_validity: Option<arrow::buffer::NullBuffer> = None;
89 as_array_ref(ListArray::try_new(
90 std::sync::Arc::new(Field::new(
91 "item",
92 <crate::datatypes::Vec2D>::arrow_datatype(),
93 false,
94 )),
95 offsets,
96 {
97 let data0_inner_data_inner_data: Vec<_> = data0_inner_data
98 .into_iter()
99 .map(|datum| datum.0)
100 .flatten()
101 .collect();
102 let data0_inner_data_inner_validity: Option<arrow::buffer::NullBuffer> =
103 None;
104 as_array_ref(FixedSizeListArray::new(
105 std::sync::Arc::new(Field::new("item", DataType::Float32, false)),
106 2,
107 as_array_ref(PrimitiveArray::<Float32Type>::new(
108 ScalarBuffer::from(
109 data0_inner_data_inner_data.into_iter().collect::<Vec<_>>(),
110 ),
111 data0_inner_data_inner_validity,
112 )),
113 data0_inner_validity,
114 ))
115 },
116 data0_validity,
117 )?)
118 }
119 })
120 }
121
122 fn from_arrow_opt(
123 arrow_data: &dyn arrow::array::Array,
124 ) -> DeserializationResult<Vec<Option<Self>>>
125 where
126 Self: Sized,
127 {
128 use ::re_types_core::{Loggable as _, ResultExt as _, arrow_zip_validity::ZipValidity};
129 use arrow::{array::*, buffer::*, datatypes::*};
130 Ok({
131 let arrow_data = arrow_data
132 .as_any()
133 .downcast_ref::<arrow::array::ListArray>()
134 .ok_or_else(|| {
135 let expected = Self::arrow_datatype();
136 let actual = arrow_data.data_type().clone();
137 DeserializationError::datatype_mismatch(expected, actual)
138 })
139 .with_context("rerun.components.LineStrip2D#points")?;
140 if arrow_data.is_empty() {
141 Vec::new()
142 } else {
143 let arrow_data_inner = {
144 let arrow_data_inner = &**arrow_data.values();
145 {
146 let arrow_data_inner = arrow_data_inner
147 .as_any()
148 .downcast_ref::<arrow::array::FixedSizeListArray>()
149 .ok_or_else(|| {
150 let expected = DataType::FixedSizeList(
151 std::sync::Arc::new(Field::new(
152 "item",
153 DataType::Float32,
154 false,
155 )),
156 2,
157 );
158 let actual = arrow_data_inner.data_type().clone();
159 DeserializationError::datatype_mismatch(expected, actual)
160 })
161 .with_context("rerun.components.LineStrip2D#points")?;
162 if arrow_data_inner.is_empty() {
163 Vec::new()
164 } else {
165 let offsets = (0..)
166 .step_by(2usize)
167 .zip((2usize..).step_by(2usize).take(arrow_data_inner.len()));
168 let arrow_data_inner_inner = {
169 let arrow_data_inner_inner = &**arrow_data_inner.values();
170 arrow_data_inner_inner
171 .as_any()
172 .downcast_ref::<Float32Array>()
173 .ok_or_else(|| {
174 let expected = DataType::Float32;
175 let actual = arrow_data_inner_inner.data_type().clone();
176 DeserializationError::datatype_mismatch(expected, actual)
177 })
178 .with_context("rerun.components.LineStrip2D#points")?
179 .into_iter()
180 .collect::<Vec<_>>()
181 };
182 ZipValidity::new_with_validity(offsets, arrow_data_inner.nulls())
183 .map(|elem| {
184 elem.map(|(start, end): (usize, usize)| {
185 debug_assert!(end - start == 2usize);
186 if arrow_data_inner_inner.len() < end {
187 return Err(DeserializationError::offset_slice_oob(
188 (start, end),
189 arrow_data_inner_inner.len(),
190 ));
191 }
192
193 #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
194 let data = unsafe {
195 arrow_data_inner_inner.get_unchecked(start..end)
196 };
197 let data =
198 data.iter().cloned().map(Option::unwrap_or_default);
199
200 #[expect(clippy::unwrap_used)]
202 Ok(array_init::from_iter(data).unwrap())
203 })
204 .transpose()
205 })
206 .map(|res_or_opt| {
207 res_or_opt
208 .map(|res_or_opt| res_or_opt.map(crate::datatypes::Vec2D))
209 })
210 .collect::<DeserializationResult<Vec<Option<_>>>>()?
211 }
212 .into_iter()
213 }
214 .collect::<Vec<_>>()
215 };
216 let offsets = arrow_data.offsets();
217 ZipValidity::new_with_validity(offsets.windows(2), arrow_data.nulls())
218 .map(|elem| {
219 elem.map(|window| {
220 let start = window[0] as usize;
221 let end = window[1] as usize;
222 if arrow_data_inner.len() < end {
223 return Err(DeserializationError::offset_slice_oob(
224 (start, end),
225 arrow_data_inner.len(),
226 ));
227 }
228
229 #[expect(unsafe_code, clippy::undocumented_unsafe_blocks)]
230 let data = unsafe { arrow_data_inner.get_unchecked(start..end) };
231 let data = data
232 .iter()
233 .cloned()
234 .map(Option::unwrap_or_default)
235 .collect();
236 Ok(data)
237 })
238 .transpose()
239 })
240 .collect::<DeserializationResult<Vec<Option<_>>>>()?
241 }
242 .into_iter()
243 }
244 .map(|v| v.ok_or_else(DeserializationError::missing_data))
245 .map(|res| res.map(|v| Some(Self(v))))
246 .collect::<DeserializationResult<Vec<Option<_>>>>()
247 .with_context("rerun.components.LineStrip2D#points")
248 .with_context("rerun.components.LineStrip2D")?)
249 }
250}
251
252impl<I: Into<crate::datatypes::Vec2D>, T: IntoIterator<Item = I>> From<T> for LineStrip2D {
253 fn from(v: T) -> Self {
254 Self(v.into_iter().map(|v| v.into()).collect())
255 }
256}
257
258impl ::re_byte_size::SizeBytes for LineStrip2D {
259 #[inline]
260 fn heap_size_bytes(&self) -> u64 {
261 self.0.heap_size_bytes()
262 }
263
264 #[inline]
265 fn is_pod() -> bool {
266 <Vec<crate::datatypes::Vec2D>>::is_pod()
267 }
268}