AtlatestRepositorysigil-geometry

sigil-geometry / tree / testtest-mat3.sgl

1;;; Tests for (sigil geometry mat3)
2
3(import (sigil test)
4 (sigil geometry))
5
6(define (assert-mat3 expected actual)
7 (unless (mat3-approx=? expected actual)
8 (assert-equal (mat3->list expected) (mat3->list actual))))
9
10(define (assert-vec3 expected actual)
11 (unless (vec3-approx=? expected actual)
12 (assert-equal (vec3->list expected) (vec3->list actual))))
14;; Rows (2 0 1) (1 3 2) (1 1 2). numpy (float64): det = 6, and the
15;; inverse is 1/6 times rows (4 1 -3) (0 3 -3) (-2 -2 6).
16(define fixture3 (list->mat3 '(2 1 1 0 3 1 1 2 2)))
18(test-group "mat3"
19 (test "identity and layout"
20 (assert-equal '(1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0) (mat3->list (make-mat3)))
21 (assert-equal 36 (bytevector-length (make-mat3)))
22 (assert-true (= 5.0 (mat3-ref (mat3 1 2 3 4 5 6 7 8 9) 1 1)))
23 (assert-true (= 7.0 (mat3-ref (mat3 1 2 3 4 5 6 7 8 9) 2 0))))
25 (test "multiply"
26 ;; rows of a: (1 2 3) (4 5 6) (7 8 9); a*a rows: (30 36 42) (66 81 96)
27 ;; (102 126 150)
28 (let ((a (mat3 1 4 7 2 5 8 3 6 9)))
29 (assert-mat3 (mat3 30 66 102 36 81 126 42 96 150) (mat3-mul a a))
30 (mat3-mul! a a a)
31 (assert-mat3 (mat3 30 66 102 36 81 126 42 96 150) a)))
33 (test "transpose"
34 (assert-mat3 (mat3 1 4 7 2 5 8 3 6 9) (mat3-transpose (mat3 1 2 3 4 5 6 7 8 9))))
36 (test "determinant and inverse"
37 (assert-true (float-approx=? 6.0 (mat3-determinant fixture3)))
38 (assert-mat3 (mat3 0.666666667 0 -0.333333333
39 0.166666667 0.5 -0.333333333
40 -0.5 -0.5 1)
41 (mat3-inverse fixture3))
42 (assert-mat3 (make-mat3) (mat3-mul (mat3-inverse fixture3) fixture3))
43 (assert-mat3 (make-mat3) (mat3-mul fixture3 (mat3-inverse fixture3))))
45 (test "singular gives #f and leaves dst alone"
46 (let ((dst (mat3 9 9 9 9 9 9 9 9 9)))
47 (assert-false (mat3-inverse! dst (mat3 1 2 3 2 4 6 0 0 1)))
48 (assert-mat3 (mat3 9 9 9 9 9 9 9 9 9) dst)))
50 (test "from-mat4 takes the upper-left 3x3"
51 (let ((r (mat4-rotation (vec3 1 1 0) 0.5)))
52 (assert-mat3 (mat3 (mat4-ref r 0 0) (mat4-ref r 0 1) (mat4-ref r 0 2)
53 (mat4-ref r 1 0) (mat4-ref r 1 1) (mat4-ref r 1 2)
54 (mat4-ref r 2 0) (mat4-ref r 2 1) (mat4-ref r 2 2))
55 (mat3-from-mat4 (mat4-mul (mat4-translation (vec3 4 5 6)) r)))))
57 (test "normal matrix of a rotation is the rotation"
58 (let ((r (mat4-rotation (vec3 0 1 1) 1.2)))
59 (assert-mat3 (mat3-from-mat4 r) (mat3-normal-matrix r))))
61 (test "normal matrix keeps normals perpendicular under non-uniform scale"
62 ;; A surface along the line x = y in the xy plane has normal (1 -1 0)/..
63 ;; After scaling x by 4 its tangent (1 1 0) becomes (4 1 0); the
64 ;; transformed normal must stay perpendicular to it.
65 (let* ((model (mat4-scaling (vec3 4 1 1)))
66 (n (mat3-transform-vec3 (mat3-normal-matrix model) (vec3 1 -1 0)))
67 (tangent (mat4-transform-direction model (vec3 1 1 0))))
68 (assert-true (float-approx=? 0.0 (vec3-dot n tangent)))))
70 (test "from-quat matches mat4-from-quat"
71 (let ((q (quat-from-axis-angle (vec3 1 2 3) 0.8)))
72 (assert-mat3 (mat3-from-mat4 (mat4-from-quat q)) (mat3-from-quat q))))
74 (test "transform-vec3"
75 (assert-vec3 (vec3 14 32 50)
76 (mat3-transform-vec3 (mat3 1 4 7 2 5 8 3 6 9) (vec3 1 2 3))))
78 (test "std140 pads each column to four floats"
79 (let ((p (mat3->std140 (mat3 1 2 3 4 5 6 7 8 9))))
80 (assert-equal 48 (bytevector-length p))
81 (assert-equal '(1.0 2.0 3.0 0.0 4.0 5.0 6.0 0.0 7.0 8.0 9.0 0.0)
82 (let loop ((i 11) (acc '()))
83 (if (< i 0)
84 acc
85 (loop (- i 1) (cons (bytevector-ieee-single-ref p (* 4 i)) acc))))))))
87(test-group "mat3 - NaN and std140 in place"
88 (test "a NaN element is never approx=?"
89 (let ((m (make-mat3)))
90 (mat3-set! m 0 0 (- (* 1e300 1e300) (* 1e300 1e300)))
91 (assert-false (mat3-approx=? (make-mat3) m))))
93 (test "std140 in place over a 48-byte buffer"
94 (let ((buf (make-bytevector 48 0)))
95 (bytevector-copy! buf 0 (mat3 1 2 3 4 5 6 7 8 9) 0 36)
96 (mat3->std140! buf buf)
97 (assert-equal (mat3->std140 (mat3 1 2 3 4 5 6 7 8 9)) buf))))