From 207937f64b64bca03598f31daafe69a90834b55e Mon Sep 17 00:00:00 2001 From: Antoine Beyeler Date: Tue, 22 Oct 2024 16:01:00 +0200 Subject: [PATCH] refactored visualizer and walkers plugin --- crates/viewer/re_space_view_map/src/lib.rs | 2 +- .../re_space_view_map/src/map_space_view.rs | 51 +++------------ .../geo_points.rs} | 65 +++++++++++++++---- .../re_space_view_map/src/visualizers/mod.rs | 1 + 4 files changed, 64 insertions(+), 55 deletions(-) rename crates/viewer/re_space_view_map/src/{map_visualizer_system.rs => visualizers/geo_points.rs} (57%) create mode 100644 crates/viewer/re_space_view_map/src/visualizers/mod.rs diff --git a/crates/viewer/re_space_view_map/src/lib.rs b/crates/viewer/re_space_view_map/src/lib.rs index de05cdfe34cd..9b40809f3dc2 100644 --- a/crates/viewer/re_space_view_map/src/lib.rs +++ b/crates/viewer/re_space_view_map/src/lib.rs @@ -3,7 +3,7 @@ //! A Space View that shows GPS coordinates on a map. mod map_space_view; -mod map_visualizer_system; mod map_windows; +mod visualizers; pub use map_space_view::MapSpaceView; diff --git a/crates/viewer/re_space_view_map/src/map_space_view.rs b/crates/viewer/re_space_view_map/src/map_space_view.rs index cc0f51e35308..944426f2a70b 100644 --- a/crates/viewer/re_space_view_map/src/map_space_view.rs +++ b/crates/viewer/re_space_view_map/src/map_space_view.rs @@ -25,42 +25,8 @@ use { walkers::{HttpTiles, Map, MapMemory, Plugin, Tiles}, }; -use crate::map_visualizer_system::{MapEntry, MapVisualizerSystem}; use crate::map_windows; - -// walkers plugin to visualize points on a Map -pub struct PositionsOnMap { - positions: Vec, -} - -impl Plugin for PositionsOnMap { - fn run( - &mut self, - _response: &egui::Response, - painter: egui::Painter, - projector: &walkers::Projector, - ) { - for entry in &self.positions { - // Position of the point we want to put our shapes. - let position = entry.position; - - // Project it into the position on the screen. - let position = projector.project(position).to_pos2(); - - // Radius of the circle - let radius = f32::from( - *entry - .radii - .unwrap_or(Radius(re_types::datatypes::Float32(10.))), - ); - - // Color of the circle - let color = entry.color.unwrap_or(Color::new(Color32::RED)); - - painter.circle_filled(position, radius, color); - } - } -} +use crate::visualizers::geo_points::{GeoPointEntry, GeoPointsVisualizer}; #[derive(Default)] pub struct MapSpaceViewState { @@ -141,7 +107,7 @@ Displays a Position3D on a map. &self, system_registry: &mut SpaceViewSystemRegistrator<'_>, ) -> Result<(), SpaceViewClassRegistryError> { - system_registry.register_visualizer::() + system_registry.register_visualizer::() } fn new_state(&self) -> Box { @@ -162,7 +128,7 @@ Displays a Position3D on a map. } fn spawn_heuristics(&self, ctx: &ViewerContext<'_>) -> SpaceViewSpawnHeuristics { - suggest_space_view_for_each_entity::(ctx, self) + suggest_space_view_for_each_entity::(ctx, self) } fn selection_ui( @@ -207,7 +173,6 @@ Displays a Position3D on a map. system_output: SystemExecutionOutput, ) -> Result<(), SpaceViewSystemExecutionError> { let state = state.downcast_mut::()?; - let map_viz_system = system_output.view_systems.get::()?; let blueprint_db = ctx.blueprint_db(); let view_id = query.space_view_id; @@ -235,21 +200,21 @@ Displays a Position3D on a map. Err(err) => return Err(err), }; + let geo_points_visualizer = system_output.view_systems.get::()?; + egui::Frame::default().show(ui, |ui| { let some_tiles_manager: Option<&mut dyn Tiles> = Some(tiles); let map_widget = ui.add( Map::new( some_tiles_manager, map_memory, - map_viz_system + geo_points_visualizer .map_entries .first() - .unwrap_or(&MapEntry::default()) + .unwrap_or(&GeoPointEntry::default()) .position, ) - .with_plugin(PositionsOnMap { - positions: map_viz_system.map_entries.clone(), - }), + .with_plugin(geo_points_visualizer.plugin()), ); map_widget.double_clicked().then(|| { diff --git a/crates/viewer/re_space_view_map/src/map_visualizer_system.rs b/crates/viewer/re_space_view_map/src/visualizers/geo_points.rs similarity index 57% rename from crates/viewer/re_space_view_map/src/map_visualizer_system.rs rename to crates/viewer/re_space_view_map/src/visualizers/geo_points.rs index bb84a4784690..b0facde7d15f 100644 --- a/crates/viewer/re_space_view_map/src/map_visualizer_system.rs +++ b/crates/viewer/re_space_view_map/src/visualizers/geo_points.rs @@ -1,5 +1,7 @@ use re_chunk_store::LatestAtQuery; +use re_renderer::Color32; use re_space_view::DataResultQuery; +use re_types::components::{Color, Radius}; use re_types::{ archetypes::Points3D, components::{self, Position3D}, @@ -8,18 +10,17 @@ use re_viewer_context::{ IdentifiedViewSystem, SpaceViewSystemExecutionError, ViewContext, ViewContextCollection, ViewQuery, VisualizerQueryInfo, VisualizerSystem, }; -use walkers::Position; - +use walkers::{Plugin, Position}; // --- #[derive(Debug, Clone)] -pub struct MapEntry { +pub struct GeoPointEntry { pub position: Position, pub radii: Option, pub color: Option, } -impl Default for MapEntry { +impl Default for GeoPointEntry { fn default() -> Self { Self { position: Position::from_lat_lon(51.4934, 0.), @@ -31,17 +32,17 @@ impl Default for MapEntry { /// A map scene, with entries on the map to render. #[derive(Default)] -pub struct MapVisualizerSystem { - pub map_entries: Vec, +pub struct GeoPointsVisualizer { + pub map_entries: Vec, } -impl IdentifiedViewSystem for MapVisualizerSystem { +impl IdentifiedViewSystem for GeoPointsVisualizer { fn identifier() -> re_viewer_context::ViewSystemIdentifier { - "Map".into() + "GeoPoints".into() } } -impl VisualizerSystem for MapVisualizerSystem { +impl VisualizerSystem for GeoPointsVisualizer { fn visualizer_query_info(&self) -> VisualizerQueryInfo { VisualizerQueryInfo::from_archetype::() } @@ -67,7 +68,7 @@ impl VisualizerSystem for MapVisualizerSystem { let color = results.get_mono_with_fallback::(); let radii = results.get_mono_with_fallback::(); - self.map_entries.push(MapEntry { + self.map_entries.push(GeoPointEntry { position: Position::from_lat_lon(position.x() as f64, position.y() as f64), radii: Some(radii), color: Some(color), @@ -86,4 +87,46 @@ impl VisualizerSystem for MapVisualizerSystem { } } -re_viewer_context::impl_component_fallback_provider!(MapVisualizerSystem => []); +re_viewer_context::impl_component_fallback_provider!(GeoPointsVisualizer => []); + +impl GeoPointsVisualizer { + /// Return a [`walkers::Plugin`] for this visualizer. + pub fn plugin<'a>(&'a self) -> impl Plugin + 'a { + GeoPointsPlugin { + map_entries: &self.map_entries, + } + } +} + +pub struct GeoPointsPlugin<'a> { + pub map_entries: &'a Vec, +} + +impl Plugin for GeoPointsPlugin<'_> { + fn run( + &mut self, + _response: &egui::Response, + painter: egui::Painter, + projector: &walkers::Projector, + ) { + for entry in self.map_entries { + // Position of the point we want to put our shapes. + let position = entry.position; + + // Project it into the position on the screen. + let position = projector.project(position).to_pos2(); + + // Radius of the circle + let radius = f32::from( + *entry + .radii + .unwrap_or(Radius(re_types::datatypes::Float32(10.))), + ); + + // Color of the circle + let color = entry.color.unwrap_or(Color::new(Color32::RED)); + + painter.circle_filled(position, radius, color); + } + } +} diff --git a/crates/viewer/re_space_view_map/src/visualizers/mod.rs b/crates/viewer/re_space_view_map/src/visualizers/mod.rs new file mode 100644 index 000000000000..039576cd7157 --- /dev/null +++ b/crates/viewer/re_space_view_map/src/visualizers/mod.rs @@ -0,0 +1 @@ +pub mod geo_points;