librarylibs/Glass/src/Glass.xtl
Glass: bounded dielectric transport with explicit medium identity. Import with "gl:" u_se< "Glass". Depends on Scene, RayTrace, Vector3 and Random. World-space v2 arrays remain separate, including every Int identity. Original array implementation of Snell, Fresnel and Beer-Lambert laws.
ʰf̲inite : Float -> Float
Reject nonfinite data and arithmetic overflow.
ʰf̲inite ← { a → n̲ot '∧ r̲/ r̲avel (a − a) = 0.0 ? @ p̲anic< "Glass requires finite numbers and results" a }
ʰr̲ows : Num a => Int -> a -> Float
Validate an N by columns matrix and convert it to Float.
ʰr̲ows ← { columns data → n̲ot ((t̲ally data) c̲at columns) m̲atch s̲hape data ? @ p̲anic< "Glass matrix has the wrong row width" ʰf̲inite f̲loat data }
ʰv̲ector : Num a => Int -> a -> Float
Exactly n finite numeric vector entries, converted to Float.
ʰv̲ector ← { n data → n̲ot (1 r̲eshape n) m̲atch s̲hape data ? @ p̲anic< "Glass vector has the wrong length" ʰf̲inite f̲loat data }
ʰb̲roadcast : Int -> Float -> Float
Repeat one row n times.
ʰb̲roadcast ← { n row → (n r̲eshape 1.0) '× t̲able row }
ʰs̲cale : Float -> Float -> Float
Multiply xyz rows by a per-row coefficient vector.
ʰs̲cale ← { weights rows → rows × weights '× t̲able 1.0 1.0 1.0 }
ʰu̲nit : Num a => a -> Float
Require finite unit xyz rows, permitting only relative 1e-10 roundoff.
ʰu̲nit ← { data → a ← 3 ʰr̲ows data n̲ot '∧ r̲/ (a̲bs (ᵛl̲ength a) − 1.0) ≤ 1e-10 ? @ p̲anic< "Glass interface vectors must be unit length" a }
ˡi̲nterface : (Num a, Num b, Num c, Num d) => a -> b -> c -> d -> (Float, Float, Int, Float)
(((incidentIOR i_nterface transmittedIOR)_ normals)_ directions): (reflected xyz, transmitted xyz, valid-transmission Int mask, Fresnel). IORs are positive N-vectors; unit normals point against unit directions. TIR has zero transmitted direction, mask 0 and reflectance 1.
ˡi̲nterface ← { incidentIOR transmittedIOR normals directions → n ← ʰu̲nit normals d ← ʰu̲nit directions n̲ot (s̲hape n) m̲atch s̲hape d ? @ p̲anic< "Glass interface batches must match" ni ← (t̲ally n) ʰv̲ector incidentIOR nt ← (t̲ally n) ʰv̲ector transmittedIOR n̲ot '∧ r̲/ (ni > 0.0) ∧ nt > 0.0 ? @ p̲anic< "Glass IORs must be positive" cosine ← n̲eg n ᵛd̲ot d n̲ot '∧ r̲/ (cosine ≥ -1e-10) ∧ cosine ≤ 1.0 + 1e-10 ? @ p̲anic< "Glass interface normals must oppose incident directions" ci ← 0.0 m̲ax 1.0 m̲in cosine eta ← ni ÷ nt st2 ← ʰf̲inite (eta × eta) × 0.0 m̲ax 1.0 − ci × ci valid ← st2 ≤ 1.0 ct ← s̲qrt 0.0 m̲ax 1.0 − st2 transmitted ← (f̲loat valid) ʰs̲cale (eta ʰs̲cale d) + ((eta × ci) − ct) ʰs̲cale n ds ← ʰf̲inite (ni × ci) + nt × ct dp ← ʰf̲inite (nt × ci) + ni × ct rs ← ((ni × ci) − nt × ct) ÷ ds + f̲loat ds = 0.0 rp ← ((nt × ci) − ni × ct) ÷ dp + f̲loat dp = 0.0 raw ← 0.5 × (rs × rs) + rp × rp fr ← (f̲loat n̲ot valid) + (f̲loat valid ∧ ni ≠ nt) × raw n̲ot '∧ r̲/ (fr ≥ -1e-12) ∧ fr ≤ 1.0 + 1e-12 ? @ p̲anic< "Glass Fresnel result escaped its numeric tolerance" (n ʳᵗr̲eflect d, ʰf̲inite transmitted, f̲loor f̲loat valid, 0.0 m̲ax 1.0 m̲in ʰf̲inite fr) }
ˡa̲bsorb : (Num a, Num b) => a -> b -> Float
sigma a_bsorb distances: N by 3 absorption >=0 and N physical lengths >=0 to Beer-Lambert transmittance exp(-sigma*distance), N by 3 Float.
ˡa̲bsorb ← { sigma distances → a ← 3 ʰr̲ows sigma ds ← (t̲ally a) ʰv̲ector distances n̲ot ('∧ r̲/ r̲avel a ≥ 0.0) ∧ '∧ r̲/ ds ≥ 0.0 ? @ p̲anic< "Glass absorption and distances must be nonnegative" ʰf̲inite e̲xp n̲eg ds ʰs̲cale a }
ˡu̲niform : Int -> Int -> Int -> Float
((seed u_niform interaction)_ sampleids): deterministic N Float in (0,1). Keys are nonnegative Int <=2147483645; interaction is Int 0..63. Bounded modular products stay below Int64 overflow; no global RNG state.
ˡu̲niform ← { seed interaction sampleids → n̲ot 0 = t̲ally s̲hape seed ? @ p̲anic< "Glass seed must be scalar" n̲ot 0 = t̲ally s̲hape interaction ? @ p̲anic< "Glass interaction must be scalar" (seed < 0) ∨ seed > 2147483645 ? @ p̲anic< "Glass seed is out of range" (interaction < 0) ∨ interaction > 63 ? @ p̲anic< "Glass interaction is out of range" n̲ot 1 = t̲ally s̲hape sampleids ? @ p̲anic< "Glass sample IDs must be a vector" n̲ot '∧ r̲/ (sampleids ≥ 0) ∧ sampleids ≤ 2147483645 ? @ p̲anic< "Glass sample IDs are out of range" x ← 1 + (sampleids + seed) m̲od 2147483646 step̲ ← { state → (48271 × state) m̲od 2147483647 } z ← (interaction + 2) 'step̲ p̲ower x ((f̲loat z) + 0.5) ÷ 2147483647.0 }
ʰi̲ds : Int -> Int -> Int -> Int
Validate an Int vector with n entries and minimum allowed ID.
ʰi̲ds ← { n minimum ids → n̲ot (1 r̲eshape n) m̲atch s̲hape ids ? @ p̲anic< "Glass ID count does not match its primitive count" n̲ot '∧ r̲/ ids ≥ minimum ? @ p̲anic< "Glass ID is below its allowed minimum" ids + 0 × ids m̲od 1 }
ʰl̲ocal : Num a => a -> a -> Int
One-based vector lookup for known IDs; zero sentinel stays zero.
ʰl̲ocal ← { ids selected → '{ 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 }
ʰb̲ounds : (Num a, Eq b) => a -> a -> b -> b -> b -> a
Object bounds, using the same mesh/sphere arrays as actual intersection.
ʰb̲ounds ← { triangles spheres toids soids object → mesh ← (toids = object) r̲eplicate triangles balls ← (soids = object) r̲eplicate spheres 0 < t̲ally balls ? { @ → row ← r̲avel balls center ← 3 t̲ake row radius ← f̲irst 3 d̲rop row 2 3 r̲eshape (center − radius) c̲at center + radius } @ points ← ((3 × t̲ally mesh) c̲at 3) r̲eshape mesh 2 3 r̲eshape ('m̲in r̲/ points) c̲at 'm̲ax r̲/ points }
ʰs̲eparate : Truthy a => Float -> Int -> a
Require strict separation of conservative axis-aligned glass bounds.
ʰs̲eparate ← { bounds i → a ← i s̲elect bounds others ← bounds low ← 1 s̲elect₂ others high ← 2 s̲elect₂ others alo ← (t̲ally bounds) ʰb̲roadcast 1 s̲elect a ahi ← (t̲ally bounds) ʰb̲roadcast 2 s̲elect a gap ← (low − ahi) m̲ax alo − high separated ← '∨ r̲/₂ gap > 1e-10 '∧ r̲/ separated ∨ (r̲ange t̲ally bounds) = i }
ˡp̲reflight : (Num a, Num b, Num c, Num d, Num e, Num f) => (a, b) -> (Int, Int, Int, Int, Int, Int) -> (c, Int, d, e, f) -> ((Float, Float), (Int, Int, Int, Int, Int, Int), (Float, Int, Float, Float, Float), Int, Int, Float)
((geometry p_reflight links)_ shading): validated world scene. geometry=(triangles,spheres); links=(triangleids,sphereids, triangleobjectids,sphereobjectids,materialids,spherematerialids); shading=(materials,materialkind,optics,emission,lights). Returns (geometry,links,shading,glassobjectids,objectmaterialids,bounds). Glass volumes must be convex closed meshes or one sphere per object, with a single material and strictly disjoint conservative AABB bounds.
ˡp̲reflight ← { geometry links shading → (triangles, spheres) ← geometry (triangleids, sphereids, triangleobjectids, sphereobjectids, materialids, spherematerialids) ← links (materials, materialkind, optics, emission, lights) ← shading mesh ← ˢᶜt̲riangles triangles balls ← ˢᶜs̲pheres spheres t ← t̲ally mesh s ← t̲ally balls tids ← (t ʰi̲ds 1) triangleids sids ← (s ʰi̲ds 1) sphereids allids ← tids c̲at sids n̲ot (t̲ally allids) = t̲ally u̲nique allids ? @ p̲anic< "Glass global primitive IDs must be unique" toids ← (t ʰi̲ds 0) triangleobjectids soids ← (s ʰi̲ds 0) sphereobjectids mats ← ˢᶜm̲aterials materials kinds ← ˢᶜm̲aterialKinds materialkind op ← ˢᶜo̲ptics optics em ← ˢᶜe̲mission emission ls ← ˢᶜl̲ights lights m ← t̲ally mats n̲ot (m = t̲ally kinds) ∧ (m = t̲ally op) ∧ m = t̲ally em ? @ p̲anic< "Glass material array counts must match" tmids ← m ˢᶜi̲ds materialids smids ← m ˢᶜi̲ds spherematerialids n̲ot (t = t̲ally tmids) ∧ s = t̲ally smids ? @ p̲anic< "Glass needs one material ID per primitive" tk ← tmids s̲elect kinds sk ← smids s̲elect kinds n̲ot ('∧ r̲/ (tk = 0) ∨ toids > 0) ∧ '∧ r̲/ (sk = 0) ∨ soids > 0 ? @ p̲anic< "Glass dielectric primitives require positive object IDs" objects ← u̲nique ((tk = 1) r̲eplicate toids) c̲at (sk = 1) r̲eplicate soids omids ← '{ object → ti ← toids = object si ← soids = object mids ← (ti r̲eplicate tmids) c̲at si r̲eplicate smids n̲ot 1 = t̲ally u̲nique mids ? @ p̲anic< "Glass object must have a single material" faces ← ti r̲eplicate mesh bs ← si r̲eplicate balls (t̲ally bs) > 1 ? @ p̲anic< "Glass object ID may identify only one sphere" (0 < t̲ally bs) ∧ (0 < t̲ally faces) ? @ p̲anic< "Glass object cannot mix mesh and sphere geometry" _ ← { @ → 0 < t̲ally faces ? t̲ally ˢᶜc̲losed faces◆ 0 } @ f̲irst mids } e̲ach objects bb ← '{ object → ((((mesh ʰb̲ounds balls) toids) soids) object) } m̲ap objects 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 } @ bounds ← ʰf̲inite ((t̲ally objects) c̲at 2 3) r̲eshape flat 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" ((mesh, balls), (tids, sids, toids, soids, tmids, smids), (mats, kinds, op, em, ls), objects, omids, bounds) }
ʰc̲hecked : (Num a, Num b, Num c, Num d, Num e, Num f, Any g, Any h, Any i) => ((a, b), (Int, Int, Int, Int, Int, Int), (c, Int, d, e, f), g, h, i) -> ((Float, Float), (Int, Int, Int, Int, Int, Int), (Float, Int, Float, Float, Float), Int, Int, Float)
Revalidate the public immutable scene bundle rather than trusting caches.
ʰc̲hecked ← { scene → (geometry, links, shading, _, _, _) ← scene (geometry ˡp̲reflight links) shading }
ʰc̲ontains : (Any a, Any b, Eq c, Any d, Any e, Any f, Any g, Any h, Any i, Truthy j) => ((Float, Float), (a, b, c, c, d, e), f, g, h, i) -> Float -> c -> j
Initial containment for one validated convex glass object; boundary starts within relative 1e-10 refuse rather than guessing a medium.
ʰc̲ontains ← { scene origins object → ((triangles, spheres), (_, _, toids, soids, _, _), _, _, _, _) ← scene balls ← (soids = object) r̲eplicate spheres 0 < t̲ally balls ? { @ → row ← r̲avel balls radius ← f̲irst 3 d̲rop row distance ← ᵛl̲ength origins − (t̲ally origins) ʰb̲roadcast 3 t̲ake row '∨ r̲/ (a̲bs distance − radius) ≤ 1e-10 × radius ? @ p̲anic< "Glass initial origin lies on a glass boundary" distance < radius } @ mesh ← (toids = object) r̲eplicate triangles a ← 1 s̲elect₂ mesh normals ← ᵛn̲ormalize ((2 s̲elect₂ mesh) − a) ᵛc̲ross (3 s̲elect₂ mesh) − a points ← ((3 × t̲ally mesh) c̲at 3) r̲eshape mesh extent ← 'm̲ax r̲/ ('m̲ax r̲/ points) − 'm̲in r̲/ points eps ← 1e-10 × extent '{ i → delta ← ((t̲ally mesh) ʰb̲roadcast i s̲elect origins) − a values ← '+ r̲/₂ delta × normals inside ← '∧ r̲/ values < 0.0 boundary ← ('∧ r̲/ values ≤ eps) ∧ '∨ r̲/ (a̲bs values) ≤ eps boundary ? @ p̲anic< "Glass initial origin lies on a glass boundary" inside } e̲ach r̲ange t̲ally origins }
ʰm̲ediums : (Any a, Any b, Any c, Any d, Any e, Any f, Any g) => ((Float, Float), (a, b, Int, Int, c, d), e, Int, f, g) -> Float -> Int
One unique object ID per contained origin, zero for air.
ʰm̲ediums ← { scene origins → (_, _, _, objects, _, _) ← scene step̲ ← { state → k ← f̲irst state ids ← 1 d̲rop state object ← f̲irst k s̲elect objects inside ← ((scene ʰc̲ontains origins) object) (k + 1) c̲at ids + object × f̲loor f̲loat inside } 1 d̲rop (t̲ally objects) 'step̲ p̲ower 1 c̲at (t̲ally origins) r̲eshape 0 }
ˡm̲ediums : (Num a, Num b, Num c, Num d, Num e, Num f, Any g, Any h, Any i, Num j) => ((a, b), (Int, Int, Int, Int, Int, Int), (c, Int, d, e, f), g, h, i) -> j -> Int
scene m_ediums origins: N Int initial medium IDs from N by 3 origins. Reuses shared preflight; zero is air, positive IDs identify glass objects.
ˡm̲ediums ← { scene origins → world ← ʰc̲hecked scene o ← 3 ʰr̲ows origins world ʰm̲ediums o }
ʰl̲ocals : (Any a, Eq b, Any c, Any d, Any e, Any f, Any g, Any h, Any i, Any j) => (a, (b, b, c, d, e, f), g, h, i, j) -> b -> (Int, Int)
Global selected IDs to separate local triangle and sphere indices.
ʰl̲ocals ← { scene selected → (_, (tids, sids, _, _, _, _), _, _, _, _) ← scene ti ← '{ id → at ← w̲here tids = id◆ 0 = t̲ally at ? 0◆ f̲irst at } e̲ach selected si ← '{ id → at ← w̲here sids = id◆ 0 = t̲ally at ? 0◆ f̲irst at } e̲ach selected (ti, si) }
ʰs̲elected : Num a => (a, a) -> (Int, Int) -> a
Paired per-primitive Int arrays to one selected Int vector, zero miss.
ʰs̲elected ← { arrays locals → (ta, sa) ← arrays (ti, si) ← locals ((ti + 1) s̲elect 0 c̲at ta) + (si + 1) s̲elect 0 c̲at sa }
ʰn̲ormals : (Any a, Any b, Any c, Any d, Any e) => ((Float, Float), a, b, c, d, e) -> (Int, Int) -> Float -> Float
Outward geometric normals, preserving entry/exit information.
ʰn̲ormals ← { scene locals points → ((triangles, spheres), _, _, _, _, _) ← scene (ti, si) ← locals selected ← (ti + 1) s̲elect (1 3 3 r̲eshape 0.0) c̲at triangles a ← 1 s̲elect₂ selected tn ← ᵛn̲ormalize ((2 s̲elect₂ selected) − a) ᵛc̲ross (3 s̲elect₂ selected) − a balls ← (si + 1) s̲elect (1 4 r̲eshape 0.0 0.0 0.0 1.0) c̲at spheres sn ← (f̲loat si > 0) ʰs̲cale ᵛn̲ormalize points − 3 t̲ake₂ balls tn + sn }
ʰc̲lip : Num a => a -> Float
Clip entries retain the v1 meanings and positive near/far interval.
ʰc̲lip ← { clip → c ← 4 ʰv̲ector clip n̲ot '∧ r̲/ c > 0.0 ? @ p̲anic< "Glass clip entries must be positive" (f̲irst c) ≥ f̲irst 1 d̲rop c ? @ p̲anic< "Glass near must be less than far" (f̲irst 2 d̲rop c) ≥ 1.0 ? @ p̲anic< "Glass intersection epsilon must be less than one" c }
ʰe̲nvironment : Num a => a -> Float
Nonnegative ambient/background rows, retaining v1 environment units.
ʰe̲nvironment ← { environment → n̲ot 2 3 m̲atch s̲hape environment ? @ p̲anic< "Glass environment requires ambient and background RGB rows" e ← ʰf̲inite f̲loat environment n̲ot '∧ r̲/ r̲avel e ≥ 0.0 ? @ p̲anic< "Glass environment must be nonnegative" e }
ʰf̲ields : a -> a -> Int -> a
Select Float rows with an explicit zero/default first row for ID zero.
ʰf̲ields ← { rows padding ids → (ids + 1) s̲elect padding c̲at rows }
ʰl̲ighting : (Num a, Num b, Any c, Any d, Any e, Any f, Any g, Any h, Any i, Any j, Any k, Any l, Any m) => ((a, b), (Int, Int, c, d, e, f), (g, h, i, j, Float), k, l, m) -> Float -> Float -> Float -> Float -> Float
Direct Lambertian point lighting with conservative opaque visibility: glass also blocks this shadow ray; no refracted caustic approximation.
ʰl̲ighting ← { scene clip points normals directions → ((triangles, spheres), (tids, sids, _, _, _, _), (_, _, _, _, lights), _, _, _) ← scene n ← t̲ally points step̲ ← { state → (k, accum) ← state light ← k s̲elect lights origins ← points + (f̲irst 3 d̲rop clip) × normals delta ← (n ʰb̲roadcast 3 t̲ake light) − origins distance ← ᵛl̲ength delta dirs ← (ᵛn̲ormalize delta) + (f̲loat distance = 0.0) ʰs̲cale directions selected ← ((((((clip ʳᵗm̲ixedNearest triangles) spheres) tids) sids) origins) dirs) hits ← (((((((clip ʳᵗm̲ixedHits triangles) spheres) tids) sids) selected) origins) dirs) visible ← (selected = 0) ∨ (ʳᵗd̲istances hits) ≥ distance − f̲irst 3 d̲rop clip cosine ← 0.0 m̲ax normals ᵛd̲ot dirs safe ← distance + f̲loat distance = 0.0 weight ← (f̲loat visible ∧ distance > 0.0) × cosine ÷ (p̲i @) × safe × safe (k + 1, accum + weight ʰs̲cale n ʰb̲roadcast 3 d̲rop light) } (_, result) ← (t̲ally lights) 'step̲ p̲ower (1, (n c̲at 3) r̲eshape 0.0) result }
ʰs̲tep : (Num a, Any b, Any c, Num d) => ((Float, Float), (Int, Int, Int, Int, Int, Int), (Float, a, Float, Float, b), Int, Int, c) -> Float -> Float -> (d -> Float) -> (Float -> Float -> Float -> d -> Float) -> (Float, Float, Float, Float, Int, Int, Int, Float, Int, d) -> (Float, Float, Float, Float, Int, Int, Int, Float, Int, d)
One bounded sampled continuation per active ray; tuple members keep Int identities/masks/counts separate from Float geometric/radiance state.
ʰs̲tep ← { scene clip environment sample̲ light̲ state → (origins, directions, beta, radiance, medium, active, counts, carry, clipped, bounce) ← state 0 = '+ r̲/ active ? state ((triangles, spheres), (tids, sids, toids, soids, tmids, smids), (materials, kinds, optics, emission, _), objects, omids, _) ← scene n ← t̲ally origins ids ← ((((((clip ʳᵗm̲ixedNearest triangles) spheres) tids) sids) origins) directions) hits ← (((((((clip ʳᵗm̲ixedHits triangles) spheres) tids) sids) ids) origins) directions) hit ← active × ʳᵗm̲ask hits miss ← active × f̲loor f̲loat ids = 0 local ← scene ʰl̲ocals ids object ← (toids, soids) ʰs̲elected local mid ← (tmids, smids) ʰs̲elected local surface ← ((materials ʰf̲ields 1 4 r̲eshape 0.0) mid) op ← ((optics ʰf̲ields 1 5 r̲eshape 0.0 1.0 0.0 0.0 0.0) mid) emitted ← ((emission ʰf̲ields 1 3 r̲eshape 0.0) mid) kind ← (mid + 1) s̲elect 0 c̲at kinds mediumlocal ← objects ʰl̲ocal medium mediummat ← (mediumlocal + 1) s̲elect 0 c̲at omids insideop ← ((optics ʰf̲ields 1 5 r̲eshape 0.0 1.0 0.0 0.0 0.0) mediummat) distance ← ʳᵗd̲istances hits attenuation ← (2 d̲rop₂ insideop) ˡa̲bsorb (f̲loat hit) × distance + carry attenuated ← beta × attenuation points ← origins + distance ʰs̲cale directions outward ← ((scene ʰn̲ormals local) points) dot ← outward ᵛd̲ot directions front ← dot ≤ 0.0 grazing ← (a̲bs dot) ≤ 1e-10 normal ← ((1.0 − 2.0 × f̲loat n̲ot front) ʰs̲cale outward) − (f̲loat ids = 0) ʰs̲cale directions glass ← (hit = 1) ∧ kind = 1 badentry ← glass ∧ front ∧ (n̲ot grazing) ∧ medium ≠ 0 badexit ← glass ∧ (n̲ot front) ∧ (n̲ot grazing) ∧ medium ≠ object '∨ r̲/ badentry ∨ badexit ? @ p̲anic< "Glass medium identity disagrees with geometric entry or exit" ni ← 2 s̲elect₂ insideop target ← ((f̲loat front) × (2 s̲elect₂ op)) + f̲loat n̲ot front nt ← ((f̲loat kind = 1) × target) + (f̲loat kind = 0) × ni (reflected, transmitted, valid, fresnel) ← (((ni ˡi̲nterface nt) normal) directions) w ← (f̲loat kind = 1) × 1 s̲elect₂ op r ← 4 s̲elect₂ surface diffuse ← (1.0 − w) × 1.0 − r mirror ← ((1.0 − w) × r) + w × fresnel trans ← w × (1.0 − fresnel) × f̲loat valid continuation ← mirror + trans safe ← continuation + f̲loat continuation = 0.0 xi ← sample̲ bounce reflect ← xi < mirror ÷ safe transmit ← (n̲ot reflect) ∧ valid = 1 ⍝ Parenthesized mixture: the selected lobe's weight divided by its PDF. gain ← continuation × ((f̲loat reflect) + (f̲loat transmit) × (ni ÷ nt) ^ 2.0) illum ← (n ʰb̲roadcast 1 s̲elect environment) + (((light̲ points) normal) directions) bounce direct ← ((f̲loat front) ʰs̲cale emitted) + diffuse ʰs̲cale (3 t̲ake₂ surface) × illum nextL ← radiance + ((f̲loat hit) ʰs̲cale attenuated × direct) + (f̲loat miss ∧ medium = 0) ʰs̲cale beta × n ʰb̲roadcast 2 s̲elect environment nextActive ← hit × f̲loor f̲loat continuation > 0.0 nextBeta ← beta + (f̲loat hit) ʰs̲cale (gain ʰs̲cale attenuated) − beta nextDirection ← ᵛn̲ormalize ((f̲loat reflect) ʰs̲cale reflected) + (f̲loat transmit) ʰs̲cale transmitted nextD ← directions + (f̲loat nextActive) ʰs̲cale nextDirection − directions offset ← f̲irst 3 d̲rop clip nextO ← origins + (f̲loat nextActive) ʰs̲cale (points + offset × nextD) − origins transition ← hit × f̲loor f̲loat (kind = 1) ∧ transmit ∧ n̲ot grazing destination ← object × f̲loor f̲loat front nextMedium ← medium + transition × destination − medium nextCarry ← offset × f̲loat nextActive nextClipped ← clipped m̲ax miss × f̲loor f̲loat medium ≠ 0 (ʰf̲inite nextO, ʰf̲inite nextD, ʰf̲inite nextBeta, ʰf̲inite nextL, nextMedium, nextActive, counts + hit, nextCarry, nextClipped, bounce + 1) }
ˡt̲race : (Num a, Num b, Num c, Num d, Num e, Num f, Any g, Any h, Any i, Num j, Num k, Num l, Num m) => ((a, b), (Int, Int, Int, Int, Int, Int), (c, Int, d, e, f), g, h, i) -> j -> k -> l -> m -> Int -> Int -> Int -> (Float, Float, Float, Float, Int, Int, Int, Int)
(((((((scene t_race clip)_ environment)_ origins)_ directions)_ mediumids) _ sampleids)_ controls): bounded radiance-mode path diagnostics. controls is Int [seed,cap], cap 0..64. Initial directions normalize. Result=(radiance,origins,directions,throughput,mediumids,active,counts, clipped). Active at cap marks unresolved transport; clipped marks an unresolved exit inside a medium. Missing contributions remain black.
ˡt̲race ← { scene clip environment origins directions mediumids sampleids controls → world ← ʰc̲hecked scene c ← ʰc̲lip clip env ← ʰe̲nvironment environment o ← 3 ʰr̲ows origins d ← 3 ʰr̲ows directions n̲ot (s̲hape o) m̲atch s̲hape d ? @ p̲anic< "Glass ray batches must match" n̲ot '∧ r̲/ (ᵛl̲ength d) > 0.0 ? @ p̲anic< "Glass ray directions must be nonzero" unit ← ᵛn̲ormalize d n ← t̲ally o med ← (n ʰi̲ds 0) mediumids actual ← world ʰm̲ediums o n̲ot med m̲atch actual ? @ p̲anic< "Glass supplied initial medium IDs do not match containment" n̲ot (1 r̲eshape 2) m̲atch s̲hape controls ? @ p̲anic< "Glass controls require seed and cap" control ← controls + 0 × controls m̲od 1 seed ← f̲irst control cap ← f̲irst 1 d̲rop control (cap < 0) ∨ cap > 64 ? @ p̲anic< "Glass interaction cap must be in 0..64" checked ← ((seed ˡu̲niform 0) sampleids) n̲ot n = t̲ally checked ? @ p̲anic< "Glass needs one sample ID per ray" 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) sample̲ ← { bounce → ((seed ˡu̲niform bounce) sampleids) } light̲ ← { points normals dirs bounce → ((((world ʰl̲ighting c) points) normals) dirs) } step̲ ← { state → (((((world ʰs̲tep c) env) 'sample̲) 'light̲) state) } (no, nd, beta, result, medium, active, counts, _, clipped, _) ← cap 'step̲ p̲ower initial (result, no, nd, beta, medium, active, counts, clipped) }
ˡr̲ender : (Num a, Num b, Num c, Num d, Num e, Num f, Any g, Any h, Any i, Num j, Num k, Num l, Num m) => ((a, b), (Int, Int, Int, Int, Int, Int), (c, Int, d, e, f), g, h, i) -> j -> k -> l -> m -> Int -> Int -> Int -> Float
Same arguments as t_race, returning N by 3 linear radiance only. Direct diffuse point lighting, sampled mirror/refraction and emission; glass blocks point-light shadow rays; no caustic approximation is used.
ˡr̲ender ← { scene clip environment origins directions mediumids sampleids controls → (radiance, _, _, _, _, _, _, _) ← (((((((scene ˡt̲race clip) environment) origins) directions) mediumids) sampleids) controls) radiance }
ʰf̲aceMatches : Truthy a => Float -> Float -> a
Cyclic, outward vertex order may rotate, but reversed winding cannot match.
ʰf̲aceMatches ← { face mesh → n ← t̲ally mesh scale ← 1.0 m̲ax 'm̲ax r̲/ r̲avel a̲bs face compare̲ ← { order → expected ← (n c̲at 3 3) r̲eshape order s̲elect face '∧ r̲/₂ (n c̲at 9) r̲eshape (a̲bs mesh − expected) ≤ 1e-10 × scale } (compare̲ 1 2 3) ∨ (compare̲ 2 3 1) ∨ compare̲ 3 1 2 }
ˡp̲reflightArea : (Num a, Num b, Num c, Num d, Num e, Num f, Any g, Any h, Any i, Num j, Num k) => ((a, b), (Int, Int, Int, Int, Int, Int), (c, Int, d, e, f), g, h, i) -> j -> k -> (((Float, Float), (Int, Int, Int, Int, Int, Int), (Float, Int, Float, Float, Float), Int, Int, Float), Float, Float)
((scene p_reflightArea emitters)_ radiance): validated world plus E rectangles and E by 3 radiance. Each descriptor must match exactly one pair of outward opaque triangles with that material emission. Duplicate descriptors, stale transforms, wrong radiance and winding refuse.
ˡp̲reflightArea ← { scene emitters radiance → world ← ʰc̲hecked scene ((triangles, _), (_, _, _, _, mids, _), (_, kinds, _, emission, _), _, _, _) ← world e ← ˢᶜe̲mitters emitters rgb ← ˢᶜe̲mission radiance n̲ot (t̲ally e) = t̲ally rgb ? @ p̲anic< "Glass requires one radiance row per emitter" perface ← mids s̲elect emission tags ← mids s̲elect kinds matches ← '{ i → rect ← i s̲elect e faces ← ˢᶜr̲ectangle rect light ← i s̲elect rgb same ← '∧ r̲/₂ (a̲bs perface − (t̲ally triangles) ʰb̲roadcast light) ≤ 1e-10 × 1.0 m̲ax 'm̲ax r̲/ light first ← w̲here (tags = 0) ∧ same ∧ (1 s̲elect faces) ʰf̲aceMatches triangles second ← w̲here (tags = 0) ∧ same ∧ (2 s̲elect faces) ʰf̲aceMatches triangles 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" (f̲irst first) c̲at f̲irst second } m̲ap r̲ange t̲ally e 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 } @ n̲ot (t̲ally flat) = t̲ally u̲nique flat ? @ p̲anic< "Glass emitter descriptors must not reuse visible triangles" (world, e, rgb) }
p̲anic< expands to
(⎕P̲ANIC ("Glass emitter must match exactly two outward opaque emissive triangles and their radiance"))
ʰa̲reaChecked : (Num a, Num b, Num c, Num d, Num e, Num f, Any g, Any h, Any i, Num j, Num k) => (((a, b), (Int, Int, Int, Int, Int, Int), (c, Int, d, e, f), g, h, i), j, k) -> (((Float, Float), (Int, Int, Int, Int, Int, Int), (Float, Int, Float, Float, Float), Int, Int, Float), Float, Float)
Revalidate an area bundle from original arrays and world geometry.
ʰa̲reaChecked ← { area → (world, emitters, radiance) ← area (world ˡp̲reflightArea emitters) radiance }
ʰa̲reaLight : (Num a, Num b, Any c, Any d, Any e, Any f, Any g, Any h, Any i, Any j, Num k, Num l) => (((a, b), (Int, Int, c, d, e, f), g, h, i, j), k, Float) -> Float -> Float -> Float -> l -> Float
One uniform area sample per emitter/ray. Result is outgoing Lambertian radiance for unit albedo: Le*cos(surface)*cos(emitter)/(pi*d^2*pdfArea). Shadow rays cover the entire finite light segment and glass blocks them.
ʰa̲reaLight ← { area clip points normals squares → (scene, emitters, radiance) ← area ((triangles, spheres), (tids, sids, _, _, _, _), _, _, _, _) ← scene n ← t̲ally points 0 = n ? 0 3 r̲eshape 0.0 en ← ˢᶜe̲mitterNormals emitters step̲ ← { state → (k, accum) ← state rect ← k s̲elect emitters uv ← k s̲elect squares (sampled, pdf) ← rect ˢᶜe̲mitterPoints uv delta ← sampled − points distance ← ᵛl̲ength delta wi ← ᵛn̲ormalize delta cosine ← 0.0 m̲ax normals ᵛd̲ot wi lightcos ← 0.0 m̲ax (n ʰb̲roadcast k s̲elect en) ᵛd̲ot n̲eg wi offset ← f̲irst 3 d̲rop clip origins ← points + offset × normals shadow ← sampled − origins shadowDistance ← ᵛl̲ength shadow dirs ← (ᵛn̲ormalize shadow) − (f̲loat shadowDistance = 0.0) ʰs̲cale normals far ← ʰf̲inite (f̲irst 1 d̲rop clip) m̲ax offset + 'm̲ax r̲/ shadowDistance shadowClip ← (f̲irst clip) c̲at far c̲at 2 d̲rop clip ids ← ((((((shadowClip ʳᵗm̲ixedNearest triangles) spheres) tids) sids) origins) dirs) hits ← (((((((shadowClip ʳᵗm̲ixedHits triangles) spheres) tids) sids) ids) origins) dirs) visible ← (ids = 0) ∨ (ʳᵗd̲istances hits) ≥ shadowDistance − offset safe ← distance + f̲loat distance = 0.0 weight ← (f̲loat visible ∧ distance > 0.0) × (cosine × lightcos) ÷ (p̲i @) × safe × safe × pdf (k + 1, accum + weight ʰs̲cale n ʰb̲roadcast k s̲elect radiance) } (_, result) ← (t̲ally emitters) 'step̲ p̲ower (1, (n c̲at 3) r̲eshape 0.0) ʰf̲inite result }
ˡa̲reaLight : (Num a, Num b, Num c, Num d, Num e, Num f, Any g, Any h, Any i, Num j, Num k, Num l, Num m, Num n, Num o) => (((a, b), (Int, Int, Int, Int, Int, Int), (c, Int, d, e, f), g, h, i), j, k) -> l -> m -> n -> o -> Float
((((area a_reaLight clip)_ points)_ normals)_ squares): supplied E by N by 2 unit-square samples to N by 3 unit-albedo Lambertian radiance. Exposes the same estimator used by the renderer for CPU/GPU array inputs.
ˡa̲reaLight ← { area clip points normals squares → checked ← ʰa̲reaChecked area (_, emitters, _) ← checked c ← ʰc̲lip clip p ← 3 ʰr̲ows points ns ← ʰu̲nit normals n̲ot (s̲hape p) m̲atch s̲hape ns ? @ p̲anic< "Glass area-light points and normals must match" 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" uv ← ʰf̲inite f̲loat squares n̲ot '∧ r̲/ r̲avel (uv ≥ 0.0) ∧ uv ≤ 1.0 ? @ p̲anic< "Glass area samples must lie in the unit square" ((((checked ʰa̲reaLight c) p) ns) uv) }
ʰc̲ounters : a -> a -> a -> a
N by 2 [pixel,sample] plus scalar interaction/dimension to N by 4 Philox counters. Camera sampling reserves interaction word 4294967295.
ʰc̲ounters ← { identities interaction dimension → n ← t̲ally identities o̲\ (o̲\ identities) c̲at ((1 c̲at n) r̲eshape interaction) c̲at (1 c̲at n) r̲eshape dimension }
ʰa̲reaSamples : Int -> Int -> Int -> Int -> Float
Build one N by 2 uniform sample matrix for each emitter, using dimensions 1+2*(emitter-1), 2+2*(emitter-1). Dimension 0 is the dielectric lobe.
ʰa̲reaSamples ← { seed identities interaction count → n ← t̲ally identities rows ← '{ k → x ← seed ʳs̲amples ((identities ʰc̲ounters interaction) 1 + 2 × k − 1) y ← seed ʳs̲amples ((identities ʰc̲ounters interaction) 2 + 2 × k − 1) o̲\ ((1 c̲at n) r̲eshape x) c̲at (1 c̲at n) r̲eshape y } m̲ap r̲ange count 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 } @ (count c̲at n c̲at 2) r̲eshape flat }
ˡt̲raceArea : (Num a, Num b, Num c, Num d, Num e, Num f, Any g, Any h, Any i, Num j, Num k, Num l, Num m, Num n, Num o) => (((a, b), (Int, Int, Int, Int, Int, Int), (c, Int, d, e, f), g, h, i), j, k) -> l -> m -> n -> o -> Int -> Int -> Int -> (Float, Float, Float, Float, Int, Int, Int, Int)
Same diagnostics as t_race, using an area bundle, N by 2 Int [pixel,sample] identities and controls [uint32 seed,cap 0..64]. Philox supplies independent counter dimensions for lobes and area-light points.
ˡt̲raceArea ← { area clip environment origins directions mediumids identities controls → checked ← ʰa̲reaChecked area (world, emitters, _) ← checked c ← ʰc̲lip clip env ← ʰe̲nvironment environment o ← 3 ʰr̲ows origins d ← 3 ʰr̲ows directions n̲ot (s̲hape o) m̲atch s̲hape d ? @ p̲anic< "Glass ray batches must match" n̲ot '∧ r̲/ (ᵛl̲ength d) > 0.0 ? @ p̲anic< "Glass ray directions must be nonzero" unit ← ᵛn̲ormalize d n ← t̲ally o med ← (n ʰi̲ds 0) mediumids n̲ot med m̲atch world ʰm̲ediums o ? @ p̲anic< "Glass supplied initial medium IDs do not match containment" n̲ot (1 r̲eshape 2) m̲atch s̲hape controls ? @ p̲anic< "Glass controls require seed and cap" control ← controls + 0 × controls m̲od 1 seed ← f̲irst control cap ← f̲irst 1 d̲rop control (cap < 0) ∨ cap > 64 ? @ p̲anic< "Glass interaction cap must be in 0..64" n̲ot (n c̲at 2) m̲atch s̲hape identities ? @ p̲anic< "Glass sample identities require N by 2 pixel/sample words" validation ← seed ʳs̲ampleKeys ((identities ʰc̲ounters 0) 0) 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) sample̲ ← { bounce → seed ʳs̲amples ((identities ʰc̲ounters bounce) 0) } light̲ ← { points normals dirs bounce → uv ← (((seed ʰa̲reaSamples identities) bounce) t̲ally emitters) point ← ((((world ʰl̲ighting c) points) normals) dirs) point + ((((checked ʰa̲reaLight c) points) normals) uv) } step̲ ← { state → (((((world ʰs̲tep c) env) 'sample̲) 'light̲) state) } (no, nd, beta, result, medium, active, counts, _, clipped, _) ← cap 'step̲ p̲ower initial (result, no, nd, beta, medium, active, counts, clipped) }
ˡr̲enderArea : (Num a, Num b, Num c, Num d, Num e, Num f, Any g, Any h, Any i, Num j, Num k, Num l, Num m, Num n, Num o) => (((a, b), (Int, Int, Int, Int, Int, Int), (c, Int, d, e, f), g, h, i), j, k) -> l -> m -> n -> o -> Int -> Int -> Int -> Float
Same arguments as t_raceArea, returning N by 3 linear radiance only.
ˡr̲enderArea ← { area clip environment origins directions mediumids identities controls → (radiance, _, _, _, _, _, _, _) ← (((((((area ˡt̲raceArea clip) environment) origins) directions) mediumids) identities) controls) radiance }