Use fiveam for testing.
[cl-tuples.git] / matrix.lisp
blob31c93e587463cb5c591b56c2636d94bf86983e8e
2 (in-package :cl-tuples)
4 (def-tuple-type matrix22
5 :tuple-element-type fast-float
6 :initial-element 0.0f0
7 :elements (e00 e01
8 e10 e11))
10 (export-tuple-operations matrix22)
12 (def-tuple-type matrix33
13 :tuple-element-type fast-float
14 :initial-element 0.0f0
15 :elements (e00 e01 e02
16 e10 e11 e12
17 e20 e21 e22))
19 (export-tuple-operations matrix33)
21 (def-tuple-type matrix44
22 :tuple-element-type fast-float
23 :initial-element 0.0f0
24 :elements (e00 e01 e02 e03
25 e10 e11 e12 e13
26 e20 e21 e22 e23
27 e30 e31 e32 e33))
29 (export-tuple-operations matrix44)
31 (def-tuple-op matrix33-equal*
32 ((k matrix33 (k00 k01 k02
33 k10 k11 k12
34 k20 k21 k22))
35 (m matrix33 (m00 m01 m02
36 m10 m11 m12
37 m20 m21 m22)))
38 (:return boolean
39 (and
40 (= k00 m00) (= k01 m01) (= k02 m02)
41 (= k10 m10) (= k11 m11) (= k12 m12)
42 (= k20 m20) (= k21 m21) (= k22 m22))))
44 (def-tuple-op matrix44-equal*
45 ((k matrix44 (k00 k01 k02 k03
46 k10 k11 k12 k13
47 k20 k21 k22 k23
48 k30 k31 k32 k33))
49 (m matrix44 (m00 m01 m02 m03
50 m10 m11 m12 m13
51 m20 m21 m22 m23
52 m30 m31 m32 m33)))
53 (:return boolean
54 (and
55 (= k00 m00) (= k01 m01) (= k02 m02) (= k03 m03)
56 (= k10 m10) (= k11 m11) (= k12 m12) (= k13 m13)
57 (= k20 m20) (= k21 m21) (= k22 m22) (= k23 m23)
58 (= k30 m30) (= k31 m31) (= k32 m32) (= k33 m33))))
60 ;; TODO: this probably needs to check the TRACE as well, since otherwise
61 ;; it might crash in other functions (e.g. MATRIX33-ANGLE-AXIS*)
62 (def-tuple-op check-rotation-matrix33*
63 ((m matrix33 :default))
64 (:return boolean (matrix33-equal*
65 (identity-matrix33*)
66 (matrix33-product*
67 m (transpose-matrix33* m)))))
69 ;; TODO: maybe check also whether it is purely a rotation?
70 (def-tuple-op check-rotation-matrix44*
71 ((m matrix44 :default))
72 (:return boolean (check-rotation-matrix33* (matrix44-matrix33* m))))
74 (def-tuple-op transpose-matrix22*
75 ((mat22 matrix22
76 (e00 e01
77 e10 e11)))
78 "Return the transpose of the matrix"
79 (:return matrix22
80 (matrix22-values*
81 e00 e10
82 e01 e11)))
84 (defmacro matrix-dot (dimension row col)
85 "Generate the symbols required for a dot product between the row and column of a matrix, assuming accessor symbol is e<row><col>"
86 (labels
87 ((make-matrix-element-symbol (mat row col)
88 (intern (string-upcase (format nil "e~A~A~A" mat row col)) :cl-tuples)))
89 (let
90 ((col-sym-names
91 (loop for row from 0 below dimension
92 collect (make-matrix-element-symbol 0 row col)))
93 (row-sym-names
94 (loop for col from 0 below dimension
95 collect (make-matrix-element-symbol 1 row col))))
96 `(+
97 ,@(loop
98 for col-sym in col-sym-names
99 for row-sym in row-sym-names
100 collect `(* ,col-sym ,row-sym))))))
103 (def-tuple-op transform-vertex2d*
104 ((mat matrix33 (e00 e01 e02 e10 e11 e12 e20 e21 e22))
105 (vert vector2d (x y w)))
106 (:return vertex2d
107 (vertex2d-values*
108 (+ (* x e00) (* y e01) (* w e02))
109 (+ (* x e10) (* y e11) (* w e12))
110 (+ (* x e20) (* y e21) (* w e22)))))
113 (def-tuple-op transform-vector2d*
114 ((mat matrix33 (e00 e01 e02 e10 e11 e12 e20 e21 e22))
115 (vec vector2d (x y)))
116 (:return vector2d
117 (vector2d-values*
118 (+ (* x e00) (* y e01))
119 (+ (* x e10) (* y e11)))))
122 (def-tuple-op matrix33-product*
123 ((m0 matrix33 (e000 e001 e002 e010 e011 e012 e020 e021 e022))
124 (m1 matrix33 (e100 e101 e102 e110 e111 e112 e120 e121 e122)))
125 (:return matrix33
126 (matrix33-values*
127 (matrix-dot 3 0 0)
128 (matrix-dot 3 0 1)
129 (matrix-dot 3 0 2)
131 (matrix-dot 3 1 0)
132 (matrix-dot 3 1 1)
133 (matrix-dot 3 1 2)
135 (matrix-dot 3 2 0)
136 (matrix-dot 3 2 1)
137 (matrix-dot 3 2 2))))
140 (def-tuple-op transform-vertex3d*
141 ((mat matrix44
142 (e00 e01 e02 e03
143 e10 e11 e12 e13
144 e20 e21 e22 e23
145 e30 e31 e32 e33))
146 (vert vertex3d (x y z w)))
147 (:return vertex3d
148 (vertex3d-values*
149 (+ (* x e00) (* y e01) (* z e02) (* w e03))
150 (+ (* x e10) (* y e11) (* z e12) (* w e13))
151 (+ (* x e20) (* y e21) (* z e22) (* w e23))
152 (+ (* x e30) (* y e31) (* z e32) (* w e33)))))
154 (def-tuple-op transform-vector3d*
155 ((mat matrix33
156 (e00 e01 e02
157 e10 e11 e12
158 e20 e21 e22))
159 (vect vector3d (x y z)))
160 (:return vector3d
161 (vector3d-values*
162 (+ (* x e00) (* y e01) (* z e02) )
163 (+ (* x e10) (* y e11) (* z e12) )
164 (+ (* x e20) (* y e21) (* z e12) ))))
166 (def-tuple-op transpose-matrix33*
167 ((mat33 matrix33 (e00 e01 e02 e10 e11 e12 e20 e21 e22)))
168 "Return the transpose of the matrix"
169 (:return matrix33
170 (matrix33-values*
171 e00 e10 e20
172 e01 e11 e21
173 e02 e12 e22)))
175 (def-tuple-op print-matrix33*
176 ((mat matrix44 (e00 e01 e02 e03
177 e10 e11 e12 e13
178 e20 e21 e22 e23
179 e30 e31 e32 e33)))
180 "Print a 3x3 matrix in a useful format."
181 (:return (values)
182 (format t "~A ~A ~A ~A ~%" e00 e01 e02)
183 (format t "~A ~A ~A ~A ~%" e10 e11 e12)
184 (format t "~A ~A ~A ~A ~%" e20 e21 e22)))
186 (def-tuple-op matrix44-product*
187 ((m0 matrix44 (e000 e001 e002 e003 e010 e011 e012 e013 e020 e021 e022 e023 e030 e031 e032 e033))
188 (m1 matrix44 (e100 e101 e102 e103 e110 e111 e112 e113 e120 e121 e122 e123 e130 e131 e132 e133)))
189 (:return matrix44
190 (matrix44-values*
191 (matrix-dot 4 0 0)
192 (matrix-dot 4 0 1)
193 (matrix-dot 4 0 2)
194 (matrix-dot 4 0 3)
196 (matrix-dot 4 1 0)
197 (matrix-dot 4 1 1)
198 (matrix-dot 4 1 2)
199 (matrix-dot 4 1 3)
201 (matrix-dot 4 2 0)
202 (matrix-dot 4 2 1)
203 (matrix-dot 4 2 2)
204 (matrix-dot 4 2 3)
206 (matrix-dot 4 3 0)
207 (matrix-dot 4 3 1)
208 (matrix-dot 4 3 2)
209 (matrix-dot 4 3 3))))
211 (def-tuple-op identity-matrix22*
213 (:return matrix22
214 (matrix22-key-values
215 e00 1.0
216 e11 1.0)))
218 (def-tuple-op identity-matrix33*
220 (:return matrix33
221 (matrix33-key-values
222 e00 1.0
223 e11 1.0
224 e22 1.0)))
226 (def-tuple-op identity-matrix44*
228 (:return matrix44
229 (matrix44-key-values
230 e00 1.0
231 e11 1.0
232 e22 1.0
233 e33 1.0)))
235 (def-tuple-op translation-matrix44*
236 ((tx fast-float)
237 (ty fast-float)
238 (tz fast-float))
239 "Return a matrix that represents a translation transformation"
240 (:return matrix44
241 (matrix44-values*
242 1.0f0 0.0f0 0.0f0 tx
243 0.0f0 1.0f0 0.0f0 ty
244 0.0f0 0.0f0 1.0f0 tz
245 0.0f0 0.0f0 0.0f0 1.0f0)))
247 (def-tuple-op scaling-matrix44*
248 ((sx #1=fast-float)
249 (sy #1#)
250 (sz #1#))
251 (:return matrix44
252 (matrix44-key-values
253 e00 sx
254 e11 sy
255 e22 sz
256 e33 1.0)))
258 (def-tuple-op vertex3d-translation-matrix44*
259 ((vert vertex3d (tx ty tz tw)))
260 "Return a matrix that represents a translation transformation"
261 (:return matrix44
262 (matrix44-values*
263 1.0f0 0.0f0 0.0f0 tx
264 0.0f0 1.0f0 0.0f0 ty
265 0.0f0 0.0f0 1.0f0 tz
266 0.0f0 0.0f0 0.0f0 1.0f0)))
268 (def-tuple-op rotatex-matrix33*
269 ((rotation fast-float))
270 "Return a matrix for rotating around the x axis."
271 (:return matrix33
272 (let* ((sin (sin rotation))
273 (-sin (- sin))
274 (cos (cos rotation)))
275 (matrix33-values*
276 1.0 0.0 0.0
277 0.0 cos -sin
278 0.0 sin cos))))
280 (def-tuple-op rotatex-matrix44*
281 ((rotation fast-float))
282 "Return a matrix for rotating around the x axis."
283 (:return matrix44
284 (matrix33-matrix44*
285 (rotatex-matrix33* rotation))))
288 (def-tuple-op rotatey-matrix33*
289 ((rotation fast-float))
290 "Return a matrix for rotating around the y axis."
291 (:return matrix33
292 (let* ((sin (sin rotation))
293 (-sin (- sin))
294 (cos (cos rotation)))
295 (matrix33-values*
296 cos 0.0 sin
297 0.0 1.0 0.0
298 -sin 0.0 cos))))
301 (def-tuple-op rotatey-matrix44*
302 ((rotation fast-float))
303 "Return a matrix for rotating around the y axis."
304 (:return matrix44
305 (matrix33-matrix44*
306 (rotatey-matrix33* rotation))))
308 (def-tuple-op rotatez-matrix33*
309 ((rotation fast-float))
310 "Return a matrix for rotating around the z axis."
311 (:return matrix33
312 (let* ((sin (sin rotation))
313 (-sin (- sin))
314 (cos (cos rotation)))
315 (matrix33-values*
316 cos -sin 0.0
317 sin cos 0.0
318 0.0 0.0 1.0))))
321 (def-tuple-op rotatez-matrix44*
322 ((rotation fast-float))
323 "Return a matrix for rotating around the z axis."
324 (:return matrix44
325 (matrix33-matrix44*
326 (rotatez-matrix33* rotation))))
328 (def-tuple-op transpose-matrix44*
329 ((mat44 matrix44
330 (e00 e01 e02 e03
331 e10 e11 e12 e13
332 e20 e21 e22 e23
333 e30 e31 e32 e33)))
334 "Return the transpose of the matrix"
335 (:return matrix44
336 (matrix44-values*
337 e00 e10 e20 e30
338 e01 e11 e21 e31
339 e02 e12 e22 e32
340 e03 e13 e23 e33)))
342 (def-tuple-op make-test-matrix44*
344 "Return a matrix for testing purposes"
345 (:return matrix44
346 (matrix44-values*
347 1.0f0 2.0f0 3.0f0 4.0f0
348 5.0f0 6.0f0 7.0f0 8.0f0
349 9.0f0 10.0f0 11.0f0 12.0f0
350 13.0f0 14.0f0 15.0f0 16.0f0)))
353 (def-tuple-op print-matrix44*
354 ((mat matrix44 (e00 e01 e02 e03
355 e10 e11 e12 e13
356 e20 e21 e22 e23
357 e30 e31 e32 e33)))
358 "Print a matrix in a useful format."
359 (:return (values)
360 (format t "~A ~A ~A ~A ~%" e00 e01 e02 e03)
361 (format t "~A ~A ~A ~A ~%" e10 e11 e12 e13)
362 (format t "~A ~A ~A ~A ~%" e20 e21 e22 e23)
363 (format t "~A ~A ~A ~A ~%" e30 e31 e32 e33)))
366 (def-tuple-op matrix44-matrix33*
367 ((mat44 matrix44
368 (e00 e01 e02 e03
369 e10 e11 e12 e13
370 e20 e21 e22 e23
371 e30 e31 e32 e33)))
372 "Convert a 4x4 matrix to a 3x3 matrix"
373 (:return matrix33
374 (matrix33-values* e00 e01 e02 e10 e11 e12 e20 e21 e22)))
376 (def-tuple-op matrix33-matrix44*
377 ((mat3 matrix33
378 (e00 e01 e02
379 e10 e11 e12
380 e20 e21 e22)))
381 "Convert a 3x3 matrix to a 4x4 matrix"
382 (:return matrix44
383 (matrix44-values* e00 e01 e02 0.0f0
384 e10 e11 e12 0.0f0
385 e20 e21 e22 0.0f0
386 0.0f0 0.0f0 0.0f0 1.0f0)))
388 (def-tuple-op vector3d-matrix3d*
389 ((zvec vector3d (zx zy zz))
390 (yvec vector3d (yx yy yz)))
391 "Construct a rotation matrix from 2 vectors"
392 (:return matrix33
393 (with-vector3d
394 (vector3d-cross
395 zvec yvec)
396 (xx xy xz)
397 (matrix33*
398 (xx yx zx
399 xy yy zy
400 xz yz zz)))))
403 (def-tuple-op matrix22-determinant*
404 ((mat matrix22 #.(tuple-elements 'matrix22)))
405 (:return fast-float
406 (- (* e00 e11)
407 (* e01 e10))))
409 (def-tuple-op matrix33-determinant*
410 ((mat matrix33 #.(tuple-elements 'matrix33)))
411 (:return fast-float
412 (- (+ (* e00 e11 e22)
413 (* e01 e12 e20)
414 (* e02 e10 e21))
415 (* e02 e11 e20)
416 (* e01 e10 e22)
417 (* e00 e12 e21))))
419 (def-tuple-op matrix44-determinant*
420 ((mat matrix44 #.(tuple-elements 'matrix44)))
421 (:return fast-float
422 (let ((t0 (* e00 e22))
423 (t1 (* e11 e33))
424 (t2 (* e01 e23))
425 (t3 (* e12 e30))
426 (t4 (* e03 e21))
427 (t5 (* e10 e32))
428 (t6 (* e13 e31))
429 (t7 (* e02 e20)))
430 (- (+ (* t0 t1)
431 (* t2 t3)
432 (* t7 t6)
433 (* t4 t5))
434 (* t3 t4)
435 (* t7 t1)
436 (* t2 t5)
437 (* t0 t6)))))
439 (def-tuple-op matrix22-scale*
440 ((x fast-float)
441 (mat matrix22 #1=#.(tuple-elements 'matrix22)))
442 (:return matrix22 (multiply-arguments matrix22-values* x #1#)))
444 (def-tuple-op matrix33-scale*
445 ((x fast-float)
446 (mat matrix33 #1=#.(tuple-elements 'matrix33)))
447 (:return matrix33 (multiply-arguments matrix33-values* x #1#)))
449 (def-tuple-op matrix44-scale*
450 ((x fast-float)
451 (mat matrix44 #1=#.(tuple-elements 'matrix44)))
452 (:return matrix44 (multiply-arguments matrix44-values* x #1#)))
454 (def-tuple-op cofactor-matrix22*
455 ((mat matrix22 #.(tuple-elements 'matrix22)))
456 (:return matrix22
457 (matrix22-values*
458 e11 e10
459 e01 e00)))
461 (def-tuple-op cofactor-matrix33*
462 ((mat matrix33 #.(tuple-elements 'matrix33)))
463 (:return matrix33
464 (macrolet ((cofactors ()
465 `(matrix33-values*
466 ,@(matrix-cofactors 3))))
467 (cofactors))))
469 (def-tuple-op cofactor-matrix44*
470 ((mat matrix44 #.(tuple-elements 'matrix44)))
471 (:return matrix44
472 (macrolet ((cofactors ()
473 `(matrix44-values*
474 ,@(matrix-cofactors 4))))
475 (cofactors))))
477 (def-tuple-op inverted-matrix22*
478 ((mat matrix22 #.(tuple-elements 'matrix22)))
479 (:return matrix22
480 (matrix22-scale*
481 (matrix22-determinant* mat)
482 (transpose-matrix22*
483 (cofactor-matrix22* mat)))))
485 (def-tuple-op inverted-matrix33*
486 ((mat matrix33 #.(tuple-elements 'matrix33)))
487 (:return matrix33
488 (matrix33-scale*
489 (matrix33-determinant* mat)
490 (transpose-matrix33*
491 (cofactor-matrix33* mat)))))
493 (def-tuple-op inverted-matrix44*
494 ((mat matrix44 #.(tuple-elements 'matrix44)))
495 (:return matrix44
496 (matrix44-scale*
497 (matrix44-determinant* mat)
498 (transpose-matrix44*
499 (cofactor-matrix44* mat)))))