use alloc::collections::{BTreeMap, BTreeSet};
use alloc::vec::Vec;
use crate::geom::Rect;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Item {
Segment(usize),
ObstacleLow(usize),
ObstacleHigh(usize),
WallLow,
WallHigh,
Bound(i32),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Kind {
Segments,
Obstacle,
Wall,
Boxes,
Shape,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Constraint {
pub left: Item,
pub right: Item,
pub kind: Kind,
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Object {
pub(crate) item: Item,
pub(crate) position: i32,
pub(crate) extent: (i32, i32),
pub(crate) rank: u32,
}
type Key = (i64, u8, u32, usize);
fn key(object: &Object, index: usize) -> Key {
let class = match object.item {
Item::ObstacleHigh(_) => 0,
Item::Segment(_) => 1,
Item::ObstacleLow(_) => 2,
Item::WallLow | Item::WallHigh | Item::Bound(_) => 3,
};
let position = match object.item {
Item::WallLow => i64::MIN,
Item::WallHigh => i64::MAX,
_ => i64::from(object.position),
};
(position, class, object.rank, index)
}
pub(crate) fn obstacle_objects(rects: &[Rect], axis: crate::geom::Axis) -> Vec<Object> {
let mut objects = Vec::with_capacity(2 * rects.len());
for (i, r) in rects.iter().enumerate() {
let (lo, hi) = r.range(axis);
let extent = r.range(axis.other());
objects.push(Object {
item: Item::ObstacleLow(i),
position: lo,
extent,
rank: 0,
});
objects.push(Object {
item: Item::ObstacleHigh(i),
position: hi,
extent,
rank: 0,
});
}
objects
}
pub(crate) fn generate(objects: &[Object]) -> Vec<Constraint> {
generate_with(objects, false)
}
pub(crate) fn generate_with(objects: &[Object], boxes: bool) -> Vec<Constraint> {
let mut events: Vec<(i32, u8, usize)> = Vec::with_capacity(2 * objects.len());
for (i, object) in objects.iter().enumerate() {
let is_segment = matches!(object.item, Item::Segment(_));
let (open, close) = if is_segment { (1, 2) } else { (3, 0) };
events.push((object.extent.0, open, i));
events.push((object.extent.1, close, i));
}
events.sort_unstable();
let wall_low = Object {
item: Item::WallLow,
position: 0,
extent: (0, 0),
rank: 0,
};
let wall_high = Object {
item: Item::WallHigh,
position: 0,
extent: (0, 0),
rank: 0,
};
let mut active: BTreeMap<Key, Item> = BTreeMap::new();
active.insert(key(&wall_low, usize::MAX - 1), Item::WallLow);
active.insert(key(&wall_high, usize::MAX), Item::WallHigh);
let mut constraints = BTreeSet::new();
for (_, kind, i) in events {
let k = key(&objects[i], i);
let opening = kind == 1 || kind == 3;
if opening {
active.insert(k, objects[i].item);
}
let below = active.range(..k).next_back().map(|(_, &item)| item);
let above = active
.range((core::ops::Bound::Excluded(k), core::ops::Bound::Unbounded))
.next()
.map(|(_, &item)| item);
if opening {
for (left, right) in [
(below, Some(objects[i].item)),
(Some(objects[i].item), above),
] {
if let (Some(l), Some(r)) = (left, right) {
push(&mut constraints, l, r, boxes);
}
}
} else {
active.remove(&k);
if let (Some(l), Some(r)) = (below, above) {
push(&mut constraints, l, r, boxes);
}
}
}
constraints.into_iter().collect()
}
fn push(constraints: &mut BTreeSet<Constraint>, left: Item, right: Item, boxes: bool) {
let is_segment = |item: Item| matches!(item, Item::Segment(_));
let is_wall = |item: Item| matches!(item, Item::WallLow | Item::WallHigh);
let kind = match (is_segment(left), is_segment(right)) {
(true, true) => Kind::Segments,
(false, false) if boxes && !is_wall(left) && !is_wall(right) => Kind::Boxes,
(false, false) => return,
_ if is_wall(left) || is_wall(right) => Kind::Wall,
_ => Kind::Obstacle,
};
constraints.insert(Constraint { left, right, kind });
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn chain_between_obstacles() {
let seg = |i: usize, rank: u32| Object {
item: Item::Segment(i),
position: 5,
extent: (0, 10),
rank,
};
let objects = [
Object {
item: Item::ObstacleHigh(0),
position: 0,
extent: (-5, 15),
rank: 0,
},
seg(0, 0),
seg(1, 1),
seg(2, 2),
Object {
item: Item::ObstacleLow(1),
position: 10,
extent: (-5, 15),
rank: 0,
},
];
let constraints = generate(&objects);
let has = |l: Item, r: Item| constraints.iter().any(|c| c.left == l && c.right == r);
assert!(has(Item::ObstacleHigh(0), Item::Segment(0)));
assert!(has(Item::Segment(0), Item::Segment(1)));
assert!(has(Item::Segment(1), Item::Segment(2)));
assert!(has(Item::Segment(2), Item::ObstacleLow(1)));
assert!(!has(Item::Segment(0), Item::Segment(2)));
}
}