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
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::Matrix;
use crate::Point3D;
use crate::Vec3;
use crate::Vec4;
use glib::translate::*;

impl Matrix {
    /// Initializes a [`Matrix`][crate::Matrix] with the given array of floating
    /// point values.
    /// ## `v`
    /// an array of at least 16 floating
    ///  point values
    ///
    /// # Returns
    ///
    /// the initialized matrix
    #[doc(alias = "graphene_matrix_init_from_float")]
    pub fn init_from_float(&mut self, v: &[f32; 16]) {
        unsafe {
            ffi::graphene_matrix_init_from_float(self.to_glib_none_mut().0, v as *const _);
        }
    }

    #[doc(alias = "graphene_matrix_init_from_2d")]
    #[doc(alias = "new_from_2d")]
    pub fn from_2d(xx: f64, yx: f64, xy: f64, yy: f64, x_0: f64, y_0: f64) -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_from_2d(alloc, xx, yx, xy, yy, x_0, y_0);
            from_glib_full(alloc)
        }
    }

    #[doc(alias = "graphene_matrix_init_from_float")]
    #[doc(alias = "new_from_float")]
    pub fn from_float(v: &[f32; 16]) -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_from_float(alloc, v as *const _);
            from_glib_full(alloc)
        }
    }

    #[doc(alias = "graphene_matrix_init_from_matrix")]
    #[doc(alias = "new_from_matrix")]
    pub fn from_matrix(src: &Matrix) -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_from_matrix(alloc, src.to_glib_none().0);
            from_glib_full(alloc)
        }
    }

    #[doc(alias = "graphene_matrix_init_from_vec4")]
    #[doc(alias = "new_from_vec4")]
    pub fn from_vec4(v0: &Vec4, v1: &Vec4, v2: &Vec4, v3: &Vec4) -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_from_vec4(
                alloc,
                v0.to_glib_none().0,
                v1.to_glib_none().0,
                v2.to_glib_none().0,
                v3.to_glib_none().0,
            );
            from_glib_full(alloc)
        }
    }

    #[doc(alias = "graphene_matrix_init_frustum")]
    pub fn new_frustum(
        left: f32,
        right: f32,
        bottom: f32,
        top: f32,
        z_near: f32,
        z_far: f32,
    ) -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_frustum(alloc, left, right, bottom, top, z_near, z_far);
            from_glib_full(alloc)
        }
    }

    #[doc(alias = "graphene_matrix_init_identity")]
    pub fn new_identity() -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_identity(alloc);
            from_glib_full(alloc)
        }
    }

    #[doc(alias = "graphene_matrix_init_look_at")]
    pub fn new_look_at(eye: &Vec3, center: &Vec3, up: &Vec3) -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_look_at(
                alloc,
                eye.to_glib_none().0,
                center.to_glib_none().0,
                up.to_glib_none().0,
            );
            from_glib_full(alloc)
        }
    }

    #[doc(alias = "graphene_matrix_init_ortho")]
    pub fn new_ortho(
        left: f32,
        right: f32,
        top: f32,
        bottom: f32,
        z_near: f32,
        z_far: f32,
    ) -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_ortho(alloc, left, right, top, bottom, z_near, z_far);
            from_glib_full(alloc)
        }
    }

    #[doc(alias = "graphene_matrix_init_perspective")]
    pub fn new_perspective(fovy: f32, aspect: f32, z_near: f32, z_far: f32) -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_perspective(alloc, fovy, aspect, z_near, z_far);
            from_glib_full(alloc)
        }
    }

    #[doc(alias = "graphene_matrix_init_rotate")]
    pub fn new_rotate(angle: f32, axis: &Vec3) -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_rotate(alloc, angle, axis.to_glib_none().0);
            from_glib_full(alloc)
        }
    }

    #[doc(alias = "graphene_matrix_init_scale")]
    pub fn new_scale(x: f32, y: f32, z: f32) -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_scale(alloc, x, y, z);
            from_glib_full(alloc)
        }
    }

    #[doc(alias = "graphene_matrix_init_skew")]
    pub fn new_skew(x_skew: f32, y_skew: f32) -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_skew(alloc, x_skew, y_skew);
            from_glib_full(alloc)
        }
    }

    #[doc(alias = "graphene_matrix_init_translate")]
    pub fn new_translate(p: &Point3D) -> Matrix {
        assert_initialized_main_thread!();
        unsafe {
            let alloc = ffi::graphene_matrix_alloc();
            ffi::graphene_matrix_init_translate(alloc, p.to_glib_none().0);
            from_glib_full(alloc)
        }
    }

    /// Converts a [`Matrix`][crate::Matrix] to an array of floating point
    /// values.
    ///
    /// # Returns
    ///
    ///
    /// ## `v`
    /// return location
    ///  for an array of floating point values. The array must be capable
    ///  of holding at least 16 values.
    #[doc(alias = "graphene_matrix_to_float")]
    pub fn to_float(&self) -> [f32; 16] {
        unsafe {
            let mut out = std::mem::MaybeUninit::uninit();
            ffi::graphene_matrix_to_float(self.to_glib_none().0, out.as_mut_ptr());
            out.assume_init()
        }
    }
}