From 19b3032bd8f34565798e2dbefd45e71fb29b4dfd Mon Sep 17 00:00:00 2001 From: Clement Rey Date: Fri, 10 Jan 2025 16:24:31 +0100 Subject: [PATCH] eager LineStrips2D --- .../rerun/archetypes/line_strips2d.fbs | 1 + .../re_types/src/archetypes/line_strips2d.rs | 266 +++++++----------- .../re_types/tests/types/line_strips2d.rs | 57 ++-- 3 files changed, 138 insertions(+), 186 deletions(-) diff --git a/crates/store/re_types/definitions/rerun/archetypes/line_strips2d.fbs b/crates/store/re_types/definitions/rerun/archetypes/line_strips2d.fbs index 1884710c1815..900826114edb 100644 --- a/crates/store/re_types/definitions/rerun/archetypes/line_strips2d.fbs +++ b/crates/store/re_types/definitions/rerun/archetypes/line_strips2d.fbs @@ -9,6 +9,7 @@ namespace rerun.archetypes; /// \example archetypes/line_strips2d_batch image="https://static.rerun.io/line_strip2d_batch/c6f4062bcf510462d298a5dfe9fdbe87c754acee/1200w.png" /// \example archetypes/line_strips2d_ui_radius title="Lines with scene & UI radius each" image="https://static.rerun.io/line_strip2d_ui_radius/d3d7d5bd36278564ee37e2ed6932963ec16f5131/1200w.png table LineStrips2D ( + "attr.rust.archetype_eager": "", "attr.rust.derive": "PartialEq", "attr.docs.category": "Spatial 2D", "attr.docs.view_types": "Spatial2DView, Spatial3DView: if logged under a projection" diff --git a/crates/store/re_types/src/archetypes/line_strips2d.rs b/crates/store/re_types/src/archetypes/line_strips2d.rs index 868167d34697..daef1b89ef07 100644 --- a/crates/store/re_types/src/archetypes/line_strips2d.rs +++ b/crates/store/re_types/src/archetypes/line_strips2d.rs @@ -85,35 +85,35 @@ use ::re_types_core::{DeserializationError, DeserializationResult}; /// Ok(()) /// } /// ``` -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Default)] pub struct LineStrips2D { /// All the actual 2D line strips that make up the batch. - pub strips: Vec, + pub strips: Option, /// Optional radii for the line strips. - pub radii: Option>, + pub radii: Option, /// Optional colors for the line strips. - pub colors: Option>, + pub colors: Option, /// Optional text labels for the line strips. /// /// If there's a single label present, it will be placed at the center of the entity. /// Otherwise, each instance will have its own label. - pub labels: Option>, + pub labels: Option, /// Optional choice of whether the text labels should be shown by default. - pub show_labels: Option, + pub show_labels: Option, /// An optional floating point value that specifies the 2D drawing order of each line strip. /// /// Objects with higher values are drawn on top of those with lower values. - pub draw_order: Option, + pub draw_order: Option, /// Optional [`components::ClassId`][crate::components::ClassId]s for the lines. /// /// The [`components::ClassId`][crate::components::ClassId] provides colors and labels if not specified explicitly. - pub class_ids: Option>, + pub class_ids: Option, } impl LineStrips2D { @@ -288,85 +288,33 @@ impl ::re_types_core::Archetype for LineStrips2D { re_tracing::profile_function!(); use ::re_types_core::{Loggable as _, ResultExt as _}; let arrays_by_descr: ::nohash_hasher::IntMap<_, _> = arrow_data.into_iter().collect(); - let strips = { - let array = arrays_by_descr - .get(&Self::descriptor_strips()) - .ok_or_else(DeserializationError::missing_data) - .with_context("rerun.archetypes.LineStrips2D#strips")?; - ::from_arrow_opt(&**array) - .with_context("rerun.archetypes.LineStrips2D#strips")? - .into_iter() - .map(|v| v.ok_or_else(DeserializationError::missing_data)) - .collect::>>() - .with_context("rerun.archetypes.LineStrips2D#strips")? - }; - let radii = if let Some(array) = arrays_by_descr.get(&Self::descriptor_radii()) { - Some({ - ::from_arrow_opt(&**array) - .with_context("rerun.archetypes.LineStrips2D#radii")? - .into_iter() - .map(|v| v.ok_or_else(DeserializationError::missing_data)) - .collect::>>() - .with_context("rerun.archetypes.LineStrips2D#radii")? - }) - } else { - None - }; - let colors = if let Some(array) = arrays_by_descr.get(&Self::descriptor_colors()) { - Some({ - ::from_arrow_opt(&**array) - .with_context("rerun.archetypes.LineStrips2D#colors")? - .into_iter() - .map(|v| v.ok_or_else(DeserializationError::missing_data)) - .collect::>>() - .with_context("rerun.archetypes.LineStrips2D#colors")? - }) - } else { - None - }; - let labels = if let Some(array) = arrays_by_descr.get(&Self::descriptor_labels()) { - Some({ - ::from_arrow_opt(&**array) - .with_context("rerun.archetypes.LineStrips2D#labels")? - .into_iter() - .map(|v| v.ok_or_else(DeserializationError::missing_data)) - .collect::>>() - .with_context("rerun.archetypes.LineStrips2D#labels")? - }) - } else { - None - }; - let show_labels = if let Some(array) = arrays_by_descr.get(&Self::descriptor_show_labels()) - { - ::from_arrow_opt(&**array) - .with_context("rerun.archetypes.LineStrips2D#show_labels")? - .into_iter() - .next() - .flatten() - } else { - None - }; - let draw_order = if let Some(array) = arrays_by_descr.get(&Self::descriptor_draw_order()) { - ::from_arrow_opt(&**array) - .with_context("rerun.archetypes.LineStrips2D#draw_order")? - .into_iter() - .next() - .flatten() - } else { - None - }; - let class_ids = if let Some(array) = arrays_by_descr.get(&Self::descriptor_class_ids()) { - Some({ - ::from_arrow_opt(&**array) - .with_context("rerun.archetypes.LineStrips2D#class_ids")? - .into_iter() - .map(|v| v.ok_or_else(DeserializationError::missing_data)) - .collect::>>() - .with_context("rerun.archetypes.LineStrips2D#class_ids")? - }) - } else { - None - }; + let strips = arrays_by_descr + .get(&Self::descriptor_strips()) + .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_strips())); + let radii = arrays_by_descr + .get(&Self::descriptor_radii()) + .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_radii())); + let colors = arrays_by_descr + .get(&Self::descriptor_colors()) + .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_colors())); + let labels = arrays_by_descr + .get(&Self::descriptor_labels()) + .map(|array| SerializedComponentBatch::new(array.clone(), Self::descriptor_labels())); + let show_labels = arrays_by_descr + .get(&Self::descriptor_show_labels()) + .map(|array| { + SerializedComponentBatch::new(array.clone(), Self::descriptor_show_labels()) + }); + let draw_order = arrays_by_descr + .get(&Self::descriptor_draw_order()) + .map(|array| { + SerializedComponentBatch::new(array.clone(), Self::descriptor_draw_order()) + }); + let class_ids = arrays_by_descr + .get(&Self::descriptor_class_ids()) + .map(|array| { + SerializedComponentBatch::new(array.clone(), Self::descriptor_class_ids()) + }); Ok(Self { strips, radii, @@ -380,65 +328,18 @@ impl ::re_types_core::Archetype for LineStrips2D { } impl ::re_types_core::AsComponents for LineStrips2D { - fn as_component_batches(&self) -> Vec> { - re_tracing::profile_function!(); + #[inline] + fn as_serialized_batches(&self) -> Vec { use ::re_types_core::Archetype as _; [ - Some(Self::indicator()), - (Some(&self.strips as &dyn ComponentBatch)).map(|batch| { - ::re_types_core::ComponentBatchCowWithDescriptor { - batch: batch.into(), - descriptor_override: Some(Self::descriptor_strips()), - } - }), - (self - .radii - .as_ref() - .map(|comp_batch| (comp_batch as &dyn ComponentBatch))) - .map(|batch| ::re_types_core::ComponentBatchCowWithDescriptor { - batch: batch.into(), - descriptor_override: Some(Self::descriptor_radii()), - }), - (self - .colors - .as_ref() - .map(|comp_batch| (comp_batch as &dyn ComponentBatch))) - .map(|batch| ::re_types_core::ComponentBatchCowWithDescriptor { - batch: batch.into(), - descriptor_override: Some(Self::descriptor_colors()), - }), - (self - .labels - .as_ref() - .map(|comp_batch| (comp_batch as &dyn ComponentBatch))) - .map(|batch| ::re_types_core::ComponentBatchCowWithDescriptor { - batch: batch.into(), - descriptor_override: Some(Self::descriptor_labels()), - }), - (self - .show_labels - .as_ref() - .map(|comp| (comp as &dyn ComponentBatch))) - .map(|batch| ::re_types_core::ComponentBatchCowWithDescriptor { - batch: batch.into(), - descriptor_override: Some(Self::descriptor_show_labels()), - }), - (self - .draw_order - .as_ref() - .map(|comp| (comp as &dyn ComponentBatch))) - .map(|batch| ::re_types_core::ComponentBatchCowWithDescriptor { - batch: batch.into(), - descriptor_override: Some(Self::descriptor_draw_order()), - }), - (self - .class_ids - .as_ref() - .map(|comp_batch| (comp_batch as &dyn ComponentBatch))) - .map(|batch| ::re_types_core::ComponentBatchCowWithDescriptor { - batch: batch.into(), - descriptor_override: Some(Self::descriptor_class_ids()), - }), + Self::indicator().serialized(), + self.strips.clone(), + self.radii.clone(), + self.colors.clone(), + self.labels.clone(), + self.show_labels.clone(), + self.draw_order.clone(), + self.class_ids.clone(), ] .into_iter() .flatten() @@ -455,7 +356,7 @@ impl LineStrips2D { strips: impl IntoIterator>, ) -> Self { Self { - strips: strips.into_iter().map(Into::into).collect(), + strips: try_serialize_field(Self::descriptor_strips(), strips), radii: None, colors: None, labels: None, @@ -465,13 +366,65 @@ impl LineStrips2D { } } + /// Update only some specific fields of a `LineStrips2D`. + #[inline] + pub fn update_fields() -> Self { + Self::default() + } + + /// Clear all the fields of a `LineStrips2D`. + #[inline] + pub fn clear_fields() -> Self { + use ::re_types_core::Loggable as _; + Self { + strips: Some(SerializedComponentBatch::new( + crate::components::LineStrip2D::arrow_empty(), + Self::descriptor_strips(), + )), + radii: Some(SerializedComponentBatch::new( + crate::components::Radius::arrow_empty(), + Self::descriptor_radii(), + )), + colors: Some(SerializedComponentBatch::new( + crate::components::Color::arrow_empty(), + Self::descriptor_colors(), + )), + labels: Some(SerializedComponentBatch::new( + crate::components::Text::arrow_empty(), + Self::descriptor_labels(), + )), + show_labels: Some(SerializedComponentBatch::new( + crate::components::ShowLabels::arrow_empty(), + Self::descriptor_show_labels(), + )), + draw_order: Some(SerializedComponentBatch::new( + crate::components::DrawOrder::arrow_empty(), + Self::descriptor_draw_order(), + )), + class_ids: Some(SerializedComponentBatch::new( + crate::components::ClassId::arrow_empty(), + Self::descriptor_class_ids(), + )), + } + } + + /// All the actual 2D line strips that make up the batch. + #[inline] + pub fn with_strips( + mut self, + strips: impl IntoIterator>, + ) -> Self { + self.strips = try_serialize_field(Self::descriptor_strips(), strips); + self + } + /// Optional radii for the line strips. #[inline] pub fn with_radii( mut self, radii: impl IntoIterator>, ) -> Self { - self.radii = Some(radii.into_iter().map(Into::into).collect()); + self.radii = try_serialize_field(Self::descriptor_radii(), radii); self } @@ -481,7 +434,7 @@ impl LineStrips2D { mut self, colors: impl IntoIterator>, ) -> Self { - self.colors = Some(colors.into_iter().map(Into::into).collect()); + self.colors = try_serialize_field(Self::descriptor_colors(), colors); self } @@ -494,7 +447,7 @@ impl LineStrips2D { mut self, labels: impl IntoIterator>, ) -> Self { - self.labels = Some(labels.into_iter().map(Into::into).collect()); + self.labels = try_serialize_field(Self::descriptor_labels(), labels); self } @@ -504,7 +457,7 @@ impl LineStrips2D { mut self, show_labels: impl Into, ) -> Self { - self.show_labels = Some(show_labels.into()); + self.show_labels = try_serialize_field(Self::descriptor_show_labels(), [show_labels]); self } @@ -513,7 +466,7 @@ impl LineStrips2D { /// Objects with higher values are drawn on top of those with lower values. #[inline] pub fn with_draw_order(mut self, draw_order: impl Into) -> Self { - self.draw_order = Some(draw_order.into()); + self.draw_order = try_serialize_field(Self::descriptor_draw_order(), [draw_order]); self } @@ -525,7 +478,7 @@ impl LineStrips2D { mut self, class_ids: impl IntoIterator>, ) -> Self { - self.class_ids = Some(class_ids.into_iter().map(Into::into).collect()); + self.class_ids = try_serialize_field(Self::descriptor_class_ids(), class_ids); self } } @@ -541,15 +494,4 @@ impl ::re_byte_size::SizeBytes for LineStrips2D { + self.draw_order.heap_size_bytes() + self.class_ids.heap_size_bytes() } - - #[inline] - fn is_pod() -> bool { - >::is_pod() - && >>::is_pod() - && >>::is_pod() - && >>::is_pod() - && >::is_pod() - && >::is_pod() - && >>::is_pod() - } } diff --git a/crates/store/re_types/tests/types/line_strips2d.rs b/crates/store/re_types/tests/types/line_strips2d.rs index e75bf9353c3e..e784d312970d 100644 --- a/crates/store/re_types/tests/types/line_strips2d.rs +++ b/crates/store/re_types/tests/types/line_strips2d.rs @@ -1,7 +1,5 @@ use re_types::{ - archetypes::LineStrips2D, - components::{ClassId, Color, DrawOrder, LineStrip2D, Radius}, - Archetype as _, AsComponents as _, + archetypes::LineStrips2D, components, Archetype as _, AsComponents as _, ComponentBatch, }; #[test] @@ -9,27 +7,38 @@ fn roundtrip() { let expected = LineStrips2D { #[rustfmt::skip] strips: vec![ - LineStrip2D::from_iter([[0., 0.], [2., 1.], [4., -1.], [6., 0.]]), // - LineStrip2D::from_iter([[0., 3.], [1., 4.], [2., 2.], [3., 4.], [4., 2.], [5., 4.], [6., 3.]]), // - ], - radii: Some(vec![ - Radius::from(42.0), // - Radius::from(43.0), - ]), - colors: Some(vec![ - Color::from_unmultiplied_rgba(0xAA, 0x00, 0x00, 0xCC), // - Color::from_unmultiplied_rgba(0x00, 0xBB, 0x00, 0xDD), - ]), - labels: Some(vec![ - "hello".into(), // - "friend".into(), // - ]), - draw_order: Some(DrawOrder(300.0.into())), - class_ids: Some(vec![ - ClassId::from(126), // - ClassId::from(127), // - ]), - show_labels: Some(false.into()), + components::LineStrip2D::from_iter([[0., 0.], [2., 1.], [4., -1.], [6., 0.]]), // + components::LineStrip2D::from_iter([[0., 3.], [1., 4.], [2., 2.], [3., 4.], [4., 2.], [5., 4.], [6., 3.]]), // + ] + .serialized() + .map(|batch| batch.with_descriptor_override(LineStrips2D::descriptor_strips())), + radii: vec![ + components::Radius::from(42.0), // + components::Radius::from(43.0), + ] + .serialized() + .map(|batch| batch.with_descriptor_override(LineStrips2D::descriptor_radii())), + colors: vec![ + components::Color::from_unmultiplied_rgba(0xAA, 0x00, 0x00, 0xCC), // + components::Color::from_unmultiplied_rgba(0x00, 0xBB, 0x00, 0xDD), + ] + .serialized() + .map(|batch| batch.with_descriptor_override(LineStrips2D::descriptor_colors())), + labels: (vec!["hello".into(), "friend".into()] as Vec) + .serialized() + .map(|batch| batch.with_descriptor_override(LineStrips2D::descriptor_labels())), + draw_order: vec![components::DrawOrder(300.0.into())] + .serialized() + .map(|batch| batch.with_descriptor_override(LineStrips2D::descriptor_draw_order())), + class_ids: vec![ + components::ClassId::from(126), // + components::ClassId::from(127), // + ] + .serialized() + .map(|batch| batch.with_descriptor_override(LineStrips2D::descriptor_class_ids())), + show_labels: components::ShowLabels(false.into()) + .serialized() + .map(|batch| batch.with_descriptor_override(LineStrips2D::descriptor_show_labels())), }; #[rustfmt::skip]