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
// 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

//! Vector types
//!
//! For drawing operations, all dimensions use the `f32` type.

use crate::cast::*;
use crate::dir::Directional;
use crate::geom::{Coord, Offset, Rect, Size};
use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};

/// Axis-aligned 2D cuboid, specified via two corners `a` and `b`
///
/// Typically it is expected that `a.le(b)`, although this is not required.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Quad {
    pub a: Vec2,
    pub b: Vec2,
}

impl Quad {
    /// Zero
    pub const ZERO: Quad = Quad::from_coords(Vec2::ZERO, Vec2::ZERO);

    /// Negative infinity to positive infinity (everything)
    pub const INFINITY: Quad = Quad::from_coords(Vec2::NEG_INFINITY, Vec2::INFINITY);

    /// Not a Number (NaN)
    pub const NAN: Quad = Quad::from_coords(Vec2::NAN, Vec2::NAN);

    /// Construct with two coords
    #[inline]
    pub const fn from_coords(a: Vec2, b: Vec2) -> Self {
        Quad { a, b }
    }

    /// Construct with position and size
    #[inline]
    pub fn from_pos_and_size(pos: Vec2, size: Vec2) -> Self {
        Quad {
            a: pos,
            b: pos + size,
        }
    }

    /// Get the size
    #[inline]
    pub fn size(&self) -> Vec2 {
        self.b - self.a
    }

    /// Swizzle coordinates: x from first, y from second point
    #[inline]
    pub fn ab(&self) -> Vec2 {
        Vec2(self.a.0, self.b.1)
    }

    /// Swizzle coordinates: x from second, y from first point
    #[inline]
    pub fn ba(&self) -> Vec2 {
        Vec2(self.b.0, self.a.1)
    }

    /// Shrink self in all directions by the given `value`
    #[inline]
    #[must_use = "method does not modify self but returns a new value"]
    pub fn shrink(&self, value: f32) -> Quad {
        let a = self.a + value;
        let b = self.b - value;
        Quad { a, b }
    }

    /// Grow self in all directions by the given `value`
    #[inline]
    #[must_use = "method does not modify self but returns a new value"]
    pub fn grow(&self, value: f32) -> Quad {
        let a = self.a - value;
        let b = self.b + value;
        Quad { a, b }
    }

    /// Shrink self in all directions by the given `value`
    #[inline]
    #[must_use = "method does not modify self but returns a new value"]
    pub fn shrink_vec(&self, value: Vec2) -> Quad {
        let a = self.a + value;
        let b = self.b - value;
        Quad { a, b }
    }

    /// Calculate the intersection of two quads
    #[inline]
    pub fn intersection(&self, rhs: &Quad) -> Option<Quad> {
        let a = Vec2(self.a.0.max(rhs.a.0), self.a.1.max(rhs.a.1));
        let x = (self.b.0.min(rhs.b.0) - a.0).max(0.0);
        let y = (self.b.1.min(rhs.b.1) - a.1).max(0.0);
        if x * y > 0.0 {
            Some(Quad::from_pos_and_size(a, Vec2(x, y)))
        } else {
            None
        }
    }
}

impl AddAssign<Vec2> for Quad {
    #[inline]
    fn add_assign(&mut self, rhs: Vec2) {
        self.a += rhs;
        self.b += rhs;
    }
}

impl SubAssign<Vec2> for Quad {
    #[inline]
    fn sub_assign(&mut self, rhs: Vec2) {
        self.a -= rhs;
        self.b -= rhs;
    }
}

impl Add<Vec2> for Quad {
    type Output = Quad;
    #[inline]
    fn add(mut self, rhs: Vec2) -> Self::Output {
        self += rhs;
        self
    }
}

impl Sub<Vec2> for Quad {
    type Output = Quad;
    #[inline]
    fn sub(mut self, rhs: Vec2) -> Self::Output {
        self -= rhs;
        self
    }
}

impl Conv<Rect> for Quad {
    #[inline]
    fn try_conv(rect: Rect) -> Result<Self> {
        let a = Vec2::try_conv(rect.pos)?;
        let b = a + Vec2::try_conv(rect.size)?;
        Ok(Quad { a, b })
    }
}

/// 2D vector
///
/// Usually used as either a coordinate or a difference of coordinates, but
/// may have some other uses.
///
/// Vectors are partially ordered and support component-wise comparison via
/// methods like `lhs.lt(rhs)`. The `PartialOrd` trait is not implemented since
/// it implements `lhs ≤ rhs` as `lhs < rhs || lhs == rhs` which is wrong for
/// vectors (consider for `lhs = (0, 1), rhs = (1, 0)`).
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vec2(pub f32, pub f32);

/// 2D vector (double precision)
///
/// Usually used as either a coordinate or a difference of coordinates, but
/// may have some other uses.
///
/// Vectors are partially ordered and support component-wise comparison via
/// methods like `lhs.lt(rhs)`. The `PartialOrd` trait is not implemented since
/// it implements `lhs ≤ rhs` as `lhs < rhs || lhs == rhs` which is wrong for
/// vectors (consider for `lhs = (0, 1), rhs = (1, 0)`).
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DVec2(pub f64, pub f64);

macro_rules! impl_vec2 {
    ($T:ident, $f:ty) => {
        impl $T {
            /// Zero
            pub const ZERO: $T = $T::splat(0.0);

            /// One
            pub const ONE: $T = $T::splat(1.0);

            /// Negative infinity
            pub const NEG_INFINITY: $T = $T::splat(<$f>::NEG_INFINITY);

            /// Positive infinity
            pub const INFINITY: $T = $T::splat(<$f>::INFINITY);

            /// Not a Number (NaN)
            pub const NAN: $T = $T::splat(<$f>::NAN);

            /// Constructs a new instance with each element initialized to `value`.
            #[inline]
            pub const fn splat(value: $f) -> Self {
                $T(value, value)
            }

            /// Take the minimum component
            #[inline]
            pub fn min_comp(self) -> $f {
                self.0.min(self.1)
            }

            /// Take the maximum component
            #[inline]
            pub fn max_comp(self) -> $f {
                self.0.max(self.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 {
                $T(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 {
                $T(self.0.max(other.0), self.1.max(other.1))
            }

            /// Take the absolute value of each component
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn abs(self) -> Self {
                $T(self.0.abs(), self.1.abs())
            }

            /// Take the floor of each component
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn floor(self) -> Self {
                $T(self.0.floor(), self.1.floor())
            }

            /// Take the ceiling of each component
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn ceil(self) -> Self {
                $T(self.0.ceil(), self.1.ceil())
            }

            /// Round each component to the nearest integer
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn round(self) -> Self {
                $T(self.0.round(), self.1.round())
            }

            /// Take the trunc of each component
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn trunc(self) -> Self {
                $T(self.0.trunc(), self.1.trunc())
            }

            /// Take the fract of each component
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn fract(self) -> Self {
                $T(self.0.fract(), self.1.fract())
            }

            /// For each component, return `±1` with the same sign as `self`.
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn sign(self) -> Self {
                let one: $f = 1.0;
                $T(one.copysign(self.0), one.copysign(self.1))
            }

            /// 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
            }

            /// Multiply two vectors as if they are complex numbers
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn complex_mul(self, rhs: Self) -> Self {
                $T(
                    self.0 * rhs.0 - self.1 * rhs.1,
                    self.0 * rhs.1 + self.1 * rhs.0,
                )
            }

            /// Divide by a second vector as if they are complex numbers
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn complex_div(self, rhs: Self) -> Self {
                self.complex_mul(rhs.complex_inv())
            }

            /// Take the complex reciprocal
            #[inline]
            #[must_use = "method does not modify self but returns a new value"]
            pub fn complex_inv(self) -> Self {
                let ssi = 1.0 / self.sum_square();
                $T(self.0 * ssi, -self.1 * ssi)
            }

            /// Return the sum of the terms
            #[inline]
            pub fn sum(self) -> $f {
                self.0 + self.1
            }

            /// Return the sum of the square of the terms
            #[inline]
            pub fn sum_square(self) -> $f {
                self.0 * self.0 + self.1 * self.1
            }

            /// 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) -> $f {
                match dir.is_vertical() {
                    false => self.0,
                    true => self.1,
                }
            }
        }

        impl Neg for $T {
            type Output = $T;
            #[inline]
            fn neg(self) -> Self::Output {
                $T(-self.0, -self.1)
            }
        }

        impl Add<$T> for $T {
            type Output = $T;
            #[inline]
            fn add(self, rhs: $T) -> Self::Output {
                $T(self.0 + rhs.0, self.1 + rhs.1)
            }
        }

        impl Add<$f> for $T {
            type Output = $T;
            #[inline]
            fn add(self, rhs: $f) -> Self::Output {
                $T(self.0 + rhs, self.1 + rhs)
            }
        }

        impl AddAssign<$T> for $T {
            #[inline]
            fn add_assign(&mut self, rhs: $T) {
                self.0 += rhs.0;
                self.1 += rhs.1;
            }
        }

        impl AddAssign<$f> for $T {
            #[inline]
            fn add_assign(&mut self, rhs: $f) {
                self.0 += rhs;
                self.1 += rhs;
            }
        }

        impl Sub<$T> for $T {
            type Output = $T;
            #[inline]
            fn sub(self, rhs: $T) -> Self::Output {
                $T(self.0 - rhs.0, self.1 - rhs.1)
            }
        }

        impl Sub<$f> for $T {
            type Output = $T;
            #[inline]
            fn sub(self, rhs: $f) -> Self::Output {
                $T(self.0 - rhs, self.1 - rhs)
            }
        }

        impl SubAssign<$T> for $T {
            #[inline]
            fn sub_assign(&mut self, rhs: $T) {
                self.0 -= rhs.0;
                self.1 -= rhs.1;
            }
        }

        impl SubAssign<$f> for $T {
            #[inline]
            fn sub_assign(&mut self, rhs: $f) {
                self.0 -= rhs;
                self.1 -= rhs;
            }
        }

        impl Mul<$T> for $T {
            type Output = $T;
            #[inline]
            fn mul(self, rhs: $T) -> Self::Output {
                $T(self.0 * rhs.0, self.1 * rhs.1)
            }
        }

        impl Mul<$f> for $T {
            type Output = $T;
            #[inline]
            fn mul(self, rhs: $f) -> Self::Output {
                $T(self.0 * rhs, self.1 * rhs)
            }
        }

        impl Div<$T> for $T {
            type Output = $T;
            #[inline]
            fn div(self, rhs: $T) -> Self::Output {
                $T(self.0 / rhs.0, self.1 / rhs.1)
            }
        }

        impl Div<$f> for $T {
            type Output = $T;
            #[inline]
            fn div(self, rhs: $f) -> Self::Output {
                $T(self.0 / rhs, self.1 / rhs)
            }
        }

        impl Div<$T> for $f {
            type Output = $T;
            #[inline]
            fn div(self, rhs: $T) -> Self::Output {
                $T(self / rhs.0, self / rhs.1)
            }
        }

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

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

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

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

impl ConvApprox<DVec2> for Vec2 {
    fn try_conv_approx(size: DVec2) -> Result<Vec2> {
        Ok(Vec2(size.0.try_cast_approx()?, size.1.try_cast_approx()?))
    }
}

impl_vec2!(Vec2, f32);
impl_vec2!(DVec2, f64);

macro_rules! impl_conv_vec2 {
    ($S:ty, $T:ty) => {
        impl Conv<$S> for $T {
            #[inline]
            fn try_conv(arg: $S) -> Result<Self> {
                Ok(Self(arg.0.try_cast()?, arg.1.try_cast()?))
            }
        }

        impl ConvApprox<$T> for $S {
            #[inline]
            fn try_conv_approx(arg: $T) -> Result<Self> {
                Ok(Self(arg.0.try_cast_approx()?, arg.1.try_cast_approx()?))
            }
        }

        impl ConvFloat<$T> for $S {
            #[inline]
            fn try_conv_trunc(x: $T) -> Result<Self> {
                Ok(Self(i32::try_conv_trunc(x.0)?, i32::try_conv_trunc(x.1)?))
            }
            #[inline]
            fn try_conv_nearest(x: $T) -> Result<Self> {
                Ok(Self(
                    i32::try_conv_nearest(x.0)?,
                    i32::try_conv_nearest(x.1)?,
                ))
            }
            #[inline]
            fn try_conv_floor(x: $T) -> Result<Self> {
                Ok(Self(i32::try_conv_floor(x.0)?, i32::try_conv_floor(x.1)?))
            }
            #[inline]
            fn try_conv_ceil(x: $T) -> Result<Self> {
                Ok(Self(i32::try_conv_ceil(x.0)?, i32::try_conv_ceil(x.1)?))
            }
        }
    };
}

impl_conv_vec2!(Coord, Vec2);
impl_conv_vec2!(Size, Vec2);
impl_conv_vec2!(Offset, Vec2);
impl_conv_vec2!(Coord, DVec2);
impl_conv_vec2!(Size, DVec2);
impl_conv_vec2!(Offset, DVec2);

/// 3D vector
///
/// Usually used for a 2D coordinate with a depth value.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vec3(pub f32, pub f32, pub f32);

impl Vec3 {
    /// Construct from a [`Vec2`] and third value
    #[inline]
    pub fn from2(v: Vec2, z: f32) -> Self {
        Vec3(v.0, v.1, z)
    }
}