-
Notifications
You must be signed in to change notification settings - Fork 9
/
matrix.lisp
418 lines (377 loc) · 16.9 KB
/
matrix.lisp
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
;;;; By Nikodemus Siivola <nikodemus@random-state.net>, 2009.
;;;;
;;;; Permission is hereby granted, free of charge, to any person
;;;; obtaining a copy of this software and associated documentation files
;;;; (the "Software"), to deal in the Software without restriction,
;;;; including without limitation the rights to use, copy, modify, merge,
;;;; publish, distribute, sublicense, and/or sell copies of the Software,
;;;; and to permit persons to whom the Software is furnished to do so,
;;;; subject to the following conditions:
;;;;
;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;;;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
;;;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
;;;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
;;;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(in-package :sb-cga)
;;;; ACCESSORS
(declaim (ftype (sfunction (matrix (integer 0 3) (integer 0 3)) single-float) mref)
(inline mref))
(defun mref (matrix row column)
"Accessor for value in the specificed ROW and COLUMN in MATRIX."
(aref matrix (+ row (* column 4))))
(declaim (inline (setf mref)))
(defun (setf mref) (value matrix row column)
(setf (aref matrix (+ row (* column 4))) value))
;;; PRETTY-PRINTING
(defun pprint-matrix (stream matrix)
(pprint-logical-block (stream nil)
(print-unreadable-object (matrix stream :type nil :identity nil)
(dotimes (i 4)
(format stream "[~s, ~s, ~s, ~s]"
(mref matrix i 0)
(mref matrix i 1)
(mref matrix i 2)
(mref matrix i 3))
(unless (= 3 i)
(pprint-newline :mandatory stream))))))
(set-pprint-dispatch 'matrix 'pprint-matrix)
;;;; PREDICATES
(declaim (ftype (sfunction (t) boolean))
(inline matrixp))
(defun matrixp (object)
"Return true of OBJECT is a matrix."
(typep object 'matrix))
;;;; COMPARISON
(declaim (ftype (sfunction (matrix matrix) boolean) matrix=))
(defun matrix= (m1 m2)
"Return true if MATRIX M1 is elementwise equal to MATRIX M1."
(dotimes (i 16 t)
(unless (= (aref m1 i) (aref m2 i))
(return nil))))
(declaim (ftype (sfunction (matrix matrix &optional single-float) boolean) matrix~))
(defun matrix~ (m1 m2 &optional (epsilon +default-epsilon+))
"Return true if MATRIX M1 and MATRIX M2 are elementwise within EPSILON of each other.
EPSILON defaults to +DEFAULT-EPSILON+"
(let ((-e (- epsilon)))
(dotimes (i 16 t)
(unless (<= -e (- (aref m1 i) (aref m2 i)) epsilon)
(return nil)))))
;;;; CONSTRUCTORS
(declaim (ftype (sfunction (single-float single-float single-float single-float
single-float single-float single-float single-float
single-float single-float single-float single-float
single-float single-float single-float single-float)
matrix)
matrix)
(inline matrix))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun matrix (m11 m12 m13 m14
m21 m22 m23 m24
m31 m32 m33 m34
m41 m42 m43 m44)
"Construct MATRIX with the given elements (arguments are provided in row
major order.)"
(make-array 16
:element-type 'single-float
:initial-contents (list m11 m21 m31 m41
m12 m22 m32 m42
m13 m23 m33 m43
m14 m24 m34 m44))))
(declaim (ftype (sfunction () matrix) zero-matrix)
(inline zero-matrix))
(defun zero-matrix ()
"Construct a zero matrix."
(make-array 16 :element-type 'single-float :initial-element 0f0))
(declaim (ftype (sfunction () matrix) identity-matrix))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun identity-matrix ()
"Construct an identity matrix."
(matrix 1f0 0f0 0f0 0f0
0f0 1f0 0f0 0f0
0f0 0f0 1f0 0f0
0f0 0f0 0f0 1f0)))
(defconstant +identity-matrix+
(if (boundp '+identity-matrix+)
(symbol-value '+identity-matrix+)
(identity-matrix))
"Constant identity matrix.")
;;;; MATRIX MULTIPLICATION
(declaim (ftype (sfunction (&rest matrix) matrix) matrix*))
(defun matrix* (&rest matrices)
"Multiply MATRICES. The result might not be freshly allocated if all,
or all but one multiplicant is an identity matrix."
(macrolet ((inline-mul (left right dest)
`(progn
,@(loop for i below 4
append (loop for j below 4
collect
`(setf
(mref ,dest ,i ,j)
(+ ,@(loop for k below 4
collect `(* (mref ,left ,i ,k) (mref ,right ,k ,j))))))))))
(labels ((mul (left more)
(declare (matrix left))
(if more
(if (eq left +identity-matrix+)
(mul (pop more) more)
(let ((right (pop more)))
(declare (matrix right))
(if (eq +identity-matrix+ right)
(mul left more)
(let ((result (zero-matrix)))
(declare (matrix result))
(inline-mul left right result)
(mul result more)))))
left)))
(cond ((not matrices)
+identity-matrix+)
((cdr matrices)
(mul (car matrices) (cdr matrices)))
(t
(car matrices))))))
;;;; TRANSFORMATIONS
(declaim (ftype (sfunction (single-float single-float single-float) matrix) translate*))
(defun translate* (x y z)
"Construct a translation matrix from translation factors X, Y and Z."
(matrix 1f0 0f0 0f0 x
0f0 1f0 0f0 y
0f0 0f0 1f0 z
0f0 0f0 0f0 1f0))
(declaim (ftype (sfunction (vec) matrix) translate))
(defun translate (vec)
"Construct a translation matrix using first three elements of VEC as the
translation factors."
(translate* (aref vec 0) (aref vec 1) (aref vec 2)))
(declaim (ftype (sfunction (single-float single-float single-float) matrix) scale*))
(defun scale* (x y z)
"Construct a scaling matrix from scaling factors X, Y, and Z."
(matrix x 0f0 0f0 0f0
0f0 y 0f0 0f0
0f0 0f0 z 0f0
0f0 0f0 0f0 1f0))
(declaim (ftype (sfunction (vec) matrix) scale))
(defun scale (vec)
"Construct a scaling matrix using first threee elements of VEC as the
scaling factors."
(scale* (aref vec 0) (aref vec 1) (aref vec 2)))
(declaim (ftype (sfunction (single-float single-float single-float) matrix) rotate*))
(defun rotate* (x y z)
"Construct a rotation matrix from rotation factors X, Y, Z."
(let ((rotate (identity-matrix)))
(unless (= 0f0 z)
(let ((c (cos z))
(s (sin z)))
(setf rotate (matrix* rotate
(matrix c (- s) 0f0 0f0
s c 0f0 0f0
0f0 0f0 1f0 0f0
0f0 0f0 0f0 1f0)))))
(unless (= 0f0 y)
(let ((c (cos y))
(s (sin y)))
(setf rotate (matrix* rotate
(matrix c 0f0 s 0f0
0f0 1f0 0f0 0f0
(- s) 0f0 c 0f0
0f0 0f0 0f0 1f0)))))
(unless (= 0f0 x)
(let ((c (cos x))
(s (sin x)))
(setf rotate (matrix* rotate
(matrix 1f0 0f0 0f0 0f0
0f0 c (- s) 0f0
0f0 s c 0f0
0f0 0f0 0f0 1f0)))))
rotate))
(declaim (ftype (sfunction (vec) matrix) rotate))
(defun rotate (vec)
"Construct a rotation matrix using first three elements of VEC as the
rotation factors."
(rotate* (aref vec 0) (aref vec 1) (aref vec 2)))
(declaim (ftype (sfunction (vec single-float) matrix) rotate-around))
(defun rotate-around (v radians)
"Construct a rotation matrix that rotates by RADIANS around VEC V. 4th
element of V is ignored."
(cond ((vec= v (vec 1f0 0f0 0f0))
(rotate* radians 0f0 0f0))
((vec= v (vec 0f0 1f0 0f0))
(rotate* 0f0 radians 0f0))
((vec= v (vec 0f0 0f0 1f0))
(rotate* 0f0 0f0 radians))
(t
(let ((c (cos radians))
(s (sin radians))
(g (- 1f0 (cos radians))))
(let* ((x (aref v 0))
(y (aref v 1))
(z (aref v 2))
(gxx (* g x x)) (gxy (* g x y)) (gxz (* g x z))
(gyy (* g y y)) (gyz (* g y z)) (gzz (* g z z)))
(matrix
(+ gxx c) (- gxy (* s z)) (+ gxz (* s y)) 0f0
(+ gxy (* s z)) (+ gyy c) (- gyz (* s x)) 0f0
(- gxz (* s y)) (+ gyz (* s x)) (+ gzz c) 0f0
0f0 0f0 0f0 1f0))))))
(declaim (ftype (sfunction (vec vec) matrix) reorient))
(defun reorient (v1 v2)
"Construct a transformation matrix to reorient V1 with V2."
(let ((nv1 (normalize v1))
(nv2 (normalize v2)))
(if (vec~ nv1 nv2)
(identity-matrix)
(rotate-around (normalize (cross-product nv1 nv2))
(acos (dot-product nv1 nv2))))))
(declaim (ftype (sfunction (matrix) matrix) transpose-matrix))
(defun transpose-matrix (matrix)
"Transpose of MATRIX."
(let ((transpose (zero-matrix)))
(dotimes (i 4)
(dotimes (j 4)
(setf (mref transpose i j) (mref matrix j i))))
transpose))
(declaim (ftype (sfunction (matrix) matrix) inverse-matrix))
(defun inverse-matrix (matrix)
"Inverse of MATRIX. Signals an error if there is no inverse."
(declare (type matrix matrix))
(if (eq matrix +identity-matrix+)
+identity-matrix+
(if (and (= 0f0 (mref matrix 3 0) (mref matrix 3 1) (mref matrix 3 2))
(= 1f0 (mref matrix 3 3)))
;; Affine matrix, fast track (and less loss of accuracy from multiplications)
(let ((inverse (zero-matrix)))
;; Inverse of the upper left 3x3
(let ((det (submatrix-determinant matrix)))
(if (zerop det)
;; If the 3x3 is zero, we're fine -- otherwise punt to the complete
;; implementation.
(dotimes (i 3)
(dotimes (j 3)
(unless (= 0f0 (mref matrix i j))
(return-from inverse-matrix (generic-inverse-matrix matrix)))))
(macrolet ((inv ((i j) (ai aj bi bj) (ci cj di dj))
`(setf (mref inverse ,(1- i) ,(1- j))
(/ (- (* (mref matrix ,(1- ai) ,(1- aj))
(mref matrix ,(1- bi) ,(1- bj)))
(* (mref matrix ,(1- ci) ,(1- cj))
(mref matrix ,(1- di) ,(1- dj))))
det))))
(inv (1 1) (2 2 3 3) (2 3 3 2))
(inv (1 2) (1 3 3 2) (1 2 3 3))
(inv (1 3) (1 2 2 3) (1 3 2 2))
(inv (2 1) (2 3 3 1) (2 1 3 3))
(inv (2 2) (1 1 3 3) (1 3 3 1))
(inv (2 3) (1 3 2 1) (1 1 2 3))
(inv (3 1) (2 1 3 2) (2 2 3 1))
(inv (3 2) (1 2 3 1) (1 1 3 2))
(inv (3 3) (1 1 2 2) (1 2 2 1)))))
;; translation: negation after dotting with upper rows
(let ((x (mref matrix 0 3))
(y (mref matrix 1 3))
(z (mref matrix 2 3)))
(dotimes (i 3)
(setf (mref inverse i 3) (- (+ (* x (mref inverse i 0))
(* y (mref inverse i 1))
(* z (mref inverse i 2)))))))
;; affine bottom row (0 0 0 1)
(setf (mref inverse 3 3) 1f0)
inverse)
(generic-inverse-matrix matrix))))
(defun submatrix-determinant (matrix)
"Determinant of the upper left 3x3 submatrix of MATRIX."
(macrolet ((a (i j)
`(mref matrix ,(- i 1) ,(- j 1))))
(- (+ (* (a 1 1) (a 2 2) (a 3 3))
(* (a 2 1) (a 3 2) (a 1 3))
(* (a 3 1) (a 1 2) (a 2 3)))
(* (a 1 1) (a 3 2) (a 2 3))
(* (a 3 1) (a 2 2) (a 1 3))
(* (a 2 1) (a 1 2) (a 3 3)))))
(defun matrix-determinant (matrix)
"Determinant of MATRIX."
(macrolet ((a (i j)
`(mref matrix (- ,i 1) (- ,j 1))))
(- (+ (* (a 1 1) (a 2 2) (a 3 3) (a 4 4))
(* (a 1 1) (a 2 3) (a 3 4) (a 4 2))
(* (a 1 1) (a 2 4) (a 3 2) (a 4 3))
(* (a 1 2) (a 2 1) (a 3 4) (a 4 3))
(* (a 1 2) (a 2 3) (a 3 1) (a 4 4))
(* (a 1 2) (a 2 4) (a 3 3) (a 4 1))
(* (a 1 3) (a 2 1) (a 3 2) (a 4 4))
(* (a 1 3) (a 2 2) (a 3 4) (a 4 1))
(* (a 1 3) (a 2 4) (a 3 1) (a 4 2))
(* (a 1 4) (a 2 1) (a 3 3) (a 4 2))
(* (a 1 4) (a 2 2) (a 3 1) (a 4 3))
(* (a 1 4) (a 2 3) (a 3 2) (a 4 1)))
(* (a 1 1) (a 2 2) (a 3 4) (a 4 3))
(* (a 1 1) (a 2 3) (a 3 2) (a 4 4))
(* (a 1 1) (a 2 4) (a 3 3) (a 4 2))
(* (a 1 2) (a 2 1) (a 3 3) (a 4 4))
(* (a 1 2) (a 2 3) (a 3 4) (a 4 1))
(* (a 1 2) (a 2 4) (a 3 1) (a 4 3))
(* (a 1 3) (a 2 1) (a 3 4) (a 4 2))
(* (a 1 3) (a 2 2) (a 3 1) (a 4 4))
(* (a 1 3) (a 2 4) (a 3 2) (a 4 1))
(* (a 1 4) (a 2 1) (a 3 2) (a 4 3))
(* (a 1 4) (a 2 2) (a 3 3) (a 4 1))
(* (a 1 4) (a 2 3) (a 3 1) (a 4 2)))))
;;; KLUDGE: Default is too low to do a good job with GENERIC-INVERSE-MATRIX.
#+sbcl
(eval-when (:compile-toplevel)
(setf sb-ext:*inline-expansion-limit* 1000))
(defun generic-inverse-matrix (matrix)
(let ((det (matrix-determinant matrix)))
(if (< (abs det) +default-epsilon+)
(error "Cannot invert matrix with zero determinant:~% ~S"
matrix)
(macrolet ((a (x y z)
(multiple-value-bind (r1 c1) (truncate (- x 11) 10)
(multiple-value-bind (r2 c2) (truncate (- y 11) 10)
(multiple-value-bind (r3 c3) (truncate (- z 11) 10)
`(* (mref matrix ,r1 ,c1)
(mref matrix ,r2 ,c2)
(mref matrix ,r3 ,c3)))))))
(let ((m
(matrix
;; row 1
(- (+ (a 22 33 44) (a 23 34 42) (a 24 32 43))
(a 22 34 43) (a 23 32 44) (a 24 33 42))
(- (+ (a 12 34 43) (a 13 32 44) (a 14 33 42))
(a 12 33 44) (a 13 34 42) (a 14 32 43))
(- (+ (a 12 23 44) (a 13 24 42) (a 14 22 43))
(a 12 24 43) (a 13 22 44) (a 14 23 42))
(- (+ (a 12 24 33) (a 13 22 34) (a 14 23 32))
(a 12 23 34) (a 13 24 32) (a 14 22 33))
;; row 2
(- (+ (a 21 34 43) (a 23 31 44) (a 24 33 41))
(a 21 33 44) (a 23 34 41) (a 24 31 43))
(- (+ (a 11 33 44) (a 13 34 41) (a 14 31 43))
(a 11 34 43) (a 13 31 44) (a 14 33 41))
(- (+ (a 11 24 43) (a 13 21 44) (a 14 23 41))
(a 11 23 44) (a 13 24 41) (a 14 21 43))
(- (+ (a 11 23 34) (a 13 24 31) (a 14 21 33))
(a 11 24 33) (a 13 21 34) (a 14 23 31))
;; row 3
(- (+ (a 21 32 44) (a 22 34 41) (a 24 31 42))
(a 21 34 42) (a 22 31 44) (a 24 32 41))
(- (+ (a 11 34 42) (a 12 31 44) (a 14 32 41))
(a 11 32 44) (a 12 34 41) (a 14 31 42))
(- (+ (a 11 22 44) (a 12 24 41) (a 14 21 42))
(a 11 24 42) (a 12 21 44) (a 14 22 41))
(- (+ (a 11 24 32) (a 12 21 34) (a 14 22 31))
(a 11 22 34) (a 12 24 31) (a 14 21 32))
;; row 4
(- (+ (a 21 33 42) (a 22 31 43) (a 23 32 41))
(a 21 32 43) (a 22 33 41) (a 23 31 42))
(- (+ (a 11 32 43) (a 12 33 41) (a 13 31 42))
(a 11 33 42) (a 12 31 43) (a 13 32 41))
(- (+ (a 11 23 42) (a 12 21 43) (a 13 22 41))
(a 11 22 43) (a 12 23 41) (a 13 21 42))
(- (+ (a 11 22 33) (a 12 23 31) (a 13 21 32))
(a 11 23 32) (a 12 21 33) (a 13 22 31)))))
(dotimes (i 4)
(dotimes (j 4)
(setf (mref m i j) (/ (mref m i j) det))))
m)))))