|
| 1 | +use core::ops::Deref; |
| 2 | + |
| 3 | +use alloc::vec::Vec; |
| 4 | + |
| 5 | +use crate::TILE_SIZE; |
| 6 | + |
| 7 | +#[derive(Debug, Clone, Copy)] |
| 8 | +pub struct DirtyRect { |
| 9 | + pub min_x: u32, |
| 10 | + pub min_y: u32, |
| 11 | + pub max_x: u32, |
| 12 | + pub max_y: u32, |
| 13 | +} |
| 14 | + |
| 15 | +impl DirtyRect { |
| 16 | + pub const fn new_empty() -> Self { |
| 17 | + Self { |
| 18 | + min_x: 0, |
| 19 | + min_y: 0, |
| 20 | + max_x: 0, |
| 21 | + max_y: 0, |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + #[inline] |
| 26 | + pub const fn tiled<const TILE_SIZE: u32>(self) -> Self { |
| 27 | + Self { |
| 28 | + min_x: self.min_x / TILE_SIZE * TILE_SIZE, |
| 29 | + min_y: self.min_y / TILE_SIZE * TILE_SIZE, |
| 30 | + max_x: self.max_x.div_ceil(TILE_SIZE) * TILE_SIZE, |
| 31 | + max_y: self.max_y.div_ceil(TILE_SIZE) * TILE_SIZE, |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + #[inline] |
| 36 | + pub const fn width(self) -> u32 { |
| 37 | + self.max_x - self.min_x |
| 38 | + } |
| 39 | + #[inline] |
| 40 | + pub const fn height(self) -> u32 { |
| 41 | + self.max_y - self.min_y |
| 42 | + } |
| 43 | + |
| 44 | + #[inline] |
| 45 | + pub const fn to_egui_rect(self) -> egui::Rect { |
| 46 | + egui::Rect { |
| 47 | + min: egui::Pos2 { |
| 48 | + x: self.min_x as f32, |
| 49 | + y: self.min_y as f32, |
| 50 | + }, |
| 51 | + max: egui::Pos2 { |
| 52 | + x: self.max_x as f32, |
| 53 | + y: self.max_y as f32, |
| 54 | + }, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + #[inline] |
| 59 | + pub const fn is_empty(&self) -> bool { |
| 60 | + self.min_x == self.max_x || self.min_y == self.max_y |
| 61 | + } |
| 62 | + |
| 63 | + #[inline] |
| 64 | + pub const fn intersects(self, other: Self) -> bool { |
| 65 | + self.min_x < other.max_x && self.max_x > other.min_x |
| 66 | + } |
| 67 | + |
| 68 | + #[inline] |
| 69 | + pub fn intersection(self, other: DirtyRect) -> Self { |
| 70 | + Self { |
| 71 | + min_x: self.min_x.max(other.min_x), |
| 72 | + min_y: self.min_y.max(other.min_y), |
| 73 | + max_x: self.max_x.min(other.max_x), |
| 74 | + max_y: self.max_y.min(other.max_y), |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + #[inline] |
| 79 | + pub fn union(&self, other: DirtyRect) -> Self { |
| 80 | + Self { |
| 81 | + min_x: self.min_x.min(other.min_x), |
| 82 | + min_y: self.min_y.min(other.min_y), |
| 83 | + max_x: self.max_x.max(other.max_x), |
| 84 | + max_y: self.max_y.max(other.max_y), |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#[derive(Debug, Default)] |
| 90 | +pub struct ComputeTiledDirtyRects { |
| 91 | + minimal_non_overlapping_bboxes: Vec<DirtyRect>, |
| 92 | + pub(crate) bboxes: Vec<DirtyRect>, |
| 93 | + x_intervals: Vec<(u32, u32)>, |
| 94 | + ys: Vec<u32>, |
| 95 | +} |
| 96 | + |
| 97 | +impl Deref for ComputeTiledDirtyRects { |
| 98 | + type Target = [DirtyRect]; |
| 99 | + |
| 100 | + fn deref(&self) -> &Self::Target { |
| 101 | + &self.minimal_non_overlapping_bboxes |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl ComputeTiledDirtyRects { |
| 106 | + pub fn intersections(&self, other: DirtyRect) -> impl Iterator<Item = DirtyRect> + '_ { |
| 107 | + self.minimal_non_overlapping_bboxes |
| 108 | + .iter() |
| 109 | + .filter(move |bbox| bbox.intersects(other)) |
| 110 | + .map(move |bbox| bbox.intersection(other)) |
| 111 | + } |
| 112 | + |
| 113 | + pub fn set_bboxes(&mut self, boxes: impl Iterator<Item = DirtyRect>) { |
| 114 | + fn merge_intervals(intervals: &mut [(u32, u32)], mut f_yield: impl FnMut((u32, u32))) { |
| 115 | + if intervals.is_empty() { |
| 116 | + return; |
| 117 | + } |
| 118 | + intervals.sort_unstable_by(|a, b| a.0.cmp(&b.0)); |
| 119 | + let mut it = intervals.iter().copied(); |
| 120 | + if let Some(mut last) = it.next() { |
| 121 | + for (start, end) in it { |
| 122 | + if start <= last.1 { |
| 123 | + last.1 = last.1.max(end); |
| 124 | + } else { |
| 125 | + f_yield(last); |
| 126 | + last = (start, end); |
| 127 | + } |
| 128 | + } |
| 129 | + f_yield(last); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + self.minimal_non_overlapping_bboxes.clear(); |
| 134 | + self.bboxes.clear(); |
| 135 | + self.bboxes.extend(boxes.map(|b| b.tiled::<TILE_SIZE>())); |
| 136 | + // Step 1: collect all unique y-coordinates |
| 137 | + self.ys.clear(); |
| 138 | + self.ys |
| 139 | + .extend(self.bboxes.iter().flat_map(|b| [b.min_y, b.max_y])); |
| 140 | + self.ys.sort_unstable(); |
| 141 | + self.ys.dedup(); |
| 142 | + |
| 143 | + // Step 2: iterate over horizontal strips |
| 144 | + for strip in self.ys.windows(2) { |
| 145 | + let min_y = strip[0]; |
| 146 | + let max_y = strip[1]; |
| 147 | + |
| 148 | + // Find boxes intersecting this horizontal strip |
| 149 | + self.x_intervals.clear(); |
| 150 | + for b in &self.bboxes { |
| 151 | + if b.min_y < max_y && b.max_y > min_y { |
| 152 | + self.x_intervals.push((b.min_x, b.max_x)); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // Merge overlapping x-intervals |
| 157 | + merge_intervals(&mut self.x_intervals, |(min_x, max_x)| { |
| 158 | + match self.minimal_non_overlapping_bboxes.last_mut() { |
| 159 | + Some(rect) |
| 160 | + if rect.min_x == min_x && rect.max_x == max_x && rect.max_y == min_y => |
| 161 | + { |
| 162 | + rect.max_y = max_y; |
| 163 | + } |
| 164 | + _ => { |
| 165 | + self.minimal_non_overlapping_bboxes.push(DirtyRect { |
| 166 | + min_x, |
| 167 | + min_y, |
| 168 | + max_x, |
| 169 | + max_y, |
| 170 | + }); |
| 171 | + } |
| 172 | + } |
| 173 | + }); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments