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
use crate::prelude::*;
use crate::{scalar, Canvas, Matrix};
use skia_bindings as sb;
use skia_bindings::{Sk3DView, SkCamera3D, SkMatrix3D, SkPatch3D, SkPoint3D, SkUnit3D};

#[derive(Copy, Clone, PartialEq, Default, Debug)]
#[repr(C)]
pub struct Unit3D {
    pub x: scalar,
    pub y: scalar,
    pub z: scalar,
}

impl NativeTransmutable<SkUnit3D> for Unit3D {}

#[test]
fn test_unit_3d_layout() {
    Unit3D::test_layout();
}

impl Unit3D {
    pub fn set(&mut self, x: scalar, y: scalar, z: scalar) -> &mut Self {
        self.x = x;
        self.y = y;
        self.z = z;
        self
    }

    pub fn dot(a: &Self, b: &Self) -> scalar {
        unsafe { SkUnit3D::Dot(a.native(), b.native()) }
    }

    pub fn cross(a: &Self, b: &Self) -> Self {
        let mut r = Self::default();
        unsafe { SkUnit3D::Cross(a.native(), b.native(), r.native_mut()) };
        r
    }
}

#[derive(Copy, Clone, PartialEq, Default, Debug)]
#[repr(C)]
pub struct Point3D {
    pub x: scalar,
    pub y: scalar,
    pub z: scalar,
}

impl NativeTransmutable<SkPoint3D> for Point3D {}
#[test]
fn test_point_3d_layout() {
    Point3D::test_layout();
}

impl From<(scalar, scalar, scalar)> for Point3D {
    fn from((x, y, z): (scalar, scalar, scalar)) -> Self {
        Point3D { x, y, z }
    }
}

impl Point3D {
    pub fn set(&mut self, x: scalar, y: scalar, z: scalar) -> &mut Self {
        self.x = x;
        self.y = y;
        self.z = z;
        self
    }

    pub fn normalize(&self, unit: &mut Unit3D) -> scalar {
        unsafe { self.native().normalize(unit.native_mut()) }
    }
}

pub type Vector3D = Point3D;

// note: Default is an empty matrix, and not the identity matrix, which is generated with reset()!
#[derive(Clone, PartialEq, Default, Debug)]
#[repr(C)]
pub struct Matrix3D {
    pub mat: [[scalar; 4]; 3],
}

impl NativeTransmutable<SkMatrix3D> for Matrix3D {}
#[test]
fn test_matrix_3d_layout() {
    Matrix3D::test_layout();
}

impl Matrix3D {
    pub fn reset(&mut self) -> &mut Self {
        unsafe {
            self.native_mut().reset();
        }
        self
    }

    pub fn set_row(
        &mut self,
        row: usize,
        a: scalar,
        b: scalar,
        c: scalar,
        d: impl Into<Option<scalar>>,
    ) -> &mut Self {
        debug_assert!(row < self.mat.len());
        let mat = &mut self.mat;
        mat[row][0] = a;
        mat[row][1] = b;
        mat[row][2] = c;
        mat[row][3] = d.into().unwrap_or_default();
        self
    }

    pub fn set_rotate_x(&mut self, deg: scalar) -> &mut Self {
        unsafe { self.native_mut().setRotateX(deg) }
        self
    }

    pub fn set_rotate_y(&mut self, deg: scalar) -> &mut Self {
        unsafe { self.native_mut().setRotateY(deg) }
        self
    }

    pub fn set_rotate_z(&mut self, deg: scalar) -> &mut Self {
        unsafe { self.native_mut().setRotateZ(deg) }
        self
    }

    pub fn set_translate(&mut self, x: scalar, y: scalar, z: scalar) -> &mut Self {
        unsafe { self.native_mut().setTranslate(x, y, z) }
        self
    }

    pub fn pre_rotate_x(&mut self, deg: scalar) -> &mut Self {
        unsafe { self.native_mut().preRotateX(deg) }
        self
    }

    pub fn pre_rotate_y(&mut self, deg: scalar) -> &mut Self {
        unsafe { self.native_mut().preRotateY(deg) }
        self
    }

    pub fn pre_rotate_z(&mut self, deg: scalar) -> &mut Self {
        unsafe { self.native_mut().preRotateZ(deg) }
        self
    }

    pub fn pre_translate(&mut self, x: scalar, y: scalar, z: scalar) -> &mut Self {
        unsafe { self.native_mut().preTranslate(x, y, z) }
        self
    }

    pub fn set_concat(&mut self, a: &Self, b: &Self) -> &mut Self {
        unsafe { self.native_mut().setConcat(a.native(), b.native()) }
        self
    }

    pub fn map_point(&self, src: impl Into<Point3D>) -> Point3D {
        let mut dst = Point3D::default();
        unsafe {
            self.native()
                .mapPoint(src.into().native(), dst.native_mut())
        };
        dst
    }

    pub fn map_vector(&self, src: impl Into<Vector3D>) -> Vector3D {
        let mut dst = Vector3D::default();
        unsafe {
            self.native()
                .mapVector(src.into().native(), dst.native_mut())
        };
        dst
    }
}

#[derive(Clone, PartialEq, Debug)]
#[repr(C)]
pub struct Patch3D {
    pub u: Vector3D,
    pub v: Vector3D,
    pub origin: Point3D,
}

impl NativeTransmutable<SkPatch3D> for Patch3D {}
#[test]
fn test_patch_3d_layout() {
    Patch3D::test_layout();
}

impl Default for Patch3D {
    fn default() -> Self {
        Patch3D::from_native(unsafe { SkPatch3D::new() })
    }
}

impl Patch3D {
    pub fn reset(&mut self) -> &mut Self {
        unsafe { self.native_mut().reset() }
        self
    }

    pub fn transform(&self, m: &Matrix3D) -> Self {
        let mut dst = Patch3D::default();
        unsafe { self.native().transform(m.native(), dst.native_mut()) }
        dst
    }

    pub fn dot_with(&self, v: impl Into<Vector3D>) -> scalar {
        let v = v.into();
        unsafe { self.native().dotWith(v.x, v.y, v.z) }
    }
}

#[derive(Clone, PartialEq, Debug)]
#[repr(C)]
pub struct Camera3D {
    pub location: Point3D,
    pub axis: Point3D,
    pub zenith: Point3D,
    pub observer: Point3D,
    orientation: Matrix,
    need_to_update: bool,
}

impl NativeTransmutable<SkCamera3D> for Camera3D {}
#[test]
fn test_camera_3d_layout() {
    Camera3D::test_layout();
}

impl Default for Camera3D {
    fn default() -> Self {
        Camera3D::from_native(unsafe { SkCamera3D::new() })
    }
}

impl Camera3D {
    pub fn reset(&mut self) -> &mut Self {
        unsafe { self.native_mut().reset() }
        self
    }

    pub fn update(&mut self) -> &mut Self {
        unsafe { self.native_mut().update() }
        self
    }

    pub fn patch_to_matrix(&self, quilt: &Patch3D) -> Matrix {
        let mut matrix = Matrix::default();
        unsafe {
            self.native()
                .patchToMatrix(quilt.native(), matrix.native_mut())
        }
        matrix
    }
}

// Note: original name is Sk3DView not SkView3D
// Also note that the implementation uses interior pointers,
// so we let Skia do the allocation.

pub type View3D = RefHandle<Sk3DView>;
unsafe impl Send for View3D {}

impl Default for View3D {
    fn default() -> Self {
        Self::new()
    }
}

impl NativeDrop for Sk3DView {
    fn drop(&mut self) {
        unsafe { sb::C_Sk3DView_delete(self) }
    }
}

impl RefHandle<Sk3DView> {
    pub fn new() -> Self {
        View3D::from_ptr(unsafe { sb::C_Sk3DView_new() }).unwrap()
    }

    pub fn save(&mut self) -> &mut Self {
        unsafe { self.native_mut().save() }
        self
    }

    pub fn restore(&mut self) -> &mut Self {
        unsafe { self.native_mut().restore() }
        self
    }

    pub fn translate(&mut self, d: impl Into<Vector3D>) -> &mut Self {
        let d = d.into();
        unsafe { self.native_mut().translate(d.x, d.y, d.z) }
        self
    }

    pub fn rotate_x(&mut self, deg: scalar) -> &mut Self {
        unsafe { self.native_mut().rotateX(deg) }
        self
    }

    pub fn rotate_y(&mut self, deg: scalar) -> &mut Self {
        unsafe { self.native_mut().rotateY(deg) }
        self
    }

    pub fn rotate_z(&mut self, deg: scalar) -> &mut Self {
        unsafe { self.native_mut().rotateZ(deg) }
        self
    }

    pub fn matrix(&self) -> Matrix {
        let mut m = Matrix::default();
        unsafe { self.native().getMatrix(m.native_mut()) }
        m
    }

    pub fn apply_to_canvas(&self, mut canvas: impl AsMut<Canvas>) -> &Self {
        unsafe { self.native().applyToCanvas(canvas.as_mut().native_mut()) }
        self
    }

    pub fn dot_with_normal(&self, d: impl Into<Vector3D>) -> scalar {
        let d = d.into();
        unsafe { self.native().dotWithNormal(d.x, d.y, d.z) }
    }
}

#[test]
fn test_canvas_passing_syntax() {
    use crate::utils::new_null_canvas;
    use crate::Surface;

    let mut null_canvas = new_null_canvas();
    let view = View3D::default();
    // as mutable reference
    view.apply_to_canvas(&mut null_canvas);
    // moved
    view.apply_to_canvas(null_canvas);

    // and one with a mutable reference to a shared Canvas:
    let mut surface = Surface::new_raster_n32_premul((100, 100)).unwrap();
    view.apply_to_canvas(surface.canvas());
}