sourcelibs/RayTrace/src/RayTrace.xtl

1⍝│ RayTrace: triangle/sphere tracing, stable mixed IDs and opaque lighting. 2⍝│ Import with "rt:" u_se< "RayTrace"; depends on Vector3 and Matrix. 3⍝│ Right-handed xyz, y up, radians, N by 3 ray rows, T by 3 by 3 meshes. 4⍝│ Original implementation of Moller-Trumbore and Lambertian equations. 5 6"v:" u̲se< "Vector3" 7 8⍝│ Require finite inputs and computed results. 9ʰf̲inite ← { a → 10 n̲ot '∧ r̲/ r̲avel (a − a) = 0.0 ? @ p̲anic< "RayTrace requires finite numbers and results"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace requires finite numbers and results"))
11 a 12} 13 14⍝│ A vector with exactly n items, converted to Float. 15ʰv̲ector ← { n a → 16 n̲ot (1 r̲eshape n) m̲atch s̲hape a ? @ p̲anic< "RayTrace requires a vector of the documented length"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace requires a vector of the documented length"))
17 ʰf̲inite f̲loat a 18} 19 20⍝│ A matrix of xyz rows, converted to Float. 21ʰb̲atch ← { a → 22 n̲ot ((t̲ally a) c̲at 3) m̲atch s̲hape a ? @ p̲anic< "RayTrace requires an N by 3 ray batch"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace requires an N by 3 ray batch"))
23 ʰf̲inite f̲loat a 24} 25 26⍝│ Replicate a vector as n identical rows. 27ʰr̲ows ← { n a → (n r̲eshape 1.0) '× t̲able a } 28 29⍝│ Scale xyz rows by one number per row. 30ʰs̲cale ← { k a → a × k '× t̲able 1.0 1.0 1.0 } 31 32⍝│ Near, far, relative intersection epsilon and surface offset. 33ʰc̲lip ← { a → 34 c ← 4 ʰv̲ector a 35 n̲ot '∧ r̲/ c > 0.0 ? @ p̲anic< "RayTrace clip entries must be positive"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace clip entries must be positive"))
36 (f̲irst c) ≥ f̲irst 1 d̲rop c ? @ p̲anic< "RayTrace near must be less than far"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace near must be less than far"))
37 (f̲irst 2 d̲rop c) ≥ 1.0 ? @ p̲anic< "RayTrace intersection epsilon must be less than 1"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace intersection epsilon must be less than 1"))
38 c 39} 40 41⍝│ Validate a camera's eye, target, up and fov; reject a collapsed basis. 42ʰc̲amera ← { camera → 43 c ← 10 ʰv̲ector camera 44 forward ← (3 t̲ake 3 d̲rop c) − 3 t̲ake c 45 up ← 3 t̲ake 6 d̲rop c 46 0.0 = f̲irst ᵛl̲ength 1 3 r̲eshape forward ? @ p̲anic< "RayTrace camera eye and target must differ"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace camera eye and target must differ"))
47 side ← (ᵛn̲ormalize 1 3 r̲eshape forward) ᵛc̲ross ᵛn̲ormalize 1 3 r̲eshape up 48 1e-12 ≥ f̲irst ᵛl̲ength side ? @ p̲anic< "RayTrace camera up must be nonzero and independent of view"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace camera up must be nonzero and independent of view"))
49 fov ← f̲irst 9 d̲rop c 50 (fov ≤ 0.0) ∨ fov ≥ p̲i @ ? @ p̲anic< "RayTrace camera fov must be between 0 and pi"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace camera fov must be between 0 and pi"))
51 c 52} 53 54⍝│ camera o_rigins count: count copies of the eye position. 55ˡo̲rigins ← { camera count → 56 n̲ot 0 = t̲ally s̲hape count ? @ p̲anic< "RayTrace ray count must be scalar"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace ray count must be scalar"))
57 (count < 0) ∨ count ≠ f̲loor count ? @ p̲anic< "RayTrace ray count must be a nonnegative integer"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace ray count must be a nonnegative integer"))
58 count ʰr̲ows 3 t̲ake ʰc̲amera camera 59} 60 61⍝│ ((camera r_ays size)_ pixels): normalized pinhole directions at pixel 62⍝│ centers; size is width height and pixels are one-based flat row indices. 63ˡr̲ays ← { camera size pixels → 64 c ← ʰc̲amera camera 65 sz ← 2 ʰv̲ector size 66 n̲ot '∧ r̲/ (sz > 0.0) ∧ sz = f̲loat f̲loor sz ? @ p̲anic< "RayTrace image size must contain positive integers"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace image size must contain positive integers"))
67 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"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace pixel indices must be a vector within the image"))
68 n̲ot '∧ r̲/ pixels = f̲loor pixels ? @ p̲anic< "RayTrace pixel indices must be integers"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace pixel indices must be integers"))
69 w ← f̲irst sz 70 ht ← f̲irst 1 d̲rop sz 71 forward ← ᵛn̲ormalize 1 3 r̲eshape (3 t̲ake 3 d̲rop c) − 3 t̲ake c 72 right ← ᵛn̲ormalize forward ᵛc̲ross 1 3 r̲eshape 3 t̲ake 6 d̲rop c 73 up ← right ᵛc̲ross forward 74 half ← 0.5 × f̲irst 9 d̲rop c 75 tangent ← (s̲in half) ÷ c̲os half 76 col ← (pixels − 1) m̲od f̲loor w 77 row ← (pixels − 1) d̲iv f̲loor w 78 xx ← tangent × (w ÷ ht) × ((2.0 × 0.5 + f̲loat col) ÷ w) − 1.0 79 yy ← tangent × 1.0 − (2.0 × 0.5 + f̲loat row) ÷ ht 80 n ← t̲ally pixels 81 ᵛ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 82} 83 84⍝│ Validate finite triangles, allowing degenerate faces (which always miss). 85ʰm̲esh ← { triangles → 86 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"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace requires a T by 3 by 3 triangle array"))
87 ʰf̲inite f̲loat triangles 88} 89 90⍝│ Matched per-ray triangles, used for a broadcast face and selected hits. 91⍝│ Original Moller-Trumbore equations with finite masked determinants. 92ʰp̲airs ← { clip selected origins directions → 93 n ← t̲ally origins 94 a ← 1 s̲elect₂ selected 95 e1 ← (2 s̲elect₂ selected) − a 96 e2 ← (3 s̲elect₂ selected) − a 97 p ← directions ᵛc̲ross e2 98 det ← e1 ᵛd̲ot p 99 eps ← f̲irst 2 d̲rop clip 100 bound ← eps × (ᵛl̲ength e1) × (ᵛl̲ength e2) × ᵛl̲ength directions 101 good ← (a̲bs det) > bound 102 denom ← (det × f̲loat good) + f̲loat n̲ot good 103 tv ← origins − a 104 u ← (tv ᵛd̲ot p) ÷ denom 105 q ← tv ᵛc̲ross e1 106 vv ← (directions ᵛd̲ot q) ÷ denom 107 t ← (e2 ᵛd̲ot q) ÷ denom 108 ok ← good ∧ (u ≥ 0.0) ∧ (vv ≥ 0.0) ∧ ((u + vv) ≤ 1.0) ∧ (t ≥ f̲irst clip) ∧ t ≤ f̲irst 1 d̲rop clip 109 ʰ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 110} 111 112⍝│ One broadcast triangle against a ray batch, using the same pair solver. 113ʰi̲ntersect ← { clip triangle origins directions → 114 selected ← ((t̲ally origins) c̲at 3 3) r̲eshape triangle 115 (((clip ʰp̲airs selected) origins) directions) 116} 117 118⍝│ Validate matching origin/direction batches, refusing zero directions. 119ʰd̲irections ← { origins directions → 120 d ← ʰb̲atch directions 121 n̲ot (s̲hape origins) m̲atch s̲hape d ? @ p̲anic< "RayTrace origin and direction batches must match"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace origin and direction batches must match"))
122 n̲ot '∧ r̲/ (ᵛl̲ength d) > 0.0 ? @ p̲anic< "RayTrace directions must be nonzero"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace directions must be nonzero"))
123 d 124} 125 126⍝│ (((clip i_ntersect triangle)_ origins)_ directions): N by 3 t,u,v; 127⍝│ miss rows are zero, with an explicit Int mask available via m_ask. 128ˡi̲ntersect ← { clip triangle origins directions → 129 c ← ʰc̲lip clip 130 n̲ot 3 3 m̲atch s̲hape triangle ? @ p̲anic< "RayTrace requires one 3 by 3 triangle"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace requires one 3 by 3 triangle"))
131 tri ← ʰf̲inite f̲loat triangle 132 o ← ʰb̲atch origins 133 d ← o ʰd̲irections directions 134 (((c ʰi̲ntersect tri) o) d) 135} 136 137⍝│ m_ask hits: N Int validity mask for an N by 3 t,u,v result. 138ˡm̲ask ← { hits → f̲loor f̲loat (f̲irst₂ ʰb̲atch hits) > 0.0 } 139 140⍝│ d_istances hits: N Float ray parameters, zero on a miss. 141ˡd̲istances ← { hits → f̲irst₂ ʰb̲atch hits } 142 143⍝│ Conservative slab broad phase; parallel axes use the clip interval. 144⍝│ Padding includes faces/edges despite roundoff; the original triangle 145⍝│ solver still decides every candidate hit and exact declaration-order tie. 146ʰb̲oxCandidates ← { clip triangle origins directions → 147 n ← t̲ally origins 148 lo ← 'm̲in r̲/ triangle 149 hi ← 'm̲ax r̲/ triangle 150 margin ← 1e-12 × 1.0 + 'm̲ax r̲/ (a̲bs lo) m̲ax a̲bs hi 151 lower ← n ʰr̲ows lo − margin 152 upper ← n ʰr̲ows hi + margin 153 parallel ← directions = 0.0 154 safe ← directions + f̲loat parallel 155 a ← (lower − origins) ÷ safe 156 b ← (upper − origins) ÷ safe 157 moving ← f̲loat n̲ot parallel 158 low ← (moving × a m̲in b) + (f̲loat parallel) × f̲irst clip 159 high ← (moving × a m̲ax b) + (f̲loat parallel) × f̲irst 1 d̲rop clip 160 inside ← '∧ r̲/₂ (n̲ot parallel) ∨ (origins ≥ lower) ∧ origins ≤ upper 161 first ← (f̲irst clip) m̲ax 'm̲ax r̲/₂ low 162 last ← (f̲irst 1 d̲rop clip) m̲in 'm̲in r̲/₂ high 163 f̲loor f̲loat inside ∧ first ≤ last 164} 165 166⍝│ Sweep exactly T primitives, with separate Int cursor/IDs and Float 167⍝│ distance cache. Conservative boxes compact candidate rays; strict closer 168⍝│ hits preserve declaration-order ties. Float selection avoids subtraction. 169ʰn̲earest ← { clip triangles origins directions → 170 n ← t̲ally origins 171 step̲ ← { state → 172 (k, ids, distances) ← state 173 triangle ← k s̲elect triangles 174 candidates ← (((clip ʰb̲oxCandidates triangle) origins) directions) 175 n̲ot '∨ r̲/ candidates = 1 ? (k + 1, ids, distances) 176 active ← candidates r̲eplicate ids 177 old ← candidates r̲eplicate distances 178 o ← candidates r̲eplicate origins 179 d ← candidates r̲eplicate directions 180 hit ← (((clip ʰi̲ntersect triangle) o) d) 181 t ← ˡd̲istances hit 182 choose ← (ˡm̲ask hit) ∧ ((active = 0) ∨ t < old) 183 delta ← (f̲loor f̲loat choose) × k − active 184 selectedT ← ((f̲loat choose) × t) + (f̲loat n̲ot choose) × old 185 slots ← 1 + '+ s̲\ candidates 186 next ← ids + candidates × slots s̲elect 0 c̲at delta 187 cached ← ((f̲loat candidates) × slots s̲elect 0.0 c̲at selectedT) + (f̲loat candidates = 0) × distances 188 (k + 1, next, cached) 189 } 190 (_, ids, _) ← (t̲ally triangles) 'step̲ p̲ower (1, n r̲eshape 0, n r̲eshape 0.0) 191 ids 192} 193 194⍝│ Intersect selected IDs without indexing zero or materializing all pairs. 195ʰh̲its ← { clip triangles ids origins directions → 196 n ← t̲ally origins 197 (0 = n) ∨ 0 = t̲ally triangles ? (n c̲at 3) r̲eshape 0.0 198 selected ← (ids m̲ax 1) s̲elect triangles 199 (f̲loat ids > 0) ʰs̲cale (((clip ʰp̲airs selected) origins) directions) 200} 201 202⍝│ (((clip n_earest triangles)_ origins)_ directions): N Int triangle IDs, 203⍝│ zero on a miss; t counts ray-direction units, not necessarily distance. 204ˡn̲earest ← { clip triangles origins directions → 205 c ← ʰc̲lip clip 206 mesh ← ʰm̲esh triangles 207 o ← ʰb̲atch origins 208 d ← o ʰd̲irections directions 209 (((c ʰn̲earest mesh) o) d) 210} 211 212⍝│ ((((clip h_its triangles)_ ids)_ origins)_ directions): selected hit data. 213⍝│ IDs must come from n_earest for these same rays, triangles and clip. 214ˡh̲its ← { clip triangles ids origins directions → 215 c ← ʰc̲lip clip 216 mesh ← ʰm̲esh triangles 217 o ← ʰb̲atch origins 218 d ← o ʰd̲irections directions 219 n̲ot (s̲hape ids) m̲atch s̲hape f̲irst₂ o ? @ p̲anic< "RayTrace hit IDs must match the ray count"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace hit IDs must match the ray count"))
220 n̲ot '∧ r̲/ (ids = f̲loor ids) ∧ (ids ≥ 0) ∧ ids ≤ t̲ally mesh ? @ p̲anic< "RayTrace hit IDs are out of range"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace hit IDs are out of range"))
221 ((((c ʰh̲its mesh) ids) o) d) 222} 223 224⍝│ Unit geometric normals for selected triangles, zero for misses. 225ʰn̲ormals ← { triangles ids → 226 n ← t̲ally ids 227 (0 = n) ∨ 0 = t̲ally triangles ? (n c̲at 3) r̲eshape 0.0 228 selected ← (ids m̲ax 1) s̲elect triangles 229 a ← 1 s̲elect₂ selected 230 (f̲loat ids > 0) ʰs̲cale ᵛn̲ormalize ((2 s̲elect₂ selected) − a) ᵛc̲ross (3 s̲elect₂ selected) − a 231} 232 233⍝│ ((triangles n_ormals ids)_ directions): selected unit normals oriented 234⍝│ against incoming rays; zero for miss IDs. Winding supplies geometry. 235ˡn̲ormals ← { triangles ids directions → 236 mesh ← ʰm̲esh triangles 237 d ← ʰb̲atch directions 238 n̲ot (s̲hape ids) m̲atch s̲hape f̲irst₂ d ? @ p̲anic< "RayTrace normal IDs must match the ray count"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace normal IDs must match the ray count"))
239 n̲ot '∧ r̲/ (ids ≥ 0) ∧ ids ≤ t̲ally mesh ? @ p̲anic< "RayTrace normal IDs are out of range"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace normal IDs are out of range"))
240 n ← mesh ʰn̲ormals ids 241 (1.0 − 2.0 × f̲loat (n ᵛd̲ot d) > 0.0) ʰs̲cale n 242} 243 244⍝│ normals r_eflect directions: d - 2(d dot n)n for unit normals. 245⍝│ Zero normals leave directions unchanged, for masked miss rows. 246ˡr̲eflect ← { normals directions → 247 n ← ʰb̲atch normals 248 d ← ʰb̲atch directions 249 len ← ᵛl̲ength n 250 n̲ot '∧ r̲/ (len = 0.0) ∨ (a̲bs len − 1.0) ≤ 1e-10 ? @ p̲anic< "RayTrace reflection normals must be unit or zero"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace reflection normals must be unit or zero"))
251 ʰf̲inite d − (2.0 × d ᵛd̲ot n) ʰs̲cale n 252} 253 254⍝│ Validate M by 4 linear RGB and reflection weights in 0..1. 255ʰm̲aterials ← { materials → 256 n̲ot ((t̲ally materials) c̲at 4) m̲atch s̲hape materials ? @ p̲anic< "RayTrace requires M by 4 materials"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace requires M by 4 materials"))
257 m ← ʰf̲inite f̲loat materials 258 n̲ot '∧ r̲/ r̲avel (m ≥ 0.0) ∧ m ≤ 1.0 ? @ p̲anic< "RayTrace material RGB and reflection must be in 0..1"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace material RGB and reflection must be in 0..1"))
259 m 260} 261 262⍝│ Validate one material index per triangle; the caller's Int array stays Int. 263ʰm̲aterialIds ← { materials triangles ids → 264 n̲ot (1 r̲eshape t̲ally triangles) m̲atch s̲hape ids ? @ p̲anic< "RayTrace requires one material ID per triangle"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace requires one material ID per triangle"))
265 n̲ot '∧ r̲/ (ids ≥ 1) ∧ ids ≤ t̲ally materials ? @ p̲anic< "RayTrace material IDs are out of range"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace material IDs are out of range"))
266 ids 267} 268 269⍝│ L by 6 point lights: xyz and nonnegative RGB radiant intensity. 270ʰl̲ights ← { lights → 271 n̲ot ((t̲ally lights) c̲at 6) m̲atch s̲hape lights ? @ p̲anic< "RayTrace requires L by 6 lights"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace requires L by 6 lights"))
272 a ← ʰf̲inite f̲loat lights 273 n̲ot '∧ r̲/ r̲avel (3 d̲rop₂ a) ≥ 0.0 ? @ p̲anic< "RayTrace light intensities must be nonnegative"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace light intensities must be nonnegative"))
274 a 275} 276 277⍝│ 2 by 3 nonnegative ambient illumination and background radiance. 278ʰe̲nvironment ← { environment → 279 n̲ot 2 3 m̲atch s̲hape environment ? @ p̲anic< "RayTrace requires 2 by 3 ambient and background RGB"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace requires 2 by 3 ambient and background RGB"))
280 e ← ʰf̲inite f̲loat environment 281 n̲ot '∧ r̲/ r̲avel e ≥ 0.0 ? @ p̲anic< "RayTrace environment RGB must be nonnegative"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace environment RGB must be nonnegative"))
282 e 283} 284 285⍝│ Surface material rows with a zero row for a miss (no zero indexing). 286ʰs̲urfaces ← { materialids materials ids → 287 n ← t̲ally ids 288 0 = t̲ally materialids ? (n c̲at 4) r̲eshape 0.0 289 mids ← (ids m̲ax 1) s̲elect materialids 290 idx ← mids × f̲loor f̲loat ids > 0 291 (idx + 1) s̲elect (1 4 r̲eshape 0.0) c̲at materials 292} 293 294⍝│ One light at a time: Lambertian cosine/pi and inverse-square falloff, 295⍝│ with an offset shadow origin and an open segment before the light. 296ʰl̲ighting ← { clip triangles lights points normals k accum → 297 k > t̲ally lights ? accum 298 light ← k s̲elect lights 299 n ← t̲ally points 300 offset ← f̲irst 3 d̲rop clip 301 origins ← points + offset × normals 302 delta ← (n ʰr̲ows 3 t̲ake light) − origins 303 distance ← ᵛl̲ength delta 304 dirs ← ᵛn̲ormalize delta 305 ids ← (((clip ˡn̲earest triangles) origins) dirs + (f̲loat distance = 0.0) ʰs̲cale n ʰr̲ows 1.0 0.0 0.0) 306 hits ← ((((clip ʰh̲its triangles) ids) origins) dirs) 307 visible ← (ids = 0) ∨ (ˡd̲istances hits) ≥ distance − offset 308 cosine ← 0.0 m̲ax normals ᵛd̲ot dirs 309 divisor ← distance + f̲loat distance = 0.0 310 weight ← (f̲loat visible ∧ distance > 0.0) × cosine ÷ (p̲i @) × divisor × divisor 311 contribution ← weight ʰs̲cale n ʰr̲ows 3 d̲rop light 312 ((((((clip ʰl̲ighting triangles) lights) points) normals) k + 1) accum + contribution) 313} 314 315⍝│ Direct light for known nearest IDs; unused fields on misses stay finite. 316ʰs̲hade ← { clip triangles materialids materials lights environment origins directions ids → 317 hits ← ((((clip ʰh̲its triangles) ids) origins) directions) 318 points ← origins + (ˡd̲istances hits) ʰs̲cale directions 319 normals ← ((triangles ˡn̲ormals ids) directions) 320 surface ← ((materialids ʰs̲urfaces materials) ids) 321 n ← t̲ally origins 322 initial ← n ʰr̲ows 1 s̲elect environment 323 illum ← ((((((clip ʰl̲ighting triangles) lights) points) normals) 1) initial) 324 color ← (3 t̲ake₂ surface) × illum 325 ʰf̲inite color + (f̲loat ids = 0) ʰs̲cale n ʰr̲ows 2 s̲elect environment 326} 327 328⍝│ ((((((clip s_hade triangles)_ materialids)_ materials)_ lights)_ environment) 329⍝│ is a dyadic function of origin and direction batches, returning N by 3 330⍝│ linear RGB with ambient and hard-shadowed diffuse light, no reflection. 331ˡs̲hade ← { clip triangles materialids materials lights environment origins directions → 332 c ← ʰc̲lip clip 333 mesh ← ʰm̲esh triangles 334 m ← ʰm̲aterials materials 335 mids ← ((m ʰm̲aterialIds mesh) materialids) 336 ls ← ʰl̲ights lights 337 env ← ʰe̲nvironment environment 338 o ← ʰb̲atch origins 339 d ← o ʰd̲irections directions 340 ids ← (((c ˡn̲earest mesh) o) d) 341 ((((((((c ʰs̲hade mesh) mids) m) ls) env) o) d) ids) 342} 343 344⍝│ The same arguments as s_hade, with exactly one bounded reflection. 345⍝│ Mix (1-r)*primary + r*secondary; secondary shading does not reflect. 346ˡr̲ender ← { clip triangles materialids materials lights environment origins directions → 347 c ← ʰc̲lip clip 348 mesh ← ʰm̲esh triangles 349 m ← ʰm̲aterials materials 350 mids ← ((m ʰm̲aterialIds mesh) materialids) 351 ls ← ʰl̲ights lights 352 env ← ʰe̲nvironment environment 353 o ← ʰb̲atch origins 354 d ← o ʰd̲irections directions 355 ids ← (((c ˡn̲earest mesh) o) d) 356 primary ← ((((((((c ʰs̲hade mesh) mids) m) ls) env) o) d) ids) 357 hits ← ((((c ʰh̲its mesh) ids) o) d) 358 normals ← ((mesh ˡn̲ormals ids) d) 359 position ← o + (ˡd̲istances hits) ʰs̲cale d 360 nextOrigins ← position + (f̲irst 3 d̲rop c) × normals 361 nextDirections ← normals ˡr̲eflect d 362 nextIds ← (((c ˡn̲earest mesh) nextOrigins) nextDirections) 363 secondary ← ((((((((c ʰs̲hade mesh) mids) m) ls) env) nextOrigins) nextDirections) nextIds) 364 surface ← ((mids ʰs̲urfaces m) ids) 365 r ← 4 s̲elect₂ surface 366 ʰf̲inite ((1.0 − r) ʰs̲cale primary) + r ʰs̲cale secondary 367} 368 369⍝│ exposure s_rgb linear: clamp exposed linear RGB to 0..1 then apply 370⍝│ the standard sRGB transfer; returns Float RGB in 0..1 before quantizing. 371ˡs̲rgb ← { exposure linear → 372 n̲ot 0 = t̲ally s̲hape exposure ? @ p̲anic< "RayTrace exposure must be scalar"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace exposure must be scalar"))
373 e ← ʰf̲inite f̲loat exposure 374 e < 0.0 ? @ p̲anic< "RayTrace exposure must be nonnegative"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace exposure must be nonnegative"))
375 x ← 0.0 m̲ax 1.0 m̲in ʰf̲inite e × ʰb̲atch linear 376 low ← f̲loat x ≤ 0.0031308 377 (low × 12.92 × x) + (1.0 − low) × (1.055 × x ^ (1.0 ÷ 2.4)) − 0.055 378} 379 380⍝│ s_pheres data: finite S by 4 center xyz/radius, strictly positive radii. 381ˡs̲pheres ← { data → 382 n̲ot ((t̲ally data) c̲at 4) m̲atch s̲hape data ? @ p̲anic< "RayTrace spheres require S by 4 center/radius rows"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace spheres require S by 4 center/radius rows"))
383 a ← ʰf̲inite f̲loat data 384 n̲ot '∧ r̲/ (4 s̲elect₂ a) > 0.0 ? @ p̲anic< "RayTrace sphere radius must be positive"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace sphere radius must be positive"))
385 a 386} 387 388⍝│ Matched sphere rows and rays, stable quadratic roots using q and c/q. 389⍝│ Each result is t,0,0, with the same zero-miss convention as triangles. 390ʰs̲pherePairs ← { clip spheres origins directions → 391 delta ← origins − 3 t̲ake₂ spheres 392 a ← directions ᵛd̲ot directions 393 b ← delta ᵛd̲ot directions 394 radius ← 4 s̲elect₂ spheres 395 cc ← (delta ᵛd̲ot delta) − radius × radius 396 perpendicular ← delta ᵛc̲ross directions 397 disc ← ʰf̲inite (a × radius × radius) − perpendicular ᵛd̲ot perpendicular 398 root ← s̲qrt 0.0 m̲ax disc 399 sign ← 1.0 − 2.0 × f̲loat b < 0.0 400 q ← n̲eg b + sign × root 401 safe ← q + f̲loat q = 0.0 402 x ← q ÷ a 403 y ← cc ÷ safe 404 lo ← x m̲in y 405 hi ← x m̲ax y 406 near ← f̲irst clip 407 far ← f̲irst 1 d̲rop clip 408 firstok ← (lo ≥ near) ∧ lo ≤ far 409 secondok ← (hi ≥ near) ∧ hi ≤ far 410 t ← ((f̲loat firstok) × lo) + (f̲loat (n̲ot firstok) ∧ secondok) × hi 411 valid ← (disc ≥ 0.0) ∧ (firstok ∨ secondok) 412 ʰf̲inite ((f̲loat valid) × t) '× t̲able 1.0 0.0 0.0 413} 414 415⍝│ (((clip s_phereHit sphere)_ origins)_ directions): one length-4 sphere 416⍝│ against N rays, returning N by 3 t,0,0. Nonunit directions retain t units. 417ˡs̲phereHit ← { clip sphere origins directions → 418 c ← ʰc̲lip clip 419 s ← ˡs̲pheres 1 4 r̲eshape 4 ʰv̲ector sphere 420 o ← ʰb̲atch origins 421 d ← o ʰd̲irections directions 422 (((c ʰs̲pherePairs ((t̲ally o) c̲at 4) r̲eshape s) o) d) 423} 424 425⍝│ sphere s_phereNormals points: outward unit geometric normals at surface 426⍝│ points, independent of incident direction; points must lie on the sphere. 427ˡs̲phereNormals ← { sphere points → 428 s ← ˡs̲pheres 1 4 r̲eshape 4 ʰv̲ector sphere 429 p ← ʰb̲atch points 430 delta ← p − (t̲ally p) ʰr̲ows 3 t̲ake r̲avel s 431 radius ← f̲irst 3 d̲rop r̲avel s 432 n̲ot '∧ r̲/ (a̲bs ((ᵛl̲ength delta) ÷ radius) − 1.0) ≤ 1e-10 ? @ p̲anic< "RayTrace sphere normal points must lie on its surface"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace sphere normal points must lie on its surface"))
433 ᵛn̲ormalize delta 434} 435 436⍝│ Validate one positive unique global Int ID per primitive, across kinds. 437ʰg̲lobalIds ← { triangles spheres triangleids sphereids → 438 n̲ot (1 r̲eshape t̲ally triangles) m̲atch s̲hape triangleids ? @ p̲anic< "RayTrace needs one global ID per triangle"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace needs one global ID per triangle"))
439 n̲ot (1 r̲eshape t̲ally spheres) m̲atch s̲hape sphereids ? @ p̲anic< "RayTrace needs one global ID per sphere"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace needs one global ID per sphere"))
440 ids ← triangleids c̲at sphereids 441 n̲ot '∧ r̲/ ids > 0 ? @ p̲anic< "RayTrace global IDs must be positive"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace global IDs must be positive"))
442 n̲ot (t̲ally ids) = t̲ally u̲nique ids ? @ p̲anic< "RayTrace global IDs must be unique across geometry kinds"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace global IDs must be unique across geometry kinds"))
443 ids + 0 × ids m̲od 1 444} 445 446⍝│ Private global-ID lookup uses equality, 447⍝│ returning zero for a miss without encoding IDs in floating point. 448ʰl̲ocal ← { ids selected → 449 '{ id → 0 = id ? 0◆ at ← w̲here ids = id◆ 0 = t̲ally at ? 0◆ f̲irst at } e̲ach selected 450} 451 452⍝│ ((triangleids h_itKinds sphereids)_ selected): N Int tags, 0 miss, 453⍝│ 1 triangle, 2 sphere. Refuse unknown nonzero global IDs. 454ˡh̲itKinds ← { triangleids sphereids selected → 455 checked ← selected m̲od 1 456 allids ← (((triangleids ʰg̲lobalIds sphereids) triangleids) sphereids) 457 sn ← t̲ally allids m̲od 1 458 n̲ot 1 = t̲ally s̲hape selected ? @ p̲anic< "RayTrace selected global IDs must be a vector"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace selected global IDs must be a vector"))
459 n̲ot '∧ r̲/ (selected = 0) ∨ selected m̲ember? triangleids c̲at sphereids ? @ p̲anic< "RayTrace unknown selected global ID"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace unknown selected global ID"))
460 (f̲loor f̲loat selected m̲ember? triangleids) + (2 × f̲loor f̲loat selected m̲ember? sphereids) + 0 × checked + sn 461} 462 463⍝│ ((triangleids h_itLocals sphereids)_ selected): per-kind one-based 464⍝│ indices, separate from global IDs and kind tags; zero on misses. 465ˡh̲itLocals ← { triangleids sphereids selected → 466 kinds ← ((triangleids ˡh̲itKinds sphereids) selected) 467 (triangleids ʰl̲ocal selected) + (sphereids ʰl̲ocal selected) + 0 × kinds 468} 469 470⍝│ Resolve known global IDs to matched triangle/sphere hit data. 471ʰm̲ixedHits ← { clip triangles spheres triangleids sphereids selected origins directions → 472 ti ← triangleids ʰl̲ocal selected 473 si ← sphereids ʰl̲ocal selected 474 th ← ((((clip ʰh̲its triangles) ti) origins) directions) 475 0 = t̲ally spheres ? th 476 matched ← (si m̲ax 1) s̲elect spheres 477 sh ← (((clip ʰs̲pherePairs matched) origins) directions) 478 th + (f̲loat si > 0) ʰs̲cale sh 479} 480 481⍝│ (((((((clip m_ixedHits triangles)_ spheres)_ triangleids)_ sphereids) 482⍝│ _ selected)_ origins)_ directions): selected N by 3 t,u,v, sphere u/v zero. 483ˡm̲ixedHits ← { clip triangles spheres triangleids sphereids selected origins directions → 484 c ← ʰc̲lip clip 485 mesh ← ʰm̲esh triangles 486 balls ← ˡs̲pheres spheres 487 ids ← (((mesh ʰg̲lobalIds balls) triangleids) sphereids) 488 kinds ← ((triangleids ˡh̲itKinds sphereids) selected) 489 o ← ʰb̲atch origins 490 d ← o ʰd̲irections directions 491 n̲ot (s̲hape selected) m̲atch s̲hape f̲irst₂ o ? @ p̲anic< "RayTrace selected IDs must match rays"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace selected IDs must match rays"))
492 (((((((c ʰm̲ixedHits mesh) balls) triangleids) sphereids) selected + 0 × kinds) o) d) 493} 494 495⍝│ ((((((clip m_ixedNearest triangles)_ spheres)_ triangleids)_ sphereids) 496⍝│ _ origins)_ directions): global Int IDs, distance then smallest global 497⍝│ ID for exact ties, independent of per-kind row order and ray chunks. 498ˡm̲ixedNearest ← { clip triangles spheres triangleids sphereids origins directions → 499 c ← ʰc̲lip clip 500 mesh ← ʰm̲esh triangles 501 balls ← ˡs̲pheres spheres 502 allids ← (((mesh ʰg̲lobalIds balls) triangleids) sphereids) 503 o ← ʰb̲atch origins 504 d ← o ʰd̲irections directions 505 step̲ ← { state → 506 k ← f̲irst state 507 oldids ← 1 d̲rop state 508 id ← f̲irst k s̲elect allids 509 candidate ← (t̲ally o) r̲eshape id 510 hit ← (((((((c ʰm̲ixedHits mesh) balls) triangleids) sphereids) candidate) o) d) 511 prev ← (((((((c ʰm̲ixedHits mesh) balls) triangleids) sphereids) oldids) o) d) 512 t ← ˡd̲istances hit 513 old ← ˡd̲istances prev 514 choose ← (ˡm̲ask hit) ∧ ((oldids = 0) ∨ (t < old) ∨ (t = old) ∧ id < oldids) 515 next ← oldids + (f̲loor f̲loat choose) × id − oldids 516 (k + 1) c̲at next 517 } 518 1 d̲rop (t̲ally allids) 'step̲ p̲ower 1 c̲at (t̲ally o) r̲eshape 0 519} 520 521⍝│ (((camera r_aysAt size)_ pixels)_ film): pinhole directions with N by 2 522⍝│ subpixel offsets in [0,1], x right/y down. Centers [0.5,0.5] match r_ays. 523ˡr̲aysAt ← { camera size pixels film → 524 c ← ʰc̲amera camera 525 sz ← 2 ʰv̲ector size 526 n̲ot '∧ r̲/ (sz > 0.0) ∧ sz = f̲loat f̲loor sz ? @ p̲anic< "RayTrace image size must contain positive integers"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace image size must contain positive integers"))
527 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"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace pixel indices must be a vector within the image"))
528 n ← t̲ally pixels 529 n̲ot (n c̲at 2) m̲atch s̲hape film ? @ p̲anic< "RayTrace film samples require N by 2 offsets"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace film samples require N by 2 offsets"))
530 f ← ʰf̲inite f̲loat film 531 n̲ot '∧ r̲/ r̲avel (f ≥ 0.0) ∧ f ≤ 1.0 ? @ p̲anic< "RayTrace film offsets must be in 0..1"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace film offsets must be in 0..1"))
532 w ← f̲irst sz 533 ht ← f̲irst 1 d̲rop sz 534 forward ← ᵛn̲ormalize 1 3 r̲eshape (3 t̲ake 3 d̲rop c) − 3 t̲ake c 535 right ← ᵛn̲ormalize forward ᵛc̲ross 1 3 r̲eshape 3 t̲ake 6 d̲rop c 536 up ← right ᵛc̲ross forward 537 half ← 0.5 × f̲irst 9 d̲rop c 538 tangent ← (s̲in half) ÷ c̲os half 539 col ← (pixels − 1) m̲od f̲loor w 540 row ← (pixels − 1) d̲iv f̲loor w 541 xx ← tangent × (w ÷ ht) × ((2.0 × (1 s̲elect₂ f) + f̲loat col) ÷ w) − 1.0 542 yy ← tangent × 1.0 − (2.0 × (2 s̲elect₂ f) + f̲loat row) ÷ ht 543 ᵛ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 544} 545 546⍝│ (((camera l_ensRays lens)_ pinhole)_ disk): returns (N by 3 origins, 547⍝│ N by 3 directions). Lens is [aperture radius, forward focus distance]; 548⍝│ disk is N by 2 unit-disk coordinates. Zero aperture returns pinhole 549⍝│ directions unchanged, exactly. All input pinhole directions must be unit. 550ˡl̲ensRays ← { camera lens pinhole disk → 551 c ← ʰc̲amera camera 552 l ← 2 ʰv̲ector lens 553 radius ← f̲irst l 554 focus ← f̲irst 1 d̲rop l 555 (radius < 0.0) ∨ focus ≤ 0.0 ? @ p̲anic< "RayTrace lens requires nonnegative aperture and positive focus distance"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace lens requires nonnegative aperture and positive focus distance"))
556 d ← ʰb̲atch pinhole 557 n ← t̲ally d 558 n̲ot '∧ r̲/ (a̲bs (ᵛl̲ength d) − 1.0) ≤ 1e-10 ? @ p̲anic< "RayTrace lens pinhole directions must be unit"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace lens pinhole directions must be unit"))
559 n̲ot (n c̲at 2) m̲atch s̲hape disk ? @ p̲anic< "RayTrace lens requires N by 2 disk samples"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace lens requires N by 2 disk samples"))
560 ds ← ʰf̲inite f̲loat disk 561 n̲ot '∧ r̲/ ('+ r̲/₂ ds × ds) ≤ 1.0 + 1e-10 ? @ p̲anic< "RayTrace lens samples must lie in the unit disk"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace lens samples must lie in the unit disk"))
562 origins ← c ˡo̲rigins n 563 forward ← ᵛn̲ormalize 1 3 r̲eshape (3 t̲ake 3 d̲rop c) − 3 t̲ake c 564 right ← ᵛn̲ormalize forward ᵛc̲ross 1 3 r̲eshape 3 t̲ake 6 d̲rop c 565 up ← right ᵛc̲ross forward 566 axial ← d ᵛd̲ot n ʰr̲ows r̲avel forward 567 n̲ot '∧ r̲/ axial > 0.0 ? @ p̲anic< "RayTrace lens rays must point toward the camera focal plane"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace lens rays must point toward the camera focal plane"))
568 0.0 = radius ? (origins, d) 569 focal ← ʰf̲inite origins + (focus ÷ axial) ʰs̲cale d 570 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) 571 moved ← ʰf̲inite origins + offset 572 next ← ᵛn̲ormalize focal − moved 573 n̲ot '∧ r̲/ (ᵛl̲ength next) > 0.0 ? @ p̲anic< "RayTrace lens lost its focal direction at this numeric scale"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace lens lost its focal direction at this numeric scale"))
574 (moved, next) 575} 576 577"svg:" u̲se< "Svg" 578 579⍝│ Balanced joining keeps large pixel pictures from quadratic text copying. 580ʰp̲ictureText ← { boxes → 581 n ← t̲ally boxes 582 n = 0 ? "" 583 n = 1 ? d̲isclose f̲irst boxes 584 half ← n d̲iv 2 585 (ʰp̲ictureText half t̲ake boxes) c̲at ʰp̲ictureText half d̲rop boxes 586} 587 588⍝│ size p_icture displayRGB: row-major N by 3 sRGB values in 0..1 to an 589⍝│ SVG of square color pixels, shown at four times native dimensions. 590⍝│ Use exposure s_rgb linear first. This only displays computed pixels; 591⍝│ all lighting, shadows and reflections remain in the ray tracer. 592ˡp̲icture ← { size displayRGB → 593 sz ← 2 ʰv̲ector size 594 n̲ot '∧ r̲/ (sz > 0.0) ∧ sz = f̲loat f̲loor sz ? @ p̲anic< "RayTrace picture dimensions must be positive integers"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace picture dimensions must be positive integers"))
595 rgb ← ʰb̲atch displayRGB 596 wh ← f̲loor sz 597 n̲ot (t̲ally rgb) = '× r̲/ wh ? @ p̲anic< "RayTrace picture requires width times height RGB rows"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace picture requires width times height RGB rows"))
598 n̲ot '∧ r̲/ r̲avel (rgb ≥ 0.0) ∧ rgb ≤ 1.0 ? @ p̲anic< "RayTrace picture sRGB values must be in 0..1"
p̲anic< expands to
(⎕P̲ANIC ("RayTrace picture sRGB values must be in 0..1"))
599 width ← f̲irst wh 600 cells ← '{ i → 601 x ← (i − 1) m̲od width 602 y ← (i − 1) d̲iv width 603 corners ← 2 4 r̲eshape (x + 0 1 1 0) c̲at y + 0 0 1 1 604 (ˢᵛᵍf̲ill ˢᵛᵍr̲gb 255.0 × i s̲elect rgb) ˢᵛᵍp̲olygon corners 605 } m̲ap r̲ange t̲ally rgb 606 attrs ← (ˢᵛᵍs̲cale 4) c̲at "shape-rendering" ˢᵛᵍa̲ttr "crispEdges" 607 (4 × wh) ˢᵛᵍp̲icture attrs ˢᵛᵍg̲roup ʰp̲ictureText cells 608} 609 610⍝│ size s_how! displayRGB: show the computed color image in the live demo 611⍝│ or write it through the CLI's --draw directory; return the SVG text. 612ˡs̲how! ← { size displayRGB → ⎕S̲HOW size ˡp̲icture displayRGB }