atagen

monolith

public
Star0
Watch0
e9e9b7ddae8d46e33b57af884eb8c3e5699cccf2
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
b563d2da1c80atagen2026-10-16 08:25 UTC29
    pub fn center_x(&self) -> f64 {
b563d2da1c80atagen2026-10-16 08:25 UTC30
        self.x + self.w / 2.0
b563d2da1c80atagen2026-10-16 08:25 UTC31
    }
b563d2da1c80atagen2026-10-16 08:25 UTC32
b563d2da1c80atagen2026-10-16 08:25 UTC33
    pub fn center_y(&self) -> f64 {
b563d2da1c80atagen2026-10-16 08:25 UTC34
        self.y + self.h / 2.0
b563d2da1c80atagen2026-10-16 08:25 UTC35
    }
b563d2da1c80atagen2026-10-16 08:25 UTC36
b563d2da1c80atagen2026-10-16 08:25 UTC37
    pub fn lerp(&self, other: &Rect, s: f64) -> Rect {
b563d2da1c80atagen2026-10-16 08:25 UTC38
        Rect::new(
b563d2da1c80atagen2026-10-16 08:25 UTC39
            self.x + (other.x - self.x) * s,
b563d2da1c80atagen2026-10-16 08:25 UTC40
            self.y + (other.y - self.y) * s,
b563d2da1c80atagen2026-10-16 08:25 UTC41
            self.w + (other.w - self.w) * s,
b563d2da1c80atagen2026-10-16 08:25 UTC42
            self.h + (other.h - self.h) * s,
b563d2da1c80atagen2026-10-16 08:25 UTC43
        )
b563d2da1c80atagen2026-10-16 08:25 UTC44
    }
b563d2da1c80atagen2026-10-16 08:25 UTC45
b563d2da1c80atagen2026-10-16 08:25 UTC46
    pub(crate) fn same_x(&self, o: &Rect) -> bool {
b563d2da1c80atagen2026-10-16 08:25 UTC47
        (self.x - o.x).abs() < TOL && (self.w - o.w).abs() < TOL
b563d2da1c80atagen2026-10-16 08:25 UTC48
    }
b563d2da1c80atagen2026-10-16 08:25 UTC49
b563d2da1c80atagen2026-10-16 08:25 UTC50
    pub(crate) fn same_y(&self, o: &Rect) -> bool {
b563d2da1c80atagen2026-10-16 08:25 UTC51
        (self.y - o.y).abs() < TOL && (self.h - o.h).abs() < TOL
b563d2da1c80atagen2026-10-16 08:25 UTC52
    }
b563d2da1c80atagen2026-10-16 08:25 UTC53
b563d2da1c80atagen2026-10-16 08:25 UTC54
    /// Takes the horizontal extent of `x_from` and the vertical extent of `y_from`.
b563d2da1c80atagen2026-10-16 08:25 UTC55
    pub(crate) fn mixed(x_from: &Rect, y_from: &Rect) -> Rect {
b563d2da1c80atagen2026-10-16 08:25 UTC56
        Rect::new(x_from.x, y_from.y, x_from.w, y_from.h)
b563d2da1c80atagen2026-10-16 08:25 UTC57
    }
b563d2da1c80atagen2026-10-16 08:25 UTC58
b563d2da1c80atagen2026-10-16 08:25 UTC59
    pub(crate) fn bounding<'a>(rects: impl IntoIterator<Item = &'a Rect>) -> Rect {
b563d2da1c80atagen2026-10-16 08:25 UTC60
        let (mut x0, mut y0, mut x1, mut y1) = (
b563d2da1c80atagen2026-10-16 08:25 UTC61
            f64::INFINITY,
b563d2da1c80atagen2026-10-16 08:25 UTC62
            f64::INFINITY,
b563d2da1c80atagen2026-10-16 08:25 UTC63
            f64::NEG_INFINITY,
b563d2da1c80atagen2026-10-16 08:25 UTC64
            f64::NEG_INFINITY,
b563d2da1c80atagen2026-10-16 08:25 UTC65
        );
b563d2da1c80atagen2026-10-16 08:25 UTC66
        for r in rects {
b563d2da1c80atagen2026-10-16 08:25 UTC67
            x0 = x0.min(r.x);
b563d2da1c80atagen2026-10-16 08:25 UTC68
            y0 = y0.min(r.y);
b563d2da1c80atagen2026-10-16 08:25 UTC69
            x1 = x1.max(r.right());
b563d2da1c80atagen2026-10-16 08:25 UTC70
            y1 = y1.max(r.bottom());
b563d2da1c80atagen2026-10-16 08:25 UTC71
        }
b563d2da1c80atagen2026-10-16 08:25 UTC72
        Rect::new(x0, y0, x1 - x0, y1 - y0)
b563d2da1c80atagen2026-10-16 08:25 UTC73
    }
b563d2da1c80atagen2026-10-16 08:25 UTC74
b563d2da1c80atagen2026-10-16 08:25 UTC75
    pub(crate) fn expanded(&self, by: f64) -> Rect {
b563d2da1c80atagen2026-10-16 08:25 UTC76
        Rect::new(
b563d2da1c80atagen2026-10-16 08:25 UTC77
            self.x - by,
b563d2da1c80atagen2026-10-16 08:25 UTC78
            self.y - by,
b563d2da1c80atagen2026-10-16 08:25 UTC79
            self.w + 2.0 * by,
b563d2da1c80atagen2026-10-16 08:25 UTC80
            self.h + 2.0 * by,
b563d2da1c80atagen2026-10-16 08:25 UTC81
        )
b563d2da1c80atagen2026-10-16 08:25 UTC82
    }
b563d2da1c80atagen2026-10-16 08:25 UTC83
b563d2da1c80atagen2026-10-16 08:25 UTC84
    pub(crate) fn intersects(&self, o: &Rect) -> bool {
b563d2da1c80atagen2026-10-16 08:25 UTC85
        self.x < o.right() && o.x < self.right() && self.y < o.bottom() && o.y < self.bottom()
b563d2da1c80atagen2026-10-16 08:25 UTC86
    }
b563d2da1c80atagen2026-10-16 08:25 UTC87
}
b563d2da1c80atagen2026-10-16 08:25 UTC88
b563d2da1c80atagen2026-10-16 08:25 UTC89
/// How a window changes between two keyframes.
b563d2da1c80atagen2026-10-16 08:25 UTC90
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
b563d2da1c80atagen2026-10-16 08:25 UTC91
pub(crate) struct Change {
b563d2da1c80atagen2026-10-16 08:25 UTC92
    pub x: bool,
b563d2da1c80atagen2026-10-16 08:25 UTC93
    pub y: bool,
b563d2da1c80atagen2026-10-16 08:25 UTC94
}
b563d2da1c80atagen2026-10-16 08:25 UTC95
b563d2da1c80atagen2026-10-16 08:25 UTC96
impl Change {
b563d2da1c80atagen2026-10-16 08:25 UTC97
    pub fn between(a: &Rect, b: &Rect) -> Self {
b563d2da1c80atagen2026-10-16 08:25 UTC98
        Self {
b563d2da1c80atagen2026-10-16 08:25 UTC99
            x: !a.same_x(b),
b563d2da1c80atagen2026-10-16 08:25 UTC100
            y: !a.same_y(b),
b563d2da1c80atagen2026-10-16 08:25 UTC101
        }
b563d2da1c80atagen2026-10-16 08:25 UTC102
    }
b563d2da1c80atagen2026-10-16 08:25 UTC103
b563d2da1c80atagen2026-10-16 08:25 UTC104
    pub fn moves(&self) -> bool {
b563d2da1c80atagen2026-10-16 08:25 UTC105
        self.x || self.y
b563d2da1c80atagen2026-10-16 08:25 UTC106
    }
b563d2da1c80atagen2026-10-16 08:25 UTC107
b563d2da1c80atagen2026-10-16 08:25 UTC108
    pub fn both(&self) -> bool {
b563d2da1c80atagen2026-10-16 08:25 UTC109
        self.x && self.y
b563d2da1c80atagen2026-10-16 08:25 UTC110
    }
b563d2da1c80atagen2026-10-16 08:25 UTC111
}