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
use na::{U2, U3};

use traits::Number;
use aliases::{TVec2, TVec3, TMat3, TMat4};

/// Build planar projection matrix along normal axis and right-multiply it to `m`.
pub fn proj2d<N: Number>(m: &TMat3<N>, normal: &TVec2<N>) -> TMat3<N> {
    let mut res = TMat3::identity();

    {
        let mut part = res.fixed_slice_mut::<U2, U2>(0, 0);
        part -= normal * normal.transpose();
    }

    m * res
}

/// Build planar projection matrix along normal axis, and right-multiply it to `m`.
pub fn proj<N: Number>(m: &TMat4<N>, normal: &TVec3<N>) -> TMat4<N> {
    let mut res = TMat4::identity();

    {
        let mut part = res.fixed_slice_mut::<U3, U3>(0, 0);
        part -= normal * normal.transpose();
    }

    m * res
}

/// Builds a reflection matrix and right-multiply it to `m`.
pub fn reflect2d<N: Number>(m: &TMat3<N>, normal: &TVec2<N>) -> TMat3<N> {
    let mut res = TMat3::identity();

    {
        let mut part = res.fixed_slice_mut::<U2, U2>(0, 0);
        part -= (normal * N::from_f64(2.0).unwrap()) * normal.transpose();
    }

    m * res
}

/// Builds a reflection matrix, and right-multiply it to `m`.
pub fn reflect<N: Number>(m: &TMat4<N>, normal: &TVec3<N>) -> TMat4<N> {
    let mut res = TMat4::identity();

    {
        let mut part = res.fixed_slice_mut::<U3, U3>(0, 0);
        part -= (normal * N::from_f64(2.0).unwrap()) * normal.transpose();
    }

    m * res
}

/// Builds a scale-bias matrix.
pub fn scale_bias_matrix<N: Number>(scale: N, bias: N) -> TMat4<N> {
    let _0 = N::zero();
    let _1 = N::one();

    TMat4::new(
        scale, _0, _0, bias,
        _0, scale, _0, bias,
        _0, _0, scale, bias,
        _0, _0, _0, _1,
    )
}

/// Builds a scale-bias matrix, and right-multiply it to `m`.
pub fn scale_bias<N: Number>(m: &TMat4<N>, scale: N, bias: N) -> TMat4<N> {
    m * scale_bias_matrix(scale, bias)
}

/// Transforms a matrix with a shearing on X axis.
pub fn shear2d_x<N: Number>(m: &TMat3<N>, y: N) -> TMat3<N> {
    let _0 = N::zero();
    let _1 = N::one();

    let shear = TMat3::new(
        _1,  y, _0,
        _0, _1, _0,
        _0, _0, _1
    );
    m * shear
}

/// Transforms a matrix with a shearing on Y axis.
pub fn shear_x<N: Number>(m: &TMat4<N>, y: N, z: N) -> TMat4<N> {
    let _0 = N::zero();
    let _1 = N::one();
    let shear = TMat4::new(
        _1, _0, _0, _0,
         y, _1, _0, _0,
         z, _0, _1, _0,
        _0, _0, _0, _1,
    );

    m * shear
}

/// Transforms a matrix with a shearing on Y axis.
pub fn shear2d_y<N: Number>(m: &TMat3<N>, x: N) -> TMat3<N> {
    let _0 = N::zero();
    let _1 = N::one();

    let shear = TMat3::new(
        _1, _0, _0,
        x, _1, _0,
        _0, _0, _1
    );
    m * shear
}

/// Transforms a matrix with a shearing on Y axis.
pub fn shear_y<N: Number>(m: &TMat4<N>, x: N, z: N) -> TMat4<N> {
    let _0 = N::zero();
    let _1 = N::one();
    let shear = TMat4::new(
        _1,  x, _0, _0,
        _0, _1, _0, _0,
        _0,  z, _1, _0,
        _0, _0, _0, _1,
    );

    m * shear
}

/// Transforms a matrix with a shearing on Z axis.
pub fn shear_z<N: Number>(m: &TMat4<N>, x: N, y: N) -> TMat4<N> {
    let _0 = N::zero();
    let _1 = N::one();
    let shear = TMat4::new(
        _1, _0,  x, _0,
        _0, _1,  y, _0,
        _0, _0, _1, _0,
        _0, _0, _0, _1,
    );

    m * shear
}