Skip to content

Commit

Permalink
Separate crate for bar chart space view (#2322)
Browse files Browse the repository at this point in the history
<!--
Open the PR up as a draft until you feel it is ready for a proper
review.

Do not make PR:s from your own `main` branch, as that makes it difficult
for reviewers to add their own fixes.

Add any improvements to the branch as new commits to make it easier for
reviewers to follow the progress. All commits will be squashed to a
single commit once the PR is merged into `main`.

Make sure you mention any issues that this PR closes in the description,
as well as any other related issues.

To get an auto-generated PR description you can put "copilot:summary" or
"copilot:walkthrough" anywhere.
-->

### What

Moves bar chart into its own crate.

Remove some general unused crates.
Add generic `ScenePartCollection` impl for single element scenes.

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)

<!-- This line will get updated when the PR build summary job finishes.
-->
PR Build Summary: https://build.rerun.io/pr/2322

<!-- pr-link-docs:start -->
Docs preview: https://rerun.io/preview/b42f4f0/docs
Examples preview: https://rerun.io/preview/b42f4f0/examples
<!-- pr-link-docs:end -->
  • Loading branch information
Wumpf authored Jun 8, 2023
1 parent 46bd4e3 commit 56437db
Show file tree
Hide file tree
Showing 21 changed files with 263 additions and 181 deletions.
24 changes: 19 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ re_sdk = { path = "crates/re_sdk", version = "0.7.0-alpha.0", default-features =
re_sdk_comms = { path = "crates/re_sdk_comms", version = "0.7.0-alpha.0", default-features = false }
re_smart_channel = { path = "crates/re_smart_channel", version = "0.7.0-alpha.0", default-features = false }
re_space_view = { path = "crates/re_space_view", version = "0.7.0-alpha.0", default-features = false }
re_space_view_bar_chart = { path = "crates/re_space_view_bar_chart", version = "0.7.0-alpha.0", default-features = false }
re_space_view_spatial = { path = "crates/re_space_view_spatial", version = "0.7.0-alpha.0", default-features = false }
re_space_view_text = { path = "crates/re_space_view_text", version = "0.7.0-alpha.0", default-features = false }
re_space_view_text_box = { path = "crates/re_space_view_text_box", version = "0.7.0-alpha.0", default-features = false }
re_space_view_spatial = { path = "crates/re_space_view_spatial", version = "0.7.0-alpha.0", default-features = false }
re_string_interner = { path = "crates/re_string_interner", version = "0.7.0-alpha.0", default-features = false }
re_tensor_ops = { path = "crates/re_tensor_ops", version = "0.7.0-alpha.0", default-features = false }
re_time_panel = { path = "crates/re_time_panel", version = "=0.7.0-alpha.0", default-features = false }
Expand Down
4 changes: 0 additions & 4 deletions crates/re_space_view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ all-features = true

[dependencies]
re_data_store.workspace = true
re_log_types.workspace = true
re_renderer.workspace = true
re_tracing.workspace = true
re_viewer_context.workspace = true

ahash.workspace = true
egui.workspace = true
lazy_static.workspace = true
nohash-hasher.workspace = true
serde = "1.0"
slotmap.workspace = true
Expand Down
31 changes: 31 additions & 0 deletions crates/re_space_view_bar_chart/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
authors.workspace = true
description = "A Space View that shows a single bar chart."
edition.workspace = true
homepage.workspace = true
license.workspace = true
name = "re_space_view_bar_chart"
publish = true
readme = "README.md"
repository.workspace = true
rust-version.workspace = true
version.workspace = true
include = ["../../LICENSE-APACHE", "../../LICENSE-MIT", "**/*.rs", "Cargo.toml"]

[package.metadata.docs.rs]
all-features = true

[dependencies]
re_arrow_store.workspace = true
re_components.workspace = true
re_data_store.workspace = true
re_log_types.workspace = true
re_log.workspace = true
re_renderer.workspace = true
re_space_view.workspace = true
re_tracing.workspace = true
re_ui.workspace = true
re_viewer_context.workspace = true

egui.workspace = true
vec1.workspace = true
11 changes: 11 additions & 0 deletions crates/re_space_view_bar_chart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# re_space_view_bar_chart

Part of the [`rerun`](https://github.com/rerun-io/rerun) family of crates.

[![Latest version](https://img.shields.io/crates/v/re_space_view_bar_chart.svg)](https://crates.io/crates/re_space_view_bar_chart)
[![Documentation](https://docs.rs/re_space_view_bar_chart/badge.svg)](https://docs.rs/re_space_view_bar_chart)
![MIT](https://img.shields.io/badge/license-MIT-blue.svg)
![Apache](https://img.shields.io/badge/license-Apache-blue.svg)

A Space View that shows a single bar chart.

8 changes: 8 additions & 0 deletions crates/re_space_view_bar_chart/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! Rerun bar chart Space View.
//!
//! A Space View that shows a single bar chart.
mod scene_part;
mod space_view_class;

pub use space_view_class::BarChartSpaceView;
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,32 @@ use std::collections::BTreeMap;
use re_arrow_store::LatestAtQuery;
use re_components::Tensor;
use re_data_store::EntityPath;
use re_viewer_context::{SceneQuery, ViewerContext};
use re_log_types::Component as _;
use re_viewer_context::{
ArchetypeDefinition, ScenePart, SceneQuery, SpaceViewClass, SpaceViewHighlights, ViewerContext,
};

use crate::BarChartSpaceView;

/// A bar chart scene, with everything needed to render it.
#[derive(Default)]
pub struct SceneBarChart {
pub charts: BTreeMap<EntityPath, Tensor>,
}

impl SceneBarChart {
pub(crate) fn load(&mut self, ctx: &mut ViewerContext<'_>, query: &SceneQuery<'_>) {
re_tracing::profile_function!();

self.load_tensors(ctx, query);
impl ScenePart<BarChartSpaceView> for SceneBarChart {
fn archetype(&self) -> ArchetypeDefinition {
vec1::vec1![Tensor::name()]
}

fn load_tensors(&mut self, ctx: &mut ViewerContext<'_>, query: &SceneQuery<'_>) {
fn populate(
&mut self,
ctx: &mut ViewerContext<'_>,
query: &SceneQuery<'_>,
_state: &<BarChartSpaceView as SpaceViewClass>::State,
_scene_context: &<BarChartSpaceView as SpaceViewClass>::Context,
_highlights: &SpaceViewHighlights,
) -> Vec<re_renderer::QueueableDrawData> {
re_tracing::profile_function!();

let store = &ctx.store_db.entity_db.data_store;
Expand All @@ -37,5 +47,7 @@ impl SceneBarChart {
}
}
}

Vec::new()
}
}
150 changes: 150 additions & 0 deletions crates/re_space_view_bar_chart/src/space_view_class.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use egui::util::hash;
use re_components::TensorData;
use re_log_types::EntityPath;
use re_space_view::{controls, EmptySpaceViewState};
use re_viewer_context::{
auto_color, SpaceViewClass, SpaceViewClassName, SpaceViewId, TypedScene, ViewerContext,
};

use super::scene_part::SceneBarChart;

#[derive(Default)]
pub struct BarChartSpaceView;

impl SpaceViewClass for BarChartSpaceView {
type State = EmptySpaceViewState;
type Context = re_space_view::EmptySceneContext;
type SceneParts = SceneBarChart;
type ScenePartData = ();

fn name(&self) -> SpaceViewClassName {
"Bar Chart".into()
}

fn icon(&self) -> &'static re_ui::Icon {
&re_ui::icons::SPACE_VIEW_HISTOGRAM
}

fn help_text(&self, re_ui: &re_ui::ReUi, _state: &Self::State) -> egui::WidgetText {
let mut layout = re_ui::LayoutJobBuilder::new(re_ui);

layout.add("Pan by dragging, or scroll (+ ");
layout.add(controls::HORIZONTAL_SCROLL_MODIFIER);
layout.add(" for horizontal).\n");

layout.add("Zoom with pinch gesture or scroll + ");
layout.add(controls::ZOOM_SCROLL_MODIFIER);
layout.add(".\n");

layout.add("Drag ");
layout.add(controls::SELECTION_RECT_ZOOM_BUTTON);
layout.add(" to zoom in/out using a selection.\n\n");

layout.add_button_text("double-click");
layout.add(" to reset the view.");

layout.layout_job.into()
}

fn preferred_tile_aspect_ratio(&self, _state: &Self::State) -> Option<f32> {
None
}

fn selection_ui(
&self,
_ctx: &mut ViewerContext<'_>,
_ui: &mut egui::Ui,
_state: &mut Self::State,
_space_origin: &EntityPath,
_space_view_id: SpaceViewId,
) {
}

fn ui(
&self,
_ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
_state: &mut Self::State,
scene: &mut TypedScene<Self>,
_space_origin: &EntityPath,
_space_view_id: SpaceViewId,
) {
use egui::plot::{Bar, BarChart, Legend, Plot};

ui.scope(|ui| {
Plot::new("bar_chart_plot")
.legend(Legend::default())
.clamp_grid(true)
.show(ui, |plot_ui| {
fn create_bar_chart<N: Into<f64>>(
ent_path: &EntityPath,
values: impl Iterator<Item = N>,
) -> BarChart {
let color = auto_color(hash(ent_path) as _);
let fill = color.gamma_multiply(0.75).additive(); // make sure overlapping bars are obvious
BarChart::new(
values
.enumerate()
.map(|(i, value)| {
Bar::new(i as f64 + 0.5, value.into())
.width(0.95)
.name(format!("{ent_path} #{i}"))
.fill(fill)
.stroke(egui::Stroke::NONE)
})
.collect(),
)
.name(ent_path.to_string())
.color(color)
}

for (ent_path, tensor) in &scene.parts.charts {
let chart = match &tensor.data {
TensorData::U8(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorData::U16(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorData::U32(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorData::U64(data) => {
create_bar_chart(ent_path, data.iter().copied().map(|v| v as f64))
}
TensorData::I8(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorData::I16(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorData::I32(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorData::I64(data) => {
create_bar_chart(ent_path, data.iter().copied().map(|v| v as f64))
}
TensorData::F16(data) => {
create_bar_chart(ent_path, data.iter().map(|f| f.to_f32()))
}
TensorData::F32(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorData::F64(data) => {
create_bar_chart(ent_path, data.iter().copied())
}
TensorData::JPEG(_) => {
re_log::warn_once!(
"trying to display JPEG data as a bar chart ({:?})",
ent_path
);
continue;
}
};

plot_ui.bar_chart(chart);
}
});
});
}
}
2 changes: 1 addition & 1 deletion crates/re_space_view_text/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Rerun Text Space Views
//! Rerun text Space View
//!
//! A Space View that shows text entries in a table and scrolls with the active time.
Expand Down
13 changes: 1 addition & 12 deletions crates/re_space_view_text/src/scene_part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use re_data_store::EntityPath;
use re_log_types::{Component as _, InstanceKey, RowId};
use re_query::{range_entity_with_primary, QueryError};
use re_viewer_context::{
ArchetypeDefinition, ScenePart, ScenePartCollection, SceneQuery, SpaceViewClass,
SpaceViewHighlights, ViewerContext,
ArchetypeDefinition, ScenePart, SceneQuery, SpaceViewClass, SpaceViewHighlights, ViewerContext,
};

use crate::TextSpaceView;
Expand Down Expand Up @@ -32,16 +31,6 @@ pub struct SceneText {
pub text_entries: Vec<TextEntry>,
}

impl ScenePartCollection<TextSpaceView> for SceneText {
fn vec_mut(&mut self) -> Vec<&mut dyn ScenePart<TextSpaceView>> {
vec![self]
}

fn as_any(&self) -> &dyn std::any::Any {
self
}
}

impl ScenePart<TextSpaceView> for SceneText {
fn archetype(&self) -> ArchetypeDefinition {
vec1::vec1![re_components::TextEntry::name()]
Expand Down
13 changes: 1 addition & 12 deletions crates/re_space_view_text_box/src/scene_part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use re_arrow_store::LatestAtQuery;
use re_components::Component;
use re_query::{query_entity_with_primary, QueryError};
use re_viewer_context::{
ArchetypeDefinition, ScenePart, ScenePartCollection, SceneQuery, SpaceViewClass,
SpaceViewHighlights, ViewerContext,
ArchetypeDefinition, ScenePart, SceneQuery, SpaceViewClass, SpaceViewHighlights, ViewerContext,
};

use crate::TextBoxSpaceView;
Expand All @@ -21,16 +20,6 @@ pub struct SceneTextBox {
pub text_entries: Vec<TextBoxEntry>,
}

impl ScenePartCollection<TextBoxSpaceView> for SceneTextBox {
fn vec_mut(&mut self) -> Vec<&mut dyn ScenePart<TextBoxSpaceView>> {
vec![self]
}

fn as_any(&self) -> &dyn std::any::Any {
self
}
}

impl ScenePart<TextBoxSpaceView> for SceneTextBox {
fn archetype(&self) -> ArchetypeDefinition {
vec1::vec1![re_components::TextBox::name()]
Expand Down
Loading

1 comment on commit 56437db

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Rust Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.25.

Benchmark suite Current: 56437db Previous: 03f9224 Ratio
batch_points_arrow/encode_log_msg 126176 ns/iter (± 724) 92390 ns/iter (± 1005) 1.37
batch_points_arrow/decode_log_msg 85988 ns/iter (± 258) 68497 ns/iter (± 527) 1.26
arrow_batch_vecs/insert 35971 ns/iter (± 83) 27367 ns/iter (± 327) 1.31

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.