2 (in-package :cl-tuples
)
4 (def-tuple-type matrix22
5 :tuple-element-type fast-float
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
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
29 (export-tuple-operations matrix44
)
31 (defmacro matrix-dot
(dimension row col
)
32 "Generate the symbols required for a dot product between the row and column of a matrix, assuming accessor symbol is e<row><col>"
34 ((make-matrix-element-symbol (mat row col
)
35 (intern (string-upcase (format nil
"e~A~A~A" mat row col
)) :cl-tuples
)))
38 (loop for row from
0 below dimension
39 collect
(make-matrix-element-symbol 0 row col
)))
41 (loop for col from
0 below dimension
42 collect
(make-matrix-element-symbol 1 row col
))))
45 for col-sym in col-sym-names
46 for row-sym in row-sym-names
47 collect
`(* ,col-sym
,row-sym
))))))
50 (def-tuple-op transform-vertex2d
*
51 ((mat matrix33
(e00 e01 e02 e10 e11 e12 e20 e21 e22
))
52 (vert vector2d
(x y w
)))
55 (+ (* x e00
) (* y e01
) (* w e02
))
56 (+ (* x e10
) (* y e11
) (* w e12
))
57 (+ (* x e20
) (* y e21
) (* w e22
)))))
60 (def-tuple-op transform-vector2d
*
61 ((mat matrix33
(e00 e01 e02 e10 e11 e12 e20 e21 e22
))
65 (+ (* x e00
) (* y e01
))
66 (+ (* x e10
) (* y e11
)))))
69 (def-tuple-op matrix33-product
*
70 ((m0 matrix33
(e000 e001 e002 e010 e011 e012 e020 e021 e022
))
71 (m1 matrix33
(e100 e101 e102 e110 e111 e112 e120 e121 e122
)))
87 (def-tuple-op transform-vertex3d
*
93 (vert vertex3d
(x y z w
)))
96 (+ (* x e00
) (* y e01
) (* z e02
) (* w e03
))
97 (+ (* x e10
) (* y e11
) (* z e12
) (* w e13
))
98 (+ (* x e20
) (* y e21
) (* z e22
) (* w e23
))
99 (+ (* x e30
) (* y e31
) (* z e32
) (* w e33
)))))
101 (def-tuple-op transform-vector3d
*
106 (vect vector3d
(x y z
)))
109 (+ (* x e00
) (* y e01
) (* z e02
) )
110 (+ (* x e10
) (* y e11
) (* z e12
) )
111 (+ (* x e20
) (* y e21
) (* z e12
) ))))
113 (def-tuple-op transpose-matrix33
*
114 ((mat33 matrix33
(e00 e01 e02 e10 e11 e12 e20 e21 e22
)))
115 "Return the transpose of the matrix"
122 (def-tuple-op print-matrix33
*
123 ((mat matrix44
(e00 e01 e02 e03
127 "Print a 3x3 matrix in a useful format."
129 (format t
"~A ~A ~A ~A ~%" e00 e01 e02
)
130 (format t
"~A ~A ~A ~A ~%" e10 e11 e12
)
131 (format t
"~A ~A ~A ~A ~%" e20 e21 e22
)))
133 (def-tuple-op matrix44-product
*
134 ((m0 matrix44
(e000 e001 e002 e003 e010 e011 e012 e013 e020 e021 e022 e023 e030 e031 e032 e033
))
135 (m1 matrix44
(e100 e101 e102 e103 e110 e111 e112 e113 e120 e121 e122 e123 e130 e131 e132 e133
)))
156 (matrix-dot 4 3 3))))
158 (def-tuple-op identity-matrix44
*
162 1.0f0
0.0f0
0.0f0
0.0f0
163 0.0f0
1.0f0
0.0f0
0.0f0
164 0.0f0
0.0f0
1.0f0
0.0f0
165 0.0f0
0.0f0
0.0f0
1.0f0
)))
167 (def-tuple-op translation-matrix44
*
171 "Return a matrix that represents a translation transformation"
177 0.0f0
0.0f0
0.0f0
1.0f0
)))
179 (def-tuple-op vertex3d-translation-matrix44
*
180 ((vert vertex3d
(tx ty tz tw
)))
181 "Return a matrix that represents a translation transformation"
187 0.0f0
0.0f0
0.0f0
1.0f0
)))
190 (def-tuple-op rotatex-matrix44
*
191 ((rotation fast-float
))
192 "Return a matrix for rotating around the x axis."
195 1.0f0
0.0f0
0.0f0
0.0f0
196 0.0f0
(cos rotation
) (- (sin rotation
)) 0.0f0
197 0.0f0
(sin rotation
) (cos rotation
) 0.0f0
198 0.0f0
0.0f0
0.0f0
1.0f0
)))
202 (def-tuple-op rotatey-matrix44
*
203 ((rotation fast-float
))
204 "Return a matrix for rotating around the y axis."
207 (cos rotation
) 0.0f0
(sin rotation
) 0.0f0
208 0.0f0
1.0f0
0.0f0
0.0f0
209 (- (sin rotation
)) 0.0f0
(cos rotation
) 0.0f0
210 0.0f0
0.0f0
0.0f0
1.0f0
)))
212 (def-tuple-op rotatez-matrix44
*
213 ((rotation fast-float
))
214 "Return a matrix for rotating around the z axis."
217 (cos rotation
) 0.0f0
(- (sin rotation
)) 0.0f0
218 (sin rotation
) 0.0f0
(cos rotation
) 0.0f0
219 0.0f0
0.0f0
1.0f0
0.0f0
220 0.0f0
0.0f0
0.0f0
1.0f0
)))
222 (def-tuple-op transpose-matrix44
*
228 "Return the transpose of the matrix"
236 (def-tuple-op make-test-matrix44
*
238 "Return a matrix for testing purposes"
241 1.0f0
2.0f0
3.0f0
4.0f0
242 5.0f0
6.0f0
7.0f0
8.0f0
243 9.0f0
10.0f0
11.0f0
12.0f0
244 13.0f0
14.0f0
15.0f0
16.0f0
)))
247 (def-tuple-op print-matrix44
*
248 ((mat matrix44
(e00 e01 e02 e03
252 "Print a matrix in a useful format."
254 (format t
"~A ~A ~A ~A ~%" e00 e01 e02 e03
)
255 (format t
"~A ~A ~A ~A ~%" e10 e11 e12 e13
)
256 (format t
"~A ~A ~A ~A ~%" e20 e21 e22 e23
)
257 (format t
"~A ~A ~A ~A ~%" e30 e31 e32 e33
)))
260 (def-tuple-op matrix44-matrix33
*
266 "Convert a 4x4 matrix to a 3x3 matrix"
268 (matrix33-values* e00 e01 e02 e10 e11 e12 e20 e21 e22
)))
270 (def-tuple-op matrix33-matrix44
*
275 "Convert a 3x3 matrix to a 4x4 matrix"
277 (matrix44-values* e00 e01 e02
0.0f0
280 0.0f0
0.0f0
0.0f0
1.0f0
)))
282 (def-tuple-op vector3d-matrix3d
*
283 ((zvec vector3d
(zx zy zz
))
284 (yvec vector3d
(yx yy yz
)))
285 "Construct a rotation matrix from 2 vectors"
297 (def-tuple-op matrix22-determinant
*
298 ((mat matrix22
#.
(tuple-elements 'matrix22
)))
303 (def-tuple-op matrix33-determinant
*
304 ((mat matrix33
#.
(tuple-elements 'matrix33
)))
306 (- (+ (* e00 e11 e22
)
313 (def-tuple-op matrix44-determinant
*
314 ((mat matrix44
#.
(tuple-elements 'matrix44
)))
316 (let ((t0 (* e00 e22
))
333 (def-tuple-op matrix22-scale
*
335 (mat matrix22
#1=#.
(tuple-elements 'matrix22
)))
336 (:return matrix22
(multiply-arguments matrix22-values
* x
#1#)))
338 (def-tuple-op matrix33-scale
*
340 (mat matrix33
#1=#.
(tuple-elements 'matrix33
)))
341 (:return matrix33
(multiply-arguments matrix33-values
* x
#1#)))
343 (def-tuple-op matrix44-scale
*
345 (mat matrix44
#1=#.
(tuple-elements 'matrix44
)))
346 (:return matrix44
(multiply-arguments matrix44-values
* x
#1#)))