atagen

monolith

public
Star0
Watch0
0119c7cadaf5ac25cb47eec5241f535ed52d7ee7
1
ssh://brewery@pls.lobotomise.me:33/atagen/monolith.git

Blame: crates/planner/src/geom.rs

b563d2da1c80atagen2026-10-16 08:25 UTC1
#[derive(Clone, Copy, Debug, PartialEq, Default)]
b563d2da1c80atagen2026-10-16 08:25 UTC2
pub struct Rect {
b563d2da1c80atagen2026-10-16 08:25 UTC3
    pub x: f64,
b563d2da1c80atagen2026-10-16 08:25 UTC4
    pub y: f64,
b563d2da1c80atagen2026-10-16 08:25 UTC5
    pub w: f64,
b563d2da1c80atagen2026-10-16 08:25 UTC6
    pub h: f64,
b563d2da1c80atagen2026-10-16 08:25 UTC7
}
b563d2da1c80atagen2026-10-16 08:25 UTC8
b563d2da1c80atagen2026-10-16 08:25 UTC9
pub(crate) const TOL: f64 = 0.05;
b563d2da1c80atagen2026-10-16 08:25 UTC10
b563d2da1c80atagen2026-10-16 08:25 UTC11
impl Rect {
b563d2da1c80atagen2026-10-16 08:25 UTC12
    pub const fn new(x: f64, y: f64, w: f64, h: f64) -> Self {
b563d2da1c80atagen2026-10-16 08:25 UTC13
        Self { x, y, w, h }
b563d2da1c80atagen2026-10-16 08:25 UTC14
    }
b563d2da1c80atagen2026-10-16 08:25 UTC15
b563d2da1c80atagen2026-10-16 08:25 UTC16
    pub fn right(&self) -> f64 {
b563d2da1c80atagen2026-10-16 08:25 UTC17
        self.x + self.w
b563d2da1c80atagen2026-10-16 08:25 UTC18
    }
b563d2da1c80atagen2026-10-16 08:25 UTC19
b563d2da1c80atagen2026-10-16 08:25 UTC20
    pub fn bottom(&self) -> f64 {
b563d2da1c80atagen2026-10-16 08:25 UTC21
        self.y + self.h
b563d2da1c80atagen2026-10-16 08:25 UTC22
    }
b563d2da1c80atagen2026-10-16 08:25 UTC23
b22c0511d5afatagen2026-10-17 03:48 UTC24
    /// Whether the point lies inside, counting the left and top edges only.
b22c0511d5afatagen2026-10-17 03:48 UTC25
    pub fn contains(&self, x: f64, y: f64) -> bool {
b22c0511d5afatagen2026-10-17 03:48 UTC26
        x >= self.x && y >= self.y && x < self.right() && y < self.bottom()
b22c0511d5afatagen2026-10-17 03:48 UTC27
    }
b22c0511d5afatagen2026-10-17 03:48 UTC28
9e5c356ebe9eatagen2026-10-17 05:10 UTC29
    pub fn union(&self, other: &Rect) -> Rect {
9e5c356ebe9eatagen2026-10-17 05:10 UTC30
        let (x, y) = (self.x.min(other.x), self.y.min(other.y));
9e5c356ebe9eatagen2026-10-17 05:10 UTC31
        let r = self.right().max(other.right());
9e5c356ebe9eatagen2026-10-17 05:10 UTC32
        let b = self.bottom().max(other.bottom());
9e5c356ebe9eatagen2026-10-17 05:10 UTC33
        Rect::new(x, y, r - x, b - y)
9e5c356ebe9eatagen2026-10-17 05:10 UTC34
    }
9e5c356ebe9eatagen2026-10-17 05:10 UTC35
b563d2da1c80atagen2026-10-16 08:25 UTC36
    pub fn center_x(&self) -> f64 {
b563d2da1c80atagen2026-10-16 08:25 UTC37
        self.x + self.w / 2.0
b563d2da1c80atagen2026-10-16 08:25 UTC38
    }
b563d2da1c80atagen2026-10-16 08:25 UTC39
b563d2da1c80atagen2026-10-16 08:25 UTC40
    pub fn center_y(&self) -> f64 {
b563d2da1c80atagen2026-10-16 08:25 UTC41
        self.y + self.h / 2.0
b563d2da1c80atagen2026-10-16 08:25 UTC42
    }
b563d2da1c80atagen2026-10-16 08:25 UTC43
b563d2da1c80atagen2026-10-16 08:25 UTC44
    pub fn lerp(&self, other: &Rect, s: f64) -> Rect {
b563d2da1c80atagen2026-10-16 08:25 UTC45
        Rect::new(
b563d2da1c80atagen2026-10-16 08:25 UTC46
            self.x + (other.x - self.x) * s,
b563d2da1c80atagen2026-10-16 08:25 UTC47
            self.y + (other.y - self.y) * s,
b563d2da1c80atagen2026-10-16 08:25 UTC48
            self.w + (other.w - self.w) * s,
b563d2da1c80atagen2026-10-16 08:25 UTC49
            self.h + (other.h - self.h) * s,
b563d2da1c80atagen2026-10-16 08:25 UTC50
        )
b563d2da1c80atagen2026-10-16 08:25 UTC51
    }
b563d2da1c80atagen2026-10-16 08:25 UTC52
b563d2da1c80atagen2026-10-16 08:25 UTC53
    pub(crate) fn same_x(&self, o: &Rect) -> bool {
b563d2da1c80atagen2026-10-16 08:25 UTC54
        (self.x - o.x).abs() < TOL && (self.w - o.w).abs() < TOL
b563d2da1c80atagen2026-10-16 08:25 UTC55
    }
b563d2da1c80atagen2026-10-16 08:25 UTC56
b563d2da1c80atagen2026-10-16 08:25 UTC57
    pub(crate) fn same_y(&self, o: &Rect) -> bool {
b563d2da1c80atagen2026-10-16 08:25 UTC58
        (self.y - o.y).abs() < TOL && (self.h - o.h).abs() < TOL
b563d2da1c80atagen2026-10-16 08:25 UTC59
    }
b563d2da1c80atagen2026-10-16 08:25 UTC60
b563d2da1c80atagen2026-10-16 08:25 UTC61
    /// Takes the horizontal extent of `x_from` and the vertical extent of `y_from`.
b563d2da1c80atagen2026-10-16 08:25 UTC62
    pub(crate) fn mixed(x_from: &Rect, y_from: &Rect) -> Rect {
b563d2da1c80atagen2026-10-16 08:25 UTC63
        Rect::new(x_from.x, y_from.y, x_from.w, y_from.h)
b563d2da1c80atagen2026-10-16 08:25 UTC64
    }
b563d2da1c80atagen2026-10-16 08:25 UTC65
b563d2da1c80atagen2026-10-16 08:25 UTC66
    pub(crate) fn bounding<'a>(rects: impl IntoIterator<Item = &'a Rect>) -> Rect {
b563d2da1c80atagen2026-10-16 08:25 UTC67
        let (mut x0, mut y0, mut x1, mut y1) = (
b563d2da1c80atagen2026-10-16 08:25 UTC68
            f64::INFINITY,
b563d2da1c80atagen2026-10-16 08:25 UTC69
            f64::INFINITY,
b563d2da1c80atagen2026-10-16 08:25 UTC70
            f64::NEG_INFINITY,
b563d2da1c80atagen2026-10-16 08:25 UTC71
            f64::NEG_INFINITY,
b563d2da1c80atagen2026-10-16 08:25 UTC72
        );
b563d2da1c80atagen2026-10-16 08:25 UTC73
        for r in rects {
b563d2da1c80atagen2026-10-16 08:25 UTC74
            x0 = x0.min(r.x);
b563d2da1c80atagen2026-10-16 08:25 UTC75
            y0 = y0.min(r.y);
b563d2da1c80atagen2026-10-16 08:25 UTC76
            x1 = x1.max(r.right());
b563d2da1c80atagen2026-10-16 08:25 UTC77
            y1 = y1.max(r.bottom());
b563d2da1c80atagen2026-10-16 08:25 UTC78
        }
b563d2da1c80atagen2026-10-16 08:25 UTC79
        Rect::new(x0, y0, x1 - x0, y1 - y0)
b563d2da1c80atagen2026-10-16 08:25 UTC80
    }
b563d2da1c80atagen2026-10-16 08:25 UTC81
b563d2da1c80atagen2026-10-16 08:25 UTC82
    pub(crate) fn expanded(&self, by: f64) -> Rect {
b563d2da1c80atagen2026-10-16 08:25 UTC83
        Rect::new(
b563d2da1c80atagen2026-10-16 08:25 UTC84
            self.x - by,
b563d2da1c80atagen2026-10-16 08:25 UTC85
            self.y - by,
b563d2da1c80atagen2026-10-16 08:25 UTC86
            self.w + 2.0 * by,
b563d2da1c80atagen2026-10-16 08:25 UTC87
            self.h + 2.0 * by,
b563d2da1c80atagen2026-10-16 08:25 UTC88
        )
b563d2da1c80atagen2026-10-16 08:25 UTC89
    }
b563d2da1c80atagen2026-10-16 08:25 UTC90
b563d2da1c80atagen2026-10-16 08:25 UTC91
    pub(crate) fn intersects(&self, o: &Rect) -> bool {
b563d2da1c80atagen2026-10-16 08:25 UTC92
        self.x < o.right() && o.x < self.right() && self.y < o.bottom() && o.y < self.bottom()
b563d2da1c80atagen2026-10-16 08:25 UTC93
    }
b563d2da1c80atagen2026-10-16 08:25 UTC94
}
b563d2da1c80atagen2026-10-16 08:25 UTC95
b563d2da1c80atagen2026-10-16 08:25 UTC96
/// How a window changes between two keyframes.
b563d2da1c80atagen2026-10-16 08:25 UTC97
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
b563d2da1c80atagen2026-10-16 08:25 UTC98
pub(crate) struct Change {
b563d2da1c80atagen2026-10-16 08:25 UTC99
    pub x: bool,
b563d2da1c80atagen2026-10-16 08:25 UTC100
    pub y: bool,
b563d2da1c80atagen2026-10-16 08:25 UTC101
}
b563d2da1c80atagen2026-10-16 08:25 UTC102
b563d2da1c80atagen2026-10-16 08:25 UTC103
impl Change {
b563d2da1c80atagen2026-10-16 08:25 UTC104
    pub fn between(a: &Rect, b: &Rect) -> Self {
b563d2da1c80atagen2026-10-16 08:25 UTC105
        Self {
b563d2da1c80atagen2026-10-16 08:25 UTC106
            x: !a.same_x(b),
b563d2da1c80atagen2026-10-16 08:25 UTC107
            y: !a.same_y(b),
b563d2da1c80atagen2026-10-16 08:25 UTC108
        }
b563d2da1c80atagen2026-10-16 08:25 UTC109
    }
b563d2da1c80atagen2026-10-16 08:25 UTC110
b563d2da1c80atagen2026-10-16 08:25 UTC111
    pub fn moves(&self) -> bool {
b563d2da1c80atagen2026-10-16 08:25 UTC112
        self.x || self.y
b563d2da1c80atagen2026-10-16 08:25 UTC113
    }
b563d2da1c80atagen2026-10-16 08:25 UTC114
b563d2da1c80atagen2026-10-16 08:25 UTC115
    pub fn both(&self) -> bool {
b563d2da1c80atagen2026-10-16 08:25 UTC116
        self.x && self.y
b563d2da1c80atagen2026-10-16 08:25 UTC117
    }
b563d2da1c80atagen2026-10-16 08:25 UTC118
}