Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Generalize map clustering

Neeraj Guptae9f7a40a0b21
diff --git a/mobile/apps/photos/lib/ui/map/map_screen.dart b/mobile/apps/photos/lib/ui/map/map_screen.dart
index 841d7ff71d..84f7203181 100644
--- a/mobile/apps/photos/lib/ui/map/map_screen.dart
+++ b/mobile/apps/photos/lib/ui/map/map_screen.dart
@@ -58,6 +58,7 @@ class _MapScreenState extends State<MapScreen> {
executionInterval: const Duration(milliseconds: 750),
);
static const _markerViewportPaddingPixels = 256.0;
+ static const _minimumMarkerDistancePixels = 100.0;
@override
void initState() {
@@ -121,7 +122,10 @@ class _MapScreenState extends State<MapScreen> {
);
}
- _mapClusterer = await createMapClusterer(points: mapPoints);
+ _mapClusterer = await createMapClusterer(
+ points: mapPoints,
+ minimumMarkerDistance: _minimumMarkerDistancePixels,
+ );
if (!mounted) return;
setState(() {
hasLocationData = true;
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index f4f102e9e0..1a626a7808 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2621,6 +2621,9 @@ dependencies = [
[[package]]
name = "ente-location"
version = "0.0.0"
+dependencies = [
+ "thiserror 2.0.18",
+]
[[package]]
name = "ente-locker-frb"
diff --git a/rust/bindings/frb/photos/src/api/location_api.rs b/rust/bindings/frb/photos/src/api/location_api.rs
new file mode 100644
index 0000000000..c1a390741a
--- /dev/null
+++ b/rust/bindings/frb/photos/src/api/location_api.rs
@@ -0,0 +1,15 @@
+use ente_location as core;
+use flutter_rust_bridge::frb;
+
+#[frb]
+pub enum LocationError {
+ Other { message: String },
+}
+
+impl From<core::Error> for LocationError {
+ fn from(error: core::Error) -> Self {
+ Self::Other {
+ message: ente_core::error::chain(&error),
+ }
+ }
+}
diff --git a/rust/bindings/frb/photos/src/api/map_cluster_api.rs b/rust/bindings/frb/photos/src/api/map_cluster_api.rs
index 250193d6e5..71ab35f988 100644
--- a/rust/bindings/frb/photos/src/api/map_cluster_api.rs
+++ b/rust/bindings/frb/photos/src/api/map_cluster_api.rs
@@ -1,6 +1,8 @@
use ente_location::cluster as core;
use flutter_rust_bridge::frb;
+use super::location_api::LocationError;
+
pub struct MapPoint {
pub image_index: u32,
pub latitude: f64,
@@ -10,7 +12,7 @@ pub struct MapPoint {
impl From<MapPoint> for core::MapPoint {
fn from(point: MapPoint) -> Self {
Self {
- image_index: point.image_index,
+ item_index: point.image_index,
latitude: point.latitude,
longitude: point.longitude,
}
@@ -29,19 +31,20 @@ pub struct MapViewport {
pub marker_max_y: f64,
}
-impl From<MapViewport> for core::MapViewport {
- fn from(viewport: MapViewport) -> Self {
- Self {
- north: viewport.north,
- south: viewport.south,
- east: viewport.east,
- west: viewport.west,
- zoom: viewport.zoom,
- marker_min_x: viewport.marker_min_x,
- marker_min_y: viewport.marker_min_y,
- marker_max_x: viewport.marker_max_x,
- marker_max_y: viewport.marker_max_y,
- }
+impl TryFrom<MapViewport> for core::MapViewport {
+ type Error = LocationError;
+
+ fn try_from(viewport: MapViewport) -> Result<Self, Self::Error> {
+ Ok(Self::new(
+ core::GeoBounds::new(viewport.north, viewport.south, viewport.east, viewport.west)?,
+ viewport.zoom,
+ core::PixelBounds::new(
+ viewport.marker_min_x,
+ viewport.marker_min_y,
+ viewport.marker_max_x,
+ viewport.marker_max_y,
+ )?,
+ )?)
}
}
@@ -59,10 +62,10 @@ pub struct MapMarkerGroup {
impl From<core::MapMarkerGroup> for MapMarkerGroup {
fn from(group: core::MapMarkerGroup) -> Self {
Self {
- image_index: group.image_index,
+ image_index: group.item_index,
latitude: group.latitude,
longitude: group.longitude,
- image_count: group.image_count,
+ image_count: group.item_count,
north: group.north,
south: group.south,
east: group.east,
@@ -81,19 +84,25 @@ pub struct MapClusterer {
inner: core::MapClusterer,
}
-pub fn create_map_clusterer(points: Vec<MapPoint>) -> MapClusterer {
- MapClusterer {
- inner: core::MapClusterer::new(points.into_iter().map(Into::into).collect()),
- }
+pub fn create_map_clusterer(
+ points: Vec<MapPoint>,
+ minimum_marker_distance: f64,
+) -> Result<MapClusterer, LocationError> {
+ Ok(MapClusterer {
+ inner: core::MapClusterer::new(
+ points.into_iter().map(Into::into).collect(),
+ minimum_marker_distance,
+ )?,
+ })
}
impl MapClusterer {
- pub fn cluster(&self, viewport: MapViewport) -> MapViewportResult {
- let result = self.inner.cluster(viewport.into());
+ pub fn cluster(&self, viewport: MapViewport) -> Result<MapViewportResult, LocationError> {
+ let result = self.inner.cluster(viewport.try_into()?);
- MapViewportResult {
- visible_image_indexes: result.visible_image_indexes,
+ Ok(MapViewportResult {
+ visible_image_indexes: result.visible_item_indices,
marker_groups: result.marker_groups.into_iter().map(Into::into).collect(),
- }
+ })
}
}
diff --git a/rust/bindings/frb/photos/src/api/mod.rs b/rust/bindings/frb/photos/src/api/mod.rs
index 96e1d3efe4..459be57658 100644
--- a/rust/bindings/frb/photos/src/api/mod.rs
+++ b/rust/bindings/frb/photos/src/api/mod.rs
@@ -2,6 +2,7 @@ pub mod cast_api;
pub mod contacts;
pub mod image_processing_api;
pub mod init;
+pub mod location_api;
#[cfg(any(feature = "flutter", frb_expand))]
pub mod log;
pub mod map_cluster_api;
diff --git a/rust/crates/location/Cargo.toml b/rust/crates/location/Cargo.toml
index ede06ca01e..61c12f4b6f 100644
--- a/rust/crates/location/Cargo.toml
+++ b/rust/crates/location/Cargo.toml
@@ -1,3 +1,6 @@
[package]
name = "ente-location"
edition = "2024"
+
+[dependencies]
+thiserror.workspace = true
diff --git a/rust/crates/location/src/cluster.rs b/rust/crates/location/src/cluster.rs
index ae4c97980f..9507400cc6 100644
--- a/rust/crates/location/src/cluster.rs
+++ b/rust/crates/location/src/cluster.rs
@@ -1,140 +1,269 @@
use std::collections::HashMap;
use std::f64::consts::PI;
-const MINIMUM_MARKER_DISTANCE_PIXELS: f64 = 100.0;
+use crate::{Coordinate, Error, Result};
+
const WEB_MERCATOR_TILE_SIZE: f64 = 256.0;
const MERCATOR_LATITUDE_LIMIT: f64 = 85.051_128_78;
-#[derive(Clone, Copy)]
+#[derive(Clone, Copy, Debug)]
pub struct MapPoint {
- pub image_index: u32,
+ pub item_index: u32,
pub latitude: f64,
pub longitude: f64,
}
+#[derive(Clone, Copy, Debug)]
+pub struct GeoBounds {
+ north: f64,
+ south: f64,
+ east: f64,
+ west: f64,
+}
+
+impl GeoBounds {
+ pub fn new(north: f64, south: f64, east: f64, west: f64) -> Result<Self> {
+ if !north.is_finite()
+ || !south.is_finite()
+ || !east.is_finite()
+ || !west.is_finite()
+ || !(-90.0..=90.0).contains(&north)
+ || !(-90.0..=90.0).contains(&south)
+ || north < south
+ || !(-180.0..=180.0).contains(&east)
+ || !(-180.0..=180.0).contains(&west)
+ {
+ return Err(Error::InvalidViewport);
+ }
+ Ok(Self {
+ north,
+ south,
+ east,
+ west,
+ })
+ }
+}
+
+#[derive(Clone, Copy, Debug)]
+pub struct PixelBounds {
+ min_x: f64,
+ min_y: f64,
+ max_x: f64,
+ max_y: f64,
+}
+
+impl PixelBounds {
+ pub fn new(min_x: f64, min_y: f64, max_x: f64, max_y: f64) -> Result<Self> {
+ if [min_x, min_y, max_x, max_y]
+ .iter()
+ .any(|value| !value.is_finite())
+ || min_x > max_x
+ || min_y > max_y
+ {
+ return Err(Error::InvalidViewport);
+ }
+ Ok(Self {
+ min_x,
+ min_y,
+ max_x,
+ max_y,
+ })
+ }
+}
+
+#[derive(Clone, Copy, Debug)]
pub struct MapViewport {
- pub north: f64,
- pub south: f64,
- pub east: f64,
- pub west: f64,
- pub zoom: f64,
- pub marker_min_x: f64,
- pub marker_min_y: f64,
- pub marker_max_x: f64,
- pub marker_max_y: f64,
+ bounds: GeoBounds,
+ zoom: f64,
+ markers: PixelBounds,
+}
+
+impl MapViewport {
+ pub fn new(bounds: GeoBounds, zoom: f64, markers: PixelBounds) -> Result<Self> {
+ let scale = 2.0_f64.powf(zoom);
+ if !zoom.is_finite() || !scale.is_finite() || scale == 0.0 {
+ return Err(Error::InvalidViewport);
+ }
+ Ok(Self {
+ bounds,
+ zoom,
+ markers,
+ })
+ }
}
+#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MapMarkerGroup {
- pub image_index: u32,
+ pub item_index: u32,
pub latitude: f64,
pub longitude: f64,
- pub image_count: u32,
+ pub item_count: u32,
pub north: f64,
pub south: f64,
pub east: f64,
pub west: f64,
}
+#[derive(Debug, PartialEq)]
pub struct MapViewportResult {
- pub visible_image_indexes: Vec<u32>,
+ pub visible_item_indices: Vec<u32>,
pub marker_groups: Vec<MapMarkerGroup>,
}
pub struct MapClusterer {
points: Vec<MapPoint>,
+ minimum_marker_distance: f64,
}
impl MapClusterer {
- pub fn new(points: Vec<MapPoint>) -> Self {
- Self { points }
+ pub fn new(points: Vec<MapPoint>, minimum_marker_distance: f64) -> Result<Self> {
+ if !minimum_marker_distance.is_finite() || minimum_marker_distance <= 0.0 {
+ return Err(Error::InvalidMarkerDistance);
+ }
+ if let Some((index, _)) = points
+ .iter()
+ .enumerate()
+ .find(|(_, point)| !Coordinate::new(point.latitude, point.longitude).is_valid())
+ {
+ return Err(Error::InvalidMapPoint(index));
+ }
+ Ok(Self {
+ points,
+ minimum_marker_distance,
+ })
}
pub fn cluster(&self, viewport: MapViewport) -> MapViewportResult {
- let mut visible_image_indexes = Vec::new();
+ let mut visible_item_indices = Vec::new();
let mut visible_points = Vec::new();
let mut buffered_points = Vec::new();
- let scale = WEB_MERCATOR_TILE_SIZE * 2.0_f64.powf(viewport.zoom);
+ let world_size = WEB_MERCATOR_TILE_SIZE * 2.0_f64.powf(viewport.zoom);
+ let viewport_center_x = (viewport.markers.min_x + viewport.markers.max_x) / 2.0;
for point in &self.points {
- let is_visible = point.longitude >= viewport.west
- && point.longitude <= viewport.east
- && point.latitude >= viewport.south
- && point.latitude <= viewport.north;
+ let is_visible =
+ longitude_is_visible(point.longitude, viewport.bounds.west, viewport.bounds.east)
+ && point.latitude >= viewport.bounds.south
+ && point.latitude <= viewport.bounds.north;
if is_visible {
- visible_image_indexes.push(point.image_index);
+ visible_item_indices.push(point.item_index);
}
let latitude = point
.latitude
.clamp(-MERCATOR_LATITUDE_LIMIT, MERCATOR_LATITUDE_LIMIT);
let sin_latitude = (latitude * PI / 180.0).sin();
- let x = (point.longitude + 180.0) / 360.0 * scale;
- let y = (0.5 - ((1.0 + sin_latitude) / (1.0 - sin_latitude)).ln() / (4.0 * PI)) * scale;
- if x < viewport.marker_min_x
- || x > viewport.marker_max_x
- || y < viewport.marker_min_y
- || y > viewport.marker_max_y
+ let canonical_x = (point.longitude + 180.0) / 360.0 * world_size;
+ let x =
+ canonical_x + ((viewport_center_x - canonical_x) / world_size).round() * world_size;
+ let y = (0.5 - ((1.0 + sin_latitude) / (1.0 - sin_latitude)).ln() / (4.0 * PI))
+ * world_size;
+ if x < viewport.markers.min_x
+ || x > viewport.markers.max_x
+ || y < viewport.markers.min_y
+ || y > viewport.markers.max_y
{
continue;
}
- let projected_point = ProjectedMapPoint {
+ let projected = ProjectedPoint {
point: *point,
x,
y,
};
if is_visible {
- visible_points.push(projected_point);
+ visible_points.push(projected);
} else {
- buffered_points.push(projected_point);
+ buffered_points.push(projected);
}
}
let mut groups_by_cell = HashMap::<(i64, i64), Vec<usize>>::new();
- let mut groups = Vec::<ProjectedMarkerGroup>::new();
- for point in visible_points.iter().chain(&buffered_points) {
- add_to_marker_group(*point, &mut groups_by_cell, &mut groups);
+ let mut groups = Vec::new();
+ for point in visible_points.into_iter().chain(buffered_points) {
+ self.add_to_group(point, &mut groups_by_cell, &mut groups);
}
MapViewportResult {
- visible_image_indexes,
- marker_groups: groups
- .into_iter()
- .map(ProjectedMarkerGroup::into_marker_group)
- .collect(),
+ visible_item_indices,
+ marker_groups: groups.into_iter().map(ProjectedGroup::finish).collect(),
+ }
+ }
+
+ fn add_to_group(
+ &self,
+ point: ProjectedPoint,
+ groups_by_cell: &mut HashMap<(i64, i64), Vec<usize>>,
+ groups: &mut Vec<ProjectedGroup>,
+ ) {
+ let distance = self.minimum_marker_distance;
+ let cell = (
+ (point.x / distance).floor() as i64,
+ (point.y / distance).floor() as i64,
+ );
+ let mut closest = None;
+ let mut closest_distance_squared = f64::INFINITY;
+
+ for x_offset in -1..=1 {
+ for y_offset in -1..=1 {
+ let Some(nearby) = groups_by_cell.get(&(cell.0 + x_offset, cell.1 + y_offset))
+ else {
+ continue;
+ };
+ for &group_index in nearby {
+ let group = &groups[group_index];
+ let x_distance = point.x - group.x;
+ let y_distance = point.y - group.y;
+ let distance_squared = x_distance * x_distance + y_distance * y_distance;
+ if distance_squared <= distance * distance
+ && distance_squared < closest_distance_squared
+ {
+ closest = Some(group_index);
+ closest_distance_squared = distance_squared;
+ }
+ }
+ }
+ }
+
+ if let Some(index) = closest {
+ groups[index].add(point.point);
+ } else {
+ let index = groups.len();
+ groups.push(ProjectedGroup::new(point));
+ groups_by_cell.entry(cell).or_default().push(index);
}
}
}
#[derive(Clone, Copy)]
-struct ProjectedMapPoint {
+struct ProjectedPoint {
point: MapPoint,
x: f64,
y: f64,
}
-struct ProjectedMarkerGroup {
- image_index: u32,
+struct ProjectedGroup {
+ item_index: u32,
latitude: f64,
longitude: f64,
x: f64,
y: f64,
- image_count: u32,
+ item_count: u32,
north: f64,
south: f64,
east: f64,
west: f64,
}
-impl ProjectedMarkerGroup {
- fn new(point: ProjectedMapPoint) -> Self {
+impl ProjectedGroup {
+ fn new(point: ProjectedPoint) -> Self {
Self {
- image_index: point.point.image_index,
+ item_index: point.point.item_index,
latitude: point.point.latitude,
longitude: point.point.longitude,
x: point.x,
y: point.y,
- image_count: 1,
+ item_count: 1,
north: point.point.latitude,
south: point.point.latitude,
east: point.point.longitude,
@@ -143,70 +272,87 @@ impl ProjectedMarkerGroup {
}
fn add(&mut self, point: MapPoint) {
- self.image_count += 1;
+ self.item_count += 1;
self.north = self.north.max(point.latitude);
self.south = self.south.min(point.latitude);
- self.east = self.east.max(point.longitude);
- self.west = self.west.min(point.longitude);
+ let longitude = unwrap_longitude(point.longitude, self.longitude);
+ self.east = self.east.max(longitude);
+ self.west = self.west.min(longitude);
}
- fn into_marker_group(self) -> MapMarkerGroup {
+ fn finish(self) -> MapMarkerGroup {
MapMarkerGroup {
- image_index: self.image_index,
+ item_index: self.item_index,
latitude: self.latitude,
longitude: self.longitude,
- image_count: self.image_count,
+ item_count: self.item_count,
north: self.north,
south: self.south,
- east: self.east,
- west: self.west,
+ east: normalize_longitude(self.east),
+ west: normalize_longitude(self.west),
}
}
}
-fn add_to_marker_group(
- point: ProjectedMapPoint,
- groups_by_cell: &mut HashMap<(i64, i64), Vec<usize>>,
- groups: &mut Vec<ProjectedMarkerGroup>,
-) {
- let cell = (
- (point.x / MINIMUM_MARKER_DISTANCE_PIXELS).floor() as i64,
- (point.y / MINIMUM_MARKER_DISTANCE_PIXELS).floor() as i64,
- );
- let mut closest_group_index = None;
- let mut closest_distance_squared = f64::INFINITY;
-
- for x_offset in -1..=1 {
- for y_offset in -1..=1 {
- let Some(nearby_group_indexes) =
- groups_by_cell.get(&(cell.0 + x_offset, cell.1 + y_offset))
- else {
- continue;
- };
+fn unwrap_longitude(longitude: f64, origin: f64) -> f64 {
+ origin + (longitude - origin + 180.0).rem_euclid(360.0) - 180.0
+}
- for group_index in nearby_group_indexes {
- let group = &groups[*group_index];
- let x_distance = point.x - group.x;
- let y_distance = point.y - group.y;
- let distance_squared = x_distance * x_distance + y_distance * y_distance;
- if distance_squared
- <= MINIMUM_MARKER_DISTANCE_PIXELS * MINIMUM_MARKER_DISTANCE_PIXELS
- && distance_squared < closest_distance_squared
- {
- closest_group_index = Some(*group_index);
- closest_distance_squared = distance_squared;
- }
- }
- }
+fn normalize_longitude(longitude: f64) -> f64 {
+ (longitude + 180.0).rem_euclid(360.0) - 180.0
+}
+
+fn longitude_is_visible(longitude: f64, west: f64, east: f64) -> bool {
+ if west <= east {
+ (west..=east).contains(&longitude)
+ } else {
+ longitude >= west || longitude <= east
}
+}
+
+#[cfg(test)]
+mod tests {
+ use super::{GeoBounds, MapClusterer, MapPoint, MapViewport, PixelBounds};
+
+ #[test]
+ fn clusters_visible_points_across_the_antimeridian() {
+ let clusterer = MapClusterer::new(
+ vec![
+ point(1, 0.0, 179.9),
+ point(2, 0.0, -179.9),
+ point(3, 0.0, 0.0),
+ ],
+ 10.0,
+ )
+ .unwrap();
+ let viewport = viewport(
+ [10.0, -10.0, -170.0, 170.0],
+ 2.0,
+ [0.0, 0.0, 2048.0, 1024.0],
+ );
- if let Some(group_index) = closest_group_index {
- groups[group_index].add(point.point);
- return;
+ let result = clusterer.cluster(viewport);
+
+ assert_eq!(result.visible_item_indices, [1, 2]);
+ assert_eq!(result.marker_groups[0].item_index, 1);
+ assert_eq!(result.marker_groups[0].item_count, 2);
+ assert!(result.marker_groups[0].west > result.marker_groups[0].east);
}
- let group_index = groups.len();
- groups.push(ProjectedMarkerGroup::new(point));
- groups_by_cell.entry(cell).or_default().push(group_index);
-}
+ fn point(item_index: u32, latitude: f64, longitude: f64) -> MapPoint {
+ MapPoint {
+ item_index,
+ latitude,
+ longitude,
+ }
+ }
+ fn viewport(bounds: [f64; 4], zoom: f64, markers: [f64; 4]) -> MapViewport {
+ MapViewport::new(
+ GeoBounds::new(bounds[0], bounds[1], bounds[2], bounds[3]).unwrap(),
+ zoom,
+ PixelBounds::new(markers[0], markers[1], markers[2], markers[3]).unwrap(),
+ )
+ .unwrap()
+ }
+}
diff --git a/rust/crates/location/src/error.rs b/rust/crates/location/src/error.rs
new file mode 100644
index 0000000000..95d41e2b9a
--- /dev/null
+++ b/rust/crates/location/src/error.rs
@@ -0,0 +1,13 @@
+use thiserror::Error;
+
+pub type Result<T> = std::result::Result<T, Error>;
+
+#[derive(Debug, Error)]
+pub enum Error {
+ #[error("map point {0} has an invalid coordinate")]
+ InvalidMapPoint(usize),
+ #[error("minimum marker distance must be finite and positive")]
+ InvalidMarkerDistance,
+ #[error("map viewport is invalid")]
+ InvalidViewport,
+}
diff --git a/rust/crates/location/src/lib.rs b/rust/crates/location/src/lib.rs
index f7b48702af..fc016b57b9 100644
--- a/rust/crates/location/src/lib.rs
+++ b/rust/crates/location/src/lib.rs
@@ -1 +1,26 @@
pub mod cluster;
+mod error;
+
+pub use error::{Error, Result};
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct Coordinate {
+ pub latitude: f64,
+ pub longitude: f64,
+}
+
+impl Coordinate {
+ pub const fn new(latitude: f64, longitude: f64) -> Self {
+ Self {
+ latitude,
+ longitude,
+ }
+ }
+
+ pub fn is_valid(self) -> bool {
+ self.latitude.is_finite()
+ && (-90.0..=90.0).contains(&self.latitude)
+ && self.longitude.is_finite()
+ && (-180.0..=180.0).contains(&self.longitude)
+ }
+}