Skip to content

Commit

Permalink
refactored visualizer and walkers plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
abey79 committed Oct 22, 2024
1 parent aa6ff1c commit 207937f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 55 deletions.
2 changes: 1 addition & 1 deletion crates/viewer/re_space_view_map/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
51 changes: 8 additions & 43 deletions crates/viewer/re_space_view_map/src/map_space_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MapEntry>,
}

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 {
Expand Down Expand Up @@ -141,7 +107,7 @@ Displays a Position3D on a map.
&self,
system_registry: &mut SpaceViewSystemRegistrator<'_>,
) -> Result<(), SpaceViewClassRegistryError> {
system_registry.register_visualizer::<MapVisualizerSystem>()
system_registry.register_visualizer::<GeoPointsVisualizer>()
}

fn new_state(&self) -> Box<dyn SpaceViewState> {
Expand All @@ -162,7 +128,7 @@ Displays a Position3D on a map.
}

fn spawn_heuristics(&self, ctx: &ViewerContext<'_>) -> SpaceViewSpawnHeuristics {
suggest_space_view_for_each_entity::<MapVisualizerSystem>(ctx, self)
suggest_space_view_for_each_entity::<GeoPointsVisualizer>(ctx, self)
}

fn selection_ui(
Expand Down Expand Up @@ -207,7 +173,6 @@ Displays a Position3D on a map.
system_output: SystemExecutionOutput,
) -> Result<(), SpaceViewSystemExecutionError> {
let state = state.downcast_mut::<MapSpaceViewState>()?;
let map_viz_system = system_output.view_systems.get::<MapVisualizerSystem>()?;

let blueprint_db = ctx.blueprint_db();
let view_id = query.space_view_id;
Expand Down Expand Up @@ -235,21 +200,21 @@ Displays a Position3D on a map.
Err(err) => return Err(err),
};

let geo_points_visualizer = system_output.view_systems.get::<GeoPointsVisualizer>()?;

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(|| {
Expand Down
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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<components::Radius>,
pub color: Option<components::Color>,
}

impl Default for MapEntry {
impl Default for GeoPointEntry {
fn default() -> Self {
Self {
position: Position::from_lat_lon(51.4934, 0.),
Expand All @@ -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<MapEntry>,
pub struct GeoPointsVisualizer {
pub map_entries: Vec<GeoPointEntry>,
}

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::<Points3D>()
}
Expand All @@ -67,7 +68,7 @@ impl VisualizerSystem for MapVisualizerSystem {
let color = results.get_mono_with_fallback::<components::Color>();
let radii = results.get_mono_with_fallback::<components::Radius>();

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),
Expand All @@ -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<GeoPointEntry>,
}

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);
}
}
}
1 change: 1 addition & 0 deletions crates/viewer/re_space_view_map/src/visualizers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod geo_points;

0 comments on commit 207937f

Please sign in to comment.