Debian Codegraph @

benchmarks.larceny/src/ray.scm

raw
1 ;;; RAY -- Ray-trace a simple scene with spheres, generating a ".pgm" file.
2 ;;; Translated to Scheme from Paul Graham's book ANSI Common Lisp, Example 9.8
3
4 (define (make-point x y z)
5 (vector x y z))
6
7 (define (point-x p) (vector-ref p 0))
8 (define (point-y p) (vector-ref p 1))
9 (define (point-z p) (vector-ref p 2))
10
11 (define (sq x) (FLOAT* x x))
12
13 (define (mag x y z)
14 (FLOATsqrt (FLOAT+ (sq x) (sq y) (sq z))))
15
16 (define (unit-vector x y z)
17 (let ((d (mag x y z)))
18 (make-point (FLOAT/ x d) (FLOAT/ y d) (FLOAT/ z d))))
19
20 (define (distance p1 p2)
21 (mag (FLOAT- (point-x p1) (point-x p2))
22 (FLOAT- (point-y p1) (point-y p2))
23 (FLOAT- (point-z p1) (point-z p2))))
24
25 (define (minroot a b c)
26 (if (FLOATzero? a)
27 (FLOAT/ (FLOAT- c) b)
28 (let ((disc (FLOAT- (sq b) (FLOAT* 4.0 a c))))
29 (if (FLOATnegative? disc)
30 #f
31 (let ((discrt (FLOATsqrt disc))
32 (minus-b (FLOAT- b))
33 (two-a (FLOAT* 2.0 a)))
34 (FLOATmin (FLOAT/ (FLOAT+ minus-b discrt) two-a)
35 (FLOAT/ (FLOAT- minus-b discrt) two-a)))))))
36
37 (define *world* '())
38
39 (define eye (make-point 0.0 0.0 200.0))
40
41 (define (tracer pathname res)
42 (call-with-output-file/truncate
43 pathname
44 (lambda (p)
45 (let ((extent (* res 100)))
46 (display "P2 " p)
47 (write extent p)
48 (display " " p)
49 (write extent p)
50 (display " 255" p)
51 (newline p)
52 (do ((y 0 (+ y 1)))
53 ((= y extent))
54 (do ((x 0 (+ x 1)))
55 ((= x extent))
56 (write (color-at
57 (FLOAT+ -50.0
58 (FLOAT/ (exact->inexact x) (exact->inexact res)))
59 (FLOAT+ -50.0
60 (FLOAT/ (exact->inexact y) (exact->inexact res))))
61 p)
62 (newline p)))))))
63
64 (define (color-at x y)
65 (let ((ray (unit-vector (FLOAT- x (point-x eye))
66 (FLOAT- y (point-y eye))
67 (FLOAT- (point-z eye)))))
68 (FLOATinexact->exact (FLOATround (FLOAT* (sendray eye ray) 255.0)))))
69
70 (define (sendray pt ray)
71 (let* ((x (first-hit pt ray))
72 (s (vector-ref x 0))
73 (int (vector-ref x 1)))
74 (if s
75 (FLOAT* (lambert s int ray)
76 (surface-color s))
77 0.0)))
78
79 (define (first-hit pt ray)
80 (let loop ((lst *world*) (surface #f) (hit #f) (dist 1e308))
81 (if (null? lst)
82 (vector surface hit)
83 (let ((s (car lst)))
84 (let ((h (intersect s pt ray)))
85 (if h
86 (let ((d (distance h pt)))
87 (if (FLOAT< d dist)
88 (loop (cdr lst) s h d)
89 (loop (cdr lst) surface hit dist)))
90 (loop (cdr lst) surface hit dist)))))))
91
92 (define (lambert s int ray)
93 (let ((n (normal s int)))
94 (FLOATmax 0.0
95 (FLOAT+ (FLOAT* (point-x ray) (point-x n))
96 (FLOAT* (point-y ray) (point-y n))
97 (FLOAT* (point-z ray) (point-z n))))))
98
99 (define (make-sphere color radius center)
100 (vector color radius center))
101
102 (define (sphere-color s) (vector-ref s 0))
103 (define (sphere-radius s) (vector-ref s 1))
104 (define (sphere-center s) (vector-ref s 2))
105
106 (define (defsphere x y z r c)
107 (let ((s (make-sphere c r (make-point x y z))))
108 (set! *world* (cons s *world*))
109 s))
110
111 (define (surface-color s)
112 (sphere-color s))
113
114 (define (intersect s pt ray)
115 (sphere-intersect s pt ray))
116
117 (define (sphere-intersect s pt ray)
118 (let* ((xr (point-x ray))
119 (yr (point-y ray))
120 (zr (point-z ray))
121 (c (sphere-center s))
122 (n (minroot
123 (FLOAT+ (sq xr) (sq yr) (sq zr))
124 (FLOAT* 2.0
125 (FLOAT+ (FLOAT* (FLOAT- (point-x pt) (point-x c)) xr)
126 (FLOAT* (FLOAT- (point-y pt) (point-y c)) yr)
127 (FLOAT* (FLOAT- (point-z pt) (point-z c)) zr)))
128 (FLOAT+ (sq (FLOAT- (point-x pt) (point-x c)))
129 (sq (FLOAT- (point-y pt) (point-y c)))
130 (sq (FLOAT- (point-z pt) (point-z c)))
131 (FLOAT- (sq (sphere-radius s)))))))
132 (if n
133 (make-point (FLOAT+ (point-x pt) (FLOAT* n xr))
134 (FLOAT+ (point-y pt) (FLOAT* n yr))
135 (FLOAT+ (point-z pt) (FLOAT* n zr)))
136 #f)))
137
138 (define (normal s pt)
139 (sphere-normal s pt))
140
141 (define (sphere-normal s pt)
142 (let ((c (sphere-center s)))
143 (unit-vector (FLOAT- (point-x c) (point-x pt))
144 (FLOAT- (point-y c) (point-y pt))
145 (FLOAT- (point-z c) (point-z pt)))))
146
147 (define (ray-test . opt)
148 (set! *world* '())
149 (defsphere 0.0 -300.0 -1200.0 200.0 0.8)
150 (defsphere -80.0 -150.0 -1200.0 200.0 0.7)
151 (defsphere 70.0 -100.0 -1200.0 200.0 0.9)
152 (do ((x -2 (+ x 1)))
153 ((> x 2))
154 (do ((z 2 (+ z 1)))
155 ((> z 7))
156 (defsphere
157 (FLOAT* (exact->inexact x) 200.0)
158 300.0
159 (FLOAT* (exact->inexact z) -400.0)
160 40.0
161 0.75)))
162 (tracer "spheres.pgm" (if (null? opt) 1 (car opt))))
163
164 (define (run)
165 (ray-test 1)
166 'ok)
167
168 (define (main . args)
169 (run-benchmark
170 "ray"
171 ray-iters
172 (lambda (result)
173 (equal? result 'ok))
174 (lambda () (lambda () (run)))))