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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Geometry data types
//!
//! [`Coord`], [`Size`] and [`Offset`] are all 2D integer (`i32`) types,
//! representing positions, sizes and scroll deltas respectively.
//!
//! [`Vec2`] is a 2D floating-point (`f32`) type used mainly for screen-space
//! position during rendering.
//!
//! Conversions types mostly use [`Cast`] and [`Conv`]. [`From`] may be used to
//! simply pack/unpack components. To convert from floating-point types to
//! integer types, use [`CastApprox`] or [`CastFloat`] to specify the rounding
//! mode.

use crate::cast::*;
use crate::dir::Directional;

mod vector;
pub use vector::{DVec2, Quad, Vec2, Vec3};

macro_rules! impl_common {
    ($T:ty) => {
        impl $T {
            /// The constant `(0, 0)`
            pub const ZERO: Self = Self(0, 0);

            /// The minimum value
            pub const MIN: Self = Self(i32::MIN, i32::MIN);

            /// The maximum value
            pub const MAX: Self = Self(i32::MAX, i32::MAX);

            /// True when for all components, `lhs < rhs`
            #[inline]
            pub fn lt(self, rhs: Self) -> bool {
                self.0 < rhs.0 && self.1 < rhs.1
            }

            /// True when for all components, `lhs ≤ rhs`
            #[inline]
            pub fn le(self, rhs: Self) -> bool {
                self.0 <= rhs.0 && self.1 <= rhs.1
            }

            /// True when for all components, `lhs ≥ rhs`
            #[inline]
            pub fn ge(self, rhs: Self) -> bool {
                self.0 >= rhs.0 && self.1 >= rhs.1
            }

            /// True when for all components, `lhs > rhs`
            #[inline]
            pub fn gt(self, rhs: Self) -> bool {
                self.0 > rhs.0 && self.1 > rhs.1
            }

            /// Return the minimum, componentwise
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn min(self, other: Self) -> Self {
                Self(self.0.min(other.0), self.1.min(other.1))
            }

            /// Return the maximum, componentwise
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn max(self, other: Self) -> Self {
                Self(self.0.max(other.0), self.1.max(other.1))
            }

            /// Restrict a value to the specified interval, componentwise
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn clamp(self, min: Self, max: Self) -> Self {
                debug_assert!(min.le(max));
                self.min(max).max(min)
            }

            /// Return the transpose (swap x and y values)
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn transpose(self) -> Self {
                Self(self.1, self.0)
            }

            /// Return the result of component-wise multiplication
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn cwise_mul(self, rhs: Self) -> Self {
                Self(self.0 * rhs.0, self.1 * rhs.1)
            }

            /// Return the result of component-wise division
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn cwise_div(self, rhs: Self) -> Self {
                Self(self.0 / rhs.0, self.1 / rhs.1)
            }

            /// Return the L1 (rectilinear / taxicab) distance
            #[inline]
            pub fn distance_l1(self) -> i32 {
                self.0.abs() + self.1.abs()
            }

            /// Return the L-inf (max) distance
            #[inline]
            pub fn distance_l_inf(self) -> i32 {
                self.0.abs().max(self.1.abs())
            }

            /// Extract one component, based on a direction
            ///
            /// This merely extracts the horizontal or vertical component.
            /// It never negates it, even if the axis is reversed.
            #[inline]
            pub fn extract<D: Directional>(self, dir: D) -> i32 {
                match dir.is_vertical() {
                    false => self.0,
                    true => self.1,
                }
            }

            /// Set one component of self, based on a direction
            ///
            /// This does not negate components when the direction is reversed.
            #[inline]
            pub fn set_component<D: Directional>(&mut self, dir: D, value: i32) {
                match dir.is_vertical() {
                    false => self.0 = value,
                    true => self.1 = value,
                }
            }
        }

        impl From<(i32, i32)> for $T {
            #[inline]
            fn from(v: (i32, i32)) -> Self {
                Self(v.0, v.1)
            }
        }
        impl Conv<(i32, i32)> for $T {
            #[inline]
            fn conv(v: (i32, i32)) -> Self {
                Self(v.0, v.1)
            }
            #[inline]
            fn try_conv(v: (i32, i32)) -> Result<Self> {
                Ok(Self::conv(v))
            }
        }
    };
}

/// A 2D coordinate, also known as a point
///
/// A coordinate (or point) is an absolute position. One cannot add a point to
/// a point. The difference between two points is an [`Offset`].
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Coord(pub i32, pub i32);

impl_common!(Coord);

impl Coord {
    /// Construct
    #[inline]
    pub fn new(x: i32, y: i32) -> Self {
        Self(x, y)
    }

    /// Construct, using the same value on all axes
    #[inline]
    pub const fn splat(n: i32) -> Self {
        Self(n, n)
    }
}

impl std::ops::Sub for Coord {
    type Output = Offset;

    #[inline]
    fn sub(self, other: Self) -> Offset {
        Offset(self.0 - other.0, self.1 - other.1)
    }
}

impl std::ops::Add<Offset> for Coord {
    type Output = Self;

    #[inline]
    fn add(self, other: Offset) -> Self {
        Coord(self.0 + other.0, self.1 + other.1)
    }
}
impl std::ops::AddAssign<Offset> for Coord {
    #[inline]
    fn add_assign(&mut self, rhs: Offset) {
        self.0 += rhs.0;
        self.1 += rhs.1;
    }
}
impl std::ops::Sub<Offset> for Coord {
    type Output = Self;

    #[inline]
    fn sub(self, other: Offset) -> Self {
        Coord(self.0 - other.0, self.1 - other.1)
    }
}
impl std::ops::SubAssign<Offset> for Coord {
    #[inline]
    fn sub_assign(&mut self, rhs: Offset) {
        self.0 -= rhs.0;
        self.1 -= rhs.1;
    }
}

impl std::ops::Add<Size> for Coord {
    type Output = Self;

    #[inline]
    fn add(self, other: Size) -> Self {
        Coord(self.0 + other.0, self.1 + other.1)
    }
}
impl std::ops::AddAssign<Size> for Coord {
    #[inline]
    fn add_assign(&mut self, rhs: Size) {
        self.0 += rhs.0;
        self.1 += rhs.1;
    }
}
impl std::ops::Sub<Size> for Coord {
    type Output = Self;

    #[inline]
    fn sub(self, other: Size) -> Self {
        Coord(self.0 - other.0, self.1 - other.1)
    }
}
impl std::ops::SubAssign<Size> for Coord {
    #[inline]
    fn sub_assign(&mut self, rhs: Size) {
        self.0 -= rhs.0;
        self.1 -= rhs.1;
    }
}

impl Conv<Coord> for kas_text::Vec2 {
    #[inline]
    fn try_conv(pos: Coord) -> Result<Self> {
        Ok(Vec2::try_conv(pos)?.into())
    }
}

/// A 2D size, also known as an extent
///
/// This is both a size and a relative position. One can add or subtract a size
/// from a [`Coord`]. One can multiply a size by a scalar.
///
/// A `Size` is expected to be non-negative; some methods such as [`Size::new`]
/// and implementations of subtraction will check this, but only in debug mode
/// (similar to overflow checks on integers).
///
/// Subtraction is defined to be saturating subtraction.
///
/// This may be converted to [`Offset`] with `from` / `into`.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Size(pub i32, pub i32);

impl_common!(Size);

impl Size {
    /// Construct
    ///
    /// In debug mode, this asserts that components are non-negative.
    #[inline]
    pub fn new(w: i32, h: i32) -> Self {
        debug_assert!(w >= 0 && h >= 0, "Size::new({w}, {h}): negative value");
        Self(w, h)
    }

    /// Construct, using the same value on all axes
    ///
    /// In debug mode, this asserts that components are non-negative.
    #[inline]
    pub fn splat(n: i32) -> Self {
        debug_assert!(n >= 0, "Size::splat({n}): negative value");
        Self(n, n)
    }

    /// Scale to fit within the target size, keeping aspect ratio
    ///
    /// If either dimension of self is 0, this returns None.
    pub fn aspect_scale_to(self, target: Size) -> Option<Size> {
        if self.0 == 0 || self.1 == 0 {
            return None;
        }

        let h = i32::conv((i64::conv(self.1) * i64::conv(target.0)) / i64::conv(self.0));
        if h <= target.1 {
            Some(Size(target.0, h))
        } else {
            let w = i32::conv((i64::conv(self.0) * i64::conv(target.1)) / i64::conv(self.1));
            Some(Size(w, target.1))
        }
    }
}

impl std::ops::Add for Size {
    type Output = Self;

    #[inline]
    fn add(self, other: Self) -> Self {
        Size(self.0 + other.0, self.1 + other.1)
    }
}
impl std::ops::AddAssign for Size {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
        self.1 += rhs.1;
    }
}

/// Subtract a `Size` from a `Size`
///
/// This is saturating subtraction: `Size::ZERO - Size::splat(6) == Size::ZERO`.
impl std::ops::Sub for Size {
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Self) -> Self {
        // This impl should aid vectorisation.
        Size(self.0 - rhs.0, self.1 - rhs.1).max(Size::ZERO)
    }
}
/// Subtract a `Size` from a `Size`
///
/// This is saturating subtraction.
impl std::ops::SubAssign for Size {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

/// Multiply a `Size` by an integer
///
/// In debug mode this asserts that the result is non-negative.
impl std::ops::Mul<i32> for Size {
    type Output = Self;

    #[inline]
    fn mul(self, x: i32) -> Self {
        debug_assert!(x >= 0);
        Size(self.0 * x, self.1 * x)
    }
}
/// Divide a `Size` by an integer
///
/// In debug mode this asserts that the result is non-negative.
impl std::ops::Div<i32> for Size {
    type Output = Self;

    #[inline]
    fn div(self, x: i32) -> Self {
        debug_assert!(x >= 0);
        Size(self.0 / x, self.1 / x)
    }
}

/// Convert an [`Offset`] into a [`Coord`]
///
/// In debug mode this asserts that the result is non-negative.
impl Conv<Offset> for Coord {
    #[inline]
    fn try_conv(v: Offset) -> Result<Self> {
        debug_assert!(v.0 >= 0 && v.1 >= 0, "Coord::conv({v:?}): negative value");
        Ok(Self(v.0, v.1))
    }
}

/// Convert an [`Offset`] into a [`Size`]
///
/// In debug mode this asserts that the result is non-negative.
impl Conv<Offset> for Size {
    #[inline]
    fn try_conv(v: Offset) -> Result<Self> {
        debug_assert!(v.0 >= 0 && v.1 >= 0, "Size::conv({v:?}): negative value");
        Ok(Self(v.0, v.1))
    }
}

// used for marigns
impl Conv<Size> for (u16, u16) {
    #[inline]
    fn try_conv(size: Size) -> Result<Self> {
        Ok((size.0.try_cast()?, size.1.try_cast()?))
    }
}
impl Conv<(u16, u16)> for Size {
    #[inline]
    fn try_conv(v: (u16, u16)) -> Result<Self> {
        Ok(Self(i32::try_conv(v.0)?, i32::try_conv(v.1)?))
    }
}

impl Conv<(u32, u32)> for Size {
    #[inline]
    fn try_conv(v: (u32, u32)) -> Result<Self> {
        Ok(Self(i32::try_conv(v.0)?, i32::try_conv(v.1)?))
    }
}

impl Conv<Size> for (u32, u32) {
    #[inline]
    fn try_conv(size: Size) -> Result<Self> {
        Ok((u32::try_conv(size.0)?, u32::try_conv(size.1)?))
    }
}

impl Conv<Size> for kas_text::Vec2 {
    #[inline]
    fn try_conv(size: Size) -> Result<Self> {
        Ok(Vec2::try_conv(size)?.into())
    }
}

/// A `(x, y)` offset, also known as a **vector**
///
/// This is a relative position. It can be added to or subtracted from a
/// [`Coord`], and it can be added to or subtracted from itself. It can be
/// negative. It can be multiplied by a scalar.
///
/// This may be converted to [`Size`] with `from` / `into`.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Offset(pub i32, pub i32);

impl_common!(Offset);

impl Offset {
    /// Construct
    #[inline]
    pub fn new(x: i32, y: i32) -> Self {
        Self(x, y)
    }

    /// Construct, using the same value on all axes
    #[inline]
    pub const fn splat(n: i32) -> Self {
        Self(n, n)
    }
}

impl std::ops::Add for Offset {
    type Output = Self;

    #[inline]
    fn add(self, other: Self) -> Self {
        Offset(self.0 + other.0, self.1 + other.1)
    }
}
impl std::ops::AddAssign for Offset {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        self.0 += rhs.0;
        self.1 += rhs.1;
    }
}

impl std::ops::Sub for Offset {
    type Output = Self;

    #[inline]
    fn sub(self, other: Self) -> Self {
        Offset(self.0 - other.0, self.1 - other.1)
    }
}
impl std::ops::SubAssign for Offset {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        self.0 -= rhs.0;
        self.1 -= rhs.1;
    }
}

impl std::ops::Mul<i32> for Offset {
    type Output = Self;

    #[inline]
    fn mul(self, x: i32) -> Self {
        Offset(self.0 * x, self.1 * x)
    }
}
impl std::ops::Div<i32> for Offset {
    type Output = Self;

    #[inline]
    fn div(self, x: i32) -> Self {
        Offset(self.0 / x, self.1 / x)
    }
}

impl Conv<Coord> for Offset {
    #[inline]
    fn try_conv(v: Coord) -> Result<Self> {
        Ok(Self(v.0, v.1))
    }
}

impl Conv<Size> for Offset {
    #[inline]
    fn try_conv(v: Size) -> Result<Self> {
        Ok(Self(v.0, v.1))
    }
}

impl Conv<Offset> for kas_text::Vec2 {
    #[inline]
    fn try_conv(v: Offset) -> Result<Self> {
        Ok(Vec2::try_conv(v)?.into())
    }
}

/// An axis-aligned rectangular region
///
/// The region is defined by a point `pos` and an extent `size`, allowing easy
/// translations. It is empty unless `size` is positive on both axes.
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Rect {
    pub pos: Coord,
    pub size: Size,
}

impl Rect {
    /// The empty rect (all fields zero)
    pub const ZERO: Self = Self::new(Coord::ZERO, Size::ZERO);

    /// Construct from a [`Coord`] and [`Size`]
    #[inline]
    pub const fn new(pos: Coord, size: Size) -> Self {
        Rect { pos, size }
    }

    /// Get the second point (pos + size)
    #[inline]
    pub fn pos2(&self) -> Coord {
        self.pos + self.size
    }

    /// Check whether the given coordinate is contained within this rect
    #[inline]
    pub fn contains(&self, c: Coord) -> bool {
        c.0 >= self.pos.0
            && c.0 < self.pos.0 + (self.size.0)
            && c.1 >= self.pos.1
            && c.1 < self.pos.1 + (self.size.1)
    }

    /// Calculate the intersection of two rects
    #[inline]
    pub fn intersection(&self, rhs: &Rect) -> Option<Rect> {
        let (l1, l2) = (self.pos, self.pos2());
        let (r1, r2) = (rhs.pos, rhs.pos2());
        let pos = l1.max(r1);
        let pos2 = l2.min(r2);
        if pos.le(pos2) {
            Some(Rect::new(pos, (pos2 - pos).cast()))
        } else {
            None
        }
    }

    /// Shrink self in all directions by the given `n`
    #[inline]
    #[must_use = "method does not modify self but returns a new value"]
    pub fn shrink(&self, n: i32) -> Rect {
        let pos = self.pos + Offset::splat(n);
        let size = self.size - Size::splat(n + n);
        Rect { pos, size }
    }

    /// Expand self in all directions by the given `n`
    ///
    /// In debug mode this asserts that `n` is non-negative.
    #[inline]
    #[must_use = "method does not modify self but returns a new value"]
    pub fn expand(&self, n: i32) -> Rect {
        debug_assert!(n >= 0);
        let pos = self.pos - Offset::splat(n);
        let size = self.size + Size::splat(n + n);
        Rect { pos, size }
    }
}

impl std::ops::Add<Offset> for Rect {
    type Output = Self;

    #[inline]
    fn add(self, offset: Offset) -> Self {
        Rect::new(self.pos + offset, self.size)
    }
}
impl std::ops::AddAssign<Offset> for Rect {
    #[inline]
    fn add_assign(&mut self, offset: Offset) {
        self.pos += offset;
    }
}

impl std::ops::Sub<Offset> for Rect {
    type Output = Self;

    #[inline]
    fn sub(self, offset: Offset) -> Self {
        Rect::new(self.pos - offset, self.size)
    }
}
impl std::ops::SubAssign<Offset> for Rect {
    #[inline]
    fn sub_assign(&mut self, offset: Offset) {
        self.pos -= offset;
    }
}

#[cfg(winit)]
#[cfg_attr(doc_cfg, doc(cfg(feature = "winit")))]
mod winit_impls {
    use super::{Coord, Size};
    use crate::cast::{Cast, CastApprox, Conv, ConvApprox};
    use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize};

    impl<X: CastApprox<i32>> ConvApprox<PhysicalPosition<X>> for Coord {
        #[inline]
        fn try_conv_approx(pos: PhysicalPosition<X>) -> cast::Result<Self> {
            Ok(Coord(pos.x.cast_approx(), pos.y.cast_approx()))
        }
    }

    impl<X: Cast<i32>> Conv<PhysicalSize<X>> for Size {
        #[inline]
        fn try_conv(size: PhysicalSize<X>) -> cast::Result<Self> {
            Ok(Size(size.width.cast(), size.height.cast()))
        }
    }

    impl Size {
        /// Convert to a "physical" [`winit::dpi::Size`]
        ///
        /// This implies that the [`Size`] was calculated using the correct
        /// scale factor. Before the window has been constructed (when the
        /// scale factor is supposedly unknown) this should not be used.
        #[inline]
        pub fn as_physical(self) -> winit::dpi::Size {
            let (w, h): (u32, u32) = self.cast();
            winit::dpi::Size::Physical(PhysicalSize::new(w, h))
        }

        /// Convert to a "logical" [`winit::dpi::Size`]
        ///
        /// This implies that the [`Size`] was calculated using `scale_factor = 1`.
        #[inline]
        pub fn as_logical(self) -> winit::dpi::Size {
            let (w, h) = (self.0 as f64, self.1 as f64);
            winit::dpi::Size::Logical(LogicalSize::new(w, h))
        }
    }
}