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

Properly scale radii for 2D parts shown in 3D space view's pinhole image plane #4196

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions crates/re_space_view_spatial/src/contexts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ use re_viewer_context::{
/// Context objects for a single entity in a spatial scene.
pub struct SpatialSceneEntityContext<'a> {
pub world_from_entity: glam::Affine3A,

/// Scale factor to be applied on radii for 2D object drawn on a 3D space view's pinhole camera.
///
/// `Some` when a parent pinhole transform exists for the entity (that isn't the space origin).
/// In this case the factor converts from the "pixel" unit of pinhole to the 3D pinhole widget
/// actual pixel size (taking into account the image plane distance).
pub radii_scale_factor: Option<f32>,
Copy link
Member

Choose a reason for hiding this comment

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

This needs a docstring: what is it? And what is the difference between None and Some(1.0)?

pub depth_offset: DepthOffset,
pub annotations: std::sync::Arc<Annotations>,
pub shared_render_builders: &'a SharedRenderBuilders,
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/arrows3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Arrows3DPart {
process_annotations::<Vector3D, Arrows3D>(query, arch_view, &ent_context.annotations)?;

let colors = process_colors(arch_view, ent_path, &annotation_infos)?;
let radii = process_radii(arch_view, ent_path)?;
let radii = process_radii(arch_view, None, ent_path)?;

if arch_view.num_instances() <= self.max_labels {
// Max labels is small enough that we can afford iterating on the colors again.
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/boxes2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Boxes2DPart {
let positions = arch_view
.iter_optional_component::<Position2D>()?
.map(|position| position.unwrap_or(Position2D::ZERO));
let radii = process_radii(arch_view, ent_path)?;
let radii = process_radii(arch_view, ent_context.radii_scale_factor, ent_path)?;
let colors = process_colors(arch_view, ent_path, &annotation_infos)?;

if arch_view.num_instances() <= self.max_labels {
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/boxes3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Boxes3DPart {
let rotation = arch_view
.iter_optional_component::<Rotation3D>()?
.map(|position| position.unwrap_or(Rotation3D::IDENTITY));
let radii = process_radii(arch_view, ent_path)?;
let radii = process_radii(arch_view, None, ent_path)?;
let colors = process_colors(arch_view, ent_path, &annotation_infos)?;
let labels = process_labels(arch_view, &annotation_infos)?;

Expand Down
28 changes: 28 additions & 0 deletions crates/re_space_view_spatial/src/parts/entity_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ where
.per_entity
.get(&ent_path.hash())
.unwrap_or(&default_depth_offset),
radii_scale_factor: radii_scale_factor(ctx, query, transforms, ent_path),
annotations: annotations.0.find(ent_path),
shared_render_builders,
highlight: query.highlights.entity_outline_mask(ent_path.hash()),
Expand Down Expand Up @@ -96,3 +97,30 @@ where

Ok(())
}

fn radii_scale_factor(
ctx: &mut ViewerContext<'_>,
query: &ViewQuery<'_>,
transforms: &TransformContext,
ent_path: &EntityPath,
) -> Option<f32> {
let pinhole_ent_path = transforms.parent_pinhole(ent_path)?;

let pinhole = crate::query_pinhole(
ctx.store_db.store(),
&query.latest_at_query(),
pinhole_ent_path,
)?;

let distance = *query
.entity_props_map
.get(pinhole_ent_path)
.pinhole_image_plane_distance
.get();

let focal_length = pinhole.focal_length_in_pixels();
let focal_length = glam::vec2(focal_length.x(), focal_length.y());
let scale = distance / focal_length;

Some(2.0 / (1.0 / scale.x + 1.0 / scale.y))
}
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/lines2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Lines2DPart {
)?;

let colors = process_colors(arch_view, ent_path, &annotation_infos)?;
let radii = process_radii(arch_view, ent_path)?;
let radii = process_radii(arch_view, ent_context.radii_scale_factor, ent_path)?;

if arch_view.num_instances() <= self.max_labels {
// Max labels is small enough that we can afford iterating on the colors again.
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/lines3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl Lines3DPart {
)?;

let colors = process_colors(arch_view, ent_path, &annotation_infos)?;
let radii = process_radii(arch_view, ent_path)?;
let radii = process_radii(arch_view, None, ent_path)?;

if arch_view.num_instances() <= self.max_labels {
// Max labels is small enough that we can afford iterating on the colors again.
Expand Down
4 changes: 3 additions & 1 deletion crates/re_space_view_spatial/src/parts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,18 @@ pub fn process_labels<'a, A: Archetype>(
/// where no radius is specified.
pub fn process_radii<'a, A: Archetype>(
arch_view: &'a re_query::ArchetypeView<A>,
radii_scale_factor: Option<f32>,
ent_path: &EntityPath,
) -> Result<impl Iterator<Item = re_renderer::Size> + 'a, re_query::QueryError> {
re_tracing::profile_function!();

let ent_path = ent_path.clone();
Ok(arch_view
.iter_optional_component::<re_types::components::Radius>()?
.map(move |radius| {
radius.map_or(re_renderer::Size::AUTO, |r| {
if 0.0 <= r.0 && r.0.is_finite() {
re_renderer::Size::new_scene(r.0)
re_renderer::Size::new_scene(r.0 * radii_scale_factor.unwrap_or(1.0))
} else {
if r.0 < 0.0 {
re_log::warn_once!("Found negative radius in entity {ent_path}");
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/points2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Points2DPart {
)?;

let colors = process_colors(arch_view, ent_path, &annotation_infos)?;
let radii = process_radii(arch_view, ent_path)?;
let radii = process_radii(arch_view, ent_context.radii_scale_factor, ent_path)?;

let positions = arch_view
.iter_required_component::<Position2D>()?
Expand Down
2 changes: 1 addition & 1 deletion crates/re_space_view_spatial/src/parts/points3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl LoadedPoints {
ent_path: &EntityPath,
) -> Result<Vec<re_renderer::Size>, QueryError> {
re_tracing::profile_function!();
process_radii(arch_view, ent_path).map(|radii| {
process_radii(arch_view, None, ent_path).map(|radii| {
re_tracing::profile_scope!("collect");
radii.collect()
})
Expand Down
Loading