1
0
Fork 0
forked from wry/wry

autocommit 2022-04-04 23:44:25 CEST

This commit is contained in:
Julian Orth 2022-04-04 23:44:25 +02:00
parent 5f79aab15f
commit 1f05ea431e
2 changed files with 24 additions and 16 deletions

View file

@ -17,7 +17,7 @@ pub struct Rect {
type Container = SmallVec<[Rect; 1]>; type Container = SmallVec<[Rect; 1]>;
#[derive(Default)] #[derive(Default, Clone)]
pub struct Region { pub struct Region {
rects: Container, rects: Container,
extents: Rect, extents: Rect,

View file

@ -8,43 +8,52 @@ use std::ops::Deref;
use std::rc::Rc; use std::rc::Rc;
impl Region { impl Region {
pub fn new(rect: Rect) -> Self { pub fn new(rect: Rect) -> Rc<Self> {
let mut rects = SmallVec::new(); let mut rects = SmallVec::new();
rects.push(rect); rects.push(rect);
Self { Rc::new(Self {
rects, rects,
extents: rect, extents: rect,
} })
} }
pub fn from_rects(rects: &[Rect]) -> Self { pub fn from_rects(rects: &[Rect]) -> Rc<Self> {
if rects.is_empty() { if rects.is_empty() {
return Self::default(); return Rc::new(Self::default());
} }
if rects.len() == 1 { if rects.len() == 1 {
return Self::new(rects[0]); return Self::new(rects[0]);
} }
let rects = rects_to_bands(rects); let rects = rects_to_bands(rects);
Self { Rc::new(Self {
extents: extents(&rects), extents: extents(&rects),
rects, rects,
} })
} }
pub fn union(&self, other: &Self) -> Self { pub fn union(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
if self.extents.is_empty() {
return other.clone();
}
if other.extents.is_empty() {
return self.clone();
}
let rects = op::<Union>(&self.rects, &other.rects); let rects = op::<Union>(&self.rects, &other.rects);
Self { Rc::new(Self {
rects, rects,
extents: self.extents.union(other.extents), extents: self.extents.union(other.extents),
} })
} }
pub fn subtract(&self, other: &Self) -> Self { pub fn subtract(self: &Rc<Self>, other: &Rc<Self>) -> Rc<Self> {
if self.extents.is_empty() || other.extents.is_empty() {
return self.clone();
}
let rects = op::<Subtract>(&self.rects, &other.rects); let rects = op::<Subtract>(&self.rects, &other.rects);
Self { Rc::new(Self {
extents: extents(&rects), extents: extents(&rects),
rects, rects,
} })
} }
pub fn extents(&self) -> Rect { pub fn extents(&self) -> Rect {
@ -562,10 +571,9 @@ impl RegionBuilder {
return; return;
} }
let region = Region::from_rects(&self.pending); let region = Region::from_rects(&self.pending);
let base = match self.op { self.base = match self.op {
BuilderOp::Add => self.base.union(&region), BuilderOp::Add => self.base.union(&region),
BuilderOp::Sub => self.base.subtract(&region), BuilderOp::Sub => self.base.subtract(&region),
}; };
self.base = Rc::new(base);
} }
} }