rust-assimp 0.0.23

A rust wrapper for assimp the open asset import library
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
//! Defines basic data types and primitives used by assimp.

use std::str;
use std::fmt;
// use std::f32::Float;
use core::num::Float;
use libc::{c_float, size_t, c_uchar, c_uint};
use std::ops::{Add, Div, Mul, Sub};

use vecmath as m;
use ffi;

/// Maximum dimension for strings, ASSIMP strings are zero terminated.
const MAXLEN : usize = 1024;

/// Boolean type used by assimp.
#[doc(hidden)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub enum AiBool {
    /// Represents false
    AiFalse = 0x0,
    /// Represents true
    AiTrue = 0x1,
}

impl AiBool {
    /// Creates a new `AiBool` from the builtin `bool`.
    pub fn new(val: bool) -> AiBool {
        match val {
            true => AiBool::AiTrue,
            false => AiBool::AiFalse,
        }
    }
}

///	Standard return type for some library functions.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub enum Return {
    /// Indicates that a function was successful
    Success = 0x0,

    /// Indicates that a function failed
    Failure = -0x1,

    /// Indicates that not enough memory was availabe to perform the requested
    /// operation
    OutOfMemory = -0x3,
}

/// Represents a plane in a three-dimensional, euclidean space.
///
/// The components are the coefficients in the equation
/// `ax + by + cz + d = 0`.
#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(C, packed)]
pub struct Plane {
    /// x coefficient in the plane equation
    pub a: c_float,
    /// y coefficient in the plane equation
    pub b: c_float,
    /// z coefficient in the plane equation
    pub c: c_float,
    /// constant in the plane equation
    pub d: c_float,
}

/// Represents a ray.
#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(C, packed)]
pub struct Ray {
    /// Position of the ray
    pub pos: Vector3D,
    /// Direction of the ray
    pub dir: Vector3D,
}

/// Represents a color in Red-Green-Blue space.
#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(C, packed)]
pub struct Color3D {
    /// Red component
    pub r: c_float,
    /// Green component
    pub g: c_float,
    /// Blue component
    pub b: c_float,
}

/// Represents a color in Red-Green-Blue-Alpha space.
#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(C, packed)]
pub struct Color4D {
    /// Red component
    pub r: c_float,
    /// Green component
    pub g: c_float,
    /// Blue component
    pub b: c_float,
    /// Alpha component
    pub a: c_float,
}

/// Stores the memory requirements for different components.
///
/// All sizes are in bytes. Returned by Scene::get_memory_info()
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct MemoryInfo {
    /// Storage allocated for texture data
    pub textures: c_uint,

    /// Storage allocated for material data
    pub materials: c_uint,

    /// Storage allocated for mesh data
    pub meshes: c_uint,

    /// Storage allocated for node data
    pub nodes: c_uint,

    /// Storage allocated for animation data
    pub animations: c_uint,

    /// Storage allocated for camera data
    pub cameras: c_uint,

    /// Storage allocated for light data
    pub lights: c_uint,

    /// Total storage allocated for the full import.
    pub total: c_uint,
}

/// Represents an UTF-8 string, zero byte terminated.
///
/// The character set of an `AiString` is explicitly defined to be UTF-8. This
/// Unicode transformation was chosen in the belief that most strings in 3d
/// files are limited to ASCII, thus the character set needed to be strictly
/// ASCII compatible.
///
/// Most text file loaders provide proper Unicode input file handling, special
/// unicode characters are correctly transcoded to UTF8 and are kept
/// throughout the libraries' import pipeline.
///
/// For most applications, it will be absolutely sufficient to interpret the
/// aiString as ASCII data and work with it as one would work with a plain
/// char*.
///
/// The (binary) length of such a string is limited to MAXLEN characters
/// (including the the terminating zero).
#[derive(Copy)]
#[repr(C, packed)]
pub struct AiString {
    /// Binary length of the string excluding the terminal 0. This is NOT the
    /// logical length of strings containing UTF-8 multibyte sequences! It's
    /// the number of bytes from the beginning of the string to its end.
    length: size_t,

    /// String buffer. Size limit is MAXLEN
    data: [c_uchar; MAXLEN],
}

impl AiString {
    /// Create a new empty string
    pub fn new() -> AiString {
        AiString {
            length: 0,
            data: [0u8; MAXLEN],
        }
    }

    /// Get a `str` representation of this `AiString`
    pub fn as_str(&self) -> Result<&str, str::Utf8Error> {
        str::from_utf8(&(self.data))
    }

    /// Get a `String` representation of this `AiString`
    pub fn into_string(&self) -> Option<String> {
        match String::from_utf8((self.data)
                                .to_vec()) {
            Err(_) => None,
            Ok(s) => Some(s),
        }
    }
}

impl fmt::Debug for AiString {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.as_str() {
            Ok(s) => write!(f, "{}", s),
            //Ok(s) => s.fmt(f),
            //_    => "".fmt(f),
            _    => write!(f, "{}", ""),
        }
    }
}

impl PartialEq for AiString {
    fn eq(&self, other: &AiString) -> bool {
        if self.length != other.length {
            return false
        }

        for i in 0 .. self.length as usize {
            if self.data[i] != other.data[i] {
                return false
            }
        }
        return true
    }
}

impl Clone for AiString {
    fn clone(&self) -> AiString {
        AiString {
            length: self.length,
            data: self.data,
        }
    }
}

impl fmt::Display for AiString {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.as_str() {
            Ok(s) => write!(f, "{}", s),
            _   => write!(f, "{}", ""),
        }
    }
}

/// Represents a vector in 2 dimensional space.
#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(C, packed)]
pub struct Vector2D {
    /// x component
    pub x: c_float,
    /// y component
    pub y: c_float,
}

impl Vector2D {
    /// Create an array representation of the vector
    pub fn to_array(&self) -> [c_float; 2] {
        [self.x, self.y]
    }

    /// Dot product
    #[inline(always)]
    pub fn dot(&self, other: &Vector2D) -> f32 {
        self.x * other.x +
        self.y * other.y
    }

    /// Calculate the norm of the vector
    #[inline]
    pub fn norm(&self) -> f32 {
        self.dot(self).sqrt()
    }

    /// Calculate the norm of the vector
    #[inline]
    pub fn rnorm(&self) -> f32 {
        self.dot(self).rsqrt()
    }

    /// Normalize the vector
    #[inline]
    pub fn normalize(&mut self) -> Vector2D {
        (*self) * self.rnorm()
    }
}

impl Add for Vector2D {
    type Output = Vector2D;

    fn add(self, rhs: Vector2D) -> Vector2D {
        Vector2D {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
        }
    }
}

impl Sub for Vector2D {
    type Output = Vector2D;

    fn sub(self, rhs: Vector2D) -> Vector2D {
        Vector2D {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}

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

    fn mul(self, rhs: f32) -> Vector2D {
        Vector2D {
            x: self.x * rhs,
            y: self.y * rhs,
        }
    }
}

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

    fn mul(self, rhs: Vector2D) -> Vector2D {
        Vector2D {
            x: self * rhs.x,
            y: self * rhs.y,
        }
    }
}

impl Div<f32> for Vector2D {
    type Output = Vector2D;

    fn div(self, rhs: f32) -> Vector2D {
        Vector2D {
            x: self.x / rhs,
            y: self.y / rhs,
        }
    }
}

/// Represents a vector in 3 dimensional space.
#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(C, packed)]
pub struct Vector3D {
    /// x component
    pub x: c_float,
    /// y component
    pub y: c_float,
    /// z component
    pub z: c_float,
}

impl Vector3D {
    /// Create an array representation of the vector
    pub fn to_array(&self) -> [c_float; 3] {
        [self.x, self.y, self.z]
    }

    /// Create a translation matrix from this vector
    pub fn translation_matrix(&self) -> Matrix4x4 {
        Matrix4x4 {
            a1: 1.0, a2: 0.0, a3: 0.0, a4: self.x,
            b1: 0.0, b2: 1.0, b3: 0.0, b4: self.y,
            c1: 0.0, c2: 0.0, c3: 1.0, c4: self.z,
            d1: 0.0, d2: 0.0, d3: 0.0, d4: 1.0,
        }
    }

    /// Create a scaling matrix from this vector
    pub fn scaling_matrix(&self) -> Matrix4x4 {
        Matrix4x4 {
            a1: self.x, a2: 0.0,    a3: 0.0,    a4: 0.0,
            b1: 0.0,    b2: self.y, b3: 0.0,    b4: 0.0,
            c1: 0.0,    c2: 0.0,    c3: self.z, c4: 0.0,
            d1: 0.0,    d2: 0.0,    d3: 0.0,    d4: 1.0,
        }
    }

    /// Dot product
    #[inline(always)]
    pub fn dot(&self, other: &Vector3D) -> f32 {
        self.x * other.x +
        self.y * other.y +
        self.z * other.z
    }

    /// Calculate the norm of the vector
    #[inline]
    pub fn norm(&self) -> f32 {
        self.dot(self).sqrt()
    }

    /// Calculate the norm of the vector
    #[inline]
    pub fn rnorm(&self) -> f32 {
        self.dot(self).rsqrt()
    }

    /// Normalize the vector
    #[inline]
    pub fn normalize(&mut self) -> Vector3D {
        (*self) * self.rnorm()
    }
}

impl Add for Vector3D {
    type Output = Vector3D;
    
    fn add(self, rhs: Vector3D) -> Vector3D {
        Vector3D {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            z: self.z + rhs.z,
        }
    }
}

impl Sub for Vector3D {
    type Output = Vector3D;
    
    fn sub(self, rhs: Vector3D) -> Vector3D {
        Vector3D {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
            z: self.z - rhs.z,
        }
    }
}

impl Mul<f32> for Vector3D {
    type Output = Vector3D;
    
    fn mul(self, rhs: f32) -> Vector3D {
        Vector3D {
            x: self.x * rhs,
            y: self.y * rhs,
            z: self.z * rhs,
        }
    }
}

impl Mul<Vector3D> for f32 {
    type Output = Vector3D;
    
    fn mul(self, rhs: Vector3D) -> Vector3D {
        Vector3D {
            x: self * rhs.x,
            y: self * rhs.y,
            z: self * rhs.z,
        }
    }
}

impl Div<f32> for Vector3D {
    type Output = Vector3D;
    
    fn div(self, rhs: f32) -> Vector3D {
        Vector3D {
            x: self.x / rhs,
            y: self.y / rhs,
            z: self.z / rhs,
        }
    }
}

/// Represents a quaternion.
#[allow(missing_docs)]
#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(C, packed)]
pub struct Quaternion {
    pub w: c_float,
    pub x: c_float,
    pub y: c_float,
    pub z: c_float,
}

impl Quaternion {
    fn zero() -> Quaternion {
        Quaternion { w: 0.0, x: 0.0, y: 0.0, z: 0.0 }
    }
}

impl Quaternion {
    /// Creates a rotation quaternion from the given matrix
    pub fn from_matrix(mat: &Matrix3x3) -> Quaternion {
        let mut quat: Quaternion = Quaternion::zero();
        unsafe {
            ffi::aiCreateQuaternionFromMatrix(&mut quat, mat);
        }
        quat
    }

    /// Create an array representation of the vector
    pub fn to_array(&self) -> [c_float; 4] {
        [self.w, self.x, self.y, self.z]
    }

    /// Create a rotation matrix from this quaternion
    pub fn rotation_matrix(&self) -> Matrix4x4 {
        let norm = self.dot(self);
        let s = if norm < 1e-6 {
            0.0
        } else {
            2.0 / norm
        };
        let (w, x, y, z) = (self.w, self.x, self.y, self.z);
        let wx = s*w*x; let wy = s*w*y; let wz = s*w*z;
        let xx = s*x*x; let xy = s*x*y; let xz = s*x*z;
        let yy = s*y*y; let yz = s*y*z; let zz = s*z*z;
        Matrix4x4 {
            a1:  1. - (yy + zz), a2:      (xy - wz), a3:      (xz + wy), a4: 0.0,
            b1:       (xy + wz), b2: 1. - (xx + zz), b3:      (yz - wx), b4: 0.0,
            c1:       (xz - wy), c2:      (yz + wx), c3: 1. - (xx + yy), c4: 0.0,
            d1:             0.0, d2:            0.0, d3:            0.0, d4: 1.0,
        }
    }

    /// Dot product
    #[inline(always)]
    pub fn dot(&self, other: &Quaternion) -> f32 {
        self.w * other.w +
        self.x * other.x +
        self.y * other.y +
        self.z * other.z
    }

    /// Calculate the norm of the quaternion
    #[inline]
    pub fn norm(&self) -> f32 {
        self.dot(self).sqrt()
    }

    /// Calculate the norm of the quaternion
    #[inline]
    pub fn rnorm(&self) -> f32 {
        self.dot(self).rsqrt()
    }

    /// Normalize the quaternion
    #[inline]
    pub fn normalize(&mut self) -> Quaternion {
        (*self) * self.rnorm()
    }
}

impl Add for Quaternion {
    type Output = Quaternion;
    
    fn add(self, rhs: Quaternion) -> Quaternion {
        Quaternion {
            w: self.w + rhs.w,
            x: self.x + rhs.x,
            y: self.y + rhs.y,
            z: self.z + rhs.z,
        }
    }
}

impl Sub for Quaternion {
    type Output = Quaternion;
    
    fn sub(self, rhs: Quaternion) -> Quaternion {
        Quaternion {
            w: self.w - rhs.w,
            x: self.x - rhs.x,
            y: self.y - rhs.y,
            z: self.z - rhs.z,
        }
    }
}

impl Mul<f32> for Quaternion {
    type Output = Quaternion;
    
    fn mul(self, rhs: f32) -> Quaternion {
        Quaternion {
            w: self.w * rhs,
            x: self.x * rhs,
            y: self.y * rhs,
            z: self.z * rhs,
        }
    }
}

impl Mul<Quaternion> for f32 {
    type Output = Quaternion;
    
    fn mul(self, rhs: Quaternion) -> Quaternion {
        Quaternion {
            w: self * rhs.w,
            x: self * rhs.x,
            y: self * rhs.y,
            z: self * rhs.z,
        }
    }
}

impl Div<f32> for Quaternion {
    type Output = Quaternion;
    
    fn div(self, rhs: f32) -> Quaternion {
        Quaternion {
            w: self.w / rhs,
            x: self.x / rhs,
            y: self.y / rhs,
            z: self.z / rhs,
        }
    }
}

/// Represents a 3x3 matrix.
#[allow(missing_docs)]
#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(C, packed)]
pub struct Matrix3x3 {
    pub a1: c_float, pub a2: c_float, pub a3: c_float,
    pub b1: c_float, pub b2: c_float, pub b3: c_float,
    pub c1: c_float, pub c2: c_float, pub c3: c_float,
}

impl Matrix3x3 {
    /// Create a 3x3 identity matrix
    pub fn identity() -> Matrix3x3 {
        Matrix3x3 {
            a1:  1.0, a2: 0.0, a3: 0.0,
            b1:  0.0, b2: 1.0, b3: 0.0,
            c1:  0.0, c2: 0.0, c3: 1.0,
        }
    }

    /// Compute the inverse of a 3x3 matrix
    pub fn inverse(self) -> Matrix3x3 {
        let inv = m::mat3_inv([
                    [self.a1, self.a2, self.a3],
                    [self.b1, self.b2, self.b3],
                    [self.c1, self.c2, self.c3],
                  ]);
        Matrix3x3 {
            a1:  inv[0][0], a2: inv[0][1], a3: inv[0][2],
            b1:  inv[1][0], b2: inv[1][1], b3: inv[1][2],
            c1:  inv[2][0], c2: inv[2][1], c3: inv[2][2],
        }
    }

    /// Returns the transpose of this matrix
    pub fn transpose(&self) -> Matrix3x3 {
        let mut temp = self.clone();
        unsafe {
            ffi::aiTransposeMatrix3(&mut temp)
        }
        temp
    }

}

impl Mul for Matrix3x3 {
    type Output = Matrix3x3;
    
    fn mul(self, rhs: Matrix3x3) -> Matrix3x3 {
        let mut result = self.clone();
        unsafe {
            ffi::aiMultiplyMatrix3(&mut result, &rhs)
        }
        result
    }
}

/// Represents a 4x4 matrix.
#[allow(missing_docs)]
#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(C, packed)]
pub struct Matrix4x4 {
    pub a1: c_float, pub a2: c_float, pub a3: c_float, pub a4: c_float,
    pub b1: c_float, pub b2: c_float, pub b3: c_float, pub b4: c_float,
    pub c1: c_float, pub c2: c_float, pub c3: c_float, pub c4: c_float,
    pub d1: c_float, pub d2: c_float, pub d3: c_float, pub d4: c_float,
}

impl Matrix4x4 {
    /// Create a 4x4 identity matrix
    pub fn identity() -> Matrix4x4 {
        Matrix4x4 {
            a1:  1.0, a2: 0.0, a3: 0.0, a4: 0.0,
            b1:  0.0, b2: 1.0, b3: 0.0, b4: 0.0,
            c1:  0.0, c2: 0.0, c3: 1.0, c4: 0.0,
            d1:  0.0, d2: 0.0, d3: 0.0, d4: 1.0,
        }
    }

    /// Returns a slice equivalent to this matrix in row-major format
    pub fn to_array(&self) -> [[f32; 4]; 4] {
        [
            [self.a1, self.a2, self.a3, self.a4,],
            [self.b1, self.b2, self.b3, self.b4,],
            [self.c1, self.c2, self.c3, self.c4,],
            [self.d1, self.d2, self.d3, self.d4,],
        ]
    }

    /// Returns the transpose of this matrix
    pub fn transpose(&self) -> Matrix4x4 {
        let mut temp = self.clone();
        unsafe {
            ffi::aiTransposeMatrix4(&mut temp)
        }
        temp
    }

    /// Compute the inverse of a 4x4 matrix
    pub fn inverse(&self) -> Matrix4x4 {
        let inv = m::mat4_inv(self.to_array());
        Matrix4x4 {
            a1:  inv[0][0], a2: inv[0][1], a3: inv[0][2], a4: inv[0][3],
            b1:  inv[1][0], b2: inv[1][1], b3: inv[1][2], b4: inv[1][3],
            c1:  inv[2][0], c2: inv[2][1], c3: inv[2][2], c4: inv[2][3],
            d1:  inv[3][0], d2: inv[3][1], d3: inv[3][2], d4: inv[3][3],
        }
    }
}

impl Mul for Matrix4x4 {
    type Output = Matrix4x4;
    
    fn mul(self, rhs: Matrix4x4) -> Matrix4x4 {
        let mut result = self.clone();
        unsafe {
            ffi::aiMultiplyMatrix4(&mut result, &rhs)
        }
        result
    }
}

// #[cfg(test)]
// mod test {
//     use super::Matrix4x4;
//     #[test]
//     fn test_inv() {
//         Matrix4x4::identity() * 
//     }
// }