sourcelibs/Scene/src/Scene.xtlm
1⍝│ Scene: named v1 scene arrays from a small declarative notation.
2⍝│ Import with the ordinary constructors: "sc:" u_se< "Scene".
3⍝│ The statement macro resolves labels after parsing, emits literal arrays,
4⍝│ and leaves runtime animation/rendering to ordinary functions.
5
6"sc:" u̲se< "Scene.xtl"
7"pa:" u̲se< "Parsing"
8"t:" u̲se< "Strings"
9
10⍝│ A typed failure caught at the macro boundary and reported at its input.
11ʰb̲ad ← { message → "bad-scene" ⎕S̲IGNAL message }
12
13⍝│ Add the complete declaration to a parser or ordinary-constructor error.
14ʰc̲ontext ← { f̲ text →
15 'f̲ ⎕T̲RAP '{ e → ⎕R̲ECOVER "bad-scene" ⎕S̲IGNAL text c̲at ": " c̲at ⎕E̲MESSAGE e }
16}
17
18⍝│ One word from a boxed token vector.
19ʰw̲ord ← { i words → d̲isclose i s̲elect words }
20
21⍝│ Require a keyword at an already length-checked token position.
22ʰk̲ey ← { expected i words →
23 actual ← i ʰw̲ord words
24 n̲ot expected m̲atch actual ? ʰb̲ad "expected " c̲at expected c̲at ", got " c̲at actual
25 ""
26}
27
28⍝│ Exactly n tokens, with no silently ignored suffix.
29ʰe̲xact ← { n words →
30 n̲ot n = t̲ally words ? ʰb̲ad "expected " c̲at (f̲ormat n) c̲at " tokens, got " c̲at f̲ormat t̲ally words
31 ""
32}
33
34⍝│ Validate labels in one shared namespace, excluding grammar words.
35ʰn̲ame ← { name →
36 reserved ← "identity" "camera" "eye" "target" "up" "fov" "light" "position" "color" "material" "reflection" "object" "extrude" "depth" "group" "objects" "pivot"
37 n̲ot ᵖᵃn̲ame? name ? ʰb̲ad "invalid label: " c̲at name
38 (e̲nclose name) m̲ember? reserved ? ʰb̲ad "reserved label: " c̲at name
39 name
40}
41
42⍝│ Drop one optional sign before validating a numeric component.
43ʰu̲nsigned ← { text →
44 0 = t̲ally text ? text
45 (f̲irst text) m̲ember? "+-" ? 1 d̲rop text
46 text
47}
48
49⍝│ Parsing checks a padded unsigned decimal; at least one digit is required.
50ʰd̲ecimal? ← { text →
51 0 = t̲ally w̲here text m̲ember? ⎕D ? 0 = 1
52 a ← { @ → (f̲irst text) = f̲irst "." ? "0" c̲at text◆ text } @
53 b ← { @ → (f̲irst -1 t̲ake a) = f̲irst "." ? a c̲at "0"◆ a } @
54 ᵖᵃn̲umber? b
55}
56
57⍝│ Parse one finite signed decimal with optional signed decimal exponent.
58ʰn̲umber ← { text →
59 b ← ʰu̲nsigned text
60 at ← w̲here b m̲ember? "eE"
61 (t̲ally at) > 1 ? ʰb̲ad "invalid number: " c̲at text
62 mantissa ← { @ → 0 = t̲ally at ? b◆ ((f̲irst at) − 1) t̲ake b } @
63 n̲ot ʰd̲ecimal? mantissa ? ʰb̲ad "invalid number: " c̲at text
64 exponent ← { @ → 0 = t̲ally at ? "0"◆ ʰu̲nsigned (f̲irst at) d̲rop b } @
65 n̲ot (0 < t̲ally exponent) ∧ '∧ r̲/ exponent m̲ember? ⎕D ? ʰb̲ad "invalid exponent: " c̲at text
66 nums ← n̲umbers text
67 n̲ot 1 = t̲ally nums ? ʰb̲ad "expected one number: " c̲at text
68 value ← f̲irst nums
69 n̲ot (value − value) = 0.0 ? ʰb̲ad "number must be finite: " c̲at text
70 value
71}
72
73⍝│ Parse numeric tokens at an explicit list of positions.
74ʰn̲umbers ← { indices words → '{ i → ʰn̲umber i ʰw̲ord words } e̲ach indices }
75
76⍝│ Strip # comments per line, trim whitespace, and retain nonempty lines.
77ʰl̲ines ← { text →
78 raw ← "\n" ᵗs̲plit text
79 clean ← '{ box →
80 line ← d̲isclose box
81 comment ← w̲here line = f̲irst "#"
82 stop ← (f̲irst comment c̲at 1 + t̲ally line) − 1
83 ᵗt̲rim stop t̲ake line
84 } m̲ap raw
85 ('{ box → 0 < t̲ally d̲isclose box } e̲ach clean) r̲eplicate clean
86}
87
88⍝│ Declaration kind is its first word; no unknown line is dropped.
89ʰk̲ind ← { line → 1 ʰw̲ord ᵗw̲ords line }
90
91⍝│ Select declarations of one kind, preserving source order.
92ʰo̲f ← { kind lines → ('{ box → kind m̲atch ʰk̲ind d̲isclose box } e̲ach lines) r̲eplicate lines }
93
94⍝│ Camera constructor parses all fifteen tokens and reuses shared checks.
95ʰc̲amera ← { line →
96 '{ @ →
97 w ← ᵗw̲ords line
98 _ ← 15 ʰe̲xact w
99 _ ← ("eye" ʰk̲ey 2) w
100 _ ← ("target" ʰk̲ey 6) w
101 _ ← ("up" ʰk̲ey 10) w
102 _ ← ("fov" ʰk̲ey 14) w
103 ˢᶜc̲amera 3 4 5 7 8 9 11 12 13 15 ʰn̲umbers w
104 } ʰc̲ontext line
105}
106
107⍝│ Compile-time material record: label and one validated numeric row.
108ʰm̲aterial ← { line →
109 '{ @ →
110 w ← ᵗw̲ords line
111 _ ← 8 ʰe̲xact w
112 _ ← ("color" ʰk̲ey 3) w
113 _ ← ("reflection" ʰk̲ey 7) w
114 name ← ʰn̲ame 2 ʰw̲ord w
115 row ← ˢᶜm̲aterials 1 4 r̲eshape 4 5 6 8 ʰn̲umbers w
116 (name, row)
117 } ʰc̲ontext line
118}
119
120⍝│ Compile-time light record: label and validated point/intensity row.
121ʰl̲ight ← { line →
122 '{ @ →
123 w ← ᵗw̲ords line
124 _ ← 10 ʰe̲xact w
125 _ ← ("position" ʰk̲ey 3) w
126 _ ← ("color" ʰk̲ey 7) w
127 name ← ʰn̲ame 2 ʰw̲ord w
128 row ← ˢᶜl̲ights 1 6 r̲eshape 4 5 6 8 9 10 ʰn̲umbers w
129 (name, row)
130 } ʰc̲ontext line
131}
132
133⍝│ Object record holds its label, mesh and unresolved material reference.
134ʰo̲bject ← { line →
135 '{ @ →
136 w ← ᵗw̲ords line
137 n̲ot (8 = t̲ally w) ∨ 12 = t̲ally w ? ʰb̲ad "object needs extrude WORD depth NUMBER material LABEL and optional position X Y Z"
138 _ ← ("extrude" ʰk̲ey 3) w
139 _ ← ("depth" ʰk̲ey 5) w
140 _ ← ("material" ʰk̲ey 7) w
141 name ← ʰn̲ame 2 ʰw̲ord w
142 asset ← 4 ʰw̲ord w
143 depth ← ʰn̲umber 6 ʰw̲ord w
144 material ← ʰn̲ame 8 ʰw̲ord w
145 offset ← { @ →
146 8 = t̲ally w ? 0.0 0.0 0.0
147 _ ← ("position" ʰk̲ey 9) w
148 10 11 12 ʰn̲umbers w
149 } @
150 mesh ← ((depth ˢᶜo̲bject asset) offset)
151 (name, mesh, material)
152 } ʰc̲ontext line
153}
154
155⍝│ Group record holds its label, unresolved object names, and local pivot.
156ʰg̲roup ← { line →
157 '{ @ →
158 w ← ᵗw̲ords line
159 (t̲ally w) < 4 ? ʰb̲ad "group needs objects and at least one object label"
160 _ ← ("objects" ʰk̲ey 3) w
161 name ← ʰn̲ame 2 ʰw̲ord w
162 rest ← 3 d̲rop w
163 at ← w̲here '{ box → "pivot" m̲atch d̲isclose box } e̲ach rest
164 (t̲ally at) > 1 ? ʰb̲ad "group has more than one pivot"
165 stop ← (f̲irst at c̲at 1 + t̲ally rest) − 1
166 members ← '{ box → ʰn̲ame d̲isclose box } m̲ap stop t̲ake rest
167 0 = t̲ally members ? ʰb̲ad "group needs at least one object label"
168 n̲ot (t̲ally members) = t̲ally u̲nique members ? ʰb̲ad "group repeats an object label"
169 pivot ← { @ →
170 0 = t̲ally at ? 0.0 0.0 0.0
171 after ← (f̲irst at) d̲rop rest
172 _ ← 3 ʰe̲xact after
173 1 2 3 ʰn̲umbers after
174 } @
175 (name, members, pivot)
176 } ʰc̲ontext line
177}
178
179⍝│ Concatenate boxed numeric rows/meshes, including typed empty output.
180ʰr̲aze ← { fill boxes →
181 0 = t̲ally boxes ? 0 r̲eshape fill
182 d̲isclose '{ e̲nclose (r̲avel d̲isclose ⍺) c̲at r̲avel d̲isclose ⍵ } r̲/ boxes
183}
184
185⍝│ Serialize numeric arrays, supplying a typed fill even for an empty shape.
186ʰf̲ield ← { name suffix array fill →
187 body ← { @ → 0 = t̲ally r̲avel array ? fill◆ f̲ormat r̲avel array } @
188 name c̲at suffix c̲at " := " c̲at (f̲ormat s̲hape array) c̲at " r_eshape " c̲at body c̲at "\n"
189}
190
191⍝│ "logo" sc:s_cene< TEXT creates the eight prefix-named v1 arrays.
192⍝│ Declaration lines contain camera, material, light, extruded word object,
193⍝│ or flat group with optional pivot. # starts an end-of-line comment.
194⍝│ Object position is optional; forward references are resolved after parsing.
195⍝│ takes: text text
196⍝│ uses: parsed
197ᵐs̲cene< ← { prefix text →
198 n̲ot ⎕S̲TATEMENT @ ? "bad-scene call" ⎕R̲EJECT "scene macro creates bindings and must stand as a statement"
199 0 = t̲ally prefix ? "bad-scene left" ⎕R̲EJECT "scene prefix must be nonempty"
200 n̲ot ᵖᵃn̲ame? prefix ? "bad-scene left" ⎕R̲EJECT "scene prefix must be a lowercase variable name: " c̲at prefix
201 '{ @ →
202 lines ← ʰl̲ines text
203 unknown ← (n̲ot '{ box → (e̲nclose ʰk̲ind d̲isclose box) m̲ember? "camera" "material" "light" "object" "group" } e̲ach lines) r̲eplicate lines
204 0 < t̲ally unknown ? ʰb̲ad "unknown declaration: " c̲at d̲isclose f̲irst unknown
205 cameras ← "camera" ʰo̲f lines
206 n̲ot 1 = t̲ally cameras ? ʰb̲ad "scene needs exactly one camera declaration"
207 camera ← ʰc̲amera d̲isclose f̲irst cameras
208 materials ← '{ box → ʰm̲aterial d̲isclose box } m̲ap "material" ʰo̲f lines
209 lights ← '{ box → ʰl̲ight d̲isclose box } m̲ap "light" ʰo̲f lines
210 objects ← '{ box → ʰo̲bject d̲isclose box } m̲ap "object" ʰo̲f lines
211 groups ← '{ box → ʰg̲roup d̲isclose box } m̲ap "group" ʰo̲f lines
212 materialnames ← '{ box → (name, _) ← d̲isclose box◆ name } m̲ap materials
213 lightnames ← '{ box → (name, _) ← d̲isclose box◆ name } m̲ap lights
214 objectnames ← '{ box → (name, _, _) ← d̲isclose box◆ name } m̲ap objects
215 groupnames ← '{ box → (name, _, _) ← d̲isclose box◆ name } m̲ap groups
216 names ← materialnames c̲at lightnames c̲at objectnames c̲at groupnames
217 n̲ot (t̲ally names) = t̲ally u̲nique names ? ʰb̲ad "duplicate scene labels: " c̲at ", " ᵗj̲oin names
218 ⍝ Resolve each group's members after all object labels are known.
219 _ ← '{ box →
220 (name, members, _) ← d̲isclose box
221 bad ← (n̲ot members m̲ember? objectnames) r̲eplicate members
222 0 < t̲ally bad ? ʰb̲ad "group " c̲at name c̲at " has unresolved object " c̲at d̲isclose f̲irst bad
223 ""
224 } m̲ap groups
225 ⍝ Determine one Int group per object, defaulting to identity row 1.
226 assignments ← '{ box →
227 (name, _, _) ← d̲isclose box
228 belongs ← '{ grp → (_, members, _) ← d̲isclose grp◆ (e̲nclose name) m̲ember? members } e̲ach groups
229 at ← w̲here belongs
230 (t̲ally at) > 1 ? ʰb̲ad "object belongs to multiple groups: " c̲at name
231 0 = t̲ally at ? 1
232 1 + f̲irst at
233 } e̲ach objects
234 meshparts ← '{ box → (_, mesh, _) ← d̲isclose box◆ r̲avel mesh } m̲ap objects
235 flat ← 0.0 ʰr̲aze meshparts
236 triangles ← ˢᶜt̲riangles ((((t̲ally flat) d̲iv 9) c̲at 3 3) r̲eshape flat)
237 midparts ← '{ box →
238 (name, mesh, material) ← d̲isclose box
239 id ← f̲irst materialnames i̲ndexOf e̲nclose material
240 id > t̲ally materialnames ? ʰb̲ad "object " c̲at name c̲at " has unresolved material " c̲at material
241 (t̲ally mesh) r̲eshape id
242 } m̲ap objects
243 materialids ← 0 ʰr̲aze midparts
244 gidparts ← '{ i → (_, mesh, _) ← d̲isclose i s̲elect objects◆ (t̲ally mesh) r̲eshape f̲irst i s̲elect assignments } m̲ap r̲ange t̲ally objects
245 groupids ← 0 ʰr̲aze gidparts
246 materialrows ← '{ box → (_, row) ← d̲isclose box◆ row } m̲ap materials
247 materialarray ← ˢᶜm̲aterials ((t̲ally materials) c̲at 4) r̲eshape 0.0 ʰr̲aze materialrows
248 lightrows ← '{ box → (_, row) ← d̲isclose box◆ row } m̲ap lights
249 lightarray ← ˢᶜl̲ights ((t̲ally lights) c̲at 6) r̲eshape 0.0 ʰr̲aze lightrows
250 pivotrows ← '{ box → (_, _, pivot) ← d̲isclose box◆ pivot } m̲ap groups
251 pivots ← ˢᶜp̲ivots ((1 + t̲ally groups) c̲at 3) r̲eshape 0.0 0.0 0.0 c̲at 0.0 ʰr̲aze pivotrows
252 transforms ← ˢᶜi̲dentities 1 + t̲ally groups
253 f̲ ← { suffix a → ((prefix ʰf̲ield suffix) a) "0.0" }
254 i̲ ← { suffix a → ((prefix ʰf̲ield suffix) a) "0" }
255 ("triangles" f̲ triangles) c̲at ("materialids" i̲ materialids) c̲at ("groupids" i̲ groupids) c̲at ("materials" f̲ materialarray) c̲at ("transforms" f̲ transforms) c̲at ("pivots" f̲ pivots) c̲at ("camera" f̲ camera) c̲at "lights" f̲ lightarray
256 } ⎕T̲RAP '{ e → ⎕R̲ECOVER "bad-scene right" ⎕R̲EJECT ⎕E̲MESSAGE e }
257}
258
259"v:" u̲se< "Vector3"
260
261⍝│ V2 material record, with optional emission RGB after the base declaration.
262ʰm̲aterial2 ← { line →
263 '{ @ →
264 w ← ᵗw̲ords line
265 opaque ← "material" m̲atch ʰk̲ind line
266 base ← { @ → opaque ? 8◆ 14 } @
267 n̲ot (base = t̲ally w) ∨ (base + 4) = t̲ally w ? ʰb̲ad "material needs its base fields and optional emission R G B"
268 em ← { @ →
269 base = t̲ally w ? 1 3 r̲eshape 0.0
270 _ ← ("emission" ʰk̲ey base + 1) w
271 ˢᶜe̲mission 1 3 r̲eshape (base + 2 3 4) ʰn̲umbers w
272 } @
273 opaque ? { @ →
274 (name, row) ← ʰm̲aterial " " ᵗj̲oin base t̲ake w
275 (name, row, 0 m̲od 1, 1 5 r̲eshape 0.0 1.0 0.0 0.0 0.0, em)
276 } @
277 _ ← ("color" ʰk̲ey 3) w
278 _ ← ("transmission" ʰk̲ey 7) w
279 _ ← ("ior" ʰk̲ey 9) w
280 _ ← ("absorption" ʰk̲ey 11) w
281 name ← ʰn̲ame 2 ʰw̲ord w
282 row ← ˢᶜm̲aterials 1 4 r̲eshape (4 5 6 ʰn̲umbers w) c̲at 0.0
283 optics ← ˢᶜo̲ptics 1 5 r̲eshape 8 10 12 13 14 ʰn̲umbers w
284 (name, row, 1 + 0 m̲od 1, optics, em)
285 } ʰc̲ontext line
286}
287
288⍝│ Emitter record: label, validated origin/u/v descriptor and radiance row.
289ʰe̲mitter ← { line →
290 '{ @ →
291 w ← ᵗw̲ords line
292 _ ← 18 ʰe̲xact w
293 _ ← ("origin" ʰk̲ey 3) w
294 _ ← ("u" ʰk̲ey 7) w
295 _ ← ("v" ʰk̲ey 11) w
296 _ ← ("radiance" ʰk̲ey 15) w
297 name ← ʰn̲ame 2 ʰw̲ord w
298 rect ← ˢᶜe̲mitters 1 3 3 r̲eshape 4 5 6 8 9 10 12 13 14 ʰn̲umbers w
299 radiance ← ˢᶜe̲mission 1 3 r̲eshape 16 17 18 ʰn̲umbers w
300 (name, rect, radiance)
301 } ʰc̲ontext line
302}
303
304⍝│ Optional v2 lens declaration with aperture radius and axial focus distance.
305ʰl̲ens ← { line →
306 '{ @ →
307 w ← ᵗw̲ords line
308 _ ← 5 ʰe̲xact w
309 _ ← ("aperture" ʰk̲ey 2) w
310 _ ← ("focus" ʰk̲ey 4) w
311 ˢᶜl̲ens 3 5 ʰn̲umbers w
312 } ʰc̲ontext line
313}
314
315⍝│ V2 object record: label, triangles, spheres, material, closed-volume flag.
316ʰo̲bject2 ← { line →
317 kind ← ʰk̲ind line
318 "emitter" m̲atch kind ? { @ →
319 (name, rect, _) ← ʰe̲mitter line
320 (name, ˢᶜr̲ectangle 3 3 r̲eshape rect, 0 4 r̲eshape 0.0, name, 0 m̲od 1)
321 } @
322 "object" m̲atch kind ? { @ →
323 (name, mesh, material) ← ʰo̲bject line
324 (name, mesh, 0 4 r̲eshape 0.0, material, 1 + 0 m̲od 1)
325 } @
326 '{ @ →
327 w ← ᵗw̲ords line
328 name ← ʰn̲ame 2 ʰw̲ord w
329 "sphere" m̲atch kind ? { @ →
330 _ ← 10 ʰe̲xact w
331 _ ← ("center" ʰk̲ey 3) w
332 _ ← ("radius" ʰk̲ey 7) w
333 _ ← ("material" ʰk̲ey 9) w
334 balls ← ˢᶜs̲pheres 1 4 r̲eshape 4 5 6 8 ʰn̲umbers w
335 (name, 0 3 3 r̲eshape 0.0, balls, ʰn̲ame 10 ʰw̲ord w, 1 + 0 m̲od 1)
336 } @
337 "floor" m̲atch kind ? { @ →
338 _ ← 16 ʰe̲xact w
339 _ ← ("origin" ʰk̲ey 3) w
340 _ ← ("u" ʰk̲ey 7) w
341 _ ← ("v" ʰk̲ey 11) w
342 _ ← ("material" ʰk̲ey 15) w
343 mesh ← ˢᶜr̲ectangle 3 3 r̲eshape 4 5 6 8 9 10 12 13 14 ʰn̲umbers w
344 (name, mesh, 0 4 r̲eshape 0.0, ʰn̲ame 16 ʰw̲ord w, 0 m̲od 1)
345 } @
346 n̲ot (8 = t̲ally w) ∨ 12 = t̲ally w ? ʰb̲ad "polyhedron needs size/radii X Y Z material NAME and optional position X Y Z"
347 key ← { @ → "box" m̲atch kind ? "size"◆ "radii" } @
348 _ ← (key ʰk̲ey 3) w
349 _ ← ("material" ʰk̲ey 7) w
350 size ← 4 5 6 ʰn̲umbers w
351 mesh ← { @ → "box" m̲atch kind ? ˢᶜb̲ox size◆ ˢᶜo̲ctahedron size } @
352 offset ← { @ →
353 8 = t̲ally w ? 0.0 0.0 0.0
354 _ ← ("position" ʰk̲ey 9) w
355 10 11 12 ʰn̲umbers w
356 } @
357 moved ← (s̲hape mesh) r̲eshape offset ᵛt̲ranslate ((3 × t̲ally mesh) c̲at 3) r̲eshape mesh
358 (name, moved, 0 4 r̲eshape 0.0, ʰn̲ame 8 ʰw̲ord w, 1 + 0 m̲od 1)
359 } ʰc̲ontext line
360}
361
362⍝│ Select multiple declaration kinds while retaining their shared order.
363ʰk̲inds ← { kinds lines →
364 ('{ box → (e̲nclose ʰk̲ind d̲isclose box) m̲ember? kinds } e̲ach lines) r̲eplicate lines
365}
366
367⍝│ Explicit v2 opt-in; all v1 declarations retain their meanings. New sphere,
368⍝│ box, octahedron, floor and dielectric declarations share the namespace.
369⍝│ Emits v1 arrays and the separate v2 fields, with opaque/pinhole defaults.
370⍝│ takes: text text
371⍝│ uses: parsed
372ᵐs̲cene2< ← { prefix text →
373 n̲ot ⎕S̲TATEMENT @ ? "bad-scene call" ⎕R̲EJECT "scene2 macro creates bindings and must stand as a statement"
374 (0 = t̲ally prefix) ∨ n̲ot ᵖᵃn̲ame? prefix ? "bad-scene left" ⎕R̲EJECT "scene prefix must be a nonempty lowercase variable name"
375 '{ @ →
376 lines ← ʰl̲ines text
377 allowed ← "camera" "material" "dielectric" "light" "object" "group" "sphere" "box" "octahedron" "floor" "lens" "emitter"
378 unknown ← (n̲ot '{ box → (e̲nclose ʰk̲ind d̲isclose box) m̲ember? allowed } e̲ach lines) r̲eplicate lines
379 0 < t̲ally unknown ? ʰb̲ad "unknown v2 declaration: " c̲at d̲isclose f̲irst unknown
380 cameras ← "camera" ʰo̲f lines
381 n̲ot 1 = t̲ally cameras ? ʰb̲ad "scene needs exactly one camera declaration"
382 camera ← ʰc̲amera d̲isclose f̲irst cameras
383 authored ← '{ box → ʰm̲aterial2 d̲isclose box } m̲ap "material" "dielectric" ʰk̲inds lines
384 emitterrecords ← '{ box → ʰe̲mitter d̲isclose box } m̲ap "emitter" ʰo̲f lines
385 automatic ← '{ box →
386 (name, _, radiance) ← d̲isclose box
387 (name, 1 4 r̲eshape 0.0, 0 m̲od 1, 1 5 r̲eshape 0.0 1.0 0.0 0.0 0.0, radiance)
388 } m̲ap emitterrecords
389 materials ← authored c̲at automatic
390 lenslines ← "lens" ʰo̲f lines
391 (t̲ally lenslines) > 1 ? ʰb̲ad "scene permits at most one lens declaration"
392 lens ← { @ → 0 = t̲ally lenslines ? ˢᶜl̲ens 0.0 1.0◆ ʰl̲ens d̲isclose f̲irst lenslines } @
393 lights ← '{ box → ʰl̲ight d̲isclose box } m̲ap "light" ʰo̲f lines
394 objects ← '{ box → ʰo̲bject2 d̲isclose box } m̲ap "object" "sphere" "box" "octahedron" "floor" "emitter" ʰk̲inds lines
395 groups ← '{ box → ʰg̲roup d̲isclose box } m̲ap "group" ʰo̲f lines
396 materialnames ← '{ box → (name, _, _, _, _) ← d̲isclose box◆ name } m̲ap materials
397 lightnames ← '{ box → (name, _) ← d̲isclose box◆ name } m̲ap lights
398 objectnames ← '{ box → (name, _, _, _, _) ← d̲isclose box◆ name } m̲ap objects
399 groupnames ← '{ box → (name, _, _) ← d̲isclose box◆ name } m̲ap groups
400 authorednames ← '{ box → (name, _, _, _, _) ← d̲isclose box◆ name } m̲ap authored
401 names ← authorednames c̲at lightnames c̲at objectnames c̲at groupnames
402 n̲ot (t̲ally names) = t̲ally u̲nique names ? ʰb̲ad "duplicate scene labels"
403 reserved ← "dielectric" "transmission" "ior" "absorption" "sphere" "center" "radius" "box" "size" "octahedron" "radii" "floor" "origin" "u" "v" "emitter" "radiance" "lens" "aperture" "focus" "emission"
404 '∨ r̲/ names m̲ember? reserved ? ʰb̲ad "reserved v2 scene label"
405 _ ← '{ box →
406 (name, members, _) ← d̲isclose box
407 n̲ot '∧ r̲/ members m̲ember? objectnames ? ʰb̲ad "unresolved group object in " c̲at name
408 ""
409 } m̲ap groups
410 assignments ← '{ box →
411 (name, _, _, _, _) ← d̲isclose box
412 at ← w̲here '{ grp → (_, members, _) ← d̲isclose grp◆ (e̲nclose name) m̲ember? members } e̲ach groups
413 (t̲ally at) > 1 ? ʰb̲ad "object belongs to multiple groups: " c̲at name
414 0 = t̲ally at ? 1
415 1 + f̲irst at
416 } e̲ach objects
417 mids ← '{ box →
418 (name, mesh, _, material, closed) ← d̲isclose box
419 id ← f̲irst materialnames i̲ndexOf e̲nclose material
420 id > t̲ally materialnames ? ʰb̲ad "unresolved material for " c̲at name
421 (_, _, kind, _, _) ← d̲isclose id s̲elect materials
422 (kind = 1) ∧ closed = 0 ? ʰb̲ad "dielectric requires a closed object: " c̲at name
423 _ ← { @ → (kind = 1) ∧ (t̲ally mesh) > 0 ? t̲ally ˢᶜc̲losed mesh◆ 0 } @
424 id
425 } e̲ach objects
426 meshflat ← r̲avel 0.0 ʰr̲aze '{ box → (_, mesh, _, _, _) ← d̲isclose box◆ mesh } m̲ap objects
427 triangles ← ˢᶜt̲riangles ((((t̲ally meshflat) d̲iv 9) c̲at 3 3) r̲eshape meshflat)
428 ballflat ← r̲avel 0.0 ʰr̲aze '{ box → (_, _, balls, _, _) ← d̲isclose box◆ balls } m̲ap objects
429 spheres ← ˢᶜs̲pheres ((((t̲ally ballflat) d̲iv 4) c̲at 4) r̲eshape ballflat)
430 counts ← '{ box → (_, mesh, balls, _, _) ← d̲isclose box◆ (t̲ally mesh) + t̲ally balls } e̲ach objects
431 ⍝ Prefix sums count primitives of both kinds in source declaration order.
432 starts ← '{ i → 1 + '+ r̲/ (i − 1) t̲ake counts } e̲ach r̲ange t̲ally objects
433 tri̲ ← { field → 0 ʰr̲aze '{ i →
434 (_, mesh, _, _, closed) ← d̲isclose i s̲elect objects
435 n ← t̲ally mesh
436 value ← { @ → field = 1 ? f̲irst i s̲elect mids◆ field = 2 ? f̲irst i s̲elect assignments◆ i × closed } @
437 n r̲eshape value
438 } m̲ap r̲ange t̲ally objects }
439 ball̲ ← { field → 0 ʰr̲aze '{ i →
440 (_, _, balls, _, _) ← d̲isclose i s̲elect objects
441 value ← { @ → field = 1 ? f̲irst i s̲elect mids◆ field = 2 ? f̲irst i s̲elect assignments◆ i } @
442 (t̲ally balls) r̲eshape value
443 } m̲ap r̲ange t̲ally objects }
444 triangleids ← 0 ʰr̲aze '{ i → (_, mesh, _, _, _) ← d̲isclose i s̲elect objects◆ ((f̲irst i s̲elect starts) − 1) + r̲ange t̲ally mesh } m̲ap r̲ange t̲ally objects
445 sphereids ← 0 ʰr̲aze '{ i → (_, _, balls, _, _) ← d̲isclose i s̲elect objects◆ ((f̲irst i s̲elect starts) − 1) + r̲ange t̲ally balls } m̲ap r̲ange t̲ally objects
446 materialarray ← ˢᶜm̲aterials ((t̲ally materials) c̲at 4) r̲eshape 0.0 ʰr̲aze '{ box → (_, row, _, _, _) ← d̲isclose box◆ row } m̲ap materials
447 kinds ← ˢᶜm̲aterialKinds 0 ʰr̲aze '{ box → (_, _, tag, _, _) ← d̲isclose box◆ 1 r̲eshape tag } m̲ap materials
448 optics ← ˢᶜo̲ptics ((t̲ally materials) c̲at 5) r̲eshape 0.0 ʰr̲aze '{ box → (_, _, _, row, _) ← d̲isclose box◆ row } m̲ap materials
449 emission ← ˢᶜe̲mission ((t̲ally materials) c̲at 3) r̲eshape 0.0 ʰr̲aze '{ box → (_, _, _, _, row) ← d̲isclose box◆ row } m̲ap materials
450 lightarray ← ˢᶜl̲ights ((t̲ally lights) c̲at 6) r̲eshape 0.0 ʰr̲aze '{ box → (_, row) ← d̲isclose box◆ row } m̲ap lights
451 pivots ← ˢᶜp̲ivots ((1 + t̲ally groups) c̲at 3) r̲eshape 0.0 0.0 0.0 c̲at 0.0 ʰr̲aze '{ box → (_, _, pivot) ← d̲isclose box◆ pivot } m̲ap groups
452 f̲ ← { suffix a → ((prefix ʰf̲ield suffix) a) "0.0" }
453 i̲ ← { suffix a → ((prefix ʰf̲ield suffix) a) "0" }
454 base ← ("triangles" f̲ triangles) c̲at ("materialids" i̲ tri̲ 1) c̲at ("groupids" i̲ tri̲ 2) c̲at ("materials" f̲ materialarray) c̲at ("transforms" f̲ ˢᶜi̲dentities 1 + t̲ally groups) c̲at ("pivots" f̲ pivots) c̲at ("camera" f̲ camera) c̲at "lights" f̲ lightarray
455 ids ← ("triangleids" i̲ triangleids) c̲at ("sphereids" i̲ sphereids) c̲at ("triangleobjectids" i̲ tri̲ 3) c̲at ("sphereobjectids" i̲ ball̲ 3) c̲at ("spherematerialids" i̲ ball̲ 1) c̲at ("spheregroupids" i̲ ball̲ 2)
456 emitters ← ˢᶜe̲mitters ((t̲ally emitterrecords) c̲at 3 3) r̲eshape 0.0 ʰr̲aze '{ box → (_, rect, _) ← d̲isclose box◆ rect } m̲ap emitterrecords
457 emitterradiance ← ˢᶜe̲mission ((t̲ally emitterrecords) c̲at 3) r̲eshape 0.0 ʰr̲aze '{ box → (_, _, radiance) ← d̲isclose box◆ radiance } m̲ap emitterrecords
458 defaults ← (prefix c̲at "sceneversion := 2\n") c̲at ("lens" f̲ lens) c̲at ("emitters" f̲ emitters) c̲at "emitterradiance" f̲ emitterradiance
459 base c̲at ids c̲at defaults c̲at ("spheres" f̲ spheres) c̲at ("materialkind" i̲ kinds) c̲at ("optics" f̲ optics) c̲at "emission" f̲ emission
460 } ⎕T̲RAP '{ e → ⎕R̲ECOVER "bad-scene right" ⎕R̲EJECT ⎕E̲MESSAGE e }
461}