AtlatestRepositorysigil-geometry

sigil-geometry / tree / testtest-mat4.sgl

1;;; Tests for (sigil geometry mat4)
2
3(import (sigil test)
4 (sigil geometry))
5
6(define pi 3.141592653589793)
7
8(define (assert-mat4 expected actual)
9 (unless (mat4-approx=? expected actual)
10 (assert-equal (mat4->list expected) (mat4->list actual))))
12(define (assert-vec3 expected actual)
13 (unless (vec3-approx=? expected actual)
14 (assert-equal (vec3->list expected) (vec3->list actual))))
16(define (assert-float expected actual)
17 (unless (float-approx=? expected actual)
18 (assert-equal expected actual)))
20;; A matrix with no special structure, used by several tests. Its rows are
21;; (2 0 1 3) (1 1 0 2) (0 3 1 1) (1 0 2 1). Reference values from numpy
22;; (float64): det = -1, and the inverse has integer rows
23;; (-11 15 -5 8) (-3 4 -1 2) (2 -3 1 -1) (7 -9 3 -5).
24(define general
25 (list->mat4 '(2 1 0 1 0 1 3 0 1 0 1 2 3 2 1 1)))
27(test-group "mat4 - construction"
28 (test "make-mat4 and mat4-identity are the identity"
29 (assert-equal '(1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0)
30 (mat4->list (make-mat4)))
31 (assert-mat4 (make-mat4) (mat4-identity)))
33 (test "mat4 takes values in column-major order"
34 (let ((m (mat4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)))
35 ;; column 1, row 0 is the fifth value
36 (assert-float 5.0 (mat4-ref m 1 0))
37 (assert-float 4.0 (mat4-ref m 0 3))
38 (assert-float 15.0 (mat4-ref m 3 2))))
40 (test "mat4-set! addresses (column, row)"
41 (let ((m (make-mat4)))
42 (mat4-set! m 3 0 7.5)
43 (assert-float 7.5 (list-ref (mat4->list m) 12))))
45 (test "list->mat4 rejects the wrong length"
46 (assert-error (list->mat4 '(1 2 3))))
48 (test "copy is independent"
49 (let* ((a (mat4-translation (vec3 1 2 3))) (b (mat4-copy a)))
50 (mat4-identity! a)
51 (assert-float 1.0 (mat4-ref b 3 0))
52 (assert-true (eq? a (mat4-copy! a a))))))
54(test-group "mat4 - multiply and transpose"
55 (test "identity is neutral"
56 (assert-mat4 general (mat4-mul (make-mat4) general))
57 (assert-mat4 general (mat4-mul general (make-mat4))))
59 (test "known product"
60 ;; Rows of a: (1 2 3 4) (5 6 7 8) (9 10 11 12) (13 14 15 16); b = a.
61 ;; a*a rows: (90 100 110 120) (202 228 254 280) (314 356 398 440)
62 ;; (426 484 542 600).
63 (let ((a (mat4 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16)))
64 (assert-mat4 (mat4 90 202 314 426 100 228 356 484 110 254 398 542 120 280 440 600)
65 (mat4-mul a a))))
67 (test "known product of two different matrices, both orders"
68 ;; a is rows (1 2 3 4) ... (13 14 15 16); numpy (float64) products with
69 ;; `general`, column by column. a*a above cannot tell a*b from b*a.
70 (let ((a (mat4 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16)))
71 (assert-mat4 (mat4 8 24 40 56 11 27 43 59 12 28 44 60 14 42 70 98)
72 (mat4-mul a general))
73 (assert-mat4 (mat4 50 32 37 32 56 36 42 36 62 40 47 40 68 44 52 44)
74 (mat4-mul general a))))
76 (test "mul applies the right-hand matrix first"
77 (let ((tr (mat4-translation (vec3 10 0 0)))
78 (sc (mat4-scaling (vec3 2 2 2)))
79 (p (vec3 1 1 1)))
80 ;; scale, then translate
81 (assert-vec3 (vec3 12 2 2) (mat4-transform-point (mat4-mul tr sc) p))
82 ;; translate, then scale
83 (assert-vec3 (vec3 22 2 2) (mat4-transform-point (mat4-mul sc tr) p))))
85 (test "mul! with dst aliasing either input"
86 (let ((expected (mat4-mul general (mat4-rotation-x 0.3))))
87 (let ((a (mat4-copy general)))
88 (mat4-mul! a a (mat4-rotation-x 0.3))
89 (assert-mat4 expected a))
90 (let ((b (mat4-rotation-x 0.3)))
91 (mat4-mul! b general b)
92 (assert-mat4 expected b)))
93 (let ((sq (mat4-mul general general)) (a (mat4-copy general)))
94 (mat4-mul! a a a)
95 (assert-mat4 sq a)))
97 (test "transpose"
98 (assert-mat4 (mat4 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16)
99 (mat4-transpose (mat4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)))
100 (let ((a (mat4-copy general)))
101 (mat4-transpose! a a)
102 (assert-mat4 (mat4-transpose general) a))))
104(test-group "mat4 - determinant and inverse"
105 (test "determinant"
106 (assert-float 1.0 (mat4-determinant (make-mat4)))
107 (assert-float 24.0 (mat4-determinant (mat4-scaling (vec3 2 3 4))))
108 (assert-float -1.0 (mat4-determinant general)))
110 (test "inverse of a known matrix"
111 ;; numpy.linalg.inv(general), written column by column
112 (let ((inv (mat4-inverse general)))
113 (assert-mat4 (mat4 -11 -3 2 7 15 4 -3 -9 -5 -1 1 3 8 2 -1 -5) inv)
114 (assert-mat4 (make-mat4) (mat4-mul inv general))
115 (assert-mat4 (make-mat4) (mat4-mul general inv))))
117 (test "inverse of a translation is the opposite translation"
118 (assert-mat4 (mat4-translation (vec3 -1 -2 -3))
119 (mat4-inverse (mat4-translation (vec3 1 2 3)))))
121 (test "inverse of a scale"
122 (assert-mat4 (mat4-scaling (vec3 0.5 0.25 0.125))
123 (mat4-inverse (mat4-scaling (vec3 2 4 8)))))
125 (test "inverse of a rotation is its transpose"
126 (let ((r (mat4-rotation (vec3 1 2 3) 0.7)))
127 (assert-mat4 (mat4-transpose r) (mat4-inverse r))))
129 (test "singular matrix gives #f and leaves dst alone"
130 (let ((dst (mat4-translation (vec3 5 6 7)))
131 (singular (mat4-scaling (vec3 1 0 1))))
132 (assert-false (mat4-inverse! dst singular))
133 (assert-mat4 (mat4-translation (vec3 5 6 7)) dst)
134 (assert-false (mat4-inverse (make-bytevector 64 0)))))
136 (test "inverse! in place"
137 (let ((a (mat4-copy general)))
138 (assert-true (eq? a (mat4-inverse! a a)))
139 (assert-mat4 (make-mat4) (mat4-mul a general)))))
141(test-group "mat4 - builders"
142 (test "translation sits in floats 12, 13, 14"
143 (let ((m (mat4-translation (vec3 1 2 3))))
144 (assert-equal '(1.0 2.0 3.0 1.0)
145 (map (lambda (i) (bytevector-ieee-single-ref m (* 4 i))) '(12 13 14 15)))
146 (assert-vec3 (vec3 1 2 3) (mat4-get-translation m))))
148 (test "rotation-x turns y toward z"
149 (assert-vec3 (vec3 0 0 1) (mat4-transform-direction (mat4-rotation-x (/ pi 2)) (vec3 0 1 0))))
151 (test "rotation-y turns z toward x"
152 (assert-vec3 (vec3 1 0 0) (mat4-transform-direction (mat4-rotation-y (/ pi 2)) (vec3 0 0 1))))
154 (test "rotation-z turns x toward y"
155 (assert-vec3 (vec3 0 1 0) (mat4-transform-direction (mat4-rotation-z (/ pi 2)) (vec3 1 0 0))))
157 (test "axis rotation matches the single-axis builders"
158 (assert-mat4 (mat4-rotation-x 0.4) (mat4-rotation (vec3 1 0 0) 0.4))
159 (assert-mat4 (mat4-rotation-y 0.4) (mat4-rotation (vec3 0 2 0) 0.4))
160 (assert-mat4 (mat4-rotation-z -1.1) (mat4-rotation (vec3 0 0 5) -1.1)))
162 (test "a zero axis gives the identity"
163 (assert-mat4 (make-mat4) (mat4-rotation (make-vec3) 1.0)))
165 (test "from-trs is translation * rotation * scale"
166 (let ((t (vec3 1 2 3))
167 (q (quat-from-axis-angle (vec3 0 1 0) 0.9))
168 (s (vec3 2 3 4)))
169 (assert-mat4 (mat4-mul (mat4-translation t)
170 (mat4-mul (mat4-from-quat q) (mat4-scaling s)))
171 (mat4-from-trs t q s)))))
173(test-group "mat4 - transforms"
174 (test "transform-point applies translation"
175 (assert-vec3 (vec3 11 2 3)
176 (mat4-transform-point (mat4-translation (vec3 10 0 0)) (vec3 1 2 3))))
178 (test "transform-direction ignores translation"
179 (assert-vec3 (vec3 1 2 3)
180 (mat4-transform-direction (mat4-translation (vec3 10 0 0)) (vec3 1 2 3))))
182 (test "transform-point divides by w"
183 ;; w' = 2 for every point: the result is halved
184 (let ((m (mat4 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 2)))
185 (assert-vec3 (vec3 2 3 4) (mat4-transform-point m (vec3 4 6 8)))))
187 (test "transform-vec4 keeps w"
188 (let ((r (mat4-transform-vec4 (mat4-translation (vec3 1 2 3)) (vec4 1 1 1 1))))
189 (assert-equal '(2.0 3.0 4.0 1.0) (vec4->list r))))
191 (test "transform! forms may alias the input vector"
192 (let ((p (vec3 1 2 3)))
193 (mat4-transform-point! p (mat4-rotation-z (/ pi 2)) p)
194 (assert-vec3 (vec3 -2 1 3) p))))
196(test-group "mat4 - NaN"
197 (test "a NaN element is never approx=?"
198 (let ((m (make-mat4)) (nan-value (- (* 1e300 1e300) (* 1e300 1e300))))
199 (mat4-set! m 2 1 nan-value)
200 (assert-false (mat4-approx=? (make-mat4) m))
201 (assert-false (mat4-approx=? m m)))))