use std::sync::Arc;
use geometry::Point;
use glam::{DVec2, Vec2};
use mln_style::feature::GeometryCoordinates;
use mln_style::hash::{HashMap, HashSet};
use smallvec::SmallVec;
use super::grid::{Aabb, Grid, Shape};
use crate::camera::View;
use crate::geo::ScreenLineString;
use crate::geo::intersection_tests::polygon_intersects_polygon;
use crate::map::MapMode;
use crate::query::feature_index::{FeatureIndex, FeatureSortOrder, IndexedSubfeature};
use crate::tile::OverscaledTileID;
use crate::worker::job::SourceKey;
const VIEWPORT_PADDING_DEFAULT: f32 = 100.0;
const VIEWPORT_PADDING_FOR_STATIC_TILES: f32 = 1024.0;
const CELL_SIZE: f32 = 25.0;
fn find_viewport_padding(view: &View, map_mode: MapMode) -> f32 {
if map_mode == MapMode::Tile {
return VIEWPORT_PADDING_FOR_STATIC_TILES;
}
if view.pitch() == 0.0 { VIEWPORT_PADDING_DEFAULT } else { VIEWPORT_PADDING_DEFAULT * 2.0 }
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Overlap {
#[default]
Never,
Always,
Cooperative,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct CollisionGroup(Option<u16>);
impl CollisionGroup {
pub const ALL: CollisionGroup = CollisionGroup(None);
fn id(self) -> u16 {
self.0.unwrap_or(0)
}
fn has(self, feature: &IndexedSubfeature) -> bool {
self.0.is_none_or(|group| feature.collision_group_id == group)
}
}
pub(crate) struct CollisionGroups<'a> {
ids: SmallVec<[&'a str; 4]>,
cross_source_collisions: bool,
}
impl<'a> CollisionGroups<'a> {
pub(crate) fn new(cross_source_collisions: bool) -> Self {
CollisionGroups { ids: SmallVec::new(), cross_source_collisions }
}
pub(crate) fn get(&mut self, source: &'a str) -> CollisionGroup {
if self.cross_source_collisions {
return CollisionGroup(None);
}
let index = self.ids.iter().position(|id| *id == source).unwrap_or_else(|| {
self.ids.push(source);
self.ids.len() - 1
});
CollisionGroup(Some(index as u16 + 1))
}
}
#[derive(Clone, Copy)]
pub struct Item<'a> {
pub feature: &'a IndexedSubfeature,
pub record: u32,
pub overlap: Overlap,
pub ignore_placement: bool,
}
#[derive(Debug)]
pub struct RetainedQueryData {
pub bucket_instance_id: u32,
pub feature_index: Option<Arc<FeatureIndex>>,
pub tile_id: OverscaledTileID,
pub source: SourceKey,
pub feature_sort_order: FeatureSortOrder,
}
pub struct CollisionIndex {
view: View,
size: DVec2,
viewport_padding: f32,
collision_grid: Grid<IndexedSubfeature>,
ignored_grid: Grid<IndexedSubfeature>,
screen_right_boundary: f32,
screen_bottom_boundary: f32,
grid_right_boundary: f32,
grid_bottom_boundary: f32,
retained_query_data: HashMap<u32, RetainedQueryData>,
}
impl CollisionIndex {
#[must_use]
pub fn new(view: &View, map_mode: MapMode) -> Self {
CollisionIndex::reusing(view, map_mode, None)
}
pub(crate) fn reusing(view: &View, map_mode: MapMode, previous: Option<CollisionIndex>) -> Self {
let viewport_padding = find_viewport_padding(view, map_mode);
let size = view.size();
let width = size.width as f32;
let height = size.height as f32;
let grid_width = width + 2.0 * viewport_padding;
let grid_height = height + 2.0 * viewport_padding;
let grid_size = Vec2::new(grid_width, grid_height);
let reset = |mut grid: Grid<IndexedSubfeature>| {
grid.reset(grid_size, CELL_SIZE);
grid
};
let (collision_grid, ignored_grid) = match previous {
Some(previous) => (reset(previous.collision_grid), reset(previous.ignored_grid)),
None => (Grid::new(grid_size, CELL_SIZE), Grid::new(grid_size, CELL_SIZE)),
};
CollisionIndex {
view: view.clone(),
size: DVec2::new(f64::from(size.width), f64::from(size.height)),
viewport_padding,
collision_grid,
ignored_grid,
screen_right_boundary: width + viewport_padding,
screen_bottom_boundary: height + viewport_padding,
grid_right_boundary: grid_width,
grid_bottom_boundary: grid_height,
retained_query_data: HashMap::default(),
}
}
#[must_use]
pub fn view(&self) -> &View {
&self.view
}
#[must_use]
pub fn viewport_padding(&self) -> f32 {
self.viewport_padding
}
#[inline]
#[must_use]
pub fn to_grid(&self, x: f64, y: f64) -> Vec2 {
let viewport_padding = f64::from(self.viewport_padding);
Vec2::new(
(f64::midpoint(x, 1.0) * self.size.x + viewport_padding) as f32,
(f64::midpoint(-y, 1.0) * self.size.y + viewport_padding) as f32,
)
}
#[inline]
#[must_use]
pub fn is_offscreen(&self, bounds: &Aabb) -> bool {
bounds.max.x < self.viewport_padding
|| bounds.min.x >= self.screen_right_boundary
|| bounds.max.y < self.viewport_padding
|| bounds.min.y >= self.screen_bottom_boundary
}
#[inline]
#[must_use]
pub fn is_inside_grid(&self, bounds: &Aabb) -> bool {
bounds.max.x >= 0.0
&& bounds.min.x < self.grid_right_boundary
&& bounds.max.y >= 0.0
&& bounds.min.y < self.grid_bottom_boundary
}
#[inline]
#[must_use]
pub fn collides(&self, shape: &Shape, overlap: Overlap, group: CollisionGroup) -> bool {
match overlap {
Overlap::Never => self.collision_grid.any(shape, |feature| group.has(feature)),
Overlap::Always => false,
Overlap::Cooperative => {
self.collision_grid.any(shape, |feature| feature.overlap_mode == Overlap::Never && group.has(feature))
}
}
}
#[inline]
pub fn insert(&mut self, shapes: &[Option<Shape>], item: &Item<'_>, group: CollisionGroup) {
let grid = if item.ignore_placement { &mut self.ignored_grid } else { &mut self.collision_grid };
for shape in shapes.iter().flatten() {
let feature = IndexedSubfeature {
bucket_instance_id: item.record,
collision_group_id: group.id(),
overlap_mode: item.overlap,
..item.feature.clone()
};
grid.insert(feature, *shape);
}
}
pub fn retain(&mut self, record: u32, data: impl FnOnce() -> RetainedQueryData) {
self.retained_query_data.entry(record).or_insert_with(data);
}
pub fn set_query_order(&mut self, record: u32, order: &FeatureSortOrder) {
if let Some(retained) = self.retained_query_data.get_mut(&record) {
retained.feature_sort_order.clone_from(order);
}
}
pub(crate) fn query_data(&self, record: u32) -> Result<&RetainedQueryData, String> {
self.retained_query_data.get(&record).ok_or_else(|| format!("no query data of the bucket instance {record}"))
}
pub(crate) fn query_rendered(&self, query_geometry: &ScreenLineString) -> HashMap<u32, Vec<IndexedSubfeature>> {
let mut result: HashMap<u32, Vec<IndexedSubfeature>> = HashMap::default();
if query_geometry.is_empty() || (self.collision_grid.is_empty() && self.ignored_grid.is_empty()) {
return result;
}
let mut grid_query: Vec<Point<f32>> = Vec::with_capacity(query_geometry.len());
for point in query_geometry {
grid_query.push(Point::new(point.x as f32 + self.viewport_padding, point.y as f32 + self.viewport_padding));
}
let mut envelope = Aabb::new(Vec2::INFINITY, Vec2::NEG_INFINITY);
for point in &grid_query {
let point = Vec2::new(point.x, point.y);
envelope.min = envelope.min.min(point);
envelope.max = envelope.max.max(point);
}
let envelope = Shape::Box(envelope);
let features = self.collision_grid.query(&envelope).chain(self.ignored_grid.query(&envelope));
let mut seen_buckets: HashMap<u32, HashSet<usize>> = HashMap::default();
for (feature, shape) in features {
let seen_features = seen_buckets.entry(feature.bucket_instance_id).or_default();
if seen_features.contains(&feature.index) {
continue;
}
if !polygon_intersects_box(&grid_query, &shape.bounds()) {
continue;
}
seen_features.insert(feature.index);
result.entry(feature.bucket_instance_id).or_default().push(feature.clone());
}
result
}
}
fn polygon_intersects_box(polygon: &[Point<f32>], bbox: &Aabb) -> bool {
let mut integer_polygon = GeometryCoordinates::new();
for point in polygon {
integer_polygon.push(Point::new(point.x as i16, point.y as i16));
}
let min_x1 = bbox.min.x as i16;
let max_y1 = bbox.max.y as i16;
let min_y1 = bbox.min.y as i16;
let max_x1 = bbox.max.x as i16;
let bbox_points: GeometryCoordinates = vec![
Point::new(min_x1, min_y1),
Point::new(max_x1, min_y1),
Point::new(max_x1, max_y1),
Point::new(min_x1, max_y1),
];
polygon_intersects_polygon(&integer_polygon, &bbox_points)
}
#[cfg(test)]
mod tests {
use mln_style::size::Size;
use super::*;
fn feature() -> IndexedSubfeature {
IndexedSubfeature {
index: 0,
source_layer_name: Arc::from(""),
bucket_leader_id: Arc::from(""),
sort_index: 0,
bucket_instance_id: 0,
collision_group_id: 0,
overlap_mode: Overlap::Never,
}
}
fn collision_index() -> CollisionIndex {
let mut state = crate::camera::TransformState::default();
state.set_size(Size::new(512, 512));
CollisionIndex::new(state.view(), MapMode::Static)
}
fn insert(index: &mut CollisionIndex, overlap: Overlap, group: CollisionGroup) {
let projected = [Some(Shape::Box(Aabb::new(Vec2::splat(100.0), Vec2::splat(200.0))))];
let feature = feature();
let item = Item { feature: &feature, record: 1, overlap, ignore_placement: false };
index.insert(&projected, &item, group);
}
fn collides(index: &CollisionIndex, overlap: Overlap, group: CollisionGroup) -> bool {
let projected = Shape::Box(Aabb::new(Vec2::splat(150.0), Vec2::splat(250.0)));
index.collides(&projected, overlap, group)
}
const ALL: CollisionGroup = CollisionGroup::ALL;
#[test]
fn modes_collide_with_a_never_box() {
let mut index = collision_index();
insert(&mut index, Overlap::Never, ALL);
assert!(collides(&index, Overlap::Never, ALL));
assert!(!collides(&index, Overlap::Always, ALL));
assert!(collides(&index, Overlap::Cooperative, ALL));
}
#[test]
fn only_never_collides_with_an_overlapping_box() {
for placed in [Overlap::Always, Overlap::Cooperative] {
let mut index = collision_index();
insert(&mut index, placed, ALL);
assert!(collides(&index, Overlap::Never, ALL), "never over {placed:?}");
assert!(!collides(&index, Overlap::Always, ALL), "always over {placed:?}");
assert!(!collides(&index, Overlap::Cooperative, ALL), "cooperative over {placed:?}");
}
}
#[test]
fn cooperative_keeps_the_collision_group() {
let mut index = collision_index();
insert(&mut index, Overlap::Never, CollisionGroup(Some(2)));
assert!(collides(&index, Overlap::Cooperative, CollisionGroup(Some(2))));
assert!(!collides(&index, Overlap::Cooperative, CollisionGroup(Some(3))));
assert!(collides(&index, Overlap::Never, CollisionGroup(Some(2))));
assert!(!collides(&index, Overlap::Never, CollisionGroup(Some(3))));
}
#[test]
fn sources_take_groups_unless_they_collide_across() {
let mut groups = CollisionGroups::new(false);
let (a, b) = (groups.get("a"), groups.get("b"));
assert_ne!(a, b);
assert_eq!(groups.get("a"), a);
assert_eq!(CollisionGroups::new(true).get("a"), ALL);
}
}