sourcelibs/Scene/src/Scene.xtl
1⍝│ Scene: validated v1/v2 numeric arrays, closed solids and rigid groups.
2⍝│ Import with "sc:" u_se< "Scene". Functions depend on Extrusion,
3⍝│ Vector3, Matrix and RayTrace; the macro also uses Parsing and Strings.
4⍝│ Separate arrays keep Int IDs out of Float material/transform data.
5
6"ex:" u̲se< "Extrusion"
7"v:" u̲se< "Vector3"
8"mx:" u̲se< "Matrix"
9"rt:" u̲se< "RayTrace"
10
11⍝│ Reject nonfinite inputs or arithmetic overflow.
12ʰf̲inite ← { a →
13 n̲ot '∧ r̲/ r̲avel (a − a) = 0.0 ? @ p̲anic< "Scene requires finite numbers and results"
14 a
15}
16
17⍝│ Validate an N by columns matrix and convert to Float.
18ʰr̲ows ← { columns a →
19 n̲ot ((t̲ally a) c̲at columns) m̲atch s̲hape a ? @ p̲anic< "Scene matrix has the wrong row width"
20 ʰf̲inite f̲loat a
21}
22
23⍝│ c_amera data: validated length-10 eye/target/up/fov, using RayTrace's
24⍝│ camera basis and fov checks; returns Float data without generating rays.
25ˡc̲amera ← { data →
26 c ← ʰf̲inite f̲loat data
27 n ← t̲ally c ʳᵗo̲rigins 0
28 c + f̲loat n
29}
30
31⍝│ m_aterials rows: M by 4 linear RGB/reflection, every value in 0..1.
32ˡm̲aterials ← { rows →
33 m ← 4 ʰr̲ows rows
34 n̲ot '∧ r̲/ r̲avel (m ≥ 0.0) ∧ m ≤ 1.0 ? @ p̲anic< "Scene material RGB and reflection must be in 0..1"
35 m
36}
37
38⍝│ l_ights rows: L by 6 point position/intensity, nonnegative RGB intensity.
39ˡl̲ights ← { rows →
40 a ← 6 ʰr̲ows rows
41 n̲ot '∧ r̲/ r̲avel (3 d̲rop₂ a) ≥ 0.0 ? @ p̲anic< "Scene light intensity must be nonnegative"
42 a
43}
44
45⍝│ count i_ds ids: rank-1 one-based Int IDs into count rows; empty is valid.
46ˡi̲ds ← { count ids →
47 n̲ot 0 = t̲ally s̲hape count ? @ p̲anic< "Scene ID row count must be scalar"
48 count < 0 ? @ p̲anic< "Scene ID row count must be nonnegative"
49 n̲ot 1 = t̲ally s̲hape ids ? @ p̲anic< "Scene IDs must be a vector"
50 n̲ot '∧ r̲/ (ids ≥ 1) ∧ ids ≤ count ? @ p̲anic< "Scene ID is outside its row range"
51 ids + 0 × count m̲od 1
52}
53
54⍝│ t_riangles mesh: finite T by 3 by 3 Float world/group-local triangles.
55ˡt̲riangles ← { mesh →
56 n̲ot ((t̲ally mesh) c̲at 3 3) m̲atch s̲hape mesh ? @ p̲anic< "Scene requires T by 3 by 3 triangles"
57 ʰf̲inite f̲loat mesh
58}
59
60⍝│ i_dentities count: count rigid identity matrices, including row 1.
61ˡi̲dentities ← { count →
62 n̲ot 0 = t̲ally s̲hape count ? @ p̲anic< "Scene group count must be scalar"
63 count < 1 ? @ p̲anic< "Scene needs at least the identity group"
64 (count c̲at 4 4) r̲eshape f̲loat ᵐˣi̲dentity 4
65}
66
67⍝│ p_ivots rows: G by 3 Float local pivots; reserved identity pivot is zero.
68ˡp̲ivots ← { rows →
69 p ← 3 ʰr̲ows rows
70 0 = t̲ally p ? @ p̲anic< "Scene needs an identity pivot row"
71 n̲ot '∧ r̲/ (f̲irst p) = 0.0 ? @ p̲anic< "Scene identity pivot must be zero"
72 p
73}
74
75⍝│ Check every matrix through Vector3's rigid-transform validator.
76ʰr̲igid ← { transforms i →
77 i > t̲ally transforms ? transforms
78 checked ← (i s̲elect transforms) ᵛt̲ransform 0 3 r̲eshape 0.0
79 n̲ot 0 3 m̲atch s̲hape checked ? @ p̲anic< "Scene invalid rigid matrix"
80 transforms ʰr̲igid i + 1
81}
82
83⍝│ t_ransforms data: G by 4 by 4 proper rigid matrices; row 1 is identity.
84ˡt̲ransforms ← { data →
85 n̲ot ((t̲ally data) c̲at 4 4) m̲atch s̲hape data ? @ p̲anic< "Scene requires G by 4 by 4 transforms"
86 0 = t̲ally data ? @ p̲anic< "Scene needs an identity transform row"
87 m ← ʰf̲inite f̲loat data
88 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"
89 m ʰr̲igid 1
90}
91
92⍝│ ((depth o_bject text)_ offset): original letter mesh translated by xyz.
93ˡo̲bject ← { depth text offset →
94 mesh ← depth ᵉˣl̲etters text
95 points ← ((3 × t̲ally mesh) c̲at 3) r̲eshape mesh
96 (s̲hape mesh) r̲eshape offset ᵛt̲ranslate points
97}
98
99⍝│ (((pivots r_otateGroup group)_ angle)_ transforms): compose a local
100⍝│ pivot y rotation on the right of one group matrix, preserving all others.
101⍝│ Group 1 is reserved; call with the base transforms for absolute frames.
102ˡr̲otateGroup ← { pivots group angle transforms →
103 p ← ˡp̲ivots pivots
104 m ← ˡt̲ransforms transforms
105 n̲ot (t̲ally p) = t̲ally m ? @ p̲anic< "Scene pivots and transforms must have the same group count"
106 n̲ot 0 = t̲ally s̲hape group ? @ p̲anic< "Scene group ID must be scalar"
107 (group < 2) ∨ group > t̲ally m ? @ p̲anic< "Scene rotating group must be declared, never identity row 1"
108 pivot ← group s̲elect p
109 rotation ← (ᵛt̲ranslation pivot) ᵐˣm̲ul (ᵛr̲otationY angle) ᵐˣm̲ul ᵛt̲ranslation n̲eg pivot
110 old ← group s̲elect m
111 delta ← (old ᵐˣm̲ul rotation) − old
112 mask ← f̲loat (r̲ange t̲ally m) = group
113 m + (s̲hape m) r̲eshape mask '× t̲able r̲avel delta
114}
115
116⍝│ One matrix per group, applied only to that group's original vertices.
117ʰa̲pply ← { mesh ids transforms i accum →
118 i > t̲ally transforms ? accum
119 points ← ((3 × t̲ally mesh) c̲at 3) r̲eshape mesh
120 moved ← (i s̲elect transforms) ᵛt̲ransform points
121 delta ← (s̲hape mesh) r̲eshape moved − points
122 mask ← f̲loat ids = i
123 next ← accum + delta × (s̲hape mesh) r̲eshape mask '× t̲able 9 r̲eshape 1.0
124 ((((mesh ʰa̲pply ids) transforms) i + 1) next)
125}
126
127⍝│ ((triangles a_pplyGroups groupids)_ transforms): transformed mesh in
128⍝│ the same triangle order; IDs remain separate, with one group per face.
129ˡa̲pplyGroups ← { triangles groupids transforms →
130 mesh ← ˡt̲riangles triangles
131 m ← ˡt̲ransforms transforms
132 ids ← (t̲ally m) ˡi̲ds groupids
133 n̲ot (t̲ally mesh) = t̲ally ids ? @ p̲anic< "Scene needs one group ID per triangle"
134 ʰf̲inite ((((mesh ʰa̲pply ids) m) 1) mesh)
135}
136
137⍝│ o_paque @: v2 material tag 0, preserving v1 reflection semantics.
138ˡo̲paque ← { @ → 0 m̲od 1 }
139
140⍝│ d_ielectric @: v2 material tag 1; transport is a separate renderer API.
141ˡd̲ielectric ← { @ → 1 + 0 m̲od 1 }
142
143⍝│ s_pheres data: S by 4 finite center xyz and positive radius.
144ˡs̲pheres ← { data → ʳᵗs̲pheres data }
145
146⍝│ m_aterialKinds tags: rank-1 Int opaque/dielectric tags (0/1).
147ˡm̲aterialKinds ← { tags →
148 n̲ot 1 = t̲ally s̲hape tags ? @ p̲anic< "Scene material kinds must be a vector"
149 n̲ot '∧ r̲/ (tags = ˡo̲paque @) ∨ tags = ˡd̲ielectric @ ? @ p̲anic< "Scene material kind must be opaque or dielectric"
150 tags + 0 × tags m̲od 1
151}
152
153⍝│ o_ptics data: M by 5 transmission 0..1, positive IOR, nonnegative RGB
154⍝│ absorption per scene unit. Row counts are checked by the scene macro.
155ˡo̲ptics ← { data →
156 a ← 5 ʰr̲ows data
157 weight ← 1 s̲elect₂ a
158 n̲ot '∧ r̲/ (weight ≥ 0.0) ∧ weight ≤ 1.0 ? @ p̲anic< "Scene transmission must be in 0..1"
159 n̲ot '∧ r̲/ (2 s̲elect₂ a) > 0.0 ? @ p̲anic< "Scene IOR must be positive"
160 n̲ot '∧ r̲/ r̲avel (2 d̲rop₂ a) ≥ 0.0 ? @ p̲anic< "Scene absorption must be nonnegative"
161 a
162}
163
164⍝│ e_mission data: M by 3 finite nonnegative linear emitted radiance.
165ˡe̲mission ← { data →
166 a ← 3 ʰr̲ows data
167 n̲ot '∧ r̲/ r̲avel a ≥ 0.0 ? @ p̲anic< "Scene emission must be nonnegative"
168 a
169}
170
171⍝│ o_paqueOptics materials: v1 adapter defaults [0,1,0,0,0] per row.
172ˡo̲paqueOptics ← { materials →
173 m ← ˡm̲aterials materials
174 ((t̲ally m) c̲at 5) r̲eshape 0.0 1.0 0.0 0.0 0.0
175}
176
177⍝│ l_ens data: aperture radius >=0 and positive focus distance, scene units.
178ˡl̲ens ← { data →
179 n̲ot (1 r̲eshape 2) m̲atch s̲hape data ? @ p̲anic< "Scene lens must have aperture and focus distance"
180 a ← ʰf̲inite f̲loat data
181 ((f̲irst a) < 0.0) ∨ (f̲irst 1 d̲rop a) ≤ 0.0 ? @ p̲anic< "Scene lens requires nonnegative aperture and positive focus distance"
182 a
183}
184
185⍝│ r_ectangle data: origin,u,v rows form two finite nondegenerate triangles.
186⍝│ Winding follows u cross v; this is a finite surface, not a closed object.
187ˡr̲ectangle ← { data →
188 n̲ot 3 3 m̲atch s̲hape data ? @ p̲anic< "Scene rectangle requires origin,u,v rows"
189 a ← ʰf̲inite f̲loat data
190 origin ← 1 s̲elect a
191 u ← 2 s̲elect a
192 v ← 3 s̲elect a
193 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"
194 ˡ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
195}
196
197⍝│ Check exact reverse edge pairing on normalized supplied coordinates.
198ʰe̲dge ← { starts ends i →
199 a ← i s̲elect starts
200 b ← i s̲elect ends
201 same ← ('∧ r̲/₂ starts = ((t̲ally starts) c̲at 3) r̲eshape a) ∧ '∧ r̲/₂ ends = ((t̲ally ends) c̲at 3) r̲eshape b
202 reverse ← ('∧ r̲/₂ starts = ((t̲ally starts) c̲at 3) r̲eshape b) ∧ '∧ r̲/₂ ends = ((t̲ally ends) c̲at 3) r̲eshape a
203 (1 = '+ r̲/ f̲loor f̲loat same) ∧ 1 = '+ r̲/ f̲loor f̲loat reverse
204}
205
206⍝│ c_losed mesh: finite nonempty convex closed outward triangle shell.
207⍝│ Exact shared vertices, exactly one reverse edge, nonzero faces, and all
208⍝│ vertices on/inside every outward plane; normalized tolerance is 1e-10.
209ˡc̲losed ← { mesh →
210 a ← ˡt̲riangles mesh
211 (t̲ally a) < 4 ? @ p̲anic< "Scene closed mesh requires at least four faces"
212 points ← ((3 × t̲ally a) c̲at 3) r̲eshape a
213 local ← points − (s̲hape points) r̲eshape f̲irst points
214 span ← 'm̲ax r̲/ r̲avel a̲bs local
215 span ≤ 0.0 ? @ p̲anic< "Scene closed mesh has no extent"
216 q ← ʰf̲inite (s̲hape a) r̲eshape local ÷ span
217 p ← (s̲hape points) r̲eshape q
218 first ← 1 s̲elect₂ q
219 second ← 2 s̲elect₂ q
220 third ← 3 s̲elect₂ q
221 normals ← (second − first) ᵛc̲ross third − first
222 n̲ot '∧ r̲/ (ᵛl̲ength normals) > 1e-10 ? @ p̲anic< "Scene closed mesh has degenerate faces"
223 starts ← first c̲at second c̲at third
224 ends ← second c̲at third c̲at first
225 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"
226 unit ← ᵛn̲ormalize normals
227 convex ← '{ i →
228 n ← i s̲elect unit
229 delta ← p − (s̲hape p) r̲eshape i s̲elect first
230 '∧ r̲/ ('+ r̲/₂ delta × (s̲hape p) r̲eshape n) ≤ 1e-10
231 } e̲ach r̲ange t̲ally q
232 n̲ot '∧ r̲/ convex ? @ p̲anic< "Scene closed mesh must be convex with outward winding"
233 volume ← '+ r̲/ first ᵛd̲ot second ᵛc̲ross third
234 volume ≤ 1e-10 ? @ p̲anic< "Scene closed mesh requires positive enclosed volume"
235 a
236}
237
238⍝│ b_ox size: positive xyz full side lengths, centered closed outward mesh.
239ˡb̲ox ← { size →
240 n̲ot (1 r̲eshape 3) m̲atch s̲hape size ? @ p̲anic< "Scene box size must be xyz"
241 s ← ʰf̲inite f̲loat size
242 n̲ot '∧ r̲/ s > 0.0 ? @ p̲anic< "Scene box side lengths must be positive"
243 x ← 0.5 × f̲irst s
244 y ← 0.5 × f̲irst 1 d̲rop s
245 z ← f̲irst 2 d̲rop s
246 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
247 ˡc̲losed z ᵉˣe̲xtrude contour
248}
249
250⍝│ o_ctahedron radii: positive xyz axis radii, eight outward faces.
251ˡo̲ctahedron ← { radii →
252 n̲ot (1 r̲eshape 3) m̲atch s̲hape radii ? @ p̲anic< "Scene octahedron radii must be xyz"
253 r ← ʰf̲inite f̲loat radii
254 n̲ot '∧ r̲/ r > 0.0 ? @ p̲anic< "Scene octahedron radii must be positive"
255 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
256 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
257 ˡc̲losed 8 3 3 r̲eshape indices s̲elect vertices
258}
259
260⍝│ ((spheres a_pplySphereGroups groupids)_ transforms): rigid centers,
261⍝│ unchanged radii and row order. Scaling/shear/reflection matrices refuse.
262ˡa̲pplySphereGroups ← { spheres groupids transforms →
263 s ← ˡs̲pheres spheres
264 m ← ˡt̲ransforms transforms
265 ids ← (t̲ally m) ˡi̲ds groupids
266 n̲ot (t̲ally ids) = t̲ally s ? @ p̲anic< "Scene needs one group ID per sphere"
267 centers ← 3 t̲ake₂ s
268 moved ← '{ i →
269 group ← f̲irst i s̲elect ids
270 r̲avel (group s̲elect m) ᵛt̲ransform 1 3 r̲eshape i s̲elect centers
271 } m̲ap r̲ange t̲ally s
272 flat ← { @ → 0 = t̲ally moved ? 0 r̲eshape 0.0◆ d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at d̲isclose ⍵ } r̲/ moved } @
273 ˡ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)
274}
275
276⍝│ e_mitters data: E by 3 by 3 finite origin,u,v rectangles, nonzero area.
277ˡe̲mitters ← { data →
278 a ← ˡt̲riangles data
279 cross ← (2 s̲elect₂ a) ᵛc̲ross 3 s̲elect₂ a
280 n̲ot '∧ r̲/ (ᵛl̲ength cross) > 0.0 ? @ p̲anic< "Scene emitter spans must have nonzero area"
281 cosine ← (ᵛn̲ormalize 2 s̲elect₂ a) ᵛd̲ot ᵛn̲ormalize 3 s̲elect₂ a
282 n̲ot '∧ r̲/ (a̲bs cosine) ≤ 1e-10 ? @ p̲anic< "Scene emitter rectangle spans must be perpendicular"
283 a
284}
285
286⍝│ e_mitterAreas data: E positive Float rectangle areas in scene units squared.
287ˡe̲mitterAreas ← { data →
288 a ← ˡe̲mitters data
289 ʰf̲inite ᵛl̲ength (2 s̲elect₂ a) ᵛc̲ross 3 s̲elect₂ a
290}
291
292⍝│ e_mitterNormals data: E by 3 unit normals, u cross v, one-sided emission.
293ˡe̲mitterNormals ← { data →
294 a ← ˡe̲mitters data
295 ᵛn̲ormalize (2 s̲elect₂ a) ᵛc̲ross 3 s̲elect₂ a
296}
297
298⍝│ emitter e_mitterPoints square: one 3 by 3 rectangle and N by 2 unit
299⍝│ square samples to (N by 3 points, N Float uniform-area PDF 1/area).
300ˡe̲mitterPoints ← { emitter square →
301 n̲ot 3 3 m̲atch s̲hape emitter ? @ p̲anic< "Scene sampling requires one emitter rectangle"
302 a ← ˡe̲mitters 1 3 3 r̲eshape emitter
303 n ← t̲ally square
304 uv ← 2 ʰr̲ows square
305 n̲ot '∧ r̲/ r̲avel (uv ≥ 0.0) ∧ uv ≤ 1.0 ? @ p̲anic< "Scene emitter samples must lie in the unit square"
306 origin ← r̲avel 1 s̲elect₂ a
307 u ← r̲avel 2 s̲elect₂ a
308 v ← r̲avel 3 s̲elect₂ a
309 points ← (n r̲eshape 1.0) '× t̲able origin
310 offset ← ((1 s̲elect₂ uv) '× t̲able u) + (2 s̲elect₂ uv) '× t̲able v
311 pdf ← ʰf̲inite 1.0 ÷ f̲irst ˡe̲mitterAreas a
312 (ʰf̲inite points + offset, n r̲eshape pdf)
313}
314
315⍝│ matrix t_ransformEmitters data: proper rigid transform of rectangle
316⍝│ origins and spans, preserving area and descriptor order. Transform the
317⍝│ matching triangle geometry by the same matrix before transport preflight.
318ˡt̲ransformEmitters ← { matrix data →
319 a ← ˡe̲mitters data
320 n ← t̲ally a
321 origin ← 1 s̲elect₂ a
322 moved ← matrix ᵛt̲ransform origin
323 u ← (matrix ᵛt̲ransform origin + 2 s̲elect₂ a) − moved
324 v ← (matrix ᵛt̲ransform origin + 3 s̲elect₂ a) − moved
325 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
326 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 } @
327 ˡe̲mitters (n c̲at 3 3) r̲eshape flat
328}
p̲anic< expands to
(⎕P̲ANIC ("Scene lens requires nonnegative aperture and positive focus distance"))