Fix compilation problem for non-tuple types.
[cl-tuples.git] / matrix.lisp
blob882fba443be5e3376ec4ff6a119847382268721a
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))))
287 (def-tuple-op rotatey-matrix33*
288 ((rotation fast-float))
289 "Return a matrix for rotating around the y axis."
290 (:return matrix33
291 (let* ((sin (sin rotation))
292 (-sin (- sin))
293 (cos (cos rotation)))
294 (matrix33-values*
295 cos 0.0 sin
296 0.0 1.0 0.0
297 -sin 0.0 cos))))
299 (def-tuple-op rotatey-matrix44*
300 ((rotation fast-float))
301 "Return a matrix for rotating around the y axis."
302 (:return matrix44
303 (matrix33-matrix44*
304 (rotatey-matrix33* rotation))))
306 (def-tuple-op rotatez-matrix33*
307 ((rotation fast-float))
308 "Return a matrix for rotating around the z axis."
309 (:return matrix33
310 (let* ((sin (sin rotation))
311 (-sin (- sin))
312 (cos (cos rotation)))
313 (matrix33-values*
314 cos -sin 0.0
315 sin cos 0.0
316 0.0 0.0 1.0))))
318 (def-tuple-op rotatez-matrix44*
319 ((rotation fast-float))
320 "Return a matrix for rotating around the z axis."
321 (:return matrix44
322 (matrix33-matrix44*
323 (rotatez-matrix33* rotation))))
325 (def-tuple-op transpose-matrix44*
326 ((mat44 matrix44
327 (e00 e01 e02 e03
328 e10 e11 e12 e13
329 e20 e21 e22 e23
330 e30 e31 e32 e33)))
331 "Return the transpose of the matrix"
332 (:return matrix44
333 (matrix44-values*
334 e00 e10 e20 e30
335 e01 e11 e21 e31
336 e02 e12 e22 e32
337 e03 e13 e23 e33)))
339 (def-tuple-op make-test-matrix44*
341 "Return a matrix for testing purposes"
342 (:return matrix44
343 (matrix44-values*
344 1.0f0 2.0f0 3.0f0 4.0f0
345 5.0f0 6.0f0 7.0f0 8.0f0
346 9.0f0 10.0f0 11.0f0 12.0f0
347 13.0f0 14.0f0 15.0f0 16.0f0)))
350 (def-tuple-op print-matrix44*
351 ((mat matrix44 (e00 e01 e02 e03
352 e10 e11 e12 e13
353 e20 e21 e22 e23
354 e30 e31 e32 e33)))
355 "Print a matrix in a useful format."
356 (:return (values)
357 (format t "~A ~A ~A ~A ~%" e00 e01 e02 e03)
358 (format t "~A ~A ~A ~A ~%" e10 e11 e12 e13)
359 (format t "~A ~A ~A ~A ~%" e20 e21 e22 e23)
360 (format t "~A ~A ~A ~A ~%" e30 e31 e32 e33)))
363 (def-tuple-op matrix44-matrix33*
364 ((mat44 matrix44
365 (e00 e01 e02 e03
366 e10 e11 e12 e13
367 e20 e21 e22 e23
368 e30 e31 e32 e33)))
369 "Convert a 4x4 matrix to a 3x3 matrix"
370 (:return matrix33
371 (matrix33-values* e00 e01 e02 e10 e11 e12 e20 e21 e22)))
373 (def-tuple-op matrix33-matrix44*
374 ((mat3 matrix33
375 (e00 e01 e02
376 e10 e11 e12
377 e20 e21 e22)))
378 "Convert a 3x3 matrix to a 4x4 matrix"
379 (:return matrix44
380 (matrix44-values* e00 e01 e02 0.0f0
381 e10 e11 e12 0.0f0
382 e20 e21 e22 0.0f0
383 0.0f0 0.0f0 0.0f0 1.0f0)))
385 (def-tuple-op vector3d-matrix3d*
386 ((zvec vector3d (zx zy zz))
387 (yvec vector3d (yx yy yz)))
388 "Construct a rotation matrix from 2 vectors"
389 (:return matrix33
390 (with-vector3d
391 (vector3d-cross
392 zvec yvec)
393 (xx xy xz)
394 (matrix33*
395 (xx yx zx
396 xy yy zy
397 xz yz zz)))))
400 (def-tuple-op matrix22-determinant*
401 ((mat matrix22 #.(tuple-elements 'matrix22)))
402 (:return fast-float
403 (- (* e00 e11)
404 (* e01 e10))))
406 (def-tuple-op matrix33-determinant*
407 ((mat matrix33 #.(tuple-elements 'matrix33)))
408 (:return fast-float
409 (- (+ (* e00 e11 e22)
410 (* e01 e12 e20)
411 (* e02 e10 e21))
412 (* e02 e11 e20)
413 (* e01 e10 e22)
414 (* e00 e12 e21))))
416 (def-tuple-op matrix44-determinant*
417 ((mat matrix44 #.(tuple-elements 'matrix44)))
418 (:return fast-float
419 (let ((t0 (* e00 e22))
420 (t1 (* e11 e33))
421 (t2 (* e01 e23))
422 (t3 (* e12 e30))
423 (t4 (* e03 e21))
424 (t5 (* e10 e32))
425 (t6 (* e13 e31))
426 (t7 (* e02 e20)))
427 (- (+ (* t0 t1)
428 (* t2 t3)
429 (* t7 t6)
430 (* t4 t5))
431 (* t3 t4)
432 (* t7 t1)
433 (* t2 t5)
434 (* t0 t6)))))
436 (def-tuple-op matrix22-scale*
437 ((x fast-float)
438 (mat matrix22 #1=#.(tuple-elements 'matrix22)))
439 (:return matrix22 (multiply-arguments matrix22-values* x #1#)))
441 (def-tuple-op matrix33-scale*
442 ((x fast-float)
443 (mat matrix33 #1=#.(tuple-elements 'matrix33)))
444 (:return matrix33 (multiply-arguments matrix33-values* x #1#)))
446 (def-tuple-op matrix44-scale*
447 ((x fast-float)
448 (mat matrix44 #1=#.(tuple-elements 'matrix44)))
449 (:return matrix44 (multiply-arguments matrix44-values* x #1#)))
451 (def-tuple-op cofactor-matrix22*
452 ((mat matrix22 #.(tuple-elements 'matrix22)))
453 (:return matrix22
454 (matrix22-values*
455 e11 e10
456 e01 e00)))
458 (def-tuple-op cofactor-matrix33*
459 ((mat matrix33 #.(tuple-elements 'matrix33)))
460 (:return matrix33
461 (macrolet ((cofactors ()
462 `(matrix33-values*
463 ,@(matrix-cofactors 3))))
464 (cofactors))))
466 (def-tuple-op cofactor-matrix44*
467 ((mat matrix44 #.(tuple-elements 'matrix44)))
468 (:return matrix44
469 (macrolet ((cofactors ()
470 `(matrix44-values*
471 ,@(matrix-cofactors 4))))
472 (cofactors))))
474 (def-tuple-op inverted-matrix22*
475 ((mat matrix22 #.(tuple-elements 'matrix22)))
476 (:return matrix22
477 (matrix22-scale*
478 (matrix22-determinant* mat)
479 (transpose-matrix22*
480 (cofactor-matrix22* mat)))))
482 (def-tuple-op inverted-matrix33*
483 ((mat matrix33 #.(tuple-elements 'matrix33)))
484 (:return matrix33
485 (matrix33-scale*
486 (matrix33-determinant* mat)
487 (transpose-matrix33*
488 (cofactor-matrix33* mat)))))
490 (def-tuple-op inverted-matrix44*
491 ((mat matrix44 #.(tuple-elements 'matrix44)))
492 (:return matrix44
493 (matrix44-scale*
494 (matrix44-determinant* mat)
495 (transpose-matrix44*
496 (cofactor-matrix44* mat)))))