Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make draw order editable & solve 2D flickering issues, add draw order to arrow2d archetype #6644

Merged
merged 7 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions crates/re_edit_ui/src/datatype_editors/float_drag.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::RangeInclusive;

use egui::NumExt as _;
use re_types::datatypes;

Expand All @@ -7,7 +9,7 @@ pub fn edit_f32_zero_to_max(
ui: &mut egui::Ui,
value: &mut impl std::ops::DerefMut<Target = datatypes::Float32>,
) -> egui::Response {
edit_f32_zero_to_max_float_raw_impl(ui, &mut value.deref_mut().0)
edit_f32_float_raw_impl(ui, &mut value.deref_mut().0, 0.0..=f32::MAX)
}

/// Generic editor for a raw f32 value from zero to max float.
Expand All @@ -16,15 +18,28 @@ pub fn edit_f32_zero_to_max_float_raw(
ui: &mut egui::Ui,
value: &mut impl std::ops::DerefMut<Target = f32>,
) -> egui::Response {
edit_f32_zero_to_max_float_raw_impl(ui, value)
edit_f32_float_raw_impl(ui, value, 0.0..=f32::MAX)
}

/// Non monomorphized implementation of [`edit_f32_zero_to_max_float_raw`].
fn edit_f32_zero_to_max_float_raw_impl(ui: &mut egui::Ui, value: &mut f32) -> egui::Response {
/// Generic editor for a raw f32 value from min to max float.
pub fn edit_f32_min_to_max_float_raw(
_ctx: &re_viewer_context::ViewerContext<'_>,
ui: &mut egui::Ui,
value: &mut impl std::ops::DerefMut<Target = f32>,
) -> egui::Response {
edit_f32_float_raw_impl(ui, value, f32::MIN..=f32::MAX)
}

/// Non monomorphized implementation for f32 float editing.
fn edit_f32_float_raw_impl(
ui: &mut egui::Ui,
value: &mut f32,
range: RangeInclusive<f32>,
) -> egui::Response {
let speed = (*value * 0.01).at_least(0.001);
ui.add(
egui::DragValue::new(value)
.clamp_range(0.0..=f32::MAX) // TODO(#6633): Don't change incoming values
.clamp_range(range) // TODO(#6633): Don't change incoming values
.speed(speed),
)
}
Expand Down
5 changes: 4 additions & 1 deletion crates/re_edit_ui/src/datatype_editors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ mod singleline_string;

pub use bool_toggle::{edit_bool, edit_bool_raw};
pub use enum_combobox::edit_enum;
pub use float_drag::{edit_f32_zero_to_max, edit_f32_zero_to_max_float_raw, edit_f32_zero_to_one};
pub use float_drag::{
edit_f32_min_to_max_float_raw, edit_f32_zero_to_max, edit_f32_zero_to_max_float_raw,
edit_f32_zero_to_one,
};
pub use singleline_string::edit_singleline_string;
12 changes: 7 additions & 5 deletions crates/re_edit_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ mod response_utils;
mod visual_bounds2d;

use datatype_editors::{
edit_bool, edit_bool_raw, edit_enum, edit_f32_zero_to_max, edit_f32_zero_to_max_float_raw,
edit_f32_zero_to_one, edit_singleline_string,
edit_bool, edit_bool_raw, edit_enum, edit_f32_min_to_max_float_raw, edit_f32_zero_to_max,
edit_f32_zero_to_max_float_raw, edit_f32_zero_to_one, edit_singleline_string,
};
use re_types::{
blueprint::components::{BackgroundKind, Corner2D, LockRangeDuringZoom, ViewFit, Visible},
components::{
AggregationPolicy, AxisLength, Color, Colormap, DepthMeter, FillRatio, GammaCorrection,
ImagePlaneDistance, MagnificationFilter, MarkerSize, Name, Opacity, Radius, StrokeWidth,
Text,
AggregationPolicy, AxisLength, Color, Colormap, DepthMeter, DrawOrder, FillRatio,
GammaCorrection, ImagePlaneDistance, MagnificationFilter, MarkerSize, Name, Opacity,
Radius, StrokeWidth, Text,
},
};
use re_viewer_context::ViewerContext;
Expand Down Expand Up @@ -54,6 +54,8 @@ pub fn register_editors(registry: &mut re_viewer_context::ComponentUiRegistry) {
registry.add_singleline_editor_ui::<ImagePlaneDistance>(edit_f32_zero_to_max);
registry.add_singleline_editor_ui::<GammaCorrection>(edit_f32_zero_to_max);

registry.add_singleline_editor_ui::<DrawOrder>(edit_f32_min_to_max_float_raw);

registry.add_singleline_editor_ui::<DepthMeter>(edit_f32_zero_to_max_float_raw);
registry.add_singleline_editor_ui::<MarkerSize>(edit_f32_zero_to_max_float_raw);
registry.add_singleline_editor_ui::<Radius>(edit_f32_zero_to_max_float_raw);
Expand Down
128 changes: 46 additions & 82 deletions crates/re_space_view_spatial/src/contexts/depth_offsets.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use std::collections::{BTreeMap, BTreeSet};

use nohash_hasher::IntMap;
use ahash::HashMap;

use re_log_types::EntityPathHash;
use re_space_view::latest_at_with_blueprint_resolved_data;
use re_types::{components::DrawOrder, ComponentNameSet, Loggable as _};
use re_viewer_context::{IdentifiedViewSystem, ViewContextSystem};
use re_viewer_context::{IdentifiedViewSystem, ViewContextSystem, ViewSystemIdentifier};

use crate::visualizers::visualizers_processing_draw_order;

/// Context for creating a mapping from [`DrawOrder`] to [`re_renderer::DepthOffset`].
#[derive(Default)]
pub struct EntityDepthOffsets {
// TODO(wumpf): Given that archetypes (should) contain DrawData, we should have a map of DrawData to DepthOffset.
// Mapping entities to depth offset instead is inconsistent with the archetype queries which are
// expected to care about DepthOffset iff they can make use of it.
pub per_entity: IntMap<EntityPathHash, re_renderer::DepthOffset>,
pub box2d: re_renderer::DepthOffset,
pub lines2d: re_renderer::DepthOffset,
pub image: re_renderer::DepthOffset,
pub points: re_renderer::DepthOffset,
pub per_entity_and_visualizer:
HashMap<(ViewSystemIdentifier, EntityPathHash), re_renderer::DepthOffset>,
}

impl IdentifiedViewSystem for EntityDepthOffsets {
Expand All @@ -35,54 +32,11 @@ impl ViewContextSystem for EntityDepthOffsets {
ctx: &re_viewer_context::ViewContext<'_>,
query: &re_viewer_context::ViewQuery<'_>,
) {
#[derive(PartialEq, PartialOrd, Eq, Ord)]
enum DrawOrderTarget {
Entity(EntityPathHash),
DefaultBox2D,
DefaultLines2D,
DefaultImage,
DefaultPoints,
let mut entities_per_draw_order = BTreeMap::new();
for visualizer in visualizers_processing_draw_order() {
collect_draw_order_per_visualizer(ctx, query, visualizer, &mut entities_per_draw_order);
}

// Use a BTreeSet for entity hashes to get a stable order.
let mut entities_per_draw_order = BTreeMap::<DrawOrder, BTreeSet<DrawOrderTarget>>::new();
for data_result in query.iter_all_data_results() {
// Note that we can't use `query.iter_visible_data_results` here since `EntityDepthOffsets` isn't a visualizer
// and thus not in the list of per system data results.
if !data_result.is_visible(ctx.viewer_ctx) {
continue;
}

// TODO(#5607): what should happen if the promise is still pending?
if let Some(draw_order) = ctx
.recording()
.latest_at_component::<DrawOrder>(&data_result.entity_path, &ctx.current_query())
{
entities_per_draw_order
.entry(draw_order.value)
.or_default()
.insert(DrawOrderTarget::Entity(data_result.entity_path.hash()));
}
}

// Push in default draw orders. All of them using the none hash.
entities_per_draw_order.insert(
DrawOrder::DEFAULT_BOX2D,
[DrawOrderTarget::DefaultBox2D].into(),
);
entities_per_draw_order.insert(
DrawOrder::DEFAULT_IMAGE,
[DrawOrderTarget::DefaultImage].into(),
);
entities_per_draw_order.insert(
DrawOrder::DEFAULT_LINES2D,
[DrawOrderTarget::DefaultLines2D].into(),
);
entities_per_draw_order.insert(
DrawOrder::DEFAULT_POINTS2D,
[DrawOrderTarget::DefaultPoints].into(),
);

// Determine re_renderer draw order from this.
//
// We give objects with the same `DrawOrder` still a different depth offset
Expand All @@ -94,33 +48,14 @@ impl ViewContextSystem for EntityDepthOffsets {
.values()
.map(|entities| entities.len())
.sum();
let mut draw_order = -((num_entities_with_draw_order / 2) as re_renderer::DepthOffset);
self.per_entity = entities_per_draw_order
let mut depth_offset = -((num_entities_with_draw_order / 2) as re_renderer::DepthOffset);
self.per_entity_and_visualizer = entities_per_draw_order
.into_values()
.flat_map(|targets| {
targets
.into_iter()
.filter_map(|target| {
draw_order += 1;
match target {
DrawOrderTarget::Entity(entity) => Some((entity, draw_order)),
DrawOrderTarget::DefaultBox2D => {
self.box2d = draw_order;
None
}
DrawOrderTarget::DefaultLines2D => {
self.lines2d = draw_order;
None
}
DrawOrderTarget::DefaultImage => {
self.image = draw_order;
None
}
DrawOrderTarget::DefaultPoints => {
self.points = draw_order;
None
}
}
.flat_map(|keys| {
keys.into_iter()
.map(|key| {
depth_offset += 1;
(key, depth_offset)
})
.collect::<Vec<_>>()
})
Expand All @@ -131,3 +66,32 @@ impl ViewContextSystem for EntityDepthOffsets {
self
}
}

fn collect_draw_order_per_visualizer(
ctx: &re_viewer_context::ViewContext<'_>,
query: &re_viewer_context::ViewQuery<'_>,
visualizer_identifier: ViewSystemIdentifier,
entities_per_draw_order: &mut BTreeMap<
DrawOrder,
BTreeSet<(ViewSystemIdentifier, EntityPathHash)>,
>,
) {
let latest_at_query = ctx.current_query();
for data_result in query.iter_visible_data_results(ctx, visualizer_identifier) {
let query_shadowed_components = false;
let draw_order = latest_at_with_blueprint_resolved_data(
ctx,
None,
&latest_at_query,
data_result,
std::iter::once(DrawOrder::name()),
query_shadowed_components,
)
.get_mono_with_fallback::<DrawOrder>();

entities_per_draw_order
.entry(draw_order)
.or_default()
.insert((visualizer_identifier, data_result.entity_path.hash()));
}
}
20 changes: 13 additions & 7 deletions crates/re_space_view_spatial/src/visualizers/arrows2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ use re_query::range_zip_1x6;
use re_renderer::{renderer::LineStripFlags, LineDrawableBuilder, PickingLayerInstanceId};
use re_types::{
archetypes::Arrows2D,
components::{ClassId, Color, KeypointId, Position2D, Radius, Text, Vector2D},
components::{ClassId, Color, DrawOrder, KeypointId, Position2D, Radius, Text, Vector2D},
};
use re_viewer_context::{
ApplicableEntities, IdentifiedViewSystem, ResolvedAnnotationInfos,
SpaceViewSystemExecutionError, ViewContext, ViewContextCollection, ViewQuery,
VisualizableEntities, VisualizableFilterContext, VisualizerQueryInfo, VisualizerSystem,
ApplicableEntities, IdentifiedViewSystem, QueryContext, ResolvedAnnotationInfos,
SpaceViewSystemExecutionError, TypedComponentFallbackProvider, ViewContext,
ViewContextCollection, ViewQuery, VisualizableEntities, VisualizableFilterContext,
VisualizerQueryInfo, VisualizerSystem,
};

use crate::{
contexts::{EntityDepthOffsets, SpatialSceneEntityContext},
contexts::SpatialSceneEntityContext,
view_kind::SpatialSpaceViewKind,
visualizers::{filter_visualizable_2d_entities, UiLabel, UiLabelTarget},
};
Expand Down Expand Up @@ -223,7 +224,6 @@ impl VisualizerSystem for Arrows2DVisualizer {
ctx,
view_query,
context_systems,
context_systems.get::<EntityDepthOffsets>()?.points,
|ctx, entity_path, spatial_ctx, results| {
re_tracing::profile_scope!(format!("{entity_path}"));

Expand Down Expand Up @@ -305,4 +305,10 @@ impl VisualizerSystem for Arrows2DVisualizer {
}
}

re_viewer_context::impl_component_fallback_provider!(Arrows2DVisualizer => []);
impl TypedComponentFallbackProvider<DrawOrder> for Arrows2DVisualizer {
fn fallback_for(&self, _ctx: &QueryContext<'_>) -> DrawOrder {
DrawOrder::DEFAULT_LINES2D
}
}

re_viewer_context::impl_component_fallback_provider!(Arrows2DVisualizer => [DrawOrder]);
3 changes: 1 addition & 2 deletions crates/re_space_view_spatial/src/visualizers/arrows3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use re_viewer_context::{
};

use crate::{
contexts::{EntityDepthOffsets, SpatialSceneEntityContext},
contexts::SpatialSceneEntityContext,
view_kind::SpatialSpaceViewKind,
visualizers::{filter_visualizable_3d_entities, UiLabel, UiLabelTarget},
};
Expand Down Expand Up @@ -226,7 +226,6 @@ impl VisualizerSystem for Arrows3DVisualizer {
ctx,
view_query,
context_systems,
context_systems.get::<EntityDepthOffsets>()?.points,
|ctx, entity_path, spatial_ctx, results| {
re_tracing::profile_scope!(format!("{entity_path}"));

Expand Down
3 changes: 1 addition & 2 deletions crates/re_space_view_spatial/src/visualizers/assets3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use re_viewer_context::{

use super::{filter_visualizable_3d_entities, SpatialViewVisualizerData};
use crate::{
contexts::{EntityDepthOffsets, SpatialSceneEntityContext},
contexts::SpatialSceneEntityContext,
instance_hash_conversions::picking_layer_id_from_instance_path_hash,
mesh_cache::{AnyMesh, MeshCache, MeshCacheKey},
view_kind::SpatialSpaceViewKind,
Expand Down Expand Up @@ -145,7 +145,6 @@ impl VisualizerSystem for Asset3DVisualizer {
ctx,
view_query,
context_systems,
context_systems.get::<EntityDepthOffsets>()?.points,
|ctx, entity_path, spatial_ctx, results| {
re_tracing::profile_scope!(format!("{entity_path}"));

Expand Down
20 changes: 13 additions & 7 deletions crates/re_space_view_spatial/src/visualizers/boxes2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ use re_query::range_zip_1x6;
use re_renderer::{LineDrawableBuilder, PickingLayerInstanceId};
use re_types::{
archetypes::Boxes2D,
components::{ClassId, Color, HalfSizes2D, KeypointId, Position2D, Radius, Text},
components::{ClassId, Color, DrawOrder, HalfSizes2D, KeypointId, Position2D, Radius, Text},
};
use re_viewer_context::{
ApplicableEntities, IdentifiedViewSystem, ResolvedAnnotationInfos,
SpaceViewSystemExecutionError, ViewContext, ViewContextCollection, ViewQuery,
VisualizableEntities, VisualizableFilterContext, VisualizerQueryInfo, VisualizerSystem,
ApplicableEntities, IdentifiedViewSystem, QueryContext, ResolvedAnnotationInfos,
SpaceViewSystemExecutionError, TypedComponentFallbackProvider, ViewContext,
ViewContextCollection, ViewQuery, VisualizableEntities, VisualizableFilterContext,
VisualizerQueryInfo, VisualizerSystem,
};

use crate::{
contexts::{EntityDepthOffsets, SpatialSceneEntityContext},
contexts::SpatialSceneEntityContext,
view_kind::SpatialSpaceViewKind,
visualizers::{UiLabel, UiLabelTarget},
};
Expand Down Expand Up @@ -219,7 +220,6 @@ impl VisualizerSystem for Boxes2DVisualizer {
ctx,
view_query,
context_systems,
context_systems.get::<EntityDepthOffsets>()?.points,
|ctx, entity_path, spatial_ctx, results| {
re_tracing::profile_scope!(format!("{entity_path}"));

Expand Down Expand Up @@ -311,4 +311,10 @@ impl VisualizerSystem for Boxes2DVisualizer {
}
}

re_viewer_context::impl_component_fallback_provider!(Boxes2DVisualizer => []);
impl TypedComponentFallbackProvider<DrawOrder> for Boxes2DVisualizer {
fn fallback_for(&self, _ctx: &QueryContext<'_>) -> DrawOrder {
DrawOrder::DEFAULT_BOX2D
}
}

re_viewer_context::impl_component_fallback_provider!(Boxes2DVisualizer => [DrawOrder]);
3 changes: 1 addition & 2 deletions crates/re_space_view_spatial/src/visualizers/boxes3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use re_viewer_context::{
};

use crate::{
contexts::{EntityDepthOffsets, SpatialSceneEntityContext},
contexts::SpatialSceneEntityContext,
view_kind::SpatialSpaceViewKind,
visualizers::{UiLabel, UiLabelTarget},
};
Expand Down Expand Up @@ -210,7 +210,6 @@ impl VisualizerSystem for Boxes3DVisualizer {
ctx,
view_query,
context_systems,
context_systems.get::<EntityDepthOffsets>()?.points,
|ctx, entity_path, spatial_ctx, results| {
re_tracing::profile_scope!(format!("{entity_path}"));

Expand Down
Loading
Loading