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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use super::UnknownUnit;
#[cfg(feature = "mint")]
use mint;
use length::Length;
use scale::TypedScale;
use vector::{TypedVector2D, vec2, BoolVector2D};
use vector::{TypedVector3D, vec3, BoolVector3D};
use num::*;

use num_traits::{Float, NumCast, Signed};
use core::fmt;
use core::ops::{Add, Div, Mul, Sub};
use core::marker::PhantomData;

/// A 2d size tagged with a unit.
#[derive(EuclidMatrix)]
#[repr(C)]
pub struct TypedSize2D<T, U> {
    pub width: T,
    pub height: T,
    #[doc(hidden)]
    pub _unit: PhantomData<U>,
}

/// Default 2d size type with no unit.
///
/// `Size2D` provides the same methods as `TypedSize2D`.
pub type Size2D<T> = TypedSize2D<T, UnknownUnit>;

impl<T: fmt::Debug, U> fmt::Debug for TypedSize2D<T, U> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}×{:?}", self.width, self.height)
    }
}

impl<T: fmt::Display, U> fmt::Display for TypedSize2D<T, U> {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "({}x{})", self.width, self.height)
    }
}

impl<T: Default, U> Default for TypedSize2D<T, U> {
    fn default() -> Self {
        TypedSize2D::new(Default::default(), Default::default())
    }
}

impl<T, U> TypedSize2D<T, U> {
    /// Constructor taking scalar values.
    pub fn new(width: T, height: T) -> Self {
        TypedSize2D {
            width,
            height,
            _unit: PhantomData,
        }
    }
}

impl<T: Clone, U> TypedSize2D<T, U> {
    /// Constructor taking scalar strongly typed lengths.
    pub fn from_lengths(width: Length<T, U>, height: Length<T, U>) -> Self {
        TypedSize2D::new(width.get(), height.get())
    }
}

impl<T: Round, U> TypedSize2D<T, U> {
    /// Rounds each component to the nearest integer value.
    ///
    /// This behavior is preserved for negative values (unlike the basic cast).
    pub fn round(&self) -> Self {
        TypedSize2D::new(self.width.round(), self.height.round())
    }
}

impl<T: Ceil, U> TypedSize2D<T, U> {
    /// Rounds each component to the smallest integer equal or greater than the original value.
    ///
    /// This behavior is preserved for negative values (unlike the basic cast).
    pub fn ceil(&self) -> Self {
        TypedSize2D::new(self.width.ceil(), self.height.ceil())
    }
}

impl<T: Floor, U> TypedSize2D<T, U> {
    /// Rounds each component to the biggest integer equal or lower than the original value.
    ///
    /// This behavior is preserved for negative values (unlike the basic cast).
    pub fn floor(&self) -> Self {
        TypedSize2D::new(self.width.floor(), self.height.floor())
    }
}

impl<T: Copy + Add<T, Output = T>, U> Add for TypedSize2D<T, U> {
    type Output = Self;
    fn add(self, other: Self) -> Self {
        TypedSize2D::new(self.width + other.width, self.height + other.height)
    }
}

impl<T: Copy + Sub<T, Output = T>, U> Sub for TypedSize2D<T, U> {
    type Output = Self;
    fn sub(self, other: Self) -> Self {
        TypedSize2D::new(self.width - other.width, self.height - other.height)
    }
}

impl<T: Copy + Clone + Mul<T>, U> TypedSize2D<T, U> {
    pub fn area(&self) -> T::Output {
        self.width * self.height
    }
}

impl<T, U> TypedSize2D<T, U>
where
    T: Copy + One + Add<Output = T> + Sub<Output = T> + Mul<Output = T>,
{
    /// Linearly interpolate between this size and another size.
    ///
    /// `t` is expected to be between zero and one.
    #[inline]
    pub fn lerp(&self, other: Self, t: T) -> Self {
        let one_t = T::one() - t;
        size2(
            one_t * self.width + t * other.width,
            one_t * self.height + t * other.height,
        )
    }
}

impl<T: Zero + PartialOrd, U> TypedSize2D<T, U> {
    pub fn is_empty_or_negative(&self) -> bool {
        let zero = T::zero();
        self.width <= zero || self.height <= zero
    }
}

impl<T: Zero, U> TypedSize2D<T, U> {
    pub fn zero() -> Self {
        TypedSize2D::new(Zero::zero(), Zero::zero())
    }
}

impl<T: Zero, U> Zero for TypedSize2D<T, U> {
    fn zero() -> Self {
        TypedSize2D::new(Zero::zero(), Zero::zero())
    }
}

impl<T: Copy + Mul<T, Output = T>, U> Mul<T> for TypedSize2D<T, U> {
    type Output = Self;
    #[inline]
    fn mul(self, scale: T) -> Self {
        TypedSize2D::new(self.width * scale, self.height * scale)
    }
}

impl<T: Copy + Div<T, Output = T>, U> Div<T> for TypedSize2D<T, U> {
    type Output = Self;
    #[inline]
    fn div(self, scale: T) -> Self {
        TypedSize2D::new(self.width / scale, self.height / scale)
    }
}

impl<T: Copy + Mul<T, Output = T>, U1, U2> Mul<TypedScale<T, U1, U2>> for TypedSize2D<T, U1> {
    type Output = TypedSize2D<T, U2>;
    #[inline]
    fn mul(self, scale: TypedScale<T, U1, U2>) -> TypedSize2D<T, U2> {
        TypedSize2D::new(self.width * scale.get(), self.height * scale.get())
    }
}

impl<T: Copy + Div<T, Output = T>, U1, U2> Div<TypedScale<T, U1, U2>> for TypedSize2D<T, U2> {
    type Output = TypedSize2D<T, U1>;
    #[inline]
    fn div(self, scale: TypedScale<T, U1, U2>) -> TypedSize2D<T, U1> {
        TypedSize2D::new(self.width / scale.get(), self.height / scale.get())
    }
}

impl<T: Copy, U> TypedSize2D<T, U> {
    /// Returns self.width as a Length carrying the unit.
    #[inline]
    pub fn width_typed(&self) -> Length<T, U> {
        Length::new(self.width)
    }

    /// Returns self.height as a Length carrying the unit.
    #[inline]
    pub fn height_typed(&self) -> Length<T, U> {
        Length::new(self.height)
    }

    #[inline]
    pub fn to_array(&self) -> [T; 2] {
        [self.width, self.height]
    }

    #[inline]
    pub fn to_tuple(&self) -> (T, T) {
        (self.width, self.height)
    }

    #[inline]
    pub fn to_vector(&self) -> TypedVector2D<T, U> {
        vec2(self.width, self.height)
    }

    /// Drop the units, preserving only the numeric value.
    pub fn to_untyped(&self) -> Size2D<T> {
        TypedSize2D::new(self.width, self.height)
    }

    /// Tag a unitless value with units.
    pub fn from_untyped(p: &Size2D<T>) -> Self {
        TypedSize2D::new(p.width, p.height)
    }
}

impl<T: NumCast + Copy, Unit> TypedSize2D<T, Unit> {
    /// Cast from one numeric representation to another, preserving the units.
    ///
    /// When casting from floating point to integer coordinates, the decimals are truncated
    /// as one would expect from a simple cast, but this behavior does not always make sense
    /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
    pub fn cast<NewT: NumCast + Copy>(&self) -> TypedSize2D<NewT, Unit> {
        self.try_cast().unwrap()
    }

    /// Fallible cast from one numeric representation to another, preserving the units.
    ///
    /// When casting from floating point to integer coordinates, the decimals are truncated
    /// as one would expect from a simple cast, but this behavior does not always make sense
    /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
    pub fn try_cast<NewT: NumCast + Copy>(&self) -> Option<TypedSize2D<NewT, Unit>> {
        match (NumCast::from(self.width), NumCast::from(self.height)) {
            (Some(w), Some(h)) => Some(TypedSize2D::new(w, h)),
            _ => None,
        }
    }

    // Convenience functions for common casts

    /// Cast into an `f32` size.
    pub fn to_f32(&self) -> TypedSize2D<f32, Unit> {
        self.cast()
    }

    /// Cast into an `f64` size.
    pub fn to_f64(&self) -> TypedSize2D<f64, Unit> {
        self.cast()
    }

    /// Cast into an `uint` size, truncating decimals if any.
    ///
    /// When casting from floating point sizes, it is worth considering whether
    /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
    /// the desired conversion behavior.
    pub fn to_usize(&self) -> TypedSize2D<usize, Unit> {
        self.cast()
    }

    /// Cast into an `u32` size, truncating decimals if any.
    ///
    /// When casting from floating point sizes, it is worth considering whether
    /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
    /// the desired conversion behavior.
    pub fn to_u32(&self) -> TypedSize2D<u32, Unit> {
        self.cast()
    }

    /// Cast into an `i32` size, truncating decimals if any.
    ///
    /// When casting from floating point sizes, it is worth considering whether
    /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
    /// the desired conversion behavior.
    pub fn to_i32(&self) -> TypedSize2D<i32, Unit> {
        self.cast()
    }

    /// Cast into an `i64` size, truncating decimals if any.
    ///
    /// When casting from floating point sizes, it is worth considering whether
    /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
    /// the desired conversion behavior.
    pub fn to_i64(&self) -> TypedSize2D<i64, Unit> {
        self.cast()
    }
}

impl<T, U> TypedSize2D<T, U>
where
    T: Signed,
{
    pub fn abs(&self) -> Self {
        size2(self.width.abs(), self.height.abs())
    }

    pub fn is_positive(&self) -> bool {
        self.width.is_positive() && self.height.is_positive()
    }
}

impl<T: PartialOrd, U> TypedSize2D<T, U> {
    pub fn greater_than(&self, other: &Self) -> BoolVector2D {
        BoolVector2D {
            x: self.width > other.width,
            y: self.height > other.height,
        }
    }

    pub fn lower_than(&self, other: &Self) -> BoolVector2D {
        BoolVector2D {
            x: self.width < other.width,
            y: self.height < other.height,
        }
    }
}


impl<T: PartialEq, U> TypedSize2D<T, U> {
    pub fn equal(&self, other: &Self) -> BoolVector2D {
        BoolVector2D {
            x: self.width == other.width,
            y: self.height == other.height,
        }
    }

    pub fn not_equal(&self, other: &Self) -> BoolVector2D {
        BoolVector2D {
            x: self.width != other.width,
            y: self.height != other.height,
        }
    }
}

impl<T: Float, U> TypedSize2D<T, U> {
    #[inline]
    pub fn min(self, other: Self) -> Self {
        size2(
            self.width.min(other.width),
            self.height.min(other.height),
        )
    }

    #[inline]
    pub fn max(self, other: Self) -> Self {
        size2(
            self.width.max(other.width),
            self.height.max(other.height),
        )
    }

    #[inline]
    pub fn clamp(&self, start: Self, end: Self) -> Self {
        self.max(start).min(end)
    }
}


/// Shorthand for `TypedSize2D::new(w, h)`.
pub fn size2<T, U>(w: T, h: T) -> TypedSize2D<T, U> {
    TypedSize2D::new(w, h)
}

#[cfg(feature = "mint")]
impl<T, U> From<mint::Vector2<T>> for TypedSize2D<T, U> {
    fn from(v: mint::Vector2<T>) -> Self {
        TypedSize2D {
            width: v.x,
            height: v.y,
            _unit: PhantomData,
        }
    }
}
#[cfg(feature = "mint")]
impl<T, U> Into<mint::Vector2<T>> for TypedSize2D<T, U> {
    fn into(self) -> mint::Vector2<T> {
        mint::Vector2 {
            x: self.width,
            y: self.height,
        }
    }
}

impl<T: Copy, U> Into<[T; 2]> for TypedSize2D<T, U> {
    fn into(self) -> [T; 2] {
        self.to_array()
    }
}

impl<T: Copy, U> From<[T; 2]> for TypedSize2D<T, U> {
    fn from(array: [T; 2]) -> Self {
        size2(array[0], array[1])
    }
}

impl<T: Copy, U> Into<(T, T)> for TypedSize2D<T, U> {
    fn into(self) -> (T, T) {
        self.to_tuple()
    }
}

impl<T: Copy, U> From<(T, T)> for TypedSize2D<T, U> {
    fn from(tuple: (T, T)) -> Self {
        size2(tuple.0, tuple.1)
    }
}

#[cfg(test)]
mod size2d {
    use super::Size2D;
    #[cfg(feature = "mint")]
    use mint;

    #[test]
    pub fn test_add() {
        let p1 = Size2D::new(1.0, 2.0);
        let p2 = Size2D::new(3.0, 4.0);
        assert_eq!(p1 + p2, Size2D::new(4.0, 6.0));

        let p1 = Size2D::new(1.0, 2.0);
        let p2 = Size2D::new(0.0, 0.0);
        assert_eq!(p1 + p2, Size2D::new(1.0, 2.0));

        let p1 = Size2D::new(1.0, 2.0);
        let p2 = Size2D::new(-3.0, -4.0);
        assert_eq!(p1 + p2, Size2D::new(-2.0, -2.0));

        let p1 = Size2D::new(0.0, 0.0);
        let p2 = Size2D::new(0.0, 0.0);
        assert_eq!(p1 + p2, Size2D::new(0.0, 0.0));
    }

    #[test]
    pub fn test_sub() {
        let p1 = Size2D::new(1.0, 2.0);
        let p2 = Size2D::new(3.0, 4.0);
        assert_eq!(p1 - p2, Size2D::new(-2.0, -2.0));

        let p1 = Size2D::new(1.0, 2.0);
        let p2 = Size2D::new(0.0, 0.0);
        assert_eq!(p1 - p2, Size2D::new(1.0, 2.0));

        let p1 = Size2D::new(1.0, 2.0);
        let p2 = Size2D::new(-3.0, -4.0);
        assert_eq!(p1 - p2, Size2D::new(4.0, 6.0));

        let p1 = Size2D::new(0.0, 0.0);
        let p2 = Size2D::new(0.0, 0.0);
        assert_eq!(p1 - p2, Size2D::new(0.0, 0.0));
    }

    #[test]
    pub fn test_area() {
        let p = Size2D::new(1.5, 2.0);
        assert_eq!(p.area(), 3.0);
    }

    #[cfg(feature = "mint")]
    #[test]
    pub fn test_mint() {
        let s1 = Size2D::new(1.0, 2.0);
        let sm: mint::Vector2<_> = s1.into();
        let s2 = Size2D::from(sm);

        assert_eq!(s1, s2);
    }
}

/// A 3d size tagged with a unit.
#[derive(EuclidMatrix)]
#[repr(C)]
pub struct TypedSize3D<T, U> {
    pub width: T,
    pub height: T,
    pub depth: T,
    #[doc(hidden)]
    pub _unit: PhantomData<U>,
}

/// Default 3d size type with no unit.
///
/// `Size3D` provides the same methods as `TypedSize3D`.
pub type Size3D<T> = TypedSize3D<T, UnknownUnit>;

impl<T: fmt::Debug, U> fmt::Debug for TypedSize3D<T, U> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}×{:?}×{:?}", self.width, self.height, self.depth)
    }
}

impl<T: fmt::Display, U> fmt::Display for TypedSize3D<T, U> {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "({}x{}x{})", self.width, self.height, self.depth)
    }
}

impl<T: Default, U> Default for TypedSize3D<T, U> {
    fn default() -> Self {
        TypedSize3D::new(Default::default(), Default::default(), Default::default())
    }
}

impl<T, U> TypedSize3D<T, U> {
    /// Constructor taking scalar values.
    pub fn new(width: T, height: T, depth: T) -> Self {
        TypedSize3D {
            width,
            height,
            depth,
            _unit: PhantomData,
        }
    }
}

impl<T: Clone, U> TypedSize3D<T, U> {
    /// Constructor taking scalar strongly typed lengths.
    pub fn from_lengths(width: Length<T, U>, height: Length<T, U>, depth: Length<T, U>) -> Self {
        TypedSize3D::new(width.get(), height.get(), depth.get())
    }
}

impl<T: Round, U> TypedSize3D<T, U> {
    /// Rounds each component to the nearest integer value.
    ///
    /// This behavior is preserved for negative values (unlike the basic cast).
    pub fn round(&self) -> Self {
        TypedSize3D::new(self.width.round(), self.height.round(), self.depth.round())
    }
}

impl<T: Ceil, U> TypedSize3D<T, U> {
    /// Rounds each component to the smallest integer equal or greater than the original value.
    ///
    /// This behavior is preserved for negative values (unlike the basic cast).
    pub fn ceil(&self) -> Self {
        TypedSize3D::new(self.width.ceil(), self.height.ceil(), self.depth.ceil())
    }
}

impl<T: Floor, U> TypedSize3D<T, U> {
    /// Rounds each component to the biggest integer equal or lower than the original value.
    ///
    /// This behavior is preserved for negative values (unlike the basic cast).
    pub fn floor(&self) -> Self {
        TypedSize3D::new(self.width.floor(), self.height.floor(), self.depth.floor())
    }
}

impl<T: Copy + Add<T, Output = T>, U> Add for TypedSize3D<T, U> {
    type Output = Self;
    fn add(self, other: Self) -> Self {
        TypedSize3D::new(self.width + other.width, self.height + other.height, self.depth + other.depth)
    }
}

impl<T: Copy + Sub<T, Output = T>, U> Sub for TypedSize3D<T, U> {
    type Output = Self;
    fn sub(self, other: Self) -> Self {
        TypedSize3D::new(self.width - other.width, self.height - other.height, self.depth - other.depth)
    }
}

impl<T: Copy + Clone + Mul<T, Output=T>, U> TypedSize3D<T, U> {
    pub fn volume(&self) -> T {
        self.width * self.height * self.depth
    }
}

impl<T, U> TypedSize3D<T, U>
where
    T: Copy + One + Add<Output = T> + Sub<Output = T> + Mul<Output = T>,
{
    /// Linearly interpolate between this size and another size.
    ///
    /// `t` is expected to be between zero and one.
    #[inline]
    pub fn lerp(&self, other: Self, t: T) -> Self {
        let one_t = T::one() - t;
        size3(
            one_t * self.width + t * other.width,
            one_t * self.height + t * other.height,
            one_t * self.depth + t * other.depth,
        )
    }
}

impl<T: Zero + PartialOrd, U> TypedSize3D<T, U> {
    pub fn is_empty_or_negative(&self) -> bool {
        let zero = T::zero();
        self.width <= zero || self.height <= zero || self.depth <= zero
    }
}

impl<T: Zero, U> TypedSize3D<T, U> {
    pub fn zero() -> Self {
        TypedSize3D::new(Zero::zero(), Zero::zero(), Zero::zero())
    }
}

impl<T: Zero, U> Zero for TypedSize3D<T, U> {
    fn zero() -> Self {
        TypedSize3D::new(Zero::zero(), Zero::zero(), Zero::zero())
    }
}

impl<T: Copy + Mul<T, Output = T>, U> Mul<T> for TypedSize3D<T, U> {
    type Output = Self;
    #[inline]
    fn mul(self, scale: T) -> Self {
        TypedSize3D::new(self.width * scale, self.height * scale, self.depth * scale)
    }
}

impl<T: Copy + Div<T, Output = T>, U> Div<T> for TypedSize3D<T, U> {
    type Output = Self;
    #[inline]
    fn div(self, scale: T) -> Self {
        TypedSize3D::new(self.width / scale, self.height / scale, self.depth / scale)
    }
}

impl<T: Copy + Mul<T, Output = T>, U1, U2> Mul<TypedScale<T, U1, U2>> for TypedSize3D<T, U1> {
    type Output = TypedSize3D<T, U2>;
    #[inline]
    fn mul(self, scale: TypedScale<T, U1, U2>) -> TypedSize3D<T, U2> {
        TypedSize3D::new(self.width * scale.get(), self.height * scale.get(), self.depth * scale.get())
    }
}

impl<T: Copy + Div<T, Output = T>, U1, U2> Div<TypedScale<T, U1, U2>> for TypedSize3D<T, U2> {
    type Output = TypedSize3D<T, U1>;
    #[inline]
    fn div(self, scale: TypedScale<T, U1, U2>) -> TypedSize3D<T, U1> {
        TypedSize3D::new(self.width / scale.get(), self.height / scale.get(), self.depth / scale.get())
    }
}

impl<T: Copy, U> TypedSize3D<T, U> {
    /// Returns self.width as a Length carrying the unit.
    #[inline]
    pub fn width_typed(&self) -> Length<T, U> {
        Length::new(self.width)
    }

    /// Returns self.height as a Length carrying the unit.
    #[inline]
    pub fn height_typed(&self) -> Length<T, U> {
        Length::new(self.height)
    }

    /// Returns self.depth as a Length carrying the unit.
    #[inline]
    pub fn depth_typed(&self) -> Length<T, U> {
        Length::new(self.depth)
    }

    #[inline]
    pub fn to_array(&self) -> [T; 3] {
        [self.width, self.height, self.depth]
    }

    #[inline]
    pub fn to_vector(&self) -> TypedVector3D<T, U> {
        vec3(self.width, self.height, self.depth)
    }

    /// Drop the units, preserving only the numeric value.
    pub fn to_untyped(&self) -> Size3D<T> {
        TypedSize3D::new(self.width, self.height, self.depth)
    }

    /// Tag a unitless value with units.
    pub fn from_untyped(p: &Size3D<T>) -> Self {
        TypedSize3D::new(p.width, p.height, p.depth)
    }
}

impl<T: NumCast + Copy, Unit> TypedSize3D<T, Unit> {
    /// Cast from one numeric representation to another, preserving the units.
    ///
    /// When casting from floating point to integer coordinates, the decimals are truncated
    /// as one would expect from a simple cast, but this behavior does not always make sense
    /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
    pub fn cast<NewT: NumCast + Copy>(&self) -> TypedSize3D<NewT, Unit> {
        self.try_cast().unwrap()
    }

    /// Fallible cast from one numeric representation to another, preserving the units.
    ///
    /// When casting from floating point to integer coordinates, the decimals are truncated
    /// as one would expect from a simple cast, but this behavior does not always make sense
    /// geometrically. Consider using `round()`, `ceil()` or `floor()` before casting.
    pub fn try_cast<NewT: NumCast + Copy>(&self) -> Option<TypedSize3D<NewT, Unit>> {
        match (NumCast::from(self.width), NumCast::from(self.height), NumCast::from(self.depth)) {
            (Some(w), Some(h), Some(d)) => Some(TypedSize3D::new(w, h, d)),
            _ => None,
        }
    }

    // Convenience functions for common casts

    /// Cast into an `f32` size.
    pub fn to_f32(&self) -> TypedSize3D<f32, Unit> {
        self.cast()
    }

    /// Cast into an `f64` size.
    pub fn to_f64(&self) -> TypedSize3D<f64, Unit> {
        self.cast()
    }

    /// Cast into an `uint` size, truncating decimals if any.
    ///
    /// When casting from floating point sizes, it is worth considering whether
    /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
    /// the desired conversion behavior.
    pub fn to_usize(&self) -> TypedSize3D<usize, Unit> {
        self.cast()
    }

    /// Cast into an `u32` size, truncating decimals if any.
    ///
    /// When casting from floating point sizes, it is worth considering whether
    /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
    /// the desired conversion behavior.
    pub fn to_u32(&self) -> TypedSize3D<u32, Unit> {
        self.cast()
    }

    /// Cast into an `i32` size, truncating decimals if any.
    ///
    /// When casting from floating point sizes, it is worth considering whether
    /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
    /// the desired conversion behavior.
    pub fn to_i32(&self) -> TypedSize3D<i32, Unit> {
        self.cast()
    }

    /// Cast into an `i64` size, truncating decimals if any.
    ///
    /// When casting from floating point sizes, it is worth considering whether
    /// to `round()`, `ceil()` or `floor()` before the cast in order to obtain
    /// the desired conversion behavior.
    pub fn to_i64(&self) -> TypedSize3D<i64, Unit> {
        self.cast()
    }
}

impl<T, U> TypedSize3D<T, U>
where
    T: Signed,
{
    pub fn abs(&self) -> Self {
        size3(self.width.abs(), self.height.abs(), self.depth.abs())
    }

    pub fn is_positive(&self) -> bool {
        self.width.is_positive() && self.height.is_positive() && self.depth.is_positive()
    }
}

impl<T: PartialOrd, U> TypedSize3D<T, U> {
    pub fn greater_than(&self, other: &Self) -> BoolVector3D {
        BoolVector3D {
            x: self.width > other.width,
            y: self.height > other.height,
            z: self.depth > other.depth,
        }
    }

    pub fn lower_than(&self, other: &Self) -> BoolVector3D {
        BoolVector3D {
            x: self.width < other.width,
            y: self.height < other.height,
            z: self.depth < other.depth,
        }
    }
}


impl<T: PartialEq, U> TypedSize3D<T, U> {
    pub fn equal(&self, other: &Self) -> BoolVector3D {
        BoolVector3D {
            x: self.width == other.width,
            y: self.height == other.height,
            z: self.depth == other.depth,
        }
    }

    pub fn not_equal(&self, other: &Self) -> BoolVector3D {
        BoolVector3D {
            x: self.width != other.width,
            y: self.height != other.height,
            z: self.depth != other.depth,
        }
    }
}

impl<T: Float, U> TypedSize3D<T, U> {
    #[inline]
    pub fn min(self, other: Self) -> Self {
        size3(
            self.width.min(other.width),
            self.height.min(other.height),
            self.depth.min(other.depth),
        )
    }

    #[inline]
    pub fn max(self, other: Self) -> Self {
        size3(
            self.width.max(other.width),
            self.height.max(other.height),
            self.depth.max(other.depth),
        )
    }

    #[inline]
    pub fn clamp(&self, start: Self, end: Self) -> Self {
        self.max(start).min(end)
    }
}


/// Shorthand for `TypedSize3D::new(w, h, d)`.
pub fn size3<T, U>(w: T, h: T, d: T) -> TypedSize3D<T, U> {
    TypedSize3D::new(w, h, d)
}

#[cfg(feature = "mint")]
impl<T, U> From<mint::Vector3<T>> for TypedSize3D<T, U> {
    fn from(v: mint::Vector3<T>) -> Self {
        TypedSize3D {
            width: v.x,
            height: v.y,
            depth: v.z,
            _unit: PhantomData,
        }
    }
}
#[cfg(feature = "mint")]
impl<T, U> Into<mint::Vector3<T>> for TypedSize3D<T, U> {
    fn into(self) -> mint::Vector3<T> {
        mint::Vector3 {
            x: self.width,
            y: self.height,
            z: self.depth,
        }
    }
}