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
use cgmath;
use std::fmt;

use approx::ApproxEq;
use {DQuat, DMat4, DVec3, Quat, Mat4, Vec3};

/// Single-precision translation + rotation + non-uniform scale transform.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Trs {
    /// Translation vector.
    pub t: Vec3,

    /// Rotation quaternion.
    pub r: Quat,

    /// *Non-uniform* scale factor.
    pub s: Vec3,
}

impl fmt::Display for Trs {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", (self.t, self.r, self.s))
    }
}

impl Default for Trs {
    fn default() -> Self {
        Self::identity()
    }
}

impl Trs {
    /// Full constructor.
    pub fn new(t: Vec3, r: Quat, s: Vec3) -> Self {
        Trs { t, r, s }
    }

    /// Identity constructor.
    pub fn identity() -> Self {
        Trs {
            t: vec3!(),
            r: quat!(),
            s: vec3!(1.0),
        }
    }

    /// Returns the equivalent matrix representation for this transform.
    pub fn matrix(&self) -> Mat4 {
        let t = cgmath::Matrix4::from_translation(cgmath::Vector3::new(self.t.x, self.t.y, self.t.z));
        let r = cgmath::Matrix4::from(cgmath::Quaternion::new(self.r.s, self.r.x,  self.r.y, self.r.z));
        let s = cgmath::Matrix4::from_nonuniform_scale(self.s.x, self.s.y, self.s.z);
        let m: [[f32; 4]; 4] = (t * r * s).into();
        Mat4::from(m)
    }
}

impl ApproxEq for Trs {
    type Epsilon = <f32 as ApproxEq>::Epsilon;

    fn default_epsilon() -> Self::Epsilon {
        <f32 as ApproxEq>::default_epsilon()
    }

    fn default_max_relative() -> Self::Epsilon {
        <f32 as ApproxEq>::default_max_relative()
    }

    fn default_max_ulps() -> u32 {
        <f32 as ApproxEq>::default_max_ulps()
    }

    fn relative_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool {
        self.t.relative_eq(&other.t, epsilon, max_relative)
            &&    
        self.r.relative_eq(&other.r, epsilon, max_relative)
            &&
        self.s.relative_eq(&other.s, epsilon, max_relative)
    }

    fn ulps_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_ulps: u32,
    ) -> bool {
        self.t.ulps_eq(&other.t, epsilon, max_ulps)
            &&
        self.r.ulps_eq(&other.r, epsilon, max_ulps)
            &&
        self.s.ulps_eq(&other.s, epsilon, max_ulps)
    }
}

/// Double-precision translation + rotation + non-uniform scale transform.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct DTrs {
    /// Translation vector.
    pub t: DVec3,

    /// Rotation quaternion.
    pub r: DQuat,

    /// *Non-uniform* scale factor.
    pub s: DVec3,
}

impl fmt::Display for DTrs {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", (self.t, self.r, self.s))
    }
}

impl Default for DTrs {
    fn default() -> Self {
        Self::identity()
    }
}

impl DTrs {
    /// Full constructor.
    pub fn new(t: DVec3, r: DQuat, s: DVec3) -> Self {
        DTrs { t, r, s }
    }

    /// Identity constructor.
    pub fn identity() -> Self {
        DTrs {
            t: dvec3!(),
            r: dquat!(),
            s: dvec3!(1.0),
        }
    }

    /// Returns the equivalent matrix representation for this transform.
    pub fn matrix(&self) -> DMat4 {
        let t = cgmath::Matrix4::from_translation(cgmath::Vector3::new(self.t.x, self.t.y, self.t.z));
        let r = cgmath::Matrix4::from(cgmath::Quaternion::new(self.r.s, self.r.x,  self.r.y, self.r.z));
        let s = cgmath::Matrix4::from_nonuniform_scale(self.s.x, self.s.y, self.s.z);
        let m: [[f64; 4]; 4] = (t * r * s).into();
        DMat4::from(m)
    }
}

impl ApproxEq for DTrs {
    type Epsilon = <f64 as ApproxEq>::Epsilon;

    fn default_epsilon() -> Self::Epsilon {
        <f64 as ApproxEq>::default_epsilon()
    }

    fn default_max_relative() -> Self::Epsilon {
        <f64 as ApproxEq>::default_max_relative()
    }

    fn default_max_ulps() -> u32 {
        <f64 as ApproxEq>::default_max_ulps()
    }

    fn relative_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_relative: Self::Epsilon,
    ) -> bool {
        self.t.relative_eq(&other.t, epsilon, max_relative)
            &&    
        self.r.relative_eq(&other.r, epsilon, max_relative)
            &&
        self.s.relative_eq(&other.s, epsilon, max_relative)
    }

    fn ulps_eq(
        &self,
        other: &Self,
        epsilon: Self::Epsilon,
        max_ulps: u32,
    ) -> bool {
        self.t.ulps_eq(&other.t, epsilon, max_ulps)
            &&
        self.r.ulps_eq(&other.r, epsilon, max_ulps)
            &&
        self.s.ulps_eq(&other.s, epsilon, max_ulps)
    }
}