Skip to content

Commit

Permalink
Double click on entity in blueprint & timepanel focuses camera on it …
Browse files Browse the repository at this point in the history
…in 3D space views (#4799)

### What

* Fixes #918
* Fixes #917

This PR introduces on-the-fly determined per-entity bounding boxes and
unifies a lot of behavior under the hood: A double click of a hovered
item (pretty much everywhere) causes now a "focused" event for that
item. Focuses are right now set on the viewer context for a single
frame, so each part of the viewer can react on it as it pleases. The 3D
Space view reacts by focusing the camera on the entity (using the new
bounding box) and tracks cameras.
Tracking of entities has been unified - originally this PR tracked
entities as well, but I found this a bit too confusing for this action,
we should instead have something more explicit (this was disabled in
86ddf0d so it's easy to get it back).


https://github.com/rerun-io/rerun/assets/1220815/b6077865-d500-4bc9-b112-8ff41b370fe0



Tradeoffs:
* can't focus on instances
-> before focused on 3D positions (without knowing anything about the
object under it) now focusing on entities; different behavior,
_sometimes_ worse, sometimes better
-> note that tracking an instance rarely what you want here (e.g. point
instance ids vary widely)
* tracking an entity is not always what you want -> disabled it
* it's very hard to tell that we're tracking something, can sometimes be
surprising
* Alternative design of `focus`: Send a focus event to space views that
should be interested in it. This makes would unify the dispatching code
for this which is nicer going forward. But for the moment it was just a
lot easier to implement it the way I did. I'm fairly sure though about
making focus not sticky in the global state since this way we'd end up
with two levels of selection.



### 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)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using newly built examples:
[app.rerun.io](https://app.rerun.io/pr/4799/index.html)
* Using examples from latest `main` build:
[app.rerun.io](https://app.rerun.io/pr/4799/index.html?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[app.rerun.io](https://app.rerun.io/pr/4799/index.html?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG

- [PR Build Summary](https://build.rerun.io/pr/4799)
- [Docs
preview](https://rerun.io/preview/7183423673682ce3d9c0c68fcb335689b8c13693/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/7183423673682ce3d9c0c68fcb335689b8c13693/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)
  • Loading branch information
Wumpf authored Jan 16, 2024
1 parent 8cd526a commit 1989951
Show file tree
Hide file tree
Showing 31 changed files with 428 additions and 231 deletions.
10 changes: 9 additions & 1 deletion crates/re_data_ui/src/item_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use re_entity_db::{EntityTree, InstancePath};
use re_log_types::{ComponentPath, EntityPath, TimeInt, Timeline};
use re_ui::SyntaxHighlighting;
use re_viewer_context::{
DataQueryId, HoverHighlight, Item, Selection, SpaceViewId, UiVerbosity, ViewerContext,
DataQueryId, HoverHighlight, Item, Selection, SpaceViewId, SystemCommandSender, UiVerbosity,
ViewerContext,
};

use super::DataUi;
Expand Down Expand Up @@ -339,6 +340,13 @@ pub fn select_hovered_on_click(
selection_state.set_hovered(selection.clone());
}

if response.double_clicked() {
if let Some((item, _)) = selection.first() {
ctx.command_sender
.send_system(re_viewer_context::SystemCommand::SetFocus(item.clone()));
}
}

if response.clicked() {
if response.ctx.input(|i| i.modifiers.command) {
selection_state.toggle_selection(selection);
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view/src/controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub const ROLL_MOUSE_MODIFIER: egui::Modifiers = egui::Modifiers::ALT;
pub const SPEED_UP_3D_MODIFIER: egui::Modifiers = egui::Modifiers::SHIFT;

/// Key to restore the camera.
pub const TRACKED_CAMERA_RESTORE_KEY: egui::Key = egui::Key::Escape;
pub const TRACKED_OBJECT_RESTORE_KEY: egui::Key = egui::Key::Escape;

/// Description text for which action resets a space view.
pub const RESET_VIEW_BUTTON_TEXT: &str = "double click";
Expand Down
3 changes: 2 additions & 1 deletion crates/re_space_view_spatial/src/eye.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ impl OrbitEye {
// The hard part is finding a good center. Let's try to keep the same, and see how that goes:
let distance = eye
.forward_in_world()
.dot(self.orbit_center - eye.pos_in_world());
.dot(self.orbit_center - eye.pos_in_world())
.abs();
self.orbit_radius = distance.at_least(self.orbit_radius / 5.0);
self.orbit_center = eye.pos_in_world() + self.orbit_radius * eye.forward_in_world();
self.world_from_view_rot = eye.world_from_rub_view.rotation();
Expand Down
1 change: 1 addition & 0 deletions crates/re_space_view_spatial/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod instance_hash_conversions;
mod mesh_cache;
mod mesh_loader;
mod picking;
mod scene_bounding_boxes;
mod space_camera_3d;
mod space_view_2d;
mod space_view_3d;
Expand Down
60 changes: 60 additions & 0 deletions crates/re_space_view_spatial/src/scene_bounding_boxes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use nohash_hasher::IntMap;
use re_log_types::EntityPathHash;
use re_viewer_context::VisualizerCollection;

use crate::visualizers::SpatialViewVisualizerData;

#[derive(Clone)]
pub struct SceneBoundingBoxes {
/// Accumulated bounding box over several frames.
pub accumulated: macaw::BoundingBox,

/// Overall bounding box of the scene for the current query.
pub current: macaw::BoundingBox,

/// Per-entity bounding boxes for the current query.
pub per_entity: IntMap<EntityPathHash, macaw::BoundingBox>,
}

impl Default for SceneBoundingBoxes {
fn default() -> Self {
Self {
accumulated: macaw::BoundingBox::nothing(),
current: macaw::BoundingBox::nothing(),
per_entity: IntMap::default(),
}
}
}

impl SceneBoundingBoxes {
pub fn update(&mut self, visualizers: &VisualizerCollection) {
re_tracing::profile_function!();

self.current = macaw::BoundingBox::nothing();
self.per_entity.clear();

for visualizer in visualizers.iter() {
if let Some(data) = visualizer
.data()
.and_then(|d| d.downcast_ref::<SpatialViewVisualizerData>())
{
for (entity, bbox) in &data.bounding_boxes {
self.per_entity
.entry(*entity)
.and_modify(|bbox_entry| *bbox_entry = bbox_entry.union(*bbox))
.or_insert(*bbox);
}
}
}

for bbox in self.per_entity.values() {
self.current = self.current.union(*bbox);
}

if self.accumulated.is_nothing() || !self.accumulated.size().is_finite() {
self.accumulated = self.current;
} else {
self.accumulated = self.accumulated.union(self.current);
}
}
}
13 changes: 6 additions & 7 deletions crates/re_space_view_spatial/src/space_view_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use crate::{
heuristics::{auto_spawn_heuristic, update_object_property_heuristics},
ui::SpatialSpaceViewState,
view_kind::SpatialSpaceViewKind,
visualizers::{
calculate_bounding_box, register_2d_spatial_visualizers, SpatialViewVisualizerData,
},
visualizers::{register_2d_spatial_visualizers, SpatialViewVisualizerData},
};

// TODO(#4388): 2D/3D relationships should be solved via a "SpatialTopology" construct.
Expand Down Expand Up @@ -60,7 +58,7 @@ impl SpaceViewClass for SpatialSpaceView2D {
}

fn preferred_tile_aspect_ratio(&self, state: &Self::State) -> Option<f32> {
let size = state.scene_bbox_accum.size();
let size = state.bounding_boxes.accumulated.size();
Some(size.x / size.y)
}

Expand Down Expand Up @@ -123,7 +121,7 @@ impl SpaceViewClass for SpatialSpaceView2D {
ctx,
ent_paths,
entity_properties,
&state.scene_bbox_accum,
&state.bounding_boxes.accumulated,
SpatialSpaceViewKind::TwoD,
);
}
Expand Down Expand Up @@ -205,8 +203,9 @@ impl SpaceViewClass for SpatialSpaceView2D {
query: &ViewQuery<'_>,
system_output: re_viewer_context::SystemExecutionOutput,
) -> Result<(), SpaceViewSystemExecutionError> {
state.scene_bbox =
calculate_bounding_box(&system_output.view_systems, &mut state.scene_bbox_accum);
re_tracing::profile_function!();

state.bounding_boxes.update(&system_output.view_systems);
state.scene_num_primitives = system_output
.context_systems
.get::<PrimitiveCounter>()?
Expand Down
7 changes: 3 additions & 4 deletions crates/re_space_view_spatial/src/space_view_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
heuristics::{auto_spawn_heuristic, update_object_property_heuristics},
ui::SpatialSpaceViewState,
view_kind::SpatialSpaceViewKind,
visualizers::{calculate_bounding_box, register_3d_spatial_visualizers, CamerasVisualizer},
visualizers::{register_3d_spatial_visualizers, CamerasVisualizer},
};

// TODO(andreas): This context is used to determine whether a 2D entity has a valid transform
Expand Down Expand Up @@ -157,7 +157,7 @@ impl SpaceViewClass for SpatialSpaceView3D {
ctx,
ent_paths,
entity_properties,
&state.scene_bbox_accum,
&state.bounding_boxes.accumulated,
SpatialSpaceViewKind::ThreeD,
);
}
Expand Down Expand Up @@ -185,8 +185,7 @@ impl SpaceViewClass for SpatialSpaceView3D {
) -> Result<(), SpaceViewSystemExecutionError> {
re_tracing::profile_function!();

state.scene_bbox =
calculate_bounding_box(&system_output.view_systems, &mut state.scene_bbox_accum);
state.bounding_boxes.update(&system_output.view_systems);
state.scene_num_primitives = system_output
.context_systems
.get::<PrimitiveCounter>()?
Expand Down
38 changes: 8 additions & 30 deletions crates/re_space_view_spatial/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use re_viewer_context::{

use super::{eye::Eye, ui_2d::View2DState, ui_3d::View3DState};
use crate::heuristics::auto_size_world_heuristic;
use crate::scene_bounding_boxes::SceneBoundingBoxes;
use crate::{
contexts::{AnnotationSceneContext, NonInteractiveEntities},
picking::{PickableUiRect, PickingContext, PickingHitType, PickingResult},
Expand Down Expand Up @@ -48,15 +49,9 @@ impl From<AutoSizeUnit> for WidgetText {
}

/// TODO(andreas): Should turn this "inside out" - [`SpatialSpaceViewState`] should be used by [`View2DState`]/[`View3DState`], not the other way round.
#[derive(Clone)]
#[derive(Clone, Default)]
pub struct SpatialSpaceViewState {
/// Estimated bounding box of all data. Accumulated over every time data is displayed.
///
/// Specify default explicitly, otherwise it will be a box at 0.0 after deserialization.
pub scene_bbox_accum: BoundingBox,

/// Estimated bounding box of all data for the last scene query.
pub scene_bbox: BoundingBox,
pub bounding_boxes: SceneBoundingBoxes,

/// Estimated number of primitives last frame. Used to inform some heuristics.
pub scene_num_primitives: usize,
Expand All @@ -71,23 +66,6 @@ pub struct SpatialSpaceViewState {
auto_size_config: re_renderer::AutoSizeConfig,
}

impl Default for SpatialSpaceViewState {
fn default() -> Self {
Self {
scene_bbox_accum: BoundingBox::nothing(),
scene_bbox: BoundingBox::nothing(),
scene_num_primitives: 0,
state_2d: Default::default(),
state_3d: Default::default(),
auto_size_config: re_renderer::AutoSizeConfig {
point_radius: re_renderer::Size::AUTO, // let re_renderer decide
line_radius: re_renderer::Size::AUTO, // let re_renderer decide
},
previous_picking_result: None,
}
}
}

impl SpaceViewState for SpatialSpaceViewState {
fn as_any(&self) -> &dyn std::any::Any {
self
Expand Down Expand Up @@ -127,7 +105,7 @@ impl SpatialSpaceViewState {

ctx.re_ui.selection_grid(ui, "spatial_settings_ui")
.show(ui, |ui| {
let auto_size_world = auto_size_world_heuristic(&self.scene_bbox_accum, self.scene_num_primitives);
let auto_size_world = auto_size_world_heuristic(&self.bounding_boxes.accumulated, self.scene_num_primitives);

ctx.re_ui.grid_left_hand_label(ui, "Default size");
ui.vertical(|ui| {
Expand Down Expand Up @@ -166,8 +144,8 @@ impl SpatialSpaceViewState {
"Resets camera position & orientation.\nYou can also double-click the 3D view.")
.clicked()
{
self.scene_bbox_accum = self.scene_bbox;
self.state_3d.reset_camera(&self.scene_bbox_accum, &view_coordinates);
self.bounding_boxes.accumulated = self.bounding_boxes.current;
self.state_3d.reset_camera(&self.bounding_boxes.accumulated, &view_coordinates);
}
let mut spin = self.state_3d.spin();
if re_ui.checkbox(ui, &mut spin, "Spin")
Expand Down Expand Up @@ -206,7 +184,7 @@ impl SpatialSpaceViewState {
.on_hover_text("The bounding box encompassing all Entities in the view right now");
ui.vertical(|ui| {
ui.style_mut().wrap = Some(false);
let BoundingBox { min, max } = self.scene_bbox;
let BoundingBox { min, max } = self.bounding_boxes.current;
ui.label(format!(
"x [{} - {}]",
format_f32(min.x),
Expand Down Expand Up @@ -655,7 +633,7 @@ pub fn picking(
SelectedSpaceContext::ThreeD {
space_3d: query.space_origin.clone(),
pos: hovered_point,
tracked_space_camera: state.state_3d.tracked_camera.clone(),
tracked_entity: state.state_3d.tracked_entity.clone(),
point_in_space_cameras: visualizers
.get::<CamerasVisualizer>()?
.space_cameras
Expand Down
5 changes: 3 additions & 2 deletions crates/re_space_view_spatial/src/ui_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,10 @@ pub fn view_2d(
let available_size = ui.available_size();
let store = ctx.entity_db.store();

let scene_rect_accum = state.bounding_boxes.accumulated;
let scene_rect_accum = egui::Rect::from_min_max(
state.scene_bbox_accum.min.truncate().to_array().into(),
state.scene_bbox_accum.max.truncate().to_array().into(),
scene_rect_accum.min.truncate().to_array().into(),
scene_rect_accum.max.truncate().to_array().into(),
);

// Determine the canvas which determines the extent of the explorable scene coordinates,
Expand Down
Loading

0 comments on commit 1989951

Please sign in to comment.