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
use crate::{NearlyEqual, Point, Vector3, Vector4};

/// A 4x4 matrix, suitable for 3D transformations.
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(C)]
pub struct Matrix4(pub [Vector4; 4]);

impl Matrix4 {
    /// A new matrix from a 2D array.
    pub const fn from_2d_array(a: [[f32; 4]; 4]) -> Self {
        Self([
            Vector4::new(a[0][0], a[0][1], a[0][2], a[0][3]),
            Vector4::new(a[1][0], a[1][1], a[1][2], a[1][3]),
            Vector4::new(a[2][0], a[2][1], a[2][2], a[2][3]),
            Vector4::new(a[3][0], a[3][1], a[3][2], a[3][3]),
        ])
    }

    /// The identity matrix.
    pub const fn identity() -> Self {
        Self([
            Vector4::new(1.0, 0.0, 0.0, 0.0),
            Vector4::new(0.0, 1.0, 0.0, 0.0),
            Vector4::new(0.0, 0.0, 1.0, 0.0),
            Vector4::new(0.0, 0.0, 0.0, 1.0),
        ])
    }

    /// A matrix composed entirely of zeroes.
    pub const fn zero() -> Self {
        Self([
            Vector4::new(0.0, 0.0, 0.0, 0.0),
            Vector4::new(0.0, 0.0, 0.0, 0.0),
            Vector4::new(0.0, 0.0, 0.0, 0.0),
            Vector4::new(0.0, 0.0, 0.0, 0.0),
        ])
    }

    /// A look-at matrix suitable for positioning a camera.
    pub fn look_at(eye: Point, target: Point, up: Vector3) -> Self {
        let z_axis = (target - eye).normalized();
        let x_axis = z_axis.cross(up).normalized();
        let y_axis = x_axis.cross(z_axis);

        let eye_vec = eye.into();

        Self([
            Vector4::new(x_axis.x, y_axis.x, -z_axis.x, 0.0),
            Vector4::new(x_axis.y, y_axis.y, -z_axis.y, 0.0),
            Vector4::new(x_axis.z, y_axis.z, -z_axis.z, 0.0),
            Vector4::new(
                -x_axis.dot(eye_vec),
                -y_axis.dot(eye_vec),
                z_axis.dot(eye_vec),
                1.0,
            ),
        ])
    }

    /// A perspective matrix suitable for use as a camera projection.
    pub fn perspective(aspect_ratio: f32, fov_radians: f32, znear: f32, zfar: f32) -> Self {
        let f = 1.0 / (fov_radians / 2.0).tan();

        Self([
            Vector4::new(f / aspect_ratio, 0.0, 0.0, 0.0),
            Vector4::new(0.0, f, 0.0, 0.0),
            Vector4::new(0.0, 0.0, (zfar + znear) / (znear - zfar), -1.0),
            Vector4::new(0.0, 0.0, (2.0 * zfar * znear) / (znear - zfar), 0.0),
        ])
    }

    /// A matrix that translates by the given vector.
    pub fn translation(v: Vector3) -> Self {
        Self([
            Vector4::new(1.0, 0.0, 0.0, 0.0),
            Vector4::new(0.0, 1.0, 0.0, 0.0),
            Vector4::new(0.0, 0.0, 1.0, 0.0),
            Vector4::new(v.x, v.y, v.z, 1.0),
        ])
    }

    /// A matrix that rotates around the x-axis.
    pub fn rotation_x(angle_radians: f32) -> Self {
        Self([
            Vector4::new(1.0, 0.0, 0.0, 0.0),
            Vector4::new(0.0, angle_radians.cos(), -angle_radians.sin(), 0.0),
            Vector4::new(0.0, angle_radians.sin(), angle_radians.cos(), 0.0),
            Vector4::new(0.0, 0.0, 0.0, 1.0),
        ])
    }

    /// A matrix that rotates around the y-axis.
    pub fn rotation_y(angle_radians: f32) -> Self {
        Self([
            Vector4::new(angle_radians.cos(), 0.0, angle_radians.sin(), 0.0),
            Vector4::new(0.0, 1.0, 0.0, 0.0),
            Vector4::new(-angle_radians.sin(), 0.0, angle_radians.cos(), 0.0),
            Vector4::new(0.0, 0.0, 0.0, 1.0),
        ])
    }

    /// A matrix that rotates around the z-axis.
    pub fn rotation_z(angle_radians: f32) -> Self {
        Self([
            Vector4::new(angle_radians.cos(), -angle_radians.sin(), 0.0, 0.0),
            Vector4::new(angle_radians.sin(), angle_radians.cos(), 0.0, 0.0),
            Vector4::new(0.0, 0.0, 1.0, 0.0),
            Vector4::new(0.0, 0.0, 0.0, 1.0),
        ])
    }

    /// A matrix that scales uniformly in all dimensions.
    pub fn uniform_scale(scale: f32) -> Self {
        Self([
            Vector4::new(scale, 0.0, 0.0, 0.0),
            Vector4::new(0.0, scale, 0.0, 0.0),
            Vector4::new(0.0, 0.0, scale, 0.0),
            Vector4::new(0.0, 0.0, 0.0, 1.0),
        ])
    }

    /// Obtain the specified row vector of this matrix.
    pub fn row(&self, i: usize) -> Vector4 {
        Vector4::new(self.0[0][i], self.0[1][i], self.0[2][i], self.0[3][i])
    }
    /// Obtain the specified column vector of this matrix.
    pub fn column(&self, i: usize) -> Vector4 {
        self.0[i]
    }

    /// The transpose of this matrix (i.e. this matrix flipped along the diagonal)
    pub fn transpose(&self) -> Self {
        let mut r = Self::zero();

        for i in 0..4 {
            for j in 0..4 {
                r.0[i][j] = self.0[j][i];
            }
        }

        r
    }

    /// The inverse of this matrix.
    pub fn invert(&self) -> Self {
        let mut inv = Matrix4::zero();

        inv.0[0][0] = self.0[1][1] * self.0[2][2] * self.0[3][3]
            - self.0[1][1] * self.0[2][3] * self.0[3][2]
            - self.0[2][1] * self.0[1][2] * self.0[3][3]
            + self.0[2][1] * self.0[1][3] * self.0[3][2]
            + self.0[3][1] * self.0[1][2] * self.0[2][3]
            - self.0[3][1] * self.0[1][3] * self.0[2][2];

        inv.0[1][0] = -self.0[1][0] * self.0[2][2] * self.0[3][3]
            + self.0[1][0] * self.0[2][3] * self.0[3][2]
            + self.0[2][0] * self.0[1][2] * self.0[3][3]
            - self.0[2][0] * self.0[1][3] * self.0[3][2]
            - self.0[3][0] * self.0[1][2] * self.0[2][3]
            + self.0[3][0] * self.0[1][3] * self.0[2][2];

        inv.0[2][0] = self.0[1][0] * self.0[2][1] * self.0[3][3]
            - self.0[1][0] * self.0[2][3] * self.0[3][1]
            - self.0[2][0] * self.0[1][1] * self.0[3][3]
            + self.0[2][0] * self.0[1][3] * self.0[3][1]
            + self.0[3][0] * self.0[1][1] * self.0[2][3]
            - self.0[3][0] * self.0[1][3] * self.0[2][1];

        inv.0[3][0] = -self.0[1][0] * self.0[2][1] * self.0[3][2]
            + self.0[1][0] * self.0[2][2] * self.0[3][1]
            + self.0[2][0] * self.0[1][1] * self.0[3][2]
            - self.0[2][0] * self.0[1][2] * self.0[3][1]
            - self.0[3][0] * self.0[1][1] * self.0[2][2]
            + self.0[3][0] * self.0[1][2] * self.0[2][1];

        inv.0[0][1] = -self.0[0][1] * self.0[2][2] * self.0[3][3]
            + self.0[0][1] * self.0[2][3] * self.0[3][2]
            + self.0[2][1] * self.0[0][2] * self.0[3][3]
            - self.0[2][1] * self.0[0][3] * self.0[3][2]
            - self.0[3][1] * self.0[0][2] * self.0[2][3]
            + self.0[3][1] * self.0[0][3] * self.0[2][2];

        inv.0[1][1] = self.0[0][0] * self.0[2][2] * self.0[3][3]
            - self.0[0][0] * self.0[2][3] * self.0[3][2]
            - self.0[2][0] * self.0[0][2] * self.0[3][3]
            + self.0[2][0] * self.0[0][3] * self.0[3][2]
            + self.0[3][0] * self.0[0][2] * self.0[2][3]
            - self.0[3][0] * self.0[0][3] * self.0[2][2];

        inv.0[2][1] = -self.0[0][0] * self.0[2][1] * self.0[3][3]
            + self.0[0][0] * self.0[2][3] * self.0[3][1]
            + self.0[2][0] * self.0[0][1] * self.0[3][3]
            - self.0[2][0] * self.0[0][3] * self.0[3][1]
            - self.0[3][0] * self.0[0][1] * self.0[2][3]
            + self.0[3][0] * self.0[0][3] * self.0[2][1];

        inv.0[3][1] = self.0[0][0] * self.0[2][1] * self.0[3][2]
            - self.0[0][0] * self.0[2][2] * self.0[3][1]
            - self.0[2][0] * self.0[0][1] * self.0[3][2]
            + self.0[2][0] * self.0[0][2] * self.0[3][1]
            + self.0[3][0] * self.0[0][1] * self.0[2][2]
            - self.0[3][0] * self.0[0][2] * self.0[2][1];

        inv.0[0][2] = self.0[0][1] * self.0[1][2] * self.0[3][3]
            - self.0[0][1] * self.0[1][3] * self.0[3][2]
            - self.0[1][1] * self.0[0][2] * self.0[3][3]
            + self.0[1][1] * self.0[0][3] * self.0[3][2]
            + self.0[3][1] * self.0[0][2] * self.0[1][3]
            - self.0[3][1] * self.0[0][3] * self.0[1][2];

        inv.0[1][2] = -self.0[0][0] * self.0[1][2] * self.0[3][3]
            + self.0[0][0] * self.0[1][3] * self.0[3][2]
            + self.0[1][0] * self.0[0][2] * self.0[3][3]
            - self.0[1][0] * self.0[0][3] * self.0[3][2]
            - self.0[3][0] * self.0[0][2] * self.0[1][3]
            + self.0[3][0] * self.0[0][3] * self.0[1][2];

        inv.0[2][2] = self.0[0][0] * self.0[1][1] * self.0[3][3]
            - self.0[0][0] * self.0[1][3] * self.0[3][1]
            - self.0[1][0] * self.0[0][1] * self.0[3][3]
            + self.0[1][0] * self.0[0][3] * self.0[3][1]
            + self.0[3][0] * self.0[0][1] * self.0[1][3]
            - self.0[3][0] * self.0[0][3] * self.0[1][1];

        inv.0[3][2] = -self.0[0][0] * self.0[1][1] * self.0[3][2]
            + self.0[0][0] * self.0[1][2] * self.0[3][1]
            + self.0[1][0] * self.0[0][1] * self.0[3][2]
            - self.0[1][0] * self.0[0][2] * self.0[3][1]
            - self.0[3][0] * self.0[0][1] * self.0[1][2]
            + self.0[3][0] * self.0[0][2] * self.0[1][1];

        inv.0[0][3] = -self.0[0][1] * self.0[1][2] * self.0[2][3]
            + self.0[0][1] * self.0[1][3] * self.0[2][2]
            + self.0[1][1] * self.0[0][2] * self.0[2][3]
            - self.0[1][1] * self.0[0][3] * self.0[2][2]
            - self.0[2][1] * self.0[0][2] * self.0[1][3]
            + self.0[2][1] * self.0[0][3] * self.0[1][2];

        inv.0[1][3] = self.0[0][0] * self.0[1][2] * self.0[2][3]
            - self.0[0][0] * self.0[1][3] * self.0[2][2]
            - self.0[1][0] * self.0[0][2] * self.0[2][3]
            + self.0[1][0] * self.0[0][3] * self.0[2][2]
            + self.0[2][0] * self.0[0][2] * self.0[1][3]
            - self.0[2][0] * self.0[0][3] * self.0[1][2];

        inv.0[2][3] = -self.0[0][0] * self.0[1][1] * self.0[2][3]
            + self.0[0][0] * self.0[1][3] * self.0[2][1]
            + self.0[1][0] * self.0[0][1] * self.0[2][3]
            - self.0[1][0] * self.0[0][3] * self.0[2][1]
            - self.0[2][0] * self.0[0][1] * self.0[1][3]
            + self.0[2][0] * self.0[0][3] * self.0[1][1];

        inv.0[3][3] = self.0[0][0] * self.0[1][1] * self.0[2][2]
            - self.0[0][0] * self.0[1][2] * self.0[2][1]
            - self.0[1][0] * self.0[0][1] * self.0[2][2]
            + self.0[1][0] * self.0[0][2] * self.0[2][1]
            + self.0[2][0] * self.0[0][1] * self.0[1][2]
            - self.0[2][0] * self.0[0][2] * self.0[1][1];

        let mut det = self.0[0][0] * inv.0[0][0]
            + self.0[0][1] * inv.0[1][0]
            + self.0[0][2] * inv.0[2][0]
            + self.0[0][3] * inv.0[3][0];
        det = 1.0 / det;

        for i in 0..4 {
            for j in 0..4 {
                inv.0[i][j] *= det;
            }
        }

        inv
    }
}

impl NearlyEqual for &Matrix4 {
    fn nearly_equals(self, rhs: Self) -> bool {
        for i in 0..4 {
            if !self.0[i].nearly_equals(&rhs.0[i]) {
                return false;
            }
        }

        true
    }
}

#[cfg(test)]
mod tests {
    use crate::*;

    #[test]
    fn identity() {
        let m = Matrix4::identity();
        let p = Point::new(1.0, 2.0, 3.0);

        assert_eq!(p, m * p);
        assert_eq!(m, m.transpose());
        assert_eq!(m, m.invert());
    }

    #[test]
    fn invert() {
        let m = Matrix4::from_2d_array([
            [3.0, 2.0, 1.0, 1.0],
            [2.0, 3.0, 2.0, 2.0],
            [1.0, 2.0, 3.0, 3.0],
            [0.0, 1.0, 1.0, 0.0],
        ]);

        assert_eq!(m.invert() * m, Matrix4::identity());

        let n = Matrix4([
            Vector4 {
                x: 0.9742785,
                y: 0.0,
                z: 0.0,
                w: 0.0,
            },
            Vector4 {
                x: 0.0,
                y: 1.7320507,
                z: 0.0,
                w: 0.0,
            },
            Vector4 {
                x: 0.0,
                y: 0.0,
                z: -1.0002,
                w: -1.0,
            },
            Vector4 {
                x: 0.0,
                y: 0.0,
                z: -2.0002,
                w: 0.0,
            },
        ]);
        let inverse = Matrix4([
            Vector4 {
                x: 1.0264006,
                y: -0.0,
                z: -0.0,
                w: -0.0,
            },
            Vector4 {
                x: -0.0,
                y: 0.5773504,
                z: -0.0,
                w: -0.0,
            },
            Vector4 {
                x: -0.0,
                y: -0.0,
                z: -0.0,
                w: -0.49995005,
            },
            Vector4 {
                x: -0.0,
                y: -0.0,
                z: -1.0000001,
                w: 0.50005007,
            },
        ]);
        assert_eq!(n.invert(), inverse);
    }

    #[test]
    fn translate() {
        let m = Matrix4::translation(Vector3::new(10.0, 1.0, 0.0));
        assert_eq!(m * Point::zero(), Point::new(10.0, 1.0, 0.0));

        let n = Matrix4::translation(Vector3::new(-2.0, -5.0, 0.0));
        assert_eq!(n * Point::zero(), Point::new(-2.0, -5.0, 0.0));

        let t = m * n;
        assert_eq!(t * Point::zero(), Point::new(8.0, -4.0, 0.0));
    }
}