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 2 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
1 change: 1 addition & 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,7 @@ use re_viewer_context::{
/// Context objects for a single entity in a spatial scene.
pub struct SpatialSceneEntityContext<'a> {
pub world_from_entity: glam::Affine3A,
pub pinhole_scale: Option<glam::Vec2>,
abey79 marked this conversation as resolved.
Show resolved Hide resolved
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.pinhole_scale, 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
27 changes: 27 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),
pinhole_scale: get_pinhole_scale(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,29 @@ where

Ok(())
}

fn get_pinhole_scale(
ctx: &mut ViewerContext<'_>,
query: &ViewQuery<'_>,
transforms: &TransformContext,
ent_path: &EntityPath,
) -> Option<glam::Vec2> {
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());

Some(distance / focal_length)
}
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.pinhole_scale, 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
10 changes: 9 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,24 @@ 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>,
pinhole_scale: Option<glam::Vec2>,
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)
let factor = if let Some(scale) = pinhole_scale {
2.0 / (1.0 / scale.x + 1.0 / scale.y)
} else {
1.
};

re_renderer::Size::new_scene(r.0 * factor)
} 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.pinhole_scale, ent_path)?;

let positions = arch_view
.iter_required_component::<Position2D>()?
Expand Down
3 changes: 2 additions & 1 deletion crates/re_space_view_spatial/src/parts/points3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,11 @@ impl LoadedPoints {
#[inline]
pub fn load_radii(
arch_view: &ArchetypeView<Points3D>,

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