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
|
;;; Sly
;;; Copyright (C) 2014 David Thompson <dthompson2@worcester.edu>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program. If not, see
;;; <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; 4x4 column-major transformation matrix.
;;
;;; Code:
(define-module (sly math transform)
#:use-module (system foreign)
#:use-module (ice-9 match)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-4)
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-42)
#:use-module (sly math)
#:use-module (sly math vector)
#:use-module (sly math quaternion)
#:export (make-transform null-transform identity-transform
transform? transform-matrix
transpose transform-vector2
transform-position
transform->pointer
transform+ transform* transform*!
scale translate rotate-x rotate-y rotate-z rotate
build-transform
orthographic-projection perspective-projection
look-at))
(define-record-type <transform>
(%make-transform matrix ptr)
transform?
(matrix transform-matrix)
(ptr transform-ptr set-transform-ptr!))
(define (make-4x4-matrix)
(make-f32vector 16))
(define-inlinable (matrix-set! matrix row column x)
(f32vector-set! matrix (+ (* row 4) column) x))
(define-inlinable (matrix-ref matrix row column)
(f32vector-ref matrix (+ (* row 4) column)))
(define (make-transform aa ab ac ad
ba bb bc bd
ca cb cc cd
da db dc dd)
"Return a new transform initialized with the given 16 values in
column-major format."
(let ((matrix (make-4x4-matrix)))
(matrix-set! matrix 0 0 aa)
(matrix-set! matrix 0 1 ab)
(matrix-set! matrix 0 2 ac)
(matrix-set! matrix 0 3 ad)
(matrix-set! matrix 1 0 ba)
(matrix-set! matrix 1 1 bb)
(matrix-set! matrix 1 2 bc)
(matrix-set! matrix 1 3 bd)
(matrix-set! matrix 2 0 ca)
(matrix-set! matrix 2 1 cb)
(matrix-set! matrix 2 2 cc)
(matrix-set! matrix 2 3 cd)
(matrix-set! matrix 3 0 da)
(matrix-set! matrix 3 1 db)
(matrix-set! matrix 3 2 dc)
(matrix-set! matrix 3 3 dd)
(%make-transform matrix #f)))
(define null-transform
(%make-transform (make-4x4-matrix) #f))
(define identity-transform
(make-transform 1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1))
(define (transpose transform)
"Return a transform that is the transpose of TRANSFORM."
(let ((m1 (transform-matrix transform))
(m2 (make-4x4-matrix)))
(do-ec (: r 4) (: c 4)
(matrix-set! m2 c r
(matrix-ref m1 r c)))
(%make-transform m2 #f)))
(define (transform-vector2 transform v)
"Apply TRANSFORM to the 2D vector V."
(let ((m (transform-matrix transform))
(x (vx v))
(y (vy v)))
(vector2 (+ (* x (matrix-ref m 0 0))
(* y (matrix-ref m 0 1))
(matrix-ref m 0 3))
(+ (* x (matrix-ref m 1 0))
(* y (matrix-ref m 1 1))
(matrix-ref m 1 3)))))
(define (transform-position transform)
"Return a vector3 containing the positional data stored in
TRANSFORM."
(let ((matrix (transform-matrix transform)))
(vector3 (matrix-ref matrix 3 0)
(matrix-ref matrix 3 1)
(matrix-ref matrix 3 2))))
(define (transform+ . transforms)
"Return the sum of TRANSFORM. Return 'null-transform' if called
without any arguments."
(define (add a b)
(let ((m1 (transform-matrix a))
(m2 (transform-matrix b))
(m3 (make-4x4-matrix)))
(do-ec (: r 4) (: c 4)
(let ((x (+ (matrix-ref m1 r c)
(matrix-ref m2 r c))))
(matrix-set! m3 r c x)))
(%make-transform m3 #f)))
(reduce add null-transform transforms))
(define (transform->pointer t)
(let ((ptr (transform-ptr t)))
(or ptr
(let ((ptr (bytevector->pointer (transform-matrix t))))
(set-transform-ptr! t ptr)
ptr))))
(define (transform* . transforms)
"Return the product of TRANSFORMS. Return identity-transform if
called without any arguments."
(define (mul a b)
(let ((result (%make-transform (make-4x4-matrix) #f)))
(transform*! result a b)
result))
(reduce mul identity-transform transforms))
;; This could be described as a loop, but the result would be much
;; slower matrix multiplication. To enable Guile's optimizer to unbox
;; floating point ops and reduce reads/writes, each matrix element is
;; bound as a local, the loop is completely unrolled, and matrix-ref
;; and matrix-set! are inlined.
(define (transform*! dest a b)
(let ((m1 (transform-matrix a))
(m2 (transform-matrix b))
(m3 (transform-matrix dest)))
(let ((m1-0-0 (matrix-ref m1 0 0))
(m1-0-1 (matrix-ref m1 0 1))
(m1-0-2 (matrix-ref m1 0 2))
(m1-0-3 (matrix-ref m1 0 3))
(m1-1-0 (matrix-ref m1 1 0))
(m1-1-1 (matrix-ref m1 1 1))
(m1-1-2 (matrix-ref m1 1 2))
(m1-1-3 (matrix-ref m1 1 3))
(m1-2-0 (matrix-ref m1 2 0))
(m1-2-1 (matrix-ref m1 2 1))
(m1-2-2 (matrix-ref m1 2 2))
(m1-2-3 (matrix-ref m1 2 3))
(m1-3-0 (matrix-ref m1 3 0))
(m1-3-1 (matrix-ref m1 3 1))
(m1-3-2 (matrix-ref m1 3 2))
(m1-3-3 (matrix-ref m1 3 3))
(m2-0-0 (matrix-ref m2 0 0))
(m2-0-1 (matrix-ref m2 0 1))
(m2-0-2 (matrix-ref m2 0 2))
(m2-0-3 (matrix-ref m2 0 3))
(m2-1-0 (matrix-ref m2 1 0))
(m2-1-1 (matrix-ref m2 1 1))
(m2-1-2 (matrix-ref m2 1 2))
(m2-1-3 (matrix-ref m2 1 3))
(m2-2-0 (matrix-ref m2 2 0))
(m2-2-1 (matrix-ref m2 2 1))
(m2-2-2 (matrix-ref m2 2 2))
(m2-2-3 (matrix-ref m2 2 3))
(m2-3-0 (matrix-ref m2 3 0))
(m2-3-1 (matrix-ref m2 3 1))
(m2-3-2 (matrix-ref m2 3 2))
(m2-3-3 (matrix-ref m2 3 3)))
(matrix-set! m3 0 0
(+ (* m1-0-0 m2-0-0)
(* m1-0-1 m2-1-0)
(* m1-0-2 m2-2-0)
(* m1-0-3 m2-3-0)))
(matrix-set! m3 0 1
(+ (* m1-0-0 m2-0-1)
(* m1-0-1 m2-1-1)
(* m1-0-2 m2-2-1)
(* m1-0-3 m2-3-1)))
(matrix-set! m3 0 2
(+ (* m1-0-0 m2-0-2)
(* m1-0-1 m2-1-2)
(* m1-0-2 m2-2-2)
(* m1-0-3 m2-3-2)))
(matrix-set! m3 0 3
(+ (* m1-0-0 m2-0-3)
(* m1-0-1 m2-1-3)
(* m1-0-2 m2-2-3)
(* m1-0-3 m2-3-3)))
(matrix-set! m3 1 0
(+ (* m1-1-0 m2-0-0)
(* m1-1-1 m2-1-0)
(* m1-1-2 m2-2-0)
(* m1-1-3 m2-3-0)))
(matrix-set! m3 1 1
(+ (* m1-1-0 m2-0-1)
(* m1-1-1 m2-1-1)
(* m1-1-2 m2-2-1)
(* m1-1-3 m2-3-1)))
(matrix-set! m3 1 2
(+ (* m1-1-0 m2-0-2)
(* m1-1-1 m2-1-2)
(* m1-1-2 m2-2-2)
(* m1-1-3 m2-3-2)))
(matrix-set! m3 1 3
(+ (* m1-1-0 m2-0-3)
(* m1-1-1 m2-1-3)
(* m1-1-2 m2-2-3)
(* m1-1-3 m2-3-3)))
(matrix-set! m3 2 0
(+ (* m1-2-0 m2-0-0)
(* m1-2-1 m2-1-0)
(* m1-2-2 m2-2-0)
(* m1-2-3 m2-3-0)))
(matrix-set! m3 2 1
(+ (* m1-2-0 m2-0-1)
(* m1-2-1 m2-1-1)
(* m1-2-2 m2-2-1)
(* m1-2-3 m2-3-1)))
(matrix-set! m3 2 2
(+ (* m1-2-0 m2-0-2)
(* m1-2-1 m2-1-2)
(* m1-2-2 m2-2-2)
(* m1-2-3 m2-3-2)))
(matrix-set! m3 2 3
(+ (* m1-2-0 m2-0-3)
(* m1-2-1 m2-1-3)
(* m1-2-2 m2-2-3)
(* m1-2-3 m2-3-3)))
(matrix-set! m3 3 0
(+ (* m1-3-0 m2-0-0)
(* m1-3-1 m2-1-0)
(* m1-3-2 m2-2-0)
(* m1-3-3 m2-3-0)))
(matrix-set! m3 3 1
(+ (* m1-3-0 m2-0-1)
(* m1-3-1 m2-1-1)
(* m1-3-2 m2-2-1)
(* m1-3-3 m2-3-1)))
(matrix-set! m3 3 2
(+ (* m1-3-0 m2-0-2)
(* m1-3-1 m2-1-2)
(* m1-3-2 m2-2-2)
(* m1-3-3 m2-3-2)))
(matrix-set! m3 3 3
(+ (* m1-3-0 m2-0-3)
(* m1-3-1 m2-1-3)
(* m1-3-2 m2-2-3)
(* m1-3-3 m2-3-3))))))
(define (translate! t . args)
(match args
((? transform? v)
(let ((x (vector2-x v))
(y (vector2-y v)))
(make-transform 1 0 0 0
0 1 0 0
0 0 1 0
x y 0 1)))
((? vector3? v)
(let ((x (vector3-x v))
(y (vector3-y v))
(z (vector3-z v)))
(make-transform 1 0 0 0
0 1 0 0
0 0 1 0
x y z 1)))))
(define translate
(match-lambda
((? vector2? v)
(let ((x (vector2-x v))
(y (vector2-y v)))
(make-transform 1 0 0 0
0 1 0 0
0 0 1 0
x y 0 1)))
((? vector3? v)
(let ((x (vector3-x v))
(y (vector3-y v))
(z (vector3-z v)))
(make-transform 1 0 0 0
0 1 0 0
0 0 1 0
x y z 1)))
(v (error "Invalid translation vector: " v))))
(define scale
(match-lambda
((? number? v)
(make-transform v 0 0 0
0 v 0 0
0 0 v 0
0 0 0 1))
((? vector2? v)
(let ((x (vector2-x v))
(y (vector2-y v)))
(make-transform x 0 0 0
0 y 0 0
0 0 1 0
0 0 0 1)))
((? vector3? v)
(let ((x (vector3-x v))
(y (vector3-y v))
(z (vector3-z v)))
(make-transform x 0 0 0
0 y 0 0
0 0 z 0
0 0 0 1)))
(v (error "Invalid scaling vector: " v))))
(define (rotate-x angle)
"Return a new transform that rotates the X axis by ANGLE radians."
(make-transform 1 0 0 0
0 (cos angle) (- (sin angle)) 0
0 (sin angle) (cos angle) 0
0 0 0 1))
(define (rotate-y angle)
"Return a new transform that rotates the Y axis by ANGLE radians."
(make-transform (cos angle) 0 (sin angle) 0
0 1 0 0
(- (sin angle)) 0 (cos angle) 0
0 0 0 1))
(define (rotate-z angle)
"Return a new transform that rotates the Z axis by ANGLE radians."
(make-transform (cos angle) (- (sin angle)) 0 0
(sin angle) (cos angle) 0 0
0 0 1 0
0 0 0 1))
(define rotate
(match-lambda
(($ <quaternion> w x y z)
(make-transform
(- 1 (* 2 (square y)) (* 2 (square z)))
(- (* 2 x y) (* 2 w z))
(+ (* 2 x z) (* 2 w y))
0
(+ (* 2 x y) (* 2 w z))
(- 1 (* 2 (square x)) (* 2 (square z)))
(- (* 2 y z) (* 2 w x))
0
(- (* 2 x z) (* 2 w y))
(+ (* 2 y z) (* 2 w x))
(- 1 (* 2 (square x)) (* 2 (square y)))
0 0 0 0 1))))
(define build-transform
(let ((%scale scale))
(lambda* (#:optional #:key (position (vector3 0 0 0))
(scale 1) (rotation null-quaternion))
(transform* (translate position)
(%scale scale)
(rotate rotation)))))
(define (orthographic-projection left right top bottom near far)
"Return a new transform that represents an orthographic projection
for the vertical clipping plane LEFT and RIGHT, the horizontal
clipping plane TOP and BOTTOM, and the depth clipping plane NEAR and
FAR."
(make-transform (/ 2 (- right left)) 0 0 0
0 (/ 2 (- top bottom)) 0 0
0 0 (/ 2 (- far near)) 0
(- (/ (+ right left) (- right left)))
(- (/ (+ top bottom) (- top bottom)))
(- (/ (+ far near) (- far near)))
1))
(define (perspective-projection field-of-vision aspect-ratio near far)
"Return a new transform that represents a perspective projection
with a FIELD-OF-VISION in degrees, the desired ASPECT-RATIO, and the
depth clipping plane NEAR and FAR."
(let ((f (cotan (/ (degrees->radians field-of-vision) 2))))
(make-transform (/ f aspect-ratio) 0 0 0
0 f 0 0
0 0 (/ (+ far near) (- near far)) -1
0 0 (/ (* 2 far near) (- near far)) 0)))
(define* (look-at eye center #:optional (up (vector3 0 1 0)))
(let* ((forward (normalize (v- center eye)))
(side (normalize (vcross forward up)))
(up (normalize (vcross side forward))))
(transform*
(make-transform (vx side) (vx up) (- (vx forward)) 0
(vy side) (vy up) (- (vy forward)) 0
(vz side) (vz up) (- (vz forward)) 0
0 0 0 1)
(translate (v- eye)))))
|