ori_core/layout/
rect.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
use std::ops::{Add, AddAssign, BitAnd, BitAndAssign, Sub, SubAssign};

use super::{Affine, Point, Size, Vector};

/// A rectangle defined by its minimum and maximum points.
#[derive(Clone, Copy, Debug, Default, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Rect {
    /// The minimum point of the rectangle.
    pub min: Point,

    /// The maximum point of the rectangle.
    pub max: Point,
}

impl Rect {
    /// A rectangle with zero area.
    pub const ZERO: Self = Self::new(Point::ZERO, Point::ZERO);

    /// Create a new rectangle with the given minimum and maximum points.
    pub const fn new(min: Point, max: Point) -> Self {
        Self { min, max }
    }

    /// Create a new rectangle with the given minimum point and size.
    pub fn min_size(min: Point, size: Size) -> Self {
        Self {
            min,
            max: min + size,
        }
    }

    /// Create a new rectangle with the given maximum point and size.
    pub fn max_size(max: Point, size: Size) -> Self {
        Self {
            min: max - size,
            max,
        }
    }

    /// Create a new rectangle with the given center point and size.
    pub fn center_size(center: Point, size: Size) -> Self {
        Self {
            min: center - size / 2.0,
            max: center + size / 2.0,
        }
    }

    /// Round the rectangle's minimum point down and its maximum point up.
    pub fn round(self) -> Self {
        Self {
            min: self.min.round(),
            max: self.max.round(),
        }
    }

    /// Clamp the rectangle to the given rectangle.
    pub fn clamp(self, other: impl Into<Self>) -> Self {
        let other = other.into();

        Self {
            min: self.min.clamp(other.min, other.max),
            max: self.max.clamp(other.min, other.max),
        }
    }

    /// Get the offset of the rectangle.
    pub fn offset(self) -> Vector {
        self.min.to_vector()
    }

    /// Get the size of the rectangle.
    pub fn size(self) -> Size {
        Size::from(self.max - self.min)
    }

    /// Get the width of the rectangle.
    pub fn width(self) -> f32 {
        self.max.x - self.min.x
    }

    /// Get the height of the rectangle.
    pub fn height(self) -> f32 {
        self.max.y - self.min.y
    }

    /// Get the area of the rectangle.
    pub fn area(self) -> f32 {
        self.width() * self.height()
    }

    /// Get the top left point of the rectangle.
    pub fn top_left(self) -> Point {
        self.min
    }

    /// Get the top center point of the rectangle.
    pub fn top_center(self) -> Point {
        Point::new(self.center().x, self.min.y)
    }

    /// Get the top right point of the rectangle.
    pub fn top_right(self) -> Point {
        Point::new(self.max.x, self.min.y)
    }

    /// Get the left center point of the rectangle.
    pub fn center_left(self) -> Point {
        Point::new(self.min.x, self.center().y)
    }

    /// Get the center point of the rectangle.
    pub fn center(self) -> Point {
        self.min + self.size() / 2.0
    }

    /// Get the right center point of the rectangle.
    pub fn center_right(self) -> Point {
        Point::new(self.max.x, self.center().y)
    }

    /// Get the bottom left point of the rectangle.
    pub fn bottom_left(self) -> Point {
        Point::new(self.min.x, self.max.y)
    }

    /// Get the bottom center point of the rectangle.
    pub fn bottom_center(self) -> Point {
        Point::new(self.center().x, self.max.y)
    }

    /// Get the bottom right point of the rectangle.
    pub fn bottom_right(self) -> Point {
        self.max
    }

    /// Get the top edge of the rectangle.
    pub fn top(self) -> f32 {
        self.min.y
    }

    /// Get the left edge of the rectangle.
    pub fn left(self) -> f32 {
        self.min.x
    }

    /// Get the right edge of the rectangle.
    pub fn right(self) -> f32 {
        self.max.x
    }

    /// Get the bottom edge of the rectangle.
    pub fn bottom(self) -> f32 {
        self.max.y
    }

    /// Check if the rectangle is finite.
    pub fn is_finite(self) -> bool {
        self.min.is_finite() && self.max.is_finite()
    }

    /// Check if the rectangle is infinite.
    pub fn is_infinite(self) -> bool {
        self.min.is_infinite() || self.max.is_infinite()
    }

    /// Shrink the rectangle by the given amount.
    pub fn shrink(self, padding: f32) -> Self {
        Self {
            min: self.min + Vector::new(padding, padding),
            max: self.max - Vector::new(padding, padding),
        }
    }

    /// Expand the rectangle by the given amount.
    pub fn expand(self, padding: f32) -> Self {
        self.shrink(-padding)
    }

    /// Compute whether the rectangle contains the given point.
    pub fn contains(self, point: Point) -> bool {
        let x = point.x >= self.min.x && point.x <= self.max.x;
        let y = point.y >= self.min.y && point.y <= self.max.y;
        x && y
    }

    /// Compute the closest point in the rectangle to the given point.
    pub fn contain(self, point: Point) -> Point {
        let x = point.x.max(self.min.x).min(self.max.x);
        let y = point.y.max(self.min.y).min(self.max.y);
        Point::new(x, y)
    }

    /// Expand the rectangle to contain the given point.
    pub fn include(self, point: Point) -> Self {
        let min_x = f32::min(self.min.x, point.x);
        let min_y = f32::min(self.min.y, point.y);
        let max_x = f32::max(self.max.x, point.x);
        let max_y = f32::max(self.max.y, point.y);

        Self {
            min: Point::new(min_x, min_y),
            max: Point::new(max_x, max_y),
        }
    }

    /// Compute the intersection of the rectangle with the given rectangle.
    pub fn try_intersection(self, other: Self) -> Option<Self> {
        let min_x = f32::max(self.min.x, other.min.x);
        let min_y = f32::max(self.min.y, other.min.y);
        let max_x = f32::min(self.max.x, other.max.x);
        let max_y = f32::min(self.max.y, other.max.y);

        if min_x <= max_x && min_y <= max_y {
            Some(Self {
                min: Point::new(min_x, min_y),
                max: Point::new(max_x, max_y),
            })
        } else {
            None
        }
    }

    /// Compute the intersection of the rectangle with the given rectangle.
    ///
    /// If the rectangles do not intersect [`Rect::ZERO`] is returned.
    pub fn intersection(self, other: Self) -> Self {
        self.try_intersection(other).unwrap_or(Self::ZERO)
    }

    /// Check if the rectangle intersects the given rectangle.
    pub fn intersects(self, other: Self) -> bool {
        self.try_intersection(other).is_some()
    }

    /// Compute the union of the rectangle with the given rectangle.
    pub fn union(self, other: Self) -> Self {
        let min_x = f32::min(self.min.x, other.min.x);
        let min_y = f32::min(self.min.y, other.min.y);
        let max_x = f32::max(self.max.x, other.max.x);
        let max_y = f32::max(self.max.y, other.max.y);

        Self {
            min: Point::new(min_x, min_y),
            max: Point::new(max_x, max_y),
        }
    }

    /// Transform the rectangle by the given affine transform.
    pub fn transform(self, transform: Affine) -> Self {
        let tl = transform * self.top_left();
        let tr = transform * self.top_right();
        let bl = transform * self.bottom_left();
        let br = transform * self.bottom_right();

        let min_x = f32::min(f32::min(tl.x, tr.x), f32::min(bl.x, br.x));
        let min_y = f32::min(f32::min(tl.y, tr.y), f32::min(bl.y, br.y));

        let max_x = f32::max(f32::max(tl.x, tr.x), f32::max(bl.x, br.x));
        let max_y = f32::max(f32::max(tl.y, tr.y), f32::max(bl.y, br.y));

        Self {
            min: Point::new(min_x, min_y),
            max: Point::new(max_x, max_y),
        }
    }
}

impl From<Size> for Rect {
    fn from(size: Size) -> Self {
        Self::new(Point::ZERO, size.into())
    }
}

impl From<[f32; 4]> for Rect {
    fn from([min_x, min_y, max_x, max_y]: [f32; 4]) -> Self {
        Self {
            min: Point::new(min_x, min_y),
            max: Point::new(max_x, max_y),
        }
    }
}

impl From<Rect> for [f32; 4] {
    fn from(rect: Rect) -> Self {
        [rect.min.x, rect.min.y, rect.max.x, rect.max.y]
    }
}

impl Add<Vector> for Rect {
    type Output = Self;

    fn add(self, rhs: Vector) -> Self::Output {
        Self {
            min: self.min + rhs,
            max: self.max + rhs,
        }
    }
}

impl Add<Size> for Rect {
    type Output = Self;

    fn add(self, rhs: Size) -> Self::Output {
        Self {
            min: self.min,
            max: self.max + rhs,
        }
    }
}

impl Sub<Vector> for Rect {
    type Output = Self;

    fn sub(self, rhs: Vector) -> Self::Output {
        Self {
            min: self.min - rhs,
            max: self.max - rhs,
        }
    }
}

impl Sub<Size> for Rect {
    type Output = Self;

    fn sub(self, rhs: Size) -> Self::Output {
        Self {
            min: self.min,
            max: self.max - rhs,
        }
    }
}

impl AddAssign<Vector> for Rect {
    fn add_assign(&mut self, rhs: Vector) {
        self.min += rhs;
        self.max += rhs;
    }
}

impl AddAssign<Size> for Rect {
    fn add_assign(&mut self, rhs: Size) {
        self.max += rhs;
    }
}

impl SubAssign<Vector> for Rect {
    fn sub_assign(&mut self, rhs: Vector) {
        self.min -= rhs;
        self.max -= rhs;
    }
}

impl SubAssign<Size> for Rect {
    fn sub_assign(&mut self, rhs: Size) {
        self.max -= rhs;
    }
}

impl BitAnd for Rect {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        self.intersection(rhs)
    }
}

impl BitAndAssign for Rect {
    fn bitand_assign(&mut self, rhs: Self) {
        *self = self.intersection(rhs);
    }
}