Debian Codegraph @

benchmark/tests/ray.sml

raw
1 (* From the SML/NJ benchmark suite. *)
2
3 (* objects.sml
4 *
5 * COPYRIGHT (c) 1992 AT&T Bell Laboratories
6 *
7 * Type declarations for the various objects in the ray tracer.
8 *)
9
10 structure Objects =
11 struct
12
13 datatype point = PT of {x : real, y : real, z : real}
14
15 datatype vector = VEC of {l : real, m : real, n : real}
16
17 datatype ray = Ray of {s : point, d : vector}
18
19 datatype camera = Camera of {
20 vp : point,
21 ul : point,
22 ur : point,
23 ll : point,
24 lr : point
25 }
26
27 datatype color = Color of {red : real, grn : real, blu : real}
28
29 datatype sphere = Sphere of {c : point, r : real, color : color}
30
31 datatype hit = Miss | Hit of {t : real, s : sphere}
32
33 datatype visible = Visible of {h : point, s : sphere}
34
35 datatype object
36 = TOP
37 | NUMBER of real
38 | NAME of string
39 | LIST of object list
40 | OPERATOR of object list -> object list
41 | MARK
42 | LITERAL of string
43 | UNMARK
44 | POINT of point
45 | VECTOR of vector
46 | RAY of ray
47 | CAMERA of camera
48 | COLOR of color
49 | SPHERE of sphere
50 | HIT
51 | VISIBLE
52
53 end (* Objects *)
54 (* interp.sml
55 *
56 * COPYRIGHT (c) 1992 AT&T Bell Laboratories
57 *)
58
59 structure Interp =
60 struct
61
62 local
63 val exit = OS.Process.exit
64 fun ordof(s, i) = Char.ord(String.sub(s, i))
65 exception NotAChar
66 exception NotAReal
67 fun fromStr x =
68 (case Char.fromString x
69 of SOME c => c
70 | NONE => raise NotAChar)
71
72 fun strToReal s =
73 (case Real.fromString s
74 of SOME r => r
75 | _ => raise NotAReal)
76
77 fun intToReal x =
78 (strToReal ((Int.toString x) ^ ".0"))
79
80
81 val explode = (fn x => map Char.toString (explode x))
82 val implode = (fn x => implode (map fromStr x))
83
84 open Objects
85 val dict = ref ([] : {key : string, value : object} list)
86 fun dictInsert (NAME key, value) = let
87 fun find [] = [{key=key, value=value}]
88 | find (x::r) = if (key = #key x)
89 then {key=key, value=value}::r
90 else x :: (find r)
91 in
92 dict := find(!dict)
93 end
94 | dictInsert _ = raise Fail "dictInsert"
95 fun prObj outStrm obj = let
96 fun printf args = TextIO.output(outStrm, implode args)
97 fun pr (NUMBER n) = printf[" ", Real.toString n, "\n"]
98 | pr (NAME s) = printf[" ", s, "\n"]
99 | pr (LITERAL s) = printf[" ", s, "\n"]
100 | pr (LIST l) = app pr l
101 | pr MARK = printf[" MARK\n"]
102 | pr (OPERATOR _) = printf[" <operator>\n"]
103 | pr TOP = printf[" TOP OF STACK\n"]
104 | pr _ = printf[" <object>\n"]
105 in
106 pr obj
107 end
108 in
109
110 exception Stop
111
112 fun error opName stk = let
113 fun prStk ([], _) = ()
114 | prStk (_, 0) = ()
115 | prStk (obj::r, i) = (prObj TextIO.stdErr obj; prStk(r, i-1))
116 in
117 TextIO.output(TextIO.stdErr, "ERROR: "^opName^"\n");
118 prStk (stk, 10);
119 raise (Fail opName)
120 end
121
122 fun installOperator (name, rator) =
123 dictInsert (NAME name, OPERATOR rator)
124
125 fun ps_def (v::k::r) = (dictInsert(k, v); r)
126 | ps_def stk = error "ps_def" stk
127
128 local
129 fun binOp (f, opName) = let
130 fun g ((NUMBER arg1)::(NUMBER arg2)::r) =
131 NUMBER(f(arg2, arg1)) :: r
132 | g stk = error opName stk
133 in
134 g
135 end
136 in
137 val ps_add = binOp (op +, "add")
138 val ps_sub = binOp (op -, "sub")
139 val ps_mul = binOp (op *, "mul")
140 val ps_div = binOp (op /, "div")
141 end
142
143 fun ps_rand stk = (NUMBER 0.5)::stk (** ??? **)
144
145 fun ps_print (obj::r) = (prObj TextIO.stdOut obj; r)
146 | ps_print stk = error "print" stk
147
148 fun ps_dup (obj::r) = (obj::obj::r)
149 | ps_dup stk = error "dup" stk
150
151 fun ps_stop _ = raise Stop
152
153 (* initialize dictionary and begin parsing input *)
154 fun parse inStrm = let
155 fun getc () = case TextIO.input1 inStrm of NONE => ""
156 | SOME c => Char.toString c
157 fun peek () = case TextIO.lookahead inStrm
158 of SOME x => Char.toString x
159 | _ => ""
160 (* parse one token from inStrm *)
161 fun toke deferred = let
162 fun doChar "" = exit OS.Process.success
163 | doChar "%" = let
164 fun lp "\n" = doChar(getc())
165 | lp "" = exit OS.Process.success
166 | lp _ = lp(getc())
167 in
168 lp(getc())
169 end
170 | doChar "{" = (MARK, deferred+1)
171 | doChar "}" = (UNMARK, deferred-1)
172 | doChar c = if Char.isSpace (fromStr c)
173 then doChar(getc())
174 else let
175 fun lp buf = (case peek()
176 of "{" => buf
177 | "}" => buf
178 | "%" => buf
179 | c => if Char.isSpace(fromStr c)
180 then buf
181 else (getc(); lp(c::buf))
182 (* end case *))
183 val tok = implode (rev (lp [c]))
184 val hd = ordof(tok, 0)
185 in
186 if (hd = ord (#"/"))
187 then (LITERAL(substring(tok, 1, size tok - 1)), deferred)
188 else
189 if ((Char.isDigit (chr hd)) orelse (hd = ord (#"-")))
190 then (NUMBER(strToReal(tok)), deferred)
191 else (NAME tok, deferred)
192 end
193 in
194 doChar(getc())
195 end
196 (* execute a token (if not deferred) *)
197 fun exec (UNMARK, stk, _) = let
198 fun lp ([], _) = raise Fail "MARK"
199 | lp (MARK::r, l) = (LIST l)::r
200 | lp (x::r, l) = lp (r, x::l)
201 in
202 lp (stk, [])
203 end
204 | exec (OPERATOR f, stk, 0) = f stk
205 | exec (LIST l, stk, 0) = let
206 fun execBody ([], stk) = stk
207 | execBody (obj::r, stk) = (exec(obj, stk, 0); execBody(r, stk))
208 in
209 execBody (l, stk)
210 end
211 | exec (NAME s, stk, 0) = let
212 fun find [] = raise Fail "undefined name"
213 | find ({key, value}::r) = if (key = s) then value else find r
214 in
215 exec (find (!dict), stk, 0)
216 end
217 | exec (obj, stk, _) = obj::stk
218 fun lp (stk, level) = let
219 val (obj, level) = toke level
220 val stk = exec (obj, stk, level)
221 in
222 lp (stk, level)
223 end
224 in
225 installOperator ("add", ps_add);
226 installOperator ("def", ps_def);
227 installOperator ("div", ps_div);
228 installOperator ("dup", ps_dup);
229 installOperator ("mul", ps_mul);
230 installOperator ("print", ps_print);
231 installOperator ("rand", ps_rand);
232 installOperator ("stop", ps_stop);
233 installOperator ("sub", ps_sub);
234 (lp ([], 0)) handle Stop => ()
235 end (* parse *)
236
237 end (* local *)
238
239 end (* Interp *)
240 (* ray.sml
241 *
242 * COPYRIGHT (c) 1992 AT&T Bell Laboratories
243 *)
244
245 structure Ray =
246 struct
247 local open Objects in
248
249 (** basic operations on points and vectors **)
250
251 fun scaleVector (s, VEC{l, m, n}) = VEC{l=s*l, m=s*m, n=s*n}
252
253 fun vecPlusVec (VEC{l, m, n}, VEC{l=l', m=m', n=n'}) = VEC{l=l+l', m=m+m', n=n+n'}
254
255 fun vecPlusPt (VEC{l, m, n}, PT{x, y, z}) = PT{x=x+l, y=y+m, z=z+n}
256
257 fun ptMinusPt (PT{x, y, z}, PT{x=x', y=y', z=z'}) = VEC{l=x-x', m=y-y', n=z-z'}
258
259 fun wave (PT{x, y, z}, PT{x=x', y=y', z=z'}, w) = PT{
260 x = w * (x' - x) + x,
261 y = w * (y' - y) + y,
262 z = w * (z' - z) + z
263 }
264
265 fun dotProd (VEC{l, m, n}, VEC{l=l', m=m', n=n'}) = ((l*l') + (m*m') + (n*n'))
266
267 (* normal vector to sphere *)
268 fun normalSphere (Visible{h, s as Sphere{c, ...}}) = let
269 val n = ptMinusPt(h, c)
270 val norm = Math.sqrt(dotProd(n, n))
271 in
272 scaleVector(1.0 / norm, n)
273 end
274
275 (* intersect a ray with a sphere *)
276 fun intersectSphere (Ray ray, s as Sphere sphere) = let
277 val a = dotProd(#d ray, #d ray)
278 val sdiffc = ptMinusPt(#s ray, #c sphere)
279 val b = 2.0 * dotProd(sdiffc, #d ray)
280 val c = dotProd(sdiffc, sdiffc) - (#r sphere * #r sphere)
281 val d = b*b - 4.0*a*c
282 in
283 if (d <= 0.0)
284 then Miss
285 else let
286 val d = Math.sqrt(d)
287 val t1 = (~b - d) / (2.0 * a)
288 val t2 = (~b + d) / (2.0 * a)
289 val t = if ((t1 > 0.0) andalso (t1 < t2)) then t1 else t2
290 in
291 Hit{t=t, s=s}
292 end
293 end
294
295 (* simple shading function *)
296 fun shade {light, phi} (visible as Visible{h, s}) = let
297 val l = ptMinusPt(light, h)
298 val n = normalSphere(visible)
299 val irradiance = phi * dotProd(l,n) / dotProd(l,l);
300 val irradiance = (if (irradiance < 0.0) then 0.0 else irradiance) + 0.05
301 val Sphere{color=Color{red, grn, blu}, ...} = s
302 in
303 Color{red=red*irradiance, grn=grn*irradiance, blu=blu*irradiance}
304 end
305
306 fun trace (ray as (Ray ray'), objList) = let
307 fun closest (Miss, x) = x
308 | closest (x, Miss) = x
309 | closest (h1 as Hit{t=t1, ...}, h2 as Hit{t=t2, ...}) =
310 if (t2 < t1) then h2 else h1
311 fun lp ([], Hit{t, s}) = Visible{
312 h = vecPlusPt(scaleVector(t, #d ray'), #s ray'),
313 s = s
314 }
315 | lp (s :: r, closestHit) =
316 lp (r, closest (closestHit, intersectSphere (ray, s)))
317 | lp _ = raise Fail "trace"
318 in
319 lp (objList, Miss)
320 end
321
322 fun camera (Camera cam) (x, y) = let
323 val l = wave (#ul cam, #ll cam, y)
324 val r = wave (#ur cam, #lr cam, y)
325 val image_point = wave(l, r, x)
326 in
327 Ray{d = ptMinusPt(image_point, #vp cam), s = #vp cam}
328 end
329
330 val shade = shade {light = PT{x = 10.0, y = ~10.0, z = ~10.0}, phi = 16.0}
331 val camera = camera (Camera{
332 vp = PT{x = 0.0, y = 0.0, z = ~3.0},
333 ul = PT{x = ~1.0, y = ~1.0, z = 0.0},
334 ur = PT{x = 1.0, y = ~1.0, z = 0.0},
335 ll = PT{x = ~1.0, y = 1.0, z = 0.0},
336 lr = PT{x = 1.0, y = 1.0, z = 0.0}
337 })
338
339 fun image objList (x, y) = shade (trace(camera(x, y), objList))
340
341 fun picture (picName, objList) = let
342 val outStrm = TextIO.openOut picName
343 val image = image objList
344 val print = fn x => TextIO.output (outStrm, x)
345 fun putc c = TextIO.output1(outStrm, chr c)
346 fun doPixel (i, j) = let
347 val x = (real i) / 512.0
348 val y = (real j) / 512.0
349 val (Color c) = image (x, y)
350 fun cvt x = if (x >= 1.0) then 255 else floor(256.0*x)
351 in
352 putc (cvt (#red c));
353 putc (cvt (#grn c));
354 putc (cvt (#blu c))
355 end
356 fun lp_j j = if (j < 512)
357 then let
358 fun lp_i i = if (i < 512)
359 then (doPixel(i, j); lp_i(i+1))
360 else ()
361 in
362 lp_i 0; lp_j(j+1)
363 end
364 else ()
365 in
366 print "TYPE=dump\n";
367 print "WINDOW=0 0 512 512\n";
368 print "NCHAN=3\n";
369 print "CHAN=rgb\n";
370 print "\n";
371 lp_j 0;
372 TextIO.closeOut outStrm
373 end
374
375 end (* local *)
376 end; (* Ray *)
377 (* interface.sml
378 *
379 * COPYRIGHT (c) 1992 AT&T Bell Laboratories
380 *
381 * The interface between the interpreter and the ray tracer.
382 *)
383
384 structure Interface =
385 struct
386 local
387 open Objects
388 in
389
390 (* color pops three numbers and pushes a color object.
391 * usage: red-value green-value blue-value color
392 *)
393 fun ps_color ((NUMBER blu)::(NUMBER grn)::(NUMBER red)::r) =
394 (COLOR(Color{red=red, grn=grn, blu=blu})) :: r
395 | ps_color stk = Interp.error "color" stk
396
397 (* pop radius, coordinates of center, and a color and push a sphere
398 * usage: radius x y z color-value sphere
399 *)
400 fun ps_sphere (
401 (COLOR c)::(NUMBER z)::(NUMBER y)::(NUMBER x)::(NUMBER rad)::r
402 ) = SPHERE(Sphere{c=PT{x=x, y=y, z=z}, r=rad, color=c}) :: r
403 | ps_sphere stk = Interp.error "sphere" stk
404
405 (* build an object list from solids on the stack, then invoke raytracer *)
406 fun ps_raytrace ((LITERAL picName)::r) = let
407 fun mkObjList ([], l) = l
408 | mkObjList ((SPHERE s)::r, l) = mkObjList(r, s::l)
409 | mkObjList (_::r, l) = mkObjList(r, l)
410 in
411 Ray.picture(picName, mkObjList(r, []));
412 []
413 end
414 | ps_raytrace stk = Interp.error "raytrace" stk
415
416 (* add ray tracing operations to interpreter dictionary *)
417 fun rtInit () = (
418 Interp.installOperator("color", ps_color);
419 Interp.installOperator("sphere", ps_sphere);
420 Interp.installOperator("raytrace", ps_raytrace))
421
422 end (* local *)
423 end;
424
425 signature BMARK =
426 sig
427 val doit : int -> unit
428 val testit : TextIO.outstream -> unit
429 end;
430 (* main.sml
431 *
432 * COPYRIGHT (c) 1992 AT&T Bell Laboratories
433 *
434 * Main structure for running raytracer as benchmark.
435 *)
436
437 structure Main : BMARK =
438 struct
439
440 fun doit n =
441 let
442 fun loop n =
443 if n = 0
444 then ()
445 else
446 let
447 val strm = TextIO.openIn "DATA/ray"
448 val _ = Interface.rtInit()
449 val _ = Interp.parse strm
450 val _ = TextIO.closeIn strm
451 in
452 loop (n - 1)
453 end
454 in
455 loop n
456 end
457
458 fun testit _ = ()
459 end