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
pub use num_traits::One;
use std::default::Default;
use std::ops::Mul;

use linear::M44;
use scene::transform::{Transform, Transformable};

/// Arbritrary scale.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Scale {
  pub x: f32,
  pub y: f32,
  pub z: f32
}

impl Scale {
  /// Arbitrary scale along the X, Y and Z axis.
  pub fn new(x: f32, y: f32, z: f32) -> Self {
    Scale {
      x: x,
      y: y,
      z: z
    }
  }

  /// Uniform scale along the X, Y and Z axis.
  pub fn uni(x: f32) -> Self {
    Scale {
      x: x,
      y: x,
      z: x
    }
  }
}

impl Default for Scale {
  fn default() -> Self { Scale::new(1., 1., 1.) }
}

impl From<[f32; 3]> for Scale {
  fn from([x, y, z]: [f32; 3]) -> Self {
    Scale {
      x: x,
      y: y,
      z: z
    }
  }
}

impl From<Scale> for [f32; 3] {
  fn from(scale: Scale) -> Self {
    [scale.x, scale.y, scale.z]
  }
}

impl From<Scale> for M44<f32> {
  fn from(scale: Scale) -> Self {
    let mat: M44<f32> = [
      [scale.x,      0.,      0., 0.],
      [     0., scale.y,      0., 0.],
      [     0.,      0., scale.z, 0.],
      [     0.,      0.,      0., 1.]
    ].into();

    mat.into()
  }
}

impl Transformable for Scale {
  fn transform(&self) -> Transform {
    let m: M44<_> = (*self).into();

    m.into() 
  }
}

impl Mul for Scale {
  type Output = Scale;

  fn mul(self, rhs: Self) -> Self::Output {
    Scale {
      x: self.x * rhs.x,
      y: self.y * rhs.y,
      z: self.z * rhs.z
    }
  }
}

impl One for Scale {
  fn one() -> Self {
    Scale::new(1., 1., 1.)
  }
}