Skip to main content

rouge_render/
geometry.rs

1use std::ops::{Add, Sub};
2
3#[derive(Clone, Copy, Debug, Default, PartialEq)]
4pub struct Point {
5    pub x: f32,
6    pub y: f32,
7}
8
9impl Point {
10    pub const ZERO: Point = Point { x: 0.0, y: 0.0 };
11
12    pub const fn new(x: f32, y: f32) -> Self {
13        Point { x, y }
14    }
15
16    pub const fn to_array(self) -> [f32; 2] {
17        [self.x, self.y]
18    }
19}
20
21impl Add for Point {
22    type Output = Point;
23    fn add(self, rhs: Point) -> Point {
24        Point::new(self.x + rhs.x, self.y + rhs.y)
25    }
26}
27
28impl Sub for Point {
29    type Output = Point;
30    fn sub(self, rhs: Point) -> Point {
31        Point::new(self.x - rhs.x, self.y - rhs.y)
32    }
33}
34
35#[derive(Clone, Copy, Debug, Default, PartialEq)]
36pub struct Size {
37    pub width: f32,
38    pub height: f32,
39}
40
41impl Size {
42    pub const ZERO: Size = Size {
43        width: 0.0,
44        height: 0.0,
45    };
46
47    pub const fn new(width: f32, height: f32) -> Self {
48        Size { width, height }
49    }
50}
51
52#[derive(Clone, Copy, Debug, Default, PartialEq)]
53pub struct Rect {
54    pub origin: Point,
55    pub size: Size,
56}
57
58impl Rect {
59    pub const ZERO: Rect = Rect::new(0.0, 0.0, 0.0, 0.0);
60    pub const EVERYTHING: Rect = Rect::new(-1.0e7, -1.0e7, 2.0e7, 2.0e7);
61
62    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
63        Rect {
64            origin: Point::new(x, y),
65            size: Size::new(width, height),
66        }
67    }
68
69    pub fn from_min_max(min: Point, max: Point) -> Self {
70        Rect::new(
71            min.x,
72            min.y,
73            (max.x - min.x).max(0.0),
74            (max.y - min.y).max(0.0),
75        )
76    }
77
78    pub fn max(&self) -> Point {
79        Point::new(
80            self.origin.x + self.size.width,
81            self.origin.y + self.size.height,
82        )
83    }
84
85    pub fn center(&self) -> Point {
86        Point::new(
87            self.origin.x + self.size.width * 0.5,
88            self.origin.y + self.size.height * 0.5,
89        )
90    }
91
92    pub fn is_empty(&self) -> bool {
93        self.size.width <= 0.0 || self.size.height <= 0.0
94    }
95
96    pub fn contains(&self, point: Point) -> bool {
97        let max = self.max();
98        point.x >= self.origin.x && point.y >= self.origin.y && point.x < max.x && point.y < max.y
99    }
100
101    pub fn intersect(&self, other: &Rect) -> Rect {
102        let (a, b) = (self.max(), other.max());
103        Rect::from_min_max(
104            Point::new(
105                self.origin.x.max(other.origin.x),
106                self.origin.y.max(other.origin.y),
107            ),
108            Point::new(a.x.min(b.x), a.y.min(b.y)),
109        )
110    }
111
112    pub fn union(&self, other: &Rect) -> Rect {
113        let (a, b) = (self.max(), other.max());
114        Rect::from_min_max(
115            Point::new(
116                self.origin.x.min(other.origin.x),
117                self.origin.y.min(other.origin.y),
118            ),
119            Point::new(a.x.max(b.x), a.y.max(b.y)),
120        )
121    }
122
123    pub fn translate(&self, by: Point) -> Rect {
124        Rect {
125            origin: self.origin + by,
126            size: self.size,
127        }
128    }
129
130    pub fn scale(&self, factor: f32) -> Rect {
131        Rect::new(
132            self.origin.x * factor,
133            self.origin.y * factor,
134            self.size.width * factor,
135            self.size.height * factor,
136        )
137    }
138
139    pub fn inflate(&self, amount: f32) -> Rect {
140        Rect::new(
141            self.origin.x - amount,
142            self.origin.y - amount,
143            (self.size.width + amount * 2.0).max(0.0),
144            (self.size.height + amount * 2.0).max(0.0),
145        )
146    }
147
148    pub fn to_array(self) -> [f32; 4] {
149        [
150            self.origin.x,
151            self.origin.y,
152            self.size.width,
153            self.size.height,
154        ]
155    }
156}
157
158#[derive(Clone, Copy, Debug, Default, PartialEq)]
159pub struct Corners {
160    pub top_left: f32,
161    pub top_right: f32,
162    pub bottom_right: f32,
163    pub bottom_left: f32,
164}
165
166impl Corners {
167    pub const ZERO: Corners = Corners::all(0.0);
168
169    pub const fn all(value: f32) -> Self {
170        Corners {
171            top_left: value,
172            top_right: value,
173            bottom_right: value,
174            bottom_left: value,
175        }
176    }
177
178    pub fn map(&self, f: impl Fn(f32) -> f32) -> Corners {
179        Corners {
180            top_left: f(self.top_left),
181            top_right: f(self.top_right),
182            bottom_right: f(self.bottom_right),
183            bottom_left: f(self.bottom_left),
184        }
185    }
186
187    pub fn to_array(self) -> [f32; 4] {
188        [
189            self.top_left,
190            self.top_right,
191            self.bottom_right,
192            self.bottom_left,
193        ]
194    }
195}
196
197#[derive(Clone, Copy, Debug, Default, PartialEq)]
198pub struct Edges {
199    pub top: f32,
200    pub right: f32,
201    pub bottom: f32,
202    pub left: f32,
203}
204
205impl Edges {
206    pub const ZERO: Edges = Edges::all(0.0);
207
208    pub const fn all(value: f32) -> Self {
209        Edges {
210            top: value,
211            right: value,
212            bottom: value,
213            left: value,
214        }
215    }
216
217    pub fn is_zero(&self) -> bool {
218        *self == Edges::ZERO
219    }
220
221    pub fn to_array(self) -> [f32; 4] {
222        [self.top, self.right, self.bottom, self.left]
223    }
224}
225
226#[derive(Clone, Copy, Debug, PartialEq)]
227pub struct Affine {
228    pub a: f32,
229    pub b: f32,
230    pub c: f32,
231    pub d: f32,
232    pub tx: f32,
233    pub ty: f32,
234}
235
236impl Default for Affine {
237    fn default() -> Self {
238        Affine::IDENTITY
239    }
240}
241
242impl Affine {
243    pub const IDENTITY: Affine = Affine {
244        a: 1.0,
245        b: 0.0,
246        c: 0.0,
247        d: 1.0,
248        tx: 0.0,
249        ty: 0.0,
250    };
251
252    pub const fn translate(x: f32, y: f32) -> Self {
253        Affine {
254            tx: x,
255            ty: y,
256            ..Affine::IDENTITY
257        }
258    }
259
260    pub const fn scale(x: f32, y: f32) -> Self {
261        Affine {
262            a: x,
263            d: y,
264            ..Affine::IDENTITY
265        }
266    }
267
268    pub fn rotate(radians: f32) -> Self {
269        let (sin, cos) = radians.sin_cos();
270        Affine {
271            a: cos,
272            b: sin,
273            c: -sin,
274            d: cos,
275            tx: 0.0,
276            ty: 0.0,
277        }
278    }
279
280    pub fn then(&self, next: &Affine) -> Affine {
281        Affine {
282            a: next.a * self.a + next.c * self.b,
283            b: next.b * self.a + next.d * self.b,
284            c: next.a * self.c + next.c * self.d,
285            d: next.b * self.c + next.d * self.d,
286            tx: next.a * self.tx + next.c * self.ty + next.tx,
287            ty: next.b * self.tx + next.d * self.ty + next.ty,
288        }
289    }
290
291    pub fn about(&self, center: Point) -> Affine {
292        Affine::translate(-center.x, -center.y)
293            .then(self)
294            .then(&Affine::translate(center.x, center.y))
295    }
296
297    pub fn apply(&self, p: Point) -> Point {
298        Point::new(
299            self.a * p.x + self.c * p.y + self.tx,
300            self.b * p.x + self.d * p.y + self.ty,
301        )
302    }
303
304    pub fn apply_rect(&self, rect: &Rect) -> Rect {
305        let (min, max) = (rect.origin, rect.max());
306        let corners =
307            [min, Point::new(max.x, min.y), max, Point::new(min.x, max.y)].map(|p| self.apply(p));
308        let fold = |f: fn(f32, f32) -> f32, start: f32| {
309            corners.iter().fold(Point::new(start, start), |acc, p| {
310                Point::new(f(acc.x, p.x), f(acc.y, p.y))
311            })
312        };
313        Rect::from_min_max(
314            fold(f32::min, f32::INFINITY),
315            fold(f32::max, f32::NEG_INFINITY),
316        )
317    }
318
319    pub fn inverse(&self) -> Option<Affine> {
320        let det = self.a * self.d - self.b * self.c;
321        if det.abs() < 1e-9 {
322            return None;
323        }
324        let (a, b, c, d) = (self.d / det, -self.b / det, -self.c / det, self.a / det);
325        Some(Affine {
326            a,
327            b,
328            c,
329            d,
330            tx: -(a * self.tx + c * self.ty),
331            ty: -(b * self.tx + d * self.ty),
332        })
333    }
334}