Skip to content

Commit

Permalink
Introduce marker size component (#5057)
Browse files Browse the repository at this point in the history
### What

* Needed for #5042

Allows to configure size again for points wiht the new PointSeries
archetype.

Makes size of plot line and marker independent which is kinda neat :)
<img width="952" alt="image"
src="https://github.com/rerun-io/rerun/assets/1220815/cd24a449-e839-4db2-abf5-782e8dcf96af">
(Color is *not* independent, we'll want to solve this differently in the
future in accordance to the model we're going with, but up for
discussion obviously)
  • Loading branch information
Wumpf authored Feb 6, 2024
1 parent 524bef3 commit 91a41b4
Show file tree
Hide file tree
Showing 29 changed files with 565 additions and 58 deletions.
52 changes: 51 additions & 1 deletion crates/re_data_ui/src/editors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use re_data_store::{DataStore, LatestAtQuery};
use re_log_types::EntityPath;
use re_query::ComponentWithInstances;
use re_types::{
components::{Color, MarkerShape, Name, Radius, ScalarScattering, StrokeWidth, Text},
components::{
Color, MarkerShape, MarkerSize, Name, Radius, ScalarScattering, StrokeWidth, Text,
},
Component, Loggable,
};
use re_viewer_context::{UiVerbosity, ViewerContext};
Expand Down Expand Up @@ -366,6 +368,53 @@ fn default_stroke_width(

// ----

#[allow(clippy::too_many_arguments)]
fn edit_marker_size_ui(
ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
_verbosity: UiVerbosity,
query: &LatestAtQuery,
store: &DataStore,
entity_path: &EntityPath,
override_path: &EntityPath,
component: &ComponentWithInstances,
instance_key: &re_types::components::InstanceKey,
) {
let current_marker_size = component
.lookup::<MarkerSize>(instance_key)
.ok()
.unwrap_or_else(|| default_marker_size(ctx, query, store, entity_path));

let current_marker_size = current_marker_size.0;
let mut edit_marker_size = current_marker_size;

let speed = (current_marker_size * 0.01).at_least(0.001);

ui.add(
egui::DragValue::new(&mut edit_marker_size)
.clamp_range(0.0..=f64::INFINITY)
.speed(speed),
);

if edit_marker_size != current_marker_size {
let new_marker_size = MarkerSize::from(edit_marker_size);

ctx.save_blueprint_component(override_path, new_marker_size);
}
}

#[inline]
fn default_marker_size(
_ctx: &ViewerContext<'_>,
_query: &LatestAtQuery,
_store: &DataStore,
_entity_path: &EntityPath,
) -> MarkerSize {
MarkerSize::from(1.0)
}

// ----

fn register_editor<'a, C: Component + Loggable + 'static>(
registry: &mut re_viewer_context::ComponentUiRegistry,
default: fn(&ViewerContext<'_>, &LatestAtQuery, &DataStore, &EntityPath) -> C,
Expand Down Expand Up @@ -396,6 +445,7 @@ fn register_editor<'a, C: Component + Loggable + 'static>(
pub fn register_editors(registry: &mut re_viewer_context::ComponentUiRegistry) {
register_editor::<Color>(registry, default_color, edit_color_ui);
register_editor::<MarkerShape>(registry, default_marker_shape, edit_marker_shape_ui);
register_editor::<MarkerSize>(registry, default_marker_size, edit_marker_size_ui);
register_editor::<Name>(registry, default_name, edit_name_ui);
register_editor::<Radius>(registry, default_radius, edit_radius_ui);
register_editor::<ScalarScattering>(registry, default_scatter, edit_scatter_ui);
Expand Down
28 changes: 14 additions & 14 deletions crates/re_space_view_time_series/src/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl AverageAggregator {
let point = &points[i + j];

acc.value += point.value;
acc.attrs.stroke_width += point.attrs.stroke_width;
acc.attrs.marker_size += point.attrs.marker_size;

ratio += 1.0;
j += 1;
Expand All @@ -48,14 +48,14 @@ impl AverageAggregator {

let w = aggregation_factor_fract;
acc.value += point.value * w;
acc.attrs.stroke_width += (point.attrs.stroke_width as f64 * w) as f32;
acc.attrs.marker_size += (point.attrs.marker_size as f64 * w) as f32;

ratio += aggregation_factor_fract;
j += 1;
}

acc.value /= ratio;
acc.attrs.stroke_width = (acc.attrs.stroke_width as f64 / ratio) as _;
acc.attrs.marker_size = (acc.attrs.marker_size as f64 / ratio) as _;

aggregated.push(acc);

Expand Down Expand Up @@ -124,21 +124,21 @@ impl MinMaxAggregator {
match self {
MinMaxAggregator::MinMax | MinMaxAggregator::MinMaxAverage => {
acc_min.value = f64::min(acc_min.value, point.value);
acc_min.attrs.stroke_width =
f32::min(acc_min.attrs.stroke_width, point.attrs.stroke_width);
acc_min.attrs.marker_size =
f32::min(acc_min.attrs.marker_size, point.attrs.marker_size);
acc_max.value = f64::max(acc_max.value, point.value);
acc_max.attrs.stroke_width =
f32::max(acc_max.attrs.stroke_width, point.attrs.stroke_width);
acc_max.attrs.marker_size =
f32::max(acc_max.attrs.marker_size, point.attrs.marker_size);
}
MinMaxAggregator::Min => {
acc_min.value = f64::min(acc_min.value, point.value);
acc_min.attrs.stroke_width =
f32::min(acc_min.attrs.stroke_width, point.attrs.stroke_width);
acc_min.attrs.marker_size =
f32::min(acc_min.attrs.marker_size, point.attrs.marker_size);
}
MinMaxAggregator::Max => {
acc_max.value = f64::max(acc_max.value, point.value);
acc_max.attrs.stroke_width =
f32::max(acc_max.attrs.stroke_width, point.attrs.stroke_width);
acc_max.attrs.marker_size =
f32::max(acc_max.attrs.marker_size, point.attrs.marker_size);
}
}

Expand All @@ -157,8 +157,8 @@ impl MinMaxAggregator {
// Don't average a single point with itself.
if j > 1 {
acc_min.value = (acc_min.value + acc_max.value) * 0.5;
acc_min.attrs.stroke_width =
(acc_min.attrs.stroke_width + acc_max.attrs.stroke_width) * 0.5;
acc_min.attrs.marker_size =
(acc_min.attrs.marker_size + acc_max.attrs.marker_size) * 0.5;
}
aggregated.push(acc_min);
}
Expand Down Expand Up @@ -195,7 +195,7 @@ fn are_aggregatable(point1: &PlotPoint, point2: &PlotPoint, window_size: usize)
let PlotPointAttrs {
label,
color,
stroke_width: _,
marker_size: _,
kind,
} = attrs;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl LegacyTimeSeriesSystem {
attrs: PlotPointAttrs {
label: override_label.clone(), // default value is simply None
color: annotation_info.color(override_color, default_color),
stroke_width: override_radius.unwrap_or(DEFAULT_RADIUS),
marker_size: override_radius.unwrap_or(DEFAULT_RADIUS),
kind: if override_scattered.unwrap_or(false) {
PlotSeriesKind::Scatter(ScatterAttrs::default())
} else {
Expand Down Expand Up @@ -169,7 +169,7 @@ impl LegacyTimeSeriesSystem {
attrs: PlotPointAttrs {
label: None,
color: egui::Color32::BLACK,
stroke_width: 0.0,
marker_size: 0.0,
kind: PlotSeriesKind::Clear,
},
});
Expand Down Expand Up @@ -221,7 +221,7 @@ impl LegacyTimeSeriesSystem {

if override_radius.is_none() {
if let Some(radius) = radius.map(|r| r.0) {
point.attrs.stroke_width = radius;
point.attrs.marker_size = radius;
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/re_space_view_time_series/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(crate) fn plot_id(space_view_id: re_viewer_context::SpaceViewId) -> egui::Id
pub struct PlotPointAttrs {
pub label: Option<Utf8>,
pub color: egui::Color32,
pub stroke_width: f32,
pub marker_size: f32,
pub kind: PlotSeriesKind,
}

Expand All @@ -47,12 +47,12 @@ impl PartialEq for PlotPointAttrs {
let Self {
label,
color,
stroke_width: radius,
marker_size,
kind,
} = self;
label.eq(&rhs.label)
&& color.eq(&rhs.color)
&& radius.total_cmp(&rhs.stroke_width).is_eq()
&& marker_size.total_cmp(&rhs.marker_size).is_eq()
&& kind.eq(&rhs.kind)
}
}
Expand Down
15 changes: 7 additions & 8 deletions crates/re_space_view_time_series/src/line_visualizer_system.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use re_query_cache::{MaybeCachedComponentData, QueryError};
use re_types::archetypes;
use re_types::components::{MarkerShape, Name, StrokeWidth};
use re_types::{
archetypes::SeriesLine,
components::{Color, Scalar},
components::{Color, MarkerShape, MarkerSize, Name, Scalar, StrokeWidth},
Archetype as _, ComponentNameSet, Loggable,
};
use re_viewer_context::{
Expand Down Expand Up @@ -122,7 +121,7 @@ impl SeriesLineSystem {
attrs: PlotPointAttrs {
label: None,
color: annotation_info.color(override_color, default_color),
stroke_width: override_stroke_width.unwrap_or(DEFAULT_STROKE_WIDTH),
marker_size: override_stroke_width.unwrap_or(DEFAULT_STROKE_WIDTH),
kind: PlotSeriesKind::Continuous,
},
};
Expand All @@ -142,15 +141,15 @@ impl SeriesLineSystem {

// TODO(jleibs): need to do a "joined" archetype query
query_caches
.query_archetype_pov1_comp3::<archetypes::Scalar, Scalar, Color, StrokeWidth, MarkerShape, _>(
.query_archetype_pov1_comp4::<archetypes::Scalar, Scalar, Color, StrokeWidth, MarkerSize, MarkerShape, _>(
ctx.app_options.experimental_primary_caching_range,
store,
&query.clone().into(),
&data_result.entity_path,
// The `Scalar` archetype queries for `MarkerShape` in the point visualizer,
// The `Scalar` archetype queries for `MarkerShape` & `MarkerSize` in the point visualizer,
// and so it must do so here also.
// See https://github.com/rerun-io/rerun/pull/5029
|((time, _row_id), _, scalars, colors, stroke_widths, _)| {
|((time, _row_id), _, scalars, colors, stroke_widths, _, _)| {
let Some(time) = time else {
return;
}; // scalars cannot be timeless
Expand All @@ -163,7 +162,7 @@ impl SeriesLineSystem {
attrs: PlotPointAttrs {
label: None,
color: egui::Color32::BLACK,
stroke_width: 0.0,
marker_size: 0.0,
kind: PlotSeriesKind::Clear,
},
});
Expand Down Expand Up @@ -200,7 +199,7 @@ impl SeriesLineSystem {

if override_stroke_width.is_none() {
if let Some(stroke_width) = stroke_width.map(|r| r.0) {
point.attrs.stroke_width = stroke_width;
point.attrs.marker_size = stroke_width;
}
}

Expand Down
33 changes: 15 additions & 18 deletions crates/re_space_view_time_series/src/point_visualizer_system.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use re_query_cache::{MaybeCachedComponentData, QueryError};
use re_types::archetypes;
use re_types::components::{MarkerShape, Name, StrokeWidth};
use re_types::{
archetypes::SeriesPoint,
components::{Color, Scalar},
archetypes::{self, SeriesPoint},
components::{Color, MarkerShape, MarkerSize, Name, Scalar, StrokeWidth},
Archetype as _, ComponentNameSet, Loggable,
};
use re_viewer_context::{
Expand Down Expand Up @@ -33,7 +31,7 @@ impl IdentifiedViewSystem for SeriesPointSystem {

// We use a larger default stroke width for scatter plots so the marker is
// visible.
const DEFAULT_STROKE_WIDTH: f32 = 3.0;
const DEFAULT_MARKER_SIZE: f32 = 3.0;

impl VisualizerSystem for SeriesPointSystem {
fn visualizer_query_info(&self) -> VisualizerQueryInfo {
Expand Down Expand Up @@ -84,8 +82,8 @@ impl VisualizerSystem for SeriesPointSystem {
) -> Option<re_log_types::DataCell> {
if *component == Color::name() {
Some([initial_override_color(entity_path)].into())
} else if *component == StrokeWidth::name() {
Some([StrokeWidth(DEFAULT_STROKE_WIDTH)].into())
} else if *component == MarkerSize::name() {
Some([MarkerSize(DEFAULT_MARKER_SIZE)].into())
} else {
None
}
Expand Down Expand Up @@ -115,8 +113,7 @@ impl SeriesPointSystem {

let override_color = lookup_override::<Color>(data_result, ctx).map(|c| c.to_array());
let override_series_name = lookup_override::<Name>(data_result, ctx).map(|t| t.0);
let override_stroke_width =
lookup_override::<StrokeWidth>(data_result, ctx).map(|r| r.0);
let override_marker_size = lookup_override::<MarkerSize>(data_result, ctx).map(|r| r.0);
let override_marker = lookup_override::<MarkerShape>(data_result, ctx);

// All the default values for a `PlotPoint`, accounting for both overrides and default
Expand All @@ -127,7 +124,7 @@ impl SeriesPointSystem {
attrs: PlotPointAttrs {
label: None,
color: annotation_info.color(override_color, default_color),
stroke_width: override_stroke_width.unwrap_or(DEFAULT_STROKE_WIDTH),
marker_size: override_marker_size.unwrap_or(DEFAULT_MARKER_SIZE),
kind: PlotSeriesKind::Scatter(ScatterAttrs {
marker: override_marker.unwrap_or_default(),
}),
Expand All @@ -153,12 +150,12 @@ impl SeriesPointSystem {
// and so it must do so here also.
// See https://github.com/rerun-io/rerun/pull/5029
query_caches
.query_archetype_pov1_comp3::<archetypes::Scalar, Scalar, Color, StrokeWidth, MarkerShape, _>(
.query_archetype_pov1_comp4::<archetypes::Scalar, Scalar, Color, StrokeWidth, MarkerSize, MarkerShape, _>(
ctx.app_options.experimental_primary_caching_range,
store,
&query.clone().into(),
&data_result.entity_path,
|((time, _row_id), _, scalars, colors, stroke_widths, markers)| {
|((time, _row_id), _, scalars, colors, _, marker_sizes, markers)| {
let Some(time) = time else {
return;
}; // scalars cannot be timeless
Expand All @@ -171,17 +168,17 @@ impl SeriesPointSystem {
attrs: PlotPointAttrs {
label: None,
color: egui::Color32::BLACK,
stroke_width: 0.0,
marker_size: 0.0,
kind: PlotSeriesKind::Clear,
},
});
return;
}

for (scalar, color, stroke_width, marker) in itertools::izip!(
for (scalar, color, marker_size, marker) in itertools::izip!(
scalars.iter(),
MaybeCachedComponentData::iter_or_repeat_opt(&colors, scalars.len()),
MaybeCachedComponentData::iter_or_repeat_opt(&stroke_widths, scalars.len()),
MaybeCachedComponentData::iter_or_repeat_opt(&marker_sizes, scalars.len()),
MaybeCachedComponentData::iter_or_repeat_opt(&markers, scalars.len()),
) {
let mut point = PlotPoint {
Expand All @@ -207,9 +204,9 @@ impl SeriesPointSystem {
}
}

if override_stroke_width.is_none() {
if let Some(stroke_width) = stroke_width.map(|r| r.0) {
point.attrs.stroke_width = stroke_width;
if override_marker_size.is_none() {
if let Some(marker_size) = marker_size.map(|r| r.0) {
point.attrs.marker_size = marker_size;
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/re_space_view_time_series/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn points_to_series(
all_series.push(PlotSeries {
label: series_label,
color: points[0].attrs.color,
width: 2.0 * points[0].attrs.stroke_width,
width: 2.0 * points[0].attrs.marker_size,
kind,
points: vec![(points[0].time, points[0].value)],
entity_path: data_result.entity_path.clone(),
Expand Down Expand Up @@ -208,7 +208,7 @@ fn add_series_runs(
let mut series: PlotSeries = PlotSeries {
label: series_label.clone(),
color: attrs.color,
width: 2.0 * attrs.stroke_width,
width: 2.0 * attrs.marker_size,
points: Vec::with_capacity(num_points),
kind: attrs.kind,
entity_path: entity_path.clone(),
Expand All @@ -232,7 +232,7 @@ fn add_series_runs(
PlotSeries {
label: series_label.clone(),
color: attrs.color,
width: 2.0 * attrs.stroke_width,
width: 2.0 * attrs.marker_size,
kind: attrs.kind,
points: Vec::with_capacity(num_points - i),
entity_path: entity_path.clone(),
Expand Down
3 changes: 3 additions & 0 deletions crates/re_types/definitions/rerun/archetypes/series_point.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ table SeriesPoint (
///
/// Used in the legend.
name: rerun.components.Name ("attr.rerun.component_optional", nullable, order: 3000);

/// Size of the marker.
marker_size: rerun.components.MarkerSize ("attr.rerun.component_optional", nullable, order: 4000);
}
1 change: 1 addition & 0 deletions crates/re_types/definitions/rerun/components.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ include "./components/keypoint_id.fbs";
include "./components/line_strip2d.fbs";
include "./components/line_strip3d.fbs";
include "./components/marker_shape.fbs";
include "./components/marker_size.fbs";
include "./components/material.fbs";
include "./components/media_type.fbs";
include "./components/mesh_properties.fbs";
Expand Down
Loading

0 comments on commit 91a41b4

Please sign in to comment.