ombre 0.6.7

Shadowy game and graphics library for Rust
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
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
// (c) 2013-2014 The CGMath Developers
// (c) 2012-2013 Mozilla Foundation
// (c) 2021-2022 Alexis Sellier
//
// Code in this module was borrowed from the excellent `euler` crate.

//! Matrix transforms.
use std::marker::PhantomData;
use std::ops::{Add, Div, Mul, Sub};

use super::algebra::*;
use super::traits::*;
use super::Size;

/// A 2D affine transform.
///
/// Transforms can be parametrized over the source and destination units, to describe a
/// transformation from a space to another.
///
/// For example, `Transform2D<f32, WorldSpace, ScreenSpace>::transform_point4d`
/// takes a `Point2D<f32, WorldSpace>` and returns a `Point2D<f32, ScreenSpace>`.
///
/// The matrix representation is conceptually equivalent to a 3 by 3 matrix transformation
/// compressed to 3 by 2 with the components that aren't needed to describe the set of 2d
/// transformations we are interested in implicitly defined:
///
/// ```text
///  | m11 m12 0 |   |x|   |x'|
///  | m21 m22 0 | x |y| = |y'|
///  | m31 m32 1 |   |1|   |w |
/// ```
///
/// When translating Transform2D into general matrix representations, consider that the
/// representation follows the column-major notation with column vectors.
///
/// The translation terms are m31 and m32.
#[repr(C)]
#[rustfmt::skip]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Transform2D<Src = (), Dst = ()> {
    pub m11: f32, pub m12: f32,
    pub m21: f32, pub m22: f32,
    pub m31: f32, pub m32: f32,
    #[doc(hidden)]
    pub _unit: PhantomData<(Src, Dst)>,
}

impl<Src, Dst> Transform2D<Src, Dst> {
    /// Identity matrix.
    pub const IDENTITY: Self = Self::identity();

    /// Create a transform specifying its components in using the column-major-column-vector
    /// matrix notation.
    ///
    /// For example, the translation terms m31 and m32 are the last two parameters parameters.
    ///
    /// ```
    /// use ombre::math::Transform2D;
    ///
    /// let tx = 1.0;
    /// let ty = 2.0;
    ///
    /// let translation: Transform2D<(), ()> = Transform2D::new(
    ///   1.0, 0.0,
    ///   0.0, 1.0,
    ///   tx,  ty,
    /// );
    /// ```
    #[rustfmt::skip]
    pub const fn new(m11: f32, m12: f32, m21: f32, m22: f32, m31: f32, m32: f32) -> Self {
        Transform2D {
            m11, m12,
            m21, m22,
            m31, m32,

            _unit: PhantomData,
        }
    }

    /// Create an identity matrix:
    ///
    /// ```text
    /// 1 0
    /// 0 1
    /// 0 0
    /// ```
    #[inline]
    pub const fn identity() -> Self {
        Self::translate(Vector2D::ZERO)
    }

    /// Create a 2d translation transform:
    ///
    /// ```text
    /// 1 0
    /// 0 1
    /// x y
    /// ```
    #[inline]
    pub const fn translate(v: Vector2D<f32>) -> Self {
        Self::new(1., 0., 0., 1., v.x, v.y)
    }

    /// Get the translation component.
    #[inline]
    pub const fn translation(&self) -> Vector2D<f32> {
        Vector2D::new(self.m31, self.m32)
    }

    /// Create a 2d scale transform:
    ///
    /// ```text
    /// s 0
    /// 0 s
    /// 0 0
    /// ```
    #[inline]
    pub const fn scale(s: f32) -> Self {
        Self::new(s, 0., 0., s, 0., 0.)
    }

    /// Computes and returns the determinant of this transform.
    pub fn determinant(&self) -> f32 {
        self.m11 * self.m22 - self.m12 * self.m21
    }

    /// Returns whether it is possible to compute the inverse transform.
    #[inline]
    pub fn is_invertible(&self) -> bool {
        self.determinant() != 0.
    }

    /// Returns the inverse transform if possible.
    pub fn inverse(self) -> Transform2D<Dst, Src> {
        let idet = self.determinant().recip();

        Transform2D::new(
            idet * self.m22,
            idet * (0. - self.m12),
            idet * (0. - self.m21),
            idet * self.m11,
            idet * (self.m21 * self.m32 - self.m22 * self.m31),
            idet * (self.m31 * self.m12 - self.m11 * self.m32),
        )
    }
}

impl<Src, Dst, NewDst> Mul<Transform2D<Dst, NewDst>> for Transform2D<Src, Dst> {
    type Output = Transform2D<Src, NewDst>;

    /// Returns the multiplication of the two matrices such that mat's transformation
    /// applies after self's transformation.
    #[must_use]
    fn mul(self, mat: Transform2D<Dst, NewDst>) -> Transform2D<Src, NewDst> {
        Transform2D::new(
            self.m11 * mat.m11 + self.m12 * mat.m21,
            self.m11 * mat.m12 + self.m12 * mat.m22,
            self.m21 * mat.m11 + self.m22 * mat.m21,
            self.m21 * mat.m12 + self.m22 * mat.m22,
            self.m31 * mat.m11 + self.m32 * mat.m21 + mat.m31,
            self.m31 * mat.m12 + self.m32 * mat.m22 + mat.m32,
        )
    }
}

impl Mul<Point2D<f32>> for Transform2D {
    type Output = Point2D<f32>;

    #[inline]
    fn mul(self, point: Point2D<f32>) -> Point2D<f32> {
        Point2D::new(
            point.x * self.m11 + point.y * self.m21 + self.m31,
            point.x * self.m12 + point.y * self.m22 + self.m32,
        )
    }
}

impl Mul<Vector2D<f32>> for Transform2D {
    type Output = Vector2D<f32>;

    #[inline]
    fn mul(self, v: Vector2D<f32>) -> Vector2D<f32> {
        Vector2D::new(
            v.x * self.m11 + v.y * self.m21 + self.m31,
            v.x * self.m12 + v.y * self.m22 + self.m32,
        )
    }
}

/// A 4 x 4, column major matrix
///
/// This type is marked as `#[repr(C)]`.
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Default)]
pub struct Transform3D<S = f32, Src = (), Dst = ()> {
    /// The first column of the matrix.
    pub x: Vector4D<S>,
    /// The second column of the matrix.
    pub y: Vector4D<S>,
    /// The third column of the matrix.
    pub z: Vector4D<S>,
    /// The fourth column of the matrix.
    pub w: Vector4D<S>,

    #[doc(hidden)]
    pub _unit: PhantomData<(Src, Dst)>,
}

impl From<Transform3D<f32>> for [[f32; 4]; 4] {
    fn from(mat: Transform3D<f32>) -> Self {
        unsafe { std::mem::transmute(mat) }
    }
}

impl<S: Zero + One, Src, Dst> Transform3D<S, Src, Dst> {
    /// The zero matrix.
    #[rustfmt::skip]
    const fn zero() -> Self {
        Self::new(
            S::ZERO, S::ZERO, S::ZERO, S::ZERO,
            S::ZERO, S::ZERO, S::ZERO, S::ZERO,
            S::ZERO, S::ZERO, S::ZERO, S::ZERO,
            S::ZERO, S::ZERO, S::ZERO, S::ZERO,
        )
    }

    /// Create a new matrix, providing values for each index.
    #[inline]
    #[rustfmt::skip]
    pub const fn new(
        m11: S, m12: S, m13: S, m14: S,
        m21: S, m22: S, m23: S, m24: S,
        m31: S, m32: S, m33: S, m34: S,
        m41: S, m42: S, m43: S, m44: S,
    ) -> Self {
        Self {
            x: Vector4D::new(m11, m12, m13, m14),
            y: Vector4D::new(m21, m22, m23, m24),
            z: Vector4D::new(m31, m32, m33, m34),
            w: Vector4D::new(m41, m42, m43, m44),

            _unit: PhantomData,
        }
    }

    /// Create a transform representing a 2d transformation from the components
    /// of a 2 by 3 matrix transformation.
    ///
    /// Components follow the column-major-column-vector notation (m41 and m42
    /// representating the translation terms).
    ///
    /// ```text
    /// m11  m12   0   0
    /// m21  m22   0   0
    ///   0    0   1   0
    /// m41  m42   0   1
    /// ```
    #[inline]
    #[rustfmt::skip]
    pub const fn new_2d(m11: S, m12: S, m21: S, m22: S, m41: S, m42: S) -> Self
    where
        S: Zero + One,
    {
        Self::new(
            m11,     m12,     S::ZERO, S::ZERO,
            m21,     m22,     S::ZERO, S::ZERO,
            S::ZERO, S::ZERO, S::ONE,  S::ZERO,
            m41,     m42,     S::ZERO, S::ONE
       )
    }


    #[inline]
    #[rustfmt::skip]
    pub const fn identity() -> Self {
        Transform3D::new(
            S::ONE,  S::ZERO, S::ZERO, S::ZERO,
            S::ZERO, S::ONE,  S::ZERO, S::ZERO,
            S::ZERO, S::ZERO, S::ONE,  S::ZERO,
            S::ZERO, S::ZERO, S::ZERO, S::ONE,
        )
    }

    /// Create a homogeneous transformation matrix from a set of scale values.
    #[inline]
    #[rustfmt::skip]
    pub const fn from_nonuniform_scale(x: S, y: S, z: S) -> Transform3D<S, Src, Dst> {
        Transform3D::new(
            x,       S::ZERO, S::ZERO, S::ZERO,
            S::ZERO, y,       S::ZERO, S::ZERO,
            S::ZERO, S::ZERO, z,       S::ZERO,
            S::ZERO, S::ZERO, S::ZERO, S::ONE,
        )
    }
}

impl<S: Copy + Zero + One, Src, Dst> Transform3D<S, Src, Dst> {
    #[inline]
    pub fn row(&self, n: usize) -> Vector4D<S> {
        match n {
            0 => Vector4D::new(self.x.x, self.y.x, self.z.x, self.w.x),
            1 => Vector4D::new(self.x.y, self.y.y, self.z.y, self.w.y),
            2 => Vector4D::new(self.x.z, self.y.z, self.z.z, self.w.z),
            3 => Vector4D::new(self.x.w, self.y.w, self.z.w, self.w.w),
            _ => panic!("Transform3D::row: invalid row number: {n}"),
        }
    }

    #[inline]
    pub fn translation(&self) -> Vector3D<S> {
        Vector3D::new(self.w.x, self.w.y, self.w.z)
    }

    /// Create a homogeneous transformation matrix from a translation vector.
    #[inline]
    #[rustfmt::skip]
    pub fn from_translation(v: impl Into<Vector3D<S>>) -> Transform3D<S, Src, Dst> {
        let v = v.into();

        Transform3D::new(
            S::ONE,  S::ZERO, S::ZERO, S::ZERO,
            S::ZERO, S::ONE,  S::ZERO, S::ZERO,
            S::ZERO, S::ZERO, S::ONE,  S::ZERO,
            v.x,     v.y,     v.z,     S::ONE,
        )
    }

    #[inline]
    pub fn scale(&self) -> Vector3D<S> {
        Vector3D::new(self.x.x, self.y.y, self.z.z)
    }

    /// Create a homogeneous transformation matrix from a scale value.
    #[inline]
    pub fn from_scale(value: S) -> Transform3D<S, Src, Dst> {
        Transform3D::from_nonuniform_scale(value, value, value)
    }
}

impl<Src, Dst> Transform3D<f32, Src, Dst> {
    /// Create orthographic matrix.
    pub fn ortho(size: impl Into<Size<u32>>, origin: Origin) -> Self {
        let size = size.into();
        let (top, bottom) = match origin {
            Origin::BottomLeft => (size.h as f32, 0.),
            Origin::TopLeft => (0., size.h as f32),
        };
        Ortho::<f32> {
            left: 0.0,
            right: size.w as f32,
            bottom,
            top,
            near: -1.0,
            far: 1.0,
        }
        .into()
    }

    #[inline]
    pub fn offset(&self) -> Offset {
        Offset::new(self.w.x, self.w.y)
    }
}

impl<T, Src, Dst> Transform3D<T, Src, Dst>
where
    T: Copy
        + Add<T, Output = T>
        + Sub<T, Output = T>
        + Mul<T, Output = T>
        + Div<T, Output = T>
        + One
        + Zero,
{
    /// Returns whether it is possible to compute the inverse transform.
    #[inline]
    pub fn is_invertible(&self) -> bool {
        self.determinant() != Zero::ZERO
    }

    /// Return the inverse transform.
    ///
    /// Panics if the transform is not invertible.
    #[inline]
    pub fn inverse(&self) -> Transform3D<T, Dst, Src> {
        self.try_inverse().expect("Transform3d::inverse: matrix is not invertible")
    }

    /// Returns the inverse transform if possible.
    #[rustfmt::skip]
    pub fn try_inverse(&self) -> Option<Transform3D<T, Dst, Src>> {
        let det = self.determinant();

        if det == Zero::ZERO {
            return None;
        }

        let m = Transform3D::new(
            self.y.z * self.z.w * self.w.y - self.y.w * self.z.z * self.w.y +
            self.y.w * self.z.y * self.w.z - self.y.y * self.z.w * self.w.z -
            self.y.z * self.z.y * self.w.w + self.y.y * self.z.z * self.w.w,

            self.x.w * self.z.z * self.w.y - self.x.z * self.z.w * self.w.y -
            self.x.w * self.z.y * self.w.z + self.x.y * self.z.w * self.w.z +
            self.x.z * self.z.y * self.w.w - self.x.y * self.z.z * self.w.w,

            self.x.z * self.y.w * self.w.y - self.x.w * self.y.z * self.w.y +
            self.x.w * self.y.y * self.w.z - self.x.y * self.y.w * self.w.z -
            self.x.z * self.y.y * self.w.w + self.x.y * self.y.z * self.w.w,

            self.x.w * self.y.z * self.z.y - self.x.z * self.y.w * self.z.y -
            self.x.w * self.y.y * self.z.z + self.x.y * self.y.w * self.z.z +
            self.x.z * self.y.y * self.z.w - self.x.y * self.y.z * self.z.w,

            self.y.w * self.z.z * self.w.x - self.y.z * self.z.w * self.w.x -
            self.y.w * self.z.x * self.w.z + self.y.x * self.z.w * self.w.z +
            self.y.z * self.z.x * self.w.w - self.y.x * self.z.z * self.w.w,

            self.x.z * self.z.w * self.w.x - self.x.w * self.z.z * self.w.x +
            self.x.w * self.z.x * self.w.z - self.x.x * self.z.w * self.w.z -
            self.x.z * self.z.x * self.w.w + self.x.x * self.z.z * self.w.w,

            self.x.w * self.y.z * self.w.x - self.x.z * self.y.w * self.w.x -
            self.x.w * self.y.x * self.w.z + self.x.x * self.y.w * self.w.z +
            self.x.z * self.y.x * self.w.w - self.x.x * self.y.z * self.w.w,

            self.x.z * self.y.w * self.z.x - self.x.w * self.y.z * self.z.x +
            self.x.w * self.y.x * self.z.z - self.x.x * self.y.w * self.z.z -
            self.x.z * self.y.x * self.z.w + self.x.x * self.y.z * self.z.w,

            self.y.y * self.z.w * self.w.x - self.y.w * self.z.y * self.w.x +
            self.y.w * self.z.x * self.w.y - self.y.x * self.z.w * self.w.y -
            self.y.y * self.z.x * self.w.w + self.y.x * self.z.y * self.w.w,

            self.x.w * self.z.y * self.w.x - self.x.y * self.z.w * self.w.x -
            self.x.w * self.z.x * self.w.y + self.x.x * self.z.w * self.w.y +
            self.x.y * self.z.x * self.w.w - self.x.x * self.z.y * self.w.w,

            self.x.y * self.y.w * self.w.x - self.x.w * self.y.y * self.w.x +
            self.x.w * self.y.x * self.w.y - self.x.x * self.y.w * self.w.y -
            self.x.y * self.y.x * self.w.w + self.x.x * self.y.y * self.w.w,

            self.x.w * self.y.y * self.z.x - self.x.y * self.y.w * self.z.x -
            self.x.w * self.y.x * self.z.y + self.x.x * self.y.w * self.z.y +
            self.x.y * self.y.x * self.z.w - self.x.x * self.y.y * self.z.w,

            self.y.z * self.z.y * self.w.x - self.y.y * self.z.z * self.w.x -
            self.y.z * self.z.x * self.w.y + self.y.x * self.z.z * self.w.y +
            self.y.y * self.z.x * self.w.z - self.y.x * self.z.y * self.w.z,

            self.x.y * self.z.z * self.w.x - self.x.z * self.z.y * self.w.x +
            self.x.z * self.z.x * self.w.y - self.x.x * self.z.z * self.w.y -
            self.x.y * self.z.x * self.w.z + self.x.x * self.z.y * self.w.z,

            self.x.z * self.y.y * self.w.x - self.x.y * self.y.z * self.w.x -
            self.x.z * self.y.x * self.w.y + self.x.x * self.y.z * self.w.y +
            self.x.y * self.y.x * self.w.z - self.x.x * self.y.y * self.w.z,

            self.x.y * self.y.z * self.z.x - self.x.z * self.y.y * self.z.x +
            self.x.z * self.y.x * self.z.y - self.x.x * self.y.z * self.z.y -
            self.x.y * self.y.x * self.z.z + self.x.x * self.y.y * self.z.z
        );

        Some(m * (T::ONE / det))
    }

    /// Compute the determinant of the transform.
    #[rustfmt::skip]
    pub fn determinant(&self) -> T {
        self.x.w * self.y.z * self.z.y * self.w.x -
        self.x.z * self.y.w * self.z.y * self.w.x -
        self.x.w * self.y.y * self.z.z * self.w.x +
        self.x.y * self.y.w * self.z.z * self.w.x +
        self.x.z * self.y.y * self.z.w * self.w.x -
        self.x.y * self.y.z * self.z.w * self.w.x -
        self.x.w * self.y.z * self.z.x * self.w.y +
        self.x.z * self.y.w * self.z.x * self.w.y +
        self.x.w * self.y.x * self.z.z * self.w.y -
        self.x.x * self.y.w * self.z.z * self.w.y -
        self.x.z * self.y.x * self.z.w * self.w.y +
        self.x.x * self.y.z * self.z.w * self.w.y +
        self.x.w * self.y.y * self.z.x * self.w.z -
        self.x.y * self.y.w * self.z.x * self.w.z -
        self.x.w * self.y.x * self.z.y * self.w.z +
        self.x.x * self.y.w * self.z.y * self.w.z +
        self.x.y * self.y.x * self.z.w * self.w.z -
        self.x.x * self.y.y * self.z.w * self.w.z -
        self.x.z * self.y.y * self.z.x * self.w.w +
        self.x.y * self.y.z * self.z.x * self.w.w +
        self.x.z * self.y.x * self.z.y * self.w.w -
        self.x.x * self.y.z * self.z.y * self.w.w -
        self.x.y * self.y.x * self.z.z * self.w.w +
        self.x.x * self.y.y * self.z.z * self.w.w
    }

    /// *Look at* transform.
    pub fn look_at(eye: Vector3D<T>, center: Vector3D<T>, up: Vector3D<T>) -> Self
    where
        T: Float,
    {
        let mut res = Transform3D::<T, Src, Dst>::zero();

        let f = (center - eye).normalize();
        let s = Vector3D::cross(f, up).normalize();
        let u = Vector3D::cross(s, f);

        res.x.x = s.x;
        res.x.y = u.x;
        res.x.z = -f.x;

        res.y.x = s.y;
        res.y.y = u.y;
        res.y.z = -f.y;

        res.z.x = s.z;
        res.z.y = u.z;
        res.z.z = -f.z;

        res.w.x = -Vector3D::dot(s, eye);
        res.w.y = -Vector3D::dot(u, eye);
        res.w.z = Vector3D::dot(f, eye);
        res.w.w = T::ONE;

        res
    }
}

impl<Src, Dst> Transform3D<f32, Src, Dst> {
    /// Perspective projection.
    pub fn perspective(fov: f32, aspect: f32, near: f32, far: f32) -> Self {
        let mut res = Self::identity();
        let t = f32::tan(fov * (std::f32::consts::PI / 360.0));

        res.x.x = t.recip();
        res.y.y = aspect / t;
        res.z.w = -1.0;
        res.z.z = (near + far) / (near - far);
        res.w.z = (2.0 * near * far) / (near - far);
        res.w.w = 0.0;

        res
    }

    /// A rotation transform. The axis can be unnormalized.
    pub fn rotation(angle: f32, axis: Vector3D<f32>) -> Self {
        let mut res = Self::identity();
        let axis = axis.normalize();
        let sin_theta = f32::sin(angle.to_radians());
        let cos_theta = f32::cos(angle.to_radians());
        let cos = 1.0 - cos_theta;

        res.x.x = (axis.x * axis.x * cos) + cos_theta;
        res.x.y = (axis.x * axis.y * cos) + (axis.z * sin_theta);
        res.x.z = (axis.x * axis.z * cos) - (axis.y * sin_theta);
        res.y.x = (axis.y * axis.x * cos) - (axis.z * sin_theta);
        res.y.y = (axis.y * axis.y * cos) + cos_theta;
        res.y.z = (axis.y * axis.z * cos) + (axis.x * sin_theta);
        res.z.x = (axis.z * axis.x * cos) + (axis.y * sin_theta);
        res.z.y = (axis.z * axis.y * cos) - (axis.x * sin_theta);
        res.z.z = (axis.z * axis.z * cos) + cos_theta;

        res
    }
}

impl<S, Src, Dst, NewDst> Mul<Transform3D<S, Dst, NewDst>> for Transform3D<S, Src, Dst>
where
    S: Mul<Output = S> + Add<Output = S> + Copy,
{
    type Output = Transform3D<S, Src, NewDst>;

    #[rustfmt::skip]
    fn mul(self, rhs: Transform3D<S, Dst, NewDst>) -> Self::Output {
        let a = self.x;
        let b = self.y;
        let c = self.z;
        let d = self.w;

        Transform3D {
            x: a * rhs.x.x + b * rhs.x.y + c * rhs.x.z + d * rhs.x.w,
            y: a * rhs.y.x + b * rhs.y.y + c * rhs.y.z + d * rhs.y.w,
            z: a * rhs.z.x + b * rhs.z.y + c * rhs.z.z + d * rhs.z.w,
            w: a * rhs.w.x + b * rhs.w.y + c * rhs.w.z + d * rhs.w.w,

            _unit: PhantomData,
        }
    }
}

/// Transform a [`Vector2D`] with a [`Transform3D`].
///
/// ```
/// use ombre::math::*;
/// let m = Transform3D::from_translation(Vector3D::new(8., 8., 0.));
/// let v = Vector2D::new(1., 1.);
///
/// assert_eq!(m * v, Vector2D::new(9., 9.));
/// ```
impl Mul<Vector2D<f32>> for Transform3D<f32> {
    type Output = Vector2D<f32>;

    fn mul(self, vec: Vector2D<f32>) -> Vector2D<f32> {
        let vec = Vector4D::from(vec);
        Vector2D::new(self.row(0) * vec, self.row(1) * vec)
    }
}

/// Transform a [`Vector3D`] with a [`Transform3D`].
///
/// ```
/// use ombre::math::*;
/// let m = Transform3D::from_translation(Vector3D::new(8., 8., 0.));
/// let v = Vector3D::new(1., 1., 0.);
///
/// assert_eq!(m * v, Vector3D::new(9., 9., 0.));
/// ```
impl Mul<Vector3D<f32>> for Transform3D<f32> {
    type Output = Vector3D<f32>;

    fn mul(self, vec: Vector3D<f32>) -> Vector3D<f32> {
        let vec = Vector4D::from(vec);
        Vector3D::new(self.row(0) * vec, self.row(1) * vec, self.row(2) * vec)
    }
}

/// Transform a [`Vector4D`] with a [`Transform3D`].
///
/// ```
/// use ombre::math::*;
/// let m = Transform3D::from_translation(Vector3D::new(8., 8., 0.));
/// let v = Vector4D::new(1., 1., 0., 1.);
///
/// assert_eq!(m * v, Vector4D::new(9., 9., 0., 1.));
/// ```
impl Mul<Vector4D<f32>> for Transform3D<f32> {
    type Output = Vector4D<f32>;

    fn mul(self, vec: Vector4D<f32>) -> Vector4D<f32> {
        Vector4D::new(
            self.row(0) * vec,
            self.row(1) * vec,
            self.row(2) * vec,
            self.row(3) * vec,
        )
    }
}

/// Transform a [`Point2D`] with a [`Transform3D`].
///
/// ```
/// use ombre::math::*;
/// let m = Transform3D::from_translation(Vector3D::new(8., 8., 0.));
/// let p = Point2D::new(1., 1.);
///
/// assert_eq!(m * p, Point2D::new(9., 9.));
/// ```
impl Mul<Point2D<f32>> for Transform3D<f32> {
    type Output = Point2D<f32>;

    fn mul(self, p: Point2D<f32>) -> Point2D<f32> {
        let vec = Vector4D::new(p.x, p.y, 0., 1.);
        Point2D::new(self.row(0) * vec, self.row(1) * vec)
    }
}

/// Multiplies all of the transform's component by a scalar and returns the result.
impl<S: Copy + Zero + One + Mul<Output = S>, Src, Dst> Mul<S> for Transform3D<S, Src, Dst> {
    type Output = Self;

    #[rustfmt::skip]
    fn mul(self, x: S) -> Self {
        Transform3D::new(
            self.x.x * x, self.x.y * x, self.x.z * x, self.x.w * x,
            self.y.x * x, self.y.y * x, self.y.z * x, self.y.w * x,
            self.z.x * x, self.z.y * x, self.z.z * x, self.z.w * x,
            self.w.x * x, self.w.y * x, self.w.z * x, self.w.w * x
        )
    }
}

/// An orthographic projection with arbitrary left/right/bottom/top distances
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Ortho<S> {
    pub left: S,
    pub right: S,
    pub bottom: S,
    pub top: S,
    pub near: S,
    pub far: S,
}

impl<S: Float, Src, Dst> From<Ortho<S>> for Transform3D<S, Src, Dst> {
    #[rustfmt::skip]
    fn from(ortho: Ortho<S>) -> Self {
        let m11 = S::TWO / (ortho.right - ortho.left);
        let m12 = S::ZERO;
        let m13 = S::ZERO;
        let m14 = S::ZERO;

        let m21 = S::ZERO;
        let m22 = S::TWO / (ortho.top - ortho.bottom);
        let m23 = S::ZERO;
        let m24 = S::ZERO;

        let m31 = S::ZERO;
        let m32 = S::ZERO;
        let m33 = -S::TWO / (ortho.far - ortho.near);
        let m34 = S::ZERO;

        let m41 = -(ortho.right + ortho.left) / (ortho.right - ortho.left);
        let m42 = -(ortho.top + ortho.bottom) / (ortho.top - ortho.bottom);
        let m43 = -(ortho.far + ortho.near) / (ortho.far - ortho.near);
        let m44 = S::ONE;

        Transform3D::new(
            m11, m12, m13, m14,
            m21, m22, m23, m24,
            m31, m32, m33, m34,
            m41, m42, m43, m44,
        )
    }
}

impl<S: One + Zero + Copy> From<Point2D<S>> for Transform3D<S> {
    #[inline]
    fn from(other: Point2D<S>) -> Self {
        Transform3D::from_translation(Vector3D::new(other.x, other.y, S::ZERO))
    }
}

impl<S: One + Zero + Copy> From<Vector2D<S>> for Transform3D<S> {
    #[inline]
    fn from(other: Vector2D<S>) -> Self {
        Transform3D::from_translation(Vector3D::new(other.x, other.y, S::ZERO))
    }
}

impl<Src, Dst> From<Transform2D<Src, Dst>> for Transform3D<f32, Src, Dst> {
    /// Create a 3D transform from the current transform
    fn from(m: Transform2D<Src, Dst>) -> Self {
        Transform3D::new_2d(m.m11, m.m12, m.m21, m.m22, m.m31, m.m32)
    }
}