re_types/archetypes/
tensor.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, PartialEq, Default)]
57pub struct Tensor {
58 pub data: Option<SerializedComponentBatch>,
60
61 pub value_range: Option<SerializedComponentBatch>,
73}
74
75impl Tensor {
76 #[inline]
80 pub fn descriptor_data() -> ComponentDescriptor {
81 ComponentDescriptor {
82 archetype: Some("rerun.archetypes.Tensor".into()),
83 component: "Tensor:data".into(),
84 component_type: Some("rerun.components.TensorData".into()),
85 }
86 }
87
88 #[inline]
92 pub fn descriptor_value_range() -> ComponentDescriptor {
93 ComponentDescriptor {
94 archetype: Some("rerun.archetypes.Tensor".into()),
95 component: "Tensor:value_range".into(),
96 component_type: Some("rerun.components.ValueRange".into()),
97 }
98 }
99}
100
101static REQUIRED_COMPONENTS: std::sync::LazyLock<[ComponentDescriptor; 1usize]> =
102 std::sync::LazyLock::new(|| [Tensor::descriptor_data()]);
103
104static RECOMMENDED_COMPONENTS: std::sync::LazyLock<[ComponentDescriptor; 0usize]> =
105 std::sync::LazyLock::new(|| []);
106
107static OPTIONAL_COMPONENTS: std::sync::LazyLock<[ComponentDescriptor; 1usize]> =
108 std::sync::LazyLock::new(|| [Tensor::descriptor_value_range()]);
109
110static ALL_COMPONENTS: std::sync::LazyLock<[ComponentDescriptor; 2usize]> =
111 std::sync::LazyLock::new(|| [Tensor::descriptor_data(), Tensor::descriptor_value_range()]);
112
113impl Tensor {
114 pub const NUM_COMPONENTS: usize = 2usize;
116}
117
118impl ::re_types_core::Archetype for Tensor {
119 #[inline]
120 fn name() -> ::re_types_core::ArchetypeName {
121 "rerun.archetypes.Tensor".into()
122 }
123
124 #[inline]
125 fn display_name() -> &'static str {
126 "Tensor"
127 }
128
129 #[inline]
130 fn required_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
131 REQUIRED_COMPONENTS.as_slice().into()
132 }
133
134 #[inline]
135 fn recommended_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
136 RECOMMENDED_COMPONENTS.as_slice().into()
137 }
138
139 #[inline]
140 fn optional_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
141 OPTIONAL_COMPONENTS.as_slice().into()
142 }
143
144 #[inline]
145 fn all_components() -> ::std::borrow::Cow<'static, [ComponentDescriptor]> {
146 ALL_COMPONENTS.as_slice().into()
147 }
148
149 #[inline]
150 fn from_arrow_components(
151 arrow_data: impl IntoIterator<Item = (ComponentDescriptor, arrow::array::ArrayRef)>,
152 ) -> DeserializationResult<Self> {
153 re_tracing::profile_function!();
154 use ::re_types_core::{Loggable as _, ResultExt as _};
155 let arrays_by_descr: ::nohash_hasher::IntMap<_, _> = arrow_data.into_iter().collect();
156 let data = arrays_by_descr
157 .get(&Self::descriptor_data())
158 .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_data()));
159 let value_range = arrays_by_descr
160 .get(&Self::descriptor_value_range())
161 .map(|array| {
162 SerializedComponentBatch::new(array.clone(), Self::descriptor_value_range())
163 });
164 Ok(Self { data, value_range })
165 }
166}
167
168impl ::re_types_core::AsComponents for Tensor {
169 #[inline]
170 fn as_serialized_batches(&self) -> Vec<SerializedComponentBatch> {
171 use ::re_types_core::Archetype as _;
172 [self.data.clone(), self.value_range.clone()]
173 .into_iter()
174 .flatten()
175 .collect()
176 }
177}
178
179impl ::re_types_core::ArchetypeReflectionMarker for Tensor {}
180
181impl Tensor {
182 #[inline]
184 pub fn new(data: impl Into<crate::components::TensorData>) -> Self {
185 Self {
186 data: try_serialize_field(Self::descriptor_data(), [data]),
187 value_range: None,
188 }
189 }
190
191 #[inline]
193 pub fn update_fields() -> Self {
194 Self::default()
195 }
196
197 #[inline]
199 pub fn clear_fields() -> Self {
200 use ::re_types_core::Loggable as _;
201 Self {
202 data: Some(SerializedComponentBatch::new(
203 crate::components::TensorData::arrow_empty(),
204 Self::descriptor_data(),
205 )),
206 value_range: Some(SerializedComponentBatch::new(
207 crate::components::ValueRange::arrow_empty(),
208 Self::descriptor_value_range(),
209 )),
210 }
211 }
212
213 #[inline]
224 pub fn columns<I>(
225 self,
226 _lengths: I,
227 ) -> SerializationResult<impl Iterator<Item = ::re_types_core::SerializedComponentColumn>>
228 where
229 I: IntoIterator<Item = usize> + Clone,
230 {
231 let columns = [
232 self.data
233 .map(|data| data.partitioned(_lengths.clone()))
234 .transpose()?,
235 self.value_range
236 .map(|value_range| value_range.partitioned(_lengths.clone()))
237 .transpose()?,
238 ];
239 Ok(columns.into_iter().flatten())
240 }
241
242 #[inline]
247 pub fn columns_of_unit_batches(
248 self,
249 ) -> SerializationResult<impl Iterator<Item = ::re_types_core::SerializedComponentColumn>> {
250 let len_data = self.data.as_ref().map(|b| b.array.len());
251 let len_value_range = self.value_range.as_ref().map(|b| b.array.len());
252 let len = None.or(len_data).or(len_value_range).unwrap_or(0);
253 self.columns(std::iter::repeat_n(1, len))
254 }
255
256 #[inline]
258 pub fn with_data(mut self, data: impl Into<crate::components::TensorData>) -> Self {
259 self.data = try_serialize_field(Self::descriptor_data(), [data]);
260 self
261 }
262
263 #[inline]
268 pub fn with_many_data(
269 mut self,
270 data: impl IntoIterator<Item = impl Into<crate::components::TensorData>>,
271 ) -> Self {
272 self.data = try_serialize_field(Self::descriptor_data(), data);
273 self
274 }
275
276 #[inline]
288 pub fn with_value_range(
289 mut self,
290 value_range: impl Into<crate::components::ValueRange>,
291 ) -> Self {
292 self.value_range = try_serialize_field(Self::descriptor_value_range(), [value_range]);
293 self
294 }
295
296 #[inline]
301 pub fn with_many_value_range(
302 mut self,
303 value_range: impl IntoIterator<Item = impl Into<crate::components::ValueRange>>,
304 ) -> Self {
305 self.value_range = try_serialize_field(Self::descriptor_value_range(), value_range);
306 self
307 }
308}
309
310impl ::re_byte_size::SizeBytes for Tensor {
311 #[inline]
312 fn heap_size_bytes(&self) -> u64 {
313 self.data.heap_size_bytes() + self.value_range.heap_size_bytes()
314 }
315}