AtlatestRepositorysigil-geometry
sigil-geometry / tree / testtest-ray.sgl
1
;;; Tests for (sigil geometry ray)3
(import (sigil test)4
(sigil geometry))6
(define (assert-float expected actual)7
(unless (and actual (float-approx=? expected actual))8
(assert-equal expected actual)))10
(define (assert-vec4 expected actual)11
(unless (vec4-approx=? expected actual)12
(assert-equal (vec4->list expected) (vec4->list actual))))14
(define ground (vec4 0 1 0 0))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))22
(define box-lo (vec3 -1 -1 -1))23
(define box-hi (vec3 1 1 1))25
(define tri-a (vec3 0 0 0))26
(define tri-b (vec3 1 0 0))27
(define tri-c (vec3 0 1 0))29
(test-group "planes"30
(test "from a point and a normal"31
(assert-vec4 (vec4 0 1 0 -2) (plane-from-point-normal (vec3 5 2 -7) (vec3 0 3 0))))33
(test "from three counterclockwise points faces +z"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)))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-message42
(lambda () (plane-from-points (vec3 0 0 0) (vec3 1 1 1) (vec3 2 2 2))))))44
(test "from-point-normal! with dst aliasing the point"45
;; a 16-byte point: its first three floats are read as the vec346
(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)))50
(test "signed distance"51
(let ((ground (plane-from-point-normal (vec3 0 2 0) (vec3 0 1 0))))52
(assert-float 3.0 (plane-signed-distance ground (vec3 9 5 9)))53
(assert-float -2.0 (plane-signed-distance ground (vec3 0 0 0))))))55
(test-group "ray-plane"56
(test "straight down onto the ground"57
(assert-float 5.0 (ray-plane (vec3 0 5 0) (vec3 0 -1 0) ground)))59
(test "t is in units of dir"60
(assert-float 2.5 (ray-plane (vec3 0 5 0) (vec3 0 -2 0) ground)))62
(test "an oblique ray"63
(let* ((o (vec3 0 4 0)) (d (vec3 3 -4 0)) (t (ray-plane o d ground)))64
(assert-float 1.0 t)65
(assert-float 3.0 (vec3-x (ray-point o d t)))))67
(test "from below also hits"68
(assert-float 5.0 (ray-plane (vec3 0 -5 0) (vec3 0 1 0) ground)))70
(test "parallel and pointing away miss"71
(assert-false (ray-plane (vec3 0 5 0) (vec3 1 0 0) ground))72
(assert-false (ray-plane (vec3 0 5 0) (vec3 0 1 0) ground))))74
(test-group "ray-aabb"75
(test "head-on"76
(assert-float 4.0 (ray-aabb (vec3 -5 0 0) (vec3 1 0 0) box-lo box-hi)))78
(test "diagonal"79
(assert-float 2.0 (ray-aabb (vec3 -3 -3 -3) (vec3 1 1 1) box-lo box-hi)))81
(test "from inside is 0"82
(assert-float 0.0 (ray-aabb (vec3 0.5 0 0) (vec3 0 0 1) box-lo box-hi)))84
(test "zero dir components inside their slab"85
(assert-float 4.0 (ray-aabb (vec3 -5 0.5 0.5) (vec3 1 0 0) box-lo box-hi)))87
(test "zero dir component outside its slab misses"88
(assert-false (ray-aabb (vec3 -5 2 0) (vec3 1 0 0) box-lo box-hi)))90
(test "passing beside the box misses"91
(assert-false (ray-aabb (vec3 -5 0 0) (vec3 1 1 0) box-lo box-hi)))93
(test "box behind the ray misses"94
(assert-false (ray-aabb (vec3 5 0 0) (vec3 1 0 0) box-lo box-hi)))96
(test "skimming a face counts"97
(assert-float 4.0 (ray-aabb (vec3 -5 1 0) (vec3 1 0 0) box-lo box-hi)))99
(test "sight line: t below 1 means the box is in the way"100
(let* ((eye (vec3 -5 0 0))101
(target (vec3 5 0 0))102
(dir (vec3-sub target eye)))103
(assert-float 0.4 (ray-aabb eye dir box-lo box-hi))))105
(test "sight line: a target in front of the box is not blocked"106
(let* ((eye (vec3 -5 0 0))107
(dir (vec3-sub (vec3 -3 0 0) eye))108
(t (ray-aabb eye dir box-lo box-hi)))109
(assert-true (> t 1.0)))))111
(test-group "ray-triangle"112
(test "hit from the front"113
(assert-float 1.0 (ray-triangle (vec3 0.25 0.25 1) (vec3 0 0 -1) tri-a tri-b tri-c)))115
(test "hit from the back"116
(assert-float 1.0 (ray-triangle (vec3 0.25 0.25 -1) (vec3 0 0 1) tri-a tri-b tri-c)))118
(test "t is in units of dir"119
(assert-float 0.25 (ray-triangle (vec3 0.25 0.25 1) (vec3 0 0 -4) tri-a tri-b tri-c)))121
(test "misses outside the triangle"122
(assert-false (ray-triangle (vec3 0.6 0.6 1) (vec3 0 0 -1) tri-a tri-b tri-c))123
(assert-false (ray-triangle (vec3 -0.1 0.5 1) (vec3 0 0 -1) tri-a tri-b tri-c)))125
(test "parallel and behind miss"126
(assert-false (ray-triangle (vec3 0.25 0.25 1) (vec3 1 0 0) tri-a tri-b tri-c))127
(assert-false (ray-triangle (vec3 0.25 0.25 1) (vec3 0 0 1) tri-a tri-b tri-c)))129
(test "an edge counts"130
(assert-float 1.0 (ray-triangle (vec3 0.5 0 1) (vec3 0 0 -1) tri-a tri-b tri-c)))132
(test "hit! writes t and the barycentric weights"133
(let* ((dst (make-vec3))134
(t (ray-triangle-hit! dst (vec3 0.2 0.3 2) (vec3 0 0 -1) tri-a tri-b tri-c)))135
(assert-float 2.0 t)136
(assert-true (vec3-approx=? (vec3 2 0.2 0.3) dst))))138
(test "hit! leaves dst alone on a miss"139
(let ((dst (vec3 7 7 7)))140
(assert-false (ray-triangle-hit! dst (vec3 2 2 1) (vec3 0 0 -1) tri-a tri-b tri-c))141
(assert-true (vec3-approx=? (vec3 7 7 7) dst)))))143
(test-group "boxes"144
(test "contains-point"145
(assert-true (aabb-contains-point? box-lo box-hi (vec3 0 0 0)))146
(assert-true (aabb-contains-point? box-lo box-hi (vec3 1 1 1)))147
(assert-false (aabb-contains-point? box-lo box-hi (vec3 1.01 0 0))))149
(test "overlap"150
(assert-true (aabb-overlap? box-lo box-hi (vec3 0.5 0.5 0.5) (vec3 3 3 3)))151
(assert-true (aabb-overlap? box-lo box-hi (vec3 1 -1 -1) (vec3 2 1 1)))152
(assert-false (aabb-overlap? box-lo box-hi (vec3 1.5 0 0) (vec3 3 1 1)))153
(assert-false (aabb-overlap? box-lo box-hi (vec3 0 0 2) (vec3 1 1 3)))))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)))))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 an162
;; absolute cut-off would call parallel163
(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))))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))))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-hi172
(vec3 (- (* 1e300 1e300) (* 1e300 1e300)) 0 0)))))