ponsic-types 0.1.1

The dependency of the `ponsic` crate.
Documentation
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
use super::{Point, Size};
use std::ops::{Add, AddAssign, Div, Sub};

/// 矩形结构体
///
/// # Note
/// 若启用`perf`feature,所有方法都将不会进行标准化检查,以提高性能,
/// 该情况下,如有必要,需手动进行标准化检查,以保证方法计算结果的正确性
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Rect<T: Copy> {
    left: T,
    top: T,
    right: T,
    bottom: T,
}

impl<T: Copy + Ord> Rect<T> {
    /// 创建一个新的矩形
    ///
    /// # Example
    /// ```
    /// use ponsic_types::Rect;
    /// let rect = Rect::new(1, 2, 3, 4);
    /// ```
    #[cfg(not(feature = "perf"))]
    #[inline]
    pub fn new(left: T, top: T, right: T, bottom: T) -> Self {
        let mut res = Self {
            left,
            top,
            right,
            bottom,
        };
        res.normalize();
        res
    }
}

impl<T: Copy> Rect<T> {
    /// 创建一个新的矩形
    ///
    /// # Example
    /// ```
    /// use wy_core::Rect;
    /// let rect = Rect::new(1, 2, 3, 4);
    /// ```
    #[cfg(feature = "perf")]
    #[inline]
    pub fn new(left: T, top: T, right: T, bottom: T) -> Self {
        Self {
            left,
            top,
            right,
            bottom,
        }
    }

    /// 返回矩形的左边界坐标
    ///
    /// # Note
    /// 若矩形不是标准化的,该函数可能不会返回实际的左边界坐标,
    /// 若要标准化矩形,请使用 \[`normalize()`\]
    #[inline]
    pub fn left(&self) -> T {
        self.left
    }

    /// 返回矩形的上边界坐标
    ///
    /// # Note
    /// 若矩形不是标准化的,该函数可能不会返回实际的上边界坐标,
    /// 若要标准化矩形,请使用 \[`normalize()`\]
    #[inline]
    pub fn top(&self) -> T {
        self.top
    }

    /// 返回矩形的右边界坐标
    ///
    /// # Note
    /// 若矩形不是标准化的,该函数可能不会返回实际的右边界坐标,
    /// 若要标准化矩形,请使用 \[`normalize()`\]
    #[inline]
    pub fn right(&self) -> T {
        self.right
    }

    /// 返回矩形的下边界坐标
    ///
    /// # Note
    /// 若矩形不是标准化的,该函数可能不会返回实际的下边界坐标,
    /// 若要标准化矩形,请使用 \[`normalize()`\]
    #[inline]
    pub fn bottom(&self) -> T {
        self.bottom
    }

    /// 返回矩形的左上角坐标
    ///
    /// # Note
    /// 若矩形不是标准化的,该函数可能不会返回实际的左上角坐标,
    /// 若要标准化矩形,请使用 \[`normalize()`\]
    #[inline]
    pub fn left_top(&self) -> Point<T> {
        Point::new(self.left, self.top)
    }

    /// 返回矩形的右上角坐标
    ///
    /// # Note
    /// 若矩形不是标准化的,该函数可能不会返回实际的右上角坐标,
    /// 若要标准化矩形,请使用 \[`normalize()`\]
    #[inline]
    pub fn right_top(&self) -> Point<T> {
        Point::new(self.right, self.top)
    }

    /// 返回矩形的左下角坐标
    ///
    /// # Note
    /// 若矩形不是标准化的,该函数可能不会返回实际的左下角坐标,
    /// 若要标准化矩形,请使用 \[`normalize()`\]
    #[inline]
    pub fn left_bottom(&self) -> Point<T> {
        Point::new(self.left, self.bottom)
    }

    /// 返回矩形的右下角坐标
    ///
    /// # Note
    /// 若矩形不是标准化的,该函数可能不会返回实际的右下角坐标,
    /// 若要标准化矩形,请使用 \[`normalize()`\]
    #[inline]
    pub fn right_bottom(&self) -> Point<T> {
        Point::new(self.right, self.bottom)
    }
}

// 启用`perf`feature时,实现以下方法
// 这些方法不进行标准化检查,以提高性能
#[cfg(feature = "perf")]
impl<T: Copy> Rect<T> {
    /// 设置矩形的左边界坐标
    #[inline]
    pub fn set_left(&mut self, left: T) {
        self.left = left;
    }

    /// 设置矩形的上边界坐标
    #[inline]
    pub fn set_top(&mut self, top: T) {
        self.top = top;
    }

    /// 设置矩形的右边界坐标
    #[inline]
    pub fn set_right(&mut self, right: T) {
        self.right = right;
    }

    /// 设置矩形的下边界坐标
    #[inline]
    pub fn set_bottom(&mut self, bottom: T) {
        self.bottom = bottom;
    }

    /// 设置矩形的左上角坐标
    #[inline]
    pub fn set_left_top(&mut self, left: T, top: T) {
        self.left = left;
        self.top = top;
    }

    /// 设置矩形的右上角坐标
    #[inline]
    pub fn set_right_top(&mut self, right: T, top: T) {
        self.right = right;
        self.top = top;
    }

    /// 设置矩形的左下角坐标
    #[inline]
    pub fn set_left_bottom(&mut self, left: T, bottom: T) {
        self.left = left;
        self.bottom = bottom;
    }

    /// 设置矩形的右下角坐标
    #[inline]
    pub fn set_right_bottom(&mut self, right: T, bottom: T) {
        self.right = right;
        self.bottom = bottom;
    }
}

/// 若启用`perf`feature,以下方法将不会进行标准化检查,以提高性能
#[cfg(not(feature = "perf"))]
impl<T: Copy + Ord> Rect<T> {
    /// 设置矩形的左边界坐标
    #[inline]
    pub fn set_left(&mut self, left: T) {
        self.left = left;
        self.normalize();
    }

    /// 设置矩形的上边界坐标
    #[inline]
    pub fn set_top(&mut self, top: T) {
        self.top = top;
        self.normalize();
    }

    /// 设置矩形的右边界坐标
    #[inline]
    pub fn set_right(&mut self, right: T) {
        self.right = right;
        self.normalize();
    }

    /// 设置矩形的下边界坐标
    #[inline]
    pub fn set_bottom(&mut self, bottom: T) {
        self.bottom = bottom;
        self.normalize();
    }

    /// 设置矩形的左上角坐标
    #[inline]
    pub fn set_left_top(&mut self, left: T, top: T) {
        self.left = left;
        self.top = top;
        self.normalize();
    }

    /// 设置矩形的右上角坐标
    #[inline]
    pub fn set_right_top(&mut self, right: T, top: T) {
        self.right = right;
        self.top = top;
        self.normalize();
    }

    /// 设置矩形的左下角坐标
    #[inline]
    pub fn set_left_bottom(&mut self, left: T, bottom: T) {
        self.left = left;
        self.bottom = bottom;
        self.normalize();
    }

    /// 设置矩形的右下角坐标
    #[inline]
    pub fn set_right_bottom(&mut self, right: T, bottom: T) {
        self.right = right;
        self.bottom = bottom;
        self.normalize();
    }
}

impl<T: Copy + Sub<Output = T>> Rect<T> {
    /// 返回矩形的宽度
    ///
    /// # Note
    /// 若矩形不是标准化的,该函数可能会返回负值,
    /// 若要标准化矩形,请使用 \[`normalize()`\]
    #[inline]
    pub fn width(&self) -> T {
        self.right - self.left
    }

    /// 返回矩形的高度
    ///
    /// # Note
    /// 若矩形不是标准化的,该函数可能会返回负值,
    /// 若要标准化矩形,请使用 \[`normalize()`\]
    #[inline]
    pub fn height(&self) -> T {
        self.bottom - self.top
    }

    /// 返回矩形的尺寸
    ///
    /// # Note
    /// 若矩形不是标准化的,该函数返回值可能包含负值,
    /// 若要标准化矩形,请使用 \[`normalize()`\]
    #[inline]
    pub fn size(&self) -> Size<T> {
        Size::new(self.width(), self.height())
    }
}

impl<T: Copy + Ord> Rect<T> {
    /// 判断矩形是否标准
    ///
    /// # Note
    /// 若矩形不是标准的,可以通过 \[`normalize()`\] 进行标准化
    #[cfg(feature = "perf")]
    #[inline]
    pub fn is_normalized(&self) -> bool {
        self.left <= self.right && self.top <= self.bottom
    }

    /// 将矩形标准化
    #[cfg(feature = "perf")]
    #[inline]
    pub fn normalize(&mut self) {
        if self.is_normalized() {
            return;
        }
        if self.left > self.right {
            std::mem::swap(&mut self.left, &mut self.right);
        }
        if self.top > self.bottom {
            std::mem::swap(&mut self.top, &mut self.bottom);
        }
    }

    #[allow(dead_code)]
    #[cfg(not(feature = "perf"))]
    #[inline]
    fn is_normalized(&self) -> bool {
        self.left <= self.right && self.top <= self.bottom
    }

    #[allow(dead_code)]
    #[cfg(not(feature = "perf"))]
    #[inline]
    fn normalize(&mut self) {
        if self.is_normalized() {
            return;
        }
        if self.left > self.right {
            std::mem::swap(&mut self.left, &mut self.right);
        }
        if self.top > self.bottom {
            std::mem::swap(&mut self.top, &mut self.bottom);
        }
    }

    /// 判断点是否在矩形区域内
    ///
    /// # Note
    /// 如果点在矩形的边界上,此函数也会返回 `false`;
    /// 如果需要包含点在矩形边界上的情况,请使用 \[`contains_with_bound()`\]
    #[inline]
    pub fn contains(&self, point: Point<T>) -> bool {
        point.x > self.left && point.x < self.right && point.y > self.top && point.y < self.bottom
    }

    /// 判断点是否在矩形区域内
    ///
    /// # Note
    /// 如果点在矩形的边界上,此函数也会返回 `true`;
    /// 如果需要排除点在矩形边界上的情况,请使用 \[`contains_with_bound()`\]
    #[inline]
    pub fn contains_with_bound(&self, point: Point<T>) -> bool {
        point.x >= self.left
            && point.x <= self.right
            && point.y >= self.top
            && point.y <= self.bottom
    }

    /// 判断此矩形与另一个矩形是否存在交集
    ///
    /// 如果另一个矩形与此矩形存在公共区域,则返回 `true`
    #[inline]
    pub fn intersects(&self, other: &Rect<T>) -> bool {
        self.left <= other.right
            && self.top <= other.bottom
            && self.right >= other.left
            && self.bottom >= other.top
    }

    /// 计算两个矩形的交集
    ///
    /// 此函数会计算两个矩形的重叠部分,并返回一个包含该信息的新矩形
    ///
    /// # Note
    /// 若两个矩形没有交集,则返回 `None`
    #[inline]
    pub fn intersected(&self, other: &Self) -> Option<Self> {
        if self.intersects(other) {
            Some(Self {
                left: self.left.max(other.left),
                top: self.top.max(other.top),
                right: self.right.min(other.right),
                bottom: self.bottom.min(other.bottom),
            })
        } else {
            None
        }
    }

    /// 计算两个矩形的并集
    ///
    /// 此函数会计算两个矩形所在位置的最小轮廓矩形,并返回一个包含该信息的新矩形
    #[inline]
    pub fn united(&self, other: &Rect<T>) -> Rect<T> {
        Self {
            left: self.left.min(other.left),
            top: self.top.min(other.top),
            right: self.right.max(other.right),
            bottom: self.bottom.max(other.bottom),
        }
    }
}

#[cfg(feature = "perf")]
impl<T: Copy + AddAssign> Rect<T> {
    /// 调整矩形的位置
    #[inline]
    pub fn adjust(&mut self, left: T, top: T, right: T, bottom: T) {
        self.left += left;
        self.top += top;
        self.right += right;
        self.bottom += bottom;
    }
}

#[cfg(not(feature = "perf"))]
impl<T: Copy + AddAssign + Ord> Rect<T> {
    /// 调整矩形的位置
    #[inline]
    pub fn adjust(&mut self, left: T, top: T, right: T, bottom: T) {
        self.left += left;
        self.top += top;
        self.right += right;
        self.bottom += bottom;
        self.normalize();
    }
}

impl<T: Copy + Add<Output = T> + Div<i32, Output = T>> Rect<T> {
    /// 返回矩形的中心点坐标
    #[inline]
    pub fn center(&self) -> Point<T> {
        Point::new((self.right + self.left) / 2, (self.bottom + self.top) / 2)
    }
}

impl<T: Copy + Add<Output = T>> From<(Point<T>, Size<T>)> for Rect<T> {
    #[inline]
    fn from((pos, size): (Point<T>, Size<T>)) -> Self {
        Self {
            left: pos.x,
            top: pos.y,
            right: pos.x + size.width,
            bottom: pos.y + size.height,
        }
    }
}

mod operator_overload {
    use super::*;

    use std::ops::BitAnd;
    impl<T: Copy + Ord> BitAnd for Rect<T> {
        type Output = Option<Self>;

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

    use std::ops::BitOr;
    impl<T: Copy + Ord> BitOr for Rect<T> {
        type Output = Self;

        fn bitor(self, rhs: Self) -> Self::Output {
            self.united(&rhs)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[cfg(feature = "perf")]
    fn is_normalized_test() {
        let rect = Rect::new(1, 2, 3, 4);
        assert!(rect.is_normalized());
        let rect = Rect::new(3, 2, 1, 4);
        assert!(!rect.is_normalized());
    }

    #[test]
    fn normalize_test() {
        let mut rect = Rect::new(3, 2, 1, 4);
        rect.normalize();
        assert_eq!(rect, Rect::new(1, 2, 3, 4));
    }

    #[test]
    fn contains_test() {
        let rect = Rect::new(1, 2, 3, 4);
        assert!(rect.contains(Point::new(2, 3)));
        assert!(!rect.contains(Point::new(0, 0)));
    }

    #[test]
    fn contains_with_bound_test() {
        let rect = Rect::new(1, 2, 3, 4);
        assert!(rect.contains_with_bound(Point::new(2, 3)));
        assert!(rect.contains_with_bound(Point::new(1, 2)));
        assert!(!rect.contains_with_bound(Point::new(0, 0)));
    }

    #[test]
    fn intersects_test() {
        let rect1 = Rect::new(1, 2, 3, 4);
        let rect2 = Rect::new(2, 3, 4, 5);
        assert!(rect1.intersects(&rect2));
        let rect3 = Rect::new(4, 5, 6, 7);
        assert!(!rect1.intersects(&rect3));
    }

    #[test]
    fn intersected_test() {
        let rect1 = Rect::new(1, 2, 3, 4);
        let rect2 = Rect::new(2, 3, 4, 5);
        assert_eq!(rect1.intersected(&rect2), Some(Rect::new(2, 3, 3, 4)));
        let rect3 = Rect::new(4, 5, 6, 7);
        assert_eq!(rect1.intersected(&rect3), None);
    }

    #[test]
    fn united_test() {
        let rect1 = Rect::new(1, 2, 3, 4);
        let rect2 = Rect::new(2, 3, 4, 5);
        assert_eq!(rect1.united(&rect2), Rect::new(1, 2, 4, 5));
    }

    #[test]
    fn adjust_test() {
        let mut rect = Rect::new(1, 2, 3, 4);
        rect.adjust(1, 2, 3, 4);
        assert_eq!(rect, Rect::new(2, 4, 6, 8));
    }

    #[test]
    fn center_test() {
        let rect = Rect::new(1, 2, 3, 4);
        assert_eq!(rect.center(), Point::new(2, 3));
    }
}