1 ! Factor port of the raytracer benchmark from
2 ! http://www.ffconsultancy.com/free/ray_tracer/languages.html
4 USING: arrays accessors specialized-arrays.double io io.files
5 io.files.temp io.encodings.binary kernel math math.functions
6 math.vectors math.parser make sequences sequences.private words
8 IN: benchmark.raytracer
12 #! Normalized { -1 -3 2 }.
19 : oversampling 4 ; inline
25 : delta 1.4901161193847656E-8 ; inline
27 TUPLE: ray { orig double-array read-only } { dir double-array read-only } ;
31 TUPLE: hit { normal double-array read-only } { lambda float read-only } ;
35 GENERIC: intersect-scene ( hit ray scene -- hit )
37 TUPLE: sphere { center double-array read-only } { radius float read-only } ;
41 : sphere-v ( sphere ray -- v )
42 [ center>> ] [ orig>> ] bi* v- ; inline
44 : sphere-b ( v ray -- b )
47 : sphere-d ( sphere b v -- d )
48 [ radius>> sq ] [ sq ] [ norm-sq ] tri* - + ; inline
50 : -+ ( x y -- x-y x+y )
51 [ - ] [ + ] 2bi ; inline
53 : sphere-t ( b d -- t )
55 [ 2drop 1.0/0.0 ] [ [ [ 0.0 > ] keep ] dip ? ] if ; inline
57 : sphere-b&v ( sphere ray -- b v )
58 [ sphere-v ] [ nip ] 2bi
59 [ sphere-b ] [ drop ] 2bi ; inline
61 : ray-sphere ( sphere ray -- t )
62 [ drop ] [ sphere-b&v ] 2bi
63 [ drop ] [ sphere-d ] 3bi
64 dup 0.0 < [ 3drop 1/0. ] [ sqrt sphere-t nip ] if ; inline
66 : if-ray-sphere ( hit ray sphere quot -- hit )
67 #! quot: hit ray sphere l -- hit
69 [ ] [ swap ray-sphere nip ] [ 2drop lambda>> ] 3tri
71 ] dip [ 3drop ] if ; inline
73 : sphere-n ( ray sphere l -- n )
74 [ [ orig>> ] [ dir>> ] bi ] [ center>> ] [ ] tri*
75 swap [ v*n ] dip v- v+ ; inline
77 M: sphere intersect-scene ( hit ray sphere -- hit )
78 [ [ sphere-n normalize ] keep <hit> nip ] if-ray-sphere ;
80 TUPLE: group < sphere { objs array read-only } ;
82 : <group> ( objs bound -- group )
83 [ center>> ] [ radius>> ] bi rot group boa ; inline
85 : make-group ( bound quot -- )
86 swap [ { } make ] dip <group> ; inline
88 M: group intersect-scene ( hit ray group -- hit )
89 [ drop objs>> [ intersect-scene ] with each ] if-ray-sphere ;
91 : initial-hit T{ hit f double-array{ 0.0 0.0 0.0 } 1/0. } ; inline
93 : initial-intersect ( ray scene -- hit )
94 [ initial-hit ] 2dip intersect-scene ; inline
96 : ray-o ( ray hit -- o )
97 [ [ orig>> ] [ normal>> delta v*n ] bi* ]
98 [ [ dir>> ] [ lambda>> ] bi* v*n ]
101 : sray-intersect ( ray scene hit -- ray )
102 swap [ ray-o light vneg <ray> ] dip initial-intersect ; inline
104 : ray-g ( hit -- g ) normal>> light v. ; inline
106 : cast-ray ( ray scene -- g )
107 2dup initial-intersect dup lambda>> 1/0. = [
110 [ sray-intersect lambda>> 1/0. = ] keep swap
111 [ ray-g neg ] [ drop 0.0 ] if
114 : create-center ( c r d -- c2 )
115 [ 3.0 12.0 sqrt / * ] dip n*v v+ ; inline
117 DEFER: create ( level c r -- scene )
119 : create-step ( level c r d -- scene )
120 over [ create-center ] dip 2.0 / [ 1 - ] 2dip create ;
122 : create-offsets ( quot -- )
124 double-array{ -1.0 1.0 -1.0 }
125 double-array{ 1.0 1.0 -1.0 }
126 double-array{ -1.0 1.0 1.0 }
127 double-array{ 1.0 1.0 1.0 }
130 : create-bound ( c r -- sphere ) 3.0 * <sphere> ;
132 : create-group ( level c r -- scene )
135 [ [ 3dup ] dip create-step , ] create-offsets 3drop
138 : create ( level c r -- scene )
139 pick 1 = [ <sphere> nip ] [ create-group ] if ;
141 : ss-point ( dx dy -- point )
142 [ oversampling /f ] bi@ 0.0 double-array{ } 3sequence ;
144 : ss-grid ( -- ss-grid )
145 oversampling [ oversampling [ ss-point ] with map ] map ;
147 : ray-grid ( point ss-grid -- ray-grid )
149 [ v+ normalize double-array{ 0.0 0.0 -4.0 } swap <ray> ] with map
152 : ray-pixel ( scene point -- n )
153 ss-grid ray-grid 0.0 -rot
154 [ [ swap cast-ray + ] with each ] with each ;
156 : pixel-grid ( -- grid )
159 [ size 0.5 * - ] bi@ swap size
160 double-array{ } 3sequence
164 : pgm-header ( w h -- )
165 "P5\n" % swap # " " % # "\n255\n" % ;
167 : pgm-pixel ( n -- ) 255 * 0.5 + >fixnum , ;
169 : ray-trace ( scene -- pixels )
170 pixel-grid [ [ ray-pixel ] with map ] with map ;
173 levels double-array{ 0.0 -1.0 0.0 } 1.0 create ray-trace [
175 [ [ oversampling sq / pgm-pixel ] each ] each
178 : raytracer-main ( -- )
179 run "raytracer.pnm" temp-file binary set-file-contents ;