librarylibs/RayTrace/src/RayTrace.xtl
RayTrace: triangle/sphere tracing, stable mixed IDs and opaque lighting. Import with "rt:" u_se< "RayTrace"; depends on Vector3 and Matrix. Right-handed xyz, y up, radians, N by 3 ray rows, T by 3 by 3 meshes. Original implementation of Moller-Trumbore and Lambertian equations.
ʰf̲inite : Float -> Float
Require finite inputs and computed results.
ʰf̲inite ← { a → n̲ot '∧ r̲/ r̲avel (a − a) = 0.0 ? @ p̲anic< "RayTrace requires finite numbers and results" a }
ʰv̲ector : Num a => Int -> a -> Float
A vector with exactly n items, converted to Float.
ʰv̲ector ← { n a → n̲ot (1 r̲eshape n) m̲atch s̲hape a ? @ p̲anic< "RayTrace requires a vector of the documented length" ʰf̲inite f̲loat a }
ʰb̲atch : Num a => a -> Float
A matrix of xyz rows, converted to Float.
ʰb̲atch ← { a → n̲ot ((t̲ally a) c̲at 3) m̲atch s̲hape a ? @ p̲anic< "RayTrace requires an N by 3 ray batch" ʰf̲inite f̲loat a }
ʰr̲ows : Int -> Float -> Float
Replicate a vector as n identical rows.
ʰr̲ows ← { n a → (n r̲eshape 1.0) '× t̲able a }
ʰs̲cale : Float -> Float -> Float
Scale xyz rows by one number per row.
ʰs̲cale ← { k a → a × k '× t̲able 1.0 1.0 1.0 }
ʰc̲lip : Num a => a -> Float
Near, far, relative intersection epsilon and surface offset.
ʰc̲lip ← { a → c ← 4 ʰv̲ector a n̲ot '∧ r̲/ c > 0.0 ? @ p̲anic< "RayTrace clip entries must be positive" (f̲irst c) ≥ f̲irst 1 d̲rop c ? @ p̲anic< "RayTrace near must be less than far" (f̲irst 2 d̲rop c) ≥ 1.0 ? @ p̲anic< "RayTrace intersection epsilon must be less than 1" c }
ʰc̲amera : Num a => a -> Float
Validate a camera's eye, target, up and fov; reject a collapsed basis.
ʰc̲amera ← { camera → c ← 10 ʰv̲ector camera forward ← (3 t̲ake 3 d̲rop c) − 3 t̲ake c up ← 3 t̲ake 6 d̲rop c 0.0 = f̲irst ᵛl̲ength 1 3 r̲eshape forward ? @ p̲anic< "RayTrace camera eye and target must differ" side ← (ᵛn̲ormalize 1 3 r̲eshape forward) ᵛc̲ross ᵛn̲ormalize 1 3 r̲eshape up 1e-12 ≥ f̲irst ᵛl̲ength side ? @ p̲anic< "RayTrace camera up must be nonzero and independent of view" fov ← f̲irst 9 d̲rop c (fov ≤ 0.0) ∨ fov ≥ p̲i @ ? @ p̲anic< "RayTrace camera fov must be between 0 and pi" c }
ˡo̲rigins : Num a => a -> Int -> Float
camera o_rigins count: count copies of the eye position.
ˡo̲rigins ← { camera count → n̲ot 0 = t̲ally s̲hape count ? @ p̲anic< "RayTrace ray count must be scalar" (count < 0) ∨ count ≠ f̲loor count ? @ p̲anic< "RayTrace ray count must be a nonnegative integer" count ʰr̲ows 3 t̲ake ʰc̲amera camera }
ˡr̲ays : (Num a, Num b) => a -> b -> Int -> Float
((camera r_ays size)_ pixels): normalized pinhole directions at pixel centers; size is width height and pixels are one-based flat row indices.
ˡr̲ays ← { camera size pixels → c ← ʰc̲amera camera sz ← 2 ʰv̲ector size n̲ot '∧ r̲/ (sz > 0.0) ∧ sz = f̲loat f̲loor sz ? @ p̲anic< "RayTrace image size must contain positive integers" n̲ot (1 = t̲ally s̲hape pixels) ∧ '∧ r̲/ (pixels ≥ 1) ∧ pixels ≤ f̲loor '× r̲/ sz ? @ p̲anic< "RayTrace pixel indices must be a vector within the image" n̲ot '∧ r̲/ pixels = f̲loor pixels ? @ p̲anic< "RayTrace pixel indices must be integers" w ← f̲irst sz ht ← f̲irst 1 d̲rop sz forward ← ᵛn̲ormalize 1 3 r̲eshape (3 t̲ake 3 d̲rop c) − 3 t̲ake c right ← ᵛn̲ormalize forward ᵛc̲ross 1 3 r̲eshape 3 t̲ake 6 d̲rop c up ← right ᵛc̲ross forward half ← 0.5 × f̲irst 9 d̲rop c tangent ← (s̲in half) ÷ c̲os half col ← (pixels − 1) m̲od f̲loor w row ← (pixels − 1) d̲iv f̲loor w xx ← tangent × (w ÷ ht) × ((2.0 × 0.5 + f̲loat col) ÷ w) − 1.0 yy ← tangent × 1.0 − (2.0 × 0.5 + f̲loat row) ÷ ht n ← t̲ally pixels ᵛn̲ormalize (n ʰr̲ows r̲avel forward) + (xx ʰs̲cale n ʰr̲ows r̲avel right) + yy ʰs̲cale n ʰr̲ows r̲avel up }
ʰm̲esh : Num a => a -> Float
Validate finite triangles, allowing degenerate faces (which always miss).
ʰm̲esh ← { triangles → n̲ot ((t̲ally triangles) c̲at 3 3) m̲atch s̲hape triangles ? @ p̲anic< "RayTrace requires a T by 3 by 3 triangle array" ʰf̲inite f̲loat triangles }
ʰp̲airs : (Num a, Num b) => Float -> a -> a -> b -> Float
Matched per-ray triangles, used for a broadcast face and selected hits. Original Moller-Trumbore equations with finite masked determinants.
ʰp̲airs ← { clip selected origins directions → n ← t̲ally origins a ← 1 s̲elect₂ selected e1 ← (2 s̲elect₂ selected) − a e2 ← (3 s̲elect₂ selected) − a p ← directions ᵛc̲ross e2 det ← e1 ᵛd̲ot p eps ← f̲irst 2 d̲rop clip bound ← eps × (ᵛl̲ength e1) × (ᵛl̲ength e2) × ᵛl̲ength directions good ← (a̲bs det) > bound denom ← (det × f̲loat good) + f̲loat n̲ot good tv ← origins − a u ← (tv ᵛd̲ot p) ÷ denom q ← tv ᵛc̲ross e1 vv ← (directions ᵛd̲ot q) ÷ denom t ← (e2 ᵛd̲ot q) ÷ denom ok ← good ∧ (u ≥ 0.0) ∧ (vv ≥ 0.0) ∧ ((u + vv) ≤ 1.0) ∧ (t ≥ f̲irst clip) ∧ t ≤ f̲irst 1 d̲rop clip ʰf̲inite (f̲loat ok) ʰs̲cale o̲\ ((1 c̲at n) r̲eshape t) c̲at ((1 c̲at n) r̲eshape u) c̲at (1 c̲at n) r̲eshape vv }
ʰi̲ntersect : (Num a, Num b) => Float -> a -> a -> b -> Float
One broadcast triangle against a ray batch, using the same pair solver.
ʰi̲ntersect ← { clip triangle origins directions → selected ← ((t̲ally origins) c̲at 3 3) r̲eshape triangle (((clip ʰp̲airs selected) origins) directions) }
ʰd̲irections : Num b => a -> b -> Float
Validate matching origin/direction batches, refusing zero directions.
ʰd̲irections ← { origins directions → d ← ʰb̲atch directions n̲ot (s̲hape origins) m̲atch s̲hape d ? @ p̲anic< "RayTrace origin and direction batches must match" n̲ot '∧ r̲/ (ᵛl̲ength d) > 0.0 ? @ p̲anic< "RayTrace directions must be nonzero" d }
ˡi̲ntersect : (Num a, Num b, Num c, Num d) => a -> b -> c -> d -> Float
(((clip i_ntersect triangle)_ origins)_ directions): N by 3 t,u,v; miss rows are zero, with an explicit Int mask available via m_ask.
ˡi̲ntersect ← { clip triangle origins directions → c ← ʰc̲lip clip n̲ot 3 3 m̲atch s̲hape triangle ? @ p̲anic< "RayTrace requires one 3 by 3 triangle" tri ← ʰf̲inite f̲loat triangle o ← ʰb̲atch origins d ← o ʰd̲irections directions (((c ʰi̲ntersect tri) o) d) }
ˡm̲ask : Num a => a -> Int
m_ask hits: N Int validity mask for an N by 3 t,u,v result.
ˡm̲ask ← { hits → f̲loor f̲loat (f̲irst₂ ʰb̲atch hits) > 0.0 }
ˡd̲istances : Num a => a -> Float
d_istances hits: N Float ray parameters, zero on a miss.
ˡd̲istances ← { hits → f̲irst₂ ʰb̲atch hits }
ʰb̲oxCandidates : Float -> Float -> Float -> Float -> Int
Conservative slab broad phase; parallel axes use the clip interval. Padding includes faces/edges despite roundoff; the original triangle solver still decides every candidate hit and exact declaration-order tie.
ʰb̲oxCandidates ← { clip triangle origins directions → n ← t̲ally origins lo ← 'm̲in r̲/ triangle hi ← 'm̲ax r̲/ triangle margin ← 1e-12 × 1.0 + 'm̲ax r̲/ (a̲bs lo) m̲ax a̲bs hi lower ← n ʰr̲ows lo − margin upper ← n ʰr̲ows hi + margin parallel ← directions = 0.0 safe ← directions + f̲loat parallel a ← (lower − origins) ÷ safe b ← (upper − origins) ÷ safe moving ← f̲loat n̲ot parallel low ← (moving × a m̲in b) + (f̲loat parallel) × f̲irst clip high ← (moving × a m̲ax b) + (f̲loat parallel) × f̲irst 1 d̲rop clip inside ← '∧ r̲/₂ (n̲ot parallel) ∨ (origins ≥ lower) ∧ origins ≤ upper first ← (f̲irst clip) m̲ax 'm̲ax r̲/₂ low last ← (f̲irst 1 d̲rop clip) m̲in 'm̲in r̲/₂ high f̲loor f̲loat inside ∧ first ≤ last }
ʰn̲earest : Float -> Float -> Float -> Float -> Int
Sweep exactly T primitives, with separate Int cursor/IDs and Float distance cache. Conservative boxes compact candidate rays; strict closer hits preserve declaration-order ties. Float selection avoids subtraction.
ʰn̲earest ← { clip triangles origins directions → n ← t̲ally origins step̲ ← { state → (k, ids, distances) ← state triangle ← k s̲elect triangles candidates ← (((clip ʰb̲oxCandidates triangle) origins) directions) n̲ot '∨ r̲/ candidates = 1 ? (k + 1, ids, distances) active ← candidates r̲eplicate ids old ← candidates r̲eplicate distances o ← candidates r̲eplicate origins d ← candidates r̲eplicate directions hit ← (((clip ʰi̲ntersect triangle) o) d) t ← ˡd̲istances hit choose ← (ˡm̲ask hit) ∧ ((active = 0) ∨ t < old) delta ← (f̲loor f̲loat choose) × k − active selectedT ← ((f̲loat choose) × t) + (f̲loat n̲ot choose) × old slots ← 1 + '+ s̲\ candidates next ← ids + candidates × slots s̲elect 0 c̲at delta cached ← ((f̲loat candidates) × slots s̲elect 0.0 c̲at selectedT) + (f̲loat candidates = 0) × distances (k + 1, next, cached) } (_, ids, _) ← (t̲ally triangles) 'step̲ p̲ower (1, n r̲eshape 0, n r̲eshape 0.0) ids }
ʰh̲its : (Num a, Num b) => Float -> a -> Int -> a -> b -> Float
Intersect selected IDs without indexing zero or materializing all pairs.
ʰh̲its ← { clip triangles ids origins directions → n ← t̲ally origins (0 = n) ∨ 0 = t̲ally triangles ? (n c̲at 3) r̲eshape 0.0 selected ← (ids m̲ax 1) s̲elect triangles (f̲loat ids > 0) ʰs̲cale (((clip ʰp̲airs selected) origins) directions) }
ˡn̲earest : (Num a, Num b, Num c, Num d) => a -> b -> c -> d -> Int
(((clip n_earest triangles)_ origins)_ directions): N Int triangle IDs, zero on a miss; t counts ray-direction units, not necessarily distance.
ˡn̲earest ← { clip triangles origins directions → c ← ʰc̲lip clip mesh ← ʰm̲esh triangles o ← ʰb̲atch origins d ← o ʰd̲irections directions (((c ʰn̲earest mesh) o) d) }
ˡh̲its : (Num a, Num b, Num c, Num d) => a -> b -> Int -> c -> d -> Float
((((clip h_its triangles)_ ids)_ origins)_ directions): selected hit data. IDs must come from n_earest for these same rays, triangles and clip.
ˡh̲its ← { clip triangles ids origins directions → c ← ʰc̲lip clip mesh ← ʰm̲esh triangles o ← ʰb̲atch origins d ← o ʰd̲irections directions n̲ot (s̲hape ids) m̲atch s̲hape f̲irst₂ o ? @ p̲anic< "RayTrace hit IDs must match the ray count" n̲ot '∧ r̲/ (ids = f̲loor ids) ∧ (ids ≥ 0) ∧ ids ≤ t̲ally mesh ? @ p̲anic< "RayTrace hit IDs are out of range" ((((c ʰh̲its mesh) ids) o) d) }
ʰn̲ormals : Num a => a -> Int -> Float
Unit geometric normals for selected triangles, zero for misses.
ʰn̲ormals ← { triangles ids → n ← t̲ally ids (0 = n) ∨ 0 = t̲ally triangles ? (n c̲at 3) r̲eshape 0.0 selected ← (ids m̲ax 1) s̲elect triangles a ← 1 s̲elect₂ selected (f̲loat ids > 0) ʰs̲cale ᵛn̲ormalize ((2 s̲elect₂ selected) − a) ᵛc̲ross (3 s̲elect₂ selected) − a }
ˡn̲ormals : (Num a, Num b) => a -> Int -> b -> Float
((triangles n_ormals ids)_ directions): selected unit normals oriented against incoming rays; zero for miss IDs. Winding supplies geometry.
ˡn̲ormals ← { triangles ids directions → mesh ← ʰm̲esh triangles d ← ʰb̲atch directions n̲ot (s̲hape ids) m̲atch s̲hape f̲irst₂ d ? @ p̲anic< "RayTrace normal IDs must match the ray count" n̲ot '∧ r̲/ (ids ≥ 0) ∧ ids ≤ t̲ally mesh ? @ p̲anic< "RayTrace normal IDs are out of range" n ← mesh ʰn̲ormals ids (1.0 − 2.0 × f̲loat (n ᵛd̲ot d) > 0.0) ʰs̲cale n }
ˡr̲eflect : (Num a, Num b) => a -> b -> Float
normals r_eflect directions: d - 2(d dot n)n for unit normals. Zero normals leave directions unchanged, for masked miss rows.
ˡr̲eflect ← { normals directions → n ← ʰb̲atch normals d ← ʰb̲atch directions len ← ᵛl̲ength n n̲ot '∧ r̲/ (len = 0.0) ∨ (a̲bs len − 1.0) ≤ 1e-10 ? @ p̲anic< "RayTrace reflection normals must be unit or zero" ʰf̲inite d − (2.0 × d ᵛd̲ot n) ʰs̲cale n }
ʰm̲aterials : Num a => a -> Float
Validate M by 4 linear RGB and reflection weights in 0..1.
ʰm̲aterials ← { materials → n̲ot ((t̲ally materials) c̲at 4) m̲atch s̲hape materials ? @ p̲anic< "RayTrace requires M by 4 materials" m ← ʰf̲inite f̲loat materials n̲ot '∧ r̲/ r̲avel (m ≥ 0.0) ∧ m ≤ 1.0 ? @ p̲anic< "RayTrace material RGB and reflection must be in 0..1" m }
ʰm̲aterialIds : a -> b -> Int -> Int
Validate one material index per triangle; the caller's Int array stays Int.
ʰm̲aterialIds ← { materials triangles ids → n̲ot (1 r̲eshape t̲ally triangles) m̲atch s̲hape ids ? @ p̲anic< "RayTrace requires one material ID per triangle" n̲ot '∧ r̲/ (ids ≥ 1) ∧ ids ≤ t̲ally materials ? @ p̲anic< "RayTrace material IDs are out of range" ids }
ʰl̲ights : Num a => a -> Float
L by 6 point lights: xyz and nonnegative RGB radiant intensity.
ʰl̲ights ← { lights → n̲ot ((t̲ally lights) c̲at 6) m̲atch s̲hape lights ? @ p̲anic< "RayTrace requires L by 6 lights" a ← ʰf̲inite f̲loat lights n̲ot '∧ r̲/ r̲avel (3 d̲rop₂ a) ≥ 0.0 ? @ p̲anic< "RayTrace light intensities must be nonnegative" a }
ʰe̲nvironment : Num a => a -> Float
2 by 3 nonnegative ambient illumination and background radiance.
ʰe̲nvironment ← { environment → n̲ot 2 3 m̲atch s̲hape environment ? @ p̲anic< "RayTrace requires 2 by 3 ambient and background RGB" e ← ʰf̲inite f̲loat environment n̲ot '∧ r̲/ r̲avel e ≥ 0.0 ? @ p̲anic< "RayTrace environment RGB must be nonnegative" e }
ʰs̲urfaces : Int -> Float -> Int -> Float
Surface material rows with a zero row for a miss (no zero indexing).
ʰs̲urfaces ← { materialids materials ids → n ← t̲ally ids 0 = t̲ally materialids ? (n c̲at 4) r̲eshape 0.0 mids ← (ids m̲ax 1) s̲elect materialids idx ← mids × f̲loor f̲loat ids > 0 (idx + 1) s̲elect (1 4 r̲eshape 0.0) c̲at materials }
ʰl̲ighting : Float -> Float -> Float -> Float -> Float -> Int -> Float -> Float
One light at a time: Lambertian cosine/pi and inverse-square falloff, with an offset shadow origin and an open segment before the light.
ʰl̲ighting ← { clip triangles lights points normals k accum → k > t̲ally lights ? accum light ← k s̲elect lights n ← t̲ally points offset ← f̲irst 3 d̲rop clip origins ← points + offset × normals delta ← (n ʰr̲ows 3 t̲ake light) − origins distance ← ᵛl̲ength delta dirs ← ᵛn̲ormalize delta ids ← (((clip ˡn̲earest triangles) origins) dirs + (f̲loat distance = 0.0) ʰs̲cale n ʰr̲ows 1.0 0.0 0.0) hits ← ((((clip ʰh̲its triangles) ids) origins) dirs) visible ← (ids = 0) ∨ (ˡd̲istances hits) ≥ distance − offset cosine ← 0.0 m̲ax normals ᵛd̲ot dirs divisor ← distance + f̲loat distance = 0.0 weight ← (f̲loat visible ∧ distance > 0.0) × cosine ÷ (p̲i @) × divisor × divisor contribution ← weight ʰs̲cale n ʰr̲ows 3 d̲rop light ((((((clip ʰl̲ighting triangles) lights) points) normals) k + 1) accum + contribution) }
ʰs̲hade : Float -> Float -> Int -> Float -> Float -> Float -> Float -> Float -> Int -> Float
Direct light for known nearest IDs; unused fields on misses stay finite.
ʰs̲hade ← { clip triangles materialids materials lights environment origins directions ids → hits ← ((((clip ʰh̲its triangles) ids) origins) directions) points ← origins + (ˡd̲istances hits) ʰs̲cale directions normals ← ((triangles ˡn̲ormals ids) directions) surface ← ((materialids ʰs̲urfaces materials) ids) n ← t̲ally origins initial ← n ʰr̲ows 1 s̲elect environment illum ← ((((((clip ʰl̲ighting triangles) lights) points) normals) 1) initial) color ← (3 t̲ake₂ surface) × illum ʰf̲inite color + (f̲loat ids = 0) ʰs̲cale n ʰr̲ows 2 s̲elect environment }
ˡs̲hade : (Num a, Num b, Num c, Num d, Num e, Num f, Num g) => a -> b -> Int -> c -> d -> e -> f -> g -> Float
((((((clip s_hade triangles)_ materialids)_ materials)_ lights)_ environment) is a dyadic function of origin and direction batches, returning N by 3 linear RGB with ambient and hard-shadowed diffuse light, no reflection.
ˡs̲hade ← { clip triangles materialids materials lights environment origins directions → c ← ʰc̲lip clip mesh ← ʰm̲esh triangles m ← ʰm̲aterials materials mids ← ((m ʰm̲aterialIds mesh) materialids) ls ← ʰl̲ights lights env ← ʰe̲nvironment environment o ← ʰb̲atch origins d ← o ʰd̲irections directions ids ← (((c ˡn̲earest mesh) o) d) ((((((((c ʰs̲hade mesh) mids) m) ls) env) o) d) ids) }
ˡr̲ender : (Num a, Num b, Num c, Num d, Num e, Num f, Num g) => a -> b -> Int -> c -> d -> e -> f -> g -> Float
The same arguments as s_hade, with exactly one bounded reflection. Mix (1-r)*primary + r*secondary; secondary shading does not reflect.
ˡr̲ender ← { clip triangles materialids materials lights environment origins directions → c ← ʰc̲lip clip mesh ← ʰm̲esh triangles m ← ʰm̲aterials materials mids ← ((m ʰm̲aterialIds mesh) materialids) ls ← ʰl̲ights lights env ← ʰe̲nvironment environment o ← ʰb̲atch origins d ← o ʰd̲irections directions ids ← (((c ˡn̲earest mesh) o) d) primary ← ((((((((c ʰs̲hade mesh) mids) m) ls) env) o) d) ids) hits ← ((((c ʰh̲its mesh) ids) o) d) normals ← ((mesh ˡn̲ormals ids) d) position ← o + (ˡd̲istances hits) ʰs̲cale d nextOrigins ← position + (f̲irst 3 d̲rop c) × normals nextDirections ← normals ˡr̲eflect d nextIds ← (((c ˡn̲earest mesh) nextOrigins) nextDirections) secondary ← ((((((((c ʰs̲hade mesh) mids) m) ls) env) nextOrigins) nextDirections) nextIds) surface ← ((mids ʰs̲urfaces m) ids) r ← 4 s̲elect₂ surface ʰf̲inite ((1.0 − r) ʰs̲cale primary) + r ʰs̲cale secondary }
ˡs̲rgb : (Num a, Num b) => a -> b -> Float
exposure s_rgb linear: clamp exposed linear RGB to 0..1 then apply the standard sRGB transfer; returns Float RGB in 0..1 before quantizing.
ˡs̲rgb ← { exposure linear → n̲ot 0 = t̲ally s̲hape exposure ? @ p̲anic< "RayTrace exposure must be scalar" e ← ʰf̲inite f̲loat exposure e < 0.0 ? @ p̲anic< "RayTrace exposure must be nonnegative" x ← 0.0 m̲ax 1.0 m̲in ʰf̲inite e × ʰb̲atch linear low ← f̲loat x ≤ 0.0031308 (low × 12.92 × x) + (1.0 − low) × (1.055 × x ^ (1.0 ÷ 2.4)) − 0.055 }
ˡs̲pheres : Num a => a -> Float
s_pheres data: finite S by 4 center xyz/radius, strictly positive radii.
ˡs̲pheres ← { data → n̲ot ((t̲ally data) c̲at 4) m̲atch s̲hape data ? @ p̲anic< "RayTrace spheres require S by 4 center/radius rows" a ← ʰf̲inite f̲loat data n̲ot '∧ r̲/ (4 s̲elect₂ a) > 0.0 ? @ p̲anic< "RayTrace sphere radius must be positive" a }
ʰs̲pherePairs : Num a => Float -> Float -> Float -> a -> Float
Matched sphere rows and rays, stable quadratic roots using q and c/q. Each result is t,0,0, with the same zero-miss convention as triangles.
ʰs̲pherePairs ← { clip spheres origins directions → delta ← origins − 3 t̲ake₂ spheres a ← directions ᵛd̲ot directions b ← delta ᵛd̲ot directions radius ← 4 s̲elect₂ spheres cc ← (delta ᵛd̲ot delta) − radius × radius perpendicular ← delta ᵛc̲ross directions disc ← ʰf̲inite (a × radius × radius) − perpendicular ᵛd̲ot perpendicular root ← s̲qrt 0.0 m̲ax disc sign ← 1.0 − 2.0 × f̲loat b < 0.0 q ← n̲eg b + sign × root safe ← q + f̲loat q = 0.0 x ← q ÷ a y ← cc ÷ safe lo ← x m̲in y hi ← x m̲ax y near ← f̲irst clip far ← f̲irst 1 d̲rop clip firstok ← (lo ≥ near) ∧ lo ≤ far secondok ← (hi ≥ near) ∧ hi ≤ far t ← ((f̲loat firstok) × lo) + (f̲loat (n̲ot firstok) ∧ secondok) × hi valid ← (disc ≥ 0.0) ∧ (firstok ∨ secondok) ʰf̲inite ((f̲loat valid) × t) '× t̲able 1.0 0.0 0.0 }
ˡs̲phereHit : (Num a, Num b, Num c, Num d) => a -> b -> c -> d -> Float
(((clip s_phereHit sphere)_ origins)_ directions): one length-4 sphere against N rays, returning N by 3 t,0,0. Nonunit directions retain t units.
ˡs̲phereHit ← { clip sphere origins directions → c ← ʰc̲lip clip s ← ˡs̲pheres 1 4 r̲eshape 4 ʰv̲ector sphere o ← ʰb̲atch origins d ← o ʰd̲irections directions (((c ʰs̲pherePairs ((t̲ally o) c̲at 4) r̲eshape s) o) d) }
ˡs̲phereNormals : (Num a, Num b) => a -> b -> Float
sphere s_phereNormals points: outward unit geometric normals at surface points, independent of incident direction; points must lie on the sphere.
ˡs̲phereNormals ← { sphere points → s ← ˡs̲pheres 1 4 r̲eshape 4 ʰv̲ector sphere p ← ʰb̲atch points delta ← p − (t̲ally p) ʰr̲ows 3 t̲ake r̲avel s radius ← f̲irst 3 d̲rop r̲avel s n̲ot '∧ r̲/ (a̲bs ((ᵛl̲ength delta) ÷ radius) − 1.0) ≤ 1e-10 ? @ p̲anic< "RayTrace sphere normal points must lie on its surface" ᵛn̲ormalize delta }
ʰg̲lobalIds : a -> b -> Int -> Int -> Int
Validate one positive unique global Int ID per primitive, across kinds.
ʰg̲lobalIds ← { triangles spheres triangleids sphereids → n̲ot (1 r̲eshape t̲ally triangles) m̲atch s̲hape triangleids ? @ p̲anic< "RayTrace needs one global ID per triangle" n̲ot (1 r̲eshape t̲ally spheres) m̲atch s̲hape sphereids ? @ p̲anic< "RayTrace needs one global ID per sphere" ids ← triangleids c̲at sphereids n̲ot '∧ r̲/ ids > 0 ? @ p̲anic< "RayTrace global IDs must be positive" n̲ot (t̲ally ids) = t̲ally u̲nique ids ? @ p̲anic< "RayTrace global IDs must be unique across geometry kinds" ids + 0 × ids m̲od 1 }
ʰl̲ocal : Num a => a -> a -> Int
Private global-ID lookup uses equality, returning zero for a miss without encoding IDs in floating point.
ʰl̲ocal ← { ids selected → '{ id → 0 = id ? 0◆ at ← w̲here ids = id◆ 0 = t̲ally at ? 0◆ f̲irst at } e̲ach selected }
ˡh̲itKinds : Int -> Int -> Int -> Int
((triangleids h_itKinds sphereids)_ selected): N Int tags, 0 miss, 1 triangle, 2 sphere. Refuse unknown nonzero global IDs.
ˡh̲itKinds ← { triangleids sphereids selected → checked ← selected m̲od 1 allids ← (((triangleids ʰg̲lobalIds sphereids) triangleids) sphereids) sn ← t̲ally allids m̲od 1 n̲ot 1 = t̲ally s̲hape selected ? @ p̲anic< "RayTrace selected global IDs must be a vector" n̲ot '∧ r̲/ (selected = 0) ∨ selected m̲ember? triangleids c̲at sphereids ? @ p̲anic< "RayTrace unknown selected global ID" (f̲loor f̲loat selected m̲ember? triangleids) + (2 × f̲loor f̲loat selected m̲ember? sphereids) + 0 × checked + sn }
ˡh̲itLocals : Int -> Int -> Int -> Int
((triangleids h_itLocals sphereids)_ selected): per-kind one-based indices, separate from global IDs and kind tags; zero on misses.
ˡh̲itLocals ← { triangleids sphereids selected → kinds ← ((triangleids ˡh̲itKinds sphereids) selected) (triangleids ʰl̲ocal selected) + (sphereids ʰl̲ocal selected) + 0 × kinds }
ʰm̲ixedHits : (Num a, Num b) => Float -> Float -> Float -> a -> a -> a -> Float -> b -> Float
Resolve known global IDs to matched triangle/sphere hit data.
ʰm̲ixedHits ← { clip triangles spheres triangleids sphereids selected origins directions → ti ← triangleids ʰl̲ocal selected si ← sphereids ʰl̲ocal selected th ← ((((clip ʰh̲its triangles) ti) origins) directions) 0 = t̲ally spheres ? th matched ← (si m̲ax 1) s̲elect spheres sh ← (((clip ʰs̲pherePairs matched) origins) directions) th + (f̲loat si > 0) ʰs̲cale sh }
ˡm̲ixedHits : (Num a, Num b, Num c, Num d, Num e) => a -> b -> c -> Int -> Int -> Int -> d -> e -> Float
(((((((clip m_ixedHits triangles)_ spheres)_ triangleids)_ sphereids) _ selected)_ origins)_ directions): selected N by 3 t,u,v, sphere u/v zero.
ˡm̲ixedHits ← { clip triangles spheres triangleids sphereids selected origins directions → c ← ʰc̲lip clip mesh ← ʰm̲esh triangles balls ← ˡs̲pheres spheres ids ← (((mesh ʰg̲lobalIds balls) triangleids) sphereids) kinds ← ((triangleids ˡh̲itKinds sphereids) selected) o ← ʰb̲atch origins d ← o ʰd̲irections directions n̲ot (s̲hape selected) m̲atch s̲hape f̲irst₂ o ? @ p̲anic< "RayTrace selected IDs must match rays" (((((((c ʰm̲ixedHits mesh) balls) triangleids) sphereids) selected + 0 × kinds) o) d) }
ˡm̲ixedNearest : (Num a, Num b, Num c, Num d, Num e) => a -> b -> c -> Int -> Int -> d -> e -> Int
((((((clip m_ixedNearest triangles)_ spheres)_ triangleids)_ sphereids) _ origins)_ directions): global Int IDs, distance then smallest global ID for exact ties, independent of per-kind row order and ray chunks.
ˡm̲ixedNearest ← { clip triangles spheres triangleids sphereids origins directions → c ← ʰc̲lip clip mesh ← ʰm̲esh triangles balls ← ˡs̲pheres spheres allids ← (((mesh ʰg̲lobalIds balls) triangleids) sphereids) o ← ʰb̲atch origins d ← o ʰd̲irections directions step̲ ← { state → k ← f̲irst state oldids ← 1 d̲rop state id ← f̲irst k s̲elect allids candidate ← (t̲ally o) r̲eshape id hit ← (((((((c ʰm̲ixedHits mesh) balls) triangleids) sphereids) candidate) o) d) prev ← (((((((c ʰm̲ixedHits mesh) balls) triangleids) sphereids) oldids) o) d) t ← ˡd̲istances hit old ← ˡd̲istances prev choose ← (ˡm̲ask hit) ∧ ((oldids = 0) ∨ (t < old) ∨ (t = old) ∧ id < oldids) next ← oldids + (f̲loor f̲loat choose) × id − oldids (k + 1) c̲at next } 1 d̲rop (t̲ally allids) 'step̲ p̲ower 1 c̲at (t̲ally o) r̲eshape 0 }
ˡr̲aysAt : (Num a, Num b, Num c) => a -> b -> Int -> c -> Float
(((camera r_aysAt size)_ pixels)_ film): pinhole directions with N by 2 subpixel offsets in [0,1], x right/y down. Centers [0.5,0.5] match r_ays.
ˡr̲aysAt ← { camera size pixels film → c ← ʰc̲amera camera sz ← 2 ʰv̲ector size n̲ot '∧ r̲/ (sz > 0.0) ∧ sz = f̲loat f̲loor sz ? @ p̲anic< "RayTrace image size must contain positive integers" n̲ot (1 = t̲ally s̲hape pixels) ∧ '∧ r̲/ (pixels ≥ 1) ∧ pixels ≤ f̲loor '× r̲/ sz ? @ p̲anic< "RayTrace pixel indices must be a vector within the image" n ← t̲ally pixels n̲ot (n c̲at 2) m̲atch s̲hape film ? @ p̲anic< "RayTrace film samples require N by 2 offsets" f ← ʰf̲inite f̲loat film n̲ot '∧ r̲/ r̲avel (f ≥ 0.0) ∧ f ≤ 1.0 ? @ p̲anic< "RayTrace film offsets must be in 0..1" w ← f̲irst sz ht ← f̲irst 1 d̲rop sz forward ← ᵛn̲ormalize 1 3 r̲eshape (3 t̲ake 3 d̲rop c) − 3 t̲ake c right ← ᵛn̲ormalize forward ᵛc̲ross 1 3 r̲eshape 3 t̲ake 6 d̲rop c up ← right ᵛc̲ross forward half ← 0.5 × f̲irst 9 d̲rop c tangent ← (s̲in half) ÷ c̲os half col ← (pixels − 1) m̲od f̲loor w row ← (pixels − 1) d̲iv f̲loor w xx ← tangent × (w ÷ ht) × ((2.0 × (1 s̲elect₂ f) + f̲loat col) ÷ w) − 1.0 yy ← tangent × 1.0 − (2.0 × (2 s̲elect₂ f) + f̲loat row) ÷ ht ᵛn̲ormalize (n ʰr̲ows r̲avel forward) + (xx ʰs̲cale n ʰr̲ows r̲avel right) + yy ʰs̲cale n ʰr̲ows r̲avel up }
ˡl̲ensRays : (Num a, Num b, Num c, Num d) => a -> b -> c -> d -> (Float, Float)
(((camera l_ensRays lens)_ pinhole)_ disk): returns (N by 3 origins, N by 3 directions). Lens is [aperture radius, forward focus distance]; disk is N by 2 unit-disk coordinates. Zero aperture returns pinhole directions unchanged, exactly. All input pinhole directions must be unit.
ˡl̲ensRays ← { camera lens pinhole disk → c ← ʰc̲amera camera l ← 2 ʰv̲ector lens radius ← f̲irst l focus ← f̲irst 1 d̲rop l (radius < 0.0) ∨ focus ≤ 0.0 ? @ p̲anic< "RayTrace lens requires nonnegative aperture and positive focus distance" d ← ʰb̲atch pinhole n ← t̲ally d n̲ot '∧ r̲/ (a̲bs (ᵛl̲ength d) − 1.0) ≤ 1e-10 ? @ p̲anic< "RayTrace lens pinhole directions must be unit" n̲ot (n c̲at 2) m̲atch s̲hape disk ? @ p̲anic< "RayTrace lens requires N by 2 disk samples" ds ← ʰf̲inite f̲loat disk n̲ot '∧ r̲/ ('+ r̲/₂ ds × ds) ≤ 1.0 + 1e-10 ? @ p̲anic< "RayTrace lens samples must lie in the unit disk" origins ← c ˡo̲rigins n forward ← ᵛn̲ormalize 1 3 r̲eshape (3 t̲ake 3 d̲rop c) − 3 t̲ake c right ← ᵛn̲ormalize forward ᵛc̲ross 1 3 r̲eshape 3 t̲ake 6 d̲rop c up ← right ᵛc̲ross forward axial ← d ᵛd̲ot n ʰr̲ows r̲avel forward n̲ot '∧ r̲/ axial > 0.0 ? @ p̲anic< "RayTrace lens rays must point toward the camera focal plane" 0.0 = radius ? (origins, d) focal ← ʰf̲inite origins + (focus ÷ axial) ʰs̲cale d offset ← radius × (((1 s̲elect₂ ds) ʰs̲cale n ʰr̲ows r̲avel right) + (2 s̲elect₂ ds) ʰs̲cale n ʰr̲ows r̲avel up) moved ← ʰf̲inite origins + offset next ← ᵛn̲ormalize focal − moved n̲ot '∧ r̲/ (ᵛl̲ength next) > 0.0 ? @ p̲anic< "RayTrace lens lost its focal direction at this numeric scale" (moved, next) }
p̲anic< expands to
(⎕P̲ANIC ("RayTrace lens requires nonnegative aperture and positive focus distance"))
ʰp̲ictureText : Box Char -> Char
Balanced joining keeps large pixel pictures from quadratic text copying.
ʰp̲ictureText ← { boxes → n ← t̲ally boxes n = 0 ? "" n = 1 ? d̲isclose f̲irst boxes half ← n d̲iv 2 (ʰp̲ictureText half t̲ake boxes) c̲at ʰp̲ictureText half d̲rop boxes }
ˡp̲icture : (Num a, Num b) => a -> b -> Char
size p_icture displayRGB: row-major N by 3 sRGB values in 0..1 to an SVG of square color pixels, shown at four times native dimensions. Use exposure s_rgb linear first. This only displays computed pixels; all lighting, shadows and reflections remain in the ray tracer.
ˡp̲icture ← { size displayRGB → sz ← 2 ʰv̲ector size n̲ot '∧ r̲/ (sz > 0.0) ∧ sz = f̲loat f̲loor sz ? @ p̲anic< "RayTrace picture dimensions must be positive integers" rgb ← ʰb̲atch displayRGB wh ← f̲loor sz n̲ot (t̲ally rgb) = '× r̲/ wh ? @ p̲anic< "RayTrace picture requires width times height RGB rows" n̲ot '∧ r̲/ r̲avel (rgb ≥ 0.0) ∧ rgb ≤ 1.0 ? @ p̲anic< "RayTrace picture sRGB values must be in 0..1" width ← f̲irst wh cells ← '{ i → x ← (i − 1) m̲od width y ← (i − 1) d̲iv width corners ← 2 4 r̲eshape (x + 0 1 1 0) c̲at y + 0 0 1 1 (ˢᵛᵍf̲ill ˢᵛᵍr̲gb 255.0 × i s̲elect rgb) ˢᵛᵍp̲olygon corners } m̲ap r̲ange t̲ally rgb attrs ← (ˢᵛᵍs̲cale 4) c̲at "shape-rendering" ˢᵛᵍa̲ttr "crispEdges" (4 × wh) ˢᵛᵍp̲icture attrs ˢᵛᵍg̲roup ʰp̲ictureText cells }