sourcelibs/Glass/src/Glass.xtl
1⍝│ Glass: bounded dielectric transport with explicit medium identity.
2⍝│ Import with "gl:" u_se< "Glass". Depends on Scene, RayTrace, Vector3 and Random.
3⍝│ World-space v2 arrays remain separate, including every Int identity.
4⍝│ Original array implementation of Snell, Fresnel and Beer-Lambert laws.
5
6"sc:" u̲se< "Scene"
7"rt:" u̲se< "RayTrace"
8"v:" u̲se< "Vector3"
9
10⍝│ Reject nonfinite data and arithmetic overflow.
11ʰf̲inite ← { a →
12 n̲ot '∧ r̲/ r̲avel (a − a) = 0.0 ? @ p̲anic< "Glass requires finite numbers and results"
13 a
14}
15
16⍝│ Validate an N by columns matrix and convert it to Float.
17ʰr̲ows ← { columns data →
18 n̲ot ((t̲ally data) c̲at columns) m̲atch s̲hape data ? @ p̲anic< "Glass matrix has the wrong row width"
19 ʰf̲inite f̲loat data
20}
21
22⍝│ Exactly n finite numeric vector entries, converted to Float.
23ʰv̲ector ← { n data →
24 n̲ot (1 r̲eshape n) m̲atch s̲hape data ? @ p̲anic< "Glass vector has the wrong length"
25 ʰf̲inite f̲loat data
26}
27
28⍝│ Repeat one row n times.
29ʰb̲roadcast ← { n row → (n r̲eshape 1.0) '× t̲able row }
30
31⍝│ Multiply xyz rows by a per-row coefficient vector.
32ʰs̲cale ← { weights rows → rows × weights '× t̲able 1.0 1.0 1.0 }
33
34⍝│ Require finite unit xyz rows, permitting only relative 1e-10 roundoff.
35ʰu̲nit ← { data →
36 a ← 3 ʰr̲ows data
37 n̲ot '∧ r̲/ (a̲bs (ᵛl̲ength a) − 1.0) ≤ 1e-10 ? @ p̲anic< "Glass interface vectors must be unit length"
38 a
39}
40
41⍝│ (((incidentIOR i_nterface transmittedIOR)_ normals)_ directions):
42⍝│ (reflected xyz, transmitted xyz, valid-transmission Int mask, Fresnel).
43⍝│ IORs are positive N-vectors; unit normals point against unit directions.
44⍝│ TIR has zero transmitted direction, mask 0 and reflectance 1.
45ˡi̲nterface ← { incidentIOR transmittedIOR normals directions →
46 n ← ʰu̲nit normals
47 d ← ʰu̲nit directions
48 n̲ot (s̲hape n) m̲atch s̲hape d ? @ p̲anic< "Glass interface batches must match"
49 ni ← (t̲ally n) ʰv̲ector incidentIOR
50 nt ← (t̲ally n) ʰv̲ector transmittedIOR
51 n̲ot '∧ r̲/ (ni > 0.0) ∧ nt > 0.0 ? @ p̲anic< "Glass IORs must be positive"
52 cosine ← n̲eg n ᵛd̲ot d
53 n̲ot '∧ r̲/ (cosine ≥ -1e-10) ∧ cosine ≤ 1.0 + 1e-10 ? @ p̲anic< "Glass interface normals must oppose incident directions"
54 ci ← 0.0 m̲ax 1.0 m̲in cosine
55 eta ← ni ÷ nt
56 st2 ← ʰf̲inite (eta × eta) × 0.0 m̲ax 1.0 − ci × ci
57 valid ← st2 ≤ 1.0
58 ct ← s̲qrt 0.0 m̲ax 1.0 − st2
59 transmitted ← (f̲loat valid) ʰs̲cale (eta ʰs̲cale d) + ((eta × ci) − ct) ʰs̲cale n
60 ds ← ʰf̲inite (ni × ci) + nt × ct
61 dp ← ʰf̲inite (nt × ci) + ni × ct
62 rs ← ((ni × ci) − nt × ct) ÷ ds + f̲loat ds = 0.0
63 rp ← ((nt × ci) − ni × ct) ÷ dp + f̲loat dp = 0.0
64 raw ← 0.5 × (rs × rs) + rp × rp
65 fr ← (f̲loat n̲ot valid) + (f̲loat valid ∧ ni ≠ nt) × raw
66 n̲ot '∧ r̲/ (fr ≥ -1e-12) ∧ fr ≤ 1.0 + 1e-12 ? @ p̲anic< "Glass Fresnel result escaped its numeric tolerance"
67 (n ʳᵗr̲eflect d, ʰf̲inite transmitted, f̲loor f̲loat valid, 0.0 m̲ax 1.0 m̲in ʰf̲inite fr)
68}
69
70⍝│ sigma a_bsorb distances: N by 3 absorption >=0 and N physical lengths
71⍝│ >=0 to Beer-Lambert transmittance exp(-sigma*distance), N by 3 Float.
72ˡa̲bsorb ← { sigma distances →
73 a ← 3 ʰr̲ows sigma
74 ds ← (t̲ally a) ʰv̲ector distances
75 n̲ot ('∧ r̲/ r̲avel a ≥ 0.0) ∧ '∧ r̲/ ds ≥ 0.0 ? @ p̲anic< "Glass absorption and distances must be nonnegative"
76 ʰf̲inite e̲xp n̲eg ds ʰs̲cale a
77}
78
79⍝│ ((seed u_niform interaction)_ sampleids): deterministic N Float in (0,1).
80⍝│ Keys are nonnegative Int <=2147483645; interaction is Int 0..63.
81⍝│ Bounded modular products stay below Int64 overflow; no global RNG state.
82ˡu̲niform ← { seed interaction sampleids →
83 n̲ot 0 = t̲ally s̲hape seed ? @ p̲anic< "Glass seed must be scalar"
84 n̲ot 0 = t̲ally s̲hape interaction ? @ p̲anic< "Glass interaction must be scalar"
85 (seed < 0) ∨ seed > 2147483645 ? @ p̲anic< "Glass seed is out of range"
86 (interaction < 0) ∨ interaction > 63 ? @ p̲anic< "Glass interaction is out of range"
87 n̲ot 1 = t̲ally s̲hape sampleids ? @ p̲anic< "Glass sample IDs must be a vector"
88 n̲ot '∧ r̲/ (sampleids ≥ 0) ∧ sampleids ≤ 2147483645 ? @ p̲anic< "Glass sample IDs are out of range"
89 x ← 1 + (sampleids + seed) m̲od 2147483646
90 step̲ ← { state → (48271 × state) m̲od 2147483647 }
91 z ← (interaction + 2) 'step̲ p̲ower x
92 ((f̲loat z) + 0.5) ÷ 2147483647.0
93}
94
95⍝│ Validate an Int vector with n entries and minimum allowed ID.
96ʰi̲ds ← { n minimum ids →
97 n̲ot (1 r̲eshape n) m̲atch s̲hape ids ? @ p̲anic< "Glass ID count does not match its primitive count"
98 n̲ot '∧ r̲/ ids ≥ minimum ? @ p̲anic< "Glass ID is below its allowed minimum"
99 ids + 0 × ids m̲od 1
100}
101
102⍝│ One-based vector lookup for known IDs; zero sentinel stays zero.
103ʰl̲ocal ← { ids selected →
104 '{ id → 0 = id ? 0◆ at ← w̲here ids = id◆ 0 = t̲ally at ? @ p̲anic< "Glass unknown object ID"◆ f̲irst at } e̲ach selected
105}
106
107⍝│ Object bounds, using the same mesh/sphere arrays as actual intersection.
108ʰb̲ounds ← { triangles spheres toids soids object →
109 mesh ← (toids = object) r̲eplicate triangles
110 balls ← (soids = object) r̲eplicate spheres
111 0 < t̲ally balls ? { @ →
112 row ← r̲avel balls
113 center ← 3 t̲ake row
114 radius ← f̲irst 3 d̲rop row
115 2 3 r̲eshape (center − radius) c̲at center + radius
116 } @
117 points ← ((3 × t̲ally mesh) c̲at 3) r̲eshape mesh
118 2 3 r̲eshape ('m̲in r̲/ points) c̲at 'm̲ax r̲/ points
119}
120
121⍝│ Require strict separation of conservative axis-aligned glass bounds.
122ʰs̲eparate ← { bounds i →
123 a ← i s̲elect bounds
124 others ← bounds
125 low ← 1 s̲elect₂ others
126 high ← 2 s̲elect₂ others
127 alo ← (t̲ally bounds) ʰb̲roadcast 1 s̲elect a
128 ahi ← (t̲ally bounds) ʰb̲roadcast 2 s̲elect a
129 gap ← (low − ahi) m̲ax alo − high
130 separated ← '∨ r̲/₂ gap > 1e-10
131 '∧ r̲/ separated ∨ (r̲ange t̲ally bounds) = i
132}
133
134⍝│ ((geometry p_reflight links)_ shading): validated world scene.
135⍝│ geometry=(triangles,spheres); links=(triangleids,sphereids,
136⍝│ triangleobjectids,sphereobjectids,materialids,spherematerialids);
137⍝│ shading=(materials,materialkind,optics,emission,lights).
138⍝│ Returns (geometry,links,shading,glassobjectids,objectmaterialids,bounds).
139⍝│ Glass volumes must be convex closed meshes or one sphere per object,
140⍝│ with a single material and strictly disjoint conservative AABB bounds.
141ˡp̲reflight ← { geometry links shading →
142 (triangles, spheres) ← geometry
143 (triangleids, sphereids, triangleobjectids, sphereobjectids, materialids, spherematerialids) ← links
144 (materials, materialkind, optics, emission, lights) ← shading
145 mesh ← ˢᶜt̲riangles triangles
146 balls ← ˢᶜs̲pheres spheres
147 t ← t̲ally mesh
148 s ← t̲ally balls
149 tids ← (t ʰi̲ds 1) triangleids
150 sids ← (s ʰi̲ds 1) sphereids
151 allids ← tids c̲at sids
152 n̲ot (t̲ally allids) = t̲ally u̲nique allids ? @ p̲anic< "Glass global primitive IDs must be unique"
153 toids ← (t ʰi̲ds 0) triangleobjectids
154 soids ← (s ʰi̲ds 0) sphereobjectids
155 mats ← ˢᶜm̲aterials materials
156 kinds ← ˢᶜm̲aterialKinds materialkind
157 op ← ˢᶜo̲ptics optics
158 em ← ˢᶜe̲mission emission
159 ls ← ˢᶜl̲ights lights
160 m ← t̲ally mats
161 n̲ot (m = t̲ally kinds) ∧ (m = t̲ally op) ∧ m = t̲ally em ? @ p̲anic< "Glass material array counts must match"
162 tmids ← m ˢᶜi̲ds materialids
163 smids ← m ˢᶜi̲ds spherematerialids
164 n̲ot (t = t̲ally tmids) ∧ s = t̲ally smids ? @ p̲anic< "Glass needs one material ID per primitive"
165 tk ← tmids s̲elect kinds
166 sk ← smids s̲elect kinds
167 n̲ot ('∧ r̲/ (tk = 0) ∨ toids > 0) ∧ '∧ r̲/ (sk = 0) ∨ soids > 0 ? @ p̲anic< "Glass dielectric primitives require positive object IDs"
168 objects ← u̲nique ((tk = 1) r̲eplicate toids) c̲at (sk = 1) r̲eplicate soids
169 omids ← '{ object →
170 ti ← toids = object
171 si ← soids = object
172 mids ← (ti r̲eplicate tmids) c̲at si r̲eplicate smids
173 n̲ot 1 = t̲ally u̲nique mids ? @ p̲anic< "Glass object must have a single material"
174 faces ← ti r̲eplicate mesh
175 bs ← si r̲eplicate balls
176 (t̲ally bs) > 1 ? @ p̲anic< "Glass object ID may identify only one sphere"
177 (0 < t̲ally bs) ∧ (0 < t̲ally faces) ? @ p̲anic< "Glass object cannot mix mesh and sphere geometry"
178 _ ← { @ → 0 < t̲ally faces ? t̲ally ˢᶜc̲losed faces◆ 0 } @
179 f̲irst mids
180 } e̲ach objects
181 bb ← '{ object → ((((mesh ʰb̲ounds balls) toids) soids) object) } m̲ap objects
182 flat ← { @ → 0 = t̲ally bb ? 0 r̲eshape 0.0◆ r̲avel d̲isclose '{ e̲nclose (r̲avel d̲isclose ⍺) c̲at r̲avel d̲isclose ⍵ } r̲/ bb } @
183 bounds ← ʰf̲inite ((t̲ally objects) c̲at 2 3) r̲eshape flat
184 n̲ot '∧ r̲/ '{ i → bounds ʰs̲eparate i } e̲ach r̲ange t̲ally objects ? @ p̲anic< "Glass volumes have overlapping or touching conservative bounds; nesting and interpenetration are unsupported"
185 ((mesh, balls), (tids, sids, toids, soids, tmids, smids), (mats, kinds, op, em, ls), objects, omids, bounds)
186}
187
188⍝│ Revalidate the public immutable scene bundle rather than trusting caches.
189ʰc̲hecked ← { scene →
190 (geometry, links, shading, _, _, _) ← scene
191 (geometry ˡp̲reflight links) shading
192}
193
194⍝│ Initial containment for one validated convex glass object; boundary
195⍝│ starts within relative 1e-10 refuse rather than guessing a medium.
196ʰc̲ontains ← { scene origins object →
197 ((triangles, spheres), (_, _, toids, soids, _, _), _, _, _, _) ← scene
198 balls ← (soids = object) r̲eplicate spheres
199 0 < t̲ally balls ? { @ →
200 row ← r̲avel balls
201 radius ← f̲irst 3 d̲rop row
202 distance ← ᵛl̲ength origins − (t̲ally origins) ʰb̲roadcast 3 t̲ake row
203 '∨ r̲/ (a̲bs distance − radius) ≤ 1e-10 × radius ? @ p̲anic< "Glass initial origin lies on a glass boundary"
204 distance < radius
205 } @
206 mesh ← (toids = object) r̲eplicate triangles
207 a ← 1 s̲elect₂ mesh
208 normals ← ᵛn̲ormalize ((2 s̲elect₂ mesh) − a) ᵛc̲ross (3 s̲elect₂ mesh) − a
209 points ← ((3 × t̲ally mesh) c̲at 3) r̲eshape mesh
210 extent ← 'm̲ax r̲/ ('m̲ax r̲/ points) − 'm̲in r̲/ points
211 eps ← 1e-10 × extent
212 '{ i →
213 delta ← ((t̲ally mesh) ʰb̲roadcast i s̲elect origins) − a
214 values ← '+ r̲/₂ delta × normals
215 inside ← '∧ r̲/ values < 0.0
216 boundary ← ('∧ r̲/ values ≤ eps) ∧ '∨ r̲/ (a̲bs values) ≤ eps
217 boundary ? @ p̲anic< "Glass initial origin lies on a glass boundary"
218 inside
219 } e̲ach r̲ange t̲ally origins
220}
221
222⍝│ One unique object ID per contained origin, zero for air.
223ʰm̲ediums ← { scene origins →
224 (_, _, _, objects, _, _) ← scene
225 step̲ ← { state →
226 k ← f̲irst state
227 ids ← 1 d̲rop state
228 object ← f̲irst k s̲elect objects
229 inside ← ((scene ʰc̲ontains origins) object)
230 (k + 1) c̲at ids + object × f̲loor f̲loat inside
231 }
232 1 d̲rop (t̲ally objects) 'step̲ p̲ower 1 c̲at (t̲ally origins) r̲eshape 0
233}
234
235⍝│ scene m_ediums origins: N Int initial medium IDs from N by 3 origins.
236⍝│ Reuses shared preflight; zero is air, positive IDs identify glass objects.
237ˡm̲ediums ← { scene origins →
238 world ← ʰc̲hecked scene
239 o ← 3 ʰr̲ows origins
240 world ʰm̲ediums o
241}
242
243⍝│ Global selected IDs to separate local triangle and sphere indices.
244ʰl̲ocals ← { scene selected →
245 (_, (tids, sids, _, _, _, _), _, _, _, _) ← scene
246 ti ← '{ id → at ← w̲here tids = id◆ 0 = t̲ally at ? 0◆ f̲irst at } e̲ach selected
247 si ← '{ id → at ← w̲here sids = id◆ 0 = t̲ally at ? 0◆ f̲irst at } e̲ach selected
248 (ti, si)
249}
250
251⍝│ Paired per-primitive Int arrays to one selected Int vector, zero miss.
252ʰs̲elected ← { arrays locals →
253 (ta, sa) ← arrays
254 (ti, si) ← locals
255 ((ti + 1) s̲elect 0 c̲at ta) + (si + 1) s̲elect 0 c̲at sa
256}
257
258⍝│ Outward geometric normals, preserving entry/exit information.
259ʰn̲ormals ← { scene locals points →
260 ((triangles, spheres), _, _, _, _, _) ← scene
261 (ti, si) ← locals
262 selected ← (ti + 1) s̲elect (1 3 3 r̲eshape 0.0) c̲at triangles
263 a ← 1 s̲elect₂ selected
264 tn ← ᵛn̲ormalize ((2 s̲elect₂ selected) − a) ᵛc̲ross (3 s̲elect₂ selected) − a
265 balls ← (si + 1) s̲elect (1 4 r̲eshape 0.0 0.0 0.0 1.0) c̲at spheres
266 sn ← (f̲loat si > 0) ʰs̲cale ᵛn̲ormalize points − 3 t̲ake₂ balls
267 tn + sn
268}
269
270⍝│ Clip entries retain the v1 meanings and positive near/far interval.
271ʰc̲lip ← { clip →
272 c ← 4 ʰv̲ector clip
273 n̲ot '∧ r̲/ c > 0.0 ? @ p̲anic< "Glass clip entries must be positive"
274 (f̲irst c) ≥ f̲irst 1 d̲rop c ? @ p̲anic< "Glass near must be less than far"
275 (f̲irst 2 d̲rop c) ≥ 1.0 ? @ p̲anic< "Glass intersection epsilon must be less than one"
276 c
277}
278
279⍝│ Nonnegative ambient/background rows, retaining v1 environment units.
280ʰe̲nvironment ← { environment →
281 n̲ot 2 3 m̲atch s̲hape environment ? @ p̲anic< "Glass environment requires ambient and background RGB rows"
282 e ← ʰf̲inite f̲loat environment
283 n̲ot '∧ r̲/ r̲avel e ≥ 0.0 ? @ p̲anic< "Glass environment must be nonnegative"
284 e
285}
286
287⍝│ Select Float rows with an explicit zero/default first row for ID zero.
288ʰf̲ields ← { rows padding ids →
289 (ids + 1) s̲elect padding c̲at rows
290}
291
292⍝│ Direct Lambertian point lighting with conservative opaque visibility:
293⍝│ glass also blocks this shadow ray; no refracted caustic approximation.
294ʰl̲ighting ← { scene clip points normals directions →
295 ((triangles, spheres), (tids, sids, _, _, _, _), (_, _, _, _, lights), _, _, _) ← scene
296 n ← t̲ally points
297 step̲ ← { state →
298 (k, accum) ← state
299 light ← k s̲elect lights
300 origins ← points + (f̲irst 3 d̲rop clip) × normals
301 delta ← (n ʰb̲roadcast 3 t̲ake light) − origins
302 distance ← ᵛl̲ength delta
303 dirs ← (ᵛn̲ormalize delta) + (f̲loat distance = 0.0) ʰs̲cale directions
304 selected ← ((((((clip ʳᵗm̲ixedNearest triangles) spheres) tids) sids) origins) dirs)
305 hits ← (((((((clip ʳᵗm̲ixedHits triangles) spheres) tids) sids) selected) origins) dirs)
306 visible ← (selected = 0) ∨ (ʳᵗd̲istances hits) ≥ distance − f̲irst 3 d̲rop clip
307 cosine ← 0.0 m̲ax normals ᵛd̲ot dirs
308 safe ← distance + f̲loat distance = 0.0
309 weight ← (f̲loat visible ∧ distance > 0.0) × cosine ÷ (p̲i @) × safe × safe
310 (k + 1, accum + weight ʰs̲cale n ʰb̲roadcast 3 d̲rop light)
311 }
312 (_, result) ← (t̲ally lights) 'step̲ p̲ower (1, (n c̲at 3) r̲eshape 0.0)
313 result
314}
315
316⍝│ One bounded sampled continuation per active ray; tuple members keep
317⍝│ Int identities/masks/counts separate from Float geometric/radiance state.
318ʰs̲tep ← { scene clip environment sample̲ light̲ state →
319 (origins, directions, beta, radiance, medium, active, counts, carry, clipped, bounce) ← state
320 0 = '+ r̲/ active ? state
321 ((triangles, spheres), (tids, sids, toids, soids, tmids, smids), (materials, kinds, optics, emission, _), objects, omids, _) ← scene
322 n ← t̲ally origins
323 ids ← ((((((clip ʳᵗm̲ixedNearest triangles) spheres) tids) sids) origins) directions)
324 hits ← (((((((clip ʳᵗm̲ixedHits triangles) spheres) tids) sids) ids) origins) directions)
325 hit ← active × ʳᵗm̲ask hits
326 miss ← active × f̲loor f̲loat ids = 0
327 local ← scene ʰl̲ocals ids
328 object ← (toids, soids) ʰs̲elected local
329 mid ← (tmids, smids) ʰs̲elected local
330 surface ← ((materials ʰf̲ields 1 4 r̲eshape 0.0) mid)
331 op ← ((optics ʰf̲ields 1 5 r̲eshape 0.0 1.0 0.0 0.0 0.0) mid)
332 emitted ← ((emission ʰf̲ields 1 3 r̲eshape 0.0) mid)
333 kind ← (mid + 1) s̲elect 0 c̲at kinds
334 mediumlocal ← objects ʰl̲ocal medium
335 mediummat ← (mediumlocal + 1) s̲elect 0 c̲at omids
336 insideop ← ((optics ʰf̲ields 1 5 r̲eshape 0.0 1.0 0.0 0.0 0.0) mediummat)
337 distance ← ʳᵗd̲istances hits
338 attenuation ← (2 d̲rop₂ insideop) ˡa̲bsorb (f̲loat hit) × distance + carry
339 attenuated ← beta × attenuation
340 points ← origins + distance ʰs̲cale directions
341 outward ← ((scene ʰn̲ormals local) points)
342 dot ← outward ᵛd̲ot directions
343 front ← dot ≤ 0.0
344 grazing ← (a̲bs dot) ≤ 1e-10
345 normal ← ((1.0 − 2.0 × f̲loat n̲ot front) ʰs̲cale outward) − (f̲loat ids = 0) ʰs̲cale directions
346 glass ← (hit = 1) ∧ kind = 1
347 badentry ← glass ∧ front ∧ (n̲ot grazing) ∧ medium ≠ 0
348 badexit ← glass ∧ (n̲ot front) ∧ (n̲ot grazing) ∧ medium ≠ object
349 '∨ r̲/ badentry ∨ badexit ? @ p̲anic< "Glass medium identity disagrees with geometric entry or exit"
350 ni ← 2 s̲elect₂ insideop
351 target ← ((f̲loat front) × (2 s̲elect₂ op)) + f̲loat n̲ot front
352 nt ← ((f̲loat kind = 1) × target) + (f̲loat kind = 0) × ni
353 (reflected, transmitted, valid, fresnel) ← (((ni ˡi̲nterface nt) normal) directions)
354 w ← (f̲loat kind = 1) × 1 s̲elect₂ op
355 r ← 4 s̲elect₂ surface
356 diffuse ← (1.0 − w) × 1.0 − r
357 mirror ← ((1.0 − w) × r) + w × fresnel
358 trans ← w × (1.0 − fresnel) × f̲loat valid
359 continuation ← mirror + trans
360 safe ← continuation + f̲loat continuation = 0.0
361 xi ← sample̲ bounce
362 reflect ← xi < mirror ÷ safe
363 transmit ← (n̲ot reflect) ∧ valid = 1
364 ⍝ Parenthesized mixture: the selected lobe's weight divided by its PDF.
365 gain ← continuation × ((f̲loat reflect) + (f̲loat transmit) × (ni ÷ nt) ^ 2.0)
366 illum ← (n ʰb̲roadcast 1 s̲elect environment) + (((light̲ points) normal) directions) bounce
367 direct ← ((f̲loat front) ʰs̲cale emitted) + diffuse ʰs̲cale (3 t̲ake₂ surface) × illum
368 nextL ← radiance + ((f̲loat hit) ʰs̲cale attenuated × direct) + (f̲loat miss ∧ medium = 0) ʰs̲cale beta × n ʰb̲roadcast 2 s̲elect environment
369 nextActive ← hit × f̲loor f̲loat continuation > 0.0
370 nextBeta ← beta + (f̲loat hit) ʰs̲cale (gain ʰs̲cale attenuated) − beta
371 nextDirection ← ᵛn̲ormalize ((f̲loat reflect) ʰs̲cale reflected) + (f̲loat transmit) ʰs̲cale transmitted
372 nextD ← directions + (f̲loat nextActive) ʰs̲cale nextDirection − directions
373 offset ← f̲irst 3 d̲rop clip
374 nextO ← origins + (f̲loat nextActive) ʰs̲cale (points + offset × nextD) − origins
375 transition ← hit × f̲loor f̲loat (kind = 1) ∧ transmit ∧ n̲ot grazing
376 destination ← object × f̲loor f̲loat front
377 nextMedium ← medium + transition × destination − medium
378 nextCarry ← offset × f̲loat nextActive
379 nextClipped ← clipped m̲ax miss × f̲loor f̲loat medium ≠ 0
380 (ʰf̲inite nextO, ʰf̲inite nextD, ʰf̲inite nextBeta, ʰf̲inite nextL, nextMedium, nextActive, counts + hit, nextCarry, nextClipped, bounce + 1)
381}
382
383⍝│ (((((((scene t_race clip)_ environment)_ origins)_ directions)_ mediumids)
384⍝│ _ sampleids)_ controls): bounded radiance-mode path diagnostics.
385⍝│ controls is Int [seed,cap], cap 0..64. Initial directions normalize.
386⍝│ Result=(radiance,origins,directions,throughput,mediumids,active,counts,
387⍝│ clipped). Active at cap marks unresolved transport; clipped marks an
388⍝│ unresolved exit inside a medium. Missing contributions remain black.
389ˡt̲race ← { scene clip environment origins directions mediumids sampleids controls →
390 world ← ʰc̲hecked scene
391 c ← ʰc̲lip clip
392 env ← ʰe̲nvironment environment
393 o ← 3 ʰr̲ows origins
394 d ← 3 ʰr̲ows directions
395 n̲ot (s̲hape o) m̲atch s̲hape d ? @ p̲anic< "Glass ray batches must match"
396 n̲ot '∧ r̲/ (ᵛl̲ength d) > 0.0 ? @ p̲anic< "Glass ray directions must be nonzero"
397 unit ← ᵛn̲ormalize d
398 n ← t̲ally o
399 med ← (n ʰi̲ds 0) mediumids
400 actual ← world ʰm̲ediums o
401 n̲ot med m̲atch actual ? @ p̲anic< "Glass supplied initial medium IDs do not match containment"
402 n̲ot (1 r̲eshape 2) m̲atch s̲hape controls ? @ p̲anic< "Glass controls require seed and cap"
403 control ← controls + 0 × controls m̲od 1
404 seed ← f̲irst control
405 cap ← f̲irst 1 d̲rop control
406 (cap < 0) ∨ cap > 64 ? @ p̲anic< "Glass interaction cap must be in 0..64"
407 checked ← ((seed ˡu̲niform 0) sampleids)
408 n̲ot n = t̲ally checked ? @ p̲anic< "Glass needs one sample ID per ray"
409 initial ← (o, unit, (n c̲at 3) r̲eshape 1.0, (n c̲at 3) r̲eshape 0.0, med, n r̲eshape 1, n r̲eshape 0, n r̲eshape 0.0, n r̲eshape 0, 0 m̲od 1)
410 sample̲ ← { bounce → ((seed ˡu̲niform bounce) sampleids) }
411 light̲ ← { points normals dirs bounce → ((((world ʰl̲ighting c) points) normals) dirs) }
412 step̲ ← { state → (((((world ʰs̲tep c) env) 'sample̲) 'light̲) state) }
413 (no, nd, beta, result, medium, active, counts, _, clipped, _) ← cap 'step̲ p̲ower initial
414 (result, no, nd, beta, medium, active, counts, clipped)
415}
416
417⍝│ Same arguments as t_race, returning N by 3 linear radiance only.
418⍝│ Direct diffuse point lighting, sampled mirror/refraction and emission;
419⍝│ glass blocks point-light shadow rays; no caustic approximation is used.
420ˡr̲ender ← { scene clip environment origins directions mediumids sampleids controls →
421 (radiance, _, _, _, _, _, _, _) ← (((((((scene ˡt̲race clip) environment) origins) directions) mediumids) sampleids) controls)
422 radiance
423}
424
425"r:" u̲se< "Random"
426
427⍝│ Cyclic, outward vertex order may rotate, but reversed winding cannot match.
428ʰf̲aceMatches ← { face mesh →
429 n ← t̲ally mesh
430 scale ← 1.0 m̲ax 'm̲ax r̲/ r̲avel a̲bs face
431 compare̲ ← { order →
432 expected ← (n c̲at 3 3) r̲eshape order s̲elect face
433 '∧ r̲/₂ (n c̲at 9) r̲eshape (a̲bs mesh − expected) ≤ 1e-10 × scale
434 }
435 (compare̲ 1 2 3) ∨ (compare̲ 2 3 1) ∨ compare̲ 3 1 2
436}
437
438⍝│ ((scene p_reflightArea emitters)_ radiance): validated world plus E
439⍝│ rectangles and E by 3 radiance. Each descriptor must match exactly one
440⍝│ pair of outward opaque triangles with that material emission. Duplicate
441⍝│ descriptors, stale transforms, wrong radiance and winding refuse.
442ˡp̲reflightArea ← { scene emitters radiance →
443 world ← ʰc̲hecked scene
444 ((triangles, _), (_, _, _, _, mids, _), (_, kinds, _, emission, _), _, _, _) ← world
445 e ← ˢᶜe̲mitters emitters
446 rgb ← ˢᶜe̲mission radiance
447 n̲ot (t̲ally e) = t̲ally rgb ? @ p̲anic< "Glass requires one radiance row per emitter"
448 perface ← mids s̲elect emission
449 tags ← mids s̲elect kinds
450 matches ← '{ i →
451 rect ← i s̲elect e
452 faces ← ˢᶜr̲ectangle rect
453 light ← i s̲elect rgb
454 same ← '∧ r̲/₂ (a̲bs perface − (t̲ally triangles) ʰb̲roadcast light) ≤ 1e-10 × 1.0 m̲ax 'm̲ax r̲/ light
455 first ← w̲here (tags = 0) ∧ same ∧ (1 s̲elect faces) ʰf̲aceMatches triangles
456 second ← w̲here (tags = 0) ∧ same ∧ (2 s̲elect faces) ʰf̲aceMatches triangles
457 n̲ot (1 = t̲ally first) ∧ 1 = t̲ally second ? @ p̲anic< "Glass emitter must match exactly two outward opaque emissive triangles and their radiance"
458 (f̲irst first) c̲at f̲irst second
459 } m̲ap r̲ange t̲ally e
460 flat ← { @ → 0 = t̲ally matches ? 0 r̲eshape 0◆ r̲avel d̲isclose '{ e̲nclose (r̲avel d̲isclose ⍺) c̲at r̲avel d̲isclose ⍵ } r̲/ matches } @
461 n̲ot (t̲ally flat) = t̲ally u̲nique flat ? @ p̲anic< "Glass emitter descriptors must not reuse visible triangles"
462 (world, e, rgb)
463}
464
465⍝│ Revalidate an area bundle from original arrays and world geometry.
466ʰa̲reaChecked ← { area →
467 (world, emitters, radiance) ← area
468 (world ˡp̲reflightArea emitters) radiance
469}
470
471⍝│ One uniform area sample per emitter/ray. Result is outgoing Lambertian
472⍝│ radiance for unit albedo: Le*cos(surface)*cos(emitter)/(pi*d^2*pdfArea).
473⍝│ Shadow rays cover the entire finite light segment and glass blocks them.
474ʰa̲reaLight ← { area clip points normals squares →
475 (scene, emitters, radiance) ← area
476 ((triangles, spheres), (tids, sids, _, _, _, _), _, _, _, _) ← scene
477 n ← t̲ally points
478 0 = n ? 0 3 r̲eshape 0.0
479 en ← ˢᶜe̲mitterNormals emitters
480 step̲ ← { state →
481 (k, accum) ← state
482 rect ← k s̲elect emitters
483 uv ← k s̲elect squares
484 (sampled, pdf) ← rect ˢᶜe̲mitterPoints uv
485 delta ← sampled − points
486 distance ← ᵛl̲ength delta
487 wi ← ᵛn̲ormalize delta
488 cosine ← 0.0 m̲ax normals ᵛd̲ot wi
489 lightcos ← 0.0 m̲ax (n ʰb̲roadcast k s̲elect en) ᵛd̲ot n̲eg wi
490 offset ← f̲irst 3 d̲rop clip
491 origins ← points + offset × normals
492 shadow ← sampled − origins
493 shadowDistance ← ᵛl̲ength shadow
494 dirs ← (ᵛn̲ormalize shadow) − (f̲loat shadowDistance = 0.0) ʰs̲cale normals
495 far ← ʰf̲inite (f̲irst 1 d̲rop clip) m̲ax offset + 'm̲ax r̲/ shadowDistance
496 shadowClip ← (f̲irst clip) c̲at far c̲at 2 d̲rop clip
497 ids ← ((((((shadowClip ʳᵗm̲ixedNearest triangles) spheres) tids) sids) origins) dirs)
498 hits ← (((((((shadowClip ʳᵗm̲ixedHits triangles) spheres) tids) sids) ids) origins) dirs)
499 visible ← (ids = 0) ∨ (ʳᵗd̲istances hits) ≥ shadowDistance − offset
500 safe ← distance + f̲loat distance = 0.0
501 weight ← (f̲loat visible ∧ distance > 0.0) × (cosine × lightcos) ÷ (p̲i @) × safe × safe × pdf
502 (k + 1, accum + weight ʰs̲cale n ʰb̲roadcast k s̲elect radiance)
503 }
504 (_, result) ← (t̲ally emitters) 'step̲ p̲ower (1, (n c̲at 3) r̲eshape 0.0)
505 ʰf̲inite result
506}
507
508⍝│ ((((area a_reaLight clip)_ points)_ normals)_ squares): supplied E by N
509⍝│ by 2 unit-square samples to N by 3 unit-albedo Lambertian radiance.
510⍝│ Exposes the same estimator used by the renderer for CPU/GPU array inputs.
511ˡa̲reaLight ← { area clip points normals squares →
512 checked ← ʰa̲reaChecked area
513 (_, emitters, _) ← checked
514 c ← ʰc̲lip clip
515 p ← 3 ʰr̲ows points
516 ns ← ʰu̲nit normals
517 n̲ot (s̲hape p) m̲atch s̲hape ns ? @ p̲anic< "Glass area-light points and normals must match"
518 n̲ot ((t̲ally emitters) c̲at (t̲ally p) c̲at 2) m̲atch s̲hape squares ? @ p̲anic< "Glass area samples require E by N by 2"
519 uv ← ʰf̲inite f̲loat squares
520 n̲ot '∧ r̲/ r̲avel (uv ≥ 0.0) ∧ uv ≤ 1.0 ? @ p̲anic< "Glass area samples must lie in the unit square"
521 ((((checked ʰa̲reaLight c) p) ns) uv)
522}
523
524⍝│ N by 2 [pixel,sample] plus scalar interaction/dimension to N by 4
525⍝│ Philox counters. Camera sampling reserves interaction word 4294967295.
526ʰc̲ounters ← { identities interaction dimension →
527 n ← t̲ally identities
528 o̲\ (o̲\ identities) c̲at ((1 c̲at n) r̲eshape interaction) c̲at (1 c̲at n) r̲eshape dimension
529}
530
531⍝│ Build one N by 2 uniform sample matrix for each emitter, using dimensions
532⍝│ 1+2*(emitter-1), 2+2*(emitter-1). Dimension 0 is the dielectric lobe.
533ʰa̲reaSamples ← { seed identities interaction count →
534 n ← t̲ally identities
535 rows ← '{ k →
536 x ← seed ʳs̲amples ((identities ʰc̲ounters interaction) 1 + 2 × k − 1)
537 y ← seed ʳs̲amples ((identities ʰc̲ounters interaction) 2 + 2 × k − 1)
538 o̲\ ((1 c̲at n) r̲eshape x) c̲at (1 c̲at n) r̲eshape y
539 } m̲ap r̲ange count
540 flat ← { @ → count = 0 ? 0 r̲eshape 0.0◆ r̲avel d̲isclose '{ e̲nclose (r̲avel d̲isclose ⍺) c̲at r̲avel d̲isclose ⍵ } r̲/ rows } @
541 (count c̲at n c̲at 2) r̲eshape flat
542}
543
544⍝│ Same diagnostics as t_race, using an area bundle, N by 2 Int
545⍝│ [pixel,sample] identities and controls [uint32 seed,cap 0..64]. Philox
546⍝│ supplies independent counter dimensions for lobes and area-light points.
547ˡt̲raceArea ← { area clip environment origins directions mediumids identities controls →
548 checked ← ʰa̲reaChecked area
549 (world, emitters, _) ← checked
550 c ← ʰc̲lip clip
551 env ← ʰe̲nvironment environment
552 o ← 3 ʰr̲ows origins
553 d ← 3 ʰr̲ows directions
554 n̲ot (s̲hape o) m̲atch s̲hape d ? @ p̲anic< "Glass ray batches must match"
555 n̲ot '∧ r̲/ (ᵛl̲ength d) > 0.0 ? @ p̲anic< "Glass ray directions must be nonzero"
556 unit ← ᵛn̲ormalize d
557 n ← t̲ally o
558 med ← (n ʰi̲ds 0) mediumids
559 n̲ot med m̲atch world ʰm̲ediums o ? @ p̲anic< "Glass supplied initial medium IDs do not match containment"
560 n̲ot (1 r̲eshape 2) m̲atch s̲hape controls ? @ p̲anic< "Glass controls require seed and cap"
561 control ← controls + 0 × controls m̲od 1
562 seed ← f̲irst control
563 cap ← f̲irst 1 d̲rop control
564 (cap < 0) ∨ cap > 64 ? @ p̲anic< "Glass interaction cap must be in 0..64"
565 n̲ot (n c̲at 2) m̲atch s̲hape identities ? @ p̲anic< "Glass sample identities require N by 2 pixel/sample words"
566 validation ← seed ʳs̲ampleKeys ((identities ʰc̲ounters 0) 0)
567 initial ← (o, unit, (n c̲at 3) r̲eshape 1.0, (n c̲at 3) r̲eshape 0.0, med, n r̲eshape 1, n r̲eshape 0, n r̲eshape 0.0, n r̲eshape 0, 0 m̲od 1)
568 sample̲ ← { bounce → seed ʳs̲amples ((identities ʰc̲ounters bounce) 0) }
569 light̲ ← { points normals dirs bounce →
570 uv ← (((seed ʰa̲reaSamples identities) bounce) t̲ally emitters)
571 point ← ((((world ʰl̲ighting c) points) normals) dirs)
572 point + ((((checked ʰa̲reaLight c) points) normals) uv)
573 }
574 step̲ ← { state → (((((world ʰs̲tep c) env) 'sample̲) 'light̲) state) }
575 (no, nd, beta, result, medium, active, counts, _, clipped, _) ← cap 'step̲ p̲ower initial
576 (result, no, nd, beta, medium, active, counts, clipped)
577}
578
579⍝│ Same arguments as t_raceArea, returning N by 3 linear radiance only.
580ˡr̲enderArea ← { area clip environment origins directions mediumids identities controls →
581 (radiance, _, _, _, _, _, _, _) ← (((((((area ˡt̲raceArea clip) environment) origins) directions) mediumids) identities) controls)
582 radiance
583}
p̲anic< expands to
(⎕P̲ANIC ("Glass volumes have overlapping or touching conservative bounds; nesting and interpenetration are unsupported"))
p̲anic< expands to
(⎕P̲ANIC ("Glass emitter must match exactly two outward opaque emissive triangles and their radiance"))