Skip to main content

skia_safe/core/
m44.rs

1use std::{
2    f32,
3    ops::{Add, AddAssign, Div, DivAssign, Index, Mul, MulAssign, Neg, Sub, SubAssign},
4    slice,
5};
6
7use skia_bindings::{self as sb, SkM44, SkV2, SkV3, SkV4};
8
9use crate::{Matrix, Rect, prelude::*, private::is_finite, scalar};
10
11#[repr(C)]
12#[derive(Copy, Clone, PartialEq, Default, Debug)]
13pub struct V2 {
14    pub x: f32,
15    pub y: f32,
16}
17
18native_transmutable!(SkV2, V2);
19
20impl V2 {
21    pub const fn new(x: f32, y: f32) -> Self {
22        Self { x, y }
23    }
24
25    pub fn dot(self, b: Self) -> scalar {
26        self.x * b.x + self.y * b.y
27    }
28
29    pub fn cross(self, b: Self) -> scalar {
30        self.x * b.y - self.y * b.x
31    }
32
33    #[must_use]
34    pub fn normalize(self) -> Self {
35        self * (1.0 / self.length())
36    }
37
38    pub fn length_squared(self) -> scalar {
39        Self::dot(self, self)
40    }
41
42    pub fn length(self) -> scalar {
43        self.length_squared().sqrt()
44    }
45
46    const COMPONENTS: usize = 2;
47
48    pub fn as_array(&self) -> &[f32; Self::COMPONENTS] {
49        unsafe { slice::from_raw_parts(&self.x, Self::COMPONENTS) }
50            .try_into()
51            .unwrap()
52    }
53
54    pub fn as_mut_array(&mut self) -> &mut [f32; Self::COMPONENTS] {
55        unsafe { slice::from_raw_parts_mut(&mut self.x, Self::COMPONENTS) }
56            .try_into()
57            .unwrap()
58    }
59}
60
61impl Neg for V2 {
62    type Output = Self;
63
64    fn neg(self) -> Self::Output {
65        Self::new(-self.x, -self.y)
66    }
67}
68
69impl Add for V2 {
70    type Output = Self;
71
72    fn add(self, v: Self) -> Self::Output {
73        Self::new(self.x + v.x, self.y + v.y)
74    }
75}
76
77impl Sub for V2 {
78    type Output = Self;
79
80    fn sub(self, v: Self) -> Self::Output {
81        Self::new(self.x - v.x, self.y - v.y)
82    }
83}
84
85impl Mul for V2 {
86    type Output = Self;
87
88    fn mul(self, v: Self) -> Self::Output {
89        Self::new(self.x * v.x, self.y * v.y)
90    }
91}
92
93impl Mul<scalar> for V2 {
94    type Output = Self;
95
96    fn mul(self, s: scalar) -> Self::Output {
97        Self::new(self.x * s, self.y * s)
98    }
99}
100
101impl Mul<V2> for scalar {
102    type Output = V2;
103
104    fn mul(self, v: V2) -> Self::Output {
105        V2::new(v.x * self, v.y * self)
106    }
107}
108
109impl Div<V2> for scalar {
110    type Output = V2;
111
112    fn div(self, v: V2) -> Self::Output {
113        V2::new(self / v.x, self / v.y)
114    }
115}
116
117impl Div<scalar> for V2 {
118    type Output = V2;
119    fn div(self, s: scalar) -> Self::Output {
120        V2::new(self.x / s, self.y / s)
121    }
122}
123
124impl AddAssign for V2 {
125    fn add_assign(&mut self, v: Self) {
126        *self = *self + v
127    }
128}
129
130impl SubAssign for V2 {
131    fn sub_assign(&mut self, v: Self) {
132        *self = *self - v
133    }
134}
135
136impl MulAssign for V2 {
137    fn mul_assign(&mut self, v: Self) {
138        *self = *self * v
139    }
140}
141
142impl MulAssign<scalar> for V2 {
143    fn mul_assign(&mut self, s: scalar) {
144        *self = *self * s
145    }
146}
147
148impl DivAssign<scalar> for V2 {
149    fn div_assign(&mut self, s: scalar) {
150        *self = *self / s
151    }
152}
153
154#[repr(C)]
155#[derive(Copy, Clone, PartialEq, Default, Debug)]
156pub struct V3 {
157    pub x: f32,
158    pub y: f32,
159    pub z: f32,
160}
161
162native_transmutable!(SkV3, V3);
163
164impl V3 {
165    pub const fn new(x: f32, y: f32, z: f32) -> Self {
166        Self { x, y, z }
167    }
168
169    pub fn dot(&self, b: &Self) -> scalar {
170        self.x * b.x + self.y * b.y + self.z * b.z
171    }
172
173    #[must_use]
174    pub fn cross(&self, b: &Self) -> Self {
175        Self::new(
176            self.y * b.z - self.z * b.y,
177            self.z * b.x - self.x * b.z,
178            self.x * b.y - self.y * b.x,
179        )
180    }
181
182    #[must_use]
183    pub fn normalize(&self) -> Self {
184        *self * (1.0 / self.length())
185    }
186
187    pub fn length_squared(&self) -> scalar {
188        Self::dot(self, self)
189    }
190
191    pub fn length(&self) -> scalar {
192        Self::dot(self, self).sqrt()
193    }
194
195    const COMPONENTS: usize = 3;
196
197    pub fn as_array(&self) -> &[f32; Self::COMPONENTS] {
198        unsafe { slice::from_raw_parts(&self.x, Self::COMPONENTS) }
199            .try_into()
200            .unwrap()
201    }
202
203    pub fn as_mut_array(&mut self) -> &mut [f32; Self::COMPONENTS] {
204        unsafe { slice::from_raw_parts_mut(&mut self.x, Self::COMPONENTS) }
205            .try_into()
206            .unwrap()
207    }
208}
209
210impl Neg for V3 {
211    type Output = Self;
212
213    fn neg(self) -> Self::Output {
214        Self::new(-self.x, -self.y, -self.z)
215    }
216}
217
218impl Add for V3 {
219    type Output = Self;
220
221    fn add(self, v: Self) -> Self::Output {
222        Self::new(self.x + v.x, self.y + v.y, self.z + v.z)
223    }
224}
225
226impl Sub for V3 {
227    type Output = Self;
228
229    fn sub(self, v: Self) -> Self::Output {
230        Self::new(self.x - v.x, self.y - v.y, self.z - v.z)
231    }
232}
233
234impl Mul for V3 {
235    type Output = Self;
236
237    fn mul(self, v: Self) -> Self::Output {
238        Self::new(self.x * v.x, self.y * v.y, self.z * v.z)
239    }
240}
241
242impl Mul<scalar> for V3 {
243    type Output = Self;
244
245    fn mul(self, s: scalar) -> Self::Output {
246        Self::new(self.x * s, self.y * s, self.z * s)
247    }
248}
249
250impl Mul<V3> for scalar {
251    type Output = V3;
252
253    fn mul(self, v: V3) -> Self::Output {
254        V3::new(v.x * self, v.y * self, v.z * self)
255    }
256}
257
258impl AddAssign for V3 {
259    fn add_assign(&mut self, v: Self) {
260        *self = *self + v
261    }
262}
263
264impl SubAssign for V3 {
265    fn sub_assign(&mut self, v: Self) {
266        *self = *self - v
267    }
268}
269
270impl MulAssign for V3 {
271    fn mul_assign(&mut self, v: Self) {
272        *self = *self * v
273    }
274}
275
276impl MulAssign<scalar> for V3 {
277    fn mul_assign(&mut self, s: scalar) {
278        *self = *self * s
279    }
280}
281
282#[repr(C)]
283#[derive(Copy, Clone, PartialEq, Default, Debug)]
284pub struct V4 {
285    pub x: f32,
286    pub y: f32,
287    pub z: f32,
288    pub w: f32,
289}
290
291native_transmutable!(SkV4, V4);
292
293impl V4 {
294    pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
295        Self { x, y, z, w }
296    }
297
298    pub fn length_squared(&self) -> scalar {
299        Self::dot(self, self)
300    }
301
302    pub fn length(&self) -> scalar {
303        scalar::sqrt(Self::dot(self, self))
304    }
305
306    pub fn dot(&self, b: &Self) -> scalar {
307        self.x * b.x + self.y * b.y + self.z * b.z + self.w * b.w
308    }
309
310    pub fn normalize(&self) -> Self {
311        (*self) * (1.0 / self.length())
312    }
313
314    const COMPONENTS: usize = 4;
315
316    pub fn as_array(&self) -> &[f32; Self::COMPONENTS] {
317        unsafe { slice::from_raw_parts(&self.x, Self::COMPONENTS) }
318            .try_into()
319            .unwrap()
320    }
321
322    pub fn as_mut_array(&mut self) -> &mut [f32; Self::COMPONENTS] {
323        unsafe { slice::from_raw_parts_mut(&mut self.x, Self::COMPONENTS) }
324            .try_into()
325            .unwrap()
326    }
327}
328
329impl Neg for V4 {
330    type Output = Self;
331
332    fn neg(self) -> Self::Output {
333        Self::new(-self.x, -self.y, -self.z, -self.w)
334    }
335}
336
337impl Add for V4 {
338    type Output = Self;
339
340    fn add(self, v: Self) -> Self::Output {
341        Self::new(self.x + v.x, self.y + v.y, self.z + v.z, self.w + v.w)
342    }
343}
344
345impl Sub for V4 {
346    type Output = Self;
347
348    fn sub(self, v: Self) -> Self::Output {
349        Self::new(self.x - v.x, self.y - v.y, self.z - v.z, self.w - v.w)
350    }
351}
352
353impl Mul for V4 {
354    type Output = Self;
355
356    fn mul(self, v: Self) -> Self::Output {
357        Self::new(self.x * v.x, self.y * v.y, self.z * v.z, self.w * v.w)
358    }
359}
360
361impl Mul<scalar> for V4 {
362    type Output = Self;
363
364    fn mul(self, s: scalar) -> Self::Output {
365        Self::new(self.x * s, self.y * s, self.z * s, self.w * s)
366    }
367}
368
369impl Mul<V4> for scalar {
370    type Output = V4;
371
372    fn mul(self, v: V4) -> Self::Output {
373        V4::new(v.x * self, v.y * self, v.z * self, v.w * self)
374    }
375}
376
377impl AddAssign for V4 {
378    fn add_assign(&mut self, v: Self) {
379        *self = *self + v
380    }
381}
382
383impl SubAssign for V4 {
384    fn sub_assign(&mut self, v: Self) {
385        *self = *self - v
386    }
387}
388
389impl MulAssign for V4 {
390    fn mul_assign(&mut self, v: Self) {
391        *self = *self * v
392    }
393}
394
395impl MulAssign<scalar> for V4 {
396    fn mul_assign(&mut self, s: scalar) {
397        *self = *self * s
398    }
399}
400
401impl Index<usize> for V4 {
402    type Output = f32;
403
404    fn index(&self, index: usize) -> &Self::Output {
405        &self.as_array()[index]
406    }
407}
408
409/// 4x4 matrix used by [`crate::Canvas`] and other parts of Skia.
410///
411/// Skia assumes a right-handed coordinate system:
412/// +X goes to the right
413/// +Y goes down
414/// +Z goes into the screen (away from the viewer)
415#[repr(C)]
416#[derive(Clone, Debug)]
417pub struct M44 {
418    mat: [f32; Self::COMPONENTS],
419}
420
421native_transmutable!(SkM44, M44);
422
423impl Default for M44 {
424    fn default() -> Self {
425        Self::new_identity()
426    }
427}
428
429impl PartialEq for M44 {
430    fn eq(&self, other: &Self) -> bool {
431        unsafe { sb::C_SkM44_equals(self.native(), other.native()) }
432    }
433}
434
435impl M44 {
436    const COMPONENTS: usize = 16;
437
438    pub const fn new_identity() -> Self {
439        Self {
440            mat: [
441                1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
442            ],
443        }
444    }
445
446    pub fn concat(a: &Self, b: &Self) -> Self {
447        let mut m = Self::default();
448        m.set_concat(a, b);
449        m
450    }
451
452    pub const fn nan() -> Self {
453        Self {
454            mat: [f32::NAN; Self::COMPONENTS],
455        }
456    }
457
458    #[allow(clippy::too_many_arguments)]
459    /// The constructor parameters are in row-major order.
460    pub const fn new(
461        m0: scalar,
462        m4: scalar,
463        m8: scalar,
464        m12: scalar,
465        m1: scalar,
466        m5: scalar,
467        m9: scalar,
468        m13: scalar,
469        m2: scalar,
470        m6: scalar,
471        m10: scalar,
472        m14: scalar,
473        m3: scalar,
474        m7: scalar,
475        m11: scalar,
476        m15: scalar,
477    ) -> Self {
478        Self {
479            mat: [
480                m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15,
481            ],
482        }
483    }
484
485    pub fn rows(r0: &V4, r1: &V4, r2: &V4, r3: &V4) -> Self {
486        let mut m = Self::default();
487        m.set_row(0, r0);
488        m.set_row(1, r1);
489        m.set_row(2, r2);
490        m.set_row(3, r3);
491        m
492    }
493
494    pub fn cols(c0: &V4, c1: &V4, c2: &V4, c3: &V4) -> Self {
495        let mut m = Self::default();
496        m.set_col(0, c0);
497        m.set_col(1, c1);
498        m.set_col(2, c2);
499        m.set_col(3, c3);
500        m
501    }
502
503    pub fn row_major(r: &[scalar; Self::COMPONENTS]) -> Self {
504        Self::new(
505            r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13],
506            r[14], r[15],
507        )
508    }
509
510    pub fn col_major(c: &[scalar; Self::COMPONENTS]) -> Self {
511        Self::new(
512            c[0], c[4], c[8], c[12], c[1], c[5], c[9], c[13], c[2], c[6], c[10], c[14], c[3], c[7],
513            c[11], c[15],
514        )
515    }
516
517    pub fn translate(x: scalar, y: scalar, z: scalar) -> Self {
518        Self::new(
519            1.0, 0.0, 0.0, x, 0.0, 1.0, 0.0, y, 0.0, 0.0, 1.0, z, 0.0, 0.0, 0.0, 1.0,
520        )
521    }
522
523    pub fn scale(x: scalar, y: scalar, z: scalar) -> Self {
524        Self::new(
525            x, 0.0, 0.0, 0.0, 0.0, y, 0.0, 0.0, 0.0, 0.0, z, 0.0, 0.0, 0.0, 0.0, 1.0,
526        )
527    }
528
529    pub fn rotate(axis: V3, radians: scalar) -> Self {
530        let mut m = Self::default();
531        m.set_rotate(axis, radians);
532        m
533    }
534
535    /// Scales and translates `src` to fill `dst` exactly.
536    ///
537    /// - `src` source rectangle
538    /// - `dst` destination rectangle
539    pub fn rect_to_rect(src: impl AsRef<Rect>, dst: impl AsRef<Rect>) -> Self {
540        let (src, dst) = (src.as_ref(), dst.as_ref());
541        Self::construct(|m| unsafe { sb::C_SkM44_RectToRect(src.native(), dst.native(), m) })
542    }
543
544    pub fn look_at(eye: &V3, center: &V3, up: &V3) -> Self {
545        Self::construct(|m| unsafe {
546            sb::C_SkM44_LookAt(eye.native(), center.native(), up.native(), m)
547        })
548    }
549
550    pub fn perspective(near: f32, far: f32, angle: f32) -> Self {
551        Self::construct(|m| unsafe { sb::C_SkM44_Perspective(near, far, angle, m) })
552    }
553
554    pub fn get_col_major(&self, v: &mut [scalar; Self::COMPONENTS]) {
555        v.copy_from_slice(&self.mat)
556    }
557
558    pub fn get_row_major(&self, v: &mut [scalar; Self::COMPONENTS]) {
559        unsafe { self.native().getRowMajor(v.as_mut_ptr()) }
560    }
561
562    #[deprecated(since = "0.30.0", note = "use M44::col_major() instead")]
563    pub fn set_col_major(&mut self, v: &[scalar; Self::COMPONENTS]) -> &mut Self {
564        *self = Self::col_major(v);
565        self
566    }
567
568    #[deprecated(since = "0.30.0", note = "use M44::row_major() instead")]
569    pub fn set_row_major(&mut self, v: &[scalar; Self::COMPONENTS]) -> &mut Self {
570        *self = Self::row_major(v);
571        self
572    }
573
574    #[allow(clippy::too_many_arguments)]
575    #[deprecated(since = "0.30.0", note = "use Self::new() instead")]
576    pub fn set_44(
577        &mut self,
578        m0: scalar,
579        m1: scalar,
580        m2: scalar,
581        m3: scalar,
582        m4: scalar,
583        m5: scalar,
584        m6: scalar,
585        m7: scalar,
586        m8: scalar,
587        m9: scalar,
588        m10: scalar,
589        m11: scalar,
590        m12: scalar,
591        m13: scalar,
592        m14: scalar,
593        m15: scalar,
594    ) -> &mut Self {
595        *self = Self::new(
596            m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15,
597        );
598        self
599    }
600
601    pub fn rc(&self, r: usize, c: usize) -> scalar {
602        assert!(r <= 3);
603        assert!(c <= 3);
604        self.mat[c * 4 + r]
605    }
606
607    pub fn set_rc(&mut self, r: usize, c: usize, value: scalar) {
608        assert!(r <= 3);
609        assert!(c <= 3);
610        self.mat[c * 4 + r] = value;
611    }
612
613    pub fn row(&self, i: usize) -> V4 {
614        assert!(i <= 3);
615        V4::new(
616            self.mat[i],
617            self.mat[i + 4],
618            self.mat[i + 8],
619            self.mat[i + 12],
620        )
621    }
622
623    pub fn col(&self, i: usize) -> V4 {
624        assert!(i <= 3);
625        V4::new(
626            self.mat[i * 4],
627            self.mat[i * 4 + 1],
628            self.mat[i * 4 + 2],
629            self.mat[i * 4 + 3],
630        )
631    }
632
633    pub fn set_row(&mut self, i: usize, v: &V4) {
634        assert!(i <= 3);
635        self.mat[i] = v.x;
636        self.mat[i + 4] = v.y;
637        self.mat[i + 8] = v.z;
638        self.mat[i + 12] = v.w;
639    }
640
641    pub fn set_col(&mut self, i: usize, v: &V4) {
642        assert!(i <= 3);
643        self.mat[(i * 4)..(i * 4 + V4::COMPONENTS)].copy_from_slice(v.as_array());
644    }
645
646    pub fn set_identity(&mut self) -> &mut Self {
647        *self = Self {
648            mat: [
649                1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
650            ],
651        };
652        self
653    }
654
655    pub fn set_translate(&mut self, x: scalar, y: scalar, z: scalar) -> &mut Self {
656        *self = Self {
657            mat: [
658                1.0, 0.0, 0.0, x, 0.0, 1.0, 0.0, y, 0.0, 0.0, 1.0, z, 0.0, 0.0, 0.0, 1.0,
659            ],
660        };
661        self
662    }
663
664    pub fn set_scale(&mut self, x: scalar, y: scalar, z: scalar) -> &mut Self {
665        *self = Self {
666            mat: [
667                x, 0.0, 0.0, 0.0, 0.0, y, 0.0, 0.0, 0.0, 0.0, z, 0.0, 0.0, 0.0, 0.0, 1.0,
668            ],
669        };
670        self
671    }
672
673    /// Sets this matrix to rotate about the specified unit-length axis vector, by an angle
674    /// specified by its sin and cos.
675    ///
676    /// This does not attempt to verify that `axis.length() == 1` or that the sin, cos values are
677    /// correct.
678    ///
679    /// - `axis` unit-length axis vector
680    /// - `sin_angle` sine of the rotation angle
681    /// - `cos_angle` cosine of the rotation angle
682    pub fn set_rotate_unit_sin_cos(
683        &mut self,
684        axis: V3,
685        sin_angle: scalar,
686        cos_angle: scalar,
687    ) -> &mut Self {
688        unsafe {
689            self.native_mut()
690                .setRotateUnitSinCos(axis.into_native(), sin_angle, cos_angle)
691        };
692        self
693    }
694
695    /// Sets this matrix to rotate about the specified unit-length axis vector, by an angle
696    /// specified in radians.
697    ///
698    /// This does not attempt to verify that `axis.length() == 1`.
699    ///
700    /// - `axis` unit-length axis vector
701    /// - `radians` rotation angle in radians
702    pub fn set_rotate_unit(&mut self, axis: V3, radians: scalar) -> &mut Self {
703        self.set_rotate_unit_sin_cos(axis, radians.sin(), radians.cos())
704    }
705
706    /// Sets this matrix to rotate about the specified axis vector, by an angle specified in
707    /// radians.
708    ///
709    /// Note: `axis` is not assumed to be unit-length, so it will be normalized internally. If
710    /// `axis` is already unit-length, call [`Self::set_rotate_unit()`] instead.
711    ///
712    /// - `axis` axis vector
713    /// - `radians` rotation angle in radians
714    pub fn set_rotate(&mut self, axis: V3, radians: scalar) -> &mut Self {
715        unsafe { self.native_mut().setRotate(axis.into_native(), radians) };
716        self
717    }
718
719    #[deprecated(since = "0.30.0", note = "use M44::col_major() and M44::set_concat()")]
720    pub fn set_concat_16(&mut self, a: &M44, col_major: &[scalar; Self::COMPONENTS]) -> &mut Self {
721        self.set_concat(a, &Self::col_major(col_major))
722    }
723
724    pub fn set_concat(&mut self, a: &M44, b: &M44) -> &mut Self {
725        unsafe {
726            self.native_mut().setConcat(a.native(), b.native());
727        }
728        self
729    }
730
731    #[deprecated(since = "0.30.0", note = "use M44::col_major() and M44::pre_concat()")]
732    #[allow(deprecated)]
733    pub fn pre_concat_16(&mut self, col_major: &[scalar; Self::COMPONENTS]) -> &mut Self {
734        self.set_concat_16(&self.clone(), col_major)
735    }
736
737    pub fn pre_concat(&mut self, m: &M44) -> &mut Self {
738        unsafe {
739            let self_ptr = self.native() as *const _;
740            self.native_mut().setConcat(self_ptr, m.native());
741        }
742        self
743    }
744
745    pub fn post_concat(&mut self, m: &M44) -> &mut Self {
746        unsafe {
747            let self_ptr = self.native() as *const _;
748            self.native_mut().setConcat(m.native(), self_ptr);
749        }
750        self
751    }
752
753    /// A matrix is categorized as 'perspective' if the bottom row is not [0, 0, 0, 1]. For most
754    /// uses, a bottom row of [0, 0, 0, X] behaves like a non-perspective matrix, though it will be
755    /// categorized as perspective. Calling this will change the matrix such that, if its bottom row
756    /// was [0, 0, 0, X], it will be changed to [0, 0, 0, 1] by scaling the rest of the matrix by
757    /// 1/X.
758    ///
759    /// ```text
760    /// | A B C D |    | A/X B/X C/X D/X |
761    /// | E F G H | -> | E/X F/X G/X H/X |   for X != 0
762    /// | I J K L |    | I/X J/X K/X L/X |
763    /// | 0 0 0 X |    |  0   0   0   1  |
764    /// ```
765    pub fn normalize_perspective(&mut self) {
766        unsafe { self.native_mut().normalizePerspective() }
767    }
768
769    /// Returns true if all elements of the matrix are finite. Returns false if any element is
770    /// infinity, or NaN.
771    pub fn is_finite(&self) -> bool {
772        is_finite(&self.mat)
773    }
774
775    /// If this is invertible, returns `Some(inverse)`. If it is not invertible, returns `None`.
776    #[must_use]
777    pub fn invert(&self) -> Option<M44> {
778        let mut m = Self::default();
779        unsafe { self.native().invert(m.native_mut()) }.then_some(m)
780    }
781
782    #[must_use]
783    pub fn transpose(&self) -> Self {
784        Self::construct(|m| unsafe { sb::C_SkM44_transpose(self.native(), m) })
785    }
786
787    pub fn dump(&self) {
788        unsafe { self.native().dump() }
789    }
790
791    pub fn map(&self, x: f32, y: f32, z: f32, w: f32) -> V4 {
792        V4::from_native_c(unsafe { sb::C_SkM44_map(self.native(), x, y, z, w) })
793    }
794
795    /// When converting from [`M44`] to [`Matrix`], the third row and column is dropped. When
796    /// converting from [`Matrix`] to [`M44`] the third row and column remain as identity:
797    ///
798    /// ```text
799    /// [ a b c ]      [ a b 0 c ]
800    /// [ d e f ]  ->  [ d e 0 f ]
801    /// [ g h i ]      [ 0 0 1 0 ]
802    ///                [ g h 0 i ]
803    /// ```
804    pub fn to_m33(&self) -> Matrix {
805        let m = &self.mat;
806        Matrix::new_all(m[0], m[4], m[12], m[1], m[5], m[13], m[3], m[7], m[15])
807    }
808
809    pub fn pre_translate(
810        &mut self,
811        x: scalar,
812        y: scalar,
813        z: impl Into<Option<scalar>>,
814    ) -> &mut Self {
815        unsafe {
816            self.native_mut()
817                .preTranslate(x, y, z.into().unwrap_or(0.0))
818        };
819        self
820    }
821
822    pub fn post_translate(
823        &mut self,
824        x: scalar,
825        y: scalar,
826        z: impl Into<Option<scalar>>,
827    ) -> &mut Self {
828        unsafe {
829            self.native_mut()
830                .postTranslate(x, y, z.into().unwrap_or(0.0))
831        };
832        self
833    }
834
835    pub fn pre_scale(&mut self, x: scalar, y: scalar) -> &mut Self {
836        unsafe { self.native_mut().preScale(x, y) };
837        self
838    }
839
840    pub fn pre_scale_xyz(&mut self, x: scalar, y: scalar, z: scalar) -> &mut Self {
841        unsafe { self.native_mut().preScale1(x, y, z) };
842        self
843    }
844}
845
846impl Mul for &M44 {
847    type Output = M44;
848
849    fn mul(self, m: Self) -> Self::Output {
850        M44::concat(self, m)
851    }
852}
853
854impl Mul<V4> for &M44 {
855    type Output = V4;
856
857    fn mul(self, v: V4) -> Self::Output {
858        self.map(v.x, v.y, v.z, v.w)
859    }
860}
861
862impl Mul<V3> for &M44 {
863    type Output = V3;
864
865    fn mul(self, v: V3) -> Self::Output {
866        let v4 = self.map(v.x, v.y, v.z, 0.0);
867        V3::new(v4.x, v4.y, v4.z)
868    }
869}
870
871impl From<&Matrix> for M44 {
872    fn from(src: &Matrix) -> Self {
873        use crate::matrix::Member::*;
874
875        Self::new(
876            src[ScaleX],
877            src[SkewX],
878            0.0,
879            src[TransX],
880            src[SkewY],
881            src[ScaleY],
882            0.0,
883            src[TransY],
884            0.0,
885            0.0,
886            1.0,
887            0.0,
888            src[Persp0],
889            src[Persp1],
890            0.0,
891            src[Persp2],
892        )
893    }
894}
895
896impl From<Matrix> for M44 {
897    fn from(m: Matrix) -> Self {
898        M44::from(&m)
899    }
900}
901
902#[cfg(test)]
903mod tests {
904    use crate::{M44, Matrix, Rect, matrix};
905
906    #[test]
907    pub fn convert_from_matrix_and_back() {
908        // taken from skulpin's physics example.
909        let vr = Rect::new(-4.5, -4.0, 4.5, 2.0);
910        let dst = Rect::new(0.0, 0.0, 1350.0, 900.0);
911
912        #[allow(deprecated)]
913        let m = Matrix::from_rect_to_rect(vr, dst, matrix::ScaleToFit::Center).unwrap();
914        let m44 = M44::from(m);
915        let m3 = m44.to_m33();
916        assert_eq!(m, m3);
917    }
918}