librarylibs/Scene/src/Scene.xtl
Scene: validated v1/v2 numeric arrays, closed solids and rigid groups. Import with "sc:" u_se< "Scene". Functions depend on Extrusion, Vector3, Matrix and RayTrace; the macro also uses Parsing and Strings. Separate arrays keep Int IDs out of Float material/transform data.
ʰf̲inite : Float -> Float
Reject nonfinite inputs or arithmetic overflow.
ʰf̲inite ← { a → n̲ot '∧ r̲/ r̲avel (a − a) = 0.0 ? @ p̲anic< "Scene requires finite numbers and results" a }
ʰr̲ows : Num a => Int -> a -> Float
Validate an N by columns matrix and convert to Float.
ʰr̲ows ← { columns a → n̲ot ((t̲ally a) c̲at columns) m̲atch s̲hape a ? @ p̲anic< "Scene matrix has the wrong row width" ʰf̲inite f̲loat a }
ˡc̲amera : Num a => a -> Float
c_amera data: validated length-10 eye/target/up/fov, using RayTrace's camera basis and fov checks; returns Float data without generating rays.
ˡc̲amera ← { data → c ← ʰf̲inite f̲loat data n ← t̲ally c ʳᵗo̲rigins 0 c + f̲loat n }
ˡm̲aterials : Num a => a -> Float
m_aterials rows: M by 4 linear RGB/reflection, every value in 0..1.
ˡm̲aterials ← { rows → m ← 4 ʰr̲ows rows n̲ot '∧ r̲/ r̲avel (m ≥ 0.0) ∧ m ≤ 1.0 ? @ p̲anic< "Scene material RGB and reflection must be in 0..1" m }
ˡl̲ights : Num a => a -> Float
l_ights rows: L by 6 point position/intensity, nonnegative RGB intensity.
ˡl̲ights ← { rows → a ← 6 ʰr̲ows rows n̲ot '∧ r̲/ r̲avel (3 d̲rop₂ a) ≥ 0.0 ? @ p̲anic< "Scene light intensity must be nonnegative" a }
ˡi̲ds : Int -> Int -> Int
count i_ds ids: rank-1 one-based Int IDs into count rows; empty is valid.
ˡi̲ds ← { count ids → n̲ot 0 = t̲ally s̲hape count ? @ p̲anic< "Scene ID row count must be scalar" count < 0 ? @ p̲anic< "Scene ID row count must be nonnegative" n̲ot 1 = t̲ally s̲hape ids ? @ p̲anic< "Scene IDs must be a vector" n̲ot '∧ r̲/ (ids ≥ 1) ∧ ids ≤ count ? @ p̲anic< "Scene ID is outside its row range" ids + 0 × count m̲od 1 }
ˡt̲riangles : Num a => a -> Float
t_riangles mesh: finite T by 3 by 3 Float world/group-local triangles.
ˡt̲riangles ← { mesh → n̲ot ((t̲ally mesh) c̲at 3 3) m̲atch s̲hape mesh ? @ p̲anic< "Scene requires T by 3 by 3 triangles" ʰf̲inite f̲loat mesh }
ˡi̲dentities : Int -> Float
i_dentities count: count rigid identity matrices, including row 1.
ˡi̲dentities ← { count → n̲ot 0 = t̲ally s̲hape count ? @ p̲anic< "Scene group count must be scalar" count < 1 ? @ p̲anic< "Scene needs at least the identity group" (count c̲at 4 4) r̲eshape f̲loat ᵐˣi̲dentity 4 }
ˡp̲ivots : Num a => a -> Float
p_ivots rows: G by 3 Float local pivots; reserved identity pivot is zero.
ˡp̲ivots ← { rows → p ← 3 ʰr̲ows rows 0 = t̲ally p ? @ p̲anic< "Scene needs an identity pivot row" n̲ot '∧ r̲/ (f̲irst p) = 0.0 ? @ p̲anic< "Scene identity pivot must be zero" p }
ʰr̲igid : Num a => a -> Int -> a
Check every matrix through Vector3's rigid-transform validator.
ʰr̲igid ← { transforms i → i > t̲ally transforms ? transforms checked ← (i s̲elect transforms) ᵛt̲ransform 0 3 r̲eshape 0.0 n̲ot 0 3 m̲atch s̲hape checked ? @ p̲anic< "Scene invalid rigid matrix" transforms ʰr̲igid i + 1 }
ˡt̲ransforms : Num a => a -> Float
t_ransforms data: G by 4 by 4 proper rigid matrices; row 1 is identity.
ˡt̲ransforms ← { data → n̲ot ((t̲ally data) c̲at 4 4) m̲atch s̲hape data ? @ p̲anic< "Scene requires G by 4 by 4 transforms" 0 = t̲ally data ? @ p̲anic< "Scene needs an identity transform row" m ← ʰf̲inite f̲loat data n̲ot '∧ r̲/ r̲avel (a̲bs (f̲irst m) − f̲loat ᵐˣi̲dentity 4) ≤ 1e-10 ? @ p̲anic< "Scene transform row 1 must be identity" m ʰr̲igid 1 }
ˡo̲bject : (Num a, Num b) => a -> Char -> b -> Float
((depth o_bject text)_ offset): original letter mesh translated by xyz.
ˡo̲bject ← { depth text offset → mesh ← depth ᵉˣl̲etters text points ← ((3 × t̲ally mesh) c̲at 3) r̲eshape mesh (s̲hape mesh) r̲eshape offset ᵛt̲ranslate points }
ˡr̲otateGroup : (Num a, Num b, Num c) => a -> Int -> b -> c -> Float
(((pivots r_otateGroup group)_ angle)_ transforms): compose a local pivot y rotation on the right of one group matrix, preserving all others. Group 1 is reserved; call with the base transforms for absolute frames.
ˡr̲otateGroup ← { pivots group angle transforms → p ← ˡp̲ivots pivots m ← ˡt̲ransforms transforms n̲ot (t̲ally p) = t̲ally m ? @ p̲anic< "Scene pivots and transforms must have the same group count" n̲ot 0 = t̲ally s̲hape group ? @ p̲anic< "Scene group ID must be scalar" (group < 2) ∨ group > t̲ally m ? @ p̲anic< "Scene rotating group must be declared, never identity row 1" pivot ← group s̲elect p rotation ← (ᵛt̲ranslation pivot) ᵐˣm̲ul (ᵛr̲otationY angle) ᵐˣm̲ul ᵛt̲ranslation n̲eg pivot old ← group s̲elect m delta ← (old ᵐˣm̲ul rotation) − old mask ← f̲loat (r̲ange t̲ally m) = group m + (s̲hape m) r̲eshape mask '× t̲able r̲avel delta }
ʰa̲pply : Num a => Float -> Int -> a -> Int -> Float -> Float
One matrix per group, applied only to that group's original vertices.
ʰa̲pply ← { mesh ids transforms i accum → i > t̲ally transforms ? accum points ← ((3 × t̲ally mesh) c̲at 3) r̲eshape mesh moved ← (i s̲elect transforms) ᵛt̲ransform points delta ← (s̲hape mesh) r̲eshape moved − points mask ← f̲loat ids = i next ← accum + delta × (s̲hape mesh) r̲eshape mask '× t̲able 9 r̲eshape 1.0 ((((mesh ʰa̲pply ids) transforms) i + 1) next) }
ˡa̲pplyGroups : (Num a, Num b) => a -> Int -> b -> Float
((triangles a_pplyGroups groupids)_ transforms): transformed mesh in the same triangle order; IDs remain separate, with one group per face.
ˡa̲pplyGroups ← { triangles groupids transforms → mesh ← ˡt̲riangles triangles m ← ˡt̲ransforms transforms ids ← (t̲ally m) ˡi̲ds groupids n̲ot (t̲ally mesh) = t̲ally ids ? @ p̲anic< "Scene needs one group ID per triangle" ʰf̲inite ((((mesh ʰa̲pply ids) m) 1) mesh) }
ˡo̲paque : Unit -> Int
o_paque @: v2 material tag 0, preserving v1 reflection semantics.
ˡo̲paque ← { @ → 0 m̲od 1 }
ˡd̲ielectric : Unit -> Int
d_ielectric @: v2 material tag 1; transport is a separate renderer API.
ˡd̲ielectric ← { @ → 1 + 0 m̲od 1 }
ˡs̲pheres : Num a => a -> Float
s_pheres data: S by 4 finite center xyz and positive radius.
ˡs̲pheres ← { data → ʳᵗs̲pheres data }
ˡm̲aterialKinds : Int -> Int
m_aterialKinds tags: rank-1 Int opaque/dielectric tags (0/1).
ˡm̲aterialKinds ← { tags → n̲ot 1 = t̲ally s̲hape tags ? @ p̲anic< "Scene material kinds must be a vector" n̲ot '∧ r̲/ (tags = ˡo̲paque @) ∨ tags = ˡd̲ielectric @ ? @ p̲anic< "Scene material kind must be opaque or dielectric" tags + 0 × tags m̲od 1 }
ˡo̲ptics : Num a => a -> Float
o_ptics data: M by 5 transmission 0..1, positive IOR, nonnegative RGB absorption per scene unit. Row counts are checked by the scene macro.
ˡo̲ptics ← { data → a ← 5 ʰr̲ows data weight ← 1 s̲elect₂ a n̲ot '∧ r̲/ (weight ≥ 0.0) ∧ weight ≤ 1.0 ? @ p̲anic< "Scene transmission must be in 0..1" n̲ot '∧ r̲/ (2 s̲elect₂ a) > 0.0 ? @ p̲anic< "Scene IOR must be positive" n̲ot '∧ r̲/ r̲avel (2 d̲rop₂ a) ≥ 0.0 ? @ p̲anic< "Scene absorption must be nonnegative" a }
ˡe̲mission : Num a => a -> Float
e_mission data: M by 3 finite nonnegative linear emitted radiance.
ˡe̲mission ← { data → a ← 3 ʰr̲ows data n̲ot '∧ r̲/ r̲avel a ≥ 0.0 ? @ p̲anic< "Scene emission must be nonnegative" a }
ˡo̲paqueOptics : Num a => a -> Float
o_paqueOptics materials: v1 adapter defaults [0,1,0,0,0] per row.
ˡo̲paqueOptics ← { materials → m ← ˡm̲aterials materials ((t̲ally m) c̲at 5) r̲eshape 0.0 1.0 0.0 0.0 0.0 }
ˡl̲ens : Num a => a -> Float
l_ens data: aperture radius >=0 and positive focus distance, scene units.
ˡl̲ens ← { data → n̲ot (1 r̲eshape 2) m̲atch s̲hape data ? @ p̲anic< "Scene lens must have aperture and focus distance" a ← ʰf̲inite f̲loat data ((f̲irst a) < 0.0) ∨ (f̲irst 1 d̲rop a) ≤ 0.0 ? @ p̲anic< "Scene lens requires nonnegative aperture and positive focus distance" a }
ˡr̲ectangle : Num a => a -> Float
r_ectangle data: origin,u,v rows form two finite nondegenerate triangles. Winding follows u cross v; this is a finite surface, not a closed object.
ˡr̲ectangle ← { data → n̲ot 3 3 m̲atch s̲hape data ? @ p̲anic< "Scene rectangle requires origin,u,v rows" a ← ʰf̲inite f̲loat data origin ← 1 s̲elect a u ← 2 s̲elect a v ← 3 s̲elect a 0.0 = f̲irst ᵛl̲ength (1 3 r̲eshape u) ᵛc̲ross 1 3 r̲eshape v ? @ p̲anic< "Scene rectangle spans must be independent" ˡt̲riangles 2 3 3 r̲eshape origin c̲at (origin + u) c̲at (origin + u + v) c̲at origin c̲at (origin + u + v) c̲at origin + v }
ʰe̲dge : (Eq a, Truthy b) => a -> a -> Int -> b
Check exact reverse edge pairing on normalized supplied coordinates.
ʰe̲dge ← { starts ends i → a ← i s̲elect starts b ← i s̲elect ends same ← ('∧ r̲/₂ starts = ((t̲ally starts) c̲at 3) r̲eshape a) ∧ '∧ r̲/₂ ends = ((t̲ally ends) c̲at 3) r̲eshape b reverse ← ('∧ r̲/₂ starts = ((t̲ally starts) c̲at 3) r̲eshape b) ∧ '∧ r̲/₂ ends = ((t̲ally ends) c̲at 3) r̲eshape a (1 = '+ r̲/ f̲loor f̲loat same) ∧ 1 = '+ r̲/ f̲loor f̲loat reverse }
ˡc̲losed : Num a => a -> Float
c_losed mesh: finite nonempty convex closed outward triangle shell. Exact shared vertices, exactly one reverse edge, nonzero faces, and all vertices on/inside every outward plane; normalized tolerance is 1e-10.
ˡc̲losed ← { mesh → a ← ˡt̲riangles mesh (t̲ally a) < 4 ? @ p̲anic< "Scene closed mesh requires at least four faces" points ← ((3 × t̲ally a) c̲at 3) r̲eshape a local ← points − (s̲hape points) r̲eshape f̲irst points span ← 'm̲ax r̲/ r̲avel a̲bs local span ≤ 0.0 ? @ p̲anic< "Scene closed mesh has no extent" q ← ʰf̲inite (s̲hape a) r̲eshape local ÷ span p ← (s̲hape points) r̲eshape q first ← 1 s̲elect₂ q second ← 2 s̲elect₂ q third ← 3 s̲elect₂ q normals ← (second − first) ᵛc̲ross third − first n̲ot '∧ r̲/ (ᵛl̲ength normals) > 1e-10 ? @ p̲anic< "Scene closed mesh has degenerate faces" starts ← first c̲at second c̲at third ends ← second c̲at third c̲at first n̲ot '∧ r̲/ '{ i → ((starts ʰe̲dge ends) i) } e̲ach r̲ange t̲ally starts ? @ p̲anic< "Scene closed mesh requires exactly paired reverse edges" unit ← ᵛn̲ormalize normals convex ← '{ i → n ← i s̲elect unit delta ← p − (s̲hape p) r̲eshape i s̲elect first '∧ r̲/ ('+ r̲/₂ delta × (s̲hape p) r̲eshape n) ≤ 1e-10 } e̲ach r̲ange t̲ally q n̲ot '∧ r̲/ convex ? @ p̲anic< "Scene closed mesh must be convex with outward winding" volume ← '+ r̲/ first ᵛd̲ot second ᵛc̲ross third volume ≤ 1e-10 ? @ p̲anic< "Scene closed mesh requires positive enclosed volume" a }
ˡb̲ox : Num a => a -> Float
b_ox size: positive xyz full side lengths, centered closed outward mesh.
ˡb̲ox ← { size → n̲ot (1 r̲eshape 3) m̲atch s̲hape size ? @ p̲anic< "Scene box size must be xyz" s ← ʰf̲inite f̲loat size n̲ot '∧ r̲/ s > 0.0 ? @ p̲anic< "Scene box side lengths must be positive" x ← 0.5 × f̲irst s y ← 0.5 × f̲irst 1 d̲rop s z ← f̲irst 2 d̲rop s contour ← 2 4 r̲eshape (n̲eg x) c̲at x c̲at x c̲at (n̲eg x) c̲at (n̲eg y) c̲at (n̲eg y) c̲at y c̲at y ˡc̲losed z ᵉˣe̲xtrude contour }
ˡo̲ctahedron : Num a => a -> Float
o_ctahedron radii: positive xyz axis radii, eight outward faces.
ˡo̲ctahedron ← { radii → n̲ot (1 r̲eshape 3) m̲atch s̲hape radii ? @ p̲anic< "Scene octahedron radii must be xyz" r ← ʰf̲inite f̲loat radii n̲ot '∧ r̲/ r > 0.0 ? @ p̲anic< "Scene octahedron radii must be positive" vertices ← (6 3 r̲eshape 1 0 0 -1 0 0 0 1 0 0 -1 0 0 0 1 0 0 -1) × 6 3 r̲eshape r indices ← 1 3 5 3 2 5 2 4 5 4 1 5 3 1 6 2 3 6 4 2 6 1 4 6 ˡc̲losed 8 3 3 r̲eshape indices s̲elect vertices }
ˡa̲pplySphereGroups : (Num a, Num b) => a -> Int -> b -> Float
((spheres a_pplySphereGroups groupids)_ transforms): rigid centers, unchanged radii and row order. Scaling/shear/reflection matrices refuse.
ˡa̲pplySphereGroups ← { spheres groupids transforms → s ← ˡs̲pheres spheres m ← ˡt̲ransforms transforms ids ← (t̲ally m) ˡi̲ds groupids n̲ot (t̲ally ids) = t̲ally s ? @ p̲anic< "Scene needs one group ID per sphere" centers ← 3 t̲ake₂ s moved ← '{ i → group ← f̲irst i s̲elect ids r̲avel (group s̲elect m) ᵛt̲ransform 1 3 r̲eshape i s̲elect centers } m̲ap r̲ange t̲ally s flat ← { @ → 0 = t̲ally moved ? 0 r̲eshape 0.0◆ d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at d̲isclose ⍵ } r̲/ moved } @ ˡs̲pheres o̲\ (o̲\ ((t̲ally s) c̲at 3) r̲eshape flat) c̲at ((1 c̲at t̲ally s) r̲eshape 4 s̲elect₂ s) }
ˡe̲mitters : Num a => a -> Float
e_mitters data: E by 3 by 3 finite origin,u,v rectangles, nonzero area.
ˡe̲mitters ← { data → a ← ˡt̲riangles data cross ← (2 s̲elect₂ a) ᵛc̲ross 3 s̲elect₂ a n̲ot '∧ r̲/ (ᵛl̲ength cross) > 0.0 ? @ p̲anic< "Scene emitter spans must have nonzero area" cosine ← (ᵛn̲ormalize 2 s̲elect₂ a) ᵛd̲ot ᵛn̲ormalize 3 s̲elect₂ a n̲ot '∧ r̲/ (a̲bs cosine) ≤ 1e-10 ? @ p̲anic< "Scene emitter rectangle spans must be perpendicular" a }
ˡe̲mitterAreas : Num a => a -> Float
e_mitterAreas data: E positive Float rectangle areas in scene units squared.
ˡe̲mitterAreas ← { data → a ← ˡe̲mitters data ʰf̲inite ᵛl̲ength (2 s̲elect₂ a) ᵛc̲ross 3 s̲elect₂ a }
ˡe̲mitterNormals : Num a => a -> Float
e_mitterNormals data: E by 3 unit normals, u cross v, one-sided emission.
ˡe̲mitterNormals ← { data → a ← ˡe̲mitters data ᵛn̲ormalize (2 s̲elect₂ a) ᵛc̲ross 3 s̲elect₂ a }
ˡe̲mitterPoints : (Num a, Num b) => a -> b -> (Float, Float)
emitter e_mitterPoints square: one 3 by 3 rectangle and N by 2 unit square samples to (N by 3 points, N Float uniform-area PDF 1/area).
ˡe̲mitterPoints ← { emitter square → n̲ot 3 3 m̲atch s̲hape emitter ? @ p̲anic< "Scene sampling requires one emitter rectangle" a ← ˡe̲mitters 1 3 3 r̲eshape emitter n ← t̲ally square uv ← 2 ʰr̲ows square n̲ot '∧ r̲/ r̲avel (uv ≥ 0.0) ∧ uv ≤ 1.0 ? @ p̲anic< "Scene emitter samples must lie in the unit square" origin ← r̲avel 1 s̲elect₂ a u ← r̲avel 2 s̲elect₂ a v ← r̲avel 3 s̲elect₂ a points ← (n r̲eshape 1.0) '× t̲able origin offset ← ((1 s̲elect₂ uv) '× t̲able u) + (2 s̲elect₂ uv) '× t̲able v pdf ← ʰf̲inite 1.0 ÷ f̲irst ˡe̲mitterAreas a (ʰf̲inite points + offset, n r̲eshape pdf) }
ˡt̲ransformEmitters : (Num a, Num b) => a -> b -> Float
matrix t_ransformEmitters data: proper rigid transform of rectangle origins and spans, preserving area and descriptor order. Transform the matching triangle geometry by the same matrix before transport preflight.
ˡt̲ransformEmitters ← { matrix data → a ← ˡe̲mitters data n ← t̲ally a origin ← 1 s̲elect₂ a moved ← matrix ᵛt̲ransform origin u ← (matrix ᵛt̲ransform origin + 2 s̲elect₂ a) − moved v ← (matrix ᵛt̲ransform origin + 3 s̲elect₂ a) − moved boxes ← '{ i → 3 3 r̲eshape (i s̲elect moved) c̲at (i s̲elect u) c̲at i s̲elect v } m̲ap r̲ange n flat ← { @ → n = 0 ? 0 r̲eshape 0.0◆ r̲avel d̲isclose '{ e̲nclose (r̲avel d̲isclose ⍺) c̲at r̲avel d̲isclose ⍵ } r̲/ boxes } @ ˡe̲mitters (n c̲at 3 3) r̲eshape flat }