Commitd478406bRecorded25 Sep 2026Repositorysigil-geometry

Act on the adversarial review

Message
  • approx=? comparisons use < and reject NaN: on sigil 0.22.7, <=, >= and = answer #t for a NaN operand, so every test helper accepted NaN.
  • quat-same-rotation? compares components up to sign instead of |dot| >= 1 - eps, which passed a non-unit 60-degree rotation.
  • New tests: every Shoemake branch by value, nlerp by value, the documented error messages (not just any error), NaN rejection, a short up vector in look-at, aliased plane and std140 writes, and a small-scale triangle.
  • look-at judges "up parallel to view" relative to |up|; ray-triangle's parallel cut-off is relative to the edge and ray lengths; a fovy whose tangent underflows raises the documented error.
  • plane-from-point-normal! and mat3->std140! read every input before writing, so dst may alias them.
  • The allocation check covers every exported ! form plus the other branches (projection divide, singular inverse, degenerate look-at, each Shoemake branch, zero normalize, ray misses) and the comparisons; check-targets refuses a ! form without a check and requires the exact check count. Comparison loops are top-level recursion: a named let allocated 3-5 objects per call.
  • check-targets checks its native/bytecode detector against the harness's own procedure.
  • The facade test reads every export clause.
Changed
 harness/src/geometry-harness/alloc.sgl | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 harness/src/geometry-harness/main.sgl  |  6 ++++--
 scripts/check-targets                  | 34 +++++++++++++++++++++++++++++++---
 src/sigil/geometry/camera.sgl          | 17 ++++++++++++-----
 src/sigil/geometry/mat3.sgl            | 33 +++++++++++++++++++++------------
 src/sigil/geometry/mat4.sgl            | 18 +++++++++++-------
 src/sigil/geometry/quat.sgl            | 29 ++++++++++++++++++-----------
 src/sigil/geometry/ray.sgl             | 37 +++++++++++++++++++++++++------------
 src/sigil/geometry/vec.sgl             | 32 +++++++++++++++++++-------------
 test/test-camera.sgl                   | 37 ++++++++++++++++++++++++++++++-------
 test/test-facade.sgl                   | 10 +++++++---
 test/test-mat3.sgl                     | 12 ++++++++++++
 test/test-mat4.sgl                     |  7 +++++++
 test/test-quat.sgl                     | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 test/test-ray.sgl                      | 36 +++++++++++++++++++++++++++++++++---
 test/test-vec.sgl                      | 23 +++++++++++++++++++++++
 16 files changed, 395 insertions(+), 79 deletions(-)
Diff
harness/src/geometry-harness/alloc.sglmodified
@@ -69,6 +69,26 @@
69
(define axis (vec3 1 1 0))
70
(define scale (vec3 2 2 2))
71
+72
;; Inputs that force the other branches.
+73
(define proj (mat4-perspective 1.2 1.777 0.1 100.0)) ; w != 1: the divide
+74
(define far-point (vec3 0.3 -0.2 -7.0))
+75
(define singular4 (mat4-scaling (vec3 1 0 1)))
+76
(define singular3 (mat3 1 2 3 2 4 6 0 0 1))
+77
(define zero3 (make-vec3))
+78
(define zero2 (make-vec2))
+79
(define zero4 (make-vec4))
+80
(define zero-q (quat 0 0 0 0))
+81
(define straight-up (vec3 0 10 0)) ; look-at along up
+82
(define rx-pi (mat4-rotation-x 3.0)) ; Shoemake x branch
+83
(define ry-pi (mat4-rotation-y 3.0)) ; y branch
+84
(define rz-pi (mat4-rotation-z 3.0)) ; z branch
+85
(define n3 (mat3-from-mat4 rx-pi))
+86
(define qa-near (quat-from-axis-angle (vec3 1 2 3) 0.7001)) ; slerp's nlerp branch
+87
(define qa-neg (quat (- (quat-x qa)) (- (quat-y qa)) (- (quat-z qa)) (- (quat-w qa))))
+88
(define away (vec3 0 1 0)) ; rays pointing away
+89
(define sideways (vec3 1 0 0)) ; parallel to the plane
+90
(define p4 (vec4 5 2 -7 0))
+91
92
(define checks
93
(list
94
(cons "vec2-add!" (lambda () (vec2-add! d2 a2 b2)))
@@ -125,7 +145,79 @@
145
(cons "ray-aabb" (lambda () (ray-aabb origin down lo hi)))
146
(cons "ray-triangle" (lambda () (ray-triangle origin down t0 t1 t2)))
147
(cons "ray-triangle-hit!" (lambda () (ray-triangle-hit! d3 origin down t0 t1 t2)))
−128
(cons "plane-signed-distance" (lambda () (plane-signed-distance plane a3)))))
+148
(cons "plane-signed-distance" (lambda () (plane-signed-distance plane a3)))
+149
;; the rest of the exported ! forms
+150
(cons "mat3-copy!" (lambda () (mat3-copy! nd na)))
+151
(cons "mat3-identity!" (lambda () (mat3-identity! nd)))
+152
(cons "mat3-set!" (lambda () (mat3-set! nd 1 2 0.5)))
+153
(cons "mat3-transpose!" (lambda () (mat3-transpose! nd na)))
+154
(cons "mat4-get-translation!" (lambda () (mat4-get-translation! d3 ma)))
+155
(cons "mat4-rotation-x!" (lambda () (mat4-rotation-x! md 0.4)))
+156
(cons "mat4-rotation-z!" (lambda () (mat4-rotation-z! md 0.4)))
+157
(cons "mat4-set!" (lambda () (mat4-set! md 3 1 2.5)))
+158
(cons "plane-from-point-normal!" (lambda () (plane-from-point-normal! d4 a3 up)))
+159
(cons "plane-from-points!" (lambda () (plane-from-points! d4 t0 t1 t2)))
+160
(cons "quat-copy!" (lambda () (quat-copy! qd qa)))
+161
(cons "quat-from-mat3!" (lambda () (quat-from-mat3! qd na)))
+162
(cons "quat-identity!" (lambda () (quat-identity! qd)))
+163
(cons "quat-inverse!" (lambda () (quat-inverse! qd qa)))
+164
(cons "quat-set!" (lambda () (quat-set! qd 0.0 0.0 0.0 1.0)))
+165
(cons "vec2-copy!" (lambda () (vec2-copy! d2 a2)))
+166
(cons "vec2-lerp!" (lambda () (vec2-lerp! d2 a2 b2 0.3)))
+167
(cons "vec2-mul!" (lambda () (vec2-mul! d2 a2 b2)))
+168
(cons "vec2-negate!" (lambda () (vec2-negate! d2 a2)))
+169
(cons "vec2-scale!" (lambda () (vec2-scale! d2 a2 1.5)))
+170
(cons "vec2-set!" (lambda () (vec2-set! d2 1.0 2.0)))
+171
(cons "vec2-sub!" (lambda () (vec2-sub! d2 a2 b2)))
+172
(cons "vec3-copy!" (lambda () (vec3-copy! d3 a3)))
+173
(cons "vec3-max!" (lambda () (vec3-max! d3 a3 b3)))
+174
(cons "vec3-mul!" (lambda () (vec3-mul! d3 a3 b3)))
+175
(cons "vec3-negate!" (lambda () (vec3-negate! d3 a3)))
+176
(cons "vec3-set!" (lambda () (vec3-set! d3 1.0 2.0 3.0)))
+177
(cons "vec4-add!" (lambda () (vec4-add! d4 a4 b4)))
+178
(cons "vec4-copy!" (lambda () (vec4-copy! d4 a4)))
+179
(cons "vec4-mul!" (lambda () (vec4-mul! d4 a4 b4)))
+180
(cons "vec4-negate!" (lambda () (vec4-negate! d4 a4)))
+181
(cons "vec4-scale!" (lambda () (vec4-scale! d4 a4 2.0)))
+182
(cons "vec4-set!" (lambda () (vec4-set! d4 1.0 2.0 3.0 4.0)))
+183
(cons "vec4-sub!" (lambda () (vec4-sub! d4 a4 b4)))
+184
;; other branches of ops above
+185
(cons "mat4-transform-point! (projection, divides by w)"
+186
(lambda () (mat4-transform-point! d3 proj far-point)))
+187
(cons "mat4-inverse! (singular)" (lambda () (mat4-inverse! md singular4)))
+188
(cons "mat3-inverse! (singular)" (lambda () (mat3-inverse! nd singular3)))
+189
(cons "mat3-normal-matrix! (singular)" (lambda () (mat3-normal-matrix! nd singular4)))
+190
(cons "mat4-look-at! (up parallel to view)" (lambda () (mat4-look-at! md straight-up t0 up)))
+191
(cons "mat4-look-at! (eye = target)" (lambda () (mat4-look-at! md t0 t0 up)))
+192
(cons "quat-from-mat4! (x branch)" (lambda () (quat-from-mat4! qd rx-pi)))
+193
(cons "quat-from-mat4! (y branch)" (lambda () (quat-from-mat4! qd ry-pi)))
+194
(cons "quat-from-mat4! (z branch)" (lambda () (quat-from-mat4! qd rz-pi)))
+195
(cons "quat-from-mat3! (x branch)" (lambda () (quat-from-mat3! qd n3)))
+196
(cons "quat-slerp! (nearly equal, nlerp branch)" (lambda () (quat-slerp! qd qa qa-near 0.3)))
+197
(cons "quat-slerp! (opposite signs)" (lambda () (quat-slerp! qd qa qa-neg 0.3)))
+198
(cons "quat-normalize! (zero)" (lambda () (quat-normalize! qd zero-q)))
+199
(cons "quat-inverse! (zero)" (lambda () (quat-inverse! qd zero-q)))
+200
(cons "vec2-normalize! (zero)" (lambda () (vec2-normalize! d2 zero2)))
+201
(cons "vec3-normalize! (zero)" (lambda () (vec3-normalize! d3 zero3)))
+202
(cons "vec4-normalize! (zero)" (lambda () (vec4-normalize! d4 zero4)))
+203
(cons "mat4-rotation! (zero axis)" (lambda () (mat4-rotation! md zero3 0.4)))
+204
(cons "plane-from-point-normal! (aliased)" (lambda () (plane-from-point-normal! p4 p4 up)))
+205
(cons "ray-plane (parallel)" (lambda () (ray-plane origin sideways plane)))
+206
(cons "ray-plane (pointing away)" (lambda () (ray-plane origin away plane)))
+207
(cons "ray-aabb (miss)" (lambda () (ray-aabb origin away lo hi)))
+208
(cons "ray-aabb (inside)" (lambda () (ray-aabb t0 away lo hi)))
+209
(cons "ray-triangle (miss)" (lambda () (ray-triangle origin away t0 t1 t2)))
+210
(cons "ray-triangle-hit! (miss)" (lambda () (ray-triangle-hit! d3 origin away t0 t1 t2)))
+211
(cons "aabb-contains-point?" (lambda () (aabb-contains-point? lo hi a3)))
+212
(cons "aabb-overlap?" (lambda () (aabb-overlap? lo hi t0 hi)))
+213
(cons "vec3-approx=?" (lambda () (vec3-approx=? a3 b3)))
+214
(cons "mat4-approx=?" (lambda () (mat4-approx=? ma mb)))
+215
(cons "mat3-approx=?" (lambda () (mat3-approx=? na na)))
+216
(cons "quat-approx=?" (lambda () (quat-approx=? qa qa)))
+217
(cons "quat-same-rotation?" (lambda () (quat-same-rotation? qa qa-neg)))
+218
(cons "float-approx=?" (lambda () (float-approx=? 1.0 1.0)))
+219
(cons "vec4-approx=?" (lambda () (vec4-approx=? a4 a4)))
+220
))
221
222
;;; Run every check. Prints one line per op and returns a list:
223
;;; (checked allocating-count control-ok?).
harness/src/geometry-harness/main.sglmodified
@@ -45,9 +45,11 @@
45
(test-result-message r))))
46
(test-summary-results summary))
47
(let ((pass? (and control-ok? (= failed 0) (> total 0))))
−48
(println "HARNESS-VERDICT: ~a tests=~a failed=~a alloc-checked=~a alloc-bad=~a alloc-control=~a geometry-code=~a"
+48
(println "HARNESS-VERDICT: ~a tests=~a failed=~a alloc-checked=~a alloc-bad=~a alloc-control=~a geometry-code=~a harness-code=~a"
49
(if pass? "PASS" "FAIL") total failed alloc-checked alloc-bad
50
(if control-ok? "ok" "SETUP-FAILED")
−51
(code-kind vec3-lerp!))
+51
(code-kind vec3-lerp!)
+52
;; control for the detector: the harness's own procedure
+53
(code-kind code-kind))
54
(unless pass? (exit 1))
55
0)))))
scripts/check-targetsmodified
@@ -15,7 +15,8 @@
15
#
16
# A target passes only when its verdict is PASS, the test count equals
17
# the number of (test ...) forms in the generated suite (every test ran),
−18
# and at least MIN_ALLOC_CHECKS ! forms were measured. A target with no
+18
# and the allocation check count equals the entries in alloc.sgl (which
+19
# must cover every exported ! form). A target with no
20
# verdict line is SETUP-FAILED, which is not a pass and not a test
21
# failure: nothing was measured.
22
#
@@ -31,7 +32,6 @@ HARNESS="$ROOT/harness"
32
SUITE="$HARNESS/src/geometry-harness/suite"
33
SIGIL=${SIGIL:-sigil}
34
TARGETS=${TARGETS:-"dev release web"}
−34
MIN_ALLOC_CHECKS=50
35
LOGS=$(mktemp -d "${TMPDIR:-/tmp}/geometry-check-targets.XXXXXX")
36
37
echo "check-targets: sigil $("$SIGIL" --version 2>&1 | head -1)"
@@ -86,6 +86,25 @@ done
86
87
expected_tests=$(cat "$SUITE"/*.sgl | grep -c '(test "' || true)
88
+89
# The allocation check must measure every exported ! form, and the
+90
# harness must report exactly as many checks as alloc.sgl lists.
+91
ALLOC_SRC="$HARNESS/src/geometry-harness/alloc.sgl"
+92
expected_alloc=$(grep -c '(cons "' "$ALLOC_SRC" || true)
+93
missing=""
+94
for module in vec mat3 mat4 quat camera ray; do
+95
for name in $(awk '/^ \(export/{f=1; next} f && /^ \(begin/{exit} f' \
+96
"$ROOT/src/sigil/geometry/$module.sgl" \
+97
| sed 's/;;.*//' | tr -s ' ()' '\n\n\n' | grep -E '!$'); do
+98
grep -q -F "(cons \"$name\"" "$ALLOC_SRC" || grep -q -F "(cons \"$name (" "$ALLOC_SRC" \
+99
|| missing="$missing $name"
+100
done
+101
done
+102
if [ -n "$missing" ]; then
+103
echo "SETUP-FAILED: ! forms with no allocation check:$missing"
+104
exit 2
+105
fi
+106
echo "check-targets: allocation check covers every exported ! form ($expected_alloc checks)"
+107
108
{
109
echo ";; GENERATED by scripts/check-targets. Do not edit."
110
echo "(define-library (geometry-harness suite)"
@@ -144,6 +163,7 @@ for target in $TARGETS; do
163
tests=$(field tests); failed=$(field failed); tests=${tests:--1}; failed=${failed:--1}
164
checked=$(field alloc-checked); bad=$(field alloc-bad); checked=${checked:--1}; bad=${bad:--1}
165
code=$(echo "$verdict" | sed -n 's/.* geometry-code=\([a-z]*\).*/\1/p')
+166
hcode=$(echo "$verdict" | sed -n 's/.* harness-code=\([a-z]*\).*/\1/p')
167
168
grep -E '^(FAILED:|alloc: ALLOCATES)' "$log" | sed "s/^/ $target: /" || true
169
@@ -160,6 +180,14 @@ for target in $TARGETS; do
180
release) want="$code"
181
if [ "$code" = native ]; then gate_alloc=yes; else gate_alloc=no; fi ;;
182
esac
+183
# Control for the detector itself: the harness's own procedure is
+184
# bytecode on dev and native on the two native targets. A detector
+185
# that always answered one kind would fail here on one of them.
+186
if [ "$target" = dev ]; then hwant=bytecode; else hwant=native; fi
+187
if [ "$hcode" != "$hwant" ]; then
+188
echo "SETUP-FAILED: $target harness code read as '$hcode', expected $hwant (the detector is wrong)"
+189
status=2; summary+=("$target SETUP-FAILED harness-code=$hcode"); continue
+190
fi
191
if [ -z "$code" ] || [ "$code" != "$want" ]; then
192
echo "SETUP-FAILED: $target ran geometry code as '$code', expected $want"
193
status=2; summary+=("$target SETUP-FAILED geometry-code=$code"); continue
@@ -172,7 +200,7 @@ for target in $TARGETS; do
200
201
if [[ "$verdict" == "HARNESS-VERDICT: PASS "* ]] && [ "$run_rc" -eq 0 ] \
202
&& [ "$tests" -eq "$expected_tests" ] && [ "$failed" -eq 0 ] \
−175
&& [ "$checked" -ge "$MIN_ALLOC_CHECKS" ] && [ "$alloc_ok" = yes ]; then
+203
&& [ "$checked" -eq "$expected_alloc" ] && [ "$alloc_ok" = yes ]; then
204
summary+=("$target PASS tests=$tests/$expected_tests geometry-code=$code alloc-checked=$checked alloc-bad=$bad$note")
205
else
206
[ "$status" -eq 0 ] && status=1
src/sigil/geometry/camera.sglmodified
@@ -54,7 +54,11 @@
54
(error "mat4-perspective: aspect must be positive" aspect))
55
(unless (and (> near 0.0) (> far near))
56
(error "mat4-perspective: need 0 < near < far" near far))
−57
(let* ((f (/ 1.0 (tan (* 0.5 fovy))))
+57
(let* ((half-tan (tan (* 0.5 fovy)))
+58
(_ (unless (> half-tan 0.0)
+59
;; a fovy so small that its tangent underflows to 0
+60
(error "mat4-perspective: fovy must be between 0 and pi radians" fovy)))
+61
(f (/ 1.0 half-tan))
62
(nf (/ 1.0 (- near far))))
63
(set16! dst
64
(/ f aspect) 0.0 0.0 0.0
@@ -104,10 +108,13 @@
108
(sx (- (* fy uz) (* fz uy)))
109
(sy (- (* fz ux) (* fx uz)))
110
(sz (- (* fx uy) (* fy ux)))
−107
(slen (sqrt (+ (* sx sx) (* sy sy) (* sz sz)))))
−108
(if (< slen 1e-6)
−109
;; up is parallel to f: use the world axis least
−110
;; aligned with f instead.
+111
(slen (sqrt (+ (* sx sx) (* sy sy) (* sz sz))))
+112
(ulen (sqrt (+ (* ux ux) (* uy uy) (* uz uz)))))
+113
;; |f x up| = |up| sin(angle), so "parallel" is judged
+114
;; relative to |up|: a short but valid up is not parallel.
+115
(if (or (= slen 0.0) (< slen (* 1e-6 ulen)))
+116
;; up is parallel to f (or zero): use the world axis
+117
;; least aligned with f instead.
118
(let* ((ax (abs fx)) (ay (abs fy)) (az (abs fz))
119
(alt (cond ((and (<= ax ay) (<= ax az)) 0)
120
((<= ay az) 1)
src/sigil/geometry/mat3.sglmodified
@@ -186,20 +186,29 @@
186
187
;;; Write `m` in std140 uniform-block layout into the 48-byte `dst`:
188
;;; three columns, each 3 floats followed by one float of padding
−189
;;; (written as 0). `dst` must not alias `m`.
+189
;;; (written as 0). `dst` may be `m` itself when `m` is a 48-byte
+190
;;; buffer holding the tight mat3 in its first 36 bytes: every float
+191
;;; is read before the first write.
192
(define (mat3->std140! dst m)
−191
(f! dst 0 (f@ m 0)) (f! dst 1 (f@ m 1)) (f! dst 2 (f@ m 2)) (f! dst 3 0.0)
−192
(f! dst 4 (f@ m 3)) (f! dst 5 (f@ m 4)) (f! dst 6 (f@ m 5)) (f! dst 7 0.0)
−193
(f! dst 8 (f@ m 6)) (f! dst 9 (f@ m 7)) (f! dst 10 (f@ m 8)) (f! dst 11 0.0)
−194
dst)
+193
(let ((m0 (f@ m 0)) (m1 (f@ m 1)) (m2 (f@ m 2))
+194
(m3 (f@ m 3)) (m4 (f@ m 4)) (m5 (f@ m 5))
+195
(m6 (f@ m 6)) (m7 (f@ m 7)) (m8 (f@ m 8)))
+196
(f! dst 0 m0) (f! dst 1 m1) (f! dst 2 m2) (f! dst 3 0.0)
+197
(f! dst 4 m3) (f! dst 5 m4) (f! dst 6 m5) (f! dst 7 0.0)
+198
(f! dst 8 m6) (f! dst 9 m7) (f! dst 10 m8) (f! dst 11 0.0)
+199
dst))
200
(define (mat3->std140 m) (mat3->std140! (make-bytevector 48 0) m))
201
202
;;; True when every element differs by at most `eps` (default 1e-5).
203
(define (mat3-approx=? a b . eps)
−199
(let ((e (if (null? eps) 1e-5 (car eps))))
−200
(let loop ((i 0))
−201
(cond ((= i 9) #t)
−202
((let ((x (f@ a i)) (y (f@ b i)))
−203
(<= (abs (- x y)) (* e (max 1.0 (abs x) (abs y)))))
−204
(loop (+ i 1)))
−205
(else #f)))))))
+204
(all-close? a b (if (null? eps) 1e-5 (car eps)) 0))
+205
+206
;; Floats i..8 of a and b all within e. `<`, not `<=`, so NaN fails
+207
;; (sigil 0.22.7's `<=` answers #t for NaN). A top-level recursive
+208
;; procedure rather than a named let: a named let allocates a closure
+209
;; on every call.
+210
(define (all-close? a b e i)
+211
(or (= i 9)
+212
(and (let ((x (f@ a i)) (y (f@ b i)))
+213
(< (abs (- x y)) (* e (max 1.0 (abs x) (abs y)))))
+214
(all-close? a b e (+ i 1)))))))
src/sigil/geometry/mat4.sglmodified
@@ -320,10 +320,14 @@
320
;;; True when every element differs by at most `eps` (default 1e-5,
321
;;; relative above magnitude 1).
322
(define (mat4-approx=? a b . eps)
−323
(let ((e (if (null? eps) 1e-5 (car eps))))
−324
(let loop ((i 0))
−325
(cond ((= i 16) #t)
−326
((let ((x (f@ a i)) (y (f@ b i)))
−327
(<= (abs (- x y)) (* e (max 1.0 (abs x) (abs y)))))
−328
(loop (+ i 1)))
−329
(else #f)))))))
+323
(all-close? a b (if (null? eps) 1e-5 (car eps)) 0))
+324
+325
;; Floats i..15 of a and b all within e. `<`, not `<=`, so NaN fails
+326
;; (sigil 0.22.7's `<=` answers #t for NaN). A top-level recursive
+327
;; procedure rather than a named let: a named let allocates a closure
+328
;; on every call.
+329
(define (all-close? a b e i)
+330
(or (= i 16)
+331
(and (let ((x (f@ a i)) (y (f@ b i)))
+332
(< (abs (- x y)) (* e (max 1.0 (abs x) (abs y)))))
+333
(all-close? a b e (+ i 1)))))))
src/sigil/geometry/quat.sglmodified
@@ -221,16 +221,23 @@
221
;;; `q` and `-q` are the same rotation but not approx=?; use
222
;;; `quat-same-rotation?` for that.
223
(define (quat-approx=? a b . eps)
−224
(let ((e (if (null? eps) 1e-5 (car eps))))
−225
(let loop ((i 0))
−226
(cond ((= i 4) #t)
−227
((let ((x (f@ a i)) (y (f@ b i)))
−228
(<= (abs (- x y)) (* e (max 1.0 (abs x) (abs y)))))
−229
(loop (+ i 1)))
−230
(else #f)))))
−231
−232
;;; True when unit quaternions `a` and `b` describe the same rotation
−233
;;; within `eps` (default 1e-5), treating `q` and `-q` as equal.
+224
(same-up-to-sign? a b (if (null? eps) 1e-5 (car eps)) 1.0 0))
+225
+226
;;; True when `a` and `b` are the same quaternion up to sign: every
+227
;;; component of `a` is within `eps` (default 1e-5) of `b`'s, or of
+228
;;; `-b`'s. For unit quaternions that means the same rotation, since
+229
;;; `q` and `-q` rotate identically. A non-unit result is not the same
+230
;;; rotation as a unit one, and a NaN is never the same as anything.
231
(define (quat-same-rotation? a b . eps)
232
(let ((e (if (null? eps) 1e-5 (car eps))))
−236
(>= (abs (quat-dot a b)) (- 1.0 e))))))
+233
(or (same-up-to-sign? a b e 1.0 0)
+234
(same-up-to-sign? a b e -1.0 0))))
+235
+236
;; Components i..3 of a within e of sign * b's. `<`, not `<=`, so NaN
+237
;; fails (sigil 0.22.7's `<=` answers #t for NaN). A top-level
+238
;; recursive procedure: a named let allocates a closure per call.
+239
(define (same-up-to-sign? a b e sign i)
+240
(or (= i 4)
+241
(and (let ((x (f@ a i)) (y (* sign (f@ b i))))
+242
(< (abs (- x y)) (* e (max 1.0 (abs x) (abs y)))))
+243
(same-up-to-sign? a b e sign (+ i 1)))))))
src/sigil/geometry/ray.sglmodified
@@ -49,13 +49,15 @@
49
;;; The plane through the vec3 `point` with the vec3 `normal`, as a
50
;;; vec4 with a unit normal. A zero normal raises an error.
51
(define (plane-from-point-normal! dst point normal)
−52
(let* ((nx (f@ normal 0)) (ny (f@ normal 1)) (nz (f@ normal 2))
+52
(let* ((px (f@ point 0)) (py (f@ point 1)) (pz (f@ point 2))
+53
(nx (f@ normal 0)) (ny (f@ normal 1)) (nz (f@ normal 2))
54
(len (sqrt (+ (* nx nx) (* ny ny) (* nz nz)))))
55
(when (= len 0.0)
56
(error "plane-from-point-normal: zero normal"))
+57
;; every input is read above, before the first write
58
(let ((nx (/ nx len)) (ny (/ ny len)) (nz (/ nz len)))
59
(f! dst 0 nx) (f! dst 1 ny) (f! dst 2 nz)
−58
(f! dst 3 (- (+ (* nx (f@ point 0)) (* ny (f@ point 1)) (* nz (f@ point 2)))))
+60
(f! dst 3 (- (+ (* nx px) (* ny py) (* nz pz))))
61
dst)))
62
(define (plane-from-point-normal point normal)
63
(plane-from-point-normal! (make-bytevector 16 0) point normal))
@@ -97,7 +99,7 @@
99
(if (= denom 0.0)
100
#f
101
(let ((t (/ (- (plane-signed-distance plane origin)) denom)))
−100
(if (>= t 0.0) t #f)))))
+102
(if (or (< t 0.0) (nan? t)) #f t)))))
103
104
;; Larger than any t a float32 ray can produce.
105
(define huge 1e300)
@@ -151,8 +153,14 @@
153
(px (- (* dy e2z) (* dz e2y)))
154
(py (- (* dz e2x) (* dx e2z)))
155
(pz (- (* dx e2y) (* dy e2x)))
−154
(det (+ (* e1x px) (* e1y py) (* e1z pz))))
−155
(if (< (abs det) 1e-12)
+156
(det (+ (* e1x px) (* e1y py) (* e1z pz)))
+157
;; det = e1 . (dir x e2) scales with |e1| |e2| |dir|, so the
+158
;; "parallel to the triangle" cut-off is relative to that
+159
;; product rather than an absolute number.
+160
(scale (sqrt (* (+ (* e1x e1x) (* e1y e1y) (* e1z e1z))
+161
(+ (* e2x e2x) (* e2y e2y) (* e2z e2z))
+162
(+ (* dx dx) (* dy dy) (* dz dz))))))
+163
(if (or (= det 0.0) (< (abs det) (* 1e-9 scale)))
164
#f
165
(let* ((inv (/ 1.0 det))
166
(sx (- (f@ origin 0) ax))
@@ -185,14 +193,19 @@
193
(define (ray-triangle-hit! dst origin dir a b c)
194
(triangle-cast origin dir a b c dst))
195
−188
;;; True when the vec3 `p` lies inside or on the box.
+196
;; x <= y that answers #f when either side is NaN. Sigil 0.22.7's own
+197
;; `<=` answers #t for a NaN operand.
+198
(define (le? x y) (not (or (> x y) (nan? x) (nan? y))))
+199
+200
;;; True when the vec3 `p` lies inside or on the box. A NaN
+201
;;; coordinate is never inside.
202
(define (aabb-contains-point? box-min box-max p)
−190
(and (<= (f@ box-min 0) (f@ p 0) (f@ box-max 0))
−191
(<= (f@ box-min 1) (f@ p 1) (f@ box-max 1))
−192
(<= (f@ box-min 2) (f@ p 2) (f@ box-max 2))))
+203
(and (le? (f@ box-min 0) (f@ p 0)) (le? (f@ p 0) (f@ box-max 0))
+204
(le? (f@ box-min 1) (f@ p 1)) (le? (f@ p 1) (f@ box-max 1))
+205
(le? (f@ box-min 2) (f@ p 2)) (le? (f@ p 2) (f@ box-max 2))))
206
207
;;; True when two boxes overlap or touch.
208
(define (aabb-overlap? a-min a-max b-min b-max)
−196
(and (<= (f@ a-min 0) (f@ b-max 0)) (<= (f@ b-min 0) (f@ a-max 0))
−197
(<= (f@ a-min 1) (f@ b-max 1)) (<= (f@ b-min 1) (f@ a-max 1))
−198
(<= (f@ a-min 2) (f@ b-max 2)) (<= (f@ b-min 2) (f@ a-max 2))))))
+209
(and (le? (f@ a-min 0) (f@ b-max 0)) (le? (f@ b-min 0) (f@ a-max 0))
+210
(le? (f@ a-min 1) (f@ b-max 1)) (le? (f@ b-min 1) (f@ a-max 1))
+211
(le? (f@ a-min 2) (f@ b-max 2)) (le? (f@ b-min 2) (f@ a-max 2))))))
src/sigil/geometry/vec.sglmodified
@@ -59,15 +59,21 @@
59
;;; relative to the larger magnitude once that exceeds 1.
60
(define default-epsilon 1e-5)
61
−62
;;; True when `a` and `b` differ by at most `eps` (default 1e-5),
−63
;;; scaled by the larger magnitude when that is above 1.
+62
;;; True when `a` and `b` differ by less than `eps` (default 1e-5,
+63
;;; must be positive), scaled by the larger magnitude when that is
+64
;;; above 1. A NaN on either side is never approx=?.
65
;;;
66
;;; ```scheme
67
;;; (float-approx=? 1.0 1.000001) ; => #t
68
;;; ```
69
(define (float-approx=? a b . eps)
−69
(let ((e (if (null? eps) default-epsilon (car eps))))
−70
(<= (abs (- a b)) (* e (max 1.0 (abs a) (abs b))))))
+70
(close? a b (if (null? eps) default-epsilon (car eps))))
+71
+72
;; Written with `<` only: on sigil 0.22.7, `<=`, `>=` and `=` answer
+73
;; #t when an operand is NaN (and `max` drops a NaN), while `<` and
+74
;; `>` answer #f. A NaN difference therefore fails this test.
+75
(define (close? a b e)
+76
(< (abs (- a b)) (* e (max 1.0 (abs a) (abs b)))))
77
78
(define (eps-of rest) (if (null? rest) default-epsilon (car rest)))
79
@@ -147,8 +153,8 @@
153
154
(define (vec2-approx=? a b . eps)
155
(let ((e (eps-of eps)))
−150
(and (float-approx=? (f@ a 0) (f@ b 0) e)
−151
(float-approx=? (f@ a 1) (f@ b 1) e))))
+156
(and (close? (f@ a 0) (f@ b 0) e)
+157
(close? (f@ a 1) (f@ b 1) e))))
158
159
(define (vec2->list v) (list (f@ v 0) (f@ v 1)))
160
@@ -259,9 +265,9 @@
265
266
(define (vec3-approx=? a b . eps)
267
(let ((e (eps-of eps)))
−262
(and (float-approx=? (f@ a 0) (f@ b 0) e)
−263
(float-approx=? (f@ a 1) (f@ b 1) e)
−264
(float-approx=? (f@ a 2) (f@ b 2) e))))
+268
(and (close? (f@ a 0) (f@ b 0) e)
+269
(close? (f@ a 1) (f@ b 1) e)
+270
(close? (f@ a 2) (f@ b 2) e))))
271
272
(define (vec3->list v) (list (f@ v 0) (f@ v 1) (f@ v 2)))
273
@@ -346,9 +352,9 @@
352
353
(define (vec4-approx=? a b . eps)
354
(let ((e (eps-of eps)))
−349
(and (float-approx=? (f@ a 0) (f@ b 0) e)
−350
(float-approx=? (f@ a 1) (f@ b 1) e)
−351
(float-approx=? (f@ a 2) (f@ b 2) e)
−352
(float-approx=? (f@ a 3) (f@ b 3) e))))
+355
(and (close? (f@ a 0) (f@ b 0) e)
+356
(close? (f@ a 1) (f@ b 1) e)
+357
(close? (f@ a 2) (f@ b 2) e)
+358
(close? (f@ a 3) (f@ b 3) e))))
359
360
(define (vec4->list v) (list (f@ v 0) (f@ v 1) (f@ v 2) (f@ v 3)))))
test/test-camera.sglmodified
@@ -15,6 +15,16 @@
15
(unless (vec3-approx=? expected actual)
16
(assert-equal (vec3->list expected) (vec3->list actual))))
17
+18
;; The message of the error `thunk` raises, or #f when it returns.
+19
(define (error-message thunk)
+20
(guard (e (#t (error-object-message e)))
+21
(thunk)
+22
#f))
+23
+24
(define fovy-msg "mat4-perspective: fovy must be between 0 and pi radians")
+25
(define aspect-msg "mat4-perspective: aspect must be positive")
+26
(define near-far-msg "mat4-perspective: need 0 < near < far")
+27
28
(test-group "perspective"
29
(test "60 degrees, 16:9, near 0.1, far 100"
30
(assert-mat4 (mat4 0.974278579 0 0 0
@@ -38,12 +48,16 @@
48
(assert-vec3 (vec3 0 1 (vec3-z (mat4-transform-point p (vec3 0 1 -1))))
49
(mat4-transform-point p (vec3 0 1 -1)))))
50
−41
(test "bad arguments raise"
−42
(assert-error (mat4-perspective 0 1 0.1 10))
−43
(assert-error (mat4-perspective 1 0 0.1 10))
−44
(assert-error (mat4-perspective 1 1 0 10))
−45
(assert-error (mat4-perspective 1 1 5 5))
−46
(assert-error (mat4-perspective 4 1 0.1 10)))
+51
(test "bad arguments raise the documented error"
+52
;; the message, not just any error: an incidental division by zero
+53
;; would also raise
+54
(assert-equal fovy-msg (error-message (lambda () (mat4-perspective 0 1 0.1 10))))
+55
(assert-equal fovy-msg (error-message (lambda () (mat4-perspective 4 1 0.1 10))))
+56
(assert-equal fovy-msg (error-message (lambda () (mat4-perspective 5e-324 1 0.1 10))))
+57
(assert-equal aspect-msg (error-message (lambda () (mat4-perspective 1 0 0.1 10))))
+58
(assert-equal aspect-msg (error-message (lambda () (mat4-perspective 1 -1.5 0.1 10))))
+59
(assert-equal near-far-msg (error-message (lambda () (mat4-perspective 1 1 0 10))))
+60
(assert-equal near-far-msg (error-message (lambda () (mat4-perspective 1 1 5 5)))))
61
62
(test "! form writes into dst"
63
(let ((dst (make-mat4)))
@@ -63,7 +77,10 @@
77
(mat4-orthographic -1 1 -1 1 -1 1)))
78
79
(test "coincident planes raise"
−66
(assert-error (mat4-orthographic 1 1 0 1 0 1))))
+80
(assert-equal "mat4-orthographic: opposite planes must differ"
+81
(error-message (lambda () (mat4-orthographic 1 1 0 1 0 1))))
+82
(assert-equal "mat4-orthographic: opposite planes must differ"
+83
(error-message (lambda () (mat4-orthographic 0 1 2 2 0 1))))))
84
85
(test-group "look-at"
86
(test "gl-matrix's reference case"
@@ -107,3 +124,9 @@
124
(assert-true (float-approx=? 0.0 (vec3-x ndc)))
125
(assert-true (float-approx=? 0.0 (vec3-y ndc)))
126
(assert-true (< -1.0 (vec3-z ndc) 1.0)))))
+127
+128
(test-group "look-at with a short up vector"
+129
(test "a short up is still an up, not a parallel one"
+130
;; |f x up| is 1e-7 here; judged against |up| it is perpendicular
+131
(assert-mat4 (mat4-translation (vec3 0 0 -5))
+132
(mat4-look-at (vec3 0 0 5) (vec3 0 0 0) (vec3 0 1e-7 0)))))
test/test-facade.sglmodified
@@ -16,10 +16,14 @@
16
"src/sigil/geometry/camera.sgl"
17
"src/sigil/geometry/ray.sgl"))
18
+19
;; Every name in every (export ...) clause of the library: a library may
+20
;; have more than one.
21
(define (exports-of path)
−20
(let* ((lib (read (open-input-string (read-file-string path))))
−21
(clause (assq 'export (cddr lib))))
−22
(if clause (cdr clause) '())))
+22
(let ((lib (read (open-input-string (read-file-string path)))))
+23
(apply append
+24
(map cdr (filter (lambda (clause)
+25
(and (pair? clause) (eq? (car clause) 'export)))
+26
(cddr lib))))))
27
28
(define (missing from in)
29
(filter (lambda (name) (not (memq name in))) from))
test/test-mat3.sglmodified
@@ -83,3 +83,15 @@
83
(if (< i 0)
84
acc
85
(loop (- i 1) (cons (bytevector-ieee-single-ref p (* 4 i)) acc))))))))
+86
+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))))
+92
+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))))
test/test-mat4.sglmodified
@@ -192,3 +192,10 @@
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))))
+195
+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)))))
test/test-quat.sglmodified
@@ -139,3 +139,52 @@
139
(test "nlerp stays unit length"
140
(assert-true (float-approx=? 1.0 (quat-length (quat-nlerp rot-0 rot-90 0.3))))))
141
+142
+143
(test-group "quat - comparison is strict"
+144
(test "same-rotation? rejects a different rotation of different length"
+145
;; (0.7 0 0 1.2) is not unit length and is a 60-degree turn about x
+146
(assert-false (quat-same-rotation? (quat 0 0 0 1) (quat 0.7 0 0 1.2))))
+147
+148
(test "same-rotation? rejects a 0.5-degree difference"
+149
(assert-false (quat-same-rotation? (quat-from-axis-angle z-axis 0.0)
+150
(quat-from-axis-angle z-axis (degrees->radians 0.5)))))
+151
+152
(test "same-rotation? accepts q against -q"
+153
(let ((q (quat-from-axis-angle (vec3 1 2 3) 0.8)))
+154
(assert-true (quat-same-rotation? q (quat-scale-neg q)))))
+155
+156
(test "NaN is never the same rotation"
+157
(let ((n (- (* 1e300 1e300) (* 1e300 1e300))))
+158
(assert-false (quat-same-rotation? (make-quat) (quat 0 0 0 n)))
+159
(assert-false (quat-approx=? (make-quat) (quat n 0 0 1))))))
+160
+161
(test-group "quat - each matrix conversion branch, by value"
+162
;; The expected quaternions follow from the axis-angle definition:
+163
;; pi about x is (1 0 0 0), and so on. Up to sign.
+164
(test "x dominant: pi about x"
+165
(assert-same-rotation (quat 1 0 0 0) (quat-from-mat4 (mat4-rotation-x pi))))
+166
+167
(test "y dominant: pi about y"
+168
(assert-same-rotation (quat 0 1 0 0) (quat-from-mat4 (mat4-rotation-y pi))))
+169
+170
(test "z dominant: pi about z"
+171
(assert-same-rotation (quat 0 0 1 0) (quat-from-mat4 (mat4-rotation-z pi))))
+172
+173
(test "x dominant, not a half turn: 3.0 rad about x"
+174
(assert-same-rotation (quat-from-axis-angle (vec3 1 0 0) 3.0)
+175
(quat-from-mat3 (mat3-from-mat4 (mat4-rotation-x 3.0))))))
+176
+177
(test-group "quat - nlerp by value"
+178
;; numpy (float64): normalize((1 - t) a + t b) with a the identity and
+179
;; b 90 degrees about z.
+180
(test "nlerp at 0.25, 0.5 and 0.75"
+181
(assert-quat (quat 0 0 0.18736555 0.982290258) (quat-nlerp rot-0 rot-90 0.25))
+182
(assert-quat (quat 0 0 0.382683432 0.923879533) (quat-nlerp rot-0 rot-90 0.5))
+183
(assert-quat (quat 0 0 0.562096651 0.827071554) (quat-nlerp rot-0 rot-90 0.75)))
+184
+185
(test "nlerp differs from slerp away from the midpoint"
+186
(assert-false (quat-approx=? (quat-nlerp rot-0 rot-90 0.25) (quat-slerp rot-0 rot-90 0.25))))
+187
+188
(test "nlerp takes the shorter arc"
+189
(assert-same-rotation (quat-nlerp rot-0 rot-90 0.25)
+190
(quat-nlerp rot-0 (quat-scale-neg rot-90) 0.25))))
test/test-ray.sglmodified
@@ -13,6 +13,12 @@
13
14
(define ground (vec4 0 1 0 0))
15
+16
;; The message of the error `thunk` raises, or #f when it returns.
+17
(define (error-message thunk)
+18
(guard (e (#t (error-object-message e)))
+19
(thunk)
+20
#f))
+21
22
(define box-lo (vec3 -1 -1 -1))
23
(define box-hi (vec3 1 1 1))
24
@@ -28,9 +34,18 @@
34
(assert-vec4 (vec4 0 0 1 0) (plane-from-points tri-a tri-b tri-c))
35
(assert-vec4 (vec4 0 0 -1 0) (plane-from-points tri-a tri-c tri-b)))
36
−31
(test "degenerate input raises"
−32
(assert-error (plane-from-point-normal (vec3 0 0 0) (vec3 0 0 0)))
−33
(assert-error (plane-from-points (vec3 0 0 0) (vec3 1 1 1) (vec3 2 2 2))))
+37
(test "degenerate input raises the documented error"
+38
(assert-equal "plane-from-point-normal: zero normal"
+39
(error-message (lambda () (plane-from-point-normal (vec3 0 0 0) (vec3 0 0 0)))))
+40
(assert-equal "plane-from-points: the points are collinear"
+41
(error-message
+42
(lambda () (plane-from-points (vec3 0 0 0) (vec3 1 1 1) (vec3 2 2 2))))))
+43
+44
(test "from-point-normal! with dst aliasing the point"
+45
;; a 16-byte point: its first three floats are read as the vec3
+46
(let ((p (vec4 5 2 -7 0)))
+47
(plane-from-point-normal! p p (vec3 0 3 0))
+48
(assert-vec4 (vec4 0 1 0 -2) p)))
49
50
(test "signed distance"
51
(let ((ground (plane-from-point-normal (vec3 0 2 0) (vec3 0 1 0))))
@@ -140,3 +155,18 @@
155
(test-group "ray-point"
156
(test "origin + dir * t"
157
(assert-true (vec3-approx=? (vec3 3 5 7) (ray-point (vec3 1 1 1) (vec3 1 2 3) 2)))))
+158
+159
(test-group "ray-triangle at small scales"
+160
(test "a tiny triangle hit by a short ray"
+161
;; edges and dir of length 1e-4; det is about 1e-12, which an
+162
;; absolute cut-off would call parallel
+163
(let ((a (vec3 0 0 0)) (b (vec3 1e-4 0 0)) (c (vec3 0 1e-4 0)))
+164
(assert-float 0.5 (ray-triangle (vec3 2e-5 2e-5 5e-5) (vec3 0 0 -1e-4) a b c))))
+165
+166
(test "a ray in the triangle's plane still misses"
+167
(assert-false (ray-triangle (vec3 -1 0.25 0) (vec3 1 0 0) tri-a tri-b tri-c))))
+168
+169
(test-group "NaN and boxes"
+170
(test "a NaN point is never inside a box"
+171
(assert-false (aabb-contains-point? box-lo box-hi
+172
(vec3 (- (* 1e300 1e300) (* 1e300 1e300)) 0 0)))))
test/test-vec.sglmodified
@@ -134,3 +134,26 @@
134
(test "degrees and radians"
135
(assert-float 3.141592653589793 (degrees->radians 180))
136
(assert-float 90.0 (radians->degrees (/ 3.141592653589793 2)))))
+137
+138
;; NaN built at run time: inf - inf. Sigil 0.22.7 answers #t to (<= nan x),
+139
;; (>= nan x) and (= nan x), so every comparison here must be built on `<`.
+140
(define nan-value (- (* 1e300 1e300) (* 1e300 1e300)))
+141
+142
(test-group "NaN is never approx=?"
+143
(test "the constructed value is NaN (< and > both refuse it)"
+144
;; guards the premise of the tests below
+145
;; no real number is both >= 2 and <= 1
+146
(assert-false (< nan-value 2.0))
+147
(assert-false (> nan-value 1.0)))
+148
+149
(test "scalars"
+150
(assert-false (float-approx=? 1.0 nan-value))
+151
(assert-false (float-approx=? nan-value 1.0))
+152
(assert-false (float-approx=? nan-value nan-value)))
+153
+154
(test "vectors"
+155
(assert-false (vec2-approx=? (vec2 1 2) (vec2 nan-value 2)))
+156
(assert-false (vec3-approx=? (vec3 1 2 3) (vec3 1 2 nan-value)))
+157
(assert-false (vec3-approx=? (vec3 nan-value nan-value nan-value)
+158
(vec3 nan-value nan-value nan-value)))
+159
(assert-false (vec4-approx=? (vec4 1 2 3 4) (vec4 1 nan-value 3 4)))))