Skip to content

Commit

Permalink
Refactor the 2D heuristic again (#5166)
Browse files Browse the repository at this point in the history
### What
 - Builds on top of #5164

Adds a store subscriber for tracking the dimensions of image entities.

Gets rid of the previous bucketing logic and replaces it with a new
implementation that starts at the top of the subspace and incrementally
splits when it finds conflicting image entities within the space.

### 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/5166/index.html)
* Using examples from latest `main` build:
[app.rerun.io](https://app.rerun.io/pr/5166/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/5166/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
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/5166)
- [Docs
preview](https://rerun.io/preview/da9a0d206235016d14d9826e8c42ed8481d29643/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/da9a0d206235016d14d9826e8c42ed8481d29643/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

---------

Co-authored-by: Andreas Reich <r_andreas2@web.de>
  • Loading branch information
jleibs and Wumpf committed Feb 12, 2024
1 parent 40b43ad commit a9a466f
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 184 deletions.
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 @@ -6,6 +6,7 @@ mod contexts;
mod eye;
mod heuristics;
mod instance_hash_conversions;
mod max_image_dimension_subscriber;
mod mesh_cache;
mod mesh_loader;
mod picking;
Expand Down
96 changes: 96 additions & 0 deletions crates/re_space_view_spatial/src/max_image_dimension_subscriber.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use ahash::HashMap;
use nohash_hasher::IntMap;
use once_cell::sync::OnceCell;
use re_data_store::{StoreSubscriber, StoreSubscriberHandle};
use re_log_types::{EntityPath, StoreId};
use re_types::{components::TensorData, Loggable};

#[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]
pub struct ImageDimensions {
pub height: u64,
pub width: u64,
pub channels: u64,
}

#[derive(Default, Debug, Clone)]
pub struct MaxImageDimensions(IntMap<EntityPath, ImageDimensions>);

impl MaxImageDimensions {
/// Accesses the image dimension information for a given store
pub fn access<T>(
store_id: &StoreId,
f: impl FnOnce(&IntMap<EntityPath, ImageDimensions>) -> T,
) -> Option<T> {
re_data_store::DataStore::with_subscriber_once(
MaxImageDimensionSubscriber::subscription_handle(),
move |subscriber: &MaxImageDimensionSubscriber| {
subscriber.max_dimensions.get(store_id).map(|v| &v.0).map(f)
},
)
.flatten()
}
}

#[derive(Default)]
struct MaxImageDimensionSubscriber {
max_dimensions: HashMap<StoreId, MaxImageDimensions>,
}

impl MaxImageDimensionSubscriber {
/// Accesses the global store subscriber.
///
/// Lazily registers the subscriber if it hasn't been registered yet.
pub fn subscription_handle() -> StoreSubscriberHandle {
static SUBSCRIPTION: OnceCell<re_data_store::StoreSubscriberHandle> = OnceCell::new();
*SUBSCRIPTION.get_or_init(|| {
re_data_store::DataStore::register_subscriber(
Box::<MaxImageDimensionSubscriber>::default(),
)
})
}
}

impl StoreSubscriber for MaxImageDimensionSubscriber {
fn name(&self) -> String {
"MaxImageDimensionStoreSubscriber".to_owned()
}

fn as_any(&self) -> &dyn std::any::Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}

fn on_events(&mut self, events: &[re_data_store::StoreEvent]) {
re_tracing::profile_function!();

for event in events {
if event.diff.kind != re_data_store::StoreDiffKind::Addition {
// Max image dimensions are strictly additive
continue;
}

if let Some(cell) = event.diff.cells.get(&TensorData::name()) {
if let Ok(Some(tensor_data)) = cell.try_to_native_mono::<TensorData>() {
if let Some([height, width, channels]) =
tensor_data.image_height_width_channels()
{
let dimensions = self
.max_dimensions
.entry(event.store_id.clone())
.or_default()
.0
.entry(event.diff.entity_path.clone())
.or_default();

dimensions.height = dimensions.height.max(height);
dimensions.width = dimensions.width.max(width);
dimensions.channels = dimensions.channels.max(channels);
}
}
}
}
}
}
Loading

0 comments on commit a9a466f

Please sign in to comment.