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
use interpolation;
use math::*;

pub trait Transform: Copy {
    fn identity() -> Self;
    fn concat(self, other: Self) -> Self;
    fn inverse(self) -> Self;
    fn lerp(self, other: Self, parameter: f32) -> Self;
    fn transform_vector(self, v: Vector3<f32>) -> Vector3<f32>;
    fn to_matrix(self) -> Matrix4<f32>;
    fn from_matrix(Matrix4<f32>) -> Self;
    fn set_rotation(&mut self, rotation: Quaternion<f32>);
    fn get_rotation(self) -> Quaternion<f32>;
    fn set_translation(&mut self, translation: Vector3<f32>);
    fn get_translation(self) -> Vector3<f32>;
}

/// Transformation represented by separate scaling, translation, and rotation factors.
#[derive(Debug, Copy, Clone)]
pub struct QVTransform
{
    /// Translation
    pub translation: Vector3<f32>,

    /// Uniform scale factor.
    pub scale: f32,

    /// Rotation
    pub rotation: Quaternion<f32>

}

impl Transform for QVTransform {

    fn identity() -> QVTransform {
        QVTransform {
            translation: [0.0, 0.0, 0.0],
            scale: 1.0,
            rotation: quaternion_id(),
        }
    }

    fn set_rotation(&mut self, rotation: Quaternion<f32>) {
        self.rotation = rotation;
    }

    fn get_rotation(self) -> Quaternion<f32> {
        self.rotation
    }

    fn set_translation(&mut self, translation: Vector3<f32>) {
        self.translation = translation;
    }

    fn get_translation(self) -> Vector3<f32> {
        self.translation
    }

    fn concat(self, other: QVTransform) -> QVTransform {
        QVTransform::from_matrix(self.to_matrix().concat(other.to_matrix()))
    }

    fn inverse(self) -> QVTransform {
        QVTransform::from_matrix(self.to_matrix().inverse())
    }

    fn lerp(self, other: QVTransform, parameter: f32) -> QVTransform {
        QVTransform {
            translation: interpolation::lerp(&self.translation, &other.translation, &parameter),
            scale: interpolation::lerp(&self.scale, &other.scale, &parameter),
            rotation: lerp_quaternion(&self.rotation, &other.rotation, &parameter),
        }
    }

    fn transform_vector(self, v: Vector3<f32>) -> Vector3<f32> {
        let v = quaternion::rotate_vector(self.rotation, v);
        let v = vec3_add(v, self.translation);
        vec3_scale(v, self.scale)
    }

    fn to_matrix(self) -> Matrix4<f32> {
        let mut m = quaternion_to_matrix(self.rotation);

        m[0][3] = self.translation[0];
        m[1][3] = self.translation[1];
        m[2][3] = self.translation[2];

        m
    }

    fn from_matrix(m: Matrix4<f32>) -> QVTransform {

        let rotation = matrix_to_quaternion(&m);

        let translation = [m[0][3],
                           m[1][3],
                           m[2][3]];

        QVTransform {
            rotation: rotation,
            scale: 1.0,
            translation: translation,
        }
    }

}

impl Transform for DualQuaternion<f32> {

    fn identity() -> DualQuaternion<f32> {
        dual_quaternion::id()
    }

    fn set_rotation(&mut self, rotation: Quaternion<f32>) {
        let t = dual_quaternion::get_translation(*self);
        *self = dual_quaternion::from_rotation_and_translation(rotation, t);
    }

    fn get_rotation(self) -> Quaternion<f32> {
        dual_quaternion::get_rotation(self)
    }

    fn set_translation(&mut self, translation: Vector3<f32>) {
        let rotation = dual_quaternion::get_rotation(*self);
        *self = dual_quaternion::from_rotation_and_translation(rotation, translation);
    }

    fn get_translation(self) -> Vector3<f32> {
        dual_quaternion::get_translation(self)
    }

    fn concat(self, other: DualQuaternion<f32>) -> DualQuaternion<f32> {
        dual_quaternion::mul(self, other)
    }

    fn inverse(self) -> DualQuaternion<f32> {
        dual_quaternion::conj(self)
    }

    fn lerp(self, other: DualQuaternion<f32>, parameter: f32) -> DualQuaternion<f32> {
        lerp_dual_quaternion(self, other, parameter)
    }

    fn transform_vector(self, v: Vector3<f32>) -> Vector3<f32> {
        let t = dual_quaternion::get_translation(self);
        let r = dual_quaternion::get_rotation(self);
        vec3_add(quaternion::rotate_vector(r, v), t)
    }

    fn to_matrix(self) -> Matrix4<f32> {

        let rotation = dual_quaternion::get_rotation(self);
        let translation = dual_quaternion::get_translation(self);

        let mut m = mat4_transposed(quaternion_to_matrix(rotation));

        m[0][3] = translation[0];
        m[1][3] = translation[1];
        m[2][3] = translation[2];

        m
    }

    fn from_matrix(m: Matrix4<f32>) -> DualQuaternion<f32> {

        let rotation = matrix_to_quaternion(&mat4_transposed(m));

        let translation = [m[0][3],
                           m[1][3],
                           m[2][3]];

        dual_quaternion::from_rotation_and_translation(rotation, translation)

    }
}

impl Transform for Matrix4<f32> {

    fn identity() -> Matrix4<f32> {
        mat4_id()
    }

    fn set_rotation(&mut self, rotation: Quaternion<f32>) {

        let rotation = quaternion_to_matrix(rotation);

        self[0][0] = rotation[0][0];
        self[1][0] = rotation[1][0];
        self[2][0] = rotation[2][0];

        self[0][1] = rotation[0][1];
        self[1][1] = rotation[1][1];
        self[2][1] = rotation[2][1];

        self[0][2] = rotation[0][2];
        self[1][2] = rotation[1][2];
        self[2][2] = rotation[2][2];
    }

    fn get_rotation(self) -> Quaternion<f32> {
        matrix_to_quaternion(&self)
    }

    fn set_translation(&mut self, translation: Vector3<f32>) {
        self[0][3] = translation[0];
        self[1][3] = translation[1];
        self[2][3] = translation[2];
    }

    fn get_translation(self) -> Vector3<f32> {
        [self[0][3],
         self[1][3],
         self[2][3]]
    }

    fn concat(self, other: Matrix4<f32>) -> Matrix4<f32> {
        row_mat4_mul(self, other)
    }

    fn inverse(self) -> Matrix4<f32> {
        mat4_inv(self)
    }

    fn lerp(self, other: Matrix4<f32>, parameter: f32) -> Matrix4<f32> {
        let q1 = DualQuaternion::from_matrix(self);
        let q2 = DualQuaternion::from_matrix(other);
        q1.lerp(q2, parameter).to_matrix()
    }

    fn transform_vector(self, v: Vector3<f32>) -> Vector3<f32> {
        let t = row_mat4_transform(self, [v[0], v[1], v[2], 1.0]);
        [t[0], t[1], t[2]]
    }

    fn to_matrix(self) -> Matrix4<f32> { self }

    fn from_matrix(m: Matrix4<f32>) -> Matrix4<f32> { m }
}

pub trait FromTransform<T: Transform> {
    fn from_transform(t: T) -> Self;
}

impl FromTransform<DualQuaternion<f32>> for DualQuaternion<f32> {
    fn from_transform(t: DualQuaternion<f32>) -> DualQuaternion<f32> { t }
}

impl<T: Transform> FromTransform<T> for Matrix4<f32> {
    fn from_transform(t: T) -> Matrix4<f32> {
        t.to_matrix()
    }
}

#[cfg(test)]
mod test {

    use vecmath;
    use quaternion;
    use dual_quaternion;

    use super::Transform;

    static EPSILON: f32 = 0.000001;

    #[test]
    fn test_dual_quaternion_to_matrix() {

        let a = [1.0, 0.0, 0.0];
        let b = [0.0, 1.0, 0.0];

        let q = quaternion::rotation_from_to(a, b);

        let dq = dual_quaternion::from_rotation_and_translation(q, [0.0, 0.0, 0.0]);
        println!("{:?}", dq.transform_vector(a));
        assert!(vecmath::vec3_len(vecmath::vec3_sub(b, dq.transform_vector(a))) < EPSILON);

        let m = dq.to_matrix();
        println!("{:?}", m.transform_vector(a));
        assert!(vecmath::vec3_len(vecmath::vec3_sub(b, m.transform_vector(a))) < EPSILON);
    }

    #[test]
    fn test_dual_quaternion_set_rotation() {

        let a = [1.0, 0.0, 0.0];
        let b = [0.0, 1.0, 0.0];

        let q = quaternion::rotation_from_to(a, b);
        let q2 = quaternion::rotation_from_to(b, a);

        let mut dq = dual_quaternion::from_rotation_and_translation(q, [0.0, 1.0, 0.0]);

        println!("{:?}", dq.transform_vector(a));
        assert!(vecmath::vec3_len(vecmath::vec3_sub([0.0, 2.0, 0.0],
                                                    dq.transform_vector(a))) < EPSILON);

        dq.set_rotation(q2);
        println!("{:?}", dq.transform_vector(b));
        assert!(vecmath::vec3_len(vecmath::vec3_sub([1.0, 1.0, 0.0],
                                                    dq.transform_vector(b))) < EPSILON);
    }
}